forked from tomasf/caffeine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.m
More file actions
executable file
·233 lines (178 loc) · 6.73 KB
/
AppDelegate.m
File metadata and controls
executable file
·233 lines (178 loc) · 6.73 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
//
// AppDelegate.m
// Caffeine
//
// Created by Tomas Franzén on 2006-05-20.
// Copyright 2006 Lighthead Software. All rights reserved.
//
#import "AppDelegate.h"
#import <CoreServices/CoreServices.h>
@implementation AppDelegate
- (id)init {
[super init];
timer = [[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timer:) userInfo:nil repeats:YES] retain];
// Workaround for a bug in Snow Leopard where Caffeine would prevent the computer from going to sleep when another account was active.
userSessionIsActive = YES;
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(userSessionDidResignActive:) name:NSWorkspaceSessionDidResignActiveNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(userSessionDidBecomeActive:) name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];
return self;
}
- (void)awakeFromNib {
NSStatusItem *item = [[[NSStatusBar systemStatusBar] statusItemWithLength:30] retain];
menuView = [[LCMenuIconView alloc] initWithFrame:NSZeroRect];
[item setView:menuView];
[menuView setStatusItem:item];
[menuView setMenu:menu];
[menuView setAction:@selector(toggleActive:)];
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"SuppressLaunchMessage"]];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"SuppressLaunchMessage"])
[self showPreferences:nil];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"ActivateOnLaunch"])
[self toggleActive:nil];
}
- (void)dealloc {
[timer invalidate];
[timer release];
[menuView release];
[timeoutTimer release];
[super dealloc];
}
- (BOOL)screensaverIsRunning {
NSString *activeAppID = [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationBundleIdentifier"];
NSArray *bundleIDs = [NSArray arrayWithObjects:@"com.apple.ScreenSaver.Engine", @"com.apple.loginwindow", nil];
return activeAppID && [bundleIDs containsObject:activeAppID];
}
- (void)activateWithTimeoutDuration:(NSTimeInterval)interval {
if(timeoutTimer) [[timeoutTimer autorelease] invalidate];
timeoutTimer = nil;
if(interval > 0)
timeoutTimer = [[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timeoutReached:) userInfo:nil repeats:NO] retain];
isActive = YES;
[menuView setActive:isActive];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:interval ? interval : -1], @"duration", nil];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.lightheadsw.caffeine.activation" object:nil userInfo:info];
}
- (void)activate {
[self activateWithTimeoutDuration:0];
}
- (void)deactivate {
isActive = NO;
if(timeoutTimer) [[timeoutTimer autorelease] invalidate];
timeoutTimer = nil;
[menuView setActive:isActive];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.lightheadsw.caffeine.deactivation" object:nil userInfo:nil];
}
- (IBAction)activateWithTimeout:(id)sender {
int minutes = [(NSMenuItem*)sender tag];
int seconds = minutes*60;
if(seconds == -60) seconds = 2;
if(minutes)
[self activateWithTimeoutDuration:seconds];
else
[self activate];
}
- (void)toggleActive:(id)sender {
if(timeoutTimer) [[timeoutTimer autorelease] invalidate];
timeoutTimer = nil;
if(isActive) {
[self deactivate];
} else {
int defaultMinutesDuration = [[NSUserDefaults standardUserDefaults] integerForKey:@"DefaultDuration"];
int seconds = defaultMinutesDuration*60;
if(seconds == -60) seconds = 2;
if(defaultMinutesDuration)
[self activateWithTimeoutDuration:seconds];
else
[self activate];
}
}
- (void)timeoutReached:(NSTimer*)timer {
[self deactivate];
}
- (BOOL)isActive {
return isActive;
}
- (void)userSessionDidResignActive:(NSNotification *)note {
userSessionIsActive = NO;
}
- (void)userSessionDidBecomeActive:(NSNotification *)note {
userSessionIsActive = YES;
}
- (IBAction)showAbout:(id)sender {
[NSApp activateIgnoringOtherApps:YES];
[NSApp orderFrontStandardAboutPanel:self];
}
- (IBAction)showPreferences:(id)sender {
[NSApp activateIgnoringOtherApps:YES];
[firstTimeWindow center];
[firstTimeWindow makeKeyAndOrderFront:sender];
}
- (void)timer:(NSTimer*)timer {
if(isActive && ![self screensaverIsRunning] && userSessionIsActive)
UpdateSystemActivity(UsrActivity);
}
- (LSSharedFileListItemRef)applicationItemInList:(LSSharedFileListRef)list {
NSString *appPath = [[NSBundle mainBundle] bundlePath];
NSArray *items = (id)LSSharedFileListCopySnapshot(list, NULL);
for(id item in items) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef URL = NULL;
if(LSSharedFileListItemResolve(itemRef, 0, &URL, NULL)) continue;
BOOL matches = [[(NSURL*)URL path] isEqual:appPath];
CFRelease(URL);
if(matches)
return itemRef;
}
CFRelease(items);
return NULL;
}
- (BOOL)startsAtLogin {
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
LSSharedFileListItemRef item = [self applicationItemInList:loginItems];
BOOL starts = (item != NULL);
if(item) CFRelease(item);
CFRelease(loginItems);
return starts;
}
- (void)setStartsAtLogin:(BOOL)start {
if(start == [self startsAtLogin]) return;
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if(start) {
NSString *appPath = [[NSBundle mainBundle] bundlePath];
CFURLRef appURL = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)appPath, kCFURLPOSIXPathStyle, YES);
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, NULL, NULL, appURL, NULL, NULL);
CFRelease(appURL);
}else{
LSSharedFileListItemRef item = [self applicationItemInList:loginItems];
if(item) {
LSSharedFileListItemRemove(loginItems, item);
CFRelease(item);
}
}
CFRelease(loginItems);
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
[self showPreferences:nil];
return NO;
}
- (void)menuNeedsUpdate:(NSMenu *)m {
if(isActive) {
[infoMenuItem setHidden:NO];
[infoSeparatorItem setHidden:NO];
if(timeoutTimer) {
NSTimeInterval left = [[timeoutTimer fireDate] timeIntervalSinceNow];
if(left >= 3600)
[infoMenuItem setTitle:[NSString stringWithFormat:@"%02d:%02d left", (int)(left/3600), (int)(((int)left%3600)/60)]];
else if(left >= 60)
[infoMenuItem setTitle:[NSString stringWithFormat:@"%d minutes left", (int)(left/60)]];
else
[infoMenuItem setTitle:[NSString stringWithFormat:@"%d seconds left", (int)left]];
}else{
[infoMenuItem setTitle:@"Caffeine is active"];
}
}else{
[infoMenuItem setHidden:YES];
[infoSeparatorItem setHidden:YES];
}
}
@end