/usr/share/doc/libdbuskit-dev/examples/Apertium/ApertiumServer.m is in libdbuskit-dev 0.1.1-2+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | /* Singleton to provide the "Translate with Apertium" service.
*
* Copyright (C) 2010 Free Software Foundation, Inc.
*
* Written by: Niels Grewe
* Created: July 2010
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111
* USA.
*/
#import "ApertiumController.h"
#import "ApertiumServer.h"
#import "ApertiumTranslator.h"
static ApertiumServer* sharedServer;
@implementation ApertiumServer
+ (void) initialize
{
if (self == [ApertiumServer class])
{
sharedServer = [[self alloc] init];
}
}
+ allocWithZone: (NSZone*)aZone
{
if (nil == sharedServer)
{
return [super allocWithZone: aZone];
}
return nil;
}
+ (ApertiumServer*)sharedApertiumServer
{
return sharedServer;
}
- (void)unscheduleShutdown
{
if (shutdownTimer)
{
[shutdownTimer invalidate];
shutdownTimer = nil;
}
}
- (void)scheduleShutdown
{
[self unscheduleShutdown];
shutdownTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0
target: NSApp
selector: @selector(terminate:)
userInfo: nil
repeats: NO];
}
- (id)init
{
if (nil == (self = [super init]))
{
return nil;
}
[self scheduleShutdown];
return self;
}
- (void)translate: (NSPasteboard *)pboard
userData: (NSString*)userData
error: (NSString**)error
{
NSArray *types = [pboard types];
NSString *source = nil;
ApertiumTranslator *translator = [[ApertiumTranslator alloc] init];
ApertiumController *ctrl = nil;
NSWindow *window = nil;
NSInteger languageSelectionState = 0;
[self unscheduleShutdown];
if (NO == [types containsObject: NSStringPboardType])
{
*error = _(@"Can only translate string types from pasteboard.");
[self scheduleShutdown];
return;
}
source = [pboard stringForType: NSStringPboardType];
[translator setStringToTranslate: source];
ctrl = [[ApertiumController alloc] initWithTranslator: translator];
window = [ctrl window];
if (nil == window)
{
*error = _(@"Could not create language selection panel");
[ctrl release];
[self scheduleShutdown];
return;
}
languageSelectionState = [NSApp runModalForWindow: window];
[ctrl release];
if (NSRunAbortedResponse == languageSelectionState)
{
*error = _(@"Could not translate.");
[self scheduleShutdown];
return;
}
else
{
NSString *translation = nil;
NS_DURING
{
translation = [translator translatedString];
}
NS_HANDLER
{
*error = [NSString stringWithFormat: @"Exception during translation: %@",
localException];
translation = nil;
}
NS_ENDHANDLER
if (nil == translation)
{
if (nil == *error)
{
*error = _(@"Could not translate");
}
return;
}
[pboard setString: translation
forType: NSStringPboardType];
}
[self scheduleShutdown];
}
- (NSUInteger) retainCount
{
return UINT_MAX;
}
- (void) release
{
//Ignore, it's a singleton;
}
- (id) autorelease
{
return self;
}
- (id) retain
{
return self;
}
@end
|