blob: d4ff52d799e0649e639287a9deb847504113ea2e [file] [log] [blame]
Edric Milaret4bba46d2015-04-29 16:33:38 -04001/***************************************************************************
Edric Milaretbab169d2016-01-07 15:13:33 -05002 * Copyright (C) 2015-2016 by Savoir-faire Linux *
Edric Milaret4bba46d2015-04-29 16:33:38 -04003 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
19#include "utils.h"
20
Edric Milaretb37aa1f2015-07-09 16:39:04 -040021#ifdef Q_OS_WIN32
22#include <lmcons.h>
23#include <shobjidl.h>
24#include <shlguid.h>
25#include <shlobj.h>
26#include <shlwapi.h>
27#endif
28
Edric Milaret4bba46d2015-04-29 16:33:38 -040029bool
Edric Milaret1eca0292015-06-29 12:03:36 -040030Utils::CreateStartupLink()
31{
Edric Milaretb37aa1f2015-07-09 16:39:04 -040032#ifdef Q_OS_WIN32
Edric Milaret465a3142015-06-02 15:02:52 -040033 TCHAR szPath[MAX_PATH];
34 GetModuleFileName(NULL, szPath, MAX_PATH);
Edric Milaret4bba46d2015-04-29 16:33:38 -040035
Edric Milaret465a3142015-06-02 15:02:52 -040036 std::wstring programPath(szPath);
Edric Milaret4bba46d2015-04-29 16:33:38 -040037
Edric Milaret465a3142015-06-02 15:02:52 -040038 TCHAR startupPath[MAX_PATH];
39 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
40
41 std::wstring linkPath(startupPath);
42 linkPath += TEXT("\\Ring.lnk");
Edric Milaret4bba46d2015-04-29 16:33:38 -040043
44 return Utils::CreateLink(programPath.c_str(), linkPath.c_str());
Edric Milaretb37aa1f2015-07-09 16:39:04 -040045#else
46 return true;
47#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040048}
49
50bool
51Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) {
Edric Milaretb37aa1f2015-07-09 16:39:04 -040052#ifdef Q_OS_WIN32
Edric Milaret4bba46d2015-04-29 16:33:38 -040053 HRESULT hres;
54 IShellLink* psl;
55
56 hres = CoCreateInstance(CLSID_ShellLink, NULL,
57 CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
58 if (SUCCEEDED(hres))
59 {
60 IPersistFile* ppf;
61 psl->SetPath(lpszPathObj);
Edric Milaret90bd5a82015-06-08 10:07:59 -040062 psl->SetArguments(TEXT("--minimized"));
Edric Milaret4bba46d2015-04-29 16:33:38 -040063
64 hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
65 if (SUCCEEDED(hres))
66 {
67 hres = ppf->Save(lpszPathLink, TRUE);
68 ppf->Release();
69 }
70 psl->Release();
71 }
72 return hres;
Edric Milaretb37aa1f2015-07-09 16:39:04 -040073#else
74 return true;
75#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040076}
77
78void
79Utils::DeleteStartupLink() {
Edric Milaretb37aa1f2015-07-09 16:39:04 -040080#ifdef Q_OS_WIN32
Edric Milaret465a3142015-06-02 15:02:52 -040081 TCHAR startupPath[MAX_PATH];
82 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
Edric Milaret4bba46d2015-04-29 16:33:38 -040083
Edric Milaret465a3142015-06-02 15:02:52 -040084 std::wstring linkPath(startupPath);
85 linkPath += TEXT("\\Ring.lnk");
Edric Milaret4bba46d2015-04-29 16:33:38 -040086
87 DeleteFile(linkPath.c_str());
Edric Milaretb37aa1f2015-07-09 16:39:04 -040088#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040089}
90
91bool
92Utils::CheckStartupLink() {
Edric Milaretb37aa1f2015-07-09 16:39:04 -040093#ifdef Q_OS_WIN32
Edric Milaret465a3142015-06-02 15:02:52 -040094 TCHAR startupPath[MAX_PATH];
95 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
Edric Milaret4bba46d2015-04-29 16:33:38 -040096
Edric Milaret465a3142015-06-02 15:02:52 -040097 std::wstring linkPath(startupPath);
98 linkPath += TEXT("\\Ring.lnk");
Edric Milaret4bba46d2015-04-29 16:33:38 -040099 return PathFileExists(linkPath.c_str());
Edric Milaretb37aa1f2015-07-09 16:39:04 -0400100#else
101 return true;
102#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -0400103}
104
Edric Milaret031c3052015-04-29 18:14:18 -0400105QString
106Utils::GetRingtonePath() {
Edric Milaretb37aa1f2015-07-09 16:39:04 -0400107#ifdef Q_OS_WIN32
Edric Milaret031c3052015-04-29 18:14:18 -0400108 TCHAR workingDirectory[MAX_PATH];
109 GetCurrentDirectory(MAX_PATH, workingDirectory);
110
111 QString ringtonePath = QString::fromWCharArray(workingDirectory);
112 ringtonePath += "\\ringtones\\konga.ul";
113
114 return ringtonePath;
Edric Milaretb37aa1f2015-07-09 16:39:04 -0400115#else
116 return QString("/usr/local");
117#endif
Edric Milaret031c3052015-04-29 18:14:18 -0400118}
119
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400120QString
121Utils::GenGUID() {
122#ifdef Q_OS_WIN32
123 GUID gidReference;
124 wchar_t *str;
125 HRESULT hCreateGuid = CoCreateGuid(&gidReference);
126 if (hCreateGuid == S_OK) {
127 StringFromCLSID(gidReference, &str);
128 auto gStr = QString::fromWCharArray(str);
129 return gStr.remove("{").remove("}").toLower();
130 }
131 else
132 return QString("");
133#else
134 return QString("");
135#endif
136}
137
138QString
139Utils::GetISODate() {
140#ifdef Q_OS_WIN32
141 SYSTEMTIME lt;
142 GetSystemTime(&lt);
143 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'))
144 .arg(lt.wHour,2,10,QChar('0')).arg(lt.wMinute,2,10,QChar('0')).arg(lt.wSecond,2,10,QChar('0'));
145#else
146 return QString("");
147#endif
148}
149