aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Interfaces/English.lproj/QueryFavoriteManager.xib164
-rw-r--r--Source/CMTextView.m2
-rw-r--r--Source/CustomQuery.m64
-rw-r--r--Source/SPQueryController.h2
-rw-r--r--Source/SPQueryController.m11
-rw-r--r--Source/SPQueryFavoriteManager.h23
-rw-r--r--Source/SPQueryFavoriteManager.m455
-rw-r--r--Source/SPTableView.m2
8 files changed, 399 insertions, 324 deletions
diff --git a/Interfaces/English.lproj/QueryFavoriteManager.xib b/Interfaces/English.lproj/QueryFavoriteManager.xib
index 236d069a..0a761cf3 100644
--- a/Interfaces/English.lproj/QueryFavoriteManager.xib
+++ b/Interfaces/English.lproj/QueryFavoriteManager.xib
@@ -8,7 +8,6 @@
<string key="IBDocument.HIToolboxVersion">353.00</string>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
- <integer value="23"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -46,7 +45,7 @@
<string key="NSWindowContentMaxSize">{3.40282e+38, 3.40282e+38}</string>
<string key="NSWindowContentMinSize">{500, 371}</string>
<object class="NSView" key="NSWindowView" id="1006">
- <reference key="NSNextResponder"/>
+ <nil key="NSNextResponder"/>
<int key="NSvFlags">256</int>
<object class="NSMutableArray" key="NSSubviews">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -239,7 +238,6 @@
</object>
<object class="NSMenuItem" id="748257931">
<reference key="NSMenu" ref="299613786"/>
- <bool key="NSIsHidden">YES</bool>
<string type="base64-UTF8" key="NSTitle">SW1wb3J0IEZhdm9yaXRlc+KApg</string>
<string key="NSKeyEquiv"/>
<int key="NSMnemonicLoc">2147483647</int>
@@ -323,13 +321,13 @@
<object class="NSTableView" id="522643855">
<reference key="NSNextResponder" ref="106335880"/>
<int key="NSvFlags">4352</int>
- <string key="NSFrameSize">{250, 322}</string>
+ <string key="NSFrameSize">{249, 322}</string>
<reference key="NSSuperview" ref="106335880"/>
<bool key="NSEnabled">YES</bool>
<object class="NSTableHeaderView" key="NSHeaderView" id="724842455">
<reference key="NSNextResponder" ref="642947098"/>
<int key="NSvFlags">4352</int>
- <string key="NSFrameSize">{250, 17}</string>
+ <string key="NSFrameSize">{249, 17}</string>
<reference key="NSSuperview" ref="642947098"/>
<reference key="NSTableView" ref="522643855"/>
</object>
@@ -343,7 +341,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSTableColumn" id="901293108">
<string key="NSIdentifier">name</string>
- <double key="NSWidth">1.870000e+02</double>
+ <double key="NSWidth">1.860000e+02</double>
<double key="NSMinWidth">4.000000e+01</double>
<double key="NSMaxWidth">1.000000e+03</double>
<object class="NSTableHeaderCell" key="NSHeaderCell">
@@ -481,7 +479,6 @@
<int key="NSvFlags">-2147483392</int>
<string key="NSFrame">{{-100, -100}, {311, 15}}</string>
<reference key="NSSuperview" ref="205303038"/>
- <bool key="NSEnabled">YES</bool>
<int key="NSsFlags">1</int>
<reference key="NSTarget" ref="205303038"/>
<string key="NSAction">_doScroller:</string>
@@ -506,7 +503,7 @@
<string key="NSFrame">{{0, 22}, {249, 339}}</string>
<reference key="NSSuperview" ref="1030932325"/>
<reference key="NSNextKeyView" ref="106335880"/>
- <int key="NSsFlags">656</int>
+ <int key="NSsFlags">528</int>
<reference key="NSVScroller" ref="322405876"/>
<reference key="NSHScroller" ref="336819166"/>
<reference key="NSContentView" ref="106335880"/>
@@ -604,7 +601,7 @@
<string>WebURLsWithTitlesPboardType</string>
</object>
</object>
- <string key="NSFrameSize">{208, 10}</string>
+ <string key="NSFrameSize">{208, 14}</string>
<reference key="NSSuperview" ref="221447089"/>
<object class="NSTextContainer" key="NSTextContainer" id="74289808">
<object class="NSLayoutManager" key="NSLayoutManager">
@@ -776,7 +773,7 @@
<int key="NSDividerStyle">2</int>
<string key="NSAutosaveName">SPQueryFavoriteSplitView</string>
<object class="NSColor" key="BWSVColor">
- <int key="NSColorSpace">2</int>
+ <int key="NSColorSpace">1</int>
<bytes key="NSRGB">MC42NjY2NjY2OSAwLjY2NjY2NjY5IDAuNjY2NjY2NjkAA</bytes>
</object>
<bool key="BWSVColorIsEnabled">YES</bool>
@@ -833,7 +830,6 @@
</object>
</object>
<string key="NSFrameSize">{500, 371}</string>
- <reference key="NSSuperview"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1680, 1028}}</string>
<string key="NSMinSize">{500, 393}</string>
@@ -883,6 +879,19 @@
</object>
</object>
</object>
+ <object class="NSArrayController" id="863945469">
+ <object class="NSMutableArray" key="NSDeclaredKeys">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <string>name</string>
+ <string>query</string>
+ <string>tabtrigger</string>
+ </object>
+ <bool key="NSEditable">YES</bool>
+ <object class="_NSManagedProxy" key="_NSManagedProxy"/>
+ <bool key="NSPreservesSelection">YES</bool>
+ <bool key="NSFilterRestrictsInsertion">YES</bool>
+ <bool key="NSClearsFilterPredicateOnInsertion">YES</bool>
+ </object>
</object>
<object class="IBObjectContainer" key="IBDocument.Objects">
<object class="NSMutableArray" key="connectionRecords">
@@ -1037,14 +1046,6 @@
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
- <string key="label">copyQueryFavorite:</string>
- <reference key="source" ref="1001"/>
- <reference key="destination" ref="247392372"/>
- </object>
- <int key="connectionID">213</int>
- </object>
- <object class="IBConnectionRecord">
- <object class="IBActionConnection" key="connection">
<string key="label">saveFavoriteToFile:</string>
<reference key="source" ref="1001"/>
<reference key="destination" ref="1015707754"/>
@@ -1069,14 +1070,6 @@
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
- <string key="label">copyQueryFavorite:</string>
- <reference key="source" ref="1001"/>
- <reference key="destination" ref="161398571"/>
- </object>
- <int key="connectionID">253</int>
- </object>
- <object class="IBConnectionRecord">
- <object class="IBActionConnection" key="connection">
<string key="label">exportFavorites:</string>
<reference key="source" ref="1001"/>
<reference key="destination" ref="988339768"/>
@@ -1131,6 +1124,98 @@
</object>
<int key="connectionID">277</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBActionConnection" key="connection">
+ <string key="label">duplicateQueryFavorite:</string>
+ <reference key="source" ref="1001"/>
+ <reference key="destination" ref="161398571"/>
+ </object>
+ <int key="connectionID">282</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBActionConnection" key="connection">
+ <string key="label">duplicateQueryFavorite:</string>
+ <reference key="source" ref="1001"/>
+ <reference key="destination" ref="247392372"/>
+ </object>
+ <int key="connectionID">283</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: arrangedObjects.name</string>
+ <reference key="source" ref="901293108"/>
+ <reference key="destination" ref="863945469"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="901293108"/>
+ <reference key="NSDestination" ref="863945469"/>
+ <string key="NSLabel">value: arrangedObjects.name</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">arrangedObjects.name</string>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
+ </object>
+ <int key="connectionID">285</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: selection.name</string>
+ <reference key="source" ref="247897226"/>
+ <reference key="destination" ref="863945469"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="247897226"/>
+ <reference key="NSDestination" ref="863945469"/>
+ <string key="NSLabel">value: selection.name</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">selection.name</string>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
+ </object>
+ <int key="connectionID">287</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: selection.query</string>
+ <reference key="source" ref="556251150"/>
+ <reference key="destination" ref="863945469"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="556251150"/>
+ <reference key="NSDestination" ref="863945469"/>
+ <string key="NSLabel">value: selection.query</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">selection.query</string>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
+ </object>
+ <int key="connectionID">291</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBBindingConnection" key="connection">
+ <string key="label">value: arrangedObjects.tabtrigger</string>
+ <reference key="source" ref="183746003"/>
+ <reference key="destination" ref="863945469"/>
+ <object class="NSNibBindingConnector" key="connector">
+ <reference key="NSSource" ref="183746003"/>
+ <reference key="NSDestination" ref="863945469"/>
+ <string key="NSLabel">value: arrangedObjects.tabtrigger</string>
+ <string key="NSBinding">value</string>
+ <string key="NSKeyPath">arrangedObjects.tabtrigger</string>
+ <object class="NSDictionary" key="NSOptions">
+ <string key="NS.key.0">NSConditionallySetsEditable</string>
+ <boolean value="YES" key="NS.object.0" id="5"/>
+ </object>
+ <int key="NSNibBindingConnectorVersion">2</int>
+ </object>
+ </object>
+ <int key="connectionID">295</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBOutletConnection" key="connection">
+ <string key="label">favoritesArrayController</string>
+ <reference key="source" ref="1001"/>
+ <reference key="destination" ref="863945469"/>
+ </object>
+ <int key="connectionID">297</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -1529,6 +1614,12 @@
<reference key="object" ref="615814080"/>
<reference key="parent" ref="602197876"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">284</int>
+ <reference key="object" ref="863945469"/>
+ <reference key="parent" ref="1002"/>
+ <string key="objectName">Favorites Controller</string>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -1614,18 +1705,19 @@
<string>279.IBPluginDependency</string>
<string>280.IBPluginDependency</string>
<string>281.IBPluginDependency</string>
+ <string>284.IBPluginDependency</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string>{{288, 332}, {500, 371}}</string>
- <string>{{288, 332}, {500, 371}}</string>
+ <string>{{209, 69}, {500, 371}}</string>
+ <string>{{209, 69}, {500, 371}}</string>
<boolean value="NO"/>
<string>{196, 240}</string>
<string>{{357, 418}, {480, 270}}</string>
- <boolean value="YES"/>
+ <reference ref="5"/>
<string>{500, 371}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -1682,7 +1774,7 @@
<string>com.brandonwalkin.BWToolkit</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
- <string>{{312, -3}, {241, 152}}</string>
+ <string>{{234, 203}, {241, 152}}</string>
<string>com.brandonwalkin.BWToolkit</string>
<string>com.brandonwalkin.BWToolkit</string>
<object class="NSMutableDictionary">
@@ -1723,8 +1815,7 @@
<object class="IBToolTipAttribute" key="NS.object.0">
<string key="name">ToolTip</string>
<reference key="object" ref="748257931"/>
- <string type="base64-UTF8" key="toolTip">SW1wb3J0IHF1ZXJ5IGZhdm9yaXRlcyBmcm9tIFNQRiBmaWxlIGFuZCBhcHBlbmQgdGhlbSAoZm9yIHJl
-cGxhY2luZyBhbGwgY3VycmVudCBmYXZvcml0ZXMgcHJlc3Mg4oyYKQ</string>
+ <string key="toolTip">Import query favorites from SPF file and append them to the favorite list</string>
</object>
</object>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -1753,6 +1844,7 @@ cGxhY2luZyBhbGwgY3VycmVudCBmYXZvcml0ZXMgcHJlc3Mg4oyYKQ</string>
<string>com.brandonwalkin.BWToolkit</string>
<string>com.brandonwalkin.BWToolkit</string>
<string>com.brandonwalkin.BWToolkit</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
</object>
</object>
<object class="NSMutableDictionary" key="unlocalizedProperties">
@@ -1775,7 +1867,7 @@ cGxhY2luZyBhbGwgY3VycmVudCBmYXZvcml0ZXMgcHJlc3Mg4oyYKQ</string>
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">281</int>
+ <int key="maxID">297</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -1965,7 +2057,7 @@ cGxhY2luZyBhbGwgY3VycmVudCBmYXZvcml0ZXMgcHJlc3Mg4oyYKQ</string>
<bool key="EncodedWithXMLCoder">YES</bool>
<string>addQueryFavorite:</string>
<string>closeQueryManagerSheet:</string>
- <string>copyQueryFavorite:</string>
+ <string>duplicateQueryFavorite:</string>
<string>exportFavorites:</string>
<string>importFavoritesByAdding:</string>
<string>importFavoritesByReplacing:</string>
@@ -1994,6 +2086,7 @@ cGxhY2luZyBhbGwgY3VycmVudCBmYXZvcml0ZXMgcHJlc3Mg4oyYKQ</string>
<string>encodingPopUp</string>
<string>favoriteNameTextField</string>
<string>favoriteQueryTextView</string>
+ <string>favoritesArrayController</string>
<string>favoritesTableView</string>
<string>removeButton</string>
</object>
@@ -2003,6 +2096,7 @@ cGxhY2luZyBhbGwgY3VycmVudCBmYXZvcml0ZXMgcHJlc3Mg4oyYKQ</string>
<string>NSPopUpButton</string>
<string>NSTextField</string>
<string>NSTextView</string>
+ <string>NSArrayController</string>
<string>NSTableView</string>
<string>id</string>
</object>
diff --git a/Source/CMTextView.m b/Source/CMTextView.m
index 287bd227..4cf7b155 100644
--- a/Source/CMTextView.m
+++ b/Source/CMTextView.m
@@ -2296,7 +2296,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
}
// Validate Select Active Query
if ([menuItem action] == @selector(selectCurrentQuery)) {
- return ([self isEditable]);
+ return ([self isEditable] && [[self delegate] isKindOfClass:[CustomQuery class]]);
}
// Disable "Copy with Column Names" and "Copy as SQL INSERT"
// in the main menu
diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m
index 155113d9..0a2958ee 100644
--- a/Source/CustomQuery.m
+++ b/Source/CustomQuery.m
@@ -2398,34 +2398,23 @@
- (void)queryFavoritesHaveBeenUpdated:(id)manager
{
- NSMutableArray *globalFavorites = [[NSMutableArray alloc] init];
-
- // Invoked after adding a favorite from inside CustomQuery
- if(manager == nil) {
- [globalFavorites addObjectsFromArray:[prefs objectForKey:@"queryFavorites"]];
- }
- // Invoked by the query favorite manager
- else if(manager == favoritesManager){
- // Update global query favorites
- [globalFavorites addObjectsFromArray:[favoritesManager globalQueryFavorites]];
- [prefs setObject:globalFavorites forKey:@"queryFavorites"];
- }
+ NSMenuItem *headerMenuItem;
+ NSMenu *menu = [queryFavoritesButton menu];
// Remove all favorites beginning from the end
while([queryFavoritesButton numberOfItems] > 6)
[queryFavoritesButton removeItemAtIndex:[queryFavoritesButton numberOfItems]-1];
- // Insert new favorites and allow menu items with the same name
- NSMenu *menu = [queryFavoritesButton menu];
-
- NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Document" action:NULL keyEquivalent:@""];
- [item setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
- [item setToolTip:[NSString stringWithFormat:@"‘%@’ based favorites",
+ // Build document-based list
+ headerMenuItem = [[NSMenuItem alloc] initWithTitle:
+ [[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent]
+ action:NULL keyEquivalent:@""];
+ [headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
+ [headerMenuItem setToolTip:[NSString stringWithFormat:@"‘%@’ based favorites",
[[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent]]];
- [item setIndentationLevel:0];
- [menu addItem:item];
- [item release];
-
+ [headerMenuItem setIndentationLevel:0];
+ [menu addItem:headerMenuItem];
+ [headerMenuItem release];
for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:[tableDocumentInstance fileURL]]) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithString:[favorite objectForKey:@"name"]] action:NULL keyEquivalent:@""];
[item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]];
@@ -2434,14 +2423,14 @@
[item release];
}
- item = [[NSMenuItem alloc] initWithTitle:@"Global" action:NULL keyEquivalent:@""];
- [item setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
- [item setToolTip:@"Globally stored favorites"];
- [item setIndentationLevel:0];
- [menu addItem:item];
- [item release];
-
- for (NSDictionary *favorite in globalFavorites) {
+ // Build global list
+ headerMenuItem = [[NSMenuItem alloc] initWithTitle:@"Global" action:NULL keyEquivalent:@""];
+ [headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
+ [headerMenuItem setToolTip:@"Globally stored favorites"];
+ [headerMenuItem setIndentationLevel:0];
+ [menu addItem:headerMenuItem];
+ [headerMenuItem release];
+ for (NSDictionary *favorite in [prefs objectForKey:@"queryFavorites"]) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithString:[favorite objectForKey:@"name"]] action:NULL keyEquivalent:@""];
[item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]];
[item setIndentationLevel:1];
@@ -2449,11 +2438,7 @@
[item release];
}
- [globalFavorites release];
-
- [prefs synchronize];
-
- [queryFavoritesSearchField setStringValue:@""];
+ // [queryFavoritesSearchField setStringValue:@""];
}
@@ -2508,14 +2493,14 @@
}
if([saveQueryFavoriteGlobal state] == NSOnState) {
- [favorites addObject:[NSDictionary dictionaryWithObjects:
+ [favorites addObject:[NSMutableDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:[queryFavoriteNameTextField stringValue], queryToBeAddded, nil]
forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]]];
[prefs setObject:favorites forKey:@"queryFavorites"];
} else {
- [[SPQueryController sharedQueryController] addFavorite:[NSDictionary dictionaryWithObjects:
- [NSArray arrayWithObjects:[queryFavoriteNameTextField stringValue], queryToBeAddded, nil]
+ [[SPQueryController sharedQueryController] addFavorite:[NSMutableDictionary dictionaryWithObjects:
+ [NSArray arrayWithObjects:[queryFavoriteNameTextField stringValue], [queryToBeAddded mutableCopy], nil]
forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]] forFileURL:[tableDocumentInstance fileURL]];
}
@@ -2563,7 +2548,6 @@
#pragma mark -
-// Last but not least
- (id)init
{
if ((self = [super init])) {
@@ -2617,7 +2601,7 @@
NSMenu *menu = [queryHistoryButton menu];
NSString *searchPattern = [queryHistorySearchField stringValue];
for (i=2; i< [menu numberOfItems]; i++)
- [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", [queryHistorySearchField stringValue]]])];
+ [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", searchPattern]])];
}
diff --git a/Source/SPQueryController.h b/Source/SPQueryController.h
index df842745..abef41de 100644
--- a/Source/SPQueryController.h
+++ b/Source/SPQueryController.h
@@ -84,6 +84,8 @@
- (void)addFavorite:(NSDictionary *)favorite forFileURL:(NSURL *)fileURL;
- (void)replaceFavoritesByArray:(NSArray *)favoritesArray forFileURL:(NSURL *)fileURL;
+- (void)removeFavoriteAtIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL;
+- (void)insertFavorite:(NSDictionary *)favorite atIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL;
- (void)addHistory:(NSString *)history forFileURL:(NSURL *)fileURL;
- (void)replaceHistoryByArray:(NSArray *)historyArray forFileURL:(NSURL *)fileURL;
diff --git a/Source/SPQueryController.m b/Source/SPQueryController.m
index 4345f616..4dbd3749 100644
--- a/Source/SPQueryController.m
+++ b/Source/SPQueryController.m
@@ -569,6 +569,17 @@ static SPQueryController *sharedQueryController = nil;
}
+- (void)removeFavoriteAtIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL
+{
+ [[favoritesContainer objectForKey:[fileURL absoluteString]] removeObjectAtIndex:index];
+}
+
+- (void)insertFavorite:(NSDictionary *)favorite atIndex:(NSUInteger)index forFileURL:(NSURL *)fileURL
+{
+ [[favoritesContainer objectForKey:[fileURL absoluteString]] insertObject:favorite atIndex:index];
+}
+
+
@end
@implementation SPQueryController (PrivateAPI)
diff --git a/Source/SPQueryFavoriteManager.h b/Source/SPQueryFavoriteManager.h
index 28a625d5..4268ffe0 100644
--- a/Source/SPQueryFavoriteManager.h
+++ b/Source/SPQueryFavoriteManager.h
@@ -25,14 +25,6 @@
#import <Cocoa/Cocoa.h>
-enum sp_favorite_types
-{
- SP_FAVORITETYPE_HEADER = 0,
- SP_FAVORITETYPE_GLOBAL = 1,
- SP_FAVORITETYPE_CONNECTION = 2,
-};
-
-
@interface NSObject (SPQueryFavoriteManagerDelegate)
- (void)queryFavoritesHaveBeenUpdated:(id)manager;
@@ -47,33 +39,32 @@ enum sp_favorite_types
BOOL delegateRespondsToFavoriteUpdates;
+ NSDocument *tableDocumentInstance;
+ NSURL *delegatesFileURL;
IBOutlet NSPopUpButton *encodingPopUp;
IBOutlet NSTableView *favoritesTableView;
IBOutlet NSTextField *favoriteNameTextField;
IBOutlet NSTextView *favoriteQueryTextView;
IBOutlet id removeButton;
+ IBOutlet NSArrayController *favoritesArrayController;
+
NSMutableArray *favorites;
- NSMutableArray *favoriteProperties;
-
- NSMutableString *pendingQueryString;
-
- NSUInteger selectedRowBeforeChangingSelection;
+
BOOL isTableCellEditing;
}
- (id)initWithDelegate:(id)managerDelegate;
// Accessors
-- (NSMutableArray *)globalQueryFavorites;
-- (NSMutableArray *)connectionQueryFavorites;
+- (NSMutableArray *)queryFavoritesForFileURL:(NSURL *)fileURL;
- (id)customQueryInstance;
// IBAction methods
- (IBAction)addQueryFavorite:(id)sender;
- (IBAction)removeQueryFavorite:(id)sender;
- (IBAction)removeAllQueryFavorites:(id)sender;
-- (IBAction)copyQueryFavorite:(id)sender;
+- (IBAction)duplicateQueryFavorite:(id)sender;
- (IBAction)saveFavoriteToFile:(id)sender;
- (IBAction)exportFavorites:(id)sender;
- (IBAction)importFavoritesByAdding:(id)sender;
diff --git a/Source/SPQueryFavoriteManager.m b/Source/SPQueryFavoriteManager.m
index 5ad6644d..ce10bfa7 100644
--- a/Source/SPQueryFavoriteManager.m
+++ b/Source/SPQueryFavoriteManager.m
@@ -26,6 +26,7 @@
#import "SPQueryFavoriteManager.h"
#import "ImageAndTextCell.h"
#import "SPEncodingPopupAccessory.h"
+#import "SPQueryController.h"
#define DEFAULT_QUERY_FAVORITE_FILE_EXTENSION @"sql"
#define DEFAULT_SEQUELPRO_FILE_EXTENSION @"spf"
@@ -36,7 +37,7 @@
#define QUERY_FAVORITES_PB_DRAG_TYPE @"SequelProQueryFavoritesPasteboard"
@interface SPQueryFavoriteManager (Private)
-- (void)_writePendingQueryString;
+- (void)_initWithNoSelection;
@end
@implementation SPQueryFavoriteManager
@@ -51,12 +52,11 @@
prefs = [NSUserDefaults standardUserDefaults];
- favoriteProperties = [[NSMutableArray alloc] init];
favorites = [[NSMutableArray alloc] init];
- selectedRowBeforeChangingSelection = 0;
- pendingQueryString = [[NSMutableString alloc] init];
delegateRespondsToFavoriteUpdates = [delegate respondsToSelector:@selector(queryFavoritesHaveBeenUpdated:)];
+ tableDocumentInstance = [delegate valueForKeyPath:@"tableDocumentInstance"];
+ delegatesFileURL = [[delegate valueForKeyPath:@"tableDocumentInstance"] fileURL];
}
return self;
@@ -64,9 +64,7 @@
- (void)dealloc
{
- [favoriteProperties release];
[favorites release];
- [pendingQueryString release];
[super dealloc];
}
@@ -88,62 +86,91 @@
options:bindingOptions];
- [favorites addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"GLOBAL", @"name", nil]];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_HEADER]];
+ [favorites addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ @"Global", @"name",
+ @"", @"headerOfFileURL",
+ @"", @"query",
+ nil]];
// Build data source for global queryFavorites (as mutable copy! otherwise each
// change will be stored in the prefs at once)
if([prefs objectForKey:@"queryFavorites"]) {
- for(id fav in [prefs objectForKey:@"queryFavorites"]) {
+ for(id fav in [prefs objectForKey:@"queryFavorites"])
[favorites addObject:[fav mutableCopy]];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_GLOBAL]];
- }
}
- [favoritesTableView reloadData];
- // Set Remove button state
- [removeButton setEnabled:([favorites count] > 1)];
+ [favorites addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ [[[delegatesFileURL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent], @"name",
+ [delegatesFileURL absoluteString], @"headerOfFileURL",
+ @"", @"query",
+ nil]];
+
+ if([[SPQueryController sharedQueryController] favoritesForFileURL:delegatesFileURL]) {
+ for(id fav in [[SPQueryController sharedQueryController] favoritesForFileURL:delegatesFileURL])
+ [favorites addObject:[fav mutableCopy]];
+ }
+
+
+ // Select the first query if any
+ NSUInteger i = 0;
+ for(i=0; i < [favorites count]; i++ )
+ if(![[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"])
+ break;
- // Select the first query
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
+ [[self window] makeFirstResponder:favoritesTableView];
+ [self _initWithNoSelection];
// Register drag types
[favoritesTableView registerForDraggedTypes:[NSArray arrayWithObject:QUERY_FAVORITES_PB_DRAG_TYPE]];
+
+ [favoritesArrayController setContent:favorites];
+ [favoritesTableView reloadData];
+
+ // Set Remove button state
+ [removeButton setEnabled:([favoritesTableView numberOfSelectedRows] > 0)];
+
}
#pragma mark -
#pragma mark Accessor methods
/**
- * Returns the global query favorites array.
+ * Returns the query favorites array for fileURL.
+ * fileURL == nil → global favorites
*/
-- (NSMutableArray *)globalQueryFavorites
+- (NSMutableArray *)queryFavoritesForFileURL:(NSURL *)fileURL
{
- NSMutableArray *globals = [NSMutableArray array];
-
- NSUInteger i;
-
- for(i=1; i<[favorites count]; i++)
- if([[favoriteProperties objectAtIndex:i] intValue] == SP_FAVORITETYPE_GLOBAL)
- [globals addObject:[favorites objectAtIndex:i]];
+ NSMutableArray *favs = [NSMutableArray array];
+ NSString *fileURLstring;
- return globals;
-}
+ if(fileURL == nil)
+ fileURLstring = @"";
+ else
+ fileURLstring = [fileURL absoluteString];
-/**
- * Returns the global query favorites array.
- */
-- (NSMutableArray *)connectionQueryFavorites
-{
- NSMutableArray *conns = [NSMutableArray array];
-
- NSUInteger i;
-
- for(i=1; i<[favorites count]; i++)
- if([[favoriteProperties objectAtIndex:i] intValue] == SP_FAVORITETYPE_CONNECTION)
- [conns addObject:[favorites objectAtIndex:i]];
+ NSUInteger i = 0;
+
+ // Look for the header specified by fileURL
+ while(i<[favorites count]) {
+ if ([[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"]
+ && [[[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"] isEqualToString:fileURLstring]) {
+ i++;
+ break;
+ }
+ i++;
+ }
+
+ // Take all favorites until the next header or end of favorites
+ for(i; i<[favorites count]; i++) {
- return conns;
+ if(![[favorites objectAtIndex:i] objectForKey:@"headerOfFileURL"])
+ [favs addObject:[favorites objectAtIndex:i]];
+ else
+ break;
+
+ }
+
+ return favs;
}
/**
@@ -158,31 +185,53 @@
#pragma mark IBAction methods
/**
- * Adds a query favorite
+ * Adds/Inserts a query favorite
*/
- (IBAction)addQueryFavorite:(id)sender
{
- [self _writePendingQueryString];
- NSMutableDictionary *favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"New Favorite", @"", nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
+ NSMutableDictionary *favorite;
+ NSUInteger insertIndex;
+
+ // Duplicate a selected favorite if sender == self
+ if(sender == self)
+ favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[[favoriteNameTextField stringValue] stringByAppendingFormat:@" Copy"], [favoriteQueryTextView string], nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
+ // Add a new favorite
+ else
+ favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"New Favorite", @"", nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
- [favorites addObject:favorite];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_GLOBAL]];
+ if([favoritesTableView numberOfSelectedRows] > 0) {
+ insertIndex = [[favoritesTableView selectedRowIndexes] lastIndex]+1;
+ [favorites insertObject:favorite atIndex:insertIndex];
+ } else {
+ [favorites addObject:favorite];
+ insertIndex = [favorites count] - 1;
+ }
+
+ [favoritesArrayController rearrangeObjects];
[favoritesTableView reloadData];
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:([favorites count] - 1)] byExtendingSelection:NO];
+ [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:insertIndex] byExtendingSelection:NO];
[favoritesTableView scrollRowToVisible:[favoritesTableView selectedRow]];
- selectedRowBeforeChangingSelection = [favorites count] - 1;
-
- [removeButton setEnabled:([favorites count] > 1)];
-
+ [removeButton setEnabled:([favoritesTableView numberOfSelectedRows] > 0)];
[[self window] makeFirstResponder:favoriteNameTextField];
}
/**
+ * Duplicates a query favorite
+ */
+- (IBAction)duplicateQueryFavorite:(id)sender
+{
+ if ([favoritesTableView numberOfSelectedRows] == 1)
+ [self addQueryFavorite:self];
+ else
+ NSBeep();
+}
+
+/**
* Removes a query favorite
*/
- (IBAction)removeQueryFavorite:(id)sender
@@ -227,32 +276,6 @@
}
/**
- * Copies a query favorite
- */
-- (IBAction)copyQueryFavorite:(id)sender
-{
- if ([favoritesTableView numberOfSelectedRows] == 1) {
- NSMutableDictionary *favorite = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[[favoriteNameTextField stringValue] stringByAppendingFormat:@" Copy"], [favoriteQueryTextView string], nil] forKeys:[NSArray arrayWithObjects:@"name", @"query", nil]];
-
- [self _writePendingQueryString];
-
- [favorites addObject:favorite];
- [favoriteProperties addObject:[NSNumber numberWithInt:SP_FAVORITETYPE_GLOBAL]];
-
- [favoritesTableView reloadData];
-
- // Update selection
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:([favorites count] - 1)] byExtendingSelection:NO];
- selectedRowBeforeChangingSelection = [favorites count] - 1;
-
- [favoritesTableView scrollRowToVisible:[favoritesTableView selectedRow]];
-
- [[self window] makeFirstResponder:favoriteNameTextField];
-
- }
-}
-
-/**
* Saves the currently selected query favorite to a user specified file.
*/
- (IBAction)saveFavoriteToFile:(id)sender
@@ -289,7 +312,20 @@
- (IBAction)importFavoritesByAdding:(id)sender
{
+ NSOpenPanel *panel = [NSOpenPanel openPanel];
+ [panel setCanSelectHiddenExtension:YES];
+ [panel setDelegate:self];
+ [panel setCanChooseDirectories:NO];
+ [panel setAllowsMultipleSelection:NO];
+ [panel setResolvesAliases:YES];
+ [panel beginSheetForDirectory:nil
+ file:@""
+ types:[NSArray arrayWithObjects:@"spf", @"sql", nil]
+ modalForWindow:[self window]
+ modalDelegate:self
+ didEndSelector:@selector(importPanelDidEnd:returnCode:contextInfo:)
+ contextInfo:NULL];
}
- (IBAction)importFavoritesByReplacing:(id)sender
@@ -313,22 +349,26 @@
[NSApp endSheet:[self window] returnCode:0];
[[self window] orderOut:self];
- // Save button was pressed
+ // "Apply Changes" button was pressed
if([sender tag]) {
- // Ensure that last changes will be written to prefs
+
+ // Ensure that last changes will be written back
// if only one favorite is selected; otherwise unstable state
if ([favoritesTableView numberOfSelectedRows] == 1) {
- [self _writePendingQueryString];
[[self window] makeFirstResponder:favoritesTableView];
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRowBeforeChangingSelection] byExtendingSelection:NO];
}
- // Inform the delegate that the query favorites have been updated
- if (delegateRespondsToFavoriteUpdates)
+ // Update current document's and global query favorites
+ [[SPQueryController sharedQueryController] replaceFavoritesByArray:
+ [self queryFavoritesForFileURL:delegatesFileURL] forFileURL:delegatesFileURL];
+ [prefs setObject:[self queryFavoritesForFileURL:nil] forKey:@"queryFavorites"];
+
+ // Inform delegate to update
+ if(delegateRespondsToFavoriteUpdates)
[delegate queryFavoritesHaveBeenUpdated:self];
}
-
+
}
#pragma mark -
@@ -379,7 +419,7 @@
if([[aTableColumn identifier] isEqualToString:@"name"] && [anObject length]) {
[[favorites objectAtIndex:rowIndex] setObject:[anObject description] forKey:@"name"];
- [[favorites objectAtIndex:rowIndex] setObject:[favoriteQueryTextView string] forKey:@"query"];
+ // [[favorites objectAtIndex:rowIndex] setObject:[favoriteQueryTextView string] forKey:@"query"];
[favoriteNameTextField setStringValue:[anObject description]];
}
@@ -392,72 +432,7 @@
*/
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
- [pendingQueryString setString:[favoriteQueryTextView string]];
- [self _writePendingQueryString];
- return ([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) ? NO : YES;
-}
-
-/*
- * Update name and query view and control several table selection modi
- */
-- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
-{
-
- // store the last selected favorite for writing the pending query string
- // - for a single favorite selection
- if([favoritesTableView numberOfSelectedRows] == 1)
- selectedRowBeforeChangingSelection = [favoritesTableView selectedRow];
- // - for multiple favorite selection
- if([favoritesTableView numberOfSelectedRows] > 1) {
-
- // Save query string directly
- if(selectedRowBeforeChangingSelection > 0 && selectedRowBeforeChangingSelection < [favorites count])
- [[favorites objectAtIndex:selectedRowBeforeChangingSelection] setObject:[favoriteQueryTextView string] forKey:@"query"];
-
- // Update name text field
- [[favoriteNameTextField cell] setPlaceholderString:SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- [favoriteNameTextField setEditable:NO];
- [favoriteNameTextField setSelectable:NO];
-
- // This is an "hack"; if one set it to @"" it could happen that
- // the wrong pending query string will be saved
- [favoriteQueryTextView setTextColor:[NSColor clearColor]];
- [favoriteQueryTextView setEditable:NO];
- [favoriteQueryTextView setSelectable:NO];
-
- return;
-
- } else {
- [favoriteNameTextField setEditable:YES];
- [favoriteNameTextField setSelectable:YES];
- [favoriteQueryTextView setSelectable:YES];
- [favoriteQueryTextView setEditable:YES];
- }
-
-
- // only the "GLOBAL" header is in the table
- if([favorites count] < 2) {
- [self _writePendingQueryString];
- [[self window] makeFirstResponder:favoritesTableView];
- [[favoriteNameTextField cell] setPlaceholderString:SP_NO_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- [favoriteQueryTextView setString:@""];
- [pendingQueryString setString:@""];
- selectedRowBeforeChangingSelection = -1;
- return;
- }
-
- // Update name and query field contents
- NSUInteger row = (NSUInteger)[[aNotification object] selectedRow];
-
- // This is needed if one deletes the last table item
- if(row > [favorites count] - 1) row = [favorites count] - 1;
- if(row < 1) row = 1;
-
- [favoriteNameTextField setStringValue:[[favorites objectAtIndex:row] objectForKey:@"name"]];
- [favoriteQueryTextView setString:[[favorites objectAtIndex:row] objectForKey:@"query"]];
-
+ return ([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) ? NO : YES;
}
/*
@@ -466,14 +441,14 @@
*/
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
- if([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER && [[aTableColumn identifier] isEqualToString:@"name"]) {
+ if([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"] && [[aTableColumn identifier] isEqualToString:@"name"]) {
// if([[[favoriteProperties objectAtIndex:rowIndex] objectForKey:@"isGroup"] isEqualToString:@"1"])
// [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"NSRightFacingTriangleTemplate"]];
// else
// [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"NSLeftFacingTriangleTemplate"]];
[(ImageAndTextCell*)aCell setIndentationLevel:0];
}
- else if([[favoriteProperties objectAtIndex:rowIndex] intValue] != SP_FAVORITETYPE_HEADER && [[aTableColumn identifier] isEqualToString:@"name"]) {
+ else if(![[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"] && [[aTableColumn identifier] isEqualToString:@"name"]) {
// [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"dummy-small"]];
[(ImageAndTextCell*)aCell setIndentationLevel:1];
}
@@ -484,7 +459,7 @@
*/
- (CGFloat)tableView:(NSTableView *)aTableView heightOfRow:(NSInteger)rowIndex
{
- return ([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) ? 20 : 18;
+ return ([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) ? 20 : 18;
}
/*
@@ -492,7 +467,7 @@
*/
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
- if([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) {
+ if([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) {
return NO;
} else {
isTableCellEditing = YES;
@@ -505,7 +480,7 @@
*/
- (BOOL)tableView:(NSTableView *)aTableView isGroupRow:(NSInteger)rowIndex
{
- return ([[favoriteProperties objectAtIndex:rowIndex] intValue] == SP_FAVORITETYPE_HEADER) ? YES : NO;
+ return ([[favorites objectAtIndex:rowIndex] objectForKey:@"headerOfFileURL"]) ? YES : NO;
}
/*
* Detect if inline editing was done - then ESC to close the sheet will be activate
@@ -514,6 +489,7 @@
{
isTableCellEditing = NO;
}
+
/*
* Changes in the name text field will be saved in data source directly
* to update the table view accordingly
@@ -522,7 +498,7 @@
{
// Do nothing if no favorite is selected
- if([favoritesTableView selectedRow] < 1) return;
+ if([favoritesTableView numberOfSelectedRows] < 1) return;
id object = [notification object];
@@ -533,27 +509,6 @@
}
-/*
- * Changes in the query text view will be cached as pending query string which will update
- * the data source before selecting an other favorite or before an other event.
- * If multiple rows are selected update name field.
- */
-- (void)textViewDidChangeSelection:(NSNotification *)notification
-{
- id object = [notification object];
-
- if(object == favoriteQueryTextView) {
- if(![favoriteQueryTextView isEditable]) return;
- [pendingQueryString setString:[NSString stringWithString:[favoriteQueryTextView string]]];
- if([favoritesTableView numberOfSelectedRows] > 1) {
- [[favoriteNameTextField cell] setPlaceholderString:SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- selectedRowBeforeChangingSelection = -1;
- return;
- }
- }
-}
-
#pragma mark -
#pragma mark Menu validation
@@ -568,7 +523,7 @@
SEL action = [menuItem action];
- if ( (action == @selector(copyQueryFavorite:)) ||
+ if ( (action == @selector(duplicateQueryFavorite:)) ||
(action == @selector(saveFavoriteToFile:)))
{
return ([favoritesTableView numberOfSelectedRows] == 1);
@@ -592,27 +547,26 @@
* Return whether or not the supplied rows can be written.
*/
- (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows toPasteboard:(NSPasteboard *)pboard
-{
+{
+
+ // Up to now only one row can be dragged
if ([rows count] == 1) {
-
+
NSArray *pboardTypes = [NSArray arrayWithObject:QUERY_FAVORITES_PB_DRAG_TYPE];
NSInteger originalRow = [[rows objectAtIndex:0] intValue];
-
+
+ if(originalRow < 1) return NO;
+
+ // Do not drag headers
+ if([[favorites objectAtIndex:originalRow] objectForKey:@"headerOfFileURL"]) return NO;
+
[pboard declareTypes:pboardTypes owner:nil];
[pboard setString:[[NSNumber numberWithInt:originalRow] stringValue] forType:QUERY_FAVORITES_PB_DRAG_TYPE];
-
- // Save query string before dragging
- // and suppress the writing of pending query string changes
- [[favorites objectAtIndex:selectedRowBeforeChangingSelection] setObject:[favoriteQueryTextView string] forKey:@"query"];
- selectedRowBeforeChangingSelection = -1;
+
return YES;
- } else {
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRowBeforeChangingSelection] byExtendingSelection:NO];
- [[favoriteNameTextField cell] setPlaceholderString:SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING];
- [favoriteNameTextField setStringValue:@""];
- [favoriteQueryTextView setTextColor:[NSColor clearColor]];
+
}
-
+
return NO;
}
@@ -644,17 +598,26 @@
{
NSInteger originalRow = [[[info draggingPasteboard] stringForType:QUERY_FAVORITES_PB_DRAG_TYPE] intValue];
NSInteger destinationRow = row;
-
- if (destinationRow > originalRow) destinationRow--;
-
+
+ if(destinationRow == originalRow || row < 1) return NO;
+ if(destinationRow > originalRow) destinationRow--;
+
NSMutableDictionary *draggedRow = [NSMutableDictionary dictionaryWithDictionary:[favorites objectAtIndex:originalRow]];
-
+
[favorites removeObjectAtIndex:originalRow];
[favorites insertObject:draggedRow atIndex:destinationRow];
-
+
[favoritesTableView reloadData];
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:destinationRow] byExtendingSelection:NO];
-
+ [favoritesArrayController rearrangeObjects];
+
+ if([[favorites objectAtIndex:destinationRow] objectForKey:@"headerOfFileURL"])
+ [self _initWithNoSelection];
+ else if([favoritesTableView numberOfSelectedRows] == 1
+ && [favoritesTableView selectedRow] == originalRow) {
+ [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:destinationRow] byExtendingSelection:NO];
+ [favoritesArrayController setSelectionIndexes:[NSIndexSet indexSetWithIndex:destinationRow]];
+ }
+
return YES;
}
@@ -678,40 +641,77 @@
// get last index
NSUInteger currentIndex = [indexes lastIndex];
- NSUInteger idx = currentIndex;
-
- // Prevend to write pending changes
- selectedRowBeforeChangingSelection = -1;
- [pendingQueryString setString:@""];
while (currentIndex != NSNotFound) {
[favorites removeObjectAtIndex:currentIndex];
- [favoriteProperties removeObjectAtIndex:currentIndex];
// get next index (beginning from the end)
currentIndex = [indexes indexLessThanIndex:currentIndex];
- [favoritesTableView reloadData];
}
+ [favoritesArrayController rearrangeObjects];
+ [favoritesTableView reloadData];
+
// Set focus to favorite list to avoid an unstable state
[[self window] makeFirstResponder:favoritesTableView];
- // Try to reselect a favorite
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
- if(idx > [favorites count]-1)
- idx = [favorites count]-1;
- if(idx != 0)
- [favoritesTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:idx] byExtendingSelection:NO];
+ [removeButton setEnabled:([favoritesTableView numberOfSelectedRows] > 0)];
+ }
+ }
+}
+
+/**
+ * Import panel did end method.
+ */
+- (void)importPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(NSString *)contextInfo
+{
+
+ if (returnCode == NSOKButton) {
+
+ NSString *filename = [[panel filenames] objectAtIndex:0];
+ NSError *readError = nil;
+ NSString *convError = nil;
+ NSPropertyListFormat format;
+
+ NSDictionary *spf = nil;
+
+ if([[[filename pathExtension] lowercaseString] isEqualToString:@"spf"]) {
+ NSData *pData = [NSData dataWithContentsOfFile:filename options:NSUncachedRead error:&readError];
+
+ spf = [[NSPropertyListSerialization propertyListFromData:pData
+ mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&convError] retain];
+
+ if(!spf || readError != nil || [convError length] || !(format == NSPropertyListXMLFormat_v1_0 || format == NSPropertyListBinaryFormat_v1_0)) {
+ NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Error while reading data file", @"error while reading data file")]
+ defaultButton:NSLocalizedString(@"OK", @"OK button")
+ alternateButton:nil
+ otherButton:nil
+ informativeTextWithFormat:NSLocalizedString(@"File couldn't be read.", @"error while reading data file")];
- if(idx > 1) {
- [favoriteNameTextField setStringValue:[[favorites objectAtIndex:idx] objectForKey:@"name"]];
- [favoriteQueryTextView setString:[[favorites objectAtIndex:idx] objectForKey:@"query"]];
+ [alert setAlertStyle:NSCriticalAlertStyle];
+ [alert runModal];
+ return;
}
- [removeButton setEnabled:([favorites count] > 1)];
+ if([spf objectForKey:@"queryFavorites"] && [[spf objectForKey:@"queryFavorites"] count]) {
+ [favorites addObjectsFromArray:[spf objectForKey:@"queryFavorites"]];
+ [favoritesArrayController rearrangeObjects];
+ [favoritesTableView reloadData];
+ } else {
+ NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Error while reading data file", @"error while reading data file")]
+ defaultButton:NSLocalizedString(@"OK", @"OK button")
+ alternateButton:nil
+ otherButton:nil
+ informativeTextWithFormat:NSLocalizedString(@"No query favorites found.", @"error that no query favorites found")];
+
+ [alert setAlertStyle:NSInformationalAlertStyle];
+ [alert runModal];
+ return;
+ }
}
}
}
+
/**
* Save panel did end method.
*/
@@ -736,7 +736,8 @@
// Build a SPF with format = "query favorites"
NSMutableDictionary *spfdata = [NSMutableDictionary dictionary];
NSMutableArray *favoriteData = [NSMutableArray array];
- NSMutableDictionary *data = [NSMutableDictionary dictionary];
+
+
[spfdata setObject:[NSNumber numberWithInt:1] forKey:@"version"];
[spfdata setObject:@"query favorites" forKey:@"format"];
[spfdata setObject:[NSNumber numberWithBool:NO] forKey:@"encrypted"];
@@ -749,8 +750,7 @@
if([indexes containsIndex:i])
[favoriteData addObject:[favorites objectAtIndex:i]];
- [data setObject:favoriteData forKey:@"queryFavorites"];
- [spfdata setObject:data forKey:@"data"];
+ [spfdata setObject:favoriteData forKey:@"queryFavorites"];
NSString *err = nil;
NSData *plist = [NSPropertyListSerialization dataFromPropertyList:spfdata
@@ -777,18 +777,11 @@
}
}
-/*
- * Before an other favorite will be chosen or an other event will unfocus the
- * query text view store the query into the data source
- */
-- (void)_writePendingQueryString
+- (void)_initWithNoSelection
{
- if(selectedRowBeforeChangingSelection > 0 && selectedRowBeforeChangingSelection < [favorites count] ) {
- [[favorites objectAtIndex:selectedRowBeforeChangingSelection] setObject:[NSString stringWithString:pendingQueryString] forKey:@"query"];
- } else if(selectedRowBeforeChangingSelection = 0) {
- [[favorites objectAtIndex:[favoritesTableView selectedRow]] setObject:[NSString stringWithString:pendingQueryString] forKey:@"query"];
- }
- [pendingQueryString setString:@""];
+ [favoritesTableView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
+ [[favoriteNameTextField cell] setPlaceholderString:SP_NO_SELECTION_PLACEHOLDER_STRING];
+ [favoriteNameTextField setStringValue:@""];
+ [favoriteQueryTextView setString:@""];
}
-
@end
diff --git a/Source/SPTableView.m b/Source/SPTableView.m
index f52b44f5..9a0db4fc 100644
--- a/Source/SPTableView.m
+++ b/Source/SPTableView.m
@@ -48,7 +48,7 @@
return nil;
}
if([[[[self delegate] class] description] isEqualToString:@"SPQueryFavoriteManager"]) {
- if([NSArrayObjectAtIndex([[self delegate] valueForKeyPath:@"favoriteProperties"], row) intValue] == SP_FAVORITETYPE_HEADER)
+ if([NSArrayObjectAtIndex([[self delegate] valueForKeyPath:@"favorites"], row) objectForKey:@"headerOfFileURL"])
return nil;
}