blob: cc45a9f5e55df4584458e1e392f8a28a369e0056 [file] [log] [blame]
Edric Milaret4bba46d2015-04-29 16:33:38 -04001/***************************************************************************
Anthony LĂ©onard2fde81d2017-04-17 10:06:55 -04002 * Copyright (C) 2015-2017 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 Milaret36587362016-02-04 12:30:52 -050021#ifdef Q_OS_WIN
Edric Milaret3ba22ca2016-03-01 13:28:01 -050022#include <windows.h>
Edric Milaretb37aa1f2015-07-09 16:39:04 -040023#include <lmcons.h>
24#include <shobjidl.h>
25#include <shlguid.h>
26#include <shlobj.h>
27#include <shlwapi.h>
28#endif
29
Edric Milaret3ba22ca2016-03-01 13:28:01 -050030
31//Qt
32#include <QObject>
33#include <QErrorMessage>
Edric Milaret25236d92016-03-28 09:40:58 -040034#include <QPainter>
Edric Milaret3ba22ca2016-03-01 13:28:01 -050035
Edric Milaret4bba46d2015-04-29 16:33:38 -040036bool
Edric Milaret1eca0292015-06-29 12:03:36 -040037Utils::CreateStartupLink()
38{
Edric Milaret36587362016-02-04 12:30:52 -050039#ifdef Q_OS_WIN
Edric Milaret465a3142015-06-02 15:02:52 -040040 TCHAR szPath[MAX_PATH];
41 GetModuleFileName(NULL, szPath, MAX_PATH);
Edric Milaret4bba46d2015-04-29 16:33:38 -040042
Edric Milaret465a3142015-06-02 15:02:52 -040043 std::wstring programPath(szPath);
Edric Milaret4bba46d2015-04-29 16:33:38 -040044
Edric Milaret465a3142015-06-02 15:02:52 -040045 TCHAR startupPath[MAX_PATH];
46 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
47
48 std::wstring linkPath(startupPath);
49 linkPath += TEXT("\\Ring.lnk");
Edric Milaret4bba46d2015-04-29 16:33:38 -040050
51 return Utils::CreateLink(programPath.c_str(), linkPath.c_str());
Edric Milaretb37aa1f2015-07-09 16:39:04 -040052#else
53 return true;
54#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040055}
56
57bool
58Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) {
Edric Milaret36587362016-02-04 12:30:52 -050059#ifdef Q_OS_WIN
Edric Milaret4bba46d2015-04-29 16:33:38 -040060 HRESULT hres;
61 IShellLink* psl;
62
63 hres = CoCreateInstance(CLSID_ShellLink, NULL,
64 CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
65 if (SUCCEEDED(hres))
66 {
67 IPersistFile* ppf;
68 psl->SetPath(lpszPathObj);
Edric Milaret90bd5a82015-06-08 10:07:59 -040069 psl->SetArguments(TEXT("--minimized"));
Edric Milaret4bba46d2015-04-29 16:33:38 -040070
71 hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
72 if (SUCCEEDED(hres))
73 {
74 hres = ppf->Save(lpszPathLink, TRUE);
75 ppf->Release();
76 }
77 psl->Release();
78 }
79 return hres;
Edric Milaretb37aa1f2015-07-09 16:39:04 -040080#else
Edric Milaret36587362016-02-04 12:30:52 -050081 Q_UNUSED(lpszPathObj)
82 Q_UNUSED(lpszPathLink)
Edric Milaretb37aa1f2015-07-09 16:39:04 -040083 return true;
84#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040085}
86
87void
88Utils::DeleteStartupLink() {
Edric Milaret36587362016-02-04 12:30:52 -050089#ifdef Q_OS_WIN
Edric Milaret465a3142015-06-02 15:02:52 -040090 TCHAR startupPath[MAX_PATH];
91 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
Edric Milaret4bba46d2015-04-29 16:33:38 -040092
Edric Milaret465a3142015-06-02 15:02:52 -040093 std::wstring linkPath(startupPath);
94 linkPath += TEXT("\\Ring.lnk");
Edric Milaret4bba46d2015-04-29 16:33:38 -040095
96 DeleteFile(linkPath.c_str());
Edric Milaretb37aa1f2015-07-09 16:39:04 -040097#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040098}
99
100bool
101Utils::CheckStartupLink() {
Edric Milaret36587362016-02-04 12:30:52 -0500102#ifdef Q_OS_WIN
Edric Milaret465a3142015-06-02 15:02:52 -0400103 TCHAR startupPath[MAX_PATH];
104 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath);
Edric Milaret4bba46d2015-04-29 16:33:38 -0400105
Edric Milaret465a3142015-06-02 15:02:52 -0400106 std::wstring linkPath(startupPath);
107 linkPath += TEXT("\\Ring.lnk");
Edric Milaret4bba46d2015-04-29 16:33:38 -0400108 return PathFileExists(linkPath.c_str());
Edric Milaretb37aa1f2015-07-09 16:39:04 -0400109#else
110 return true;
111#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -0400112}
113
Edric Milaret031c3052015-04-29 18:14:18 -0400114QString
115Utils::GetRingtonePath() {
Edric Milaret36587362016-02-04 12:30:52 -0500116#ifdef Q_OS_WIN
Edric Milaret031c3052015-04-29 18:14:18 -0400117 TCHAR workingDirectory[MAX_PATH];
118 GetCurrentDirectory(MAX_PATH, workingDirectory);
119
120 QString ringtonePath = QString::fromWCharArray(workingDirectory);
Edric Milaretb1b00ce2016-02-03 14:10:05 -0500121 ringtonePath += QStringLiteral("\\ringtones\\default.wav");
Edric Milaret031c3052015-04-29 18:14:18 -0400122
123 return ringtonePath;
Edric Milaretb37aa1f2015-07-09 16:39:04 -0400124#else
125 return QString("/usr/local");
126#endif
Edric Milaret031c3052015-04-29 18:14:18 -0400127}
128
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400129QString
130Utils::GenGUID() {
Edric Milaret36587362016-02-04 12:30:52 -0500131#ifdef Q_OS_WIN
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400132 GUID gidReference;
133 wchar_t *str;
134 HRESULT hCreateGuid = CoCreateGuid(&gidReference);
135 if (hCreateGuid == S_OK) {
136 StringFromCLSID(gidReference, &str);
137 auto gStr = QString::fromWCharArray(str);
138 return gStr.remove("{").remove("}").toLower();
139 }
140 else
Edric Milaret36587362016-02-04 12:30:52 -0500141 return QString();
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400142#else
143 return QString("");
144#endif
145}
146
147QString
148Utils::GetISODate() {
Edric Milaret36587362016-02-04 12:30:52 -0500149#ifdef Q_OS_WIN
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400150 SYSTEMTIME lt;
151 GetSystemTime(&lt);
152 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'))
153 .arg(lt.wHour,2,10,QChar('0')).arg(lt.wMinute,2,10,QChar('0')).arg(lt.wSecond,2,10,QChar('0'));
154#else
Edric Milaret36587362016-02-04 12:30:52 -0500155 return QString();
156#endif
157}
158
159QString
160Utils::GetCurrentUserName() {
161#ifdef Q_OS_WIN
162 wchar_t username[UNLEN+1];
163 DWORD username_len = UNLEN+1;
164 GetUserName(username, &username_len);
Edric Milaret25236d92016-03-28 09:40:58 -0400165 return QString::fromWCharArray(username, username_len-1);
Edric Milaret36587362016-02-04 12:30:52 -0500166#else
167 return QString();
Edric Milaret43f3c1e2015-07-16 17:52:47 -0400168#endif
169}
170
Edric Milaret4097d2f2016-02-09 14:41:50 -0500171void
172Utils::InvokeMailto(const QString& subject,
173 const QString& body,
174 const QString& attachement) {
Edric Milaret3ba22ca2016-03-01 13:28:01 -0500175#ifdef Q_OS_WIN
176 HKEY hKey;
177 LONG lRes = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"mailto", 0, KEY_READ, &hKey);
178 if (lRes != ERROR_FILE_NOT_FOUND) {
179 auto addr = QString("mailto:?subject=%1&body=%2").arg(subject).arg(body);
180 if (not attachement.isEmpty())
181 addr += QString("&attachement=%1").arg(attachement);
182 ShellExecute(nullptr, L"open", addr.toStdWString().c_str(), NULL, NULL, SW_SHOWNORMAL);
183 } else {
184 QErrorMessage errorMessage;
185 errorMessage.showMessage(QObject::tr("No default mail client found"));
186 }
187#endif
Edric Milaret4097d2f2016-02-09 14:41:50 -0500188}
Edric Milaret25236d92016-03-28 09:40:58 -0400189
190QImage
191Utils::getCirclePhoto(const QImage original, int sizePhoto)
192{
193 QImage target(sizePhoto, sizePhoto, QImage::Format_ARGB32_Premultiplied);
194 target.fill(Qt::transparent);
195
196 QPainter painter(&target);
197 painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
198 painter.setBrush(QBrush(Qt::white));
199 auto scaledPhoto = original
200 .scaled(sizePhoto, sizePhoto, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)
201 .convertToFormat(QImage::Format_ARGB32_Premultiplied);
202 int margin = 0;
203 if (scaledPhoto.width() > sizePhoto) {
204 margin = (scaledPhoto.width() - sizePhoto) / 2;
205 }
206 painter.drawEllipse(0, 0, sizePhoto, sizePhoto);
207 painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
208 painter.drawImage(0, 0, scaledPhoto, margin, 0);
209 return target;
210}