-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.m
More file actions
157 lines (129 loc) · 5.97 KB
/
Settings.m
File metadata and controls
157 lines (129 loc) · 5.97 KB
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
#import <AppSupport/CPDistributedMessagingCenter.h>
#import <libactivator/libactivator.h>
#import <Preferences/Preferences.h>
#import "CmdivatorCmd.h"
#import "Common.h"
@interface CmdivatorCmdCell: PSTableCell
@end
@interface PSSpecifier (Cmdivator)
@property (retain, nonatomic) CmdivatorCmd *cmd;
@end
@interface CmdivatorSettingsController: PSListController
@end
@implementation CmdivatorSettingsController {
CPDistributedMessagingCenter *_messagingCenter;
}
static void commands_changed(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
CmdivatorSettingsController *controller = (__bridge CmdivatorSettingsController *)observer;
dispatch_async(dispatch_get_main_queue(), ^{
[controller reloadSpecifiersIfVisible];
});
}
- (instancetype)init {
if ((self = [super init])) {
_messagingCenter = [CPDistributedMessagingCenter centerNamed:MESSAGE_CENTER_NAME];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge void *)self, commands_changed,
CFSTR(COMMANDS_CHANGED_NOTIFICATION), NULL, CFNotificationSuspensionBehaviorCoalesce);
}
return self;
}
- (void)dealloc {
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge void *)self,
CFSTR(COMMANDS_CHANGED_NOTIFICATION), NULL);
}
- (void)viewDidLoad {
UIImage *refreshIcon = [UIImage imageNamed:@"Refresh" inBundle:[NSBundle bundleForClass:self.class]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:refreshIcon
style:UIBarButtonItemStylePlain target:self action:@selector(refreshCommands)];
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[self refreshCommands];
[super viewWillAppear:animated];
}
- (void)reloadSpecifiersIfVisible {
if (_specifiers && self.isViewLoaded && self.view.window) {
[self reloadSpecifiers];
}
}
- (NSArray *)specifiers {
if (!_specifiers) {
NSMutableArray *specifiers = [[NSMutableArray alloc] init];
_specifiers = specifiers;
PSSpecifier *topGroup = PSSpecifier.emptyGroupSpecifier;
NSString *footerText = [@"Commands are executable files in\r\n" stringByAppendingString:USER_COMMANDS_DIRECTORY.stringByAbbreviatingWithTildeInPath];
[topGroup setProperty:footerText forKey:@"footerText"];
[specifiers addObject:topGroup];
UIImage *iFileIcon = [LAActivator.sharedInstance smallIconForListenerName:@"eu.heinelt.ifile"];
if (iFileIcon) {
PSSpecifier *iFileSpecifier = [PSSpecifier preferenceSpecifierNamed:@"Open iFile"
target:self set:nil get:nil detail:nil cell:PSButtonCell edit:nil];
[iFileSpecifier setProperty:iFileIcon forKey:@"iconImage"];
iFileSpecifier->action = @selector(open_iFile);
[specifiers addObject:iFileSpecifier];
}
[specifiers addObject:PSSpecifier.emptyGroupSpecifier];
NSArray *cmds = [_messagingCenter sendMessageAndReceiveReplyName:@"listCommands" userInfo:nil][@"commands"];
for (NSString *path in cmds) {
CmdivatorCmd *cmd = [[CmdivatorCmd alloc] initWithPath:path];
PSSpecifier *cmdSpecifier = [PSSpecifier preferenceSpecifierNamed:cmd.displayName
target:self set:nil get:@selector(displayPathForSpecifier:) detail:nil cell:PSLinkListCell edit:nil];
cmdSpecifier.cmd = cmd;
[cmdSpecifier setProperty:CmdivatorCmdCell.class forKey:@"cellClass"];
[cmdSpecifier setProperty:cmd.listenerName forKey:@"activatorListener"];
[cmdSpecifier setProperty:cmd.displayName forKey:@"activatorTitle"];
[cmdSpecifier setProperty:[NSBundle bundleWithIdentifier:@"com.libactivator.preferencebundle"].bundlePath forKey:@"lazy-bundle"];
cmdSpecifier->action = @selector(lazyLoadBundle:);
[specifiers addObject:cmdSpecifier];
}
}
return _specifiers;
}
- (NSString *)displayPathForSpecifier:(PSSpecifier *)specifier {
return specifier.cmd.displayPath;
}
- (void)refreshCommands {
[_messagingCenter sendMessageName:@"refreshCommands" userInfo:nil];
}
- (void)open_iFile {
NSURL *fileURL = [NSURL fileURLWithPath:USER_COMMANDS_DIRECTORY];
NSURL *iFileURL = [NSURL URLWithString:[@"i" stringByAppendingString:fileURL.absoluteString]];
[UIApplication.sharedApplication openURL:iFileURL];
}
- (PSSpecifier *)specifierAtIndexPath:(NSIndexPath *)indexPath {
int specifierIndex = [self indexOfGroup:indexPath.section] + indexPath.row + 1;
return [_specifiers objectAtIndex:specifierIndex];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
PSSpecifier *specifier = [self specifierAtIndexPath:indexPath];
if (specifier.cmd.removable) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PSSpecifier *specifier = [self specifierAtIndexPath:indexPath];
if ([specifier.cmd delete]) {
[self refreshCommands];
}
}
}
@end
@implementation CmdivatorCmdCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier specifier:(PSSpecifier *)specifier {
if ((self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass(self.class) specifier:specifier])) {
self.textLabel.adjustsFontSizeToFitWidth = YES;
self.detailTextLabel.adjustsFontSizeToFitWidth = YES;
}
return self;
}
@end
@implementation PSSpecifier (Cmdivator)
- (CmdivatorCmd *)cmd {
return [self propertyForKey:@"cmdivatorCmd"];
}
- (void)setCmd:(CmdivatorCmd *)cmd {
[self setProperty:cmd forKey:@"cmdivatorCmd"];
}
@end