Changeset View
Changeset View
Standalone View
Standalone View
src/qt/guiutil.cpp
Show First 20 Lines • Show All 736 Lines • ▼ Show 20 Lines | |||||
#pragma GCC diagnostic push | #pragma GCC diagnostic push | ||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||||
// based on: | // based on: | ||||
// https://github.com/Mozketo/LaunchAtLoginController/blob/master/LaunchAtLoginController.m | // https://github.com/Mozketo/LaunchAtLoginController/blob/master/LaunchAtLoginController.m | ||||
#include <CoreFoundation/CoreFoundation.h> | #include <CoreFoundation/CoreFoundation.h> | ||||
#include <CoreServices/CoreServices.h> | #include <CoreServices/CoreServices.h> | ||||
// NB: caller must release returned ref if it's not NULL | |||||
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, | LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, | ||||
CFURLRef findUrl); | CFURLRef findUrl); | ||||
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, | LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, | ||||
CFURLRef findUrl) { | CFURLRef findUrl) { | ||||
LSSharedFileListItemRef foundItem = nullptr; | |||||
// loop through the list of startup items and try to find the bitcoin app | // loop through the list of startup items and try to find the bitcoin app | ||||
CFArrayRef listSnapshot = LSSharedFileListCopySnapshot(list, nullptr); | CFArrayRef listSnapshot = LSSharedFileListCopySnapshot(list, nullptr); | ||||
for (int i = 0; i < CFArrayGetCount(listSnapshot); i++) { | for (int i = 0; !foundItem && i < CFArrayGetCount(listSnapshot); ++i) { | ||||
LSSharedFileListItemRef item = | LSSharedFileListItemRef item = | ||||
(LSSharedFileListItemRef)CFArrayGetValueAtIndex(listSnapshot, i); | (LSSharedFileListItemRef)CFArrayGetValueAtIndex(listSnapshot, i); | ||||
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | | UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | | ||||
kLSSharedFileListDoNotMountVolumes; | kLSSharedFileListDoNotMountVolumes; | ||||
CFURLRef currentItemURL = nullptr; | CFURLRef currentItemURL = nullptr; | ||||
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && \ | #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && \ | ||||
MAC_OS_X_VERSION_MAX_ALLOWED >= 10100 | MAC_OS_X_VERSION_MAX_ALLOWED >= 10100 | ||||
if (&LSSharedFileListItemCopyResolvedURL) | if (&LSSharedFileListItemCopyResolvedURL) | ||||
currentItemURL = LSSharedFileListItemCopyResolvedURL( | currentItemURL = LSSharedFileListItemCopyResolvedURL( | ||||
item, resolutionFlags, nullptr); | item, resolutionFlags, nullptr); | ||||
#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ | #if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ | ||||
MAC_OS_X_VERSION_MIN_REQUIRED < 10100 | MAC_OS_X_VERSION_MIN_REQUIRED < 10100 | ||||
else | else | ||||
LSSharedFileListItemResolve(item, resolutionFlags, ¤tItemURL, | LSSharedFileListItemResolve(item, resolutionFlags, ¤tItemURL, | ||||
nullptr); | nullptr); | ||||
#endif | #endif | ||||
#else | #else | ||||
LSSharedFileListItemResolve(item, resolutionFlags, ¤tItemURL, | LSSharedFileListItemResolve(item, resolutionFlags, ¤tItemURL, | ||||
nullptr); | nullptr); | ||||
#endif | #endif | ||||
if (currentItemURL && CFEqual(currentItemURL, findUrl)) { | if (currentItemURL && CFEqual(currentItemURL, findUrl)) { | ||||
// found | // found | ||||
CFRelease(currentItemURL); | CFRetain(foundItem = item); | ||||
return item; | |||||
} | } | ||||
if (currentItemURL) { | if (currentItemURL) { | ||||
CFRelease(currentItemURL); | CFRelease(currentItemURL); | ||||
} | } | ||||
} | } | ||||
return nullptr; | CFRelease(listSnapshot); | ||||
return foundItem; | |||||
} | } | ||||
bool GetStartOnSystemStartup() { | bool GetStartOnSystemStartup() { | ||||
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); | CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); | ||||
LSSharedFileListRef loginItems = LSSharedFileListCreate( | LSSharedFileListRef loginItems = LSSharedFileListCreate( | ||||
nullptr, kLSSharedFileListSessionLoginItems, nullptr); | nullptr, kLSSharedFileListSessionLoginItems, nullptr); | ||||
LSSharedFileListItemRef foundItem = | LSSharedFileListItemRef foundItem = | ||||
findStartupItemInList(loginItems, bitcoinAppUrl); | findStartupItemInList(loginItems, bitcoinAppUrl); | ||||
return !!foundItem; // return boolified object | // findStartupItemInList retains the item it returned, need to release | ||||
if (foundItem) { | |||||
CFRelease(foundItem); | |||||
} | |||||
CFRelease(loginItems); | |||||
CFRelease(bitcoinAppUrl); | |||||
return foundItem; | |||||
} | } | ||||
bool SetStartOnSystemStartup(bool fAutoStart) { | bool SetStartOnSystemStartup(bool fAutoStart) { | ||||
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); | CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); | ||||
LSSharedFileListRef loginItems = LSSharedFileListCreate( | LSSharedFileListRef loginItems = LSSharedFileListCreate( | ||||
nullptr, kLSSharedFileListSessionLoginItems, nullptr); | nullptr, kLSSharedFileListSessionLoginItems, nullptr); | ||||
LSSharedFileListItemRef foundItem = | LSSharedFileListItemRef foundItem = | ||||
findStartupItemInList(loginItems, bitcoinAppUrl); | findStartupItemInList(loginItems, bitcoinAppUrl); | ||||
if (fAutoStart && !foundItem) { | if (fAutoStart && !foundItem) { | ||||
// add bitcoin app to startup item list | // add bitcoin app to startup item list | ||||
LSSharedFileListInsertItemURL(loginItems, | LSSharedFileListInsertItemURL(loginItems, | ||||
kLSSharedFileListItemBeforeFirst, nullptr, | kLSSharedFileListItemBeforeFirst, nullptr, | ||||
nullptr, bitcoinAppUrl, nullptr, nullptr); | nullptr, bitcoinAppUrl, nullptr, nullptr); | ||||
} else if (!fAutoStart && foundItem) { | } else if (!fAutoStart && foundItem) { | ||||
// remove item | // remove item | ||||
LSSharedFileListItemRemove(loginItems, foundItem); | LSSharedFileListItemRemove(loginItems, foundItem); | ||||
} | } | ||||
// findStartupItemInList retains the item it returned, need to release | |||||
if (foundItem) { | |||||
CFRelease(foundItem); | |||||
} | |||||
CFRelease(loginItems); | |||||
CFRelease(bitcoinAppUrl); | |||||
return true; | return true; | ||||
} | } | ||||
#pragma GCC diagnostic pop | #pragma GCC diagnostic pop | ||||
#else | #else | ||||
bool GetStartOnSystemStartup() { | bool GetStartOnSystemStartup() { | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 154 Lines • Show Last 20 Lines |