-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.m
299 lines (229 loc) · 8.88 KB
/
AppController.m
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
#import "AppController.h"
#import "MailComm.h"
#import "PreferenceController.h"
@implementation AppController
- (id) init
{
self = [super init];
mailComm = [[MailComm alloc] init];
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleIntervalChangeNotification:)
name:@"JKBTimerIntervalChanged"
object:nil];
[nc addObserver:self
selector:@selector(handleCheckInboxOrAllChangeNotification:)
name:@"JKBCheckInboxOrAllChanged"
object:nil];
[nc addObserver:self
selector:@selector(handleOpenOnLoginNotification:)
name:@"JKBCheckInboxOrAllChanged"
object:nil];
// Initialize the preference controller
if (!preferenceController) {
preferenceController = [[PreferenceController alloc] init];
}
return self;
}
// This method is called before any other method. It is used to
// initialize the default values for user preferences.
+ (void) initialize
{
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject: [NSNumber numberWithInt: 10] forKey: JKBMessageCheckInterval];
/* For the user preferences, the integer 1 will represent the preference to
* only count unread messages in the inbox. The integer 2 will represent the user's
* desire to count unread messages in all mailboxes. The default will be to count
* the unread messages in all mailboxes (not just the inbox)
*/
[defaultValues setObject: [NSNumber numberWithInt: 2] forKey: JKBCountInboxOrAll];
[defaultValues setObject: [NSNumber numberWithBool: NO] forKey: JKBOpenOnLogin];
[defaultValues setObject: [NSNumber numberWithBool: NO] forKey: JKBLaunchMailOnCount];
// Register the default preferences for this app
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
}
// Creates the statusbar menu
- (void)activateMenu
{
NSStatusBar *statusBar = [NSStatusBar systemStatusBar];
mailThingMenu = [statusBar statusItemWithLength: NSVariableStatusItemLength];
[mailThingMenu retain];
// Set the initial title for the menu. This is soon changed
// as we do our initial count of unread messages right after
// this method ends.
[mailThingMenu setTitle: @"Mm"];
[mailThingMenu setHighlightMode: YES];
[mailThingMenu setMenu: appMenu];
// [self updateUnreadMessages];
}
-(void) awakeFromNib
{
[self activateMenu];
NSNumber *defaultTimerInterval;
// Retrieve the default timer interval from the user's preferences
defaultTimerInterval = [[NSUserDefaults standardUserDefaults] objectForKey: JKBMessageCheckInterval];
// Set up the initial mail count timer
[self setupTimerWithInterval: [defaultTimerInterval intValue]];
}
-(void)updateUnreadMessages
{
int unreadCount;
// Check to see if we should count unread messages in only the inbox
// or in all mailboxes
int inboxOrAll = [preferenceController countInboxOrAll];
// Check to see if we should launch Mail if it is closed when we perform the count
BOOL launchMailOnCount = [preferenceController launchMailOnCount];
if (launchMailOnCount == YES)
[mailComm launchMail];
if (inboxOrAll == 1)
{
unreadCount = [mailComm countUnreadMessagesInInbox];
}
else if (inboxOrAll == 2)
{
unreadCount = [mailComm countUnreadMessagesInAllMailboxes];
}
[self updateMenuUnreadCount: unreadCount];
}
-(void)updateMenuUnreadCount: (int) count
{
NSString *titleString;
if (count == -1)
titleString = @"Mm (?)";
else if (count > 0)
titleString = [NSString stringWithFormat: @"Mm (%i)", count];
else
titleString = @"Mm";
[mailThingMenu setTitle: titleString];
}
-(void) setupTimerWithInterval: (int)interval
{
// Invalidate and release the previous timer.
[mailboxCheckTimer invalidate];
// We manually release the timer because invalidation does not guarantee
// that the timer will be immediately released from the run loop
[mailboxCheckTimer release];
mailboxCheckTimer = [NSTimer scheduledTimerWithTimeInterval: interval
target: self
selector: @selector(updateUnreadMessages)
userInfo: nil
repeats: YES];
}
/* Notification handlers */
- (void)handleIntervalChangeNotification: (NSNotification *) notification
{
PreferenceController *sender = [notification object];
int newInterval = [sender checkInterval];
[self setupTimerWithInterval: newInterval];
}
- (void)handleOpenOnLoginNotification: (NSNotification *) notification
{
PreferenceController *sender = [notification object];
BOOL newOpenOnLoginPreference = [sender openOnLogin];
if (newOpenOnLoginPreference == YES) {
[self addToLoginItems];
} else if (newOpenOnLoginPreference == NO) {
[self removeFromLoginItems];
}
}
- (IBAction)showPreferencePanel:(id)sender
{
if (!preferenceController) {
preferenceController = [[PreferenceController alloc] init];
}
[preferenceController showWindow:self];
}
- (IBAction)composeMessage:(id)sender
{
[mailComm composeMessage];
}
- (IBAction)openInbox:(id)sender
{
[mailComm openInbox];
}
- (IBAction)checkForNewMessages:(id)sender
{
[mailComm checkForNewMessages];
}
/*
This method receives an array of dicionaries and looks in each dictionary to
determine whether it contains a path to our app. If a path to our app is found,
then the matching dictionary is returned.
*/
- (NSDictionary *)pathToAppIsIn: (NSArray *) loginItems
{
NSString *pathToApp = [[NSBundle mainBundle] bundlePath];
NSDictionary *currentLoginItem;
NSEnumerator *enumerator = [loginItems objectEnumerator];
while (currentLoginItem = [enumerator nextObject]) {
// This creates a fully qualified path from a string
NSString *currentItem = [[currentLoginItem objectForKey:@"Path"] stringByStandardizingPath];
// If the path to our app and the path in the "Path" object are equal, then we've found our app
if([currentItem isEqualToString: pathToApp])
return currentLoginItem;
}
return nil;
}
- (void)addToLoginItems
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *pathToApp = [[NSBundle mainBundle] bundlePath];
NSDictionary *appDictionary;
NSMutableDictionary *loginwindowDictionary = [[userDefaults persistentDomainForName: @"loginwindow"] mutableCopy];
NSMutableArray *currentLoginItems;
if (loginwindowDictionary == nil)
// If "loginwindow" does not exist, we will create it
loginwindowDictionary = [NSMutableDictionary dictionaryWithCapacity: 1];
if(!(currentLoginItems = [[loginwindowDictionary objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy]))
// If "AutoLaunchedApplicationDictionary does not exist, we will create it
currentLoginItems = [NSMutableArray arrayWithCapacity: 1];
// Check to see if our app is already in login items. If it is, remove it
if ((appDictionary = [self pathToAppIsIn: currentLoginItems]) != nil) {
[currentLoginItems removeObject: appDictionary];
}
// Create new login item for our app
appDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithBool: YES], @"Hide", pathToApp, @"Path", nil];
// Add our newly created login item dictionary to the array of login items
[currentLoginItems addObject: appDictionary];
// Add our array to the "loginwindow" dictionary
[loginwindowDictionary setObject: currentLoginItems forKey: @"AutoLaunchedApplicationDictionary"];
// Remove the current "loginwindow" dicionary and replace it with our own
[userDefaults removePersistentDomainForName: @"loginwindow"];
[userDefaults setPersistentDomain: loginwindowDictionary forName: @"loginwindow"];
[userDefaults synchronize];
}
- (void)removeFromLoginItems
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *pathToApp = [[NSBundle mainBundle] bundlePath];
NSDictionary *appDictionary;
NSMutableDictionary *loginwindowDictionary = [[userDefaults persistentDomainForName: @"loginwindow"] mutableCopy];
NSMutableArray *currentLoginItems;
// If we don't have a "loginwindow" Persistent Domain, then our app
// is surely not in the login items for the user
if (!loginwindowDictionary)
return;
// Same as previous condition
if(!(currentLoginItems = [[loginwindowDictionary objectForKey: @"AutoLaunchedApplicationDictionary"] mutableCopy]))
return;
if ((appDictionary = [self pathToAppIsIn: currentLoginItems]) != nil) {
[currentLoginItems removeObject: appDictionary];
}
// As with the "addtoLoginItems" method, we are replacing the currently existing
// "loginwindow" persistent domain with the one that we have just created
[loginwindowDictionary setObject: currentLoginItems forKey: @"AutoLaunchedApplicationDictionary"];
[userDefaults removePersistentDomainForName: @"loginwindow"];
[userDefaults setPersistentDomain: loginwindowDictionary forName: @"loginwindow"];
[userDefaults synchronize];
}
- (void) dealloc
{
[mailComm release];
[preferenceController release];
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
[super dealloc];
}
@end