OS X: Ability to disable startup item

This commit is contained in:
Markus Goetz 2013-07-09 17:13:41 +02:00
parent 13fb49cf39
commit 892419e880

View File

@ -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");