-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZXDocumentConfigController.m
More file actions
116 lines (106 loc) · 3.92 KB
/
Copy pathZXDocumentConfigController.m
File metadata and controls
116 lines (106 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Name: ZXDocumentController.m
* Project: Strongbox
* Created on: 2008-07-15
*
* Copyright (C) 2008 Pierre-Hans Corcoran
*
* --------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (version 2) as published
* by the Free Software Foundation. This program is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have
* received a copy of the GNU General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* --------------------------------------------------------------------------
*/
#import "ZXDocumentConfigController.h"
#import "ZXNotification.h"
#import "ZXAccountController.h"
@interface ZXDocumentConfigController (Private)
- (void)setAccountSelection:(NSNotification *)note;
@end
@implementation ZXDocumentConfigController
//! Prepares content of the controller
/*!
Fetches and sets content from saved data. Registers to set selection when
accountController is ready.
*/
- (void)prepareContent
{
[super prepareContent];
[[owner managedObjectContext] processPendingChanges];
[[owner undoManager] disableUndoRegistration];
// FIXME: Check if that is necessary
id docDesc = [NSEntityDescription entityForName:@"DocumentConfig"
inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:docDesc];
[fetchRequest setFetchLimit:1];
NSError *error = nil;
NSArray *array = [[self managedObjectContext] executeFetchRequest:fetchRequest
error:&error];
if(array == nil) {
// FIXME: Some real error management needed.
[[owner undoManager] enableUndoRegistration];
NSLog(@"Could not load Document Config: %@", error);
return;
}
if([array count] < 1) {
[self setContent:[[self newObject] autorelease]];
} else {
[self setContent:[array objectAtIndex:0]];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setAccountSelection:)
name:ZXAccountControllerDidLoadNotification
object:nil];
[[owner managedObjectContext] processPendingChanges];
[[owner undoManager] enableUndoRegistration];
}
- (void)setValue:(id)value forKey:(NSString *)key
{
[super setValue:value forKey:key];
}
//! Restores previously saved selection upon initialization
/*!
Fetch the account by name and sets selection in accountController.
*/
- (void)setAccountSelection:(NSNotification *)note
{
NSError *error;
if(!accountController) return;
[[owner managedObjectContext] processPendingChanges];
[[owner undoManager] disableUndoRegistration];
[accountController fetchWithRequest:nil merge:NO error:&error];
id arr = [accountController valueForKey:@"arrangedObjects"];
id selectedAccount = nil;
id curAccountName = [[self content] valueForKey:@"currentAccountName"];
for(id account in arr) {
if([[account valueForKey:@"name"] isEqual:curAccountName]) {
selectedAccount = account;
break;
}
}
if(selectedAccount != nil) {
[accountController setSelectionIndexes:[NSIndexSet indexSet]]; // Clear selection
[accountController addSelectedObjects:[NSArray arrayWithObject:selectedAccount]];
[accountController updateGeneralMessage:nil];
}
[[owner managedObjectContext] processPendingChanges];
[[owner undoManager] enableUndoRegistration];
}
- (void)updateCurrentAccountName
{
[[self content] setValue:[accountController valueForKeyPath:@"selection.name"]
forKey:@"currentAccountName"];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end