-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.m
557 lines (457 loc) · 19.7 KB
/
AppDelegate.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
* AppDelegate.m
* BingDesktop
*
* Created by abradbury on 20/02/2013.
* Copyright 2015 abradbury. All rights reserved.
*
* Downloads the latest Bing homepage wallpaper and sets it as the user's
* desktop wallpaper.
*
* Gets the previous state of the program, i.e. the previously downloaded
* image and copyright information. Then waits for an internet connection
* before determining the URL to the latest wallpaper and downloading it.
*
* TODO: Add a random menu item that randomly selects one of the other Bing
* desktop items if the user isn't happy with the current one.
*/
#import "AppDelegate.h"
@implementation AppDelegate
/**
* Initial application configuration.
*/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
xmlLocation = @"https://www.bing.com/hpimagearchive.aspx?format=xml&idx=0&n=1&mbl=0&mkt=en-ww";
copyrightStr = [[NSMutableString alloc] init];
imgDownloadLoc = @"~/Pictures/Bing Desktop/";
imgDownloadName = @"bingImage.jpg";
wallpaperSuccessfullyDownloadedAndSet = FALSE;
imgPath = [[imgDownloadLoc stringByExpandingTildeInPath] stringByAppendingPathComponent:imgDownloadName];
// TODO: Catch NSSystemClockDidChangeNotification as well
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(resetTimers:)
name:NSWorkspaceDidWakeNotification
object:nil];
// Catch when the user changes spaces/desktops to check the image
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(setDesktopBackground:)
name:NSWorkspaceActiveSpaceDidChangeNotification
object:[NSWorkspace sharedWorkspace]];
[self getAppState]; // Gets the previous application state
[self waitForInternet:nil]; // Start once internet connection is available
}
/********************************* Scheduling *********************************/
/**
* Based on example from http://stackoverflow.com/a/9046264/1433614
*
* @param forTomrrow True if timer is for tomorrow, false for today
*/
- (void)scheduleNextTimedAction:(BOOL)forTomorrow {
NSDate* now = [NSDate date] ;
NSLog(@"Current time: %@", now);
NSDateComponents* tomorrowComponents = [NSDateComponents new] ;
if(forTomorrow) {
NSLog(@"Setting timer for tomorrow");
tomorrowComponents.day = 1 ;
} else {
NSLog(@"Setting timer for today");
tomorrowComponents.day = 0 ;
}
NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDate* tomorrow = [calendar dateByAddingComponents:tomorrowComponents toDate:now options:0] ;
NSDateComponents* tomorrowAt8AMComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:tomorrow] ;
tomorrowAt8AMComponents.hour = 7 ;
tomorrowAt8AMComponents.minute = 5 ;
NSDate* tomorrowAt8AM = [calendar dateFromComponents:tomorrowAt8AMComponents] ;
NSLog(@"Setting timer for: %@", tomorrowAt8AM);
// now create a timer to fire at the next midnight, to call
// our periodic function. NB: there's no convenience factory
// method that takes an NSDate, so we'll have to alloc/init
startProgramTimer = [[NSTimer alloc]
initWithFireDate:tomorrowAt8AM
interval:0.0 // we're not going to repeat, so...
target:self
selector:@selector(doTimedAction:)
userInfo:nil
repeats:NO];
// schedule the timer on the current run loop
[[NSRunLoop currentRunLoop] addTimer:startProgramTimer forMode:NSDefaultRunLoopMode];
// timer is retained by the run loop, so we can forget about it
[startProgramTimer release];
[tomorrowComponents release];
}
- (void)doTimedAction:(NSTimer *)timer {
NSLog(@"do action");
[self waitForInternet:nil];
[self scheduleNextTimedAction:TRUE];
}
- (void) resetTimers: (NSNotification*) note {
NSLog(@"Woken from sleep");
// Clear Timers
if ([startProgramTimer isValid]){
[startProgramTimer invalidate];
startProgramTimer = nil;
}
if ([self hasTodaysWallaperBeenDownloaded]) {
// If wallpaper downloaded, set timer for tomorrow
NSLog(@"Wallpaper has already been downloaded for today");
[self scheduleNextTimedAction:(TRUE)];
} else {
// If wallpaper not downloaded, check what time it is
// TODO: Check is time is after 8am
NSLog(@"Wallpaper has not been downloaded for today");
if ([self isAfterWallpaperDownloadTime]) {
// If now is after download time, download & set timer for tomorrow
NSLog(@"Current time is after wallpaper download time");
[self waitForInternet:nil];
[self scheduleNextTimedAction:TRUE];
} else {
// If now is before download time, set timer for later today
NSLog(@"Current time is before wallpaper download time");
[self scheduleNextTimedAction:FALSE];
}
}
}
/**
* @return True if the wallpaper was downloaded today, false otherwise
*/
-(BOOL)hasTodaysWallaperBeenDownloaded {
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate * now = [NSDate date];
NSDate * storedDate = [self dateFromString:[[NSUserDefaults standardUserDefaults] stringForKey:@"com.bingDesktop.wallpaperLastDownloaded"] :@"MM/dd/yyyy hh:mm a"];
NSLog(@"Current time: %@", now);
NSLog(@"Stored date: %@", storedDate);
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:now];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:storedDate];
return [comp1 day] == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year] == [comp2 year];
}
-(NSDate*)dateFromString:(NSString*) dateString :(NSString*) dateFormat {
NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
[dateFmt setDateFormat: dateFormat];
NSDate *dateToReturn = [dateFmt dateFromString: dateString];
[dateFmt release];
return dateToReturn;
}
-(BOOL)isAfterWallpaperDownloadTime {
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate * now = [NSDate date];
NSDateComponents *components = [calendar components: NSYearCalendarUnit|
NSMonthCalendarUnit|
NSDayCalendarUnit
fromDate:now];
[components setHour:8];
[components setMinute:5];
NSDate *reference = [calendar dateFromComponents:components];
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:now];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:reference];
return [comp1 hour] > [comp2 hour] && [comp1 minute] > [comp2 minute];
}
/***************************** Application State ******************************/
/**
* Save the current image path and copyright information. Used when launching
* the app again after at least 1 previous opening to display correct copyright
* information for current wallpaper.
*/
-(void)saveAppState {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// Store the copyright string
[defaults setObject:copyrightStr forKey:@"com.bingDesktop.copyright"];
if (![copyrightStr isEqualToString:[defaults stringForKey:@"com.bingDesktop.copyright"]]) {
NSLog(@"Failed to save copyright string.");
}
// Store the date that the last wallpaper was downloaded
if (wallpaperSuccessfullyDownloadedAndSet) {
NSString *wallpaperDownloadedDate = [self getTodaysDate];
[defaults setObject: wallpaperDownloadedDate forKey:@"com.bingDesktop.wallpaperLastDownloaded"];
wallpaperSuccessfullyDownloadedAndSet = FALSE;
if (![wallpaperDownloadedDate isEqualToString:[defaults stringForKey:@"com.bingDesktop.wallpaperLastDownloaded"]]) {
NSLog(@"Failed to save qallpaper last downloaded date.");
}
}
// Store the image path
[defaults setObject:imgPath forKey:@"com.bingDesktop.imagePath"];
if (![imgPath isEqualToString:[defaults stringForKey:@"com.bingDesktop.imagePath"]]) {
NSLog(@"Failed to save image path.");
}
}
-(NSString *)getTodaysDate {
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
return dateString;
}
/**
* Gets the previous state of the application, such as the copyright string
* and background image.
*
* @param sender desciption
*/
-(void)getAppState {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// Remove old copyright and image defaults
[defaults removeObjectForKey:@"copyright"];
[defaults removeObjectForKey:@"image"];
// Get previous copyright string
NSString *savedCopyright = [defaults stringForKey:@"com.bingDesktop.copyright"];
if (savedCopyright) {
[copyrightStr setString:savedCopyright];
[statusItem setToolTip:copyrightStr];
NSLog(@"Previous copyright string found.");
} else {
NSLog(@"No previous copyright string found.");
}
}
/*************************** Internet Availability ****************************/
/**
* Wait until there is an internet connection before continuing with the
* application and attempting to download the XML document and image. Checks
* every 5 seconds, but does not cause the UI to hang. Uses the reachability
* API from Apple's iOS work.
*/
-(IBAction)waitForInternet:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(connected:)
userInfo:nil
repeats:YES];
}
- (void)connected:(NSTimer*)t {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
if (networkStatus != NotReachable) {
[t invalidate];
NSLog(@"Internet connection available.");
[self getXML];
} else {
NSLog(@"Internet connection not available.");
}
}
/******************************** XML Parsing *********************************/
/**
* Download the XML document that indicates the path for the current day's
* image, which will be downloaded later.
*
* @param sender description
*
* @return TRUE if the XML file was downloaded, FALSE otherwise
*/
- (void)getXML {
xmlUrl = [NSURL URLWithString:xmlLocation];
// Clear copyright string
// TODO: Move to another method
// TODO: Clear image path?
[copyrightStr setString:@""];
// Asynchronously get the data
NSURLSessionDataTask *dataXmlTask = [[NSURLSession sharedSession]
dataTaskWithURL:xmlUrl completionHandler:^(NSData *dataReceived, NSURLResponse *response, NSError *error) {
urlResponse = [response retain];
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
NSLog(@"Successfully received %lu bytes of data for XML file.",
(unsigned long)[dataReceived length]);
data = dataReceived;
if (!dataReceived) {
NSLog(@"Error getting XML - no data received.");
[self setDocument:nil]; // Clear out old value
}
[self parseXmlFromURL];
}
}];
// Tasks from NSURLSession start in a suspended state. Start the task here.
[dataXmlTask resume];
}
/**
* Parse downloaded XML data to extract the URL of the current day's image.
*/
-(void)parseXmlFromURL {
NSLog(@"Parsing XML...");
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:NO];
BOOL parseResult = [xmlParser parse];
if (parseResult == NO) {
NSLog(@"...Error parsing XML.");
} else {
NSLog(@"...XML parsed successfully.");
}
// Release the parser from memory
[xmlParser release];
// Set copyright string, save app state
[statusItem setToolTip:copyrightStr];
// If image is not downloaded, then download it and set it as the desktop
// background. Else, set it as the desktop background.
if (!([self isImageDownloaded])) {
[self startDownloadingURL];
} else if (!([self isImageSetAsWallpaper])) {
[self setDesktopBackground:nil];
}
[self showNotification:nil];
}
/**
* Called when the XML parser finds a starting tag
*/
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"urlBase"])
foundImgUrl = YES;
else if([elementName isEqualToString:@"copyright"])
foundCopyright = YES;
}
/**
* Called when the XML parser finds an ending tag
*/
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"urlBase"])
foundImgUrl = NO;
else if([elementName isEqualToString:@"copyright"])
foundCopyright = NO;
}
/**
* Called when the XML parse find characters between tags
*/
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)value {
if(foundImgUrl == YES) {
imgUrlStr = [NSString stringWithFormat:@"https://www.bing.com%@_1920x1200.jpg",value];
NSArray *splitImgUrl = [imgUrlStr componentsSeparatedByString:@"/"];
NSLog(@"Found image URL: %@", imgUrlStr);
imgDownloadName = [[splitImgUrl objectAtIndex: [splitImgUrl count] -1] retain];
imgPath = [[[imgDownloadLoc stringByExpandingTildeInPath] stringByAppendingPathComponent:imgDownloadName] retain];
} else if(foundCopyright == YES) {
[copyrightStr appendString:value];
}
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"Error parsing XML: %@", [parseError localizedDescription]);
}
-(void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError {
NSLog(@"Error parsing XML: %@", [validationError localizedDescription]);
}
/******************************* Image Download *******************************/
-(void)startDownloadingURL {
NSURLSessionDownloadTask *downloadImageTask = [[NSURLSession sharedSession]
downloadTaskWithURL:[NSURL URLWithString:imgUrlStr]
completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
NSLog(@"Successfully downloaded image.");
NSData *imageData = [NSData dataWithContentsOfURL: location];
[imageData writeToFile:imgPath atomically:NO];
[self setDesktopBackground:nil];
}
}];
[downloadImageTask resume];
}
/**************************** Wallpaper Background ****************************/
- (void)setDesktopBackground:(NSNotification *)aNotification {
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSScreen *screen = [NSScreen mainScreen];
NSError *error = nil;
NSDictionary *screenOptions = [workspace desktopImageOptionsForScreen: screen];
NSURL *pathToImg = [NSURL fileURLWithPath:imgPath];
NSArray *screens = [NSScreen screens];
NSScreen *iterScreen;
// Apply wallpaper to every physical screen
// Currently not possible to set all desktops
for (iterScreen in screens) {
NSURL *curr = [workspace desktopImageURLForScreen: iterScreen];
if (![curr isEqual:pathToImg]) {
NSLog(@"Setting desktop background...");
[workspace setDesktopImageURL:pathToImg
forScreen:iterScreen
options:screenOptions
error:&error];
if (error) {
NSLog(@"Failed to set desktop background: %@", [error localizedDescription]);
}
}
}
// TODO: Or errors?
if (error) {
NSLog(@"Failed to set desktop background: %@", [error localizedDescription]);
} else {
wallpaperSuccessfullyDownloadedAndSet = TRUE;
[self saveAppState];
}
}
- (IBAction)showNotification:(id)sender{
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = @"New wallpaper downloaded";
notification.informativeText = copyrightStr;
notification.soundName = nil;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
/************************************ Menu ************************************/
/*
* Creates the menu.
*/
-(void)awakeFromNib{
// Create the NSStatusItem, add the statusMenus to it and enable highlighting
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
[statusItem setHighlightMode:YES];
// Define the main image for the statusItem
statusItemImage = [NSImage imageNamed:@"logo-black"];
if( !statusItemImage ) {
NSLog(@"ERROR: Could not load the image for 'logo-black.png'");
} else {
[statusItem setImage:statusItemImage];
}
// Define the image to use when the statusItem is highlighted
statusItemImageAlt = [NSImage imageNamed:@"logo-white"];
if( !statusItemImageAlt ) {
NSLog(@"ERROR: Could not load the image for 'logo-white.png'");
} else {
[statusItem setAlternateImage:statusItemImageAlt];
}
}
/*********************************** Other ************************************/
/**
* Checks if the image parsed from the XML has already been downloaded.
*/
-(BOOL)isImageDownloaded {
NSFileManager* fileManager = [[[NSFileManager alloc] init] autorelease];
if ([fileManager fileExistsAtPath: imgPath]){
NSLog(@"Image already exists.");
return TRUE;
} else {
NSLog(@"Image does not exist.");
return FALSE;
}
}
/**
* Checks to see if the image is the current wallpaper.
*/
-(BOOL)isImageSetAsWallpaper {
NSURL *currentWallpaperUrl = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];
NSString *currentWallpaperString = [currentWallpaperUrl absoluteString];
NSArray *currentWallpaperArray = [currentWallpaperString componentsSeparatedByString:@"/"];
NSString *currentWallpaper = [[currentWallpaperArray objectAtIndex: [currentWallpaperArray count] -1] retain];
NSLog(@"Current wallpaper: %@", currentWallpaper);
if ([currentWallpaper isEqualToString:imgDownloadName]) {
[currentWallpaper release];
NSLog(@"Image already set as wallpaper.");
return TRUE;
} else {
[currentWallpaper release];
NSLog(@"Image not set as wallpaper.");
return FALSE;
}
}
- (void)dealloc {
[statusItemImage release];
[statusItemImageAlt release];
[super dealloc];
}
- (void)setDocument:(NSXMLDocument *)doc {
if (document != doc) {
[document release];
document = [doc retain];
}
}
@end