summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorOrochimarufan <orochimarufan.x3@gmail.com>2013-02-13 04:03:15 +0100
committerOrochimarufan <orochimarufan.x3@gmail.com>2013-02-13 04:03:15 +0100
commit369b1c55c99aa8bdcd2d57ab4aad3633343f1417 (patch)
treea5d772ab16c53ac8e69dcc7e92bd3b6b48c6ef73 /util
parentc755195b9721ad9f040684c3be1e93178f95c2cf (diff)
downloadMultiMC-369b1c55c99aa8bdcd2d57ab4aad3633343f1417.tar
MultiMC-369b1c55c99aa8bdcd2d57ab4aad3633343f1417.tar.gz
MultiMC-369b1c55c99aa8bdcd2d57ab4aad3633343f1417.tar.lz
MultiMC-369b1c55c99aa8bdcd2d57ab4aad3633343f1417.tar.xz
MultiMC-369b1c55c99aa8bdcd2d57ab4aad3633343f1417.zip
implement desktop shortcut creation. windows code not tested.
Diffstat (limited to 'util')
-rw-r--r--util/userutil.cpp113
-rw-r--r--util/userutil.h17
2 files changed, 130 insertions, 0 deletions
diff --git a/util/userutil.cpp b/util/userutil.cpp
new file mode 100644
index 00000000..9a7b2e12
--- /dev/null
+++ b/util/userutil.cpp
@@ -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
+}
diff --git a/util/userutil.h b/util/userutil.h
new file mode 100644
index 00000000..11c13ed0
--- /dev/null
+++ b/util/userutil.h
@@ -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