QSettings setting(QDir::homePath() + QDir::separator() + "Library/Preferences/loginwindow.plist",QSettings::NativeFormat); QDir dir(QCoreApplication::applicationDirPath()); dir.cdUp(); dir.cdUp(); QVariantList lst = qvariant_cast<QVariantList >(setting.value("AutoLaunchedApplicationDictionary")); bool exist = false; for (int i = 0; i < lst.count(); ++i) { QVariantMap prop = qvariant_cast<QVariantMap >(lst[i]); if (prop["Path"] == dir.absolutePath()) { exist = true; break; } } if (exist == false) { QVariantMap v; v["Path"] = dir.absolutePath(); v["Hidden"] = false; lst.append(v); setting.setValue("AutoLaunchedApplicationDictionary",lst); }
C++ (Qt)void addAppAsLoginItem{ // Create a reference to the shared file list. // We are adding it to the current user only. // If we want to add it all users, use // kLSSharedFileListGlobalLoginItems instead of // kLSSharedFileListSessionLoginItems LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); if (loginItems) { // This will retrieve the path for the application // For example, /Applications/test.app CFURLRef bundleUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); // Insert an item to the list. LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, NULL, NULL, bundleUrl, NULL, NULL); if (item) CFRelease(item); else qDebug("failed to add login item"); CFRelease(bundleUrl); CFRelease(loginItems); } else qDebug("failed to obtain login items");} void deleteAppFromLoginItem{ // Create a reference to the shared file list. LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); if (loginItems) { CFURLRef bundleUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); // Retrieve the list of Login Items UInt32 seedValue; CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(loginItems, &seedValue); for (CFIndex i = 0, n = CFArrayGetCount(loginItemsArray); i < n; ++i) { LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(loginItemsArray, i); // Resolve the item with URL CFURLRef url; if (LSSharedFileListItemResolve(itemRef, 0, &url, NULL) == noErr) { bool shouldRemove = CFEqual(bundleUrl, url); CFRelease(url); if (shouldRemove) { LSSharedFileListItemRemove(loginItems, itemRef); break; } } } CFRelease(loginItemsArray); CFRelease(bundleUrl); CFRelease(loginItems); }}
C (Mac)[[NSAlert alertWithMessageText:@"An error ocurred" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"LSRegisterURL failed"] runModal];