Skip to content

Commit

Permalink
code cleanup, use QT for shortcut creation, ditched windows api
Browse files Browse the repository at this point in the history
  • Loading branch information
Odizinne committed Sep 12, 2024
1 parent 84bcf5b commit 6c969a5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 88 deletions.
4 changes: 0 additions & 4 deletions HeadsetControl-Qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ RESOURCES += \

RC_FILE = src/Resources/appicon.rc

win32 {
LIBS += -lole32
}

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
Expand Down
93 changes: 9 additions & 84 deletions src/ShortcutManager/shortcutmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,28 @@
#include <QStandardPaths>
#include <QSysInfo>
#include <QtWidgets/QWidget>
#include <QStandardPaths>

#ifdef _WIN32
#include <shlobj.h>
#include <shobjidl.h>
#include <windows.h>

QString getStartupFolder()
{
QString path;
WCHAR szPath[MAX_PATH];
if (SHGetFolderPath(NULL, CSIDL_STARTUP, NULL, 0, szPath) == S_OK) {
path = QString::fromWCharArray(szPath);
}
return path;
}

void setPaths(QString &targetPath, QString &startupFolder)
{
TCHAR executablePath[MAX_PATH];
GetModuleFileName(NULL, executablePath, MAX_PATH);
targetPath = QString::fromWCharArray(executablePath);

startupFolder = getStartupFolder();
}
const QString shortcutName = "HeadsetControl-Qt.lnk";
const QString applicationPath = QCoreApplication::applicationFilePath();
const QString startupPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + QDir::separator() + "Startup";
const QString shortcutPath = startupPath + QDir::separator() + shortcutName;

QString getShortcutPath()
{
static QString shortcutName = "HeadsetControl-Qt.lnk";
return getStartupFolder() + "\\" + shortcutName;
}

void createShortcut(const QString &targetPath)
void manageShortcut(bool state)
{
QString shortcutPath = getShortcutPath();
QString workingDirectory = QFileInfo(targetPath).path();

IShellLink *pShellLink = nullptr;
IPersistFile *pPersistFile = nullptr;

if (FAILED(CoInitialize(nullptr))) {
qDebug() << "Failed to initialize COM library.";
return;
}

if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void **) &pShellLink))) {
pShellLink->SetPath(targetPath.toStdWString().c_str());
pShellLink->SetWorkingDirectory(workingDirectory.toStdWString().c_str());
pShellLink->SetDescription(L"Launch HeadsetControl-Qt");

if (SUCCEEDED(pShellLink->QueryInterface(IID_IPersistFile, (void **) &pPersistFile))) {
pPersistFile->Save(shortcutPath.toStdWString().c_str(), TRUE);
pPersistFile->Release();
}

pShellLink->Release();
if (state) {
QFile::link(applicationPath, shortcutPath);
} else {
qDebug() << "Failed to create ShellLink instance.";
QFile::remove(shortcutPath);
}

CoUninitialize();
}

bool isShortcutPresent()
{
QString shortcutPath = getShortcutPath();
return QFile::exists(shortcutPath);
}

void removeShortcut()
{
QString shortcutPath = getShortcutPath();
if (isShortcutPresent()) {
QFile::remove(shortcutPath);
}
}

void manageShortcut(bool state)
{
QString targetPath;
QString startupFolder;

setPaths(targetPath, startupFolder);

if (state) {
if (!isShortcutPresent()) {
createShortcut(targetPath);
}
} else {
if (isShortcutPresent()) {
removeShortcut();
}
}
}
#endif

#ifdef __linux__
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ int main(int argc, char *argv[])
}
HeadsetControlQt w;
return a.exec();

}

0 comments on commit 6c969a5

Please sign in to comment.