summaryrefslogtreecommitdiffstats
path: root/util/userutil.cpp
blob: 9a7b2e12ae158f29d9b164d70849aa3aac503faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
}