-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSServiceInputRulesWindowController.m
310 lines (249 loc) · 12.4 KB
/
TSServiceInputRulesWindowController.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// TSServiceInputRulesWindowController.m
// ThisService
//
// Created by Jesper on 2011-07-17.
// Copyright 2011-2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
#import "TSServiceInputRulesWindowController.h"
#import "TSServiceInputRules.h"
@implementation TSServiceInputRulesWindowController
- (void)setInitialInputRules:(TSServiceInputRules *)anInitialInputRules {
initialInputRules = [anInitialInputRules retain];
}
- (void)setEditor:(id<TSServiceInputRulesWindowControllerDelegate>)newEditor {
editor = newEditor;
}
/*
static NSInteger CompareLanguageRows(id arg1, id arg2, void *arg3) {
return [[arg1 objectForKey:@"label"] compare:[arg2 objectForKey:@"label"]];
}*/
+ (NSArray *)languagesRowData {
static NSArray *languagesRowData = nil;
if (languagesRowData == nil) {
NSArray *preferredLanguages = [NSLocale preferredLanguages];
NSUInteger preferredCount = [preferredLanguages count];
NSArray *localeLanguages = [preferredLanguages arrayByAddingObjectsFromArray:[NSLocale ISOLanguageCodes]];
NSMutableArray *rowData = [NSMutableArray arrayWithCapacity:[localeLanguages count]];
NSMutableSet *passedLanguages = [NSMutableSet set];
NSLocale *localLocale = [NSLocale currentLocale];
NSSet *phonyLanguages = [NSSet setWithObjects:@"und", nil];
NSUInteger i = 0;
for (NSString *localeLanguage in localeLanguages) {
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeLanguage] autorelease];
NSString *localeLang = [locale objectForKey:NSLocaleLanguageCode];
BOOL pref = (preferredCount > i);
i++;
if ([phonyLanguages containsObject:localeLanguage]) continue;
if ([phonyLanguages containsObject:localeLang]) continue;
if ([passedLanguages containsObject:localeLang]) continue;
[passedLanguages addObject:localeLang];
NSString *nameInLocalLocale = [localLocale displayNameForKey:NSLocaleLanguageCode value:localeLanguage];
if (nameInLocalLocale == nil) {
// in 10.8, this is limited to the mainland Chinese languages [ gan, hak, hsn, nan, wuu ]
// http://en.wikipedia.org/wiki/ISO_639_macrolanguage#zho
continue;
}
NSString *nameInOwnLocale = [locale displayNameForKey:NSLocaleLanguageCode value:localeLanguage];
NSString *localLocaleLang = [localLocale objectForKey:NSLocaleLanguageCode];
BOOL sameLanguageLocales = [localeLang isEqualToString:localLocaleLang];
BOOL sameNames = [nameInLocalLocale isEqualToString:nameInOwnLocale];
NSString *unspecified = (sameNames && !sameLanguageLocales ? @"Y" : @"N");
NSString *preferred = (pref ? @"Y" : @"N");
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
nameInLocalLocale, @"label",
localeLanguage, @"value",
nameInOwnLocale, @"ex",
unspecified, @"unspecified",
preferred, @"preferred",
nil];
[rowData addObject:data];
}
NSArray *sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"preferred" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"unspecified" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"label" ascending:YES],
nil];
[rowData sortUsingDescriptors:sortDescriptors];
//NSLog(@"language data: %@", rowData);
languagesRowData = [rowData copy];
}
return languagesRowData;
}
- (void)awakeFromNib {
if ([initialInputRules wordLimit] == nil) {
[self setWordLimit:1];
} else {
[self setConstrainsByWordLimit:YES];
[self setWordLimit:[[initialInputRules wordLimit] integerValue]];
}
NSArray *writingSystemRowData = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Arabic", @"label", @"Arab", @"value", @"العربية", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Armenian", @"label", @"Armn", @"value", @"այբուբեն", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Cyrillic", @"label", @"Cyrl", @"value", @"а, б, в, г, д", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Devanagari", @"label", @"Deva", @"value", @"देवनागरी", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Greek", @"label", @"Grek", @"value", @"Ελληνικά", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Han", @"label", @"Hani", @"value", @"漢語", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Hangul", @"label", @"Hang", @"value", @"한글", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Hebrew", @"label", @"Hebr", @"value", @"עִבְרִית", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Hiragana", @"label", @"Hira", @"value", @"ひらがな", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Katakana", @"label", @"Kana", @"value", @"カタカナ", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Latin", @"label", @"Latn", @"value", @"a, b, c", @"scriptcharacters", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Thai", @"label", @"Thai", @"value", @"อักษรไทย", @"scriptcharacters", nil],
nil
];
NSIndexSet *writingSystemSelected = [TSCheckedListDataSource indexSetOfSelectedIndexesForRowData:writingSystemRowData
selectedValues:[initialInputRules writingSystems]];
writingSystemCheckedListDataSource =
[[TSCheckedListDataSource alloc]
initWithRowData:writingSystemRowData
selectedChoices:writingSystemSelected
labelKey:@"label"
delegate:self];
[writingSystemTableView setDelegate:writingSystemCheckedListDataSource];
[writingSystemTableView setDataSource:writingSystemCheckedListDataSource];
[writingSystemTableView reloadData];
if ([writingSystemSelected count] > 0) {
[self setConstrainsByWritingSystem:YES];
}
/*URL
Date
Address
Email
FilePath*/
NSArray *contentTypeRowData = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"URL", @"label", @"URL", @"value", @"apple.com", @"example", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Date", @"label", @"Date", @"value", @"2012-12-21", @"example", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Address", @"label", @"Address", @"value", @"1 Infinite Loop, California", @"example", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Email", @"label", @"Email", @"value", @"[email protected]", @"example", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"File path", @"label", @"FilePath", @"value", @"/Users/test/test.txt", @"example", nil],
nil
];
NSIndexSet *contentTypeSelected = [TSCheckedListDataSource indexSetOfSelectedIndexesForRowData:contentTypeRowData
selectedValues:[initialInputRules contentTypes]];
contentTypeCheckedListDataSource =
[[TSCheckedListDataSource alloc]
initWithRowData:contentTypeRowData
selectedChoices:contentTypeSelected
labelKey:@"label"
delegate:self];
[contentTypeTableView setDelegate:contentTypeCheckedListDataSource];
[contentTypeTableView setDataSource:contentTypeCheckedListDataSource];
[contentTypeTableView reloadData];
if ([contentTypeSelected count] > 0) {
[self setConstrainsByContentType:YES];
}
NSArray *languagesRowData = [[self class] languagesRowData];
NSIndexSet *languageSelected = [TSCheckedListDataSource indexSetOfSelectedIndexesForRowData:languagesRowData
selectedValues:[initialInputRules languages]];
languageCheckedListDataSource =
[[TSCheckedListDataSource alloc]
initWithRowData:languagesRowData
selectedChoices:languageSelected
labelKey:@"label"
delegate:self];
[languageTableView setDelegate:languageCheckedListDataSource];
[languageTableView setDataSource:languageCheckedListDataSource];
[languageTableView reloadData];
if ([languageSelected count] > 0) {
[self setConstrainsByLanguage:YES];
}
}
- (BOOL)constraintsForContentTypeIsNonEmpty {
return [contentTypeCheckedListDataSource anyChoicesSelected];
}
- (BOOL)constraintsForWritingSystemIsNonEmpty {
return [writingSystemCheckedListDataSource anyChoicesSelected];
}
- (BOOL)constraintsForLanguageIsNonEmpty {
return [languageCheckedListDataSource anyChoicesSelected];
}
- (BOOL)mayOkay {
return ((constrainsByContentType ? [self constraintsForContentTypeIsNonEmpty] : YES) &&
(constrainsByWritingSystem ? [self constraintsForWritingSystemIsNonEmpty] : YES) &&
(constrainsByLanguage ? [self constraintsForLanguageIsNonEmpty] : YES));
}
+ (NSSet *)keyPathsForValuesAffectingMayOkay {
return [NSSet setWithObjects:@"constraintsForContentTypeIsNonEmpty", @"constraintsForWritingSystemIsNonEmpty", @"constraintsForLanguageIsNonEmpty", @"constrainsByContentType", @"constrainsByWritingSystem", @"constrainsByLanguage", nil];
}
+ (NSSet *)keyPathsForValuesAffectingConstraintsForContentTypeIsNonEmpty {
return [NSSet setWithObject:@"constrainsByContentType"];
}
+ (NSSet *)keyPathsForValuesAffectingConstraintsForWritingSystemIsNonEmpty {
return [NSSet setWithObject:@"constrainsByWritingSystem"];
}
+ (NSSet *)keyPathsForValuesAffectingConstraintsForLanguageIsNonEmpty {
return [NSSet setWithObject:@"constrainsByLanguage"];
}
- (void)dataSource:(TSCheckedListDataSource *)dataSource checkedChoicesChanged:(NSArray *)selectedChoices {
if (dataSource == contentTypeCheckedListDataSource) {
[self willChangeValueForKey:@"constraintsForContentTypeIsNonEmpty"];
[self didChangeValueForKey:@"constraintsForContentTypeIsNonEmpty"];
} else if (dataSource == writingSystemCheckedListDataSource) {
[self willChangeValueForKey:@"constraintsForWritingSystemIsNonEmpty"];
[self didChangeValueForKey:@"constraintsForWritingSystemIsNonEmpty"];
} else if (dataSource == languageCheckedListDataSource) {
[self willChangeValueForKey:@"constraintsForLanguageIsNonEmpty"];
[self didChangeValueForKey:@"constraintsForLanguageIsNonEmpty"];
}
}
- (void)setConstrainsByWordLimit:(BOOL)c {
[self willChangeValueForKey:@"constrainsByWordLimit"];
constrainsByWordLimit = c;
[self didChangeValueForKey:@"constrainsByWordLimit"];
}
- (BOOL)constrainsByWordLimit {
return constrainsByWordLimit;
}
- (void)setConstrainsByWritingSystem:(BOOL)c {
[self willChangeValueForKey:@"constrainsByWritingSystem"];
constrainsByWritingSystem = c;
[self didChangeValueForKey:@"constrainsByWritingSystem"];
}
- (BOOL)constrainsByWritingSystem {
return constrainsByWritingSystem;
}
- (void)setConstrainsByContentType:(BOOL)c {
[self willChangeValueForKey:@"constrainsByContentType"];
constrainsByContentType = c;
[self didChangeValueForKey:@"constrainsByContentType"];
}
- (BOOL)constrainsByContentType {
return constrainsByContentType;
}
- (void)setConstrainsByLanguage:(BOOL)c {
[self willChangeValueForKey:@"constrainsByLanguage"];
constrainsByLanguage = c;
[self didChangeValueForKey:@"constrainsByLanguage"];
}
- (BOOL)constrainsByLanguage {
return constrainsByLanguage;
}
- (void)setWordLimit:(NSInteger)wordLimit_ {
[self willChangeValueForKey:@"wordLimit"];
wordLimit = wordLimit_;
[self didChangeValueForKey:@"wordLimit"];
}
- (NSInteger)wordLimit {
return wordLimit;
}
- (IBAction)okayInputRulesSheet:(id)sender {
[editor serviceInputRulesWindowController:self
savedChanges:[[[TSServiceInputRules alloc]
initWithWritingSystems:(constrainsByWritingSystem ? [NSSet setWithArray:[writingSystemCheckedListDataSource selectedChoiceValues]] : nil)
contentTypes:(constrainsByContentType ? [NSSet setWithArray:[contentTypeCheckedListDataSource selectedChoiceValues]] : nil)
wordLimit:(constrainsByWordLimit ? [NSNumber numberWithInteger:wordLimit] : nil)
applicationIdentifiers:nil
languages:(constrainsByLanguage ? [NSSet setWithArray:[languageCheckedListDataSource selectedChoiceValues]] : nil)] autorelease]];
}
- (IBAction)cancelInputRulesSheet:(id)sender {
[editor serviceInputRulesWindowControllerCancelled:self];
}
- (void) dealloc {
[writingSystemCheckedListDataSource release];
[contentTypeCheckedListDataSource release];
[languageCheckedListDataSource release];
[initialInputRules release];
[super dealloc];
}
@end