2013-02-22 01:10:32 +05:30
|
|
|
#include "include/userutils.h"
|
2013-02-13 08:33:15 +05:30
|
|
|
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
2013-02-22 01:10:32 +05:30
|
|
|
#include "include/osutils.h"
|
|
|
|
#include "include/pathutils.h"
|
2013-02-13 08:33:15 +05:30
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
HRESULT CreateLink(LPCSTR linkPath, LPCSTR targetPath, LPCSTR args)
|
2013-02-13 08:33:15 +05:30
|
|
|
{
|
2013-02-26 02:14:36 +05:30
|
|
|
HRESULT hres;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
if (!called_coinit)
|
|
|
|
{
|
|
|
|
hres = CoInitialize(NULL);
|
|
|
|
called_coinit = true;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
if (!SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
qWarning("Failed to initialize COM. Error 0x%08X", hres);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
IShellLink *link;
|
|
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
|
|
|
|
(LPVOID *)&link);
|
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
IPersistFile *persistFile;
|
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
link->SetPath(targetPath);
|
|
|
|
link->SetArguments(args);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
hres = link->QueryInterface(IID_IPersistFile, (LPVOID *)&persistFile);
|
2013-02-26 02:14:36 +05:30
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
WCHAR wstr[MAX_PATH];
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wstr, MAX_PATH);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
hres = persistFile->Save(wstr, TRUE);
|
|
|
|
persistFile->Release();
|
|
|
|
}
|
|
|
|
link->Release();
|
|
|
|
}
|
|
|
|
return hres;
|
2013-02-13 08:33:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QString Util::getDesktopDir()
|
|
|
|
{
|
2013-02-26 02:14:36 +05:30
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
|
2013-02-13 08:33:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Cross-platform Shortcut creation
|
2013-11-04 07:23:05 +05:30
|
|
|
bool Util::createShortCut(QString location, QString dest, QStringList args, QString name,
|
|
|
|
QString icon)
|
2013-02-13 08:33:15 +05:30
|
|
|
{
|
|
|
|
#if LINUX
|
2013-02-26 02:14:36 +05:30
|
|
|
location = PathCombine(location, name + ".desktop");
|
|
|
|
qDebug("location: %s", qPrintable(location));
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
QFile f(location);
|
|
|
|
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream stream(&f);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
QString argstring;
|
|
|
|
if (!args.empty())
|
|
|
|
argstring = " '" + args.join("' '") + "'";
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
stream << "[Desktop Entry]"
|
|
|
|
<< "\n";
|
|
|
|
stream << "Type=Application"
|
|
|
|
<< "\n";
|
2013-02-26 02:14:36 +05:30
|
|
|
stream << "TryExec=" << dest.toLocal8Bit() << "\n";
|
|
|
|
stream << "Exec=" << dest.toLocal8Bit() << argstring.toLocal8Bit() << "\n";
|
|
|
|
stream << "Name=" << name.toLocal8Bit() << "\n";
|
|
|
|
stream << "Icon=" << icon.toLocal8Bit() << "\n";
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
stream.flush();
|
|
|
|
f.close();
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup |
|
|
|
|
QFileDevice::ExeOther);
|
|
|
|
|
2013-02-26 02:14:36 +05:30
|
|
|
return true;
|
2013-02-13 08:33:15 +05:30
|
|
|
#elif WINDOWS
|
2013-02-26 02:14:36 +05:30
|
|
|
// TODO: Fix
|
2013-11-04 07:23:05 +05:30
|
|
|
// QFile file(PathCombine(location, name + ".lnk"));
|
|
|
|
// WCHAR *file_w;
|
|
|
|
// WCHAR *dest_w;
|
|
|
|
// WCHAR *args_w;
|
|
|
|
// file.fileName().toWCharArray(file_w);
|
|
|
|
// dest.toWCharArray(dest_w);
|
|
|
|
|
|
|
|
// QString argStr;
|
|
|
|
// for (int i = 0; i < args.count(); i++)
|
|
|
|
// {
|
|
|
|
// argStr.append(args[i]);
|
|
|
|
// argStr.append(" ");
|
|
|
|
// }
|
|
|
|
// argStr.toWCharArray(args_w);
|
|
|
|
|
|
|
|
// return SUCCEEDED(CreateLink(file_w, dest_w, args_w));
|
2013-02-26 02:14:36 +05:30
|
|
|
return false;
|
2013-02-13 08:33:15 +05:30
|
|
|
#else
|
2013-02-26 02:14:36 +05:30
|
|
|
qWarning("Desktop Shortcuts not supported on your platform!");
|
|
|
|
return false;
|
2013-02-13 08:33:15 +05:30
|
|
|
#endif
|
|
|
|
}
|