void Autostart::SetActive( bool active ) { #ifdef Q_OS_WIN QString applicationName = QCoreApplication::applicationName(); QString applicationPath = QCoreApplication::applicationFilePath(); QSettings tmpSettings( "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat ); if( active ) { tmpSettings.setValue( applicationName, QString( "\"%1\"" ).arg( QDir::toNativeSeparators( QFileInfo( applicationPath ).filePath() ) ) ); } else { tmpSettings.remove(applicationName); } #elif defined(Q_OS_MAC) LSSharedFileListRef loginItems = LSSharedFileListCreate( NULL, kLSSharedFileListSessionLoginItems, NULL ); if( !loginItems ) return; UInt32 seed = 0U; CFArrayRef currentLoginItems = LSSharedFileListCopySnapshot( loginItems, &seed ); LSSharedFileListItemRef existingItem = FindLoginItemForCurrentBundle( currentLoginItems ); if( active && (existingItem == NULL) ) { CFURLRef mainBundleURL = CFBundleCopyBundleURL( CFBundleGetMainBundle() ); LSSharedFileListInsertItemURL( loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, mainBundleURL, NULL, NULL ); CFRelease( mainBundleURL ); } else if( !active && (existingItem != NULL) ) { LSSharedFileListItemRemove(loginItems, existingItem); } CFRelease( currentLoginItems ); CFRelease( loginItems ); #elif defined Q_OS_LINUX QString homeLocation = QStandardPaths::writableLocation( QStandardPaths::HomeLocation ); QDir* autostartPath = new QDir(homeLocation + "/.config/autostart/"); if( !active ) { QFile* desktopFile = new QFile(autostartPath->filePath("track-o-bot.desktop")); desktopFile->remove(); } else { QFile* srcFile = new QFile( ":/assets/track-o-bot.desktop" ); LOG("source: %s", srcFile->fileName().toStdString().c_str()); LOG("source exists: %s", QString::number(srcFile->exists()).toStdString().c_str()); srcFile->copy(autostartPath->filePath("track-o-bot.desktop")); } #endif }
bool SetStartOnSystemStartup(bool fAutoStart) { CFURLRef patacoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, patacoinAppUrl); if(fAutoStart && !foundItem) { // add patacoin app to startup item list LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, NULL, NULL, patacoinAppUrl, NULL, NULL); } else if(!fAutoStart && foundItem) { // remove item LSSharedFileListItemRemove(loginItems, foundItem); } return true; }
void Utility::setupFavLink(const QString &folder) { #ifdef Q_OS_WIN // Windows Explorer: Place under "Favorites" (Links) wchar_t path[MAX_PATH]; SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE); QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path)); QDir folderDir(QDir::fromNativeSeparators(folder)); QString linkName = profile+QLatin1String("/Links/") + folderDir.dirName() + QLatin1String(".lnk"); if (!QFile::link(folder, linkName)) qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!"; #elif defined (Q_OS_MAC) // Finder: Place under "Places"/"Favorites" on the left sidebar CFStringRef folderCFStr = CFStringCreateWithCString(0, folder.toUtf8().data(), kCFStringEncodingUTF8); CFURLRef urlRef = CFURLCreateWithFileSystemPath (0, folderCFStr, kCFURLPOSIXPathStyle, true); LSSharedFileListRef placesItems = LSSharedFileListCreate(0, kLSSharedFileListFavoriteItems, 0); if (placesItems) { //Insert an item to the list. LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(placesItems, kLSSharedFileListItemLast, 0, 0, urlRef, 0, 0); if (item) CFRelease(item); } CFRelease(placesItems); CFRelease(folderCFStr); CFRelease(urlRef); #elif defined (Q_OS_UNIX) // Nautilus: add to ~/.gtk-bookmarks QFile gtkBookmarks(QDir::homePath()+QLatin1String("/.gtk-bookmarks")); QByteArray folderUrl = "file://" + folder.toUtf8(); if (gtkBookmarks.open(QFile::ReadWrite)) { QByteArray places = gtkBookmarks.readAll(); if (!places.contains(folderUrl)) { places += folderUrl; gtkBookmarks.reset(); gtkBookmarks.write(places + '\n'); } } #endif }
void InsertLoginItem(const char *cpath) { LSSharedFileListRef items = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, NULL); if (!items) { return; } CFStringRef path = CFStringCreateWithCString(kCFAllocatorDefault, cpath, kCFStringEncodingUTF8); CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, false); LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(items, kLSSharedFileListItemLast, NULL, NULL, url, NULL, NULL); CFRelease(items); CFRelease(path); CFRelease(url); if (item) { CFRelease(item); } }
void Utility::setLaunchOnStartup(const QString &appName, const QString& guiName, bool enable) { #if defined(Q_OS_WIN) Q_UNUSED(guiName) QString runPath = QLatin1String(runPathC); QSettings settings(runPath, QSettings::NativeFormat); if (enable) { settings.setValue(appName, QCoreApplication::applicationFilePath().replace('/','\\')); } else { settings.remove(appName); } #elif defined(Q_OS_MAC) Q_UNUSED(guiName) 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); 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); } 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"); if (enable) { if (!QDir().exists(userAutoStartPath) && !QDir().mkdir(userAutoStartPath)) { qDebug() << "Could not create autostart directory"; return; } QFile iniFile(desktopFileLocation); if (!iniFile.open(QIODevice::WriteOnly)) { qDebug() << "Could not write auto start entry" << desktopFileLocation; return; } QTextStream ts(&iniFile); ts.setCodec("UTF-8"); ts << QLatin1String("[Desktop Entry]") << endl << QLatin1String("Name=") << guiName << endl << QLatin1String("GenericName=") << QLatin1String("File Synchronizer") << endl << QLatin1String("Exec=") << QCoreApplication::applicationFilePath() << endl << QLatin1String("Terminal=") << "false" << endl << QLatin1String("Icon=") << appName << endl << QLatin1String("Categories=") << QLatin1String("Network") << endl << QLatin1String("Type=") << QLatin1String("Application") << endl << QLatin1String("StartupNotify=") << "false" << endl << QLatin1String("X-GNOME-Autostart-enabled=") << "true" << endl ; } else { if (!QFile::remove(desktopFileLocation)) { qDebug() << "Could not remove autostart desktop file"; } } #endif }
void AsemanAutoStartManager::save() { #if defined(Q_OS_LINUX) || defined(Q_OS_OPENBSD) const QString &pathDir = QDir::homePath() + "/.config/autostart"; const QString &path = pathDir + "/" + p->source + ".desktop"; QDir().mkpath(pathDir); QString data = QString("[Desktop Entry]") + "\nHidden=" + (p->active?"false":"true") + "\nX-GNOME-Autostart-enabled=" + (p->active?"true":"false") + "\nName=" + p->name + "\nName[en_US]=" + p->name + "\nComment=" + p->comment + "\nComment[en_US]=" + p->comment + "\nType=" + p->type + "\nExec=" + p->command + "\nNoDisplay=false\n"; QFile file(path); if(!file.open(QFile::WriteOnly)) return; file.write(data.toUtf8()); file.close(); #elif defined(Q_OS_WIN) QSettings autoStartSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); if(p->active) { autoStartSettings.setValue(p->source, QDir::toNativeSeparators(QDir::cleanPath(p->command))); } else { autoStartSettings.remove(p->source); } #elif defined(Q_OS_MAC) && defined(OSX_CORE_SERVICES_AVAILABLE) CFURLRef url = prepareURL(p->command); if (!url) { qWarning("unable to create CFURLRef"); return; } LSSharedFileListRef login_items = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); if (!login_items) { qWarning("unable to get login items"); return; } CFArrayRef login_items_array = LSSharedFileListCopySnapshot(login_items, NULL); if (!login_items_array) { qWarning("unable to get login items array"); CFRelease(login_items); return; } CFIndex count = CFArrayGetCount(login_items_array); CFStringRef url_string = CFURLGetString(url); CFURLRef item_url = NULL; for (CFIndex i = 0; i < count; i += 1) { LSSharedFileListItemRef item = (LSSharedFileListItemRef) CFArrayGetValueAtIndex(login_items_array, i); if (LSSharedFileListItemResolve(item, 0, &item_url, NULL) != 0) { qWarning("unable to resolve login item"); CFRelease(login_items_array); CFRelease(login_items); return; } CFStringRef item_url_string = CFURLGetString(item_url); CFComparisonResult result = CFStringCompare(url_string, item_url_string, 0); CFRelease(item_url); if (result == kCFCompareEqualTo) { if (!p->active) { LSSharedFileListItemRemove(login_items, item); CFRelease(login_items_array); CFRelease(login_items); return; } qWarning("found in login items already"); CFRelease(login_items_array); CFRelease(login_items); return; } } if (p->active) { LSSharedFileListItemRef item = LSSharedFileListInsertItemURL( login_items, kLSSharedFileListItemLast, p->name.toCFString(), NULL, url, NULL, NULL); if (!item) { qWarning("Unable to add to login items"); CFRelease(login_items_array); CFRelease(login_items); return; } CFRelease(item); } CFRelease(login_items_array); CFRelease(login_items); #endif }