Make OakSavePanel API block-based

This commit is contained in:
Allan Odgaard
2013-01-04 23:38:55 +01:00
parent 293d9a8e46
commit a166ad481c
4 changed files with 44 additions and 70 deletions

View File

@@ -873,7 +873,7 @@ static document::document_ptr create_document (NSString* fileBrowserPath)
// = Document Action Methods =
// ===========================
- (void)savePanelDidEnd:(OakSavePanel*)sheet path:(NSString*)aPath encoding:(encoding::type const&)encoding
- (void)saveDocumentAsPath:(NSString*)aPath encoding:(encoding::type const&)encoding
{
if(!aPath)
return;
@@ -911,8 +911,15 @@ static document::document_ptr create_document (NSString* fileBrowserPath)
D(DBF_DocumentController, bug("%s\n", [self selectedDocument]->path().c_str()););
document::document_ptr doc = [self selectedDocument];
if(doc->path() != NULL_STR)
[DocumentSaveHelper trySaveDocument:doc forWindow:self.window defaultDirectory:nil andCallback:NULL];
else [OakSavePanel showWithPath:DefaultSaveNameForDocument(doc) directory:self.untitledSavePath fowWindow:self.window delegate:self encoding:doc->encoding_for_save_as_path(to_s([(self.untitledSavePath ?: @"") stringByAppendingPathComponent:DefaultSaveNameForDocument(doc)]))];
{
[DocumentSaveHelper trySaveDocument:doc forWindow:self.window defaultDirectory:nil andCallback:NULL];
}
else
{
[OakSavePanel showWithPath:DefaultSaveNameForDocument(doc) directory:self.untitledSavePath fowWindow:self.window encoding:doc->encoding_for_save_as_path(to_s([(self.untitledSavePath ?: @"") stringByAppendingPathComponent:DefaultSaveNameForDocument(doc)])) completionHandler:^(NSString* path, encoding::type const& encoding){
[self saveDocumentAsPath:path encoding:encoding];
}];
}
}
- (IBAction)saveDocumentAs:(id)sender
@@ -922,7 +929,9 @@ static document::document_ptr create_document (NSString* fileBrowserPath)
std::string const documentPath = doc->path();
NSString* documentFolder = [NSString stringWithCxxString:path::parent(documentPath)];
NSString* documentName = [NSString stringWithCxxString:path::name(documentPath)];
[OakSavePanel showWithPath:(documentName ?: DefaultSaveNameForDocument(doc)) directory:(documentFolder ?: self.untitledSavePath) fowWindow:self.window delegate:self encoding:doc->encoding_for_save_as_path(doc->path() != NULL_STR ? doc->path() : to_s([(self.untitledSavePath ?: @"") stringByAppendingPathComponent:DefaultSaveNameForDocument(doc)]))];
[OakSavePanel showWithPath:(documentName ?: DefaultSaveNameForDocument(doc)) directory:(documentFolder ?: self.untitledSavePath) fowWindow:self.window encoding:doc->encoding_for_save_as_path(doc->path() != NULL_STR ? doc->path() : to_s([(self.untitledSavePath ?: @"") stringByAppendingPathComponent:DefaultSaveNameForDocument(doc)])) completionHandler:^(NSString* path, encoding::type const& encoding){
[self saveDocumentAsPath:path encoding:encoding];
}];
}
- (IBAction)saveAllDocuments:(id)sender

View File

@@ -46,7 +46,20 @@ namespace
D(DBF_DocumentController_SaveHelper, bug("\n"););
init(context);
[OakSavePanel showWithPath:DefaultSaveNameForDocument(_document) directory:_self.saveFolder fowWindow:_window delegate:_self encoding:_document->encoding_for_save_as_path(to_s([_self.saveFolder stringByAppendingPathComponent:DefaultSaveNameForDocument(_document)]))];
[OakSavePanel showWithPath:DefaultSaveNameForDocument(_document) directory:_self.saveFolder fowWindow:_window encoding:_document->encoding_for_save_as_path(to_s([_self.saveFolder stringByAppendingPathComponent:DefaultSaveNameForDocument(_document)])) completionHandler:^(NSString* path, encoding::type const& encoding){
D(DBF_DocumentController_SaveHelper, bug("%s\n", to_s(path).c_str()););
if(path)
{
_document->set_path(to_s(path));
_document->set_disk_encoding(encoding);
context->set_path(to_s(path));
}
else
{
_self.userAbort = YES;
}
[_self setContext:file::save_context_ptr()];
}];
}
void select_make_writable (std::string const& path, io::bytes_ptr content, file::save_context_ptr context)
@@ -197,24 +210,4 @@ namespace
if(flag && [[window delegate] respondsToSelector:@selector(updateProxyIcon)])
[[window delegate] performSelector:@selector(updateProxyIcon)]; // FIXME The delegate needs to update proxy icon based on “exists on disk” notifications from document_t
}
// ===================
// = Sheet Callbacks =
// ===================
- (void)savePanelDidEnd:(OakSavePanel*)sheet path:(NSString*)aPath encoding:(encoding::type const&)encoding
{
D(DBF_DocumentController_SaveHelper, bug("%s\n", to_s(aPath).c_str()););
if(aPath)
{
documents.back()->set_path(to_s(aPath));
documents.back()->set_disk_encoding(encoding);
context->set_path(to_s(aPath));
}
else
{
userAbort = YES;
}
context.reset();
}
@end

View File

@@ -1,15 +1,6 @@
#include <file/encoding.h>
#import <oak/misc.h>
@class OakEncodingSaveOptionsViewController;
PUBLIC @interface OakSavePanel : NSObject
{
OakEncodingSaveOptionsViewController* optionsViewController;
}
+ (void)showWithPath:(NSString*)aPathSuggestion directory:(NSString*)aDirectorySuggestion fowWindow:(NSWindow*)aWindow delegate:(id)aDelegate encoding:(encoding::type const&)encoding;
@end
@interface NSObject (OakSavePanelDelegate)
- (void)savePanelDidEnd:(OakSavePanel*)sheet path:(NSString*)aPath encoding:(encoding::type const&)encoding;
+ (void)showWithPath:(NSString*)aPathSuggestion directory:(NSString*)aDirectorySuggestion fowWindow:(NSWindow*)aWindow encoding:(encoding::type const&)encoding completionHandler:(void(^)(NSString* path, encoding::type const& encoding))aCompletionHandler;
@end

View File

@@ -62,43 +62,24 @@
@end
@implementation OakSavePanel
- (id)initWithPath:(NSString*)aPathSuggestion directory:(NSString*)aDirectorySuggestion fowWindow:(NSWindow*)aWindow delegate:(id)aDelegate encoding:(encoding::type const&)encoding
+ (void)showWithPath:(NSString*)aPathSuggestion directory:(NSString*)aDirectorySuggestion fowWindow:(NSWindow*)aWindow encoding:(encoding::type const&)encoding completionHandler:(void(^)(NSString* path, encoding::type const& encoding))aCompletionHandler
{
if((self = [super init]))
{
optionsViewController = [[OakEncodingSaveOptionsViewController alloc] initWithEncodingOptions:encoding];
if(!optionsViewController)
{
[self release];
return nil;
}
OakEncodingSaveOptionsViewController* optionsViewController = [[[OakEncodingSaveOptionsViewController alloc] initWithEncodingOptions:encoding] autorelease];
if(!optionsViewController)
return;
[[aWindow attachedSheet] orderOut:self]; // incase there already is a sheet showing (like “Do you want to save?”)
[[aWindow attachedSheet] orderOut:self]; // incase there already is a sheet showing (like “Do you want to save?”)
NSSavePanel* savePanel = [NSSavePanel savePanel];
[savePanel setTreatsFilePackagesAsDirectories:YES];
if(aDirectorySuggestion)
[savePanel setDirectoryURL:[NSURL fileURLWithPath:aDirectorySuggestion]];
[savePanel setNameFieldStringValue:[aPathSuggestion lastPathComponent]];
[savePanel setAccessoryView:optionsViewController.view];
[savePanel beginSheetModalForWindow:aWindow completionHandler:^(NSInteger result) {
NSString* path = result == NSOKButton ? [[savePanel.URL filePathURL] path] : nil;
[aDelegate savePanelDidEnd:self path:path encoding:optionsViewController.encodingOptions];
[self release];
}];
[savePanel deselectExtension];
}
return self;
}
- (void)dealloc
{
[optionsViewController release];
[super dealloc];
}
+ (void)showWithPath:(NSString*)aPathSuggestion directory:(NSString*)aDirectorySuggestion fowWindow:(NSWindow*)aWindow delegate:(id)aDelegate encoding:(encoding::type const&)encoding
{
[[OakSavePanel alloc] initWithPath:aPathSuggestion directory:aDirectorySuggestion fowWindow:aWindow delegate:aDelegate encoding:encoding];
NSSavePanel* savePanel = [NSSavePanel savePanel];
[savePanel setTreatsFilePackagesAsDirectories:YES];
if(aDirectorySuggestion)
[savePanel setDirectoryURL:[NSURL fileURLWithPath:aDirectorySuggestion]];
[savePanel setNameFieldStringValue:[aPathSuggestion lastPathComponent]];
[savePanel setAccessoryView:optionsViewController.view];
[savePanel beginSheetModalForWindow:aWindow completionHandler:^(NSInteger result) {
NSString* path = result == NSOKButton ? [[savePanel.URL filePathURL] path] : nil;
aCompletionHandler(path, optionsViewController.encodingOptions);
}];
[savePanel deselectExtension];
}
@end