utils.cpp 4.15 KB
Newer Older
1
/***************************************************************************
Edric Milaret's avatar
Edric Milaret committed
2
 * Copyright (C) 2015 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"

Edric Milaret's avatar
Edric Milaret committed
21 22 23 24 25 26 27 28
#ifdef Q_OS_WIN32
#include <lmcons.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <shlobj.h>
#include <shlwapi.h>
#endif

29
bool
30 31
Utils::CreateStartupLink()
{
Edric Milaret's avatar
Edric Milaret committed
32
#ifdef Q_OS_WIN32
Edric Milaret's avatar
Edric Milaret committed
33 34 35 36
    TCHAR szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, MAX_PATH);

    std::wstring programPath(szPath);
37

Edric Milaret's avatar
Edric Milaret committed
38 39
    TCHAR startupPath[MAX_PATH];
    SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
40

Edric Milaret's avatar
Edric Milaret committed
41 42
    std::wstring linkPath(startupPath);
    linkPath += TEXT("\\Ring.lnk");
43 44

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

bool
Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) {
Edric Milaret's avatar
Edric Milaret committed
52
#ifdef Q_OS_WIN32
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;
Edric Milaret's avatar
Edric Milaret committed
73 74 75
#else
    return true;
#endif
76 77 78 79
}

void
Utils::DeleteStartupLink() {
Edric Milaret's avatar
Edric Milaret committed
80
#ifdef Q_OS_WIN32
Edric Milaret's avatar
Edric Milaret committed
81 82
    TCHAR startupPath[MAX_PATH];
    SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
83

Edric Milaret's avatar
Edric Milaret committed
84 85
    std::wstring linkPath(startupPath);
    linkPath += TEXT("\\Ring.lnk");
86 87

    DeleteFile(linkPath.c_str());
Edric Milaret's avatar
Edric Milaret committed
88
#endif
89 90 91 92
}

bool
Utils::CheckStartupLink() {
Edric Milaret's avatar
Edric Milaret committed
93
#ifdef Q_OS_WIN32
Edric Milaret's avatar
Edric Milaret committed
94 95
    TCHAR startupPath[MAX_PATH];
    SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
96

Edric Milaret's avatar
Edric Milaret committed
97 98
    std::wstring linkPath(startupPath);
    linkPath += TEXT("\\Ring.lnk");
99
    return PathFileExists(linkPath.c_str());
Edric Milaret's avatar
Edric Milaret committed
100 101 102
#else
    return true;
#endif
103 104
}

105 106
QString
Utils::GetRingtonePath() {
Edric Milaret's avatar
Edric Milaret committed
107
#ifdef Q_OS_WIN32
108 109 110 111 112 113 114
    TCHAR workingDirectory[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, workingDirectory);

    QString ringtonePath = QString::fromWCharArray(workingDirectory);
    ringtonePath += "\\ringtones\\konga.ul";

    return ringtonePath;
Edric Milaret's avatar
Edric Milaret committed
115 116 117
#else
    return QString("/usr/local");
#endif
118 119
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
QString
Utils::GenGUID() {
#ifdef Q_OS_WIN32
    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
        return QString("");
#else
    return QString("");
#endif
}

QString
Utils::GetISODate() {
#ifdef Q_OS_WIN32
    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
    return QString("");
#endif
}