This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
forked from Yi-Kai/KeyKey
-
Notifications
You must be signed in to change notification settings - Fork 142
/
TakaoSmartPhonetic.m
192 lines (168 loc) · 7.01 KB
/
TakaoSmartPhonetic.m
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Copyrights licensed under the New BSD License. See the accompanying LICENSE
file for terms.
*/
// [AUTO_HEADER]
#import "TakaoSmartPhonetic.h"
#import "TakaoHelper.h"
@implementation TakaoSmartPhonetic
- (void)dealloc
{
[_preferenceFilePath release];
[_phoneticDictionary release];
[super dealloc];
}
- (void)setUI
{
if (!_phoneticDictionary)
return;
NSString *keyboardLayout = [_phoneticDictionary valueForKey:@"KeyboardLayout"];
if ([keyboardLayout isEqualToString:@"ETen"]) {
[_keyboardLayoutPopUpButton selectItemAtIndex:1];
}
else if ([keyboardLayout isEqualToString:@"Hanyu Pinyin"]) {
[_keyboardLayoutPopUpButton selectItemAtIndex:2];
}
else if ([keyboardLayout isEqualToString:@"ETen26"] ||
[keyboardLayout isEqualToString:@"bpmfdtnlvkhgvcgycjqwsexuaorwiqzpmntlhfjkd"] ||
[keyboardLayout isEqualToString:@"Hsu"] ||
[keyboardLayout isEqualToString:@"bpmfdtnlgkhjvcjvcrzasexuyhgeiawomnklldfjs"]) {
[_keyboardLayoutPopUpButton selectItemAtIndex:3];
}
else {
[_keyboardLayoutPopUpButton selectItemAtIndex:0];
}
NSString *clearComposingTextWithEsc = [_phoneticDictionary valueForKey:@"ClearComposingTextWithEsc"];
if ([clearComposingTextWithEsc isEqualToString:@"true"])
[_clearComposingTextWithEscCheckBox setIntValue:1];
else
[_clearComposingTextWithEscCheckBox setIntValue:0];
NSString *showCandidateListWithSpace = [_phoneticDictionary valueForKey:@"ShowCandidateListWithSpace"];
if ([showCandidateListWithSpace isEqualToString:@"true"])
[_showCandidateListWithSpaceCheckBox setIntValue:1];
else
[_showCandidateListWithSpaceCheckBox setIntValue:0];
NSString *candidateCursorAtEndOfTargetBlock = [_phoneticDictionary valueForKey:@"CandidateCursorAtEndOfTargetBlock"];
if ([candidateCursorAtEndOfTargetBlock isEqualToString:@"true"])
[_candidateCursorAtEndOfTargetBlockMatrix selectCellAtRow:1 column:0];
else
[_candidateCursorAtEndOfTargetBlockMatrix selectCellAtRow:0 column:0];
NSString *useCharactersSupportedByEncoding = [_phoneticDictionary valueForKey:@"UseCharactersSupportedByEncoding"];
if ([useCharactersSupportedByEncoding isEqualToString:@""])
[_useCharactersSupportedByEncodingCheckBox setIntValue:1];
else
[_useCharactersSupportedByEncodingCheckBox setIntValue:0];
NSString *selectionKeys = [_phoneticDictionary valueForKey:@"CandidateSelectionKeys"];
[_selectionKeyComboBox setStringValue:selectionKeys];
NSString *composingTextBufferSize = [_phoneticDictionary valueForKey:@"ComposingTextBufferSize"];
int bufferSize = [composingTextBufferSize intValue];
[_composingTextBufferSizeSlider setIntValue:bufferSize];
}
- (void)awakeFromNib
{
_phoneticDictionary = [NSMutableDictionary new];
[_phoneticDictionary setValue:@"Standard" forKey:@"KeyboardLayout"];
[_phoneticDictionary setValue:@"" forKey:@"UseCharactersSupportedByEncoding"];
[_phoneticDictionary setValue:@"false" forKey:@"ClearComposingTextWithEsc"];
[_phoneticDictionary setValue:@"true" forKey:@"ShowCandidateListWithSpace"];
[_phoneticDictionary setValue:@"false" forKey:@"CandidateCursorAtEndOfTargetBlock"];
[_phoneticDictionary setValue:@"12345678" forKey:@"CandidateSelectionKeys"];
[_phoneticDictionary setValue:@"10" forKey:@"ComposingTextBufferSize"];
LFRetainAssign(_preferenceFilePath, [TakaoHelper plistFilePath:PLIST_SMARTPHONETIC_FILENAME]);
NSData *data = [NSData dataWithContentsOfFile:_preferenceFilePath options:0 error:nil];
if (data) {
NSPropertyListFormat format;
NSString *errorString = nil;
NSMutableDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format: &format errorDescription:&errorString];
if (dictionary)
[_phoneticDictionary addEntriesFromDictionary:dictionary];
} // end data
[self setUI];
[self writePreference:self];
}
- (void)updateDictionary
{
if (!_phoneticDictionary)
_phoneticDictionary = [NSMutableDictionary new];
switch ([_keyboardLayoutPopUpButton indexOfSelectedItem]) {
case 1:
[_phoneticDictionary setValue:@"ETen" forKey:@"KeyboardLayout"];
break;
case 2:
[_phoneticDictionary setValue:@"Hanyu Pinyin" forKey:@"KeyboardLayout"];
break;
case 3:
if ([[_keyboardLayoutPopUpButton itemArray] count] > 4)
[_phoneticDictionary setValue:@"ETen26" forKey:@"KeyboardLayout"];
break;
case 4:
if ([[_keyboardLayoutPopUpButton itemArray] count] > 4)
[_phoneticDictionary setValue:@"Hsu" forKey:@"KeyboardLayout"];
break;
default:
[_phoneticDictionary setValue:@"Standard" forKey:@"KeyboardLayout"];
break;
}
if ([_clearComposingTextWithEscCheckBox intValue])
[_phoneticDictionary setValue:@"true" forKey:@"ClearComposingTextWithEsc"];
else
[_phoneticDictionary setValue:@"false" forKey:@"ClearComposingTextWithEsc"];
if ([_showCandidateListWithSpaceCheckBox intValue])
[_phoneticDictionary setValue:@"true" forKey:@"ShowCandidateListWithSpace"];
else
[_phoneticDictionary setValue:@"false" forKey:@"ShowCandidateListWithSpace"];
if ([[_candidateCursorAtEndOfTargetBlockMatrix selectedCell] tag])
[_phoneticDictionary setValue:@"true" forKey:@"CandidateCursorAtEndOfTargetBlock"];
else
[_phoneticDictionary setValue:@"false" forKey:@"CandidateCursorAtEndOfTargetBlock"];
if ([_useCharactersSupportedByEncodingCheckBox intValue])
[_phoneticDictionary setValue:@"" forKey:@"UseCharactersSupportedByEncoding"];
else
[_phoneticDictionary setValue:@"BIG-5" forKey:@"UseCharactersSupportedByEncoding"];
NSString *selecitonKeys = [_selectionKeyComboBox stringValue];
[_phoneticDictionary setValue:selecitonKeys forKey:@"CandidateSelectionKeys"];
int bufferSize = [_composingTextBufferSizeSlider intValue];
NSString *bufferSizeString = [NSString stringWithFormat:@"%d", bufferSize];
[_phoneticDictionary setValue:bufferSizeString forKey:@"ComposingTextBufferSize"];
}
- (BOOL)validateSelectionKeys:(NSString *)selectionKeys
{
int i = 0;
for (i = 0; i < [selectionKeys length] - 1; i++) {
char currKey = [selectionKeys characterAtIndex:i];
int j = i + 1;
for (j = i + 1; i < [selectionKeys length]; i++) {
char checkKey = [selectionKeys characterAtIndex:j];
if (currKey == checkKey)
return NO;
}
}
return YES;
}
#pragma mark Interface Builder actions
- (IBAction)setSelectionKey:(id)sender
{
NSString *selectionKeys = [_selectionKeyComboBox stringValue];
if (![self validateSelectionKeys:selectionKeys])
selectionKeys = @"12345678";
if ([selectionKeys length] > 8)
selectionKeys = [selectionKeys substringToIndex:8];
if ([selectionKeys length] < 8)
selectionKeys = @"12345678";
[_selectionKeyComboBox setStringValue:selectionKeys];
[self writePreference:sender];
}
- (IBAction)writePreference:(id)sender
{
NSString *errorString = nil;
[self updateDictionary];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:_phoneticDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorString];
if (errorString) {
[errorString release];
}
if (data) {
[data writeToFile:_preferenceFilePath atomically:YES];
}
}
@end