forked from k3a/AssistantExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAEExtension.mm
1112 lines (961 loc) · 32.6 KB
/
AEExtension.mm
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Extensions.mm
// AssistantExtensions
//
// Created by Kexik on 12/26/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#include "AEExtension.h"
#include <dlfcn.h>
#include <unistd.h> // sleep
// directory listing
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <objc/runtime.h>
#import "main.h"
#import "AESupport.h"
#import "AEX.h"
#import "AESpringBoardMsgCenter.h"
static NSMutableDictionary* s_exDict = nil;
const char* extensionsPath = EXTENSIONS_PATH; // slash must be at the end
const int extensionsPathLen = strlen(extensionsPath);
#pragma mark - EXTENSION CLASS
@implementation AEExtension
+(BOOL)initExtensions
{
s_exDict = [[NSMutableDictionary alloc] init];
struct dirent *direntp = NULL;
DIR *dirp = opendir(extensionsPath);
if (dirp == NULL)
{
NSLog(@"AE ERROR: Error opening extensions dir %s. No extensions yet?", extensionsPath);
return FALSE;
}
NSLog(@"AE: Loading Extensions:");
while ((direntp = readdir(dirp)) != NULL)
{
// ignore special directories
if ((strcmp(direntp->d_name, ".") == 0) ||
(strcmp(direntp->d_name, "..") == 0))
continue;
// if not bundle, skip
if (!strstr(direntp->d_name, ".assistantExtension")) continue;
//TODO: remove .siribundle extension from name
NSString* name = [NSString stringWithUTF8String:direntp->d_name];
AEExtension* ex = [AEExtension extensionWithName:name];
if (ex) [s_exDict setObject:ex forKey:[name lowercaseString]];
}
// finalize resources
closedir(dirp);
return TRUE;
}
+(void)reloadExtensions
{
NSLog(@"AE: Reloading Extensions:");
[s_exDict removeAllObjects];
struct dirent *direntp = NULL;
DIR *dirp = opendir(extensionsPath);
if (dirp == NULL)
{
NSLog(@"AE ERROR: Error opening extensions dir %s. No extensions yet?", extensionsPath);
return;
}
while ((direntp = readdir(dirp)) != NULL)
{
/* Ignore special directories. */
if ((strcmp(direntp->d_name, ".") == 0) ||
(strcmp(direntp->d_name, "..") == 0))
continue;
// if not bundle, skip
if (!strstr(direntp->d_name, ".assistantExtension")) continue;
//TODO: remove .siribundle extension from name
NSString* name = [NSString stringWithUTF8String:direntp->d_name];
AEExtension* ex = [AEExtension extensionWithName:name];
if (ex) [s_exDict setObject:ex forKey:[name lowercaseString]];
}
/* Finalize resources. */
closedir(dirp);
}
+(void)shutdownExtensions
{
[s_exDict release];
}
+(void)switchToLanguage:(NSString*)lang
{
static NSString* oldLang = nil;
if ([oldLang isEqualToString:lang])
return;
else
{
[oldLang release];
oldLang = [lang copy];
}
for (NSString* key in [s_exDict allKeys])
{
[[s_exDict objectForKey:key] languageChangedTo:lang];
}
NSLog(@"AE: Language switched to %@", lang);
}
+(NSArray*)allExtensionsNames
{
return [s_exDict allKeys];
}
+(NSArray*)allExtensions
{
return [s_exDict allValues];
}
+(id)findExtensionNamed:(NSString*)name
{
AEExtension* ex = [s_exDict objectForKey:name];
return ex;
}
+(id)extensionWithName:(NSString*)name
{
return [[[AEExtension alloc] initWithName:name] autorelease];
}
-(id)initWithName:(NSString*)name
{
if (_initialized)
{
NSLog(@"AE ERROR: Extension already initialized!");
return nil;
}
if ( (self = [super init]) )
{
NSLog(@"-> %@", name);
char full_name[_POSIX_PATH_MAX + 1];
if ((extensionsPathLen + strlen([name UTF8String]) + 1) > _POSIX_PATH_MAX)
{
[self release];
return nil;
}
strcpy(full_name, extensionsPath);
strcat(full_name, [name UTF8String]);
struct stat fstat;
if (stat(full_name, &fstat) < 0)
{
NSLog(@"AE ERROR: Extension Bundle at path %s cannot be found!", full_name);
[self release];
return nil;
}
_bundle = [[NSBundle bundleWithPath:[NSString stringWithUTF8String:full_name]] retain];
if (!_bundle)
{
NSLog(@"AE ERROR: Failed to open extension bundle %@ (%s)!", name, full_name);
[self release];
return nil;
}
if (![_bundle load])
{
NSLog(@"AE ERROR: Failed to load extension bundle %@ (wrong CFBundleExecutable? Missing? Not signed?)!", name);
[self release];
return nil;
}
// load principal class
Class principal = [_bundle principalClass];
if (!principal)
{
NSLog(@"AE ERROR: Extension %@ doesn't provide a NSPrincipalClass!", name);
[self release];
return nil;
}
// check version requirements
NSString* verReq = [_bundle objectForInfoDictionaryKey:@"AEVersionRequirement"];
if (!verReq && [_principal respondsToSelector:@selector(versionRequirement)])
verReq = [_principal versionRequirement];
if (verReq)
{
NSArray* verReqArr = [verReq componentsSeparatedByString:@"."];
NSString* ver = @AE_VERSION;
NSArray* verArr = [ver componentsSeparatedByString:@"."];
int reqLen = [verReqArr count];
int len = [verArr count];
int toProcess = (reqLen > len)?reqLen:len; // find highest
for (int i=0; i<toProcess; i++)
{
int rr = 0;
int vv = 0;
if (i<reqLen) rr = [[verReqArr objectAtIndex:i] intValue];
if (i<len) vv = [[verArr objectAtIndex:i] intValue];
if (rr > vv)
{
NSLog(@"AE ERROR: Extension %@ requires AE %@ or newer but you have AE %@ installed!", name, verReq, ver);
[self release];
return nil;
}
}
}
_commands = [[NSMutableArray alloc] init];
_snippets = [[NSMutableArray alloc] init];
_patterns = [[NSMutableArray alloc] init];
_principal = [[principal alloc] initWithSystem:self];
if (!_principal)
{
NSLog(@"AE ERROR: Failed to initialize NSPrincipalClass from extension %@!", name);
[self release];
return nil;
}
else if (![_principal conformsToProtocol:@protocol(SEExtension)])
{
NSLog(@"AE ERROR: Extension's NSPrincipalClass (%s) doesn't conform to SEExtension protocol!", object_getClassName(_principal));
[self release];
return nil;
}
// get extension info
_displayName = [[[_bundle infoDictionary] objectForKey:@"AEName"] copy];
if (!_displayName)
{
if ([_principal respondsToSelector:@selector(name)])
_displayName = [[_principal name] copy];
else
_displayName = [[name stringByDeletingPathExtension] retain];
}
_version = [[_bundle objectForInfoDictionaryKey:@"AEVersion"] copy];
_author = [[_bundle objectForInfoDictionaryKey:@"AEAuthor"] copy];
if (!_author && [_principal respondsToSelector:@selector(author)]) _author = [[_principal author] copy];
_web = [[_bundle objectForInfoDictionaryKey:@"AEWebsite"] copy];
if (!_web && [_principal respondsToSelector:@selector(website)]) _web = [[_principal website] copy];
_desc = [[_bundle objectForInfoDictionaryKey:@"AEDescription"] copy];
if (!_desc && [_principal respondsToSelector:@selector(description)]) _desc = [[_principal description] copy];
_ident = [[_bundle objectForInfoDictionaryKey:@"CFBundleIdentifier"] copy];
if (!_ident) _ident = [[NSString alloc] initWithFormat:@"me.k3a.%@", name];
_preferenceBundle = [[_bundle pathForResource:[_bundle objectForInfoDictionaryKey:@"AEPreferenceBundle"] ofType:@"bundle"] copy];
//NSLog(@"AE PREFERENCE BUNDLE IZ %@", _preferenceBundle);
_hasPreferences = _preferenceBundle ? YES : NO;
// since 1.0.2 - handling raw objects
_respondsToClientToServer = [_principal respondsToSelector:@selector(clientToServerObject:context:)];
_respondsToServerToClient = [_principal respondsToSelector:@selector(serverToClientObject:context:)];
if (_respondsToClientToServer)
{
NSMutableString* str = [NSMutableString string];
if (!_clientToServerFilter)
[str setString:@" ALL (consider adding filters)"];
else
for (NSString* fname in _clientToServerFilter)
[str appendFormat:@" %@", fname];
NSLog(@" [OK] raw client->server objects:%@", str);
}
if (_respondsToServerToClient)
{
NSMutableString* str = [NSMutableString string];
if (!_serverToClientFilter)
[str setString:@" ALL (consider adding filters)"];
else
for (NSString* fname in _serverToClientFilter)
[str appendFormat:@" %@", fname];
NSLog(@" [OK] raw server->client objects:%@", str);
}
/*Class principal = [_bundle principalClass];
if (!principal)
{
NSLog(@"AE ERROR: Extension %@ doesn't provide a NSPrincipalClass!", name);
[self release];
return nil;
}
_principal = [[principal alloc] initWithSystem:[SCSystem sharedInstance]];
if (!_principal)
{
NSLog(@"AE ERROR: Failed to initialize NSPrincipalClass from extension %@!", name);
[self release];
return nil;
}*/
_name = [name copy];
_initialized = YES;
// initialize patterns
[self languageChangedTo:AEGetAssistantLanguage()]; // TODO: detect changes of siri language in runtime as well
unsigned pcnt = [_patterns count];
if (pcnt>0) NSLog(@" [OK] %d AEX pattern(s)", pcnt);
}
return self;
}
-(void)dealloc
{
[_name release];
[_displayName release];
[_author release];
[_web release];
[_version release];
[_desc release];
[_ident release];
[_principal release];
[_commands release];
[_snippets release];
[_patterns release];
[_bundle release];
[_currLang release];
[_currLangDict release];
[_currLangDir release];
[_patternsPlist release];
[_serverToClientFilter release];
[_clientToServerFilter release];
[super dealloc];
}
/*-(id<SOExtension>)principalObject
{
return _principal;
}*/
-(NSObject<SECommand>*)handleSpeech:(NSString*)text tokens:(NSArray*)tokens tokenSet:(NSSet*)tokenset context:(AEContext*)ctx
{
// forced command from the previous refId (context)
if (_nextPattern)
{
AEPattern* p = _nextPattern;
[p fireWithMatch:nil context:ctx];
_nextPattern = nil;
[p autorelease];
return [p target];
}
else if (_nextCommand)
{
NSObject<SECommand>* cmd = _nextCommand;
[cmd handleSpeech:text tokens:tokens tokenSet:tokenset context:ctx];
_nextCommand = nil;
return [cmd autorelease];
}
// try patterns first
AEPattern* matchedPattern = NULL;
for (AEPattern* p in _patterns)
{
//NSLog(@"AE: Testing input '%@' for pattern %@", text, p);
if ([p execute:text language:_currLang context:ctx])
{
matchedPattern = p;
break;
}
}
if (matchedPattern) // some pattern matched
{
if ([ctx wasListenAfterSpeaking]) // needs this extension next time?
{
_nextPattern = [matchedPattern retain];
}
return [matchedPattern target];
}
else // old "handleSpeech" method
{
for (NSObject<SECommand>* cmd in _commands)
{
if ([cmd respondsToSelector:@selector(handleSpeech:tokens:tokenSet:context:)] &&
[cmd handleSpeech:text tokens:tokens tokenSet:tokenset context:ctx])
{
if ([ctx wasListenAfterSpeaking]) // needs this extension next time?
_nextCommand = [cmd retain];
return cmd;
}
}
}
return nil;
}
-(NSObject<SESnippet>*)allocSnippet:(NSString*)snippetClass properties:(NSDictionary *)props
{
// TODO: speed up
for (NSString* sn in _snippets)
{
if ([sn isEqualToString:snippetClass])
{
NSObject<SESnippet>* snip = [objc_getClass([snippetClass UTF8String]) alloc];
id initRes = nil;
if ([snip respondsToSelector:@selector(initWithProperties:system:)])
initRes = [snip initWithProperties:props system:self];
if (!initRes && [snip respondsToSelector:@selector(initWithProperties:)])
initRes = [snip initWithProperties:props];
if (!initRes)
initRes = [snip init];
if (!initRes)
{
NSLog(@"AE ERROR: Snippet class %@ failed to initialize!", snippetClass);
return nil;
}
return snip;
}
}
return nil;
}
-(NSString*)name
{
return _name;
}
-(NSString*)displayName
{
if (!_displayName) return @"Unknown";
return _displayName;
}
-(NSString*)author
{
return _author?_author:@"";
}
-(NSString*)website
{
return _web?_web:@"";
}
-(NSString*)description
{
return _desc?_desc:@"";
}
-(NSString*)version
{
return _version?_version:@"";
}
-(NSString*)preferenceBundle {
if (!_preferenceBundle) return @"";
return _preferenceBundle;
}
- (BOOL)hasPreferenceBundle {
return _hasPreferences;
}
- (NSString *)iconPath
{
NSString *k = [_bundle objectForInfoDictionaryKey:@"AEIcon"];
NSFileManager* fm = [NSFileManager defaultManager];
if (!k || !_bundle) return @"";
NSString* iconPath = [NSString stringWithFormat:@"%@/%@", [_bundle bundlePath], k];
if (![fm fileExistsAtPath:iconPath])
iconPath = [NSString stringWithFormat:@"%@/%@.png", [_bundle bundlePath], k];
return iconPath;
}
-(NSString*)identifier
{
return _ident;
}
- (BOOL)enabled
{
//NSNumber* e = [[NSDictionary dictionaryWithContentsOfFile:[self pathToInfoDictionary]] objectForKey:@"AEEnabled"];
NSNumber* e = [[AESpringBoardMsgCenter sharedInstance] prefForKey:[self identifier]];
return !e || [e boolValue];
}
- (NSString*)pathToInfoDictionary
{
NSString* path = [_bundle pathForResource:@"Info" ofType:@"plist"];
return path?path:@"";
}
-(BOOL)handlesServerToClientClass:(NSString*)className;
{
if (!_respondsToServerToClient) return NO;
if (!_serverToClientFilter) return YES;
return [_serverToClientFilter containsObject:className];
}
-(BOOL)handlesClientToServerClass:(NSString*)className;
{
if (!_respondsToClientToServer) return NO;
if (!_clientToServerFilter) return YES;
return [_clientToServerFilter containsObject:className];
}
-(NSDictionary*)serverToClient:(NSDictionary*)input context:(AEContext*)ctx
{
return [_principal serverToClientObject:[[input mutableCopy] autorelease] context:ctx];
}
-(NSDictionary*)clientToServer:(NSDictionary*)input context:(AEContext*)ctx
{
return [_principal clientToServerObject:[[input mutableCopy] autorelease] context:ctx];
}
-(void)callAssistantDismissed
{
// call to each commands class
for (NSObject<SECommand>* cmd in _commands)
{
if ([cmd respondsToSelector:@selector(assistantDismissed)])
[cmd assistantDismissed];
}
// call to extension's principal
if ([_principal respondsToSelector:@selector(assistantDismissed)])
[_principal assistantDismissed];
}
-(void)callAssistantActivated
{
// call to extension's principal
if ([_principal respondsToSelector:@selector(assistantActivatedWithContext:)])
[_principal assistantActivatedWithContext:[AEContext contextWithRefId:nil]];
}
-(void)languageChangedTo:(NSString*)lang
{
if ([lang isEqualToString:_currLang])
{
NSLog(@"AE: Already on the lang %@. Not re-loading.", lang);
return; // already using the same lang
}
NSString* resPath = [_bundle resourcePath];
NSFileManager* fm = [NSFileManager defaultManager];
NSArray* subpaths = [fm subpathsAtPath:resPath];
[_currLangDir release];
_currLangDir = nil;
for (NSString* sp in subpaths)
{
if ([sp hasSuffix:@".lproj"])
{
NSString* lname = [sp stringByDeletingPathExtension];
if ([lname caseInsensitiveCompare:lang] == NSOrderedSame) // exact match
{
_currLangDir = [resPath stringByAppendingFormat:@"/%@.lproj", lname];
break;
}
else if ([lname hasPrefix:lang] || [lang hasPrefix:lname]) // partial match
{
_currLangDir = [resPath stringByAppendingFormat:@"/%@.lproj", lname];
}
}
}
if (!_currLangDir)
{
_currLangDir = [resPath stringByAppendingFormat:@"/en.lproj"];
if (![fm fileExistsAtPath:_currLangDir])
{
//NSLog(@"AE: Info: The language directory not found for the language '%@' nor 'en'. Not using localization.", lang);
_currLangDir = nil;
}
else
NSLog(@"AE: Info: A language directory for the language '%@' was not found. Using English.", lang);
}
// load Localizable.strings
if (_currLangDir)
{
[_currLang release];
_currLang = [lang copy];
NSString* stringsPath = [_currLangDir stringByAppendingFormat:@"/Localizable.strings"];
[_currLangDict release];
_currLangDict = [[NSDictionary alloc] initWithContentsOfFile:stringsPath];
if (!_currLangDict)
NSLog(@"AE: Info: File %@ was not found. Not using localization!", stringsPath);
[_currLangDir retain];
}
// load patterns
NSString* patternFile = [_currLangDir stringByAppendingString:@"/Patterns.plist"];
if (![fm fileExistsAtPath:patternFile])
{
patternFile = [resPath stringByAppendingFormat:@"/en.lproj/Patterns.plist"];
if (![fm fileExistsAtPath:patternFile])
{
patternFile = [resPath stringByAppendingFormat:@"/Patterns.plist"];
if (![fm fileExistsAtPath:patternFile])
patternFile = nil;
}
}
if (patternFile)
{
[_patternsPlist release]; // delete the old one
_patternsPlist = [[NSDictionary alloc] initWithContentsOfFile:patternFile];
if (!_patternsPlist)
NSLog(@"AE: Error: Patterns.plist file for %@ extension can't be loaded (damaged or malformed?)!", [self name]);
else
NSLog(@" [OK] using Patterns.plist");
}
else
{
_patternsPlist = nil;
}
// remove old patterns
[self removeAllPatterns];
// register patterns of each command class
for (id<SECommand> cmd in _commands)
{
if (![cmd respondsToSelector:@selector(patternsForLang:inSystem:)]) continue;
_commandForCurrentPatternRegistrations = [cmd retain];
[cmd patternsForLang:_currLang inSystem:self];
_commandForCurrentPatternRegistrations = nil;
[cmd release];
}
}
// --- public methods (SESystem) --------------------------------------------------
-(BOOL)setServerToClientFilter:(NSArray*)allowedClasses
{
_serverToClientFilter = [[NSSet setWithArray:allowedClasses] retain];
return YES;
}
-(BOOL)setClientToServerFilter:(NSArray*)allowedClasses
{
_clientToServerFilter = [[NSSet setWithArray:allowedClasses] retain];
return YES;
}
-(BOOL)registerCommand:(Class)cls
{
const char* clsName = class_getName(cls);
if (![cls conformsToProtocol:@protocol(SECommand)])
{
NSLog(@" [ER] command %s does not conform to protocol SECommand!", clsName);
return NO;
}
// alloc
id inst = [cls alloc];
// init 1.0.2
if ([inst respondsToSelector:@selector(initWithSystem:)])
[inst initWithSystem:self];
else
[inst init];
if (!inst)
{
NSLog(@" [ER] command %s failed to initialize!", clsName);
return NO;
}
[_commands addObject:inst];
[inst release];
NSLog(@" [OK] command %s", clsName);
return YES;
}
-(BOOL)registerSnippet:(Class)cls
{
const char* clsName = class_getName(cls);
if (![cls conformsToProtocol:@protocol(SESnippet)])
{
NSLog(@" [ER] snippet %s does not conform to protocol SESnippet!", clsName);
return NO;
}
NSLog(@" [OK] snippet %s", clsName);
[_snippets addObject:[NSString stringWithUTF8String:clsName]];
return YES;
}
-(NSString*)systemVersion
{
return @AE_VERSION;
}
//
-(NSString*)localizedString:(NSString*)text
{
if (!_currLangDict) return text;
NSString* localized = [_currLangDict objectForKey:text];
if (localized)
return localized;
else
return text;
}
-(NSString*)localizedString:(NSString*)text inLanguage:(NSString*)lang
{
return @"!NOT_IMPLEMENTED_YET!"; // TODO:
}
-(void)removeAllPatterns
{
[_nextPattern release];
_nextPattern = nil;
[_patterns removeAllObjects];
}
// CONCRETE IMPLEMENTATION
-(BOOL)registerPattern:(NSString*)pattern target:(id)target selector:(SEL)sel userInfo:(id)user
{
if (!_commandForCurrentPatternRegistrations)
{
NSLog(@"AE: ERROR: registerPattern called outside patternsForLang:inSystem: method!");
return FALSE;
}
if (!pattern || [pattern length] == 0)
{
NSLog(@"AE ERROR: Missing or empty pattern!");
return FALSE;
}
else if (!target)
{
NSLog(@"AE ERROR: No target specified for pattern '%@'!", pattern);
return FALSE;
}
else if (![target respondsToSelector:sel])
{
NSLog(@"AE ERROR: Pattern '%@' registered with selector %s not available in target %p!", pattern, sel_getName(sel), target);
return FALSE;
}
else if (!sel)
{
NSLog(@"AE ERROR: No selector specified for pattern '%@'!", pattern);
return FALSE;
}
AEPattern* p = [AEPattern patternWithString:pattern target:target selector:sel userInfo:user];
if (!p) return FALSE;
[_patterns addObject:p];
return TRUE;
}
-(BOOL)registerPattern:(NSString*)pattern target:(id)target selector:(SEL)sel
{
return [self registerPattern:pattern target:target selector:sel userInfo:nil];
}
-(BOOL)registerPattern:(NSString*)pattern selector:(SEL)sel userInfo:(id)user
{
return [self registerPattern:pattern target:_commandForCurrentPatternRegistrations selector:sel userInfo:user];
}
-(BOOL)registerPattern:(NSString*)pattern selector:(SEL)sel
{
return [self registerPattern:pattern selector:sel userInfo:nil];
}
-(BOOL)registerPattern:(NSString*)pattern userInfo:(id)user
{
return [self registerPattern:pattern selector:@selector(handlePatternMatch:context:) userInfo:user];
}
-(BOOL)registerPattern:(NSString*)pattern
{
return [self registerPattern:pattern selector:@selector(handlePatternMatch:context:) userInfo:nil];;
}
//-----------
// CONCRETE IMPLEMENTATION
-(BOOL)registerNamedPattern:(NSString*)name target:(id)target selector:(SEL)sel userInfo:(id)user
{
if (!_commandForCurrentPatternRegistrations)
{
NSLog(@"AE: ERROR: registerNamedPattern called outside patternsForLang:inSystem: method!");
return FALSE;
}
else if (!name || [name length] == 0)
{
NSLog(@"AE ERROR: Missing or empty name for registerNamedPattern!");
return FALSE;
}
else if (!target)
{
NSLog(@"AE ERROR: No target specified for named pattern '%@'!", name);
return FALSE;
}
else if (![target respondsToSelector:sel])
{
NSLog(@"AE ERROR: Named pattern '%@' registered with selector %s not available in target %p!", name, sel_getName(sel), target);
return FALSE;
}
else if (!sel)
{
NSLog(@"AE ERROR: No selector specified for named pattern '%@'!", name);
return FALSE;
}
// find named pattern
if (!_patternsPlist)
{
NSLog(@"AE ERROR: Attempt to register named pattern %@ but Patterns.plist is not loaded!", name);
return FALSE;
}
NSDictionary* pttrns = [_patternsPlist objectForKey:@"patterns"];
if (!pttrns)
{
NSLog(@"AE ERROR: Attempt to register named pattern %@ but Patterns.plist does not contain patterns array!", name);
return FALSE;
}
NSDictionary* pttrn = [pttrns objectForKey:name];
if (!pttrn)
{
NSLog(@"AE ERROR: Named pattern %@ not found in Patterns.plist!", name);
return FALSE;
}
NSString* pattern = [pttrn objectForKey:@"pattern"];
if (!pattern)
{
// try pattern array
NSArray* patterns = [pttrn objectForKey:@"patterns"];
if (!patterns)
{
NSLog(@"AE ERROR: Named pattern %@ does not have pattern or patterns key in Patterns.plist!", name);
return FALSE;
}
else if ([patterns count] == 0)
{
NSLog(@"AE ERROR: Named pattern %@ has 0 strungs in patterns array in Patterns.plist!", name);
return FALSE;
}
BOOL allOk = YES;
for (NSString* xxx in patterns)
{
BOOL ok = [self registerPattern:xxx target:target selector:sel userInfo:user];
if (!ok) allOk = NO;
}
return allOk;
}
// single pattern
return [self registerPattern:pattern target:target selector:sel userInfo:user];
}
-(BOOL)registerNamedPattern:(NSString*)name target:(id)target selector:(SEL)sel
{
return [self registerNamedPattern:name target:target selector:sel userInfo:nil];
}
-(BOOL)registerNamedPattern:(NSString*)name selector:(SEL)sel userInfo:(id)user
{
return [self registerNamedPattern:name target:_commandForCurrentPatternRegistrations selector:sel userInfo:nil];
}
-(BOOL)registerNamedPattern:(NSString*)name selector:(SEL)sel
{
return [self registerNamedPattern:name selector:sel userInfo:nil];
}
-(BOOL)registerNamedPattern:(NSString*)name userInfo:(id)user
{
return [self registerNamedPattern:name selector:@selector(handlePatternMatch:context:) userInfo:user];
}
-(BOOL)registerNamedPattern:(NSString*)name
{
return [self registerNamedPattern:name userInfo:nil];
}
//----------
// CONCRETE IMPLEMENTATION
-(BOOL)registerAllNamedPatternsForTarget:(id)target selector:(SEL)sel userInfo:(id)user
{
if (!_commandForCurrentPatternRegistrations)
{
NSLog(@"AE: ERROR: registerAllNamedPatternsForTarget called outside patternsForLang:inSystem: method!");
return FALSE;
}
if (!_patternsPlist)
{
NSLog(@"AE ERROR: Attempt to register all named patterns but Patterns.plist is not loaded!");
return FALSE;
}
NSDictionary* pttrns = [_patternsPlist objectForKey:@"patterns"];
if (!pttrns)
{
NSLog(@"AE ERROR: Attempt to register all named patterns but Patterns.plist does not contain patterns array!");
return FALSE;
}
BOOL allOk = TRUE;
for (NSString* patternKey in pttrns)
{
NSDictionary* pttrn = [pttrns objectForKey:patternKey];
NSString* pattern = [pttrn objectForKey:@"pattern"];
if (!pattern)
{
// try pattern array
NSArray* patterns = [pttrn objectForKey:@"patterns"];
if (!patterns)
{
NSLog(@"AE ERROR: Named pattern %@ does not have pattern or patterns key in Patterns.plist!", patternKey);
return FALSE;
}
else if ([patterns count] == 0)
{
NSLog(@"AE ERROR: Named pattern %@ has 0 strungs in patterns array in Patterns.plist!", patternKey);
return FALSE;
}
BOOL allOk = YES;
for (NSString* xxx in patterns)
{
BOOL ok = [self registerPattern:xxx target:target selector:sel userInfo:user];
if (!ok) allOk = NO;
}
return allOk;
}
else
{
// single pattern
BOOL ok = [self registerPattern:pattern target:target selector:sel userInfo:user];
if (!ok) allOk = NO;
}
}
return allOk;
}
-(BOOL)registerAllNamedPatternsForTarget:(id)target selector:(SEL)sel
{
return [self registerAllNamedPatternsForTarget:target selector:sel userInfo:nil];
}
-(BOOL)registerAllNamedPatternsForSelector:(SEL)sel userInfo:(id)user
{
return [self registerAllNamedPatternsForTarget:_commandForCurrentPatternRegistrations selector:sel userInfo:user];
}
-(BOOL)registerAllNamedPatternsForSelector:(SEL)sel
{
return [self registerAllNamedPatternsForSelector:sel userInfo:nil];
}
-(BOOL)registerAllNamedPatternsWithUserInfo:(id)user
{
return [self registerAllNamedPatternsForSelector:@selector(handlePatternMatch:context:) userInfo:user];
}
-(BOOL)registerAllNamedPatterns
{
return [self registerAllNamedPatternsWithUserInfo:nil];
}
//----------
-(NSDictionary*)patternsPlist
{
return _patternsPlist;
}
@end
#pragma mark - SIRI COMMANDS -----------------------------------------------------------------------------------
static NSObject<SECommand>* s_exclusiveCommand = nil;
void AEExtensionBeginExclusive(NSObject<SECommand>* ex)
{
[s_exclusiveCommand autorelease];
s_exclusiveCommand = [ex retain];
}
void AEExtensionEndExclusive()
{
[s_exclusiveCommand release];
s_exclusiveCommand = nil;
}
BOOL HandleSpeechExtensions(NSString* refId, NSString* text, NSArray* tokens, NSSet* tokenset)
{
AEContext* ctx = [AEContext contextWithRefId:refId];
// if we have exclusive mode, use it
if (s_exclusiveCommand)