Skip to content

Commit e2e83b4

Browse files
committed
New Autorun implementation
* With this patch, release 1.0.2 is coming.
1 parent acde865 commit e2e83b4

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.5)
22

3-
project(StockWidget VERSION 1.0.1 LANGUAGES CXX)
3+
project(StockWidget VERSION 1.0.2 LANGUAGES CXX)
44

55
set(CMAKE_AUTOUIC ON)
66
set(CMAKE_AUTOMOC ON)

main.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
149200
bool 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

200251
int 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
}

mainwindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <QTemporaryFile>
3939
#include <QtConcurrent>
4040
#include <QCryptographicHash>
41+
#include <QStandardPaths>
4142

4243
#include "qvlabel.hpp"
4344

@@ -50,6 +51,8 @@
5051
#include <tchar.h>
5152
#include <string>
5253

54+
#include <shlobj.h>
55+
5356
#include <unzip.h>
5457

5558
QT_BEGIN_NAMESPACE

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define dSOFT_NAME "StockWidget"
77

88
const QString SOFT_NAME = dSOFT_NAME;
9-
const QString SOFT_VERSION = "1.0.1";
9+
const QString SOFT_VERSION = "1.0.2";
1010
const QString CONFIG_NAME = "config.cfg";
1111

1212
#endif // VERSION_H

0 commit comments

Comments
 (0)