2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
6
* The contents of this file are subject to the terms of either the GNU Lesser
7
* General Public License Version 2.1 only ("LGPL") or the Common Development and
8
* Distribution License ("CDDL")(collectively, the "License"). You may not use this
9
* file except in compliance with the License. You can obtain a copy of the CDDL at
10
* http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11
* http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12
* specific language governing permissions and limitations under the License. When
13
* distributing the software, include this License Header Notice in each file and
14
* include the full text of the License in the License file as well as the
17
* NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19
* For Covered Software in this distribution, this License shall be governed by the
20
* laws of the State of California (excluding conflict-of-law provisions).
21
* Any litigation relating to this License shall be subject to the jurisdiction of
22
* the Federal Courts of the Northern District of California and the state courts
23
* of the State of California, with venue lying in Santa Clara County, California.
27
* If you wish your version of this file to be governed by only the CDDL or only
28
* the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29
* include this software in this distribution under the [CDDL or LGPL Version 2.1]
30
* license." If you don't indicate a single choice of license, a recipient has the
31
* option to distribute your version of this file under either the CDDL or the LGPL
32
* Version 2.1, or to extend the choice of license to its licensees as provided
33
* above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34
* Version 2 license, then the option applies only if the new code is made subject
35
* to such option by the copyright holder.
40
#import "imi_option_keys.h"
41
#import "imi_session_wrapper.h"
42
#import "imi_plugin.h"
43
#import "SunPinyinApplicationDelegate.h"
44
#import <Sparkle/Sparkle.h>
48
void updateFactorySettings(NSUserDefaults* pref);
49
void updateQuanpinSettings(NSUserDefaults* pref);
50
void updateShuangpinSettings(NSUserDefaults* pref);
51
void updateSimplifiedChineseSettings(NSUserDefaults* pref);
52
void updateKeyProfileSettings(NSUserDefaults* pref);
54
@implementation SunPinyinApplicationDelegate
56
//this method is added so that our controllers can access the shared NSMenu.
62
-(CandidateWindow*)candiWin
67
-(void)updateUISettings
69
NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
71
//setting background color
72
NSData *data = [pref dataForKey:@"bgColor"];
73
NSColor *color = data? (NSColor*) [NSUnarchiver unarchiveObjectWithData:data]:
74
[NSColor orangeColor];
76
float alpha = [pref floatForKey:@"alpha"]/100.0;
77
NSColor *bgColor = [color colorWithAlphaComponent:alpha];
78
[_ftTxtField setBackgroundColor:color];
79
[_candiWin setBgColor:bgColor];
81
float radius = [pref floatForKey:@"radius"];
82
[_candiWin setRadius:radius];
84
data = [pref dataForKey:@"fgColor"];
85
color = data? (NSColor*) [NSUnarchiver unarchiveObjectWithData:data]:
87
[_ftTxtField setTextColor:color];
88
[_candiWin setFgColor:color];
90
data = [pref dataForKey:@"hlColor"];
91
color = data? (NSColor*) [NSUnarchiver unarchiveObjectWithData:data]:
93
[_candiWin setHlColor:color];
96
NSString *ftname = [pref stringForKey:@"fontName"];
97
float ftsize = [pref floatForKey:@"fontSize"];
98
NSFont *font = [NSFont fontWithName:ftname size:ftsize];
99
NSString* text = [NSString stringWithFormat:@"%@ %.0f",ftname,ftsize];
100
[_ftTxtField setFont:font];
101
[_ftTxtField setStringValue:text];
102
[_candiWin setFont:font];
105
-(void)updateIMESettings
107
NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
109
//setting full/half puncts and symbols
110
_inputChinesePuncts = [pref boolForKey:@"inputChinesePuncts"];
111
_inputFullSymbols = [pref boolForKey:@"inputFullSymbols"];
112
_switchingPolicy = (SwitchingPolicies) [pref integerForKey:@"switchingPolicy"];
113
_commitPolicy = (CommitPolicies) [pref integerForKey:@"commitPolicy"];
114
_usingUSKbLayout = [pref boolForKey:@"usingUSKbLayout"];
116
updateFactorySettings(pref);
117
updateQuanpinSettings(pref);
118
updateShuangpinSettings(pref);
119
updateSimplifiedChineseSettings(pref);
120
updateKeyProfileSettings(pref);
123
-(void)loadPreferences
125
[self updateUISettings];
126
[self updateIMESettings];
129
//add an awakeFromNib item so that we can set the action method. Note that
130
//any menuItems without an action will be disabled when displayed in the Text
134
[self loadPreferences];
136
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
137
[center addObserver:self
138
selector:@selector(preferencesChanged:)
139
name:NSUserDefaultsDidChangeNotification
142
[GrowlApplicationBridge setGrowlDelegate: self];
144
string res_path = [[[NSBundle mainBundle] resourcePath] UTF8String];
145
AOptionEventBus::instance().publishEvent(COptionEvent(SYSTEM_DATA_DIR, res_path));
147
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
150
string user_data_dir = [[paths objectAtIndex: 0] UTF8String];
151
user_data_dir.append ("/SunPinyin");
152
AOptionEventBus::instance().publishEvent(COptionEvent(USER_DATA_DIR, user_data_dir));
155
CIMIPluginManager& plugin_manager = AIMIPluginManager::instance();
156
plugin_manager.loadPlugin("cloudpinyin.py");
157
NSLog (@"%s", plugin_manager.getLastError().c_str());
161
-(void)preferencesChanged:(NSNotification *)notification
163
if ([[notification name] compare: @"NSUserDefaultsDidChangeNotification"])
166
[self loadPreferences];
169
//though we specified the showPrefPanel: in SunPinyinApplicationDelegate as the
170
//action receiver, the IMKInputController will actually receive the event.
171
-(IBAction)showPrefPanel:(id)sender
173
[_prefPanel makeKeyAndOrderFront:sender];
176
-(IBAction)showFontPanel:(id)sender
178
NSFont *oldFont = [_candiWin font];
179
NSFontPanel* ftDlg = [NSFontPanel sharedFontPanel];
180
[ftDlg setDelegate:self];
181
[ftDlg setPanelFont:oldFont isMultiple:NO];
182
[ftDlg makeKeyAndOrderFront:sender];
185
-(void) changeFont:(id)sender
187
NSFont *oldFont =[_candiWin font];
188
NSFont *newFont = [sender convertFont:oldFont];
190
NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
191
NSString* ftname = [newFont fontName];
192
float ftsize = [newFont pointSize];
193
[pref setObject:ftname forKey:@"fontName"];
194
[pref setFloat:ftsize forKey:@"fontSize"];
197
-(void) changeAttributes:(id)sender
199
NSColor *fgColor = [_candiWin fgColor];
200
NSDictionary *oldAttributes = [NSDictionary dictionaryWithObject:fgColor forKey:@"NSColor"];
201
NSDictionary *newAttributes = [sender convertAttributes: oldAttributes];
203
fgColor = [newAttributes objectForKey:@"NSColor"];
204
[_ftTxtField setTextColor:fgColor];
206
NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
207
NSData *data=[NSArchiver archivedDataWithRootObject:fgColor];
208
[pref setObject:data forKey:@"fgColor"];
211
-(IBAction)toggleChinesePuncts:(id)sender
213
NSMenuItem *item = [_menu itemWithTag:0];
214
_inputChinesePuncts = ![item state];
215
[[NSUserDefaults standardUserDefaults] setBool:_inputChinesePuncts
216
forKey:@"inputChinesePuncts"];
219
-(bool)inputChinesePuncts
221
return _inputChinesePuncts;
224
-(IBAction)toggleFullSymbols:(id)sender
226
NSMenuItem *item = [_menu itemWithTag:1];
227
_inputFullSymbols = ![item state];
228
[[NSUserDefaults standardUserDefaults] setBool:_inputFullSymbols
229
forKey:@"inputFullSymbols"];
232
-(bool)inputFullSymbols
234
return _inputFullSymbols;
237
-(SwitchingPolicies)switchingPolicy
239
return _switchingPolicy;
242
-(CommitPolicies)commitPolicy
244
return _commitPolicy;
247
-(bool)usingUSKbLayout
249
return _usingUSKbLayout;
256
[[NSNotificationCenter defaultCenter] removeObserver:self];
260
-(NSDictionary *)registrationDictionaryForGrowl
262
NSArray *notifications;
263
notifications = [NSArray arrayWithObject: @"SunPinyin"];
266
dict = [NSDictionary dictionaryWithObjectsAndKeys:
267
notifications, GROWL_NOTIFICATIONS_ALL,
268
notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
273
-(void)messageNotify:(NSString*)msg
275
[GrowlApplicationBridge notifyWithTitle: @"SunPinyin"
277
notificationName: @"SunPinyin"
278
iconData: [NSData dataWithData:[[NSImage imageNamed:@"SunPinyin"] TIFFRepresentation]]
284
@end //SunPinyinApplicationDelegate
286
void updateFactorySettings(NSUserDefaults* pref)
288
CSunpinyinSessionFactory& factory = CSunpinyinSessionFactory::getFactory();
289
COptionEventBus& event_bus = AOptionEventBus::instance();
291
int pinyin_scheme = [pref integerForKey: @"pinyinMode"];
292
factory.setPinyinScheme((CSunpinyinSessionFactory::EPyScheme)pinyin_scheme);
293
/* pinyin mode is not a shared configuration item, need to broadcast to session wrapper */
294
event_bus.publishEvent(COptionEvent(PINYIN_SCHEME, pinyin_scheme));
296
int candi_number = [pref integerForKey:@"candiNumbers"];
297
factory.setCandiWindowSize(candi_number);
298
/* candiWindowSize is not a shared configuration item */
299
event_bus.publishEvent(COptionEvent(CONFIG_GENERAL_PAGE_SIZE, candi_number));
302
void updateQuanpinSettings(NSUserDefaults* pref)
304
CQuanpinSchemePolicy& quanpin_policy = AQuanpinSchemePolicy::instance();
305
CShuangpinSchemePolicy& shuangpin_policy = AShuangpinSchemePolicy::instance();
307
bool simpler_initials_enabled = [pref boolForKey:@"Quanpin.Fuzzy.SimplerInitials"];
308
bool quanpin_fuzzy_enabled = [pref boolForKey: @"Quanpin.Fuzzy.Enabled"];
310
quanpin_policy.setFuzzyForwarding (quanpin_fuzzy_enabled, simpler_initials_enabled);
311
shuangpin_policy.setFuzzyForwarding (quanpin_fuzzy_enabled, simpler_initials_enabled);
313
quanpin_policy.clearFuzzyPinyinPairs();
314
shuangpin_policy.clearFuzzyPinyinPairs();
316
if (simpler_initials_enabled)
318
string_pairs fuzzy_pairs;
320
fuzzy_pairs.push_back(make_pair("z", "zh"));
321
fuzzy_pairs.push_back(make_pair("c", "ch"));
322
fuzzy_pairs.push_back(make_pair("s", "sh"));
324
quanpin_policy.setFuzzyPinyinPairs (fuzzy_pairs, false);
325
shuangpin_policy.setFuzzyPinyinPairs (fuzzy_pairs, false);
328
if (quanpin_fuzzy_enabled)
330
string_pairs fuzzy_pairs;
332
if ([pref boolForKey: @"Quanpin.Fuzzy.ZhiZi"]) fuzzy_pairs.push_back(make_pair("zh", "z"));
333
if ([pref boolForKey: @"Quanpin.Fuzzy.ChiCi"]) fuzzy_pairs.push_back(make_pair("ch", "c"));
334
if ([pref boolForKey: @"Quanpin.Fuzzy.ShiSi"]) fuzzy_pairs.push_back(make_pair("sh", "s"));
335
if ([pref boolForKey: @"Quanpin.Fuzzy.AnAng"]) fuzzy_pairs.push_back(make_pair("an", "ang"));
336
if ([pref boolForKey: @"Quanpin.Fuzzy.OnOng"]) fuzzy_pairs.push_back(make_pair("on", "ong"));
337
if ([pref boolForKey: @"Quanpin.Fuzzy.EnEng"]) fuzzy_pairs.push_back(make_pair("en", "eng"));
338
if ([pref boolForKey: @"Quanpin.Fuzzy.InIng"]) fuzzy_pairs.push_back(make_pair("in", "ing"));
339
if ([pref boolForKey: @"Quanpin.Fuzzy.EngOng"]) fuzzy_pairs.push_back(make_pair("eng", "ong"));
340
if ([pref boolForKey: @"Quanpin.Fuzzy.IanIang"]) fuzzy_pairs.push_back(make_pair("ian", "iang"));
341
if ([pref boolForKey: @"Quanpin.Fuzzy.UanUang"]) fuzzy_pairs.push_back(make_pair("uan", "uang"));
342
if ([pref boolForKey: @"Quanpin.Fuzzy.NeLe"]) fuzzy_pairs.push_back(make_pair("n", "l"));
343
if ([pref boolForKey: @"Quanpin.Fuzzy.FoHe"]) fuzzy_pairs.push_back(make_pair("f", "h"));
344
if ([pref boolForKey: @"Quanpin.Fuzzy.LeRi"]) fuzzy_pairs.push_back(make_pair("l", "r"));
345
if ([pref boolForKey: @"Quanpin.Fuzzy.KeGe"]) fuzzy_pairs.push_back(make_pair("k", "g"));
347
quanpin_policy.setFuzzyPinyinPairs (fuzzy_pairs);
348
shuangpin_policy.setFuzzyPinyinPairs (fuzzy_pairs);
351
bool quanpin_autocorrecting_enabled = [pref boolForKey: @"Quanpin.AutoCorrecting.Enabled"];
352
quanpin_policy.setAutoCorrecting (quanpin_autocorrecting_enabled);
354
string_pairs correcting_pairs;
356
if ([pref boolForKey: @"Quanpin.AutoCorrecting.GnNg"]) correcting_pairs.push_back(make_pair("gn", "ng"));
357
if ([pref boolForKey: @"Quanpin.AutoCorrecting.UenUn"]) correcting_pairs.push_back(make_pair("uen", "un"));
358
if ([pref boolForKey: @"Quanpin.AutoCorrecting.ImgIng"]) correcting_pairs.push_back(make_pair("img", "ing"));
359
if ([pref boolForKey: @"Quanpin.AutoCorrecting.IouIu"]) correcting_pairs.push_back(make_pair("iou", "iu"));
360
if ([pref boolForKey: @"Quanpin.AutoCorrecting.UeiUi"]) correcting_pairs.push_back(make_pair("uei", "ui"));
362
quanpin_policy.setAutoCorrectionPairs (correcting_pairs);
364
bool quanpin_fuzzy_segments_enabled = [pref boolForKey:@"Quanpin.FuzzySegs.Enabled"];
365
bool quanpin_inner_fuzzy_segments_enabled = [pref boolForKey:@"Quanpin.FuzzySegs.InnerFuzzy.Enabled"];
366
quanpin_policy.setFuzzySegmentation (quanpin_fuzzy_segments_enabled);
367
quanpin_policy.setInnerFuzzySegmentation (quanpin_inner_fuzzy_segments_enabled);
370
void updateShuangpinSettings(NSUserDefaults* pref)
372
/* Shuangpin type is not a shared configuration item */
373
COptionEventBus& event_bus = AOptionEventBus::instance();
374
int shuangpin_type = [pref integerForKey: @"Shuangpin.Scheme"];
375
event_bus.publishEvent(COptionEvent(SHUANGPIN_TYPE, shuangpin_type));
378
void updateSimplifiedChineseSettings(NSUserDefaults* pref)
380
CSimplifiedChinesePolicy& lang_policy = ASimplifiedChinesePolicy::instance();
382
lang_policy.enableFullSymbol ([pref boolForKey:@"inputFullSymbols"]);
383
lang_policy.enableFullPunct ([pref boolForKey:@"inputChinesePuncts"]);
385
NSString *charset = [pref stringForKey:@"charset"];
386
int charset_level = [charset isEqualToString:@"GB18030"]? 2:
387
[charset isEqualToString:@"GBK"]? 1: 0;
389
/* charset is not a shared configuration item */
390
AOptionEventBus::instance().publishEvent (COptionEvent(CONFIG_GENERAL_CHARSET_LEVEL, charset_level));
392
string_pairs punct_pairs;
393
NSArray* punct_mappings = [pref arrayForKey: @"punctationMappings"];
394
for (int i=0; i<[punct_mappings count]; ++i)
396
NSDictionary *tmp = [punct_mappings objectAtIndex:i];
397
NSNumber *enabled = [tmp objectForKey:@"enabled"];
398
NSString *ascii = [tmp objectForKey:@"ASCII"];
399
if ([ascii isEqualToString:@"SPACE"]) ascii = @" ";
401
NSString *mapped_string = [tmp objectForKey:@"mappedString"];
403
if ([enabled boolValue] && [mapped_string UTF8String])
404
punct_pairs.push_back (make_pair([ascii UTF8String], [mapped_string UTF8String]));
406
lang_policy.setPunctMapping(punct_pairs);
409
void updateKeyProfileSettings(NSUserDefaults* pref)
411
/* hotkey profile settings are not shared configuration items */
412
COptionEventBus& event_bus = AOptionEventBus::instance();
414
bool paging_by_minus_equals = [pref boolForKey:@"pagingByMinusAndEqual"];
415
event_bus.publishEvent (COptionEvent(CONFIG_KEYBOARD_PAGE_MINUS, paging_by_minus_equals));
417
bool paging_by_brackets = [pref boolForKey:@"pagingByBrackets"];
418
event_bus.publishEvent (COptionEvent(CONFIG_KEYBOARD_PAGE_BRACKET, paging_by_brackets));
420
bool paging_by_comma_period = [pref boolForKey:@"pagingByCommaAndDot"];
421
event_bus.publishEvent (COptionEvent(CONFIG_KEYBOARD_PAGE_COMMA, paging_by_comma_period));
423
bool paging_by_arrows = [pref boolForKey:@"pagingByArrowUpAndDown"];
424
event_bus.publishEvent (COptionEvent(CONFIG_KEYBOARD_PAGE_ARROWS, paging_by_arrows));
426
bool cancel_on_backspace = [pref boolForKey:@"cancelSelectionOnBackspace"];
427
event_bus.publishEvent(COptionEvent(CONFIG_KEYBOARD_MISC_CANCELONBSP, cancel_on_backspace));
429
// store the session specific configurations
430
CSessionConfigStore::instance().m_paging_by_minus_equals = paging_by_minus_equals;
431
CSessionConfigStore::instance().m_paging_by_brackets = paging_by_brackets;
432
CSessionConfigStore::instance().m_paging_by_comma_period = paging_by_comma_period;
433
CSessionConfigStore::instance().m_paging_by_arrows = paging_by_arrows;
434
CSessionConfigStore::instance().m_cancel_on_backspace = cancel_on_backspace;