implement desktop shortcut creation. windows code not tested.

This commit is contained in:
Orochimarufan 2013-02-13 04:03:15 +01:00
parent c755195b97
commit 369b1c55c9
6 changed files with 152 additions and 4 deletions

View File

@ -110,6 +110,7 @@ gui/browserdialog.cpp
util/pathutils.cpp util/pathutils.cpp
util/osutils.cpp util/osutils.cpp
util/userutil.cpp
java/javautils.cpp java/javautils.cpp
java/annotations.cpp java/annotations.cpp
@ -139,6 +140,7 @@ data/loginresponse.h
util/apputils.h util/apputils.h
util/pathutils.h util/pathutils.h
util/osutils.h util/osutils.h
util/userutil.h
multimc_pragma.h multimc_pragma.h

View File

@ -34,7 +34,7 @@ void BrowserDialog::on_btnForward_clicked()
void BrowserDialog::on_webView_urlChanged(const QUrl &url) void BrowserDialog::on_webView_urlChanged(const QUrl &url)
{ {
Q_UNUSED(url); Q_UNUSED(url);
qDebug("urlChanged"); //qDebug("urlChanged");
ui->btnBack->setEnabled(ui->webView->history()->canGoBack()); ui->btnBack->setEnabled(ui->webView->history()->canGoBack());
ui->btnForward->setEnabled(ui->webView->history()->canGoForward()); ui->btnForward->setEnabled(ui->webView->history()->canGoForward());
} }
@ -42,7 +42,7 @@ void BrowserDialog::on_webView_urlChanged(const QUrl &url)
// Window Title Magic // Window Title Magic
void BrowserDialog::refreshWindowTitle() void BrowserDialog::refreshWindowTitle()
{ {
qDebug("refreshTitle"); //qDebug("refreshTitle");
if (m_pageTitleInWindowTitle) if (m_pageTitleInWindowTitle)
setWindowTitle(m_windowTitleFormat.arg(ui->webView->title())); setWindowTitle(m_windowTitleFormat.arg(ui->webView->title()));
else else
@ -63,7 +63,7 @@ void BrowserDialog::setWindowTitleFormat(QString format)
void BrowserDialog::on_webView_titleChanged(const QString &title) void BrowserDialog::on_webView_titleChanged(const QString &title)
{ {
qDebug("titleChanged"); //qDebug("titleChanged");
if (m_pageTitleInWindowTitle) if (m_pageTitleInWindowTitle)
setWindowTitle(m_windowTitleFormat.arg(title)); setWindowTitle(m_windowTitleFormat.arg(title));
} }
@ -71,6 +71,6 @@ void BrowserDialog::on_webView_titleChanged(const QString &title)
// Public access Methods // Public access Methods
void BrowserDialog::load(const QUrl &url) void BrowserDialog::load(const QUrl &url)
{ {
qDebug("load"); //qDebug("load");
ui->webView->setUrl(url); ui->webView->setUrl(url);
} }

View File

@ -18,11 +18,14 @@
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QInputDialog>
#include <QDesktopServices> #include <QDesktopServices>
#include <QUrl> #include <QUrl>
#include "util/osutils.h" #include "util/osutils.h"
#include "util/userutil.h"
#include "util/pathutils.h"
#include "gui/settingsdialog.h" #include "gui/settingsdialog.h"
#include "gui/newinstancedialog.h" #include "gui/newinstancedialog.h"
@ -159,6 +162,17 @@ void MainWindow::onLoginComplete(LoginResponse response)
arg(response.getUsername(), response.getSessionID())); 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 // BrowserDialog
void MainWindow::openWebPage(QUrl url) void MainWindow::openWebPage(QUrl url)
{ {

View File

@ -64,6 +64,8 @@ private slots:
void on_actionLaunchInstance_triggered(); void on_actionLaunchInstance_triggered();
void on_actionMakeDesktopShortcut_triggered();
void doLogin(const QString& errorMsg = ""); void doLogin(const QString& errorMsg = "");

113
util/userutil.cpp Normal file
View File

@ -0,0 +1,113 @@
#include "userutil.h"
#include <QStandardPaths>
#include <QFile>
#include <QTextStream>
#include "osutils.h"
#include "pathutils.h"
// Win32 crap
#if WINDOWS
#include <windows.h>
#include <winnls.h>
#include <shobjidl.h>
#include <objbase.h>
#include <objidl.h>
#include <shlguid.h>
#include <shlobj.h>
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
}

17
util/userutil.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef USERUTIL_H
#define USERUTIL_H
#include <QString>
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