Files
MacPass/MacPass/MPAppDelegate.m
michael starke 780c8782e6 Updated Localization
Added configureable Table headers in Entry view
2013-07-31 02:50:13 +02:00

146 lines
5.5 KiB
Objective-C

//
// MPAppDelegate.m
// MacPass
//
// Created by Michael Starke on 19.07.12.
// Copyright (c) 2012 HicknHack Software GmbH. All rights reserved.
//
#import "MPAppDelegate.h"
#import "MPSettingsWindowController.h"
#import "MPPasswordCreatorViewController.h"
#import "MPSettingsHelper.h"
#import "MPUppercaseStringValueTransformer.h"
#import "MPStringLengthValueTransformer.h"
#import "MPStripLineBreaksTransformer.h"
#import "MPServerDaemon.h"
#import "MPLockDaemon.h"
#import "MPDocumentWindowController.h"
@interface MPAppDelegate () {
@private
MPServerDaemon *serverDaemon;
MPLockDaemon *lockDaemon;
BOOL _restoredWindows;
}
@property (strong, nonatomic) MPSettingsWindowController *settingsController;
@property (strong, nonatomic) MPPasswordCreatorViewController *passwordCreatorController;
- (IBAction)showPreferences:(id)sender;
@end
@implementation MPAppDelegate
+ (void)initialize {
[MPSettingsHelper setupDefaults];
[MPUppercaseStringValueTransformer registerTransformer];
[MPStringLengthValueTransformer registerTransformer];
[MPStripLineBreaksTransformer registerTransformer];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
return [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyOpenEmptyDatabaseOnLaunch];
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
BOOL reopen = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch];
_restoredWindows = NO;
if(reopen) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_applicationDidFinishRestoringWindows:)
name:NSApplicationDidFinishRestoringWindowsNotification
object:nil];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
serverDaemon = [[MPServerDaemon alloc] init];
lockDaemon = [[MPLockDaemon alloc] init];
BOOL reopen = [[NSUserDefaults standardUserDefaults] boolForKey:kMPSettingsKeyReopenLastDatabaseOnLaunch];
if(reopen && !_restoredWindows) {
NSDocumentController *documentController = [NSDocumentController sharedDocumentController];
NSArray *documents = [documentController documents];
if([documents count] > 0) {
return; // There's a document open
}
NSArray *recentDocuments = [documentController recentDocumentURLs];
NSURL *documentUrl;
if([recentDocuments count] > 0) {
documentUrl = recentDocuments[0];
}
else {
NSString *lastPath = [[NSUserDefaults standardUserDefaults] stringForKey:kMPSettingsKeyLastDatabasePath];
documentUrl = [NSURL URLWithString:lastPath];
}
if([documentUrl isFileURL]) {
[documentController openDocumentWithContentsOfURL:documentUrl display:YES
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) {}];
}
}
}
- (NSString *)applicationName {
return [[NSBundle mainBundle] infoDictionary][@"CFBundleName"];
}
#pragma mark Menu Actions
- (void)showPreferences:(id)sender {
if(self.settingsController == nil) {
self.settingsController = [[MPSettingsWindowController alloc] init];
}
[self.settingsController showSettings];
}
- (void)showPasswordCreator:(id)sender {
if(!self.passwordCreatorWindow) {
[[NSBundle mainBundle] loadNibNamed:@"PasswordCreatorWindow"owner:self topLevelObjects:nil];
}
if(!self.passwordCreatorController) {
self.passwordCreatorController = [[MPPasswordCreatorViewController alloc] init];
NSView *creatorView = [_passwordCreatorController view];
//NSView *contentView = [_passwordCreatorWindow contentView];
[self.passwordCreatorWindow setContentView:creatorView];
//[contentView addSubview:creatorView];
// [[_passwordCreatorWindow contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[creatorView]|"
// options:0
// metrics:nil
// views:NSDictionaryOfVariableBindings(creatorView)]];
// [[_passwordCreatorWindow contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[creatorView]|"
// options:0
// metrics:nil
// views:NSDictionaryOfVariableBindings(creatorView)]];
// [contentView layoutSubtreeIfNeeded];
}
[self.passwordCreatorWindow makeKeyAndOrderFront:self.passwordCreatorWindow];
}
- (void)lockAllDocuments {
for(NSDocument *document in [[NSDocumentController sharedDocumentController] documents]) {
NSArray *windowControllers = [document windowControllers];
if([windowControllers count] > 0) {
[windowControllers[0] lock:nil];
}
}
}
- (void)_applicationDidFinishRestoringWindows:(NSNotification *)notification {
NSDocumentController *documentController = [NSDocumentController sharedDocumentController];
NSArray *documents = [documentController documents];
_restoredWindows = [documents count] > 0;
}
@end