Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Edric Milaret | bab169d | 2016-01-07 15:13:33 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 by Savoir-faire Linux * |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 3 | * 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 Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 21 | #ifdef Q_OS_WIN |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 22 | #include <windows.h> |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 23 | #include <lmcons.h> |
| 24 | #include <shobjidl.h> |
| 25 | #include <shlguid.h> |
| 26 | #include <shlobj.h> |
| 27 | #include <shlwapi.h> |
| 28 | #endif |
| 29 | |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 30 | |
| 31 | //Qt |
| 32 | #include <QObject> |
| 33 | #include <QErrorMessage> |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame^] | 34 | #include <QPainter> |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 35 | |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 36 | bool |
Edric Milaret | 1eca029 | 2015-06-29 12:03:36 -0400 | [diff] [blame] | 37 | Utils::CreateStartupLink() |
| 38 | { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 39 | #ifdef Q_OS_WIN |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 40 | TCHAR szPath[MAX_PATH]; |
| 41 | GetModuleFileName(NULL, szPath, MAX_PATH); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 42 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 43 | std::wstring programPath(szPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 44 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 45 | TCHAR startupPath[MAX_PATH]; |
| 46 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
| 47 | |
| 48 | std::wstring linkPath(startupPath); |
| 49 | linkPath += TEXT("\\Ring.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 50 | |
| 51 | return Utils::CreateLink(programPath.c_str(), linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 52 | #else |
| 53 | return true; |
| 54 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool |
| 58 | Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 59 | #ifdef Q_OS_WIN |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 60 | 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 Milaret | 90bd5a8 | 2015-06-08 10:07:59 -0400 | [diff] [blame] | 69 | psl->SetArguments(TEXT("--minimized")); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 70 | |
| 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 Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 80 | #else |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 81 | Q_UNUSED(lpszPathObj) |
| 82 | Q_UNUSED(lpszPathLink) |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 83 | return true; |
| 84 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | Utils::DeleteStartupLink() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 89 | #ifdef Q_OS_WIN |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 90 | TCHAR startupPath[MAX_PATH]; |
| 91 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 92 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 93 | std::wstring linkPath(startupPath); |
| 94 | linkPath += TEXT("\\Ring.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 95 | |
| 96 | DeleteFile(linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 97 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool |
| 101 | Utils::CheckStartupLink() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 102 | #ifdef Q_OS_WIN |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 103 | TCHAR startupPath[MAX_PATH]; |
| 104 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 105 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 106 | std::wstring linkPath(startupPath); |
| 107 | linkPath += TEXT("\\Ring.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 108 | return PathFileExists(linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 109 | #else |
| 110 | return true; |
| 111 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 114 | QString |
| 115 | Utils::GetRingtonePath() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 116 | #ifdef Q_OS_WIN |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 117 | TCHAR workingDirectory[MAX_PATH]; |
| 118 | GetCurrentDirectory(MAX_PATH, workingDirectory); |
| 119 | |
| 120 | QString ringtonePath = QString::fromWCharArray(workingDirectory); |
Edric Milaret | b1b00ce | 2016-02-03 14:10:05 -0500 | [diff] [blame] | 121 | ringtonePath += QStringLiteral("\\ringtones\\default.wav"); |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 122 | |
| 123 | return ringtonePath; |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 124 | #else |
| 125 | return QString("/usr/local"); |
| 126 | #endif |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 129 | QString |
| 130 | Utils::GenGUID() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 131 | #ifdef Q_OS_WIN |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 132 | 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 Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 141 | return QString(); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 142 | #else |
| 143 | return QString(""); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | QString |
| 148 | Utils::GetISODate() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 149 | #ifdef Q_OS_WIN |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 150 | SYSTEMTIME lt; |
| 151 | GetSystemTime(<); |
| 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 Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 155 | return QString(); |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | QString |
| 160 | Utils::GetCurrentUserName() { |
| 161 | #ifdef Q_OS_WIN |
| 162 | wchar_t username[UNLEN+1]; |
| 163 | DWORD username_len = UNLEN+1; |
| 164 | GetUserName(username, &username_len); |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame^] | 165 | return QString::fromWCharArray(username, username_len-1); |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 166 | #else |
| 167 | return QString(); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 168 | #endif |
| 169 | } |
| 170 | |
Edric Milaret | 4097d2f | 2016-02-09 14:41:50 -0500 | [diff] [blame] | 171 | void |
| 172 | Utils::InvokeMailto(const QString& subject, |
| 173 | const QString& body, |
| 174 | const QString& attachement) { |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 175 | #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 Milaret | 4097d2f | 2016-02-09 14:41:50 -0500 | [diff] [blame] | 188 | } |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame^] | 189 | |
| 190 | QImage |
| 191 | Utils::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 | } |