Commits

Carlos Sola-Llonch committed 702a953

Fixed bug where app would hang when searching for the Stickies app. Converted most of the project to ARC.

Comments (0)

Files changed (8)

StickiesAppMonitor.h

+//
+//  StickiesAppMonitorThread.h
+//  StickiesManager
+//
+//  Created by Carlos Sola-Llonch on 3/29/10.
+//  Copyright 2010. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+@interface StickiesAppMonitor : NSObject
+
++ (StickiesAppMonitor *)instance;
+
+- (void)startMonitoring;
+- (void)stop;
+
+@end

StickiesAppMonitor.m

+//
+//  StickiesAppMonitorThread.m
+//  StickiesManager
+//
+//  Created by Carlos Sola-Llonch on 3/29/10.
+//  Copyright 2010. All rights reserved.
+//
+
+#import "StickiesAppMonitor.h"
+
+const NSString *kStickiesNotRunningNotification = @"NotRunning";
+const NSString *kStickiesRunningNotification = @"Running";
+
+static StickiesAppMonitor *gInstance = nil;
+
+@implementation StickiesAppMonitor {
+    NSTimer *_monitor;
+}
+
++ (StickiesAppMonitor *)instance {
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        gInstance = [[StickiesAppMonitor alloc] init];
+    });
+    return gInstance;
+}
+
+- (void)dealloc {
+	[_monitor invalidate];
+}
+
+- (void)startMonitoring {
+    _monitor = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
+    [[NSRunLoop currentRunLoop] addTimer:_monitor forMode:NSDefaultRunLoopMode];
+}
+
+- (void)timerFired:(NSTimer *)timer {
+	NSNotification *notification;
+	if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.Stickies"] count] == 0)
+		notification = [NSNotification notificationWithName:(NSString *)kStickiesNotRunningNotification object:nil];
+	else
+		notification = [NSNotification notificationWithName:(NSString *)kStickiesRunningNotification object:nil];
+
+    [[NSNotificationCenter defaultCenter] postNotification:notification];
+}
+
+- (void)stop {
+	[_monitor invalidate];
+    _monitor = nil;
+}
+
+@end

StickiesManager-Info.plist

 	<string>${PRODUCT_NAME}</string>
 	<key>CFBundlePackageType</key>
 	<string>APPL</string>
+	<key>CFBundleShortVersionString</key>
+	<string>1.1</string>
 	<key>CFBundleSignature</key>
 	<string>????</string>
-	<key>CFBundleShortVersionString</key>
-	<string>1.0</string>
+	<key>CFBundleVersion</key>
+	<string>1.1</string>
 	<key>LSMinimumSystemVersion</key>
 	<string>${MACOSX_DEPLOYMENT_TARGET}</string>
-	<key>CFBundleVersion</key>
-	<string>1</string>
 	<key>NSMainNibFile</key>
 	<string>MainMenu</string>
 	<key>NSPrincipalClass</key>

StickiesManager.xcodeproj/project.pbxproj

 		256AC3DA0F4B6AC300CF3369 /* StickiesManagerAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 256AC3D90F4B6AC300CF3369 /* StickiesManagerAppDelegate.m */; };
 		59A4AD2D115DAF48002A310A /* StickiesManagerWindowDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4AD2C115DAF48002A310A /* StickiesManagerWindowDelegate.m */; };
 		59A4AD4D115DB597002A310A /* Stickies.icns in Resources */ = {isa = PBXBuildFile; fileRef = 59A4AD4C115DB597002A310A /* Stickies.icns */; };
-		59A4AD50115DB6C6002A310A /* AppleScriptHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4AD4F115DB6C6002A310A /* AppleScriptHelper.m */; };
+		59A4AD50115DB6C6002A310A /* AppleScriptHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4AD4F115DB6C6002A310A /* AppleScriptHelper.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
 		59A4AD58115DBB09002A310A /* StickyHandlers.scpt in Copy AppleScripts */ = {isa = PBXBuildFile; fileRef = 59A4AD4B115DB54A002A310A /* StickyHandlers.scpt */; };
-		59A4AD9D115DC89B002A310A /* NSAppleEventDescriptor+AEDescUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4AD9C115DC89B002A310A /* NSAppleEventDescriptor+AEDescUtils.m */; };
+		59A4AD9D115DC89B002A310A /* NSAppleEventDescriptor+AEDescUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4AD9C115DC89B002A310A /* NSAppleEventDescriptor+AEDescUtils.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
 		59A4AE46115F124A002A310A /* StickyWindowItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4AE45115F124A002A310A /* StickyWindowItem.m */; };
 		59A4B12611615E85002A310A /* mgr.icns in Resources */ = {isa = PBXBuildFile; fileRef = 59A4B12511615E85002A310A /* mgr.icns */; };
