utils.cpp 4.46 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "utils.h"

21
#ifdef Q_OS_WIN
22 23 24 25 26 27 28
#include <lmcons.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <shlobj.h>
#include <shlwapi.h>
#endif

29
bool
30 31
Utils::CreateStartupLink()
{
32
#ifdef Q_OS_WIN
33 34 35 36
    TCHAR szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, MAX_PATH);

    std::wstring programPath(szPath);
37

38 39
    TCHAR startupPath[MAX_PATH];
    SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
40

41 42
    std::wstring linkPath(startupPath);
    linkPath += TEXT("\\Ring.lnk");
43 44

    return Utils::CreateLink(programPath.c_str(), linkPath.c_str());
45 46 47
#else
    return true;
#endif
48 49 50 51
}

bool
Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) {
52
#ifdef Q_OS_WIN
53 54 55 56 57 58 59 60 61
    HRESULT hres;
    IShellLink* psl;

    hres = CoCreateInstance(CLSID_ShellLink, NULL,
                            CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf;
        psl->SetPath(lpszPathObj);
62
        psl->SetArguments(TEXT("--minimized"));
63 64 65 66 67 68 69 70 71 72

        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
        if (SUCCEEDED(hres))
        {
            hres = ppf->Save(lpszPathLink, TRUE);
            ppf->Release();
        }
        psl->Release();
    }
    return hres;
73
#else
74 75
    Q_UNUSED(lpszPathObj)
    Q_UNUSED(lpszPathLink)
76 77
    return true;
#endif
78 79 80 81
}

void
Utils::DeleteStartupLink() {
82
#ifdef Q_OS_WIN
83 84
    TCHAR startupPath[MAX_PATH];
    SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
85

86 87
    std::wstring linkPath(startupPath);
    linkPath += TEXT("\\Ring.lnk");
88 89

    DeleteFile(linkPath.c_str());
90
#endif
91 92 93 94
}

bool
Utils::CheckStartupLink() {
95
#ifdef Q_OS_WIN
96 97
    TCHAR startupPath[MAX_PATH];
    SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
98

99 100
    std::wstring linkPath(startupPath);
    linkPath += TEXT("\\Ring.lnk");
101
    return PathFileExists(linkPath.c_str());
102 103 104
#else
    return true;
#endif
105 106
}

107 108
QString
Utils::GetRingtonePath() {
109
#ifdef Q_OS_WIN
110 111 112 113
    TCHAR workingDirectory[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, workingDirectory);

    QString ringtonePath = QString::fromWCharArray(workingDirectory);
114
    ringtonePath += QStringLiteral("\\ringtones\\default.wav");
115 116

    return ringtonePath;
117 118 119
#else
    return QString("/usr/local");
#endif
120 121
}

122 123
QString
Utils::GenGUID() {
124
#ifdef Q_OS_WIN
125 126 127 128 129 130 131 132 133
    GUID gidReference;
    wchar_t *str;
    HRESULT hCreateGuid = CoCreateGuid(&gidReference);
    if (hCreateGuid == S_OK) {
        StringFromCLSID(gidReference, &str);
        auto gStr = QString::fromWCharArray(str);
        return gStr.remove("{").remove("}").toLower();
    }
    else
134
        return QString();
135 136 137 138 139 140 141
#else
    return QString("");
#endif
}

QString
Utils::GetISODate() {
142
#ifdef Q_OS_WIN
143 144 145 146 147
    SYSTEMTIME lt;
    GetSystemTime(&lt);
    return QString("%1-%2-%3T%4:%5:%6Z").arg(lt.wYear).arg(lt.wMonth,2,10,QChar('0')).arg(lt.wDay,2,10,QChar('0'))
            .arg(lt.wHour,2,10,QChar('0')).arg(lt.wMinute,2,10,QChar('0')).arg(lt.wSecond,2,10,QChar('0'));
#else
148 149 150 151 152 153 154 155 156 157 158 159 160
    return QString();
#endif
}

QString
Utils::GetCurrentUserName() {
#ifdef Q_OS_WIN
    wchar_t username[UNLEN+1];
    DWORD username_len = UNLEN+1;
    GetUserName(username, &username_len);
    return QString::fromWCharArray(username, username_len);
#else
    return QString();
161 162 163
#endif
}