Home | History | Annotate | Download | only in macos
      1  157  yongsun /*
      2  157  yongsun  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
      3  157  yongsun  *
      4  177  yongsun  * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
      5  157  yongsun  *
      6  157  yongsun  * The contents of this file are subject to the terms of either the GNU Lesser
      7  157  yongsun  * General Public License Version 2.1 only ("LGPL") or the Common Development and
      8  157  yongsun  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
      9  157  yongsun  * file except in compliance with the License. You can obtain a copy of the CDDL at
     10  157  yongsun  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
     11  157  yongsun  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
     12  157  yongsun  * specific language governing permissions and limitations under the License. When
     13  157  yongsun  * distributing the software, include this License Header Notice in each file and
     14  157  yongsun  * include the full text of the License in the License file as well as the
     15  157  yongsun  * following notice:
     16  157  yongsun  *
     17  157  yongsun  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
     18  157  yongsun  * (CDDL)
     19  157  yongsun  * For Covered Software in this distribution, this License shall be governed by the
     20  157  yongsun  * laws of the State of California (excluding conflict-of-law provisions).
     21  157  yongsun  * Any litigation relating to this License shall be subject to the jurisdiction of
     22  157  yongsun  * the Federal Courts of the Northern District of California and the state courts
     23  157  yongsun  * of the State of California, with venue lying in Santa Clara County, California.
     24  157  yongsun  *
     25  157  yongsun  * Contributor(s):
     26  157  yongsun  *
     27  157  yongsun  * If you wish your version of this file to be governed by only the CDDL or only
     28  157  yongsun  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
     29  157  yongsun  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
     30  157  yongsun  * license." If you don't indicate a single choice of license, a recipient has the
     31  157  yongsun  * option to distribute your version of this file under either the CDDL or the LGPL
     32  157  yongsun  * Version 2.1, or to extend the choice of license to its licensees as provided
     33  157  yongsun  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
     34  157  yongsun  * Version 2 license, then the option applies only if the new code is made subject
     35  157  yongsun  * to such option by the copyright holder.
     36  157  yongsun  */
     37  157  yongsun 
     38  163  yongsun #include <sys/stat.h>
     39  163  yongsun #import "imi_view.h"
     40  157  yongsun #import "SunPinyinApplicationDelegate.h"
     41  302  gzjjgod #import <Sparkle/Sparkle.h>
     42  157  yongsun 
     43  174  yongsun static NSString* get_history_path ();
     44  174  yongsun static bool save_history (CICHistory*);
     45  174  yongsun static bool load_history (CICHistory*);
     46  174  yongsun static bool load_preferences (CSunpinyinOptions*);
     47  174  yongsun 
     48  157  yongsun @implementation SunPinyinApplicationDelegate
     49  157  yongsun 
     50  157  yongsun //this method is added so that our controllers can access the shared NSMenu.
     51  157  yongsun -(NSMenu*)menu
     52  157  yongsun {
     53  157  yongsun     return _menu;
     54  174  yongsun }
     55  174  yongsun 
     56  174  yongsun -(CandidateWindow*)candiWin
     57  174  yongsun {
     58  174  yongsun     return _candiWin;
     59  157  yongsun }
     60  157  yongsun 
     61  157  yongsun //this method is to return a CIMIData instance which loads the language model and lexicon file
     62  157  yongsun -(CIMIData*)sysData
     63  157  yongsun {
     64  157  yongsun     if (_data == NULL) {
     65  157  yongsun         _data = new CIMIData();
     66  157  yongsun         const char * res_path = [[[NSBundle mainBundle] resourcePath] UTF8String];
     67  157  yongsun         char slm_path[512], pydict_path[512];
     68  157  yongsun         snprintf (slm_path, sizeof(slm_path), "%s/%s", res_path, "lm_sc.t3g");
     69  157  yongsun         snprintf (pydict_path, sizeof(pydict_path), "%s/%s", res_path, "pydict_sc.bin");
     70  272  yongsun         if (_data->loadResource(slm_path, pydict_path))
     71  272  yongsun             return _data;
     72  272  yongsun 
     73  272  yongsun         delete _data;
     74  272  yongsun         _data = NULL;
     75  157  yongsun     }
     76  272  yongsun 
     77  157  yongsun     return _data;
     78  157  yongsun }
     79  174  yongsun 
     80  174  yongsun -(CBigramHistory*)history
     81  174  yongsun {
     82  174  yongsun     if (_history == nil) {
     83  174  yongsun         _history = new CBigramHistory();
     84  174  yongsun         load_history(_history);
     85  174  yongsun     }
     86  174  yongsun     return _history;
     87  174  yongsun }
     88  174  yongsun 
     89  174  yongsun -(void)saveHistory
     90  174  yongsun {
     91  174  yongsun     save_history(_history);
     92  174  yongsun }
     93  174  yongsun 
     94  174  yongsun -(CSunpinyinOptions*)preferences
     95  174  yongsun {
     96  174  yongsun     if (_pref == nil) {
     97  174  yongsun         _pref = new CSunpinyinOptions();
     98  174  yongsun         load_preferences (_pref);
     99  174  yongsun     }
    100  174  yongsun 
    101  174  yongsun     return _pref;
    102  174  yongsun }
    103  174  yongsun 
    104  174  yongsun -(void)preferencesChanged:(NSNotification *)notification
    105  190  yongsun {
    106  176  yongsun     if (_pref)
    107  176  yongsun         load_preferences (_pref);
    108  190  yongsun 
    109  176  yongsun     NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
    110  183  yongsun 
    111  190  yongsun     //setting full/half puncts and symbols
    112  274  yongsun     _inputChinesePuncts = [pref boolForKey:@"inputChinesePuncts"];
    113  274  yongsun     _inputFullSymbols   = [pref boolForKey:@"inputFullSymbols"];
    114  274  yongsun     _switchingPolicy    = (SwitchingPolicies) [pref integerForKey:@"switchingPolicy"];
    115  274  yongsun     _usingUSKbLayout    = [pref boolForKey:@"usingUSKbLayout"];
    116  190  yongsun 
    117  183  yongsun     //setting background color
    118  176  yongsun     NSData *data = [pref dataForKey:@"bgColor"];
    119  183  yongsun     NSColor *color = data? (NSColor*) [NSUnarchiver unarchiveObjectWithData:data]:
    120  183  yongsun                            [NSColor orangeColor];
    121  183  yongsun 
    122  183  yongsun     float alpha = [pref floatForKey:@"alpha"]/100.0;
    123  183  yongsun     NSColor *bgColor = [color colorWithAlphaComponent:alpha];
    124  183  yongsun     [_ftTxtField setBackgroundColor:color];
    125  183  yongsun     [_candiWin setBgColor:bgColor];
    126  183  yongsun 
    127  266  yongsun     data = [pref dataForKey:@"fgColor"];
    128  266  yongsun     color = data? (NSColor*) [NSUnarchiver unarchiveObjectWithData:data]:
    129  266  yongsun                   [NSColor whiteColor];
    130  266  yongsun     [_ftTxtField setTextColor:color];
    131  266  yongsun     [_candiWin setFgColor:color];
    132  266  yongsun 
    133  266  yongsun     data = [pref dataForKey:@"hlColor"];
    134  266  yongsun     color = data? (NSColor*) [NSUnarchiver unarchiveObjectWithData:data]:
    135  266  yongsun                   [NSColor blueColor];
    136  266  yongsun     [_candiWin setHlColor:color];
    137  266  yongsun 
    138  183  yongsun     //setting font
    139  176  yongsun     NSString *ftname = [pref stringForKey:@"fontName"];
    140  176  yongsun     float ftsize = [pref floatForKey:@"fontSize"];
    141  176  yongsun     NSFont *font = [NSFont fontWithName:ftname size:ftsize];
    142  176  yongsun     NSString* text = [NSString stringWithFormat:@"%@ %.0f",ftname,ftsize];
    143  183  yongsun     [_ftTxtField setFont:font];
    144  176  yongsun     [_ftTxtField setStringValue:text];
    145  176  yongsun     [_candiWin setFont:font];
    146  174  yongsun }
    147  174  yongsun 
    148  176  yongsun //add an awakeFromNib item so that we can set the action method.  Note that
    149  176  yongsun //any menuItems without an action will be disabled when displayed in the Text
    150  176  yongsun //Input Menud.
    151  174  yongsun -(void)awakeFromNib
    152  174  yongsun {
    153  174  yongsun     NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    154  174  yongsun     [center addObserver:self
    155  174  yongsun             selector:@selector(preferencesChanged:)
    156  174  yongsun             name:NSUserDefaultsDidChangeNotification
    157  174  yongsun             object:nil];
    158  274  yongsun 
    159  274  yongsun     [GrowlApplicationBridge setGrowlDelegate: self];
    160  176  yongsun }
    161  176  yongsun 
    162  176  yongsun //though we specified the showPrefPanel: in SunPinyinApplicationDelegate as the
    163  176  yongsun //action receiver, the IMKInputController will actually receive the event.
    164  176  yongsun -(IBAction)showPrefPanel:(id)sender
    165  176  yongsun {
    166  176  yongsun     [_prefPanel makeKeyAndOrderFront:sender];
    167  176  yongsun }
    168  176  yongsun 
    169  176  yongsun -(IBAction)showFontPanel:(id)sender
    170  176  yongsun {
    171  176  yongsun     NSFont *oldFont = [_candiWin font];
    172  176  yongsun     NSFontPanel* ftDlg = [NSFontPanel sharedFontPanel];
    173  176  yongsun     [ftDlg setDelegate:self];
    174  176  yongsun     [ftDlg setPanelFont:oldFont isMultiple:NO];
    175  176  yongsun     [ftDlg makeKeyAndOrderFront:sender];
    176  302  gzjjgod }
    177  302  gzjjgod 
    178  302  gzjjgod -(IBAction)checkForUpdate:(id)sender
    179  302  gzjjgod {
    180  302  gzjjgod     SUUpdater *updater = [SUUpdater sharedUpdater];
    181  302  gzjjgod 
    182  302  gzjjgod     [updater checkForUpdates: sender];
    183  176  yongsun }
    184  176  yongsun 
    185  176  yongsun -(void) changeFont:(id)sender
    186  176  yongsun {
    187  176  yongsun     NSFont *oldFont =[_candiWin font];
    188  176  yongsun     NSFont *newFont = [sender convertFont:oldFont];
    189  176  yongsun 
    190  176  yongsun     NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
    191  176  yongsun     NSString* ftname = [newFont familyName];
    192  176  yongsun     float ftsize = [newFont pointSize];
    193  176  yongsun     [pref setObject:ftname forKey:@"fontName"];
    194  176  yongsun     [pref setFloat:ftsize forKey:@"fontSize"];
    195  266  yongsun }
    196  266  yongsun 
    197  266  yongsun -(void) changeAttributes:(id)sender
    198  266  yongsun {
    199  266  yongsun     NSColor *fgColor = [_candiWin fgColor];
    200  266  yongsun     NSDictionary *oldAttributes = [NSDictionary dictionaryWithObject:fgColor forKey:@"NSColor"];
    201  266  yongsun     NSDictionary *newAttributes = [sender convertAttributes: oldAttributes];
    202  266  yongsun 
    203  266  yongsun     fgColor = [newAttributes objectForKey:@"NSColor"];
    204  266  yongsun     [_ftTxtField setTextColor:fgColor];
    205  266  yongsun 
    206  266  yongsun     NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
    207  266  yongsun     NSData *data=[NSArchiver archivedDataWithRootObject:fgColor];
    208  266  yongsun     [pref setObject:data forKey:@"fgColor"];
    209  190  yongsun }
    210  190  yongsun 
    211  190  yongsun -(IBAction)toggleChinesePuncts:(id)sender
    212  190  yongsun {
    213  190  yongsun     NSMenuItem *item = [_menu itemWithTag:0];
    214  190  yongsun     _inputChinesePuncts = ![item state];
    215  190  yongsun     [[NSUserDefaults standardUserDefaults] setBool:_inputChinesePuncts
    216  190  yongsun                                            forKey:@"inputChinesePuncts"];
    217  190  yongsun }
    218  190  yongsun 
    219  190  yongsun -(bool)inputChinesePuncts
    220  190  yongsun {
    221  190  yongsun     return _inputChinesePuncts;
    222  190  yongsun }
    223  190  yongsun 
    224  190  yongsun -(IBAction)toggleFullSymbols:(id)sender
    225  190  yongsun {
    226  190  yongsun     NSMenuItem *item = [_menu itemWithTag:1];
    227  190  yongsun     _inputFullSymbols = ![item state];
    228  190  yongsun     [[NSUserDefaults standardUserDefaults] setBool:_inputFullSymbols
    229  190  yongsun                                            forKey:@"inputFullSymbols"];
    230  190  yongsun }
    231  190  yongsun 
    232  190  yongsun -(bool)inputFullSymbols
    233  190  yongsun {
    234  190  yongsun     return _inputFullSymbols;
    235  190  yongsun }
    236  190  yongsun 
    237  274  yongsun -(SwitchingPolicies)switchingPolicy
    238  190  yongsun {
    239  274  yongsun     return _switchingPolicy;
    240  174  yongsun }
    241  174  yongsun 
    242  271  yongsun -(bool)usingUSKbLayout
    243  271  yongsun {
    244  271  yongsun     return _usingUSKbLayout;
    245  271  yongsun }
    246  271  yongsun 
    247  174  yongsun -(void)dealloc
    248  174  yongsun {
    249  174  yongsun     delete _data;
    250  174  yongsun     delete _history;
    251  174  yongsun     delete _pref;
    252  174  yongsun     [[NSNotificationCenter defaultCenter] removeObserver:self];
    253  174  yongsun     [super dealloc];
    254  274  yongsun }
    255  274  yongsun 
    256  274  yongsun -(NSDictionary *)registrationDictionaryForGrowl
    257  274  yongsun {
    258  274  yongsun     NSArray *notifications;
    259  274  yongsun     notifications = [NSArray arrayWithObject: @"SunPinyin"];
    260  274  yongsun 
    261  274  yongsun     NSDictionary *dict;
    262  274  yongsun     dict = [NSDictionary dictionaryWithObjectsAndKeys:
    263  274  yongsun                          notifications, GROWL_NOTIFICATIONS_ALL,
    264  274  yongsun                          notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
    265  274  yongsun 
    266  274  yongsun     return (dict);
    267  274  yongsun }
    268  274  yongsun 
    269  274  yongsun -(void)messageNotify:(NSString*)msg
    270  274  yongsun {
    271  274  yongsun     [GrowlApplicationBridge notifyWithTitle: @"SunPinyin"
    272  274  yongsun                             description: msg
    273  274  yongsun                             notificationName: @"SunPinyin"
    274  274  yongsun                             iconData: [NSData dataWithData:[[NSImage imageNamed:@"SunPinyin"] TIFFRepresentation]]
    275  274  yongsun                             priority: 0
    276  274  yongsun                             isSticky: NO
    277  274  yongsun                             clickContext: nil];
    278  174  yongsun }
    279  174  yongsun 
    280  174  yongsun @end
    281  157  yongsun 
    282  163  yongsun //this method is to return the path to history cache file.
    283  163  yongsun static NSString* get_history_path ()
    284  163  yongsun {
    285  163  yongsun     NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/SunPinyin"];
    286  163  yongsun     NSFileManager* fm = [NSFileManager defaultManager];
    287  163  yongsun     if(![fm fileExistsAtPath:path])
    288  163  yongsun         [fm createDirectoryAtPath:path attributes:nil];
    289  163  yongsun 
    290  163  yongsun     return [path stringByAppendingPathComponent:@"history"];
    291  163  yongsun }
    292  163  yongsun 
    293  163  yongsun static bool save_history (CICHistory* history)
    294  163  yongsun {
    295  163  yongsun     bool suc = NO;
    296  163  yongsun     size_t sz = 0;
    297  163  yongsun     void* buf = NULL;
    298  163  yongsun     NSString* path = get_history_path ();
    299  163  yongsun 
    300  163  yongsun     if (history->bufferize(&buf, &sz) && buf) {
    301  163  yongsun         FILE* fp = fopen ([path UTF8String], "w+b");
    302  163  yongsun         if (fp) {
    303  163  yongsun             suc = (fwrite(buf, 1, sz, fp) == sz);
    304  163  yongsun             fclose(fp);
    305  163  yongsun         }
    306  163  yongsun         free(buf);
    307  163  yongsun     }
    308  176  yongsun 
    309  163  yongsun     return suc;
    310  163  yongsun }
    311  163  yongsun 
    312  163  yongsun static bool load_history (CICHistory* history)
    313  163  yongsun {
    314  163  yongsun     bool suc = NO;
    315  163  yongsun 
    316  163  yongsun     NSString* path = get_history_path ();
    317  163  yongsun     FILE* fp = fopen([path UTF8String], "rb");
    318  163  yongsun 
    319  163  yongsun     if (fp) {
    320  163  yongsun         struct stat info;
    321  163  yongsun         fstat(fileno(fp), &info);
    322  163  yongsun         void* buf = malloc(info.st_size);
    323  163  yongsun         if (buf) {
    324  163  yongsun             fread(buf, info.st_size, 1, fp);
    325  163  yongsun             suc = history->loadFromBuffer(buf, info.st_size);
    326  163  yongsun             free(buf);
    327  163  yongsun         }
    328  163  yongsun         fclose(fp);
    329  163  yongsun     }
    330  163  yongsun 
    331  163  yongsun     return suc;
    332  163  yongsun }
    333  163  yongsun 
    334  163  yongsun static bool load_preferences (CSunpinyinOptions* opts)
    335  163  yongsun {
    336  163  yongsun     NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
    337  163  yongsun     opts->m_ViewType = [pref integerForKey: @"inputStyle"]?
    338  163  yongsun                        CIMIViewFactory::SVT_CLASSIC:
    339  163  yongsun                        CIMIViewFactory::SVT_MODERN;
    340  190  yongsun     opts->m_MinusAsPageUp = [pref boolForKey:@"pagingByMinusAndEqual"];
    341  190  yongsun     opts->m_BracketAsPageUp = [pref boolForKey:@"pagingByBrackets"];
    342  190  yongsun     opts->m_CommaAsPageUp = [pref boolForKey:@"pagingByCommaAndDot"];
    343  176  yongsun     opts->m_CandiWindowSize = [pref integerForKey:@"candiNumbers"];
    344  261  yongsun     opts->m_GBK = [[pref stringForKey:@"charset"] isEqualToString:@"GBK"];
    345  163  yongsun 
    346  163  yongsun     return YES;
    347  163  yongsun }
    348  163  yongsun 
    349