-		59A4B12F11615F10002A310A /* StickiesAppMonitorThread.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4B12E11615F10002A310A /* StickiesAppMonitorThread.m */; };
+		59A4B12F11615F10002A310A /* StickiesAppMonitor.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A4B12E11615F10002A310A /* StickiesAppMonitor.m */; };
 		8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
 		8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
 		8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
 		59A4AE44115F124A002A310A /* StickyWindowItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StickyWindowItem.h; sourceTree = "<group>"; };
 		59A4AE45115F124A002A310A /* StickyWindowItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StickyWindowItem.m; sourceTree = "<group>"; };
 		59A4B12511615E85002A310A /* mgr.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = mgr.icns; sourceTree = "<group>"; };
-		59A4B12D11615F10002A310A /* StickiesAppMonitorThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StickiesAppMonitorThread.h; sourceTree = "<group>"; };
-		59A4B12E11615F10002A310A /* StickiesAppMonitorThread.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StickiesAppMonitorThread.m; sourceTree = "<group>"; };
+		59A4B12D11615F10002A310A /* StickiesAppMonitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StickiesAppMonitor.h; sourceTree = "<group>"; };
+		59A4B12E11615F10002A310A /* StickiesAppMonitor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StickiesAppMonitor.m; sourceTree = "<group>"; };
 		8D1107310486CEB800E47090 /* StickiesManager-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "StickiesManager-Info.plist"; sourceTree = "<group>"; };
 		8D1107320486CEB800E47090 /* StickiesManager.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StickiesManager.app; sourceTree = BUILT_PRODUCTS_DIR; };
 /* End PBXFileReference section */
 				59A4AD2C115DAF48002A310A /* StickiesManagerWindowDelegate.m */,
 				59A4AE44115F124A002A310A /* StickyWindowItem.h */,
 				59A4AE45115F124A002A310A /* StickyWindowItem.m */,
-				59A4B12D11615F10002A310A /* StickiesAppMonitorThread.h */,
-				59A4B12E11615F10002A310A /* StickiesAppMonitorThread.m */,
+				59A4B12D11615F10002A310A /* StickiesAppMonitor.h */,
+				59A4B12E11615F10002A310A /* StickiesAppMonitor.m */,
 			);
 			name = Classes;
 			sourceTree = "<group>";
 			isa = PBXProject;
 			buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "StickiesManager" */;
 			compatibilityVersion = "Xcode 3.1";
+			developmentRegion = English;
 			hasScannedForEncodings = 1;
+			knownRegions = (
+				en,
+			);
 			mainGroup = 29B97314FDCFA39411CA2CEA /* StickiesManager */;
 			projectDirPath = "";
 			projectRoot = "";
 				59A4AD50115DB6C6002A310A /* AppleScriptHelper.m in Sources */,
 				59A4AD9D115DC89B002A310A /* NSAppleEventDescriptor+AEDescUtils.m in Sources */,
 				59A4AE46115F124A002A310A /* StickyWindowItem.m in Sources */,
