PageRenderTime 57ms CodeModel.GetById 13ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/XcodePlugin/GTMXcodePlugin.m

http://macfuse.googlecode.com/
Objective C | 212 lines | 155 code | 27 blank | 30 comment | 22 complexity | ed49ee4d5036cf896ff69d688098fc40 MD5 | raw file
  1//
  2//  GTMXcodePlugin.m
  3//
  4//  Copyright 2007-2009 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMXcodePlugin.h"
 20#import "GTMXcodeMenuItem.h"
 21#import "PBXPreferencesModule.h"
 22#import "GTMXcodePreferences.h"
 23#import "GTMDefines.h"
 24
 25@interface GTMXcodePlugin (PrivateMethods)
 26
 27// Called when the preference panel has been loaded so we can set up our
 28// preference pane.
 29- (void)preferencesPanelDidLoadNotification:(NSNotification *)notification;
 30
 31// Called when we start tracking the menu
 32- (void)begunTracking:(NSNotification *)notification;
 33
 34// Called whenever the pref for showing the menuitem icon changes
 35- (void)updateMenuIcons:(NSNotification *)notification;
 36
 37// the image to use for our menu items
 38- (NSImage *)imageForMenuItems;
 39@end
 40
 41// Our dictionary of menu items to add
 42static NSMutableDictionary *gRegisteredMenuItems = nil;
 43
 44static NSMutableArray *gRegisteredClasses = nil;
 45
 46@implementation GTMXcodePlugin
 47+ (void)registerMenuItem:(id)item {
 48  @synchronized([self class]) {
 49    static int gTag = 0xDADE;
 50    if (!gRegisteredMenuItems) {
 51      gRegisteredMenuItems = [[NSMutableDictionary alloc] init];
 52    }
 53    if (item) {
 54      [gRegisteredMenuItems setObject:item
 55                               forKey:[NSNumber numberWithInt:gTag++]];
 56    }
 57  }
 58}
 59
 60+ (void)registerSwizzleClass:(Class)cls {
 61  @synchronized([self class]) {
 62    if (!gRegisteredClasses) {
 63      gRegisteredClasses = [[NSMutableArray alloc] init];
 64    }
 65    if (cls) {
 66      [gRegisteredClasses addObject:cls];
 67    }
 68  }
 69}
 70
 71- (id)init {
 72  self = [super init];
 73  if (!self) return nil;
 74
 75  NSBundle *bundle = [NSBundle bundleForClass:[self class]];
 76  NSString *version = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"];
 77  NSLog(@"GTMXcodePlugin loaded (%@)", version);
 78
 79  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
 80
 81  [nc addObserver:self
 82         selector:@selector(preferencesPanelDidLoadNotification:)
 83             name:PBXPreferencesPanelDidLoadNotification
 84           object:nil];
 85
 86  [nc addObserver:self
 87         selector:@selector(begunTracking:)
 88             name:NSMenuDidBeginTrackingNotification
 89           object:[NSApp mainMenu]];
 90
 91  [nc addObserver:self
 92         selector:@selector(updateMenuIcons:)
 93             name:GTMXcodePreferencesMenuItemPrefChanged
 94           object:nil];
 95
 96  NSEnumerator *enumerator = [gRegisteredClasses objectEnumerator];
 97  Class cls;
 98  while ((cls = [enumerator nextObject])) {
 99    [[[cls alloc] init] autorelease];
100  }
101  return self;
102}
103
104- (void)dealloc {
105  [[NSNotificationCenter defaultCenter] removeObserver:self];
106  [gRegisteredMenuItems removeAllObjects];
107  [gRegisteredMenuItems release];
108  [super dealloc];
109}
110
111
112- (void)preferencesPanelDidLoadNotification:(NSNotification *)notification {
113  GTMXcodePreferences *prefs = [[[GTMXcodePreferences alloc] init] autorelease];
114  [[PBXPreferencesModule sharedPreferences] addPreferenceNamed:@"Google"
115                                                         owner:prefs];
116}
117
118- (NSImage *)imageForMenuItems {
119  NSBundle *pluginBundle = [GTMXcodePlugin pluginBundle];
120  NSString *path = [pluginBundle pathForResource:@"GTM"
121                                          ofType:@"icns"];
122  NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
123  [image setScalesWhenResized:YES];
124  [image setSize:NSMakeSize(16, 16)];
125  return image;
126}
127
128- (void)begunTracking:(NSNotification *)notification {
129  NSImage *image = nil;
130  if ([GTMXcodePreferences showImageOnMenuItems]) {
131    image = [self imageForMenuItems];
132  }
133  NSArray *sortedKeys
134    = [gRegisteredMenuItems keysSortedByValueUsingSelector:@selector(compareDepth:)];
135  NSEnumerator *keyEnum = [sortedKeys objectEnumerator];
136  NSNumber *key;
137  while ((key = [keyEnum nextObject])) {
138    id<GTMXcodeMenuItemProtocol> item = [gRegisteredMenuItems objectForKey:key];
139    NSInteger insertionIndex = [item insertionIndex];
140    NSMenu *insertionMenu = [item insertionMenu];
141    NSInteger itemCount = [insertionMenu numberOfItems];
142    if (insertionIndex > itemCount) {
143      insertionIndex = itemCount;
144    }
145    NSString *itemTitle = [item title];
146    if ([itemTitle isEqualToString:@"-"]) {
147      [insertionMenu insertItem:[NSMenuItem separatorItem]
148                        atIndex:insertionIndex];
149    } else {
150      NSMenuItem *menuItem 
151        = [insertionMenu insertItemWithTitle:[item title]
152                                      action:[item actionSelector]
153                               keyEquivalent:[item keyEquivalent]
154                                     atIndex:insertionIndex];
155      if (image && [item allowGDTMenuIcon]) {
156        [menuItem setImage:image];
157      }
158      [menuItem setTarget:item];
159      [menuItem setTag:[key intValue]];
160      [item wasInserted:menuItem];
161    }
162  }
163
164  // Now that we are installed, unregister us from the notification.
165  NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
166  [center removeObserver:self 
167                    name:NSMenuDidBeginTrackingNotification 
168                  object:[NSApp mainMenu]];
169}
170
171- (void)updateMenuIcons:(NSNotification *)notification {
172  // we use our own notification since the normal defaults one doesn't let you
173  // tell which changed.
174  NSImage *image = nil;
175  if ([GTMXcodePreferences showImageOnMenuItems]) {
176    image = [self imageForMenuItems];
177  }
178  NSEnumerator *keyEnum = [gRegisteredMenuItems keyEnumerator];
179  NSNumber *key;
180  while ((key = [keyEnum nextObject])) {
181    id<GTMXcodeMenuItemProtocol> item = [gRegisteredMenuItems objectForKey:key];
182    NSMenu *insertionMenu = [item insertionMenu];
183    NSMenuItem *menuItem = [insertionMenu itemWithTag:[key intValue]];
184    // play it safe, make sure it's the right target
185    if (menuItem && (item == [menuItem target])) {
186      // if the user wants images and this item should get it, then set the
187      // image.  if either wasn't true, just set it to nil to clear the
188      // menu.
189      if (image && [item allowGDTMenuIcon]) {
190        [menuItem setImage:image];
191      } else {
192        [menuItem setImage:nil];
193      }
194    }
195  }
196}
197
198+ (NSBundle*)pluginBundle {
199  return [NSBundle bundleForClass:self];
200}
201
202+ (float)xCodeVersion {
203  NSBundle *bundle = [NSBundle mainBundle];
204  id object = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
205  float value = [object floatValue];
206  if (!(value > 0.0)) {
207    NSLog(@"Unable to get Xcode Version");
208  }
209  return value;
210}
211
212@end