-
Notifications
You must be signed in to change notification settings - Fork 2
/
ThingsPlugin.mm
328 lines (266 loc) · 10.2 KB
/
ThingsPlugin.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
#import <UIKit/UIKit.h>
#import <SpringBoard/SpringBoard.h>
#import <objc/runtime.h>
#import <sqlite3.h>
#import "ThingsPlugin.h"
extern "C" CFStringRef UIDateFormatStringForFormatType(CFStringRef type);
#define TITLE_LABEL_TAG 328
#define TODO_TITLE_LABEL_TAG 329
#define TODO_DUE_LABEL_TAG 330
// plugin
@interface ThingsPlugin (Private)
- (void)update;
- (void)updateTodo;
- (void)updatePreference;
- (UITableViewCell *)tableView:(LITableView *)tableView cellWithTitle:(NSString *)title;
- (UITableViewCell *)tableView:(LITableView *)tableView cellWithTodo:(NSArray *)todo;
@end
@implementation ThingsPlugin
@synthesize plugin, databasePath, todoList, showNext, showDue, dueSeconds;
- (id)initWithPlugin:(LIPlugin*)thePlugin
{
self = [super init];
self.plugin = thePlugin;
self.todoList = [NSMutableArray array];
plugin.tableViewDataSource = self;
plugin.tableViewDelegate = self;
// get things path
SBApplication* thingsApp = [[objc_getClass("SBApplicationController") sharedInstance] applicationWithDisplayIdentifier:@"com.culturedcode.ThingsTouch"];
NSString *thingsPath = [[thingsApp path] stringByDeletingLastPathComponent];
self.databasePath = [[thingsPath stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:(@"db.sqlite3")];
// notification
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(update) name:LITimerNotification object:nil];
[center addObserver:self selector:@selector(update) name:LIPrefsUpdatedNotification object:nil];
[center addObserver:self selector:@selector(update) name:LIViewReadyNotification object:nil];
return self;
}
- (void)dealloc
{
self.plugin = nil;
self.todoList = nil;
self.databasePath = nil;
[super dealloc];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.todoList count];
}
- (UITableViewCell *)tableView:(LITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id object = [self.todoList objectAtIndex:indexPath.row];
UITableViewCell *cell = nil;
if ([object isKindOfClass:[NSString class]]) {
cell = [self tableView:tableView cellWithTitle:object];
} else {
cell = [self tableView:tableView cellWithTodo:object];
}
return cell;
}
- (CGFloat)tableView:(LITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
id object = [self.todoList objectAtIndex:indexPath.row];
if ([object isKindOfClass:[NSString class]]) {
// title
return 20;
} else {
return 35;
}
}
- (UITableViewCell *)tableView:(LITableView *)tableView cellWithTitle:(NSString *)title
{
NSString *reuseId = @"TitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId] autorelease];
LILabel *titleLabel = [tableView labelWithFrame:CGRectMake(0, 0, 320, 20)];
titleLabel.style = tableView.theme.summaryStyle;
titleLabel.numberOfLines = 1;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.tag = TITLE_LABEL_TAG;
[cell.contentView addSubview:titleLabel];
}
LILabel *titleLabel = [cell.contentView viewWithTag:TITLE_LABEL_TAG];
titleLabel.text = title;
return cell;
}
- (UITableViewCell *)tableView:(LITableView *)tableView cellWithTodo:(NSArray *)todo
{
NSString *reuseId = @"TodoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId] autorelease];
// padding-y 3, 15, 14, 3
CGRect titleLabelFrame = CGRectMake(15, 3, 290, 15);
CGRect detailLabelFrame = CGRectMake(15, 18, 290, 14);
LILabel *titleLabel = [tableView labelWithFrame:titleLabelFrame];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.style = tableView.theme.summaryStyle;
titleLabel.tag = TODO_TITLE_LABEL_TAG;
[cell.contentView addSubview:titleLabel];
LILabel *detailLabel = [tableView labelWithFrame:detailLabelFrame];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.style = tableView.theme.detailStyle;
detailLabel.tag = TODO_DUE_LABEL_TAG;
[cell.contentView addSubview:detailLabel];
}
LILabel *titleLabel = [cell.contentView viewWithTag:TODO_TITLE_LABEL_TAG];
titleLabel.text = (NSString *) [todo objectAtIndex:0];
LILabel *detailLabel = [cell.contentView viewWithTag:TODO_DUE_LABEL_TAG];
double due = [((NSNumber *) [todo objectAtIndex:1]) doubleValue];
if (due == 0) {
detailLabel.text = @"No due date";
} else {
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:due];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = (NSString*)UIDateFormatStringForFormatType(CFSTR("UIWeekdayNoYearDateFormat"));
detailLabel.text = [formatter stringFromDate:date];
[formatter release];
}
return cell;
}
- (void)update
{
if (!self.plugin.enabled) {
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[self updatePreference];
[self updateTodo];
[pool release];
}
- (void)updateTodo
{
int ret;
[self.todoList removeAllObjects];
sqlite3 *database = NULL;
@try {
ret = sqlite3_open([self.databasePath UTF8String], &database);
if (ret != SQLITE_OK) {
NSLog(@"TP: sqlite3_open ret %d", ret);
return;
}
// today
sqlite3_stmt *statement = NULL;
@try {
NSString *sqlForToday = @"SELECT title, duedate FROM Task WHERE type = 2 AND status = 1 AND (flagged = 1 OR focus = 1) ORDER BY duedate, touchedDate";
ret = (sqlite3_prepare_v2 (database, [sqlForToday UTF8String], -1, &statement, NULL) != SQLITE_OK) ;
if (ret != SQLITE_OK) {
NSLog(@"TP: prepare failed %d", ret);
return;
}
NSLog(@"LI: ThingsPlugin Inbox/Today sql: %@", sqlForToday);
BOOL titleAdded = NO;
while (sqlite3_step (statement) == SQLITE_ROW) {
if (!titleAdded) {
[todoList addObject:@"Inbox / Today"];
titleAdded = YES;
}
const char *titlePtr = (const char*) sqlite3_column_text (statement, 0);
double due = sqlite3_column_double (statement, 1);
NSString *title = [NSString stringWithUTF8String:(titlePtr == NULL ? "" : titlePtr)];
NSArray *todo = [NSArray arrayWithObjects:title, [NSNumber numberWithDouble:due], nil];
[todoList addObject:todo];
}
}
@finally {
if (statement != NULL) {
sqlite3_finalize(statement);
statement = NULL;
}
}
// next
if (self.showNext) {
@try {
NSString *sqlForNext = @"SELECT title, duedate FROM Task WHERE type = 2 AND status = 1 AND flagged = 0 AND focus = 2 ORDER BY duedate, touchedDate";
ret = (sqlite3_prepare_v2 (database, [sqlForNext UTF8String], -1, &statement, NULL) != SQLITE_OK);
if (ret != SQLITE_OK) {
NSLog(@"TP: prepare failed %d", ret);
return;
}
NSLog(@"LI: ThingsPlugin next sql: %@", sqlForNext);
BOOL titleAdded = NO;
while (sqlite3_step (statement) == SQLITE_ROW) {
if (!titleAdded) {
[todoList addObject:@"Next"];
titleAdded = YES;
}
const char *titlePtr = (const char*) sqlite3_column_text (statement, 0);
double due = sqlite3_column_double (statement, 1);
NSString *title = [NSString stringWithUTF8String:(titlePtr == NULL ? "" : titlePtr)];
NSArray *todo = [NSArray arrayWithObjects:title, [NSNumber numberWithDouble:due], nil];
[todoList addObject:todo];
}
}
@finally {
if (statement != NULL) {
sqlite3_finalize(statement);
}
}
}
// due
if (self.showDue) {
@try {
NSString *sqlForDue = @"SELECT title, duedate FROM Task WHERE type = 2 AND status = 1 AND flagged = 0 AND focus = 8 AND uuid NOT IN (SELECT substr(uuid, 1, 36) from Task WHERE focus != 8 AND status = 1 AND type = 2)";
if (self.dueSeconds > 0) {
double time = [NSDate timeIntervalSinceReferenceDate] + self.dueSeconds;
sqlForDue = [NSString stringWithFormat:@"%@ AND duedate < %f", sqlForDue, time];
}
sqlForDue = [NSString stringWithFormat:@"%@ %@", sqlForDue, @"ORDER BY duedate, touchedDate"];
ret = (sqlite3_prepare_v2 (database, [sqlForDue UTF8String], -1, &statement, NULL) != SQLITE_OK);
if (ret != SQLITE_OK) {
NSLog(@"TP: prepare failed %d", ret);
return;
}
NSLog(@"LI: ThingsPlugin due sql: %@", sqlForDue);
BOOL titleAdded = NO;
while (sqlite3_step (statement) == SQLITE_ROW) {
if (!titleAdded) {
[todoList addObject:@"Due"];
titleAdded = YES;
}
const char *titlePtr = (const char*) sqlite3_column_text (statement, 0);
double due = sqlite3_column_double (statement, 1);
NSString *title = [NSString stringWithUTF8String:(titlePtr == NULL ? "" : titlePtr)];
NSArray *todo = [NSArray arrayWithObjects:title, [NSNumber numberWithDouble:due], nil];
[todoList addObject:todo];
}
}
@finally {
if (statement != NULL) {
sqlite3_finalize(statement);
}
}
}
}
@finally {
if (database != NULL) {
sqlite3_close(database);
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:LIUpdateViewNotification object:self.plugin userInfo:nil];
}
- (void)updatePreference
{
NSNumber *value = nil;
value = [self.plugin.preferences valueForKey:@"ShowNext"];
self.showNext = [value boolValue];
value = [self.plugin.preferences valueForKey:@"DueInDays"];
switch ([value intValue]) {
case -1:
self.showDue = NO;
self.dueSeconds = -1;
break;
case 999:
self.showDue = YES;
self.dueSeconds = -1;
break;
default:
self.showDue = YES;
self.dueSeconds = [value intValue] * (1 * 60 * 60 * 24);
break;
}
}
@end