-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeniusAppDelegate.m
More file actions
174 lines (141 loc) · 5.11 KB
/
GeniusAppDelegate.m
File metadata and controls
174 lines (141 loc) · 5.11 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
/*
Genius
Copyright (C) 2003-2006 John R Chang
Copyright (C) 2007-2008 Chris Miner
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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.
http://www.gnu.org/licenses/gpl.txt
*/
#import "GeniusAppDelegate.h"
#import "GeniusHelpWindowController.h"
#import "GeniusItem.h"
#import "GeniusDocument.h"
#import "GeniusDocumentFile.h"
#import "GeniusPreferencesController.h"
//! Standard Cocoa Application delegate.
@implementation GeniusAppDelegate
//! Initializes application with factory defaults.
+ (void) initialize
{
// Register defaults
NSDictionary * defaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], GeniusPreferencesUseSoundEffectsKey,
[NSNumber numberWithBool:YES], GeniusPreferencesQuizUseFullScreenKey,
[NSNumber numberWithBool:YES], GeniusPreferencesQuizUseVisualErrorsKey,
[NSNumber numberWithInt:GeniusPreferencesQuizSimilarMatchingMode], GeniusPreferencesQuizMatchingModeKey,
[NSNumber numberWithInt:10], GeniusPreferencesQuizNumItemsKey,
[NSNumber numberWithInt:20], GeniusPreferencesQuizFixedTimeMinKey,
[NSNumber numberWithFloat:50.0], GeniusPreferencesQuizReviewLearnFloatKey,
NULL];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
//! Releases @a preferencesController and frees memory.
- (void) dealloc {
[preferencesController release];
[helpController release];
[super dealloc];
}
//! Instanciates a GeniusPreferencesController if needed and displays it.
- (IBAction) showPreferences:(id)sender
{
if (!preferencesController) {
preferencesController = [[GeniusPreferencesController alloc] init];
}
[preferencesController showWindow:self];
}
//! Opens Source Forge project website via finder.
- (IBAction) showWebSite:(id)sender
{
NSURL * url = [NSURL URLWithString:@"http://sourceforge.net/projects/genius"];
[[NSWorkspace sharedWorkspace] openURL:url];
}
//! Opens the yahoo genius talk website via finder.
- (IBAction) showSupportSite:(id)sender
{
NSURL * url = [NSURL URLWithString:@"http://groups.yahoo.com/group/genius-talk"];
[[NSWorkspace sharedWorkspace] openURL:url];
}
//! Empty method to ensure that our sound toggle menu stays active.
/*!
The state of the toggle menu is bound directly(through a binding) to the use sound effects user preference.
However without a target and action it wouldn't remain enabled.
*/
- (IBAction) toggleSoundEffects:(id)sender
{
}
//! Presents basic help window @see GeniusHelpWindowController#showWindow
- (IBAction) showHelpWindow:(id)sender
{
if (!helpController) {
helpController = [[GeniusHelpWindowController alloc] init];
}
[[helpController window] center];
[helpController showWindow:self];
}
//! Wraps call to GeniusDocument(FileFormat)::importFile:
- (IBAction)importFile:(id)sender
{
[GeniusDocument importFile:sender];
}
@end
@implementation GeniusAppDelegate(NSApplicationDelegate)
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
srandom(time(NULL));
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// OpenFiles
NSArray * openFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"OpenFiles"];
if (openFiles)
{
NSString * path;
NSEnumerator * pathEnumerator = [openFiles objectEnumerator];
while ((path = [pathEnumerator nextObject]))
[self application:NSApp openFile:path];
}
}
/*
This is a hack to suppress new window creation in the case where an empty window already exists.
*/
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
NSDocumentController * dc = [NSDocumentController sharedDocumentController];
GeniusDocument * doc = (GeniusDocument *)[dc currentDocument];
NSArray * documents = [dc documents];
if (doc && [documents count] == 1 && [[doc pairs] count] == 0 && [doc isDocumentEdited] == NO)
{
BOOL succeed = [doc readFromFile:filename ofType:@"Genius Document"];
if (!succeed)
return NO;
[doc reloadInterfaceFromModel];
return YES;
}
else
{
doc = [dc openDocumentWithContentsOfFile:filename display:YES];
return (doc != nil);
}
}
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
// Get all document paths
NSMutableArray * documentPaths = [NSMutableArray array];
NSArray * documents = [NSApp orderedDocuments];
NSEnumerator * documentEnumerator = [documents reverseObjectEnumerator];
NSDocument * document;
while ((document = [documentEnumerator nextObject]))
{
NSString * path = [document fileName];
if (path)
[documentPaths addObject:path];
}
[[NSUserDefaults standardUserDefaults] setObject:documentPaths forKey:@"OpenFiles"];
}
@end