aboutsummaryrefslogtreecommitdiff
path: root/QSThingsPlugin/Handlers/ThingsAreaHandler.m
blob: 6619a0e9e8d429a0cc39471d1f51e7ada4c21f80 (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
#import "ThingsAreaHandler.h"

NS_ASSUME_NONNULL_BEGIN

@implementation ThingsAreaHandler

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

- (void)setQuickIconForObject:(QSObject *)object {
  NSImage *baseImage = [QSResourceManager imageNamed:@"com.culturedcode.ThingsMac"];
  NSColor *color = [NSColor colorWithRed:0.478 green:0.702 blue:0.584 alpha:1.0];
  NSImage *imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"cube.circle" ofColor:color];
  
  if (imageWithBadge) {
    [object setIcon:imageWithBadge];
  } else {
    [object setIcon:baseImage];
  }
}

- (BOOL)loadChildrenForObject:(QSObject *)object {
  NSDictionary *contents = [object objectForType:kAreaType];
  
  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;
  }
  
  NSString *uuid = [contents objectForKey:@"uuid"];
  if (!uuid) {
    NSLog(@"Malformed Things Task: No uuid.");
    return NO;
  }
  
  NSMutableArray *children = [NSMutableArray array];
  [children addObjectsFromArray:[self tagsForAreaIdentifiedBy:uuid usingDatabaseAt:path ]];
  [children addObjectsFromArray:[self tasksForAreaIdentifiedBy:uuid usingDatabaseAt:path ]];
  [children addObjectsFromArray:[self projectsForAreaIdentifiedBy:uuid usingDatabaseAt:path ]];

  [object setChildren:children];
  return YES;
}

- (NSArray *)tagsForAreaIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path {
  NSString *query = [NSString stringWithFormat: @"SELECT "
            "tag.uuid as uuid, "
            "tag.title as title "
            "FROM TMArea AS area "
            "LEFT JOIN TMAreaTag as junction "
            "ON junction.areas = area.uuid "
            "LEFT JOIN TMTag as tag "
            "ON junction.tags = tag.uuid "
            "WHERE area.uuid = '%@' "
            "AND tag.uuid IS NOT NULL "
            "ORDER BY tag.title;", uuid];
  
  NSArray *results = [SQLHelper executeSql:query onFile:path];
  NSMutableArray *objects = [NSMutableArray array];
  [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) {
    [objects addObject:[ObjectHelper tagFromSQLResult:dictionary usingPath:path ]];
  }];
  
  return objects;
}

- (NSArray *)projectsForAreaIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path {
  NSString *query = [NSString stringWithFormat: @"SELECT "
            "uuid, "
            "creationDate, "
            "status, "
            "title, "
            "notes, "
            "area "
            "FROM TMTask "
            "WHERE area = '%@' "
            "AND type = 1 "
            "AND trashed = 0 "
            "AND status = 0 "
            "ORDER BY title;", uuid];
  
  NSArray *results = [SQLHelper executeSql:query onFile:path];
  NSMutableArray *objects = [NSMutableArray array];
  [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) {
    [objects addObject:[ObjectHelper projectFromSQLResult:dictionary usingPath:path ]];
  }];
  
  return objects;
}

- (NSArray *)tasksForAreaIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path {
  NSString *query = [NSString stringWithFormat: @"SELECT "
            "uuid, "
            "creationDate, "
            "status, "
            "title, "
            "notes, "
            "area "
            "FROM TMTask "
            "WHERE area = '%@' "
            "AND type = 0 "
            "AND trashed = 0 "
            "AND status = 0 "
            "ORDER BY title;", uuid];
  
  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;
}

@end

NS_ASSUME_NONNULL_END