headstak / StakDB.m

//
//  StakDB.m
//  headstak
//
//  Created by Veghead on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "StakDB.h"
#import "StakItem.h"
#import "FMDatabaseAdditions.h"
#import "STakView.h"

#define HSAppDomain @"com.dreadtech.headstak"
#define HSDBName    @"headstak.db"

@implementation StakDB
@synthesize itemTypes = _itemTypes;

+ (id)stackDB {
    // Look for Application Support directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask, YES);
    if (![paths count]) {
        // Fallback to /tmp
        paths = [NSArray arrayWithObject:@"/tmp"];
    }
    
    NSString *fullPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:HSAppDomain];
    [[NSFileManager defaultManager] createDirectoryAtPath:fullPath
                              withIntermediateDirectories:YES
                                               attributes:nil
                                                    error:nil];
    
   return [self databaseWithPath:[fullPath stringByAppendingPathComponent:HSDBName]];
}

- (BOOL)open {
    _itemTypes = nil;
    if (! [super open]) return NO;
    if (! [self tableExists:@"stakitems"]) {
        [self buildFreshStak];
    }
    [self itemTypes];
    return YES;
}

- (void)buildFreshStak {
    NSArray *itypes = [NSArray arrayWithObjects:
                       [NSArray arrayWithObjects:@"1",@"Task",@"#00ff00",@"t",nil],
                       [NSArray arrayWithObjects:@"2",@"Distraction",@"#ff0000",@"d",nil],
                       [NSArray arrayWithObjects:@"3",@"Sidetracked",@"#0000ff",@"d",nil],
                        nil];
    
    if (![self executeUpdate:@"CREATE TABLE IF NOT EXISTS itemtypes \
     (id INTEGER PRIMARY KEY NOT NULL,\
     description TEXT,\
     colour TEXT,\
     hotkey CHAR, \
     pop CHAR)",nil]) {
        NSLog(@"insert failed");
    }
    
    [self executeUpdate:
     @"CREATE TABLE IF NOT EXISTS stakitems \
     (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, \
     description TEXT, \
     pushdate DATETIME NOT NULL, \
     popdate DATETIME, \
     itemtype INTEGER NOT NULL )",nil];
    
    for (NSArray *itype in itypes) {
        [self setTraceExecution:YES];
        [self executeUpdate:@"INSERT INTO itemtypes (id,description, colour, hotkey) VALUES (?,?,?,?)"
       withArgumentsInArray:itype];
    }
}

- (NSDictionary *)itemTypes {
    if (! _itemTypes) {
        NSMutableDictionary  *coloursById       = [NSMutableDictionary dictionary];
        NSMutableDictionary  *descriptionsById  = [NSMutableDictionary dictionary];
        NSMutableDictionary  *idByHotkey        = [NSMutableDictionary dictionary];

        FMResultSet *fmr = [self executeQuery:@"SELECT id,description,colour,hotkey FROM itemtypes",nil];
        while ([fmr next]) {
            [coloursById setObject:[fmr stringForColumn:@"colour"] forKey:[fmr stringForColumn:@"id"]];
            [descriptionsById setObject:[fmr stringForColumn:@"description"] forKey:[fmr stringForColumn:@"id"]];
            [idByHotkey setObject:[fmr stringForColumn:@"id"] forKey:[fmr stringForColumn:@"hotkey"]];
        }
        [self setItemTypes:[NSDictionary dictionaryWithObjectsAndKeys:
                            coloursById, @"colours",
                            descriptionsById, @"descriptions",
                            idByHotkey, @"ids",nil]];
    }
    NSLog(@"%@",_itemTypes);
    return _itemTypes;
}

- (NSString *)typeNameForId:(NSUInteger)typeId {
    NSDictionary *descriptions = [[self itemTypes] objectForKey:@"descriptions"];
    return [descriptions objectForKey:[NSString stringWithFormat:@"%lu",typeId]];
}

- (void)loadStakIntoHolder:(NSMutableArray *)holder {
    FMResultSet *fmr = [self executeQuery:@"SELECT * FROM stakitems\
                        WHERE popdate IS NULL\
                        ORDER BY pushdate",nil];
    while ([fmr next]) {
        StakItem *item = [[StakItem alloc] initWithDictionary:[fmr resultDictionary] inDb:self ];
        [holder addObject:item];
    }
}
@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.