2 // RTKPreferencesController.h
5 // Copyright (c) 2005 A. Karl Keller (http://karlk.net)
7 // This code is open-source, free software, made available without warranty under
8 // the terms of the GNU General Public License, either version 2 or later (see
9 // http://www.gnu.org/licenses/gpl.html or included copy); as such, it may be
10 // redistributed and/or modified in accordance with that document.
14 #import "RTKPreferencesController.h"
15 #import "RTKConvertor.h"
16 extern id RTKSharedConvertor;
18 @implementation RTKPreferencesController
22 if(self = [super init]) {
23 inputSystemDict = [[NSMutableDictionary alloc] init];
24 scriptSystemDict = [[NSMutableDictionary alloc] init];
25 encodingSystemDict = [[NSMutableDictionary alloc] init];
27 definitionDir = [[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/active_definitions"] retain];
34 [inputSystemDict release];
35 [scriptSystemDict release];
36 [encodingSystemDict release];
37 [definitionDir release];
42 -(void)loadDefFilesAt:(NSString *)defLocation
43 ofType:(NSString *)extension
44 intoPopUpButton:(NSPopUpButton *)popUp
45 andDict:(NSMutableDictionary *)dict
48 NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
49 enumeratorAtPath:defLocation];
51 [popUp removeAllItems];
52 [dict removeAllObjects];
54 while (file = [enumerator nextObject]) {
55 if ([[file pathExtension] isEqualToString:extension]) {
56 [dict setObject:[definitionDir stringByAppendingPathComponent:file]
57 forKey:[[file lastPathComponent] stringByDeletingPathExtension]];
58 [popUp addItemWithTitle:[[file lastPathComponent] stringByDeletingPathExtension]];
63 -(void)loadDefinitions
65 NSUserDefaults * d = [NSUserDefaults standardUserDefaults];
67 [self loadDefFilesAt:definitionDir
69 intoPopUpButton:inputPopUpButton
70 andDict:inputSystemDict];
72 [self loadDefFilesAt:definitionDir
74 intoPopUpButton:scriptPopUpButton
75 andDict:scriptSystemDict];
77 [self loadDefFilesAt:definitionDir
79 intoPopUpButton:encodingPopUpButton
80 andDict:encodingSystemDict];
82 [inputPopUpButton selectItemWithTitle:[d objectForKey:@"RTKInputSystem"]];
83 [scriptPopUpButton selectItemWithTitle:[d objectForKey:@"RTKScriptSystem"]];
84 [encodingPopUpButton selectItemWithTitle:[d objectForKey:@"RTKEncodingSystem"]];
86 [RTKSharedConvertor resetDefinitions];
89 - (void)windowDidBecomeKey:(NSNotification *)aNotification
94 - (void)updateFontTextField:(NSTextField *)field
95 withFontName:(NSString *)name
98 [field setStringValue:[NSString stringWithFormat:@"%@ - %g", name, [size floatValue]]];
103 NSUserDefaults * d = [NSUserDefaults standardUserDefaults];
105 NSString * plainTextDelimiter = [d objectForKey:@"RTKPlainTextDelimiter"];
106 NSString * plainTextReturnCharacter = [d objectForKey:@"RTKPlainTextReturnCharacter"];
108 if(plainTextDelimiter)
109 [delimiterTextField setStringValue:plainTextDelimiter];
111 if(plainTextReturnCharacter)
112 [returnCharacterTextField setStringValue:plainTextReturnCharacter];
113 if(externalConvertorTextField)
114 [externalConvertorTextField setStringValue:[d objectForKey:@"RTKExternalConvertor"]];
116 [zvxButton setState:[d boolForKey:@"RTKZVXSubstitution"]];
118 [transliterationOnButton setState:[d boolForKey:@"RTKTransliterationOn"]];
120 [self updateFontTextField:committeeFontTextField
121 withFontName:(NSString *)[d valueForKey:@"RTKCommitteeFontName"]
122 size:(NSNumber *)[d valueForKey:@"RTKCommitteeFontSize"]];
123 [self updateFontTextField:scriptFontTextField
124 withFontName:(NSString *)[d valueForKey:@"RTKScriptFontName"]
125 size:(NSNumber *)[d valueForKey:@"RTKScriptFontSize"]];
126 [self updateFontTextField:romanFontTextField
127 withFontName:(NSString *)[d valueForKey:@"RTKRomanFontName"]
128 size:(NSNumber *)[d valueForKey:@"RTKRomanFontSize"]];
129 [self updateFontTextField:backTranslationFontTextField
130 withFontName:(NSString *)[d valueForKey:@"RTKBackTranslationFontName"]
131 size:(NSNumber *)[d valueForKey:@"RTKBackTranslationFontSize"]];
132 [self updateFontTextField:notesFontTextField
133 withFontName:(NSString *)[d valueForKey:@"RTKNotesFontName"]
134 size:(NSNumber *)[d valueForKey:@"RTKNotesFontSize"]];
135 [self updateFontTextField:checkingFontTextField
136 withFontName:(NSString *)[d valueForKey:@"RTKCheckingFontName"]
137 size:(NSNumber *)[d valueForKey:@"RTKCheckingFontSize"]];
138 [self updateFontTextField:publishedFontTextField
139 withFontName:(NSString *)[d valueForKey:@"RTKPublishedFontName"]
140 size:(NSNumber *)[d valueForKey:@"RTKPublishedFontSize"]];
143 NSString * transcriptionType = [d valueForKey:@"RTKTranscriptionType"];
144 if([transcriptionType isEqualToString:@"No Transcription"]) {
145 [convertorMatrix setState:1 atRow:0 column:0];
146 } else if([transcriptionType isEqualToString:@"External Transcription"]) {
147 [convertorMatrix setState:1 atRow:1 column:0];
148 } else if([transcriptionType isEqualToString:@"RTK Transcription"]) {
149 [convertorMatrix setState:1 atRow:2 column:0];
152 [self loadDefinitions];
155 - (IBAction)substituteZVXChanged:(id)sender
157 [[NSUserDefaults standardUserDefaults] setBool:[sender state]
158 forKey:@"RTKZVXSubstitution"];
159 [[NSNotificationCenter defaultCenter] postNotification:
160 [NSNotification notificationWithName:@"RTKZVXSubstitutionChanged" object:nil]];
163 - (IBAction)transliterationOnChanged:(id)sender;
165 [[NSUserDefaults standardUserDefaults] setBool:[sender state]
166 forKey:@"RTKTransliterationOn"];
167 [[NSNotificationCenter defaultCenter] postNotification:
168 [NSNotification notificationWithName:@"RTKTransliterationOnChanged" object:nil]];
171 - (void)controlTextDidChange:(NSNotification *)aNotification
173 NSUserDefaults * d = [NSUserDefaults standardUserDefaults];
174 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
176 NSTextField * object = [aNotification object];
177 if(object == delimiterTextField) {
178 [d setObject:[delimiterTextField stringValue]
179 forKey:@"RTKPlainTextDelimiter"];
180 } else if(object == returnCharacterTextField) {
181 [d setObject:[returnCharacterTextField stringValue]
182 forKey:@"RTKPlainTextReturnCharacter"];
186 - (IBAction)setExternalConvertor:(id)sender
188 NSOpenPanel * op = [NSOpenPanel openPanel];
190 [op setCanChooseDirectories:NO];
191 [op setCanChooseFiles:YES];
192 [op setAllowsMultipleSelection:NO];
193 [op setAllowsOtherFileTypes:YES];
195 [op beginSheetForDirectory:[@"~" stringByExpandingTildeInPath]
198 modalForWindow:preferencesWindow
200 didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:)
205 - (void)openPanelDidEnd:(NSWindow *)sheet
206 returnCode:(int)returnCode
207 contextInfo:(void *)contextInfo
209 NSUserDefaults * d = [NSUserDefaults standardUserDefaults];
210 NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
212 NSOpenPanel * op = (NSOpenPanel *)contextInfo;
213 NSString * filename = [op filename];
215 if(returnCode == NSOKButton) {
216 [d setObject:filename
217 forKey:@"RTKExternalConvertor"];
218 [nc postNotification:
219 [NSNotification notificationWithName:@"RTKExternalConvertorChanged" object:nil]];
220 NSLog(@"%@", [d objectForKey:@"RTKExternalConvertor"]);
221 [externalConvertorTextField setStringValue:filename];
223 NSLog(@"%@", [[self parametersForExternalConvertor:filename] description]);
228 - (NSDictionary *)parametersForExternalConvertor:(NSString *)convertor
230 //[parameters setObject:@"test" forKey:@"this is a"];
232 standardOutputPipe = [NSPipe pipe];
233 standardOutputFile = [standardOutputPipe fileHandleForReading];
235 NSTask * convertorTask = [[NSTask alloc] init];
236 [convertorTask setLaunchPath:convertor];
237 [convertorTask setStandardOutput:standardOutputPipe];
238 [convertorTask setArguments:[NSArray arrayWithObjects:@"--parameters", nil]];
239 [convertorTask launch];
241 NSData * outputData = [standardOutputFile readDataToEndOfFile];
242 NSString * outputString = [NSString stringWithUTF8String:[outputData bytes]];
244 NSDictionary * parameters = [convertor propertyList];
249 - (void) searchAction: (id) sender
255 file = [sender stringValue];
256 args = [NSArray arrayWithObjects: NSHomeDirectory(), @"-name", file, @"-print", nil];
258 ASSIGN(pipe, [NSPipe pipe]);
260 [task setLaunchPath: @"/usr/bin/find"];
261 [task setArguments: args];
262 [task setStandardOutput: pipe];
263 fileHandle = [pipe fileHandleForReading];
264 [[NSNotificationCenter defaultCenter] addObserver: self
265 selector: @selector(taskEnded:)
266 name: NSTaskDidTerminateNotification
269 [[NSNotificationCenter defaultCenter] addObserver: self
270 selector: @selector(readData:)
271 name: NSFileHandleReadCompletionNotification
273 [fileHandle readInBackgroundAndNotify];
278 - (IBAction)inputSystemChanged:(id)sender
280 [[NSUserDefaults standardUserDefaults] setObject:[sender titleOfSelectedItem]
281 forKey:@"RTKInputSystem"];
282 [[NSNotificationCenter defaultCenter] postNotification:
283 [NSNotification notificationWithName:@"RTKDefinitionsChanged" object:nil]];
286 - (IBAction)scriptSystemChanged:(id)sender
288 [[NSUserDefaults standardUserDefaults] setObject:[sender titleOfSelectedItem]
289 forKey:@"RTKScriptSystem"];
290 [[NSNotificationCenter defaultCenter] postNotification:
291 [NSNotification notificationWithName:@"RTKDefinitionsChanged" object:nil]];
294 - (IBAction)encodingSystemChanged:(id)sender
296 [[NSUserDefaults standardUserDefaults] setObject:[sender titleOfSelectedItem]
297 forKey:@"RTKEncodingSystem"];
298 [[NSNotificationCenter defaultCenter] postNotification:
299 [NSNotification notificationWithName:@"RTKDefinitionsChanged" object:nil]];
302 - (IBAction)webUpdateDefinitionsClicked:(id)sender
305 NSLog(@"downloaderTask still set to %@", downloaderTask);
307 downloaderTask = [[NSTask alloc] init];
308 [downloaderTask setLaunchPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/get_definitions.command"]];
309 [downloaderTask setCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
310 [downloaderTask launch];
312 [webUpdateDefinitionsButton setHidden:YES];
313 [webUpdateDefinitionsProgressIndicator setUsesThreadedAnimation:YES];
314 [webUpdateDefinitionsProgressIndicator setHidden:NO];
315 [webUpdateDefinitionsProgressIndicator setIndeterminate:YES];
316 [webUpdateDefinitionsProgressIndicator startAnimation:nil];
318 [[NSNotificationCenter defaultCenter] addObserver:self
319 selector:@selector(finishedDownload:)
320 name:NSTaskDidTerminateNotification
324 - (void)finishedDownload:(NSNotification *)aNotification
326 [webUpdateDefinitionsButton setHidden:NO];
327 [webUpdateDefinitionsProgressIndicator setHidden:YES];
328 [webUpdateDefinitionsProgressIndicator stopAnimation:nil];
329 [downloaderTask release];
330 downloaderTask = nil;
332 [[NSNotificationCenter defaultCenter] removeObserver:self
333 name:NSTaskDidTerminateNotification
335 [self loadDefinitions];
337 [[NSNotificationCenter defaultCenter] postNotification:
338 [NSNotification notificationWithName:@"RTKDefinitionsChanged" object:nil]];
341 // Font selection methods
343 static NSString * fontNameToChange = nil;
344 static NSString * fontSizeToChange = nil;
346 - (IBAction)changeFont:(id)sender
348 NSUserDefaults * d = [NSUserDefaults standardUserDefaults];
350 if(sender == [NSFontManager sharedFontManager]) {
351 NSFont * font = [NSFont fontWithName:(NSString *)[d valueForKey:fontNameToChange]
352 size:[(NSNumber *)[d valueForKey:fontSizeToChange] floatValue]];
353 font = [sender convertFont:font];
354 [d setValue:[font displayName] forKey:fontNameToChange];
355 [d setValue:[NSNumber numberWithFloat:[font pointSize]] forKey:fontSizeToChange];
357 [[NSNotificationCenter defaultCenter] postNotification:
358 [NSNotification notificationWithName:@"RTKFontsChanged" object:nil]];
360 } if(sender == committeeFontButton) {
361 fontNameToChange = @"RTKCommitteeFontName";
362 fontSizeToChange = @"RTKCommitteeFontSize";
363 } if(sender == scriptFontButton) {
364 fontNameToChange = @"RTKScriptFontName";
365 fontSizeToChange = @"RTKScriptFontSize";
366 } else if(sender == romanFontButton) {
367 fontNameToChange = @"RTKRomanFontName";
368 fontSizeToChange = @"RTKRomanFontSize";
369 } else if(sender == backTranslationFontButton) {
370 fontNameToChange = @"RTKBackTranslationFontName";
371 fontSizeToChange = @"RTKBackTranslationFontSize";
372 } else if(sender == notesFontButton) {
373 fontNameToChange = @"RTKNotesFontName";
374 fontSizeToChange = @"RTKNotesFontSize";
375 } else if(sender == checkingFontButton) {
376 fontNameToChange = @"RTKCheckingFontName";
377 fontSizeToChange = @"RTKCheckingFontSize";
378 } else if(sender == publishedFontButton) {
379 fontNameToChange = @"RTKPublishedFontName";
380 fontSizeToChange = @"RTKPublishedFontSize";
383 [preferencesWindow makeFirstResponder:preferencesWindow];
385 NSFont * font = [NSFont fontWithName:(NSString *)[d valueForKey:fontNameToChange]
386 size:[(NSNumber *)[d valueForKey:fontSizeToChange] floatValue]];
387 [[NSFontManager sharedFontManager] setSelectedFont:font isMultiple:NO];
388 [[NSFontManager sharedFontManager] orderFrontFontPanel:self];
391 - (IBAction)convertorSwitched:(id)sender
393 NSUserDefaults * d = [NSUserDefaults standardUserDefaults];
394 int selectedRow = [sender selectedRow];
396 if(selectedRow == 0) {
397 [d setValue:@"No Transcription" forKey:@"RTKTranscriptionType"];
398 } else if(selectedRow == 1) {
399 [d setValue:@"External Transcription" forKey:@"RTKTranscriptionType"];
400 } else if(selectedRow == 2) {
401 [d setValue:@"RTK Transcription" forKey:@"RTKTranscriptionType"];
404 [[NSNotificationCenter defaultCenter] postNotification:
405 [NSNotification notificationWithName:@"RTKTranscriptionTypeChanged" object:nil]];