From: Miltiadis Vasilakis Date: Mon, 9 Apr 2012 14:44:11 +0000 (+0300) Subject: Support open at login. X-Git-Tag: v0.9~2 X-Git-Url: https://code.grnet.gr/git/pithos-macos/commitdiff_plain/c2312252f51557069c2135bf6b495f069e5db79c Support open at login. --- diff --git a/pithos-macos/en.lproj/MainMenu.xib b/pithos-macos/en.lproj/MainMenu.xib index adaf857..b221913 100644 --- a/pithos-macos/en.lproj/MainMenu.xib +++ b/pithos-macos/en.lproj/MainMenu.xib @@ -12,7 +12,6 @@ YES - NSUserDefaultsController NSMenu NSMenuItem NSCustomObject @@ -1397,6 +1396,24 @@ + + + Open at Login + + 2147483647 + + + + + + YES + YES + + + 2147483647 + + + About Pithos+ @@ -1425,9 +1442,6 @@ - - YES - @@ -2212,6 +2226,51 @@ 599 + + + value: delegate.openAtLogin + + + + + + value: delegate.openAtLogin + value + delegate.openAtLogin + + YES + + YES + NSAllowsEditingMultipleValuesSelection + NSValidatesImmediately + + + YES + + + + + 2 + + + 618 + + + + enabled: delegate.openAtLoginEnabled + + + + + + enabled: delegate.openAtLoginEnabled + enabled + delegate.openAtLoginEnabled + 2 + + + 619 + @@ -3256,6 +3315,8 @@ + + @@ -3307,11 +3368,6 @@ - 576 - - - - 591 @@ -3337,6 +3393,17 @@ + + 606 + + + + + 607 + + + Menu Item - Open at Login + @@ -3489,7 +3556,6 @@ 545.IBPluginDependency 56.IBPluginDependency 57.IBPluginDependency - 576.IBPluginDependency 58.IBPluginDependency 591.IBPluginDependency 592.IBAttributePlaceholdersKey @@ -3497,6 +3563,8 @@ 598.IBAttributePlaceholdersKey 598.IBPluginDependency 603.IBPluginDependency + 606.IBPluginDependency + 607.IBPluginDependency 72.IBPluginDependency 73.IBPluginDependency 74.IBPluginDependency @@ -3660,7 +3728,6 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin YES @@ -3686,6 +3753,8 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin @@ -3700,362 +3769,9 @@ - 604 - - - - YES - - PithosBrowserController - NSWindowController - - YES - - YES - forceRefresh: - refresh: - - - YES - id - id - - - - YES - - YES - forceRefresh: - refresh: - - - YES - - forceRefresh: - id - - - refresh: - id - - - - - YES - - YES - activityProgressIndicator - activityTextField - browser - browserMenu - horizontalSplitView - leftBottomView - leftTopView - outlineView - outlineViewMenu - verticalSplitView - - - YES - NSProgressIndicator - NSTextField - NSBrowser - NSMenu - NSSplitView - NSView - NSView - NSOutlineView - NSMenu - NSSplitView - - - - YES - - YES - activityProgressIndicator - activityTextField - browser - browserMenu - horizontalSplitView - leftBottomView - leftTopView - outlineView - outlineViewMenu - verticalSplitView - - - YES - - activityProgressIndicator - NSProgressIndicator - - - activityTextField - NSTextField - - - browser - NSBrowser - - - browserMenu - NSMenu - - - horizontalSplitView - NSSplitView - - - leftBottomView - NSView - - - leftTopView - NSView - - - outlineView - NSOutlineView - - - outlineViewMenu - NSMenu - - - verticalSplitView - NSSplitView - - - - - IBProjectSource - ./Classes/PithosBrowserController.h - - - - PithosPreferencesController - NSWindowController - - YES - - YES - addAccount: - groupsApply: - groupsRevert: - login: - loginCancel: - removeAccount: - syncApply: - syncCancel: - - - YES - id - id - id - id - id - id - id - id - - - - YES - - YES - addAccount: - groupsApply: - groupsRevert: - login: - loginCancel: - removeAccount: - syncApply: - syncCancel: - - - YES - - addAccount: - id - - - groupsApply: - id - - - groupsRevert: - id - - - login: - id - - - loginCancel: - id - - - removeAccount: - id - - - syncApply: - id - - - syncCancel: - id - - - - - YES - - YES - accountsArrayController - groupsDictionaryController - selectedGroupMembersDictionaryController - - - YES - NSArrayController - NSDictionaryController - NSDictionaryController - - - - YES - - YES - accountsArrayController - groupsDictionaryController - selectedGroupMembersDictionaryController - - - YES - - accountsArrayController - NSArrayController - - - groupsDictionaryController - NSDictionaryController - - - selectedGroupMembersDictionaryController - NSDictionaryController - - - - - IBProjectSource - ./Classes/PithosPreferencesController.h - - - - pithos_macosAppDelegate - NSObject - - YES - - YES - showPithosAbout: - showPithosBrowser: - showPithosPreferences: - - - YES - id - id - id - - - - YES - - YES - showPithosAbout: - showPithosBrowser: - showPithosPreferences: - - - YES - - showPithosAbout: - id - - - showPithosBrowser: - id - - - showPithosPreferences: - id - - - - - YES - - YES - accountsMenu - lastSyncMenu - pithosBrowserController - pithosPreferencesController - statusMenu - - - YES - NSMenu - NSMenu - PithosBrowserController - PithosPreferencesController - NSMenu - - - - YES - - YES - accountsMenu - lastSyncMenu - pithosBrowserController - pithosPreferencesController - statusMenu - - - YES - - accountsMenu - NSMenu - - - lastSyncMenu - NSMenu - - - pithosBrowserController - PithosBrowserController - - - pithosPreferencesController - PithosPreferencesController - - - statusMenu - NSMenu - - - - - IBProjectSource - ./Classes/pithos_macosAppDelegate.h - - - + 619 + 0 IBCocoaFramework diff --git a/pithos-macos/pithos_macosAppDelegate.h b/pithos-macos/pithos_macosAppDelegate.h index 3c7e2cd..9798748 100644 --- a/pithos-macos/pithos_macosAppDelegate.h +++ b/pithos-macos/pithos_macosAppDelegate.h @@ -51,6 +51,10 @@ IBOutlet NSMenu *lastSyncMenu; NSStatusItem *statusItem; + LSSharedFileListRef loginItems; + BOOL openAtLoginEnabled; + BOOL openAtLogin; + NSUserDefaults *userDefaults; BOOL activated; @@ -68,6 +72,8 @@ @property (nonatomic, readonly) PithosBrowserController *pithosBrowserController; @property (nonatomic, readonly) PithosPreferencesController *pithosPreferencesController; @property (nonatomic, assign) BOOL alwaysNo; +@property (nonatomic, assign) BOOL openAtLoginEnabled; +@property (nonatomic, assign) BOOL openAtLogin; @property (nonatomic, assign) BOOL activated; @property (nonatomic, retain) PithosAccount *currentPithosAccount; @property (nonatomic, retain) NSMutableArray *pithosAccounts; diff --git a/pithos-macos/pithos_macosAppDelegate.m b/pithos-macos/pithos_macosAppDelegate.m index f0e78e5..a03fd36 100644 --- a/pithos-macos/pithos_macosAppDelegate.m +++ b/pithos-macos/pithos_macosAppDelegate.m @@ -46,7 +46,7 @@ #import "LastCompletedSyncTransformer.h" @implementation pithos_macosAppDelegate -@synthesize pithosBrowserController, pithosPreferencesController, alwaysNo, activated, +@synthesize pithosBrowserController, pithosPreferencesController, alwaysNo, openAtLoginEnabled, openAtLogin, activated, currentPithosAccount, pithosAccounts, pithosAccountsDictionary, syncPithosAccount, activityFacilityTimeInterval; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { @@ -55,6 +55,15 @@ currentPithosAccount, pithosAccounts, pithosAccountsDictionary, syncPithosAccoun forEventClass:kInternetEventClass andEventID:kAEGetURL]; + // Based on: https://github.com/Mozketo/LaunchAtLoginController + // and: http://cocoatutorial.grapewave.com/2010/02/creating-andor-removing-a-login-item/ + loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); + if (loginItems) { + LSSharedFileListAddObserver(loginItems, CFRunLoopGetMain(), (CFStringRef)NSDefaultRunLoopMode, LSSharedFileListChanged, self); + LSSharedFileListChanged(loginItems, self); + self.openAtLoginEnabled = YES; + } + userDefaults = [[NSUserDefaults standardUserDefaults] retain]; syncTimeInterval = [userDefaults doubleForKey:@"syncTimeInterval"]; @@ -202,10 +211,46 @@ currentPithosAccount, pithosAccounts, pithosAccountsDictionary, syncPithosAccoun if (choice == NSAlertSecondButtonReturn) return NSTerminateCancel; } + if (loginItems) { + LSSharedFileListRemoveObserver(loginItems, CFRunLoopGetMain(), (CFStringRef)NSDefaultRunLoopMode, LSSharedFileListChanged, self); + CFRelease(loginItems); + } return NSTerminateNow; } #pragma mark - +#pragma mark Callbacks + +- (void)loginItemsChanged { + NSURL *appURL = [[NSBundle mainBundle] bundleURL]; + LSSharedFileListItemRef appItem = NULL; + NSArray *snapshot = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, NULL)) autorelease]; + for (id itemObject in snapshot) { + LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject; + UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes; + CFURLRef currentItemURL = NULL; + LSSharedFileListItemResolve(item, resolutionFlags, ¤tItemURL, NULL); + if (currentItemURL && CFEqual(currentItemURL, appURL)) { + CFRelease(currentItemURL); + appItem = item; + break; + } + if (currentItemURL) + CFRelease(currentItemURL); + } + + if (appItem && (!openAtLogin || !openAtLoginEnabled)) + self.openAtLogin = YES; + else if (!appItem && (openAtLogin || !openAtLoginEnabled)) + self.openAtLogin = NO; +} + +void LSSharedFileListChanged(LSSharedFileListRef inList, void *context) { + pithos_macosAppDelegate *self = (id)context; + [self loginItemsChanged]; +} + +#pragma mark - #pragma mark Properties - (PithosBrowserController *)pithosBrowserController { @@ -222,6 +267,39 @@ currentPithosAccount, pithosAccounts, pithosAccountsDictionary, syncPithosAccoun return pithosPreferencesController; } +- (void)setOpenAtLogin:(BOOL)anOpenAtLogin { + if (!openAtLoginEnabled) { + openAtLogin = anOpenAtLogin; + } else if (anOpenAtLogin != openAtLogin) { + NSURL *appURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; + LSSharedFileListItemRef appItem = NULL; + NSArray *snapshot = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, NULL)) autorelease]; + for (id itemObject in snapshot) { + LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject; + UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes; + CFURLRef currentItemURL = NULL; + LSSharedFileListItemResolve(item, resolutionFlags, ¤tItemURL, NULL); + if (currentItemURL && CFEqual(currentItemURL, appURL)) { + CFRelease(currentItemURL); + appItem = item; + break; + } + if (currentItemURL) + CFRelease(currentItemURL); + } + + if (anOpenAtLogin) { + if (!appItem) + LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, (CFURLRef)appURL, NULL, NULL); + openAtLogin = YES; + } else { + if (appItem) + LSSharedFileListItemRemove(loginItems, appItem); + openAtLogin = NO; + } + } +} + #pragma mark - #pragma mark NSMenuDelegate