Log if setting up launch on start fails on mac

Signed-off-by: Camila Ayres <hello@camilasan.com>
This commit is contained in:
Hannah von Reth 2021-05-03 12:35:28 +02:00 committed by Camila Ayres
parent c4095d698a
commit 3762e410f8
No known key found for this signature in database
GPG key ID: 7A4A6121E88E2AD4

View file

@ -16,6 +16,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "utility.h"
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
@ -88,34 +90,40 @@ void setLaunchOnStartup_private(const QString &appName, const QString &guiName,
CFURLRef urlRef = CFURLCreateWithFileSystemPath(0, folderCFStr, kCFURLPOSIXPathStyle, true); CFURLRef urlRef = CFURLCreateWithFileSystemPath(0, folderCFStr, kCFURLPOSIXPathStyle, true);
LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0); LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0);
if (loginItems && enable) { if (!loginItems) {
//Insert an item to the list. qCWarning(lcUtility) << "Failed to retrieve loginItems";
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, } else {
kLSSharedFileListItemLast, 0, 0, if (enable) {
urlRef, 0, 0); //Insert an item to the list.
if (item) LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,
CFRelease(item); kLSSharedFileListItemLast, 0, 0,
CFRelease(loginItems); urlRef, 0, 0);
} else if (loginItems && !enable) { if (item) {
// We need to iterate over the items and check which one is "ours". CFRelease(item);
UInt32 seedValue; } else {
CFArrayRef itemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue); qCWarning(lcUtility) << "Failed to insert ourself into launch on startup list";
CFStringRef appUrlRefString = CFURLGetString(urlRef);
for (int i = 0; i < CFArrayGetCount(itemsArray); i++) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(itemsArray, i);
CFURLRef itemUrlRef = nullptr;
if (LSSharedFileListItemResolve(item, 0, &itemUrlRef, nullptr) == noErr && itemUrlRef) {
CFStringRef itemUrlString = CFURLGetString(itemUrlRef);
if (CFStringCompare(itemUrlString, appUrlRefString, 0) == kCFCompareEqualTo) {
LSSharedFileListItemRemove(loginItems, item); // remove it!
}
CFRelease(itemUrlRef);
} }
} } else {
CFRelease(itemsArray); // 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 && itemUrlRef) {
CFStringRef itemUrlString = CFURLGetString(itemUrlRef);
if (CFStringCompare(itemUrlString, appUrlRefString, 0) == kCFCompareEqualTo) {
LSSharedFileListItemRemove(loginItems, item); // remove it!
}
CFRelease(itemUrlRef);
}
}
CFRelease(itemsArray);
};
CFRelease(loginItems); CFRelease(loginItems);
}; }
CFRelease(folderCFStr); CFRelease(folderCFStr);
CFRelease(urlRef); CFRelease(urlRef);