diff --git a/src/mirall/utility.cpp b/src/mirall/utility.cpp index 7e227941fc..f0a5b94f82 100644 --- a/src/mirall/utility.cpp +++ b/src/mirall/utility.cpp @@ -199,26 +199,42 @@ void Utility::setLaunchOnStartup(const QString &appName, const QString& guiName, } #elif defined(Q_OS_MAC) Q_UNUSED(guiName) - if (enable) { - QString filePath = QDir(QCoreApplication::applicationDirPath()+QLatin1String("/../..")).absolutePath(); - CFStringRef folderCFStr = CFStringCreateWithCString(0, filePath.toUtf8().data(), kCFStringEncodingUTF8); - CFURLRef urlRef = CFURLCreateWithFileSystemPath (0, folderCFStr, kCFURLPOSIXPathStyle, true); + QString filePath = QDir(QCoreApplication::applicationDirPath()+QLatin1String("/../..")).absolutePath(); + CFStringRef folderCFStr = CFStringCreateWithCString(0, filePath.toUtf8().data(), kCFStringEncodingUTF8); + CFURLRef urlRef = CFURLCreateWithFileSystemPath (0, folderCFStr, kCFURLPOSIXPathStyle, true); + LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0); - LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0); - if (loginItems) { - //Insert an item to the list. - LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, - kLSSharedFileListItemLast, 0, 0, - urlRef, 0, 0); - if (item) - CFRelease(item); - } + if (loginItems && enable) { + //Insert an item to the list. + LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, + kLSSharedFileListItemLast, 0, 0, + urlRef, 0, 0); + if (item) + CFRelease(item); CFRelease(loginItems); - CFRelease(folderCFStr); - CFRelease(urlRef); - } else { - // implement me - } + } else if (loginItems && !enable){ + // We need to iterate over the items and check which one is "ours". + UInt32 seedValue; + CFArrayRef itemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue); + CFStringRef appUrlRefString = CFURLGetString(urlRef); + for (int i = 0; i < CFArrayGetCount(itemsArray); i++) { + LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(itemsArray, i); + CFURLRef itemUrlRef = NULL; + + if (LSSharedFileListItemResolve(item, 0, &itemUrlRef, NULL) == noErr) { + CFStringRef itemUrlString = CFURLGetString(itemUrlRef); + if (CFStringCompare(itemUrlString,appUrlRefString,0) == kCFCompareEqualTo) { + LSSharedFileListItemRemove(loginItems,item); // remove it! + } + CFRelease(itemUrlRef); + } + } + CFRelease(itemsArray); + CFRelease(loginItems); + }; + + CFRelease(folderCFStr); + CFRelease(urlRef); #elif defined(Q_OS_UNIX) QString userAutoStartPath = QDir::homePath()+QLatin1String("/.config/autostart/"); QString desktopFileLocation = userAutoStartPath+appName+QLatin1String(".desktop");