Commits

Veg Head committed dc4d2cf

Saving stak as csv

  • Participants
  • Parent commits fe01c39

Comments (0)

Files changed (12)

File MainMenu.xib

 					</object>
 					<int key="connectionID">816</int>
 				</object>
+				<object class="IBConnectionRecord">
+					<object class="IBActionConnection" key="connection">
+						<string key="label">menuHandler:</string>
+						<reference key="source" ref="905284910"/>
+						<reference key="destination" ref="795130291"/>
+					</object>
+					<int key="connectionID">837</int>
+				</object>
 			</object>
 			<object class="IBMutableOrderedSet" key="objectRecords">
 				<object class="NSArray" key="orderedObjects">
 				<reference key="dict.values" ref="0"/>
 			</object>
 			<nil key="sourceID"/>
-			<int key="maxID">836</int>
-		</object>
-		<object class="IBClassDescriber" key="IBDocument.Classes">
-			<object class="NSMutableArray" key="referencedPartialClassDescriptions">
-				<bool key="EncodedWithXMLCoder">YES</bool>
-				<object class="IBPartialClassDescription">
-					<string key="className">StakApplication</string>
-					<string key="superclassName">NSApplication</string>
-					<object class="IBClassDescriptionSource" key="sourceIdentifier">
-						<string key="majorKey">IBProjectSource</string>
-						<string key="minorKey">./Classes/StakApplication.h</string>
-					</object>
-				</object>
-				<object class="IBPartialClassDescription">
-					<string key="className">headstak_AppDelegate</string>
-					<string key="superclassName">NSObject</string>
-					<object class="NSMutableDictionary" key="actions">
-						<string key="NS.key.0">menuHandler:</string>
-						<string key="NS.object.0">id</string>
-					</object>
-					<object class="NSMutableDictionary" key="actionInfosByName">
-						<string key="NS.key.0">menuHandler:</string>
-						<object class="IBActionInfo" key="NS.object.0">
-							<string key="name">menuHandler:</string>
-							<string key="candidateClassName">id</string>
-						</object>
-					</object>
-					<object class="NSMutableDictionary" key="outlets">
-						<bool key="EncodedWithXMLCoder">YES</bool>
-						<object class="NSArray" key="dict.sortedKeys">
-							<bool key="EncodedWithXMLCoder">YES</bool>
-							<string>_stakMenu</string>
-							<string>window</string>
-						</object>
-						<object class="NSArray" key="dict.values">
-							<bool key="EncodedWithXMLCoder">YES</bool>
-							<string>NSMenu</string>
-							<string>NSWindow</string>
-						</object>
-					</object>
-					<object class="NSMutableDictionary" key="toOneOutletInfosByName">
-						<bool key="EncodedWithXMLCoder">YES</bool>
-						<object class="NSArray" key="dict.sortedKeys">
-							<bool key="EncodedWithXMLCoder">YES</bool>
-							<string>_stakMenu</string>
-							<string>window</string>
-						</object>
-						<object class="NSArray" key="dict.values">
-							<bool key="EncodedWithXMLCoder">YES</bool>
-							<object class="IBToOneOutletInfo">
-								<string key="name">_stakMenu</string>
-								<string key="candidateClassName">NSMenu</string>
-							</object>
-							<object class="IBToOneOutletInfo">
-								<string key="name">window</string>
-								<string key="candidateClassName">NSWindow</string>
-							</object>
-						</object>
-					</object>
-					<object class="IBClassDescriptionSource" key="sourceIdentifier">
-						<string key="majorKey">IBProjectSource</string>
-						<string key="minorKey">./Classes/headstak_AppDelegate.h</string>
-					</object>
-				</object>
-			</object>
+			<int key="maxID">837</int>
 		</object>
+		<object class="IBClassDescriber" key="IBDocument.Classes"/>
 		<int key="IBDocument.localizationMode">0</int>
 		<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaFramework</string>
 		<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies">

File MainStak.xib

 <archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="8.00">
 	<data>
 		<int key="IBDocument.SystemTarget">1080</int>
-		<string key="IBDocument.SystemVersion">12A256</string>
-		<string key="IBDocument.InterfaceBuilderVersion">2541</string>
-		<string key="IBDocument.AppKitVersion">1185</string>
-		<string key="IBDocument.HIToolboxVersion">622.00</string>
+		<string key="IBDocument.SystemVersion">12E55</string>
+		<string key="IBDocument.InterfaceBuilderVersion">3084</string>
+		<string key="IBDocument.AppKitVersion">1187.39</string>
+		<string key="IBDocument.HIToolboxVersion">626.00</string>
 		<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
 			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
-			<string key="NS.object.0">2541</string>
+			<string key="NS.object.0">3084</string>
 		</object>
 		<array key="IBDocument.IntegratedClassDependencies">
 			<string>NSBox</string>
 				<string key="NSWindowContentMaxSize">{480, 600}</string>
 				<string key="NSWindowContentMinSize">{480, 270}</string>
 				<object class="NSView" key="NSWindowView" id="1006">
-					<reference key="NSNextResponder"/>
+					<nil key="NSNextResponder"/>
 					<int key="NSvFlags">256</int>
 					<array class="NSMutableArray" key="NSSubviews">
 						<object class="NSBox" id="711782620">
 															<array class="NSMutableArray" key="NSSubviews"/>
 															<string key="NSFrameSize">{427, 500}</string>
 															<reference key="NSSuperview" ref="220322029"/>
-															<reference key="NSWindow"/>
 															<reference key="NSNextKeyView" ref="834539644"/>
 														</object>
 													</array>
 													<string key="NSFrameSize">{427, 220}</string>
 													<reference key="NSSuperview" ref="432310248"/>
-													<reference key="NSWindow"/>
 													<reference key="NSNextKeyView" ref="324665805"/>
 													<reference key="NSDocView" ref="324665805"/>
 													<object class="NSColor" key="NSBGColor">
 													<int key="NSvFlags">-2147483392</int>
 													<string key="NSFrame">{{-100, -100}, {15, 218}}</string>
 													<reference key="NSSuperview" ref="432310248"/>
-													<reference key="NSWindow"/>
 													<reference key="NSNextKeyView" ref="226143158"/>
 													<bool key="NSEnabled">YES</bool>
 													<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
 													<int key="NSvFlags">-2147483392</int>
 													<string key="NSFrame">{{-100, -100}, {410, 15}}</string>
 													<reference key="NSSuperview" ref="432310248"/>
-													<reference key="NSWindow"/>
 													<reference key="NSNextKeyView" ref="220322029"/>
 													<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
 													<int key="NSsFlags">1</int>
 											</array>
 											<string key="NSFrame">{{7, 7}, {427, 220}}</string>
 											<reference key="NSSuperview" ref="237142718"/>
-											<reference key="NSWindow"/>
 											<reference key="NSNextKeyView" ref="220322029"/>
 											<int key="NSsFlags">149568</int>
 											<reference key="NSVScroller" ref="834539644"/>
 									</array>
 									<string key="NSFrameSize">{440, 234}</string>
 									<reference key="NSSuperview" ref="711782620"/>
-									<reference key="NSWindow"/>
 									<reference key="NSNextKeyView" ref="432310248"/>
 								</object>
 							</array>
 							<string key="NSFrame">{{20, 16}, {440, 234}}</string>
 							<reference key="NSSuperview" ref="1006"/>
-							<reference key="NSWindow"/>
 							<reference key="NSNextKeyView" ref="237142718"/>
 							<string key="NSOffsets">{0, 0}</string>
 							<object class="NSTextFieldCell" key="NSTitleCell">
 							<int key="NSvFlags">270</int>
 							<string key="NSFrame">{{17, 251}, {446, 14}}</string>
 							<reference key="NSSuperview" ref="1006"/>
-							<reference key="NSWindow"/>
 							<reference key="NSNextKeyView" ref="711782620"/>
 							<string key="NSReuseIdentifierKey">_NS:3944</string>
 							<bool key="NSEnabled">YES</bool>
 						</object>
 					</array>
 					<string key="NSFrameSize">{480, 270}</string>
-					<reference key="NSSuperview"/>
-					<reference key="NSWindow"/>
 					<reference key="NSNextKeyView" ref="626424876"/>
 				</object>
 				<string key="NSScreenRect">{{0, 0}, {1440, 878}}</string>

File StakApplication.h

 
 @property (strong, readwrite) NSMutableArray *theStak;
 
+- (void)aboutBox;
 + (NSColor *)colourFromHexRGB:(NSString *) inColorString;
 
 @end

File StakApplication.m

     return self;
 }
 
+- (void)aboutBox {
+    [[NSApplication sharedApplication] orderFrontStandardAboutPanel:self];
+}
+
 /*
  NSColor: Instantiate from Web-like Hex RRGGBB string
  Original Source: <http://cocoa.karelia.com/Foundation_Categories/NSColor__Instantiat.m>
 - (NSString *)typeNameForId:(NSUInteger)typeId;
 - (void)loadStakIntoHolder:(NSMutableArray *)holder;
 - (void)hardDeleteStak;
+- (void)dumpStakToFileHandle:(NSFileHandle *)handle;
 
 @end
 #define HSAppDomain @"com.dreadtech.headstak"
 #define HSDBName    @"headstak.db"
 
+@interface NSString(CSVExtensions)
+- (NSString *)csvField;
+@end
+
+@implementation NSString(CSVExtensions)
+
+- (NSString *)csvField {
+    return [NSString stringWithFormat:@"\"%@\"",
+            [self stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]];
+}
+
+@end
+
+
+
+
 @implementation StakDB
 
 + (id)stackDB {
     }
 }
 
+- (void)dumpStakToFileHandle:(NSFileHandle *)file {
+    FMResultSet *fmr = [self executeQuery:@"SELECT * FROM stakitems\
+                        ORDER BY pushdate",nil];
+    while ([fmr next]) {
+        NSDictionary *dict = [fmr resultDictionary];
+        NSDateFormatter *fmt = [[NSDateFormatter alloc] initWithDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
+                                                      allowNaturalLanguage:NO];
+        [fmt setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
+        NSString *pushTime = [[fmt stringFromDate:[dict objectForKey:@"pushdate"]] csvField];
+        NSString *popTime = [[fmt stringFromDate:[dict objectForKey:@"popdate"]] csvField];
+        NSString *ftype = [[self typeNameForId:[[dict objectForKey:@"id"] integerValue] ] csvField];
+        [file writeData:[[NSString stringWithFormat:@"%@,%@,%@,%@\n",
+                pushTime,
+                popTime,
+                [dict objectForKey:@"description"],
+                ftype] dataUsingEncoding:NSUTF8StringEncoding]];
+    }
+}
+
 - (NSDictionary *)itemTypes {
     if (! _itemTypes) {
         NSMutableDictionary  *coloursById       = [NSMutableDictionary dictionary];
 - (id)initNewItemOfType:(NSUInteger)type inDb:(StakDB *)db;
 - (void)save;
 - (NSColor *)colour;
+- (NSString *)asCSV;
+
 @end
 #import "StakDB.h"
 #import "StakApplication.h"
 
+
 @implementation StakItem
 
 
         [formatter setTimeStyle:NSDateFormatterNoStyle];
         [formatter setDateStyle:NSDateFormatterMediumStyle];
         NSString *dateDesc = [NSDateFormatter localizedStringFromDate:_pushTime 
-         dateStyle:NSDateFormatterMediumStyle
-         timeStyle:NSDateFormatterMediumStyle];
+                                                            dateStyle:NSDateFormatterMediumStyle
+                                                            timeStyle:NSDateFormatterMediumStyle];
         
         _itemType = type;
         NSString *itemTypeString = [db typeNameForId:_itemType];

File StakViewController.m

 
 NSObject *_stakLock;
 
+
 @implementation StakViewController
 
 - (id)init
     [self layoutItemViews];
 }
 
+- (void)exportStakToURL:(NSURL *)exportFileURL {
+    NSFileHandle *file;
+    NSError *err;
+    [[NSFileManager defaultManager] createFileAtPath:[exportFileURL path] contents:nil attributes:nil];
+    file = [NSFileHandle fileHandleForWritingToURL:exportFileURL
+                                             error:&err];
+    
+    if (file == nil) {
+        NSLog(@"Failed to open file with error %@", err);
+        return;
+    }
+    
+    [[self db] dumpStakToFileHandle:file];
+    
+    [file closeFile];
+}
+
 - (void)exportStakSheet {
+    NSSavePanel *exportPanel = [NSSavePanel savePanel];
+    [exportPanel setAllowedFileTypes:[NSArray arrayWithObject:@"csv"]];
+    NSArray *dirs = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory
+                                                          inDomains:NSUserDomainMask];
+    [exportPanel setDirectoryURL:dirs[0]];
+    // Perform other setup
+    // Use a completion handler -- this is a block which takes one argument
+    // which corresponds to the button that was clicked
+    [exportPanel beginWithCompletionHandler:^(NSInteger result){
+        if (result == NSFileHandlingPanelOKButton) {
+            
+            NSLog(@"Got URL: %@", [exportPanel URL]);
+            [self exportStakToURL:[exportPanel URL]];
+            // Do what you need to do with the selected path
+        }
+    }];
 }
 
 

File Untitled.xcworkspace/xcuserdata/veg.xcuserdatad/UserInterfaceState.xcuserstate

Binary file removed.

File headstak_AppDelegate.h

 };
 
 @property (nonatomic, strong) IBOutlet NSWindow *window;
-@property (nonatomic, strong) StakViewController *hscontroller;
+@property (nonatomic, strong) StakViewController *controller;
 @property (strong, nonatomic) NSMenu *stakMenu;
 
 - (IBAction)menuHandler:(id)sender;

File headstak_AppDelegate.m

 @implementation headstak_AppDelegate
 
 -(void)awakeFromNib {
-    self.hscontroller = [[StakViewController alloc] init];
+    self.controller = [[StakViewController alloc] init];
     EventHotKeyRef hotKeyRef;
     EventHotKeyID hotKeyID;
     EventTypeSpec eventType;
     [statusItem setImage:[NSImage imageNamed:@"stakicon"]];
     [statusItem setMenu:[self stakMenu]];
     [statusItem setHighlightMode:YES];
-    StakWindowController *swc = [[self hscontroller] swc];
-    for (StakItem *item in [[self hscontroller] stakHolder]) {
+    StakWindowController *swc = [[self controller] swc];
+    for (StakItem *item in [[self controller] stakHolder]) {
         [[swc stakView] addSubview:(NSView *)[item viewInStakView:[swc stakView]]];
     }
-    [self.window makeFirstResponder:[self.hscontroller swc]];
+    [self.window makeFirstResponder:[self.controller swc]];
 }
 
 
 - (IBAction)menuHandler:(id)sender {
     switch ([sender tag]) {
-        case (STAK_MENU_PREFS):
-            [[[self hscontroller] spc] openStakWindow];
-            break;
+        //case (STAK_MENU_PREFS):
+        //    [[[self hscontroller] spc] openStakWindow];
+        //    break;
         case (STAK_MENU_CLEAR_HIST):
-            [[self hscontroller] clearStakWarning];
+            [[self controller] clearStakWarning];
             break;
         case (STAK_MENU_EXPORT_HIST):
-            [[self hscontroller] exportStakSheet];
+            [[self controller] exportStakSheet];
             break;
         // N.B. default also deals with a nil sender
         default:
         case (STAK_MENU_SHOW):
-            [[[self hscontroller] swc] openStakWindow];
+            [[[self controller] swc] openStakWindow];
             break;
     }
 }