From b5c4f326fb175499d0b837cb325b9eeea676fb4f Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 19 Nov 2010 10:29:42 +0000 Subject: =?UTF-8?q?=E2=80=A2=20Bundle=20Editor=20-=20fixed=20saving=20and?= =?UTF-8?q?=20duplicating=20Bundles=20if=20Bundle=20contains=20other=20fil?= =?UTF-8?q?es/folder=20-=20fixed=20adding=20a=20new=20Bundle=20-=20added?= =?UTF-8?q?=20gui=20tooltips=20=E2=80=A2=20SPTooltip=20-=20if=20no=20locat?= =?UTF-8?q?ion=20was=20passed=20and=20first=20responder=20isn't=20a=20text?= =?UTF-8?q?View=20show=20the=20tooltip=20at=20the=20mouse=20location=20?= =?UTF-8?q?=E2=80=A2=20CopyTable=20-=20show=20tooltip=20invoked=20by=20a?= =?UTF-8?q?=20Bundle=20command=20always=20at=20mouse=20location=20?= =?UTF-8?q?=E2=80=A2=20Content=20/=20Custom=20Query=20Table=20-=20suppress?= =?UTF-8?q?=20tooltip=20of=20cell=20content=20if=20another=20tooltip=20(ma?= =?UTF-8?q?inly=20displayed=20by=20a=20Bundle)=20is=20visible=20[shoudl=20?= =?UTF-8?q?be=20improved=20since=20iterating=20through=20the=20window=20li?= =?UTF-8?q?st=20isn't=20so=20good]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPBundleEditorController.m | 77 ++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 14 deletions(-) (limited to 'Source/SPBundleEditorController.m') diff --git a/Source/SPBundleEditorController.m b/Source/SPBundleEditorController.m index deb8e0be..574836e2 100644 --- a/Source/SPBundleEditorController.m +++ b/Source/SPBundleEditorController.m @@ -325,16 +325,56 @@ // Store pending changes in Query [[self window] makeFirstResponder:nameTextField]; - // Duplicate a selected favorite if sender == self + // Duplicate a selected Bundle if sender == self if (sender == self) { NSDictionary *currentDict = [commandBundleArray objectAtIndex:[commandsTableView selectedRow]]; bundle = [NSMutableDictionary dictionaryWithDictionary:currentDict]; - [bundle setObject:[NSString stringWithFormat:@"%@_Copy", [bundle objectForKey:@"bundleName"]] forKey:@"bundleName"]; + + NSString *bundleFileName = [bundle objectForKey:@"bundleName"]; + NSString *newFileName = [NSString stringWithFormat:@"%@_Copy", [bundle objectForKey:@"bundleName"]]; + NSString *possibleExisitingBundleFilePath = [NSString stringWithFormat:@"%@/%@.%@", bundlePath, bundleFileName, SPUserBundleFileExtension]; + NSString *newBundleFilePath = [NSString stringWithFormat:@"%@/%@.%@", bundlePath, newFileName, SPUserBundleFileExtension]; + + BOOL isDir; + BOOL copyingWasSuccessful = YES; + // Copy possible existing bundle with content + if([[NSFileManager defaultManager] fileExistsAtPath:possibleExisitingBundleFilePath isDirectory:&isDir] && isDir) { + if(![[NSFileManager defaultManager] copyItemAtPath:possibleExisitingBundleFilePath toPath:newBundleFilePath error:nil]) + copyingWasSuccessful = NO; + } + if(!copyingWasSuccessful) { + // try again with new name + newFileName = [NSString stringWithFormat:@"%@_%ld", newFileName, (NSUInteger)(random() % 35000)]; + newBundleFilePath = [NSString stringWithFormat:@"%@/%@.%@", bundlePath, newFileName, SPUserBundleFileExtension]; + if([[NSFileManager defaultManager] fileExistsAtPath:possibleExisitingBundleFilePath isDirectory:&isDir] && isDir) { + if([[NSFileManager defaultManager] copyItemAtPath:possibleExisitingBundleFilePath toPath:newBundleFilePath error:nil]) + copyingWasSuccessful = YES; + } + } + if(!copyingWasSuccessful) { + + [commandsTableView reloadData]; + + NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Error", @"error") + defaultButton:NSLocalizedString(@"OK", @"OK button") + alternateButton:nil + otherButton:nil + informativeTextWithFormat:NSLocalizedString(@"Error while duplicating Bundle content.", @"error while duplicating Bundle content")]; + + [alert setAlertStyle:NSCriticalAlertStyle]; + [alert runModal]; + + return; + + } + + [bundle setObject:newFileName forKey:@"bundleName"]; + } - // Add a new favorite + // Add a new Bundle else { - bundle = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"New Bundle", @"New Name", @"", nil] - forKeys:[NSArray arrayWithObjects:@"bundleName", @"name", @"command", nil]]; + bundle = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"New Bundle", @"New Name", @"", SPBundleScopeInputField, [NSNumber numberWithInt:1], nil] + forKeys:[NSArray arrayWithObjects:@"bundleName", SPBundleFileNameKey, SPBundleFileCommandKey, SPBundleFileScopeKey, SPBundleScopeInputField, nil]]; } if ([commandsTableView numberOfSelectedRows] > 0) { insertIndex = [[commandsTableView selectedRowIndexes] lastIndex]+1; @@ -354,6 +394,7 @@ [removeButton setEnabled:([commandsTableView numberOfSelectedRows] > 0)]; [[self window] makeFirstResponder:commandsTableView]; + [self scopeButtonChanged:nil]; if([commandsTableView numberOfSelectedRows] > 0) [commandsTableView editColumn:0 row:insertIndex withEvent:nil select:YES]; } @@ -487,14 +528,7 @@ NSInteger idx = 0; for(id item in commandBundleArray) { if([allNames objectForKey:[item objectForKey:@"bundleName"]]) { - NSInteger i = 0; - NSString *newName = [NSString stringWithFormat:@"%@_%ld", [item objectForKey:@"bundleName"], i++]; - while([allNames objectForKey:newName]) { - newName = [NSString stringWithFormat:@"%@_%ld", [item objectForKey:@"bundleName"], i++]; - if(i>100) { - return NO; - } - } + NSString *newName = [NSString stringWithFormat:@"%@_%ld", [item objectForKey:@"bundleName"], (NSUInteger)(random() % 35000)]; [[commandBundleArray objectAtIndex:idx] setObject:newName forKey:@"bundleName"]; } else { [allNames setObject:@"" forKey:[item objectForKey:@"bundleName"]]; @@ -648,7 +682,22 @@ } } else if([contextInfo isEqualToString:@"saveBundle"]) { if (returnCode == NSOKButton) { - if(![self saveBundle:[commandBundleArray objectAtIndex:[commandsTableView selectedRow]] atPath:[sheet filename]]) { + + id aBundle = [commandBundleArray objectAtIndex:[commandsTableView selectedRow]]; + + NSString *bundleFileName = [aBundle objectForKey:@"bundleName"]; + NSString *possibleExisitingBundleFilePath = [NSString stringWithFormat:@"%@/%@.%@", bundlePath, bundleFileName, SPUserBundleFileExtension]; + + NSString *savePath = [sheet filename]; + + BOOL isDir; + BOOL copyingWasSuccessful = YES; + // Copy possible existing bundle with content + if([[NSFileManager defaultManager] fileExistsAtPath:possibleExisitingBundleFilePath isDirectory:&isDir] && isDir) { + if(![[NSFileManager defaultManager] copyItemAtPath:possibleExisitingBundleFilePath toPath:savePath error:nil]) + copyingWasSuccessful = NO; + } + if(!copyingWasSuccessful || ![self saveBundle:aBundle atPath:savePath]) { NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Error while saving the Bundle.", @"error while saving a Bundle") defaultButton:NSLocalizedString(@"OK", @"OK button") alternateButton:nil -- cgit v1.2.3