-				59A4B12F11615F10002A310A /* StickiesAppMonitorThread.m in Sources */,
+				59A4B12F11615F10002A310A /* StickiesAppMonitor.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+				CLANG_ENABLE_OBJC_ARC = YES;
 				GCC_C_LANGUAGE_STANDARD = gnu99;
 				GCC_OPTIMIZATION_LEVEL = 0;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				PREBINDING = NO;
-				SDKROOT = macosx10.6;
+				SDKROOT = "";
 			};
 			name = Debug;
 		};
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+				CLANG_ENABLE_OBJC_ARC = YES;
 				GCC_C_LANGUAGE_STANDARD = gnu99;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
 				PREBINDING = NO;
-				SDKROOT = macosx10.6;
+				SDKROOT = "";
 			};
 			name = Release;
 		};

StickiesManagerAppDelegate.h

 //
 
 #import <Cocoa/Cocoa.h>
-#import "StickiesAppMonitorThread.h"
 
 @interface StickiesManagerAppDelegate : NSObject <NSApplicationDelegate> {
-    NSWindow *window;
-	StickiesAppMonitorThread *thread;
+    NSWindow *_window;
 }
 
-@property (assign) IBOutlet NSWindow *window;
+@property (nonatomic, strong) IBOutlet NSWindow *window;
 
 @end

StickiesManagerAppDelegate.m

 
 #import "StickiesManagerAppDelegate.h"
 #import "StickiesManagerWindowDelegate.h"
+#import "StickiesAppMonitor.h"
 
 @implementation StickiesManagerAppDelegate
 
-@synthesize window;
-
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
-	[((StickiesManagerWindowDelegate *)window.delegate) finalizeInit];
-	thread = [[StickiesAppMonitorThread alloc] init];
-	[thread start];
-}
-
-- (void)applicationWillTerminate:(NSNotification *)notification {
-	[thread cancel];
+	[((StickiesManagerWindowDelegate *)_window.delegate) finalizeInit];
+    [[StickiesAppMonitor instance] startMonitoring];
 }
 
 - (void)dealloc {
-	[thread release];
-	[super dealloc];
+    [[StickiesAppMonitor instance] stop];
 }
 
 #pragma mark -
 #pragma mark NSApplicationDelegate methods
 
 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
-	if (![window isVisible])
-		[window makeKeyAndOrderFront:self];
+	if (![_window isVisible])
+		[_window makeKeyAndOrderFront:self];
 	return NO;
 }
 

StickiesManagerWindowDelegate.m

 
 - (void)dealloc {
 	[[NSNotificationCenter defaultCenter] removeObserver:self];
-	[stickiesView release];
-	[overlayView release];
-	[stickies release];
-	[arrangedObjects release];
-	[script release];
-	[super dealloc];
 }
 
 - (void)finalizeInit {
 	if ([stickies count] > 0) {
 		[timer invalidate];
 		
-		[arrangedObjects release];
-		arrangedObjects = [[NSArray arrayWithArray:stickies] retain];
+		arrangedObjects = [NSArray arrayWithArray:stickies];
 		
 		[self refreshMenu];
 		[tableView reloadData];
 	[stickies removeAllObjects];
 	[self getStickies];
 	if ([stickies count] > 0) {
-		[arrangedObjects release];
-		arrangedObjects = [[NSArray arrayWithArray:stickies] retain];
+		arrangedObjects = [NSArray arrayWithArray:stickies];
 		
 		[self refreshMenu];
 		[tableView reloadData];
 
 - (IBAction)filter:(id)sender {
 	NSString *param = [sender stringValue];
-	NSArray *temp = arrangedObjects;
 	if ([param length] > 0) {
 		NSString *format = nil;
 		if (searchMode == Starts_With)
 		else
 			format = @"name CONTAINS[cd] %@";
 		NSPredicate *filter = [NSPredicate predicateWithFormat:format, param];
-		arrangedObjects = [[arrangedObjects filteredArrayUsingPredicate:filter] retain];
+		arrangedObjects = [arrangedObjects filteredArrayUsingPredicate:filter];
 	} else 
 		arrangedObjects = [stickies copy];
 
-	[temp release];
 	[tableView reloadData];
 }
 
 - (NSPoint)getPositionForSticky:(StickyWindowItem *)sticky scriptHelper:(AppleScriptHelper *)helper {
 	if (!helper) 
-		helper = [[[AppleScriptHelper alloc] initWithAppleScript:script] autorelease];
+		helper = [[AppleScriptHelper alloc] initWithAppleScript:script];
 
 	NSAppleEventDescriptor *paramList = [NSAppleEventDescriptor listDescriptor];
 	NSAppleEventDescriptor *param = [NSAppleEventDescriptor descriptorWithString:sticky.name];
 - (void)getStickies {
 	@try {
 		if (script) {
-			AppleScriptHelper *helper = [[[AppleScriptHelper alloc] initWithAppleScript:script] autorelease];
+			AppleScriptHelper *helper = [[AppleScriptHelper alloc] initWithAppleScript:script];
 		
 			NSAppleEventDescriptor *response = [helper callScript:@"getStickyNames" withArrayOfParameters:nil];
 			if (response) {
 						StickyWindowItem *sticky = [[StickyWindowItem alloc] init];
 						sticky.name = [lines objectAtIndex:0];
 						[stickies addObject:sticky];
-						[sticky release];
 					}
  				}
 			
 		NSLog(@"%@", e);
 	}
 	
-	[arrangedObjects release];
-	arrangedObjects = [[NSArray arrayWithArray:stickies] retain];
+	arrangedObjects = [NSArray arrayWithArray:stickies];
 }
 
 - (void)refreshMenu {
 		[newItem setState:([sw isVisible]) ? NSOnState : NSOffState];
 		[newItem setRepresentedObject:sw];
 		[stickiesMenu addItem:newItem];
-		[newItem release];
 	}	
 }
 
 }
 
 - (void)toggleSticky:(StickyWindowItem *)sticky show:(BOOL)show inMenuItem:(NSMenuItem *)menuItem inRow:(NSUInteger)row {
-	AppleScriptHelper *helper = [[[AppleScriptHelper alloc] initWithAppleScript:script] autorelease];
+	AppleScriptHelper *helper = [[AppleScriptHelper alloc] initWithAppleScript:script];
 	
 	NSAppleEventDescriptor *paramList = [NSAppleEventDescriptor listDescriptor];
 	NSAppleEventDescriptor *param = [NSAppleEventDescriptor descriptorWithString:sticky.name];
 	if (callCount++ == 0) {
 		callCount = 0;
 		NSSortDescriptor *newDescriptor = [[aTableView tableColumnWithIdentifier:@"name"] sortDescriptorPrototype];
-		NSArray *temp = arrangedObjects;
-		arrangedObjects = [[arrangedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:newDescriptor]] retain];
-		[temp release];
+		arrangedObjects = [arrangedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:newDescriptor]];
 	
 		[aTableView reloadData];
 	}

StickyWindowItem.m

 	return self;
 }
 
-- (void)dealloc {
-	[name release];
-	[super dealloc];
-}
-
 @end