PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/WidgetRunnerAppDelegate.m

http://widgetrunner.googlecode.com/
Objective C | 144 lines | 77 code | 35 blank | 32 comment | 7 complexity | 2d8a7c7194a020089b8ec626033fbd94 MD5 | raw file
  1. /* Copyright (c) 2010 Samuel R Madden (madden@csail.mit.edu)
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated documentation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. THE SOFTWARE.<
  17. */
  18. //
  19. // WidgetRunnerAppDelegate.m
  20. // WidgetRunner
  21. //
  22. #import "WidgetRunnerAppDelegate.h"
  23. @implementation WidgetRunnerAppDelegate
  24. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  25. windows = [[NSMutableArray alloc] init];
  26. preferences = [NSUserDefaults standardUserDefaults];
  27. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  28. [NSNumber numberWithInt:1],@"lastWidget",
  29. nil ]; // terminate the list
  30. [preferences registerDefaults:dict];
  31. [self loadPrefs];
  32. }
  33. -(void)loadPrefs {
  34. NSArray *s = [preferences arrayForKey:@"windows"];
  35. if (s != nil) {
  36. for (NSString *w in s) {
  37. NSArray *settings = [preferences arrayForKey:w];
  38. CustomWindow *win = [[CustomWindow alloc] initWithFile:[settings objectAtIndex:0] settings:[settings objectAtIndex:1] widgetid:w];
  39. win->parent = self;
  40. [win setLevel:[(NSNumber *)[settings objectAtIndex:2] intValue]];
  41. [win setIsVisible:YES];
  42. int x = [(NSNumber *)[settings objectAtIndex:3] intValue];
  43. int y = [(NSNumber *)[settings objectAtIndex:4] intValue];
  44. [win setFrameOrigin:NSMakePoint(x,y)];
  45. NSImage *myImage = [[NSImage alloc ]initWithContentsOfFile:[settings objectAtIndex:0]];
  46. [NSApp setApplicationIconImage: myImage];
  47. [windows addObject:win];
  48. }
  49. }
  50. }
  51. -(void)savePrefs {
  52. NSMutableArray *s = [[NSMutableArray alloc] init];
  53. for (CustomWindow *w in windows) {
  54. NSString *key = [w getKey];
  55. NSString *file = [w getFile];
  56. int level = [w level];
  57. NSRect r = [w frame];
  58. int xpos = r.origin.x;
  59. int ypos = r.origin.y;
  60. NSString *settings = [w getSettingsString];
  61. [s addObject:key];
  62. [preferences setObject:[NSArray arrayWithObjects:file,settings,[NSNumber numberWithInt:level], [NSNumber numberWithInt:xpos], [NSNumber numberWithInt: ypos], nil] forKey:key];
  63. }
  64. [preferences setObject:s forKey:@"windows"];
  65. }
  66. - (IBAction)createWidget: (id)sender {
  67. NSOpenPanel* openDlg = [NSOpenPanel openPanel];
  68. // Enable the selection of files in the dialog.
  69. [openDlg setCanChooseFiles:YES];
  70. [openDlg setAllowsOtherFileTypes:NO];
  71. [openDlg setAllowedFileTypes:[NSArray arrayWithObject:@"wdgt"]];
  72. // Display the dialog. If the OK button was pressed,
  73. // process the files.
  74. if ( [openDlg runModalForDirectory:nil file:nil types:[NSArray arrayWithObject:@"wdgt"]] == NSOKButton )
  75. {
  76. // Get an array containing the full filenames of all
  77. // files and directories selected.
  78. NSArray* files = [openDlg filenames];
  79. // Loop through all the files and process them.
  80. for( int i = 0; i < [files count]; i++ )
  81. {
  82. NSString* fileName = [files objectAtIndex:i];
  83. // Do something with the filename.
  84. // NSLog(fileName);
  85. int widgetNo = [preferences integerForKey:@"lastWidget"];
  86. NSString *id = [NSString stringWithFormat:@"%@-%d",fileName,widgetNo];
  87. widgetNo++;
  88. [preferences setInteger:widgetNo forKey:@"lastWidget"];
  89. CustomWindow *w = [[CustomWindow alloc] initWithFile:fileName settings:nil widgetid:id];
  90. w->parent = self;
  91. [w setIsVisible:YES];
  92. NSImage *myImage = [[NSImage alloc ]initWithContentsOfFile:fileName];
  93. [NSApp setApplicationIconImage: myImage];
  94. [windows addObject:w];
  95. }
  96. }
  97. }
  98. - (void) removeWidget:(CustomWindow *)widget {
  99. [windows removeObject:widget];
  100. }
  101. - (void) applicationWillTerminate: (NSNotification *)note
  102. {
  103. [self savePrefs];
  104. [preferences synchronize];
  105. }
  106. @end