From 9f5c7db05e8a999df38b200d93031b1475adf5dc Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 23 Nov 2010 12:45:07 +0000 Subject: =?UTF-8?q?=E2=80=A2=20Bundle=20Editor=20-=20bailed=20out=20from?= =?UTF-8?q?=20approach=20to=20assign=20more=20than=20one=20scope=20to=20Bu?= =?UTF-8?q?ndle=20commands;=20now=20there're=20three=20scopes=20available:?= =?UTF-8?q?=20Input=20Field=20(incl.=20Query=20Editor=20since=20it=20only?= =?UTF-8?q?=20differs=20in=20current=5Fquery=20and=20insert=5Fas=5Fsnippet?= =?UTF-8?q?=20which=20falls=20back=20to=20current=5Fline=20and=20tooltip?= =?UTF-8?q?=20message),=20Data=20Table,=20and=20General=20(which=20means?= =?UTF-8?q?=20that=20these=20commands=20are=20available=20app-wide)=20-=20?= =?UTF-8?q?moved=20"Disable=20Command"=20to=20scope=20popup=20menu=20since?= =?UTF-8?q?=20it's=20related=20-=20Input=20Field=20and=20Data=20Table=20co?= =?UTF-8?q?mmands=20will=20be=20shown=20as=20submenus=20in=20the=20Bundles?= =?UTF-8?q?=20main=20menu;=20all=20General=20commands=20will=20be=20added?= =?UTF-8?q?=20without=20creating=20a=20special=20submenu=20(only=20Categor?= =?UTF-8?q?y=20submenus=20will=20be=20generated)=20-=20fixed=20and=20simpl?= =?UTF-8?q?ified=20several=20issues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPAppController.m | 166 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 155 insertions(+), 11 deletions(-) (limited to 'Source/SPAppController.m') diff --git a/Source/SPAppController.m b/Source/SPAppController.m index c5704f80..9a4e23e6 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -33,6 +33,9 @@ #import "SPWindowController.h" #import "SPPreferencesUpgrade.h" #import "SPBundleEditorController.h" +#import "SPTooltip.h" +#import "SPBundleHTMLOutputController.h" +#import "SPAlertSheets.h" #import #import @@ -598,6 +601,132 @@ NSLog(@"command id: %@", passedProcessID); } +- (IBAction)executeBundleItemForApp:(id)sender +{ + + NSInteger idx = [sender tag] - 1000000; + NSString *infoPath = nil; + NSArray *bundleItems = [[NSApp delegate] bundleItemsForScope:SPBundleScopeGeneral]; + if(idx >=0 && idx < [bundleItems count]) { + infoPath = [[bundleItems objectAtIndex:idx] objectForKey:SPBundleInternPathToFileKey]; + } else { + if([sender tag] == 0 && [[sender toolTip] length]) { + infoPath = [sender toolTip]; + } + } + + if(!infoPath) { + NSBeep(); + return; + } + + NSError *readError = nil; + NSString *convError = nil; + NSPropertyListFormat format; + NSDictionary *cmdData = nil; + NSData *pData = [NSData dataWithContentsOfFile:infoPath options:NSUncachedRead error:&readError]; + + cmdData = [[NSPropertyListSerialization propertyListFromData:pData + mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&convError] retain]; + + if(!cmdData || readError != nil || [convError length] || !(format == NSPropertyListXMLFormat_v1_0 || format == NSPropertyListBinaryFormat_v1_0)) { + NSLog(@"“%@” file couldn't be read.", infoPath); + NSBeep(); + if (cmdData) [cmdData release]; + return; + } else { + if([cmdData objectForKey:SPBundleFileCommandKey] && [[cmdData objectForKey:SPBundleFileCommandKey] length]) { + + NSString *cmd = [cmdData objectForKey:SPBundleFileCommandKey]; + NSString *inputAction = @""; + NSString *inputFallBackAction = @""; + NSError *err = nil; + NSString *bundleInputFilePath = [NSString stringWithFormat:@"%@_%@", SPBundleTaskInputFilePath, [NSString stringWithNewUUID]]; + + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; + + if([cmdData objectForKey:SPBundleFileInputSourceKey]) + inputAction = [[cmdData objectForKey:SPBundleFileInputSourceKey] lowercaseString]; + if([cmdData objectForKey:SPBundleFileInputSourceFallBackKey]) + inputFallBackAction = [[cmdData objectForKey:SPBundleFileInputSourceFallBackKey] lowercaseString]; + + NSMutableDictionary *env = [NSMutableDictionary dictionary]; + [env setObject:[infoPath stringByDeletingLastPathComponent] forKey:@"SP_BUNDLE_PATH"]; + [env setObject:bundleInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; + + NSError *inputFileError = nil; + NSString *input = @""; + if([inputAction isEqualToString:SPBundleInputSourceSelectedTableRowsAsTab]) { + input = [self rowsAsTabStringWithHeaders:YES onlySelectedRows:YES]; + } + else if([inputAction isEqualToString:SPBundleInputSourceSelectedTableRowsAsCsv]) { + input = [self rowsAsCsvStringWithHeaders:YES onlySelectedRows:YES]; + } + else if([inputAction isEqualToString:SPBundleInputSourceSelectedTableRowsAsSqlInsert]) { + input = [self rowsAsSqlInsertsOnlySelectedRows:YES]; + } + else if([inputAction isEqualToString:SPBundleInputSourceTableRowsAsTab]) { + input = [self rowsAsTabStringWithHeaders:YES onlySelectedRows:NO]; + } + else if([inputAction isEqualToString:SPBundleInputSourceTableRowsAsCsv]) { + input = [self rowsAsCsvStringWithHeaders:YES onlySelectedRows:NO]; + } + else if([inputAction isEqualToString:SPBundleInputSourceTableRowsAsSqlInsert]) { + input = [self rowsAsSqlInsertsOnlySelectedRows:NO]; + } + + if(input == nil) input = @""; + [input writeToFile:bundleInputFilePath + atomically:YES + encoding:NSUTF8StringEncoding + error:&inputFileError]; + + if(inputFileError != nil) { + NSString *errorMessage = [inputFileError localizedDescription]; + SPBeginAlertSheet(NSLocalizedString(@"Bundle Error", @"bundle error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], self, nil, nil, + [NSString stringWithFormat:@"%@ “%@”:\n%@", NSLocalizedString(@"Error for", @"error for message"), [cmdData objectForKey:@"name"], errorMessage]); + if (cmdData) [cmdData release]; + return; + } + + NSString *output = [cmd runBashCommandWithEnvironment:env atCurrentDirectoryPath:nil error:&err]; + + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; + + if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) { + if([[cmdData objectForKey:SPBundleFileOutputActionKey] length] + && ![[cmdData objectForKey:SPBundleFileOutputActionKey] isEqualToString:SPBundleOutputActionNone]) { + NSString *action = [[cmdData objectForKey:SPBundleFileOutputActionKey] lowercaseString]; + NSPoint pos = [NSEvent mouseLocation]; + pos.y -= 16; + + if([action isEqualToString:SPBundleOutputActionShowAsTextTooltip]) { + [SPTooltip showWithObject:output atLocation:pos]; + } + + else if([action isEqualToString:SPBundleOutputActionShowAsHTMLTooltip]) { + [SPTooltip showWithObject:output atLocation:pos ofType:@"html"]; + } + + else if([action isEqualToString:SPBundleOutputActionShowAsHTML]) { + SPBundleHTMLOutputController *c = [[SPBundleHTMLOutputController alloc] init]; + [c displayHTMLContent:output withOptions:nil]; + } + } + } else { + NSString *errorMessage = [err localizedDescription]; + SPBeginAlertSheet(NSLocalizedString(@"BASH Error", @"bash error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], self, nil, nil, + [NSString stringWithFormat:@"%@ “%@”:\n%@", NSLocalizedString(@"Error for", @"error for message"), [cmdData objectForKey:@"name"], errorMessage]); + } + + } + + if (cmdData) [cmdData release]; + + } + +} + #pragma mark - #pragma mark Window management @@ -1001,6 +1130,7 @@ // Rebuild Bundles main menu item + // Add default menu items NSMenuItem *anItem; anItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Bundle Editor", @"bundle editor menu item label") action:@selector(openBundleEditor:) keyEquivalent:@"b"]; [anItem setKeyEquivalentModifierMask:(NSCommandKeyMask|NSAlternateKeyMask|NSControlKeyMask)]; @@ -1010,18 +1140,20 @@ [menu addItem:anItem]; [anItem release]; + // Bail out if no Bundle was installed if(!foundInstalledBundles) return; // Add installed Bundles + // For each scope add a submenu but not for the last one (should be General always) [menu addItem:[NSMenuItem separatorItem]]; - NSArray *scopes = [NSArray arrayWithObjects:SPBundleScopeInputField, SPBundleScopeQueryEditor, SPBundleScopeDataTable, nil]; + NSArray *scopes = [NSArray arrayWithObjects:SPBundleScopeInputField, SPBundleScopeDataTable, SPBundleScopeGeneral, nil]; NSArray *scopeTitles = [NSArray arrayWithObjects:NSLocalizedString(@"Input Fields", @"input fields menu item label"), - NSLocalizedString(@"Query Editor", @"query editor menu item label"), - NSLocalizedString(@"Data Table", @"data table menu item label"),nil]; + NSLocalizedString(@"Data Table", @"data table menu item label"), + NSLocalizedString(@"General", @"general menu item label"),nil]; NSArray *scopeSelector = [NSArray arrayWithObjects:@"executeBundleItemForInputField:", - @"executeBundleItemForEditor:", - @"executeBundleItemForDataTable:", nil]; + @"executeBundleItemForDataTable:", + @"executeBundleItemForApp:", nil]; NSInteger k = 0; for(NSString* scope in scopes) { @@ -1034,13 +1166,25 @@ continue; } - NSMenu *bundleMenu = [[[NSMenu alloc] init] autorelease]; - NSMenuItem *bundleSubMenuItem = [[NSMenuItem alloc] initWithTitle:[scopeTitles objectAtIndex:k] action:nil keyEquivalent:@""]; - [bundleSubMenuItem setTag:10000000]; + NSMenu *bundleMenu = nil; + NSMenuItem *bundleSubMenuItem = nil; + + // Add last scope (General) not as submenu + if(k < [scopes count]-1) { + bundleMenu = [[[NSMenu alloc] init] autorelease]; + + bundleSubMenuItem = [[NSMenuItem alloc] initWithTitle:[scopeTitles objectAtIndex:k] action:nil keyEquivalent:@""]; + [bundleSubMenuItem setTag:10000000]; - [menu addItem:bundleSubMenuItem]; - [menu setSubmenu:bundleMenu forItem:bundleSubMenuItem]; + [menu addItem:bundleSubMenuItem]; + [menu setSubmenu:bundleMenu forItem:bundleSubMenuItem]; + + } else { + bundleMenu = menu; + [menu addItem:[NSMenuItem separatorItem]]; + } + // Add found Category submenus NSMutableArray *categorySubMenus = [NSMutableArray array]; NSMutableArray *categoryMenus = [NSMutableArray array]; if([bundleCategories count]) { @@ -1078,7 +1222,7 @@ } } - [bundleSubMenuItem release]; + if(bundleSubMenuItem)[bundleSubMenuItem release]; k++; } -- cgit v1.2.3