dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STPreferenceController.m 225 2011-08-13 21:16:20Z stuart $
  3.  *
  4.  *  SafariTabs
  5.  *  http://stuconnolly.com/projects/safaritabs/
  6.  *
  7.  *  Copyright (c) 2010 Stuart Connolly. All rights reserved.
  8.  *
  9.  *  This program is free software: you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation, either version 3 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23. #import "STPreferenceController.h"
  24. #import "STSafariPlugin.h"
  25. #import "STUpdaterDialogs.h"
  26. #import "STSafariPluginHandler.h"
  27.  
  28. #import "STUpdater.h"
  29. #import "STUtilities.h"
  30. #import "STConstants.h"
  31.  
  32. @interface STPreferenceController ()
  33.  
  34. - (void)_toggleCheckForUpdatesButton;
  35.  
  36. @end
  37.  
  38. @implementation STPreferenceController
  39.  
  40. /*!
  41.  * This method is called automatically and is part of the NSNibAwaking protocol. It basically prepares the shared
  42.  * instance of this class after it is loaded from a NIB archive.
  43.  */
  44. - (void)awakeFromNib
  45. {
  46.     _defaults = [NSUserDefaults standardUserDefaults];    
  47.    
  48.     [self toggleTabRestoreOptions:nil];
  49.    
  50.     [authorTextField setStringValue:[NSString stringWithFormat:STLocalizedString(@"Copyright"), STPluginVersion(), STBundleVersion(), [[NSDate date] descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil]]];
  51. }
  52.  
  53. /*!
  54.  * Returns the preference pane image associated with the supplied name.
  55.  *
  56.  * @param name The name of the preference associated with the required image
  57.  *
  58.  * @return The toolbar image to be used in the preference window
  59.  */
  60. - (NSImage *)imageForPreferenceNamed:(NSString *)name
  61. {
  62.     NSImage *image = [NSImage imageNamed:@"SafariTabs"];
  63.    
  64.     if (!image) {
  65.         NSString *imagePath = [STPluginBundle() pathForImageResource:name];
  66.        
  67.         if (!imagePath) {
  68.             NSLog(@"SafariTabs Error: image path for %@ is nil", name);
  69.            
  70.             return nil;
  71.         }
  72.        
  73.         image = [[[NSImage alloc] initByReferencingFile:imagePath] autorelease];
  74.        
  75.         if (!image) {
  76.             NSLog(@"SafariTabs Error: image for %@ is nil", name);
  77.            
  78.             return nil;
  79.         }
  80.        
  81.         [image setName:name];
  82.     }
  83.    
  84.     return image;
  85. }
  86.  
  87. /*!
  88.  * Returns SafariTabs' preferences Nib name.
  89.  *
  90.  * @return SafariTabs' preferences Nib name
  91.  */
  92. - (NSString *)preferencesNibName
  93. {
  94.     return STPreferencesNibName;
  95. }
  96.  
  97. /*!
  98.  * Indicates whether the preference view is resizable
  99.  *
  100.  * @return A boolean value
  101.  */
  102. - (BOOL)isResizable
  103. {
  104.     return NO;
  105. }
  106.  
  107. #pragma mark -
  108. #pragma mark IB Action Methods
  109.  
  110. /*!
  111.  * Toggles (enable/disables) the tab restore option.
  112.  *
  113.  * @param sender The object calling the method
  114.  */
  115. - (IBAction)toggleTabRestoreOptions:(id)sender
  116. {
  117.     [launchActionMatrix setEnabled:![_defaults integerForKey:STTabRestoreQuitAction]];
  118.     [tabWindowOptionsMatrix setEnabled:![_defaults integerForKey:STTabRestoreQuitAction]];
  119. }
  120.  
  121. /*!
  122.  * Checks for available updates.
  123.  *
  124.  * @param sender The object calling the method
  125.  */
  126. - (IBAction)checkForUpdates:(id)sender
  127. {
  128.     [updateCheckProgressIndicator startAnimation:self];
  129.     [self _toggleCheckForUpdatesButton];
  130.    
  131.     [[[STSafariPluginHandler sharedInstance] safariTabsUpdater] checkForUpdatesUsingDelegate:self inBackground:NO];
  132. }
  133.  
  134. #pragma mark -
  135. #pragma mark Updater Delegate Methods
  136.  
  137. - (void)updater:(STUpdater *)updater newVersionAvailable:(NSString *)version
  138. {
  139.     [updateCheckProgressIndicator stopAnimation:self];
  140.     [self _toggleCheckForUpdatesButton];
  141.    
  142.     if ([STGetNewVersionAvailableDialog(version, NO) runModal] == NSAlertDefaultReturn) {
  143.         [[[STSafariPluginHandler sharedInstance] safariTabsUpdater] downloadUpdate];
  144.     }
  145. }
  146.  
  147. - (void)updaterNoNewVersionAvailable:(STUpdater *)updater
  148. {    
  149.     [updateCheckProgressIndicator stopAnimation:self];
  150.     [self _toggleCheckForUpdatesButton];
  151.    
  152.     STDisplayNoNewVersionAvailableDialog();
  153. }
  154.  
  155. - (void)updaterCheckForUpdateError:(STUpdater *)updater
  156. {
  157.     [updateCheckProgressIndicator stopAnimation:self];
  158.     [self _toggleCheckForUpdatesButton];
  159.    
  160.     STDisplayCheckForUpdateErrorDialog();
  161. }
  162.  
  163. - (void)updaterDownloadUpdateError:(STUpdater *)updater
  164. {
  165.     [updateCheckProgressIndicator stopAnimation:self];
  166.     [self _toggleCheckForUpdatesButton];
  167.  
  168.     STDisplayDownloadUpdateErrorDialog();
  169. }
  170.  
  171. - (void)updaterDownloadComplete:(STUpdater *)updater
  172. {
  173.     STDisplayDownloadCompleteDialog();
  174. }
  175.  
  176. /*!
  177.  * Toggles (enables/disables) the title and state of the 'Check For Updates' button.
  178.  */
  179. - (void)_toggleCheckForUpdatesButton
  180. {    
  181.     [checkForUpdatesButton setTitle:([[checkForUpdatesButton title] isEqualToString:STLocalizedString(@"Check Now")]) ? STLocalizedString(@"Checking...") : STLocalizedString(@"Check Now")];
  182.     [checkForUpdatesButton setEnabled:![checkForUpdatesButton isEnabled]];
  183. }
  184.  
  185. @end
  186.