Use block-based callback API

This commit is contained in:
Allan Odgaard
2013-01-01 17:26:13 +01:00
parent 012db690f5
commit 079b762c42
5 changed files with 47 additions and 77 deletions

View File

@@ -1,9 +1,8 @@
#import "FileTypeDialog.h"
#import <document/document.h>
@protocol DocumentOpenHelperDelegate;
@interface DocumentOpenHelper : NSObject <FileTypeDialogDelegate>
@interface DocumentOpenHelper : NSObject
@property (nonatomic, weak) id <DocumentOpenHelperDelegate> delegate;
- (void)tryOpenDocument:(document::document_ptr const&)aDocument forWindow:(NSWindow*)aWindow delegate:(id <DocumentOpenHelperDelegate>)aDelegate;
@end

View File

@@ -1,5 +1,6 @@
#import "DocumentOpenHelper.h"
#import "EncodingView.h"
#import "FileTypeDialog.h"
#import <OakAppKit/OakAppKit.h>
#import <OakFoundation/NSString Additions.h>
#import <ns/ns.h>
@@ -15,14 +16,6 @@ OAK_DEBUG_VAR(DocumentController_OpenHelper);
namespace
{
struct info_t
{
info_t (EncodingViewController* controller, file::open_context_ptr context) : controller(controller), context(context) { }
EncodingViewController* controller;
file::open_context_ptr context;
};
struct open_callback_t : document::open_callback_t
{
open_callback_t (DocumentOpenHelper* self, NSWindow* window) : _self(self), _window(window)
@@ -55,7 +48,10 @@ namespace
[_window.attachedSheet orderOut:_self];
FileTypeDialog* controller = [[FileTypeDialog alloc] initWithPath:[NSString stringWithCxxString:path] first:(content ? content->begin() : NULL) last:(content ? content->end() : NULL)];
[controller beginSheetModalForWindow:_window modalDelegate:_self contextInfo:new info_t(nil, context)];
[controller beginSheetModalForWindow:_window completionHandler:^(NSString* fileType){
if(fileType)
context->set_file_type(to_s(fileType));
}];
}
}
@@ -97,13 +93,6 @@ namespace
}
}
- (void)fileTypeDialog:(FileTypeDialog*)fileTypeDialog didSelectFileType:(NSString*)aFileType contextInfo:(void*)info
{
if(aFileType)
((info_t*)info)->context->set_file_type(to_s(aFileType));
delete (info_t*)info;
}
- (void)didOpenDocument:(document::document_ptr const&)aDocument
{
D(DBF_DocumentController_OpenHelper, bug("%s\n", aDocument->display_name().c_str()););

View File

@@ -1,7 +1,5 @@
enum enabled_grammar_t { kEnabledGrammarsRecommended = 0, kEnabledGrammarsInstalled = 1, kEnabledGrammarsAll = 2 };
@protocol FileTypeDialogDelegate;
@interface FileTypeDialog : NSWindowController
{
IBOutlet NSTextField* alertTextField;
@@ -30,10 +28,6 @@ enum enabled_grammar_t { kEnabledGrammarsRecommended = 0, kEnabledGrammarsInstal
NSString* alertFormatString;
NSString* infoFormatString;
NSString* useForAllFormatString;
NSWindow* mainWindow;
id <FileTypeDialogDelegate> delegate;
void* contextInfo;
}
@property (nonatomic, retain) NSString* path;
@property (nonatomic, assign) NSInteger enabledGrammars;
@@ -55,12 +49,8 @@ enum enabled_grammar_t { kEnabledGrammarsRecommended = 0, kEnabledGrammarsInstal
@property (nonatomic, readonly) NSString* fileType;
- (id)initWithPath:(NSString*)aPath first:(char const*)firstPointer last:(char const*)lastPointer;
- (void)beginSheetModalForWindow:(NSWindow*)aWindow modalDelegate:(id <FileTypeDialogDelegate>)aDelegate contextInfo:(void*)info;
- (void)beginSheetModalForWindow:(NSWindow*)aWindow completionHandler:(void(^)(NSString* fileType))aCompletionHandler;
- (IBAction)performOpenDocument:(id)sender;
- (IBAction)performCancelOperation:(id)sender;
@end
@protocol FileTypeDialogDelegate
- (void)fileTypeDialog:(FileTypeDialog*)fileTypeDialog didSelectFileType:(NSString*)aFileType contextInfo:(void*)info;
@end

View File

@@ -46,6 +46,11 @@ static NSArray* wrap (std::set<grammar_info_t> const& array)
return res;
}
static bool is_installed (oak::uuid_t const& uuid)
{
return bundles::lookup(uuid) ? true : false;
}
@implementation FileTypeDialog
@synthesize path, enabledGrammars, persistentSetting, canOpenDocument;
@synthesize recommendedGrammars, installedGrammars, allGrammars;
@@ -157,25 +162,49 @@ static NSArray* wrap (std::set<grammar_info_t> const& array)
return [self.grammar objectForKey:@"scope"];
}
- (void)beginSheetModalForWindow:(NSWindow*)aWindow modalDelegate:(id <FileTypeDialogDelegate>)aDelegate contextInfo:(void*)info
- (void)beginSheetModalForWindow:(NSWindow*)aWindow completionHandler:(void(^)(NSString* fileType))aCompletionHandler
{
[self setupGrammars];
self.grammars = self.recommendedGrammars;
self.selectedGrammarIndexes = [self.grammars count] == 0 ? [NSIndexSet indexSet] : [NSIndexSet indexSetWithIndex:0];
self.canOpenDocument = YES;
mainWindow = aWindow;
delegate = aDelegate;
contextInfo = info;
OakShowSheetForWindow(self.window, aWindow, ^(NSInteger returnCode){
[self sheetDidEnd:self.window returnCode:returnCode contextInfo:NULL];
});
}
self.canOpenDocument = NO;
if(returnCode == NSRunAbortedResponse)
return aCompletionHandler(nil);
static bool is_installed (oak::uuid_t const& uuid)
{
return bundles::lookup(uuid) ? true : false;
NSDictionary* grammar = self.grammar;
std::string scope = to_s((NSString*)[grammar objectForKey:@"scope"]);
oak::uuid_t uuid = to_s((NSString*)[grammar objectForKey:@"uuid"]);
oak::uuid_t bundleUUID = to_s((NSString*)[grammar objectForKey:@"bundleUUID"]);
if(is_installed(uuid))
{
if(self.persistentSetting)
file::set_type(to_s(path), scope);
return aCompletionHandler(self.fileType);
}
citerate(bundle, bundles_db::index())
{
if(bundleUUID == (*bundle)->uuid())
{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkIfBundleIsInstalled:) userInfo:[grammar objectForKey:@"uuid"] repeats:YES];
[[BundlesManager sharedInstance] installBundle:*bundle];
[installingBundleActivityTextField bind:@"value" toObject:[BundlesManager sharedInstance] withKeyPath:@"activityText" options:nil];
[installingBundleProgressIndicator bind:@"value" toObject:[BundlesManager sharedInstance] withKeyPath:@"progress" options:nil];
[installingBundleProgressIndicator startAnimation:self];
OakShowSheetForWindow(installingBundleWindow, aWindow, ^(NSInteger returnCode){
aCompletionHandler(self.fileType);
});
return;
}
}
aCompletionHandler(nil);
});
}
- (void)checkIfBundleIsInstalled:(NSTimer*)aTimer
@@ -193,43 +222,6 @@ static bool is_installed (oak::uuid_t const& uuid)
}
}
- (void)sheetDidEnd:(NSWindow*)aSheet returnCode:(NSInteger)returnCode contextInfo:(void*)unused;
{
self.canOpenDocument = NO;
if(returnCode == NSRunAbortedResponse)
return [delegate fileTypeDialog:self didSelectFileType:nil contextInfo:contextInfo];
NSDictionary* grammar = self.grammar;
std::string scope = to_s((NSString*)[grammar objectForKey:@"scope"]);
oak::uuid_t uuid = to_s((NSString*)[grammar objectForKey:@"uuid"]);
oak::uuid_t bundleUUID = to_s((NSString*)[grammar objectForKey:@"bundleUUID"]);
if(is_installed(uuid))
{
if(self.persistentSetting)
file::set_type(to_s(path), scope);
return [delegate fileTypeDialog:self didSelectFileType:self.fileType contextInfo:contextInfo];
}
citerate(bundle, bundles_db::index())
{
if(bundleUUID == (*bundle)->uuid())
{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkIfBundleIsInstalled:) userInfo:[grammar objectForKey:@"uuid"] repeats:YES];
[[BundlesManager sharedInstance] installBundle:*bundle];
[installingBundleActivityTextField bind:@"value" toObject:[BundlesManager sharedInstance] withKeyPath:@"activityText" options:nil];
[installingBundleProgressIndicator bind:@"value" toObject:[BundlesManager sharedInstance] withKeyPath:@"progress" options:nil];
[installingBundleProgressIndicator startAnimation:self];
OakShowSheetForWindow(installingBundleWindow, mainWindow, ^(NSInteger returnCode){
[delegate fileTypeDialog:self didSelectFileType:self.fileType contextInfo:contextInfo];
});
return;
}
}
return [delegate fileTypeDialog:self didSelectFileType:nil contextInfo:contextInfo];
}
- (IBAction)performOpenDocument:(id)sender
{
[self.window orderOut:self];

View File

@@ -1,5 +1,5 @@
SOURCES = src/*.mm
CP_Resources = resources/*
LINK += command text authorization scm bundles cf Find HTMLOutputWindow io ns OakAppKit OakFileBrowser OakFilterList OakFoundation OakSystem OakTextView BundleEditor network Preferences
EXPORT = src/DocumentController.h src/DocumentOpenHelper.h src/FileTypeDialog.h
EXPORT = src/DocumentController.h src/DocumentOpenHelper.h
FRAMEWORKS = Cocoa