aboutsummaryrefslogtreecommitdiff
path: root/QSThingsPlugin/Handlers/ThingsTagHandler.m
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-09-12 21:50:40 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-09-12 21:50:40 +0200
commit182872c760d437556d606cc02c6b7c570667a715 (patch)
tree11795bdb259445e5c3f8dd0177e9426ec77b0399 /QSThingsPlugin/Handlers/ThingsTagHandler.m
parent5870e5cf03ceaff7080174b573618631a413110e (diff)
Add children
Diffstat (limited to 'QSThingsPlugin/Handlers/ThingsTagHandler.m')
-rw-r--r--QSThingsPlugin/Handlers/ThingsTagHandler.m134
1 files changed, 134 insertions, 0 deletions
diff --git a/QSThingsPlugin/Handlers/ThingsTagHandler.m b/QSThingsPlugin/Handlers/ThingsTagHandler.m
new file mode 100644
index 0000000..27b93a2
--- /dev/null
+++ b/QSThingsPlugin/Handlers/ThingsTagHandler.m
@@ -0,0 +1,134 @@
+#import "ThingsTagHandler.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@implementation ThingsTagHandler
+
+- (BOOL)objectHasChildren:(QSObject *)object {
+ return YES;
+}
+
+- (void)setQuickIconForObject:(QSObject *)object {
+ NSImage *baseImage = [QSResourceManager imageNamed:@"com.culturedcode.ThingsMac"];
+ NSColor *color = [NSColor colorWithRed:0.263 green:0.502 blue:0.376 alpha:1.0];
+ NSImage *imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"tag.circle" ofColor:color];
+
+ if (imageWithBadge) {
+ [object setIcon:imageWithBadge];
+ } else {
+ [object setIcon:baseImage];
+ }
+}
+
+- (BOOL)loadChildrenForObject:(QSObject *)object {
+ NSDictionary *contents = [object objectForType:kTagType];
+
+ 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 areasForTagIdentifiedBy:uuid usingDatabaseAt:path ]];
+ [children addObjectsFromArray:[self projectsForTagIdentifiedBy:uuid usingDatabaseAt:path ]];
+ [children addObjectsFromArray:[self tasksForTagIdentifiedBy:uuid usingDatabaseAt:path ]];
+
+ [object setChildren:children];
+ return YES;
+}
+
+- (NSArray *)projectsForTagIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path {
+ NSString *query = [NSString stringWithFormat: @"SELECT "
+ "project.uuid as uuid, "
+ "project.creationDate as creationDate, "
+ "project.status as status, "
+ "project.title as title, "
+ "project.notes as notes, "
+ "project.area as area "
+ "FROM TMTag AS tag "
+ "LEFT JOIN TMTaskTag as junction "
+ "ON junction.tags = tag.uuid "
+ "LEFT JOIN TMTask as project "
+ "ON junction.tasks = project.uuid "
+ "WHERE tag.uuid = '%@' "
+ "AND project.uuid IS NOT NULL "
+ "AND project.type = 1 "
+ "AND project.status = 0 "
+ "AND project.trashed = 0 "
+ "ORDER BY project.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 *)tasksForTagIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path {
+ 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 TMTag AS tag "
+ "LEFT JOIN TMTaskTag as junction "
+ "ON junction.tags = tag.uuid "
+ "LEFT JOIN TMTask as task "
+ "ON junction.tasks = task.uuid "
+ "WHERE tag.uuid = '%@' "
+ "AND task.uuid IS NOT NULL "
+ "AND task.type = 0 "
+ "AND task.status = 0 "
+ "AND task.trashed = 0 "
+ "ORDER BY task.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;
+}
+
+- (NSArray *)areasForTagIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path {
+ NSString *query = [NSString stringWithFormat: @"SELECT "
+ "area.uuid as uuid, "
+ "area.title as title "
+ "FROM TMTag AS tag "
+ "LEFT JOIN TMAreaTag as junction "
+ "ON junction.tags = tag.uuid "
+ "LEFT JOIN TMArea as area "
+ "ON junction.areas = area.uuid "
+ "WHERE tag.uuid = '%@' "
+ "AND area.uuid IS NOT NULL "
+ "ORDER BY area.title;", uuid];
+
+ NSArray *results = [SQLHelper executeSql:query onFile:path];
+ NSMutableArray *objects = [NSMutableArray array];
+ [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) {
+ [objects addObject:[ObjectHelper areaFromSQLResult:dictionary usingPath:path ]];
+ }];
+
+ return objects;
+}
+
+@end
+
+NS_ASSUME_NONNULL_END