From f3e65efba3f76cb2149fd8427af9463032d3c45d Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 12 Jun 2011 13:56:11 +0000 Subject: Add the ability to export favorites. --- Source/SPConnectionController.h | 4 +- Source/SPConnectionController.m | 17 ++++- Source/SPConnectionControllerDelegate.h | 2 +- Source/SPConnectionControllerDelegate.m | 16 +++++ Source/SPConstants.h | 3 + Source/SPConstants.m | 3 + Source/SPFavoritesController.m | 4 +- Source/SPFavoritesExportProtocol.h | 42 +++++++++++++ Source/SPFavoritesExporter.h | 52 ++++++++++++++++ Source/SPFavoritesExporter.m | 107 ++++++++++++++++++++++++++++++++ Source/SPFavoritesImporter.h | 31 +++++++++ Source/SPFavoritesImporter.m | 30 +++++++++ 12 files changed, 307 insertions(+), 4 deletions(-) create mode 100644 Source/SPFavoritesExportProtocol.h create mode 100644 Source/SPFavoritesExporter.h create mode 100644 Source/SPFavoritesExporter.m create mode 100644 Source/SPFavoritesImporter.h create mode 100644 Source/SPFavoritesImporter.m (limited to 'Source') diff --git a/Source/SPConnectionController.h b/Source/SPConnectionController.h index a1a99526..83c38172 100644 --- a/Source/SPConnectionController.h +++ b/Source/SPConnectionController.h @@ -24,6 +24,7 @@ // More info at #import +#import "SPFavoritesExportProtocol.h" #import "SPConnectionControllerDelegateProtocol.h" #ifndef SP_REFACTOR /* headers */ @@ -51,7 +52,7 @@ #endif -@interface SPConnectionController : NSObject +@interface SPConnectionController : NSObject { id delegate; @@ -160,6 +161,7 @@ IBOutlet NSProgressIndicator *progressIndicator; IBOutlet NSTextField *progressIndicatorText; IBOutlet NSMenuItem *favoritesSortByMenuItem; + IBOutlet NSView *exportPanelAccessoryView; BOOL isEditing; BOOL reverseFavoritesSort; diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 0f12303f..6ed6e392 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -38,6 +38,8 @@ #import "SPGeneralPreferencePane.h" #import "SPDatabaseViewController.h" #import "SPTreeNode.h" +#import "SPFavoritesExporter.h" +#import "SPFavoritesImporter.h" // Constants static NSString *SPRemoveNode = @"RemoveNode"; @@ -989,6 +991,8 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v { NSSavePanel *savePanel = [NSSavePanel savePanel]; + [savePanel setAccessoryView:exportPanelAccessoryView]; + [savePanel beginSheetForDirectory:nil file:SPExportFavoritesFilename modalForWindow:[dbDocument parentWindow] @@ -1131,9 +1135,20 @@ static NSComparisonResult compareFavoritesUsingKey(id favorite1, id favorite2, v /** * Called when the user dismisses either the import of export favorites panels. */ -- (void)importExportFavoritesSheetDidEnd:(NSOpenPanel *)openPanel returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo +- (void)importExportFavoritesSheetDidEnd:(NSOpenPanel *)panel returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo { + NSError *error = nil; + if (contextInfo == SPExportFavorites) { + SPFavoritesExporter *exporter = [[[SPFavoritesExporter alloc] init] autorelease]; + + [exporter setDelegate:self]; + + [exporter writeFavorites:[self selectedFavoriteNodes] toFile:[panel filename] error:&error]; + } + else if (contextInfo == SPImportFavorites) { + //SPFavoritesImporter *importer = [[SPFavoritesImporter alloc] init]; + } } /** diff --git a/Source/SPConnectionControllerDelegate.h b/Source/SPConnectionControllerDelegate.h index 4936ee19..25d0a02d 100644 --- a/Source/SPConnectionControllerDelegate.h +++ b/Source/SPConnectionControllerDelegate.h @@ -32,6 +32,6 @@ * * Connection controller delegate category. */ -@interface SPConnectionController (SPConnectionControllerDelegate) +@interface SPConnectionController (SPConnectionControllerDelegate) @end diff --git a/Source/SPConnectionControllerDelegate.m b/Source/SPConnectionControllerDelegate.m index b86e716f..03dbd930 100644 --- a/Source/SPConnectionControllerDelegate.m +++ b/Source/SPConnectionControllerDelegate.m @@ -492,4 +492,20 @@ static NSString *SPDatabaseImage = @"database-small"; return YES; } +#pragma mark - +#pragma mark Favorites import/export delegate methods + +/** + * Called by the favorites exporter when the export completes. + */ +- (void)favoritesExportCompletedWithError:(NSError *)error +{ + if (error) { + [[NSAlert alertWithError:error] beginSheetModalForWindow:[dbDocument parentWindow] + modalDelegate:self + didEndSelector:NULL + contextInfo:NULL]; + } +} + @end diff --git a/Source/SPConstants.h b/Source/SPConstants.h index 4ff7ee31..502f0d81 100644 --- a/Source/SPConstants.h +++ b/Source/SPConstants.h @@ -448,6 +448,9 @@ extern NSString *SPFavoriteSSLCertificateFileLocationKey; extern NSString *SPFavoriteSSLCACertFileLocationEnabledKey; extern NSString *SPFavoriteSSLCACertFileLocationKey; +// Favorites import/export +extern NSString *SPFavoritesDataRootKey; + // Bundle Files and Bundle Editor extern NSString *SPBundleScopeQueryEditor; extern NSString *SPBundleScopeDataTable; diff --git a/Source/SPConstants.m b/Source/SPConstants.m index f8e3fe4f..ecb91e41 100644 --- a/Source/SPConstants.m +++ b/Source/SPConstants.m @@ -254,6 +254,9 @@ NSString *SPFavoriteSSLCertificateFileLocationKey = @"sslCertificateFileL NSString *SPFavoriteSSLCACertFileLocationEnabledKey = @"sslCACertFileLocationEnabled"; NSString *SPFavoriteSSLCACertFileLocationKey = @"sslCACertFileLocation"; +// Favorites import/export +NSString *SPFavoritesDataRootKey = @"SPConnectionFavorites"; + // Bundle Files and Bundle Editor NSString *SPBundleScopeQueryEditor = @"editor"; NSString *SPBundleScopeDataTable = @"datatable"; diff --git a/Source/SPFavoritesController.m b/Source/SPFavoritesController.m index 23be83d7..1ac410e5 100644 --- a/Source/SPFavoritesController.m +++ b/Source/SPFavoritesController.m @@ -104,7 +104,9 @@ static SPFavoritesController *sharedFavoritesController = nil; { pthread_mutex_lock(&favoritesLock); - [NSThread detachNewThreadSelector:@selector(_saveFavoritesDataInBackground:) toTarget:self withObject:[[[favoritesTree childNodes] objectAtIndex:0] dictionaryRepresentation]]; + [NSThread detachNewThreadSelector:@selector(_saveFavoritesDataInBackground:) + toTarget:self + withObject:[[[favoritesTree childNodes] objectAtIndex:0] dictionaryRepresentation]]; pthread_mutex_unlock(&favoritesLock); } diff --git a/Source/SPFavoritesExportProtocol.h b/Source/SPFavoritesExportProtocol.h new file mode 100644 index 00000000..59daf996 --- /dev/null +++ b/Source/SPFavoritesExportProtocol.h @@ -0,0 +1,42 @@ +// +// $Id$ +// +// SPFavoritesExportProtocol.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on June 11, 2011 +// Copyright (c) 2011 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 + +/** + * @protocol SPFavoritesExportProtocol SPFavoritesExportProtocol.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * Favorites exporter delegate protocol. + */ +@protocol SPFavoritesExportProtocol + +/** + * Invoked when the favorites export proccess completes + * + * @param error An error instance. Anything other than nil indicates an error occurred. + */ +- (void)favoritesExportCompletedWithError:(NSError *)error; + +@end diff --git a/Source/SPFavoritesExporter.h b/Source/SPFavoritesExporter.h new file mode 100644 index 00000000..058c5a42 --- /dev/null +++ b/Source/SPFavoritesExporter.h @@ -0,0 +1,52 @@ +// +// $Id$ +// +// SPFavoritesExporter.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on May 14, 2011 +// Copyright (c) 2011 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 + +#import "SPFavoritesExportProtocol.h" + +@interface SPFavoritesExporter : NSObject +{ + NSObject *delegate; + + NSError *exportError; + + NSString *exportPath; + NSArray *exportFavorites; +} + +@property (readwrite, assign) NSObject *delegate; + +/** + * @property exportPath The file path to export to + */ +@property (readwrite, retain) NSString *exportPath; + +/** + * @property exportFavorites The array of favorites to be exported + */ +@property (readwrite, retain) NSArray *exportFavorites; + +- (void)writeFavorites:(NSArray *)favorites toFile:(NSString *)path error:(NSError **)error; + +@end diff --git a/Source/SPFavoritesExporter.m b/Source/SPFavoritesExporter.m new file mode 100644 index 00000000..a818fdcf --- /dev/null +++ b/Source/SPFavoritesExporter.m @@ -0,0 +1,107 @@ +// +// $Id$ +// +// SPFavoritesExporter.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on May 14, 2011 +// Copyright (c) 2011 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 + +#import "SPFavoritesExporter.h" +#import "SPTreeNode.h" + +@interface SPFavoritesExporter () + +- (void)_writeFavoritesInBackground; + +@end + +@implementation SPFavoritesExporter + +@synthesize delegate; +@synthesize exportPath; +@synthesize exportFavorites; + +/*** + * Write the supplied array of favorites to the file at the supplied path. + * + * @param favorites The array of favorites to be written + * @param path The file system path that the file is to be written to + * @param filename The filename of the file to be written + * @param error Upon return if an error occurred contains the NSError instance + * + * @return A BOOL indicating the success of the operation + */ +- (void)writeFavorites:(NSArray *)favorites toFile:(NSString *)path error:(NSError **)error +{ + [self setExportFavorites:favorites]; + [self setExportPath:path]; + + [NSThread detachNewThreadSelector:@selector(_writeFavoritesInBackground) toTarget:self withObject:nil]; +} + +/** + * Writes the favorites array to disk in plist format on separate thread. + */ +- (void)_writeFavoritesInBackground +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSError *error = nil; + NSString *errorString = nil; + + NSMutableArray *favorites = [[NSMutableArray alloc] init]; + + // Get a dictionary representation of all favorites + for (SPTreeNode *node in [self exportFavorites]) + { + [favorites addObject:[node dictionaryRepresentation]]; + } + + NSDictionary *dictionary = [NSDictionary dictionaryWithObject:favorites forKey:SPFavoritesDataRootKey]; + + [favorites release]; + + // Convert the current favorites tree to a dictionary representation to create the plist data + NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dictionary + format:NSPropertyListXMLFormat_v1_0 + errorDescription:&errorString]; + + if (plistData) { + [plistData writeToFile:[self exportPath] options:NSAtomicWrite error:&error]; + + if (error) { + NSLog(@"Error writing favorites data: %@", [error localizedDescription]); + } + } + else if (errorString) { + NSLog(@"Error converting favorites data to plist format: %@", errorString); + + [errorString release]; + } + + // Inform the delegate that the export has completed and pass the error instance + if ([self delegate] && [[self delegate] respondsToSelector:@selector(favoritesExportCompletedWithError:)]) { + [[self delegate] performSelectorOnMainThread:@selector(favoritesExportCompletedWithError:) withObject:error waitUntilDone:NO]; + } + + [pool release]; +} + +@end diff --git a/Source/SPFavoritesImporter.h b/Source/SPFavoritesImporter.h new file mode 100644 index 00000000..fefc54e4 --- /dev/null +++ b/Source/SPFavoritesImporter.h @@ -0,0 +1,31 @@ +// +// $Id$ +// +// SPFavoritesImporter.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on May 14, 2011 +// Copyright (c) 2011 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 + +@interface SPFavoritesImporter : NSObject +{ + +} + +@end diff --git a/Source/SPFavoritesImporter.m b/Source/SPFavoritesImporter.m new file mode 100644 index 00000000..4b62d5d0 --- /dev/null +++ b/Source/SPFavoritesImporter.m @@ -0,0 +1,30 @@ +// +// $Id$ +// +// SPFavoritesImporter.m +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on May 14, 2011 +// Copyright (c) 2011 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 + +#import "SPFavoritesImporter.h" + +@implementation SPFavoritesImporter + +@end -- cgit v1.2.3