diff options
author | stuconnolly <stuart02@gmail.com> | 2009-08-29 19:39:28 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2009-08-29 19:39:28 +0000 |
commit | 08e024dd1af93f9b05a4bd18b227845822082175 (patch) | |
tree | 2fbfacf1f79ef2bbb20eecee999207f28c365343 | |
parent | fb82d1f21998fb4328bf04372477592e5a1af9f7 (diff) | |
download | sequelpro-08e024dd1af93f9b05a4bd18b227845822082175.tar.gz sequelpro-08e024dd1af93f9b05a4bd18b227845822082175.tar.bz2 sequelpro-08e024dd1af93f9b05a4bd18b227845822082175.zip |
More data export work.
-rw-r--r-- | Source/SPCSVExporter.h | 15 | ||||
-rw-r--r-- | Source/SPCSVExporter.m | 37 | ||||
-rw-r--r-- | Source/SPExportController.h | 19 | ||||
-rw-r--r-- | Source/SPExportController.m | 116 | ||||
-rw-r--r-- | Source/SPExporter.h | 4 | ||||
-rw-r--r-- | Source/SPExporterAccess.h | 40 | ||||
-rw-r--r-- | Source/SPSQLExporter.h | 40 | ||||
-rw-r--r-- | Source/SPSQLExporter.m | 46 |
8 files changed, 297 insertions, 20 deletions
diff --git a/Source/SPCSVExporter.h b/Source/SPCSVExporter.h index 31942cd2..3fa6f276 100644 --- a/Source/SPCSVExporter.h +++ b/Source/SPCSVExporter.h @@ -25,10 +25,11 @@ #import <Cocoa/Cocoa.h> -#import "SPExporter.h" #import "MCPKit.h" +#import "SPExporter.h" +#import "SPExporterAccess.h" -@interface SPCSVExporter : SPExporter +@interface SPCSVExporter : SPExporter <SPExporterAccess> { // CSV file NSFileHandle *csvFileHandle; @@ -48,6 +49,10 @@ // CSV encoding NSStringEncoding csvOutputEncoding; + + // Operational + BOOL csvExportIsRunning; + BOOL csvThreadShouldExit; } @property (readwrite, retain) NSFileHandle *csvFileHandle; @@ -65,8 +70,10 @@ @property (readwrite, assign) NSStringEncoding csvOutputEncoding; -- (id)initWithFileHandle:(NSFileHandle *)fileHandle; +@property (readwrite, assign) BOOL csvExportIsRunning; +@property (readwrite, assign) BOOL csvThreadShouldExit; -- (BOOL)startCSVExport; +- (BOOL)startExportProcess; +- (BOOL)stopExportProcess; @end diff --git a/Source/SPCSVExporter.m b/Source/SPCSVExporter.m index c4a33fb0..a157b051 100644 --- a/Source/SPCSVExporter.m +++ b/Source/SPCSVExporter.m @@ -49,22 +49,26 @@ @synthesize csvOutputEncoding; +@synthesize csvExportIsRunning; +@synthesize csvThreadShouldExit; + /** - * Initialize an instance of the exporter using the supplied file handle. + * Initialize an instance of the exporter setting some default values */ -- (id)initWithFileHandle:(NSFileHandle *)fileHandle +- (id)init { if ((self == [super init])) { - [self setCsvFileHandle:fileHandle]; + [self setCsvExportIsRunning:NO]; + [self setCsvThreadShouldExit:NO]; } return self; } /** - * + * Start the CSV export process. */ -- (BOOL)startCSVExport +- (BOOL)startExportProcess { // Check that we have all the required info before starting the export if ((![self csvFileHandle]) || @@ -94,10 +98,14 @@ if (delegate && [delegate respondsToSelector:@selector(exportProcessDidStart:)]) { [delegate exportProcessDidStart:self]; } + + [self setCsvExportIsRunning:YES]; // Start the export in a new thread [NSThread detachNewThreadSelector:@selector(startCSVExportInBackgroundThread) toTarget:self withObject:nil]; + [self setCsvExportIsRunning:NO]; + // Tell the delegate that the export process has ended if (delegate && [delegate respondsToSelector:@selector(exportProcessDidEnd:)]) { [delegate exportProcessDidEnd:self]; @@ -107,6 +115,18 @@ } /** + * Stop the CSV export process by killing the export thread and cleaning up if its running. + */ +- (BOOL)stopExportProcess +{ + if (![self csvExportIsRunning]) return NO; + + // Kill the running thread here + + return YES; +} + +/** * Dealloc */ - (void)dealloc @@ -210,6 +230,13 @@ // Walk through the supplied data constructing the CSV string for (i = startingRow; i < totalRows; i++) { + // Check if we should stop and exit the export operation + if ([self csvThreadShouldExit]) { + [pool release]; + + return; + } + // Update the progress value if (totalRows) [self setProgressValue:(((i + 1) * 100) / totalRows)]; diff --git a/Source/SPExportController.h b/Source/SPExportController.h index 8e901f9e..61447063 100644 --- a/Source/SPExportController.h +++ b/Source/SPExportController.h @@ -46,22 +46,29 @@ typedef NSUInteger SPExportSource; @interface SPExportController : NSObject { - // Table Document + // Table document IBOutlet id tableDocumentInstance; IBOutlet id tableWindow; - // Tables List + // Tables list IBOutlet id tablesListInstance; - // Export Window + // Export window IBOutlet id exportWindow; IBOutlet id exportToolbar; + IBOutlet id exportPathField; IBOutlet id exportTableList; IBOutlet id exportTabBar; IBOutlet id exportInputMatrix; IBOutlet id exportFilePerTableCheck; IBOutlet id exportFilePerTableNote; + // Export progress sheet + IBOutlet id exportProgressWindow; + IBOutlet id exportProgressTitle; + IBOutlet id exportProgressText; + IBOutlet id exportProgressIndicator; + // SQL IBOutlet id exportSQLIncludeStructureCheck; IBOutlet id exportSQLIncludeDropSyntaxCheck; @@ -87,13 +94,13 @@ typedef NSUInteger SPExportSource; // PDF IBOutlet id exportPDFIncludeStructureCheck; - // Token Name View + // Token name view IBOutlet id tokenNameView; IBOutlet id tokenNameField; IBOutlet id tokenNameTokensField; IBOutlet id exampleNameLabel; - // Local Variables + // Local variables MCPConnection *mySQLConnection; NSMutableArray *tables; } @@ -101,6 +108,8 @@ typedef NSUInteger SPExportSource; // Export Methods - (void)export; - (IBAction)closeSheet:(id)sender; +- (IBAction)cancelExport:(id)sender; +- (IBAction)changeExportOutputPath:(id)sender; // Utility Methods - (void)setConnection:(MCPConnection *)theConnection; diff --git a/Source/SPExportController.m b/Source/SPExportController.m index 15e47e61..5acc40c0 100644 --- a/Source/SPExportController.m +++ b/Source/SPExportController.m @@ -23,12 +23,17 @@ // More info at <http://code.google.com/p/sequel-pro/> #import "SPExportController.h" +#import "SPCSVExporter.h" #import "TablesList.h" +#import "TableDocument.h" #import "SPArrayAdditions.h" @implementation SPExportController -- (id)init; +/** + * Initializes an instance of SPExportController + */ +- (id)init { if ((self = [super init])) { tables = [[NSMutableArray alloc] init]; @@ -37,6 +42,9 @@ return self; } +/** + * Upon awakening select the first toolbar item + */ - (void)awakeFromNib { // Upon awakening select the SQL tab @@ -46,13 +54,16 @@ #pragma mark - #pragma mark Export methods +/** + * Display the export window allowing the user to select what and of what type to export. + */ - (void)export { - if (!exportWindow) { - [NSBundle loadNibNamed:@"ExportDialog" owner:self]; - } + if (!exportWindow) [NSBundle loadNibNamed:@"ExportDialog" owner:self]; [self loadTables]; + + [exportPathField setStringValue:NSHomeDirectory()]; [NSApp beginSheet:exportWindow modalForWindow:tableWindow @@ -61,12 +72,34 @@ contextInfo:nil]; } +/** + * Close the export dialog + */ - (IBAction)closeSheet:(id)sender { [NSApp endSheet:exportWindow returnCode:[sender tag]]; [exportWindow orderOut:self]; } +/** + * + */ +- (IBAction)cancelExport:(id)sender +{ + // Cancel the export operation here +} + +- (IBAction)changeExportOutputPath:(id)sender +{ + NSOpenPanel *panel = [NSOpenPanel openPanel]; + + [panel setCanChooseFiles:NO]; + [panel setCanChooseDirectories:YES]; + [panel setCanCreateDirectories:YES]; + + [panel beginSheetForDirectory:NSHomeDirectory() file:nil modalForWindow:exportWindow modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil]; +} + - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { // Perform the export @@ -83,8 +116,81 @@ } } - // Determine what data to use (filtered result, custom query result or selected tables) + // Determine what data to use (filtered result, custom query result or selected tables) for the export operation SPExportSource exportSource = ([exportInputMatrix selectedRow] + 1); + + NSMutableArray *exportTables = [NSMutableArray array]; + + // Get the data depending on the source + switch (exportSource) + { + case SP_FILTERED_EXPORT: + + break; + case SP_CUSTOM_QUERY_EXPORT: + + break; + case SP_TABLE_EXPORT: + // Create an array of tables to export + for (NSMutableArray *table in tables) + { + if ([[table objectAtIndex:0] boolValue]) { + [exportTables addObject:[table objectAtIndex:1]]; + } + } + + break; + } + + // Create the file handle + + + SPExporter *exporter; + SPCSVExporter *csvExporter; + + // Based on the type of export create a new instance of the corresponding exporter and set it's specific options + switch (exportType) + { + case SP_SQL_EXPORT: + + break; + case SP_CSV_EXPORT: + csvExporter = [[SPCSVExporter alloc] init]; + + [csvExporter setCsvOutputFieldNames:[exportCSVIncludeFieldNamesCheck state]]; + [csvExporter setCsvFieldSeparatorString:[exportCSVFieldsTerminatedField stringValue]]; + [csvExporter setCsvEnclosingCharacterString:[exportCSVFieldsWrappedField stringValue]]; + [csvExporter setCsvLineEndingString:[exportCSVLinesTerminatedField stringValue]]; + [csvExporter setCsvEscapeString:[exportCSVFieldsEscapedField stringValue]]; + + [csvExporter setCsvOutputEncoding:[MCPConnection encodingForMySQLEncoding:[[tableDocumentInstance connectionEncoding] UTF8String]]]; + [csvExporter setCsvNULLString:[[NSUserDefaults standardUserDefaults] objectForKey:@"NullValue"]]; + + exporter = csvExporter; + break; + case SP_XML_EXPORT: + + break; + case SP_PDF_EXPORT: + + break; + case SP_HTML_EXPORT: + + break; + case SP_EXCEL_EXPORT: + + break; + } + + // Set the exporter's delegate + [exporter setDelegate:self]; + } +} + +- (void)savePanelDidEnd:(NSSavePanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo +{ + if (returnCode == NSOKButton) { + [exportPathField setStringValue:[panel directory]]; } } diff --git a/Source/SPExporter.h b/Source/SPExporter.h index 70feb915..9b0f80fd 100644 --- a/Source/SPExporter.h +++ b/Source/SPExporter.h @@ -28,7 +28,9 @@ /** * This class is designed to be the base class of all data exporters and provide basic functionality * common to each of them. Each data exporter (i.e. CSV, SQL, XML, etc.) should be implemented as a subclass - * of this class, with the end result being an uncomplicated export architecture defined by export type. + * of this class, with the end result being a modular export architecture separated by export type. All exporters + * should also conform to the SPExporterAccess protocol to allow generic access to the exporter's state and common + * functionality. * * All export functionality is initially controlled by SPExportController, which is the single point within the * architecture that controls the user interface and provides user feedback. When the user starts an export diff --git a/Source/SPExporterAccess.h b/Source/SPExporterAccess.h new file mode 100644 index 00000000..484a9e69 --- /dev/null +++ b/Source/SPExporterAccess.h @@ -0,0 +1,40 @@ +// +// $Id$ +// +// SPExporterAccess.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on August 29, 2009 +// Copyright (c) 2009 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at <http://code.google.com/p/sequel-pro/> + +#import <Foundation/Foundation.h> + +@protocol SPExporterAccess <NSObject> + +/** + * Starts the export process. + */ +- (BOOL)startExportProcess; + +/** + * Stops the export process and passes control back to SPExportController. + */ +- (BOOL)stopExportProcess; + +@end diff --git a/Source/SPSQLExporter.h b/Source/SPSQLExporter.h new file mode 100644 index 00000000..847a882a --- /dev/null +++ b/Source/SPSQLExporter.h @@ -0,0 +1,40 @@ +// +// $Id$ +// +// SPSQLExporter.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on August 29, 2009 +// Copyright (c) 2009 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at <http://code.google.com/p/sequel-pro/> + +#import <Cocoa/Cocoa.h> + +#import "MCPKit.h" +#import "SPExporter.h" +#import "SPExporterAccess.h" + +@interface SPSQLExporter : SPExporter <SPExporterAccess> +{ + +} + +- (BOOL)startExportProcess; +- (BOOL)stopExportProcess; + +@end diff --git a/Source/SPSQLExporter.m b/Source/SPSQLExporter.m new file mode 100644 index 00000000..73f4ab56 --- /dev/null +++ b/Source/SPSQLExporter.m @@ -0,0 +1,46 @@ +// +// $Id$ +// +// SPSQLExporter.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on August 29, 2009 +// Copyright (c) 2009 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at <http://code.google.com/p/sequel-pro/> + +#import "SPSQLExporter.h" + +@implementation SPSQLExporter + +/** + * Start the SQL export process. + */ +- (BOOL)startExportProcess +{ + return YES; +} + +/** + * Stop the SQL export process by killing the export thread and cleaning up if its running. + */ +- (BOOL)stopExportProcess +{ + return YES; +} + +@end |