utils.cpp 3.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
 * Copyright (C) 2015 by Savoir-Faire Linux                                *
 * 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 30
bool
Utils::CreateStartupLink() {
Edric Milaret's avatar
Edric Milaret committed
31
#ifdef Q_OS_WIN32
Edric Milaret's avatar
Edric Milaret committed
32 33 34 35
    TCHAR szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, MAX_PATH);

    std::wstring programPath(szPath);
36

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

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

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

bool
Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) {
Edric Milaret's avatar
Edric Milaret committed
51
#ifdef Q_OS_WIN32
52 53 54 55 56 57 58 59 60
    HRESULT hres;
    IShellLink* psl;

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

        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
72 73 74
#else
    return true;
#endif
75 76 77 78
}

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

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

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

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

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

104 105
QString
Utils::GetRingtonePath() {
Edric Milaret's avatar
Edric Milaret committed
106
#ifdef Q_OS_WIN32
107 108 109 110 111 112 113
    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
114 115 116
#else
    return QString("/usr/local");
#endif
117 118
}