@@ -146,6 +146,57 @@ void file_remover (QString file_name) {
146146 return ;
147147}
148148
149+ bool is_soft_in_autorun ()
150+ {
151+ QString startup_dir = QDir::toNativeSeparators (QStandardPaths::standardLocations (QStandardPaths::ApplicationsLocation).first ()) + QDir::separator () + " Startup" + QDir::separator ();
152+ QString lnk_path = startup_dir + SOFT_NAME + " .lnk" ;
153+
154+ QFile link (lnk_path);
155+ if (link.exists ()) {
156+ return true ;
157+ } else {
158+ return false ;
159+ }
160+ }
161+
162+ void add_soft_to_autorun (QApplication &app) { // Another way to create .lnk in Windows
163+
164+ QString startup_dir = QDir::toNativeSeparators (QStandardPaths::standardLocations (QStandardPaths::ApplicationsLocation).first ()) + QDir::separator () + " Startup" + QDir::separator ();
165+ QString lnk_path = startup_dir + SOFT_NAME + " .lnk" ;
166+
167+ QFile link (lnk_path);
168+ if (link.exists ()) {
169+ link.remove ();
170+ }
171+
172+ IShellLink *shell_link;
173+ CoInitialize (NULL );
174+ HRESULT result = CoCreateInstance (CLSID_ShellLink, NULL , CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast <void **>(&shell_link));
175+ if (result == S_OK) {
176+ IPersistFile *persistFile;
177+ shell_link->SetPath (reinterpret_cast <const wchar_t *>(QDir::toNativeSeparators (app.applicationFilePath ()).utf16 ()));
178+ result = shell_link->QueryInterface (IID_IPersistFile, reinterpret_cast <void **>(&persistFile));
179+ if (result == S_OK) {
180+ persistFile->Save (reinterpret_cast <const wchar_t *>(lnk_path.utf16 ()), TRUE );
181+ persistFile->Release ();
182+ }
183+ shell_link->Release ();
184+ }
185+ CoUninitialize ();
186+ }
187+
188+ void remove_soft_from_autorun ()
189+ {
190+ QString startup_dir = QDir::toNativeSeparators (QStandardPaths::standardLocations (QStandardPaths::ApplicationsLocation).first ()) + QDir::separator () + " Startup" + QDir::separator ();
191+ QString lnk_path = startup_dir + SOFT_NAME + " .lnk" ;
192+
193+ QFile link (lnk_path);
194+ if (link.exists ()) {
195+ link.remove ();
196+ }
197+ }
198+
199+ /* Part of the code causing problems in work : Widget not starts automatically
149200bool is_soft_in_autorun()
150201{
151202 HKEY hKey;
@@ -175,8 +226,7 @@ void add_soft_to_autorun()
175226 // Open the autorun registry key
176227 if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS)
177228 {
178- QString qs_path = qApp->applicationFilePath ();
179- qs_path.replace (" /" , " \\ " );
229+ QString qs_path = QDir::toNativeSeparators(qApp->applicationFilePath());
180230
181231 // Add the program to the autorun list
182232 RegSetValueEx(hKey, TEXT(dSOFT_NAME), 0, REG_SZ, (LPBYTE)QString(qs_path).toStdWString().c_str(), dwSize);
@@ -196,14 +246,15 @@ void remove_soft_from_autorun()
196246 RegCloseKey(hKey);
197247 }
198248}
249+ */
199250
200251int main (int argc, char *argv[])
201252{
202253 QApplication app (argc, argv);
203254
204255 qInstallMessageHandler (messageHandler); // Output debug info to qInfo.log
205256
206- // Update Block: if the filename of the current instance is new.exe, move me from new.exe to StockWidget.exe (with 1 second sleep)
257+ // Update Block : if the filename of the current instance is new.exe, move me from new.exe to StockWidget.exe (with 1 second sleep)
207258 if (qApp->applicationName () == " new" ) {
208259
209260 QThread::msleep (3000 );
@@ -264,7 +315,7 @@ int main(int argc, char *argv[])
264315 // Add the action to toggle autorun to the tray menu
265316 QObject::connect (autorunAction, &QAction::toggled, [&](bool checked) {
266317 if (autorunAction->isChecked ()) {
267- add_soft_to_autorun ();
318+ add_soft_to_autorun (app );
268319 } else {
269320 remove_soft_from_autorun ();
270321 }
0 commit comments