Skip to content

Commit ba02753

Browse files
committed
Autostart function for Linux
1 parent 306f851 commit ba02753

2 files changed

Lines changed: 90 additions & 40 deletions

File tree

src/mainwindow.cpp

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,56 +1438,104 @@ void MainWindow::create_tray_icon() {
14381438

14391439
}
14401440

1441-
bool MainWindow::is_soft_in_autorun()
1442-
{
1443-
QString startup_dir = QDir::toNativeSeparators(QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()) + QDir::separator() + "Startup" + QDir::separator();
1444-
QString lnk_path = startup_dir + SOFT_NAME + ".lnk";
1441+
#ifdef Q_OS_WIN
1442+
bool MainWindow::is_soft_in_autorun()
1443+
{
1444+
QString startup_dir = QDir::toNativeSeparators(QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()) + QDir::separator() + "Startup" + QDir::separator();
1445+
QString lnk_path = startup_dir + SOFT_NAME + ".lnk";
14451446

1446-
QFile link(lnk_path);
1447-
if (link.exists()) {
1448-
return true;
1449-
} else {
1450-
return false;
1447+
QFile link(lnk_path);
1448+
if (link.exists()) {
1449+
return true;
1450+
} else {
1451+
return false;
1452+
}
14511453
}
1452-
}
14531454

1454-
void MainWindow::add_soft_to_autorun() {
1455+
void MainWindow::add_soft_to_autorun() {
14551456

1456-
QString startup_dir = QDir::toNativeSeparators(QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()) + QDir::separator() + "Startup" + QDir::separator();
1457-
QString lnk_path = startup_dir + SOFT_NAME + ".lnk";
1457+
QString startup_dir = QDir::toNativeSeparators(QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()) + QDir::separator() + "Startup" + QDir::separator();
1458+
QString lnk_path = startup_dir + SOFT_NAME + ".lnk";
14581459

1459-
QFile link(lnk_path);
1460-
if (link.exists()) {
1461-
link.remove();
1462-
}
1460+
QFile link(lnk_path);
1461+
if (link.exists()) {
1462+
link.remove();
1463+
}
14631464

1464-
IShellLink *shell_link;
1465-
CoInitialize(NULL);
1466-
HRESULT result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void**>(&shell_link));
1467-
if (result == S_OK) {
1468-
IPersistFile *persist_file;
1469-
shell_link->SetPath(reinterpret_cast<const wchar_t *>(QDir::toNativeSeparators(qApp->applicationFilePath()).utf16()));
1470-
shell_link->SetWorkingDirectory(reinterpret_cast<const wchar_t *>(QDir::toNativeSeparators(qApp->applicationDirPath()).utf16()));
1471-
result = shell_link->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&persist_file));
1465+
IShellLink *shell_link;
1466+
CoInitialize(NULL);
1467+
HRESULT result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void**>(&shell_link));
14721468
if (result == S_OK) {
1473-
persist_file->Save(reinterpret_cast<const wchar_t *>(lnk_path.utf16()), TRUE);
1474-
persist_file->Release();
1469+
IPersistFile *persist_file;
1470+
shell_link->SetPath(reinterpret_cast<const wchar_t *>(QDir::toNativeSeparators(qApp->applicationFilePath()).utf16()));
1471+
shell_link->SetWorkingDirectory(reinterpret_cast<const wchar_t *>(QDir::toNativeSeparators(qApp->applicationDirPath()).utf16()));
1472+
result = shell_link->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&persist_file));
1473+
if (result == S_OK) {
1474+
persist_file->Save(reinterpret_cast<const wchar_t *>(lnk_path.utf16()), TRUE);
1475+
persist_file->Release();
1476+
}
1477+
shell_link->Release();
14751478
}
1476-
shell_link->Release();
1479+
CoUninitialize();
14771480
}
1478-
CoUninitialize();
1479-
}
14801481

1481-
void MainWindow::remove_soft_from_autorun()
1482-
{
1483-
QString startup_dir = QDir::toNativeSeparators(QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()) + QDir::separator() + "Startup" + QDir::separator();
1484-
QString lnk_path = startup_dir + SOFT_NAME + ".lnk";
1482+
void MainWindow::remove_soft_from_autorun()
1483+
{
1484+
QString startup_dir = QDir::toNativeSeparators(QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation).first()) + QDir::separator() + "Startup" + QDir::separator();
1485+
QString lnk_path = startup_dir + SOFT_NAME + ".lnk";
14851486

1486-
QFile link(lnk_path);
1487-
if (link.exists()) {
1488-
link.remove();
1487+
QFile link(lnk_path);
1488+
if (link.exists()) {
1489+
link.remove();
1490+
}
14891491
}
1490-
}
1492+
#else
1493+
bool MainWindow::is_soft_in_autorun()
1494+
{
1495+
QString autostart_dir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() + "autostart" + QDir::separator();
1496+
QString desktop_file_path = autostart_dir + SOFT_NAME + ".desktop";
1497+
1498+
QFile desktop_file(desktop_file_path);
1499+
return desktop_file.exists();
1500+
}
1501+
1502+
void MainWindow::add_soft_to_autorun()
1503+
{
1504+
QString autostart_dir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() + "autostart" + QDir::separator();
1505+
1506+
// Create an autostart folder if it does not exist
1507+
QDir().mkpath(autostart_dir);
1508+
1509+
QString desktop_file_path = autostart_dir + SOFT_NAME + ".desktop";
1510+
1511+
QFile desktop_file(desktop_file_path);
1512+
if (desktop_file.exists()) {
1513+
desktop_file.remove();
1514+
}
1515+
1516+
QString desktop_file_content = "[Desktop Entry]\n"
1517+
"Type=Application\n"
1518+
"Exec=" + QCoreApplication::applicationFilePath() + "\n"
1519+
"Path=" + QCoreApplication::applicationDirPath() + "\n"
1520+
"Name=" + SOFT_NAME + "\n";
1521+
1522+
desktop_file.open(QIODevice::WriteOnly | QIODevice::Text);
1523+
QTextStream stream(&desktop_file);
1524+
stream << desktop_file_content;
1525+
desktop_file.close();
1526+
}
1527+
1528+
void MainWindow::remove_soft_from_autorun()
1529+
{
1530+
QString autostart_dir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() + "autostart" + QDir::separator();
1531+
QString desktop_file_path = autostart_dir + SOFT_NAME + ".desktop";
1532+
1533+
QFile desktop_file(desktop_file_path);
1534+
if (desktop_file.exists()) {
1535+
desktop_file.remove();
1536+
}
1537+
}
1538+
#endif
14911539

14921540
void MainWindow::on_encryption_button_clicked()
14931541
{

src/mainwindow.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#include <QByteArray>
4444
#include <QSettings>
4545

46-
#include <Windows.h>
47-
#include <shlobj.h>
46+
#ifdef Q_OS_WIN
47+
#include <windows.h>
48+
#include <shlobj.h>
49+
#endif
4850

4951
#include <algorithm>
5052

0 commit comments

Comments
 (0)