103 lines
2.7 KiB
C++
103 lines
2.7 KiB
C++
/*!
|
|
* \copyright Copyright (c) 2014-2018 Governikus GmbH & Co. KG, Germany
|
|
*/
|
|
|
|
#include "AutoStart.h"
|
|
|
|
#include <QCoreApplication>
|
|
#include <QLoggingCategory>
|
|
#include <QRegularExpression>
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
using namespace governikus;
|
|
|
|
Q_DECLARE_LOGGING_CATEGORY(settings)
|
|
|
|
static bool checkAndRemoveAutoStart(bool pRemove)
|
|
{
|
|
qCDebug(settings) << "Loading OSX login items";
|
|
|
|
QRegularExpression regex("/Contents/Resources$");
|
|
NSString* appPath = QCoreApplication::applicationDirPath().remove(regex).toNSString();
|
|
CFURLRef url = static_cast<CFURLRef>([NSURL fileURLWithPath: appPath]);
|
|
|
|
// Create a reference to the shared file list.
|
|
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL,
|
|
kLSSharedFileListSessionLoginItems, NULL);
|
|
|
|
if (loginItems)
|
|
{
|
|
//Retrieve the list of Login Items and cast them to
|
|
// a NSArray so that it will be easier to iterate.
|
|
NSArray* loginItemsArray = static_cast<NSArray*>(LSSharedFileListCopySnapshot(loginItems, NULL));
|
|
for (NSUInteger i = 0; i < [loginItemsArray count]; ++i)
|
|
{
|
|
LSSharedFileListItemRef itemRef = static_cast<LSSharedFileListItemRef>([loginItemsArray objectAtIndex:i]);
|
|
//Resolve the item with URL
|
|
if (LSSharedFileListItemResolve(itemRef, 0, static_cast<CFURLRef*>(&url), NULL) == noErr)
|
|
{
|
|
NSURL* nsUrl = static_cast<NSURL*>(url);
|
|
NSString* urlPath = [nsUrl path];
|
|
if ([urlPath compare : appPath] == NSOrderedSame)
|
|
{
|
|
if (pRemove)
|
|
{
|
|
LSSharedFileListItemRemove(loginItems, itemRef);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
const CFStringRef stringRef = LSSharedFileListItemCopyDisplayName(itemRef);
|
|
if (stringRef)
|
|
{
|
|
const QString displayName = QString::fromCFString(stringRef);
|
|
if (displayName.startsWith(QCoreApplication::applicationName()))
|
|
{
|
|
LSSharedFileListItemRemove(loginItems, itemRef);
|
|
qCDebug(settings) << "Removed the unresolvable application with diplay name" << displayName << "from OSX login items.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
[loginItemsArray release];
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool AutoStart::enabled()
|
|
{
|
|
return checkAndRemoveAutoStart(false);
|
|
}
|
|
|
|
|
|
void AutoStart::set(bool pEnabled)
|
|
{
|
|
if (pEnabled)
|
|
{
|
|
QRegularExpression regex("/Contents/Resources$");
|
|
NSString* path = QCoreApplication::applicationDirPath().remove(regex).toNSString();
|
|
CFURLRef url = static_cast<CFURLRef>([NSURL fileURLWithPath: path]);
|
|
|
|
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
|
|
if (loginItems)
|
|
{
|
|
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, NULL, NULL, url, NULL, NULL);
|
|
if (item)
|
|
{
|
|
CFRelease(item);
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
checkAndRemoveAutoStart(true);
|
|
}
|