aboutsummaryrefslogtreecommitdiff
path: root/XcodePlugin/GTMXcodePreferences.m
blob: 320b7dbceb0e2d611f474c7bfc4aa3e4f30cf66b (plain)
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
//
//  GTMXcodePreferences.m
//
//  Copyright 2007-2009 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMXcodePreferences.h"
#import "GTMXcodePlugin.h"
#import "GTMDefines.h"

NSString *GTMXcodePreferencesMenuItemPrefChanged
  = @"GTMXcodePreferencesMenuItemPrefChanged";
NSString *GTMXcodeCorrectWhiteSpaceOnSave
  = @"GTMXcodeCorrectWhiteSpaceOnSave";
NSString *GTMXCodeSuppressMenuItemIcon
  = @"GTMXCodeSuppressMenuItemIcon";

@implementation GTMXcodePreferences

// Set our minimum size for the pane
- (NSSize)minModuleSize {
  return NSMakeSize(268, 80);
}

// Return our nice little icon
- (id)imageForPreferenceNamed:(id)parameter1 {
  NSBundle *bundle = [GTMXcodePlugin pluginBundle];
  NSString *path =  [bundle pathForImageResource:@"GTM"];
  NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
  [image setScalesWhenResized:YES];
  [image setSize:NSMakeSize(32, 32)];
  return image;
}

// This gets called everytime preferences are pulled up so that we can
// set up our state.
- (void)initializeFromDefaults {
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

  // Handle menuitem icon (pref is suppress, button is show)
  // NOTE: this preference is negative, but the UI works as the positive, this
  // is done so the lack of the preference (ie-the default) will turn on the
  // icons.
  NSInteger state = NSOffState;
  if (![defaults boolForKey:GTMXCodeSuppressMenuItemIcon]) {
    state = NSOnState;
  }
  [showImageOnMenuItems_ setState:state];

  state = [defaults boolForKey:GTMXcodeCorrectWhiteSpaceOnSave] ? NSOnState
                                                                : NSOffState;
  [correctWhiteSpace_ setState:state];
}

// This gets called on Apply or OK is "hasChangesPending" returns YES.
- (void)saveChanges {
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

  // Handle menuitem icon (pref is suppress, button is show)
  // NOTE: this preference is negative, but the UI works as the positive, this
  // is done so the lack of the preference (ie-the default) will turn on the
  // icons.
  BOOL newSetting = ([showImageOnMenuItems_ state] == NSOnState);
  BOOL oldSetting = ![defaults boolForKey:GTMXCodeSuppressMenuItemIcon];
  if (newSetting != oldSetting) {
    if (newSetting) {
      [defaults removeObjectForKey:GTMXCodeSuppressMenuItemIcon];
    } else {
      [defaults setBool:YES forKey:GTMXCodeSuppressMenuItemIcon];
    }
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:GTMXcodePreferencesMenuItemPrefChanged
                      object:self];
  }

  BOOL setting = ([correctWhiteSpace_ state] == NSOnState);
  [defaults setBool:setting forKey:GTMXcodeCorrectWhiteSpaceOnSave];

  // save out our settings
  [defaults synchronize];
}


// Currently we'll always return YES as it doesn't hurt at all.
- (BOOL)hasChangesPending {
  return YES;
}

+ (BOOL)showImageOnMenuItems {
  // NOTE: this preference is negative, but the UI works as the positive, this
  // is done so the lack of the preference (ie-the default) will turn on the
  // icons.
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  return ![defaults boolForKey:GTMXCodeSuppressMenuItemIcon];
}
@end