From 369b1c55c99aa8bdcd2d57ab4aad3633343f1417 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Wed, 13 Feb 2013 04:03:15 +0100 Subject: [PATCH] implement desktop shortcut creation. windows code not tested. --- CMakeLists.txt | 2 + gui/browserdialog.cpp | 8 +-- gui/mainwindow.cpp | 14 ++++++ gui/mainwindow.h | 2 + util/userutil.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++ util/userutil.h | 17 +++++++ 6 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 util/userutil.cpp create mode 100644 util/userutil.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bbac3bdba..95a16f606 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ gui/browserdialog.cpp util/pathutils.cpp util/osutils.cpp +util/userutil.cpp java/javautils.cpp java/annotations.cpp @@ -139,6 +140,7 @@ data/loginresponse.h util/apputils.h util/pathutils.h util/osutils.h +util/userutil.h multimc_pragma.h diff --git a/gui/browserdialog.cpp b/gui/browserdialog.cpp index 58f185ced..40c50c3fe 100644 --- a/gui/browserdialog.cpp +++ b/gui/browserdialog.cpp @@ -34,7 +34,7 @@ void BrowserDialog::on_btnForward_clicked() void BrowserDialog::on_webView_urlChanged(const QUrl &url) { Q_UNUSED(url); - qDebug("urlChanged"); + //qDebug("urlChanged"); ui->btnBack->setEnabled(ui->webView->history()->canGoBack()); ui->btnForward->setEnabled(ui->webView->history()->canGoForward()); } @@ -42,7 +42,7 @@ void BrowserDialog::on_webView_urlChanged(const QUrl &url) // Window Title Magic void BrowserDialog::refreshWindowTitle() { - qDebug("refreshTitle"); + //qDebug("refreshTitle"); if (m_pageTitleInWindowTitle) setWindowTitle(m_windowTitleFormat.arg(ui->webView->title())); else @@ -63,7 +63,7 @@ void BrowserDialog::setWindowTitleFormat(QString format) void BrowserDialog::on_webView_titleChanged(const QString &title) { - qDebug("titleChanged"); + //qDebug("titleChanged"); if (m_pageTitleInWindowTitle) setWindowTitle(m_windowTitleFormat.arg(title)); } @@ -71,6 +71,6 @@ void BrowserDialog::on_webView_titleChanged(const QString &title) // Public access Methods void BrowserDialog::load(const QUrl &url) { - qDebug("load"); + //qDebug("load"); ui->webView->setUrl(url); } diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 271a67fbb..9bbc4c38f 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -18,11 +18,14 @@ #include #include +#include #include #include #include "util/osutils.h" +#include "util/userutil.h" +#include "util/pathutils.h" #include "gui/settingsdialog.h" #include "gui/newinstancedialog.h" @@ -159,6 +162,17 @@ void MainWindow::onLoginComplete(LoginResponse response) arg(response.getUsername(), response.getSessionID())); } +// Create A Desktop Shortcut +void MainWindow::on_actionMakeDesktopShortcut_triggered() +{ + QString name("Test"); + name = QInputDialog::getText(this, tr("MultiMC Shortcut"), tr("Enter a Shortcut Name."), QLineEdit::Normal, name); + + Util::createShortCut(Util::getDesktopDir(), "test", QStringList() << "-d" << "lol", name, "application-x-octet-stream"); + + QMessageBox::warning(this, "Stupidness", "A Dummy Shortcut was created. the current instance model doesnt allow for anything more"); +} + // BrowserDialog void MainWindow::openWebPage(QUrl url) { diff --git a/gui/mainwindow.h b/gui/mainwindow.h index cf6a9dbc2..f2dfbc70c 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -64,6 +64,8 @@ private slots: void on_actionLaunchInstance_triggered(); + + void on_actionMakeDesktopShortcut_triggered(); void doLogin(const QString& errorMsg = ""); diff --git a/util/userutil.cpp b/util/userutil.cpp new file mode 100644 index 000000000..9a7b2e12a --- /dev/null +++ b/util/userutil.cpp @@ -0,0 +1,113 @@ +#include "userutil.h" + +#include +#include +#include + +#include "osutils.h" +#include "pathutils.h" + +// Win32 crap +#if WINDOWS + +#include +#include +#include +#include +#include +#include +#include + +bool called_coinit = false; + +HRESULT CreateLink(LPCSTR linkPath, LPCWSTR targetPath, LPCWSTR args) +{ + HRESULT hres; + + if (!called_coinit) + { + hres = CoInitialize(NULL); + called_coinit = true; + + if (!SUCCEEDED(hres)) + { + qWarning("Failed to initialize COM. Error 0x%08X", hres); + return hres; + } + } + + + IShellLink* link; + hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&link); + + if (SUCCEEDED(hres)) + { + IPersistFile* persistFile; + + link->SetPath(targetPath); + link->SetArguments(args); + + hres = link->QueryInterface(IID_IPersistFile, (LPVOID*)&persistFile); + if (SUCCEEDED(hres)) + { + WCHAR wstr[MAX_PATH]; + + MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wstr, MAX_PATH); + + hres = persistFile->Save(wstr, TRUE); + persistFile->Release(); + } + link->Release(); + } + return hres; +} + +#endif + +QString Util::getDesktopDir() +{ + return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); +} + +// Cross-platform Shortcut creation +bool Util::createShortCut(QString location, QString dest, QStringList args, QString name, QString icon) +{ +#if LINUX + location = PathCombine(location, name + ".desktop"); + qDebug("location: %s", qPrintable(location)); + + QFile f(location); + f.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream stream(&f); + + QString argstring; + if (!args.empty()) + argstring = " '" + args.join("' '") + "'"; + + stream << "[Desktop Entry]" << "\n"; + stream << "Type=Application" << "\n"; + stream << "TryExec=" << dest.toLocal8Bit() << "\n"; + stream << "Exec=" << dest.toLocal8Bit() << argstring.toLocal8Bit() << "\n"; + stream << "Name=" << name.toLocal8Bit() << "\n"; + stream << "Icon=" << icon.toLocal8Bit() << "\n"; + + stream.flush(); + f.close(); + + f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup | QFileDevice::ExeOther); + + return true; +#elif WINDOWS + QFile file(path, name + ".lnk"); + WCHAR *file_w; + WCHAR *dest_w; + WCHAR *args_w; + file.fileName().toWCharArray(file_w); + dest.toWCharArray(dest_w); + args.toWCharArray(args_w); + return SUCCEEDED(CreateLink(file_w, dest_w, args_w)); +#else + qWarning("Desktop Shortcuts not supported on your platform!"); + return false; +#endif +} diff --git a/util/userutil.h b/util/userutil.h new file mode 100644 index 000000000..11c13ed08 --- /dev/null +++ b/util/userutil.h @@ -0,0 +1,17 @@ +#ifndef USERUTIL_H +#define USERUTIL_H + +#include + +namespace Util +{ + // Get the Directory representing the User's Desktop + QString getDesktopDir(); + + // Create a shortcut at *location*, pointing to *dest* called with the arguments *args* + // call it *name* and assign it the icon *icon* + // return true if operation succeeded + bool createShortCut(QString location, QString dest, QStringList args, QString name, QString iconLocation); +} + +#endif // USERUTIL_H