-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZXDocument.m
More file actions
368 lines (316 loc) · 11.6 KB
/
ZXDocument.m
File metadata and controls
368 lines (316 loc) · 11.6 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Name: ZXDocument.m
* Project: Strongbox
* Created on: 2008-03-02
*
* 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 "ZXDocument.h"
#import "NSStringExportAdditions.h"
#import "ZXAccountController.h"
#import "ZXAccountMergeController.h"
#import "ZXDocumentConfigController.h"
#import "ZXLabelController.h"
#import "ZXLabelMO.h"
#import "ZXNotification.h"
#import "ZXOldCashboxImporter.h"
#import "ZXPrintTransactionView.h"
#import "ZXReportWindowController.h"
#import "ZXTransactionController.h"
#import "ZXOvalTextFieldCell.h"
#import "ZXOvalPopUpButtonCell.h"
@implementation ZXDocument
@synthesize strongboxWindow, nameSortDescriptors, transactionSortDescriptors;
@synthesize accountController, transactionController, labelController;
@synthesize dateFormatter;
- (id)init
{
self = [super init];
id tmp = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"date"
ascending:NO] autorelease]];
self.transactionSortDescriptors = tmp;
tmp = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES] autorelease]];
self.nameSortDescriptors = tmp;
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[transactionSortDescriptors release];
[nameSortDescriptors release];
[super dealloc];
}
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
[super windowControllerDidLoadNib:windowController];
[ZXNotification postNotificationName:ZXAccountControllerDidLoadNotification
object:self];
[ZXNotification enqueueNotificationName:ZXAccountTotalDidChangeNotification
object:self
postingStyle:NSPostWhenIdle];
[self updateChangeCount:NSChangeCleared];
[transactionsView performSelector:@selector(reloadData)
withObject:nil
afterDelay:1.0];
[strongboxWindow setContentBorderThickness:24.0 forEdge:NSMinYEdge];
}
- (NSString *)windowNibName
{
return @"ZXDocument";
}
#pragma mark Menu items actions
- (IBAction)addTransaction:(id)sender { [transactionController add:sender]; }
- (IBAction)removeTransaction:(id)sender { [transactionController remove:sender]; }
- (IBAction)addLabel:(id)sender { [labelController add:sender]; }
- (IBAction)removeLabel:(id)sender { [labelController remove:sender]; }
- (IBAction)addAccount:(id)sender { [accountController add:sender]; }
- (IBAction)removeAccount:(id)sender { [accountController remove:sender]; }
#pragma mark Control config window
- (IBAction)raiseConfigSheet:(id)sender
{
if(!configSheet) {
[NSBundle loadNibNamed:@"ConfigWindow" owner:self];
}
[NSApp beginSheet:configSheet
modalForWindow:[self strongboxWindow]
modalDelegate:self
didEndSelector:nil
contextInfo:NULL];
}
- (IBAction)endConfigSheet:(id)sender
{
[configSheet orderOut:sender];
[NSApp endSheet:configSheet returnCode:1];
}
#pragma mark Control merge window
- (IBAction)raiseMergeSheet:(id)sender
{
id mergeController = [[[ZXAccountMergeController alloc] initWithOwner:self] autorelease];
[mergeController main];
id arr = [accountController arrangedObjects];
[arr makeObjectsPerformSelector:@selector(recalculateBalance:) withObject:nil];
}
#pragma mark Control report window
- (IBAction)toggleReportWindow:(id)sender
{
if(!reportWindowController) {
reportWindowController = [[ZXReportWindowController alloc] initWithOwner:self];
}
[reportWindowController toggleReportWindow:self];
}
#pragma mark Save options
- (IBAction)saveDocument:(id)sender
{
[documentConfigController updateCurrentAccountName];
[super saveDocument:sender];
}
// Write the last saved document to preference so it is opened automatically next time.
- (BOOL)writeSafelyToURL:(NSURL *)absoluteURL
ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation
error:(NSError **)outError
{
id dict = [[NSUserDefaultsController sharedUserDefaultsController] values];
[dict setValue:[absoluteURL absoluteString] forKey:@"lastFileURL"];
return [super writeSafelyToURL:absoluteURL
ofType:typeName
forSaveOperation:saveOperation
error:outError];
}
#pragma mark Other stuff
- (NSArray *)allLabels
{
id labelDesc = [NSEntityDescription entityForName:@"Label"
inManagedObjectContext:self.managedObjectContext];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"obsolete == NO"];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:labelDesc];
[fetchRequest setSortDescriptors:self.nameSortDescriptors];
[fetchRequest setPredicate:pred];
NSError *error = nil;
NSArray *allLabels = [[self managedObjectContext] executeFetchRequest:fetchRequest
error:&error];
if(allLabels == nil) {
// FIXME: Real error management needed.
return nil;
}
return allLabels;
}
- (void)document:(NSDocument *)doc didSave:(BOOL)didSave contextInfo:(void *)contextInfo
{
return;
}
- (id)managedObjectModel
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyModel" ofType:@"mom"];
NSURL *url = [NSURL fileURLWithPath:path];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
return [model autorelease];
}
#pragma mark Control importer window
- (IBAction)importOldCashboxStuff:(id)sender
{
oldCashboxImporter = [[[ZXOldCashboxImporter alloc] initWithOwner:self] autorelease];
[oldCashboxImporter main];
id arr = [accountController arrangedObjects];
[arr makeObjectsPerformSelector:@selector(recalculateBalance:) withObject:nil];
}
#pragma mark Table View Delegate Stuff
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
// Label column
if([[tableColumn identifier] isEqual:@"label"]) {
id tx = [[transactionController arrangedObjects] objectAtIndex:row];
return [labelController popUpCellWithTransaction:tx];
}
// Amount column
if([[tableColumn identifier] isEqual:@"amount"]) {
id cell = [tableColumn dataCellForRow:row];
id tx = [[transactionController arrangedObjects] objectAtIndex:row];
NSTextAlignment ta = NSLeftTextAlignment;
if([[tx valueForKey:@"amount"] doubleValue] < 0) {
ta = NSRightTextAlignment;
}
[cell setAlignment:ta];
return cell;
}
// All other columns
if(tableColumn) return [tableColumn dataCellForRow:row];
// Separator in case tableColumn is nil
// Might be useful to separate by month
if(row == 5 && NO) {
id cell = [[NSButtonCell alloc] init];
[cell setTitle:@"AAAAAAA"];
[cell setBordered:NO];
return [cell autorelease];
}
return nil;
}
- (void)tableViewColumnDidMove:(NSNotification *)aNotification {
[transactionsView setNeedsDisplay:YES];
}
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn*)tableColumn
row:(int)row
{
if(row >= [[transactionController arrangedObjects] count]) return;
if(tableColumn == nil) return;
id tx = [[transactionController arrangedObjects] objectAtIndex:row];
id label = [tx valueForKey:@"transactionLabel"];
BOOL reconciled = NO; //[tx valueForKey:@"reconciled"];
BOOL bordered = [[label valueForKey:@"bordered"] boolValue];
NSColor *backgroundColor = nil;
NSColor *textColor = nil;
if (reconciled) {
backgroundColor = [label valueForKey:@"reconciledBackgroundColor"];
textColor = [label valueForKey:@"reconciledTextColor"];
} else {
backgroundColor = [label valueForKey:@"backgroundColor"];
textColor = [label valueForKey:@"textColor"];
}
if(!textColor) textColor = [NSColor blackColor];
if(!backgroundColor) backgroundColor = [NSColor whiteColor];
if ([cell isOvalCell]) {
[cell setValue:[NSNumber numberWithBool:YES]
forKey:@"shouldDrawOval"];
[cell setOvalColor:backgroundColor];
BOOL shouldDrawBorder = NO;
if (bordered) {
[cell setBorderColor:textColor];
shouldDrawBorder = YES;
}
[cell setValue:[NSNumber numberWithBool:shouldDrawBorder]
forKey:@"shouldDrawBorder"];
NSArray *tableColumns = [tableView tableColumns];
int curColumn = [tableColumns indexOfObject:tableColumn];
BOOL shouldDrawLeft = YES, shouldDrawRight = YES;
if (curColumn - 1 >= 0) {
id prevCell = [[tableColumns objectAtIndex:curColumn - 1] dataCell];
if([prevCell isOvalCell]) {
shouldDrawLeft = NO;
}
}
if (curColumn + 1 < [tableColumns count]) {
id nextCell = [[tableColumns objectAtIndex:curColumn + 1] dataCell];
if([nextCell isOvalCell]) {
shouldDrawRight = NO;
}
}
[cell setValue:[NSNumber numberWithBool:shouldDrawLeft]
forKey:@"shouldDrawLeftOval"];
[cell setValue:[NSNumber numberWithBool:shouldDrawRight]
forKey:@"shouldDrawRightOval"];
}
if ([cell respondsToSelector:@selector(selectedItem)] &&
[[label valueForKey:@"obsolete"] boolValue]) {
[cell setEnabled:NO];
}
}
#pragma mark Exporter stuff
- (IBAction)exportToCSV:(id)sender
{
id accountName = [accountController valueForKeyPath:@"selection.name"];
id date = [dateFormatter stringFromDate:[NSDate date]];
// We make a path, so we don't really want any slashes.
date = [date stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
id name = [NSString stringWithFormat:@"%@ %@", accountName, date];
id panel = [NSSavePanel savePanel];
[panel setRequiredFileType:@"csv"];
[panel beginSheetForDirectory:nil
file:name
modalForWindow:[self strongboxWindow]
modalDelegate:self
didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if(returnCode != NSOKButton) return;
NSMutableString *ret = [NSMutableString string];
// FIXME: Hard-coded english
[ret appendString:@"Date,Label,Description,Amount,Balance\n"];
for(id tx in [transactionController valueForKey:@"arrangedObjects"]) {
NSString *date = [dateFormatter stringFromDate:[tx valueForKey:@"date"]];
NSString *labelName = [[tx valueForKeyPath:@"transactionLabel.name"] csvExport];
NSString *description = [[tx valueForKey:@"transactionDescription"] csvExport];
if(!labelName) labelName = @"\"\"";
if(!description) description = @"\"\"";
double amount = [[tx valueForKey:@"amount"] doubleValue];
double balance = [[tx valueForKey:@"balance"] doubleValue];
[ret appendString:[NSString stringWithFormat:@"%@,%@,%@,%.2f,%.2f\n",
date, labelName, description, amount, balance]];
}
[ret writeToURL:[sheet URL]
atomically:NO
encoding:NSUTF8StringEncoding
error:NULL];
}
#pragma mark Printing stuff
- (void)printShowingPrintPanel:(BOOL)flag
{
NSPrintInfo *printInfo = [self printInfo];
NSPrintOperation *printOp;
id printView = [[ZXPrintTransactionView alloc] initWithOwner:self];
printOp = [NSPrintOperation printOperationWithView:printView
printInfo:printInfo];
[printOp setShowPanels:flag];
[self runModalPrintOperation:printOp
delegate:nil
didRunSelector:NULL
contextInfo:NULL];
}
@end