aboutsummaryrefslogtreecommitdiff
path: root/QSThingsPlugin/Handlers/ThingsAppSectionHandler.m
blob: 0f294fc7c2a0822f2c80fc272a90351dfd27153b (plain)
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
#import "ThingsAppSectionHandler.h"

NS_ASSUME_NONNULL_BEGIN

@implementation ThingsAppSectionHandler

- (BOOL)objectHasChildren:(QSObject *)object {
  return YES;
}

- (void)setQuickIconForObject:(QSObject *)object {
  NSImage *baseImage = [QSResourceManager imageNamed:@"com.culturedcode.ThingsMac"];
  
  ThingsAppSectionKey identifier  = [object identifier];
  NSImage *imageWithBadge = nil;
  if (identifier == ThingsAppSectionKeyInbox) {
    NSColor *color = [NSColor colorWithRed:0.318 green:0.667 blue:0.949 alpha:1.0];
    imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"tray.circle" ofColor:color];
  }
  if (identifier == ThingsAppSectionKeyToday) {
    NSColor *color = [NSColor colorWithRed:0.973 green:0.839 blue:0.282 alpha:1.0];
    imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"star.circle" ofColor:color];
  }
  if (identifier == ThingsAppSectionKeyAnytime) {
    NSColor *color = [NSColor colorWithRed:0.353 green:0.651 blue:0.616 alpha:1.0];
    imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"diamond.circle" ofColor:color];
  }
  if (identifier == ThingsAppSectionKeySomeday) {
    NSColor *color = [NSColor colorWithRed:0.792 green:0.749 blue:0.525 alpha:1.0];
    imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"archivebox.circle" ofColor:color];
  }
  if (identifier == ThingsAppSectionKeyUpcoming) {
    NSColor *color = [NSColor colorWithRed:0.902 green:0.224 blue:0.349 alpha:1.0];
    imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"calendar.circle" ofColor:color];
  }

  if (imageWithBadge) {
    [object setIcon:imageWithBadge];
  } else {
    [object setIcon:baseImage];
  }
}

- (BOOL)loadChildrenForObject:(QSObject *)object {
  NSDictionary *contents = [object objectForType:kAppSectionType];
  
  if (!contents) {
    NSLog(@"Malformed Things Task: No content.");
    return NO;
  }

  NSString *path = [contents objectForKey:@"path"];
  if (!path) {
    NSLog(@"Malformed Things Task: No path.");
    return NO;
  }
  
  ThingsAppSectionKey appSection = [object identifier];
  if (!appSection) {
    NSLog(@"Malformed Things App Section: No identifier.");
    return NO;
  }

  NSMutableArray *children = [NSMutableArray array];
  [children addObjectsFromArray:[self tasksForAppSection:appSection usingDatabaseAt:path ]];
  [children addObjectsFromArray:[self projectsForAppSection:appSection usingDatabaseAt:path ]];

  [object setChildren:children];
  return YES;
}

- (NSArray *)projectsForAppSection:(ThingsAppSectionKey)appSection usingDatabaseAt:(NSString *)path {
  
  NSString *conditions = [self conditionsFor:appSection];
  
  NSString *query = [NSString stringWithFormat: @"SELECT "
            "task.uuid AS uuid, "
            "task.creationDate AS creationDate, "
            "task.status AS status, "
            "task.title AS title, "
            "task.notes AS notes, "
            "task.area AS area "
            "FROM TMTask as task "
            "LEFT JOIN TMTask AS project "
            "ON task.project = project.uuid "
            "LEFT JOIN TMTask AS heading "
            "ON task.heading = heading.uuid "
            "LEFT JOIN TMTask AS headingProject "
            "ON heading.project = headingProject.uuid "
            "WHERE task.type = 1 "
            "AND task.trashed = 0 "
            "AND task.status = 0 "
            "%@"
            "GROUP BY task.uuid "
            "ORDER BY title;", conditions];

  NSArray *results = [SQLHelper executeSql:query onFile:path];
  NSMutableArray *objects = [NSMutableArray array];
  [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) {
    NSLog(@"WE PROJECTIN!");
    [objects addObject:[ObjectHelper projectFromSQLResult:dictionary usingPath:path ]];
  }];
  
  return objects;
}

- (NSArray *)tasksForAppSection:(ThingsAppSectionKey)appSection usingDatabaseAt:(NSString *)path {
  
  NSString *conditions = [self conditionsFor:appSection];
  
  NSString *query = [NSString stringWithFormat: @"SELECT "
            "task.uuid AS uuid, "
            "task.creationDate AS creationDate, "
            "task.status AS status, "
            "task.title AS title, "
            "task.notes AS notes, "
            "task.area AS area "
            "FROM TMTask as task "
            "LEFT JOIN TMTask AS project "
            "ON task.project = project.uuid "
            "LEFT JOIN TMTask AS heading "
            "ON task.heading = heading.uuid "
            "LEFT JOIN TMTask AS headingProject "
            "ON heading.project = headingProject.uuid "
            "WHERE task.type = 0 "
            "AND task.trashed = 0 "
            "AND task.status = 0 "
            "%@"
            "GROUP BY task.uuid "
            "ORDER BY title;", conditions];
  
  NSArray *results = [SQLHelper executeSql:query onFile:path];
  NSMutableArray *objects = [NSMutableArray array];
  [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) {
    [objects addObject:[ObjectHelper taskFromSQLResult:dictionary usingPath:path ]];
  }];
  
  return objects;
}

- (NSString *)conditionsFor:(ThingsAppSectionKey)appSection {
  if (appSection == ThingsAppSectionKeyInbox) {
    return @"AND task.startDate IS NULL "
            "AND task.area IS NULL "
            "AND task.project IS NULL "
            "AND task.heading IS NULL ";
  }
  if (appSection == ThingsAppSectionKeyToday) {
    return @"AND task.todayIndexReferenceDate IS NOT NULL "
            "AND task.todayIndex <= 0 ";
  }
  if (appSection == ThingsAppSectionKeyUpcoming) {
    return @"AND task.todayIndexReferenceDate IS NOT NULL "
            "AND task.todayIndex > 0 ";
  }
  if (appSection == ThingsAppSectionKeySomeday) {
    return @"AND task.start=2 "
            "AND task.startDate IS NULL "
            "AND task.rt1_instanceCreationStartDate IS NULL ";
  }
  if (appSection == ThingsAppSectionKeyAnytime) {
    return @"AND ( "
      "( "
        "task.todayIndexReferenceDate IS NOT NULL "
        "AND task.todayIndex <= 0 "
      ") "
      "OR ( "
        "task.startDate IS NULL AND "
        "task.start=1 AND "
        "task.rt1_instanceCreationStartDate IS NULL "
        "AND ( "
          "task.area IS NOT NULL "
          "OR task.project IS NOT NULL "
          "OR task.heading IS NOT NULL "
        ") "
        "AND project.todayIndexReferenceDate IS NULL "
        "AND (project.start=1 OR headingProject.start IS NULL) "
        "AND heading.todayIndexReferenceDate IS NULL "
        "AND (heading.start=1 OR headingProject.start IS NULL) "
        "AND headingProject.todayIndexReferenceDate IS NULL "
        "AND (headingProject.start=1 OR headingProject.start IS NULL) "
      ") "
    ") ";
  }
}


@end

NS_ASSUME_NONNULL_END