Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Sébastien Blin | 68abac9 | 2019-01-02 17:41:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2019 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>* |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 4 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> * |
Isa Nanic | 601de1d | 2018-10-23 11:37:26 -0400 | [diff] [blame] | 5 | * Author: Isa Nanic <isa.nanic@savoirfairelinux.com * |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 6 | * * |
| 7 | * This program is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 3 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * This program is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 19 | **************************************************************************/ |
| 20 | |
| 21 | #include "utils.h" |
| 22 | |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 23 | #ifdef Q_OS_WIN |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 24 | #include <windows.h> |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 25 | #include <lmcons.h> |
| 26 | #include <shobjidl.h> |
| 27 | #include <shlguid.h> |
| 28 | #include <shlobj.h> |
| 29 | #include <shlwapi.h> |
| 30 | #endif |
| 31 | |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 32 | //Qt |
| 33 | #include <QObject> |
| 34 | #include <QErrorMessage> |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 35 | #include <QPainter> |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 36 | #include <QStackedWidget> |
| 37 | #include <QPropertyAnimation> |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 38 | #include <QApplication> |
Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 39 | #include <QFile> |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 40 | |
| 41 | #include "globalinstances.h" |
| 42 | #include "pixbufmanipulator.h" |
| 43 | |
| 44 | #include "globalsystemtray.h" |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 45 | #include "lrcinstance.h" |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 46 | |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 47 | bool |
Edric Milaret | 1eca029 | 2015-06-29 12:03:36 -0400 | [diff] [blame] | 48 | Utils::CreateStartupLink() |
| 49 | { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 50 | #ifdef Q_OS_WIN |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 51 | TCHAR szPath[MAX_PATH]; |
| 52 | GetModuleFileName(NULL, szPath, MAX_PATH); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 53 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 54 | std::wstring programPath(szPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 55 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 56 | TCHAR startupPath[MAX_PATH]; |
| 57 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
| 58 | |
| 59 | std::wstring linkPath(startupPath); |
Sébastien Blin | cba5b52 | 2018-12-10 12:48:36 -0500 | [diff] [blame] | 60 | linkPath += TEXT("\\Jami.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 61 | |
| 62 | return Utils::CreateLink(programPath.c_str(), linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 63 | #else |
| 64 | return true; |
| 65 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool |
| 69 | Utils::CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink) { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 70 | #ifdef Q_OS_WIN |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 71 | HRESULT hres; |
| 72 | IShellLink* psl; |
| 73 | |
| 74 | hres = CoCreateInstance(CLSID_ShellLink, NULL, |
| 75 | CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); |
| 76 | if (SUCCEEDED(hres)) |
| 77 | { |
| 78 | IPersistFile* ppf; |
| 79 | psl->SetPath(lpszPathObj); |
Edric Milaret | 90bd5a8 | 2015-06-08 10:07:59 -0400 | [diff] [blame] | 80 | psl->SetArguments(TEXT("--minimized")); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 81 | |
| 82 | hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); |
| 83 | if (SUCCEEDED(hres)) |
| 84 | { |
| 85 | hres = ppf->Save(lpszPathLink, TRUE); |
| 86 | ppf->Release(); |
| 87 | } |
| 88 | psl->Release(); |
| 89 | } |
| 90 | return hres; |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 91 | #else |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 92 | Q_UNUSED(lpszPathObj) |
| 93 | Q_UNUSED(lpszPathLink) |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 94 | return true; |
| 95 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | Utils::DeleteStartupLink() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 100 | #ifdef Q_OS_WIN |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 101 | TCHAR startupPath[MAX_PATH]; |
| 102 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 103 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 104 | std::wstring linkPath(startupPath); |
Sébastien Blin | cba5b52 | 2018-12-10 12:48:36 -0500 | [diff] [blame] | 105 | linkPath += TEXT("\\Jami.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 106 | |
| 107 | DeleteFile(linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 108 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool |
| 112 | Utils::CheckStartupLink() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 113 | #ifdef Q_OS_WIN |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 114 | TCHAR startupPath[MAX_PATH]; |
| 115 | SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, startupPath); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 116 | |
Edric Milaret | 465a314 | 2015-06-02 15:02:52 -0400 | [diff] [blame] | 117 | std::wstring linkPath(startupPath); |
Sébastien Blin | cba5b52 | 2018-12-10 12:48:36 -0500 | [diff] [blame] | 118 | linkPath += TEXT("\\Jami.lnk"); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 119 | return PathFileExists(linkPath.c_str()); |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 120 | #else |
| 121 | return true; |
| 122 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 125 | QString |
| 126 | Utils::GetRingtonePath() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 127 | #ifdef Q_OS_WIN |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 128 | TCHAR workingDirectory[MAX_PATH]; |
| 129 | GetCurrentDirectory(MAX_PATH, workingDirectory); |
| 130 | |
| 131 | QString ringtonePath = QString::fromWCharArray(workingDirectory); |
Andreas Traczyk | 3d0276c | 2019-01-02 12:00:07 -0500 | [diff] [blame] | 132 | ringtonePath += QStringLiteral("\\ringtones\\default.opus"); |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 133 | return ringtonePath; |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 134 | #else |
| 135 | return QString("/usr/local"); |
| 136 | #endif |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 139 | QString |
| 140 | Utils::GenGUID() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 141 | #ifdef Q_OS_WIN |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 142 | GUID gidReference; |
| 143 | wchar_t *str; |
| 144 | HRESULT hCreateGuid = CoCreateGuid(&gidReference); |
| 145 | if (hCreateGuid == S_OK) { |
| 146 | StringFromCLSID(gidReference, &str); |
| 147 | auto gStr = QString::fromWCharArray(str); |
| 148 | return gStr.remove("{").remove("}").toLower(); |
| 149 | } |
| 150 | else |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 151 | return QString(); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 152 | #else |
| 153 | return QString(""); |
| 154 | #endif |
| 155 | } |
| 156 | |
| 157 | QString |
| 158 | Utils::GetISODate() { |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 159 | #ifdef Q_OS_WIN |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 160 | SYSTEMTIME lt; |
| 161 | GetSystemTime(<); |
| 162 | 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')) |
| 163 | .arg(lt.wHour,2,10,QChar('0')).arg(lt.wMinute,2,10,QChar('0')).arg(lt.wSecond,2,10,QChar('0')); |
| 164 | #else |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 165 | return QString(); |
| 166 | #endif |
| 167 | } |
| 168 | |
| 169 | QString |
| 170 | Utils::GetCurrentUserName() { |
| 171 | #ifdef Q_OS_WIN |
| 172 | wchar_t username[UNLEN+1]; |
| 173 | DWORD username_len = UNLEN+1; |
| 174 | GetUserName(username, &username_len); |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 175 | return QString::fromWCharArray(username, username_len-1); |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 176 | #else |
| 177 | return QString(); |
Edric Milaret | 43f3c1e | 2015-07-16 17:52:47 -0400 | [diff] [blame] | 178 | #endif |
| 179 | } |
| 180 | |
Edric Milaret | 4097d2f | 2016-02-09 14:41:50 -0500 | [diff] [blame] | 181 | void |
| 182 | Utils::InvokeMailto(const QString& subject, |
| 183 | const QString& body, |
| 184 | const QString& attachement) { |
Edric Milaret | 3ba22ca | 2016-03-01 13:28:01 -0500 | [diff] [blame] | 185 | #ifdef Q_OS_WIN |
| 186 | HKEY hKey; |
| 187 | LONG lRes = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"mailto", 0, KEY_READ, &hKey); |
| 188 | if (lRes != ERROR_FILE_NOT_FOUND) { |
| 189 | auto addr = QString("mailto:?subject=%1&body=%2").arg(subject).arg(body); |
| 190 | if (not attachement.isEmpty()) |
| 191 | addr += QString("&attachement=%1").arg(attachement); |
| 192 | ShellExecute(nullptr, L"open", addr.toStdWString().c_str(), NULL, NULL, SW_SHOWNORMAL); |
| 193 | } else { |
| 194 | QErrorMessage errorMessage; |
| 195 | errorMessage.showMessage(QObject::tr("No default mail client found")); |
| 196 | } |
| 197 | #endif |
Edric Milaret | 4097d2f | 2016-02-09 14:41:50 -0500 | [diff] [blame] | 198 | } |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 199 | |
| 200 | QImage |
| 201 | Utils::getCirclePhoto(const QImage original, int sizePhoto) |
| 202 | { |
| 203 | QImage target(sizePhoto, sizePhoto, QImage::Format_ARGB32_Premultiplied); |
| 204 | target.fill(Qt::transparent); |
| 205 | |
| 206 | QPainter painter(&target); |
| 207 | painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
| 208 | painter.setBrush(QBrush(Qt::white)); |
| 209 | auto scaledPhoto = original |
| 210 | .scaled(sizePhoto, sizePhoto, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation) |
| 211 | .convertToFormat(QImage::Format_ARGB32_Premultiplied); |
| 212 | int margin = 0; |
| 213 | if (scaledPhoto.width() > sizePhoto) { |
| 214 | margin = (scaledPhoto.width() - sizePhoto) / 2; |
| 215 | } |
| 216 | painter.drawEllipse(0, 0, sizePhoto, sizePhoto); |
| 217 | painter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
| 218 | painter.drawImage(0, 0, scaledPhoto, margin, 0); |
| 219 | return target; |
| 220 | } |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 221 | |
| 222 | void |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 223 | Utils::setStackWidget(QStackedWidget* stack, QWidget* widget) |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 224 | { |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 225 | if (stack->indexOf(widget) != -1 && stack->currentWidget() != widget) { |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 226 | stack->setCurrentWidget(widget); |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 227 | } |
| 228 | } |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 229 | |
| 230 | void Utils::showSystemNotification(QWidget* widget, const QString & message, long delay) |
| 231 | { |
Sébastien Blin | cba5b52 | 2018-12-10 12:48:36 -0500 | [diff] [blame] | 232 | GlobalSystemTray::instance().showMessage("Jami", message); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 233 | QApplication::alert(widget, delay); |
| 234 | } |
| 235 | |
| 236 | // new lrc helpers |
| 237 | |
| 238 | inline std::string |
| 239 | removeEndlines(const std::string& str) |
| 240 | { |
| 241 | std::string trimmed(str); |
| 242 | trimmed.erase(std::remove(trimmed.begin(), trimmed.end(), '\n'), trimmed.end()); |
| 243 | trimmed.erase(std::remove(trimmed.begin(), trimmed.end(), '\r'), trimmed.end()); |
| 244 | return trimmed; |
| 245 | } |
| 246 | |
| 247 | std::string |
| 248 | Utils::bestIdForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 249 | { |
| 250 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
| 251 | if (!contact.registeredName.empty()) { |
| 252 | return removeEndlines(contact.registeredName); |
| 253 | } |
| 254 | return removeEndlines(contact.profileInfo.uri); |
| 255 | } |
| 256 | |
| 257 | std::string |
| 258 | Utils::bestIdForAccount(const lrc::api::account::Info& account) |
| 259 | { |
| 260 | if (!account.registeredName.empty()) { |
| 261 | return removeEndlines(account.registeredName); |
| 262 | } |
| 263 | return removeEndlines(account.profileInfo.uri); |
| 264 | } |
| 265 | |
| 266 | std::string |
| 267 | Utils::bestNameForAccount(const lrc::api::account::Info& account) |
| 268 | { |
| 269 | if (account.profileInfo.alias.empty()) { |
| 270 | return bestIdForAccount(account); |
| 271 | } |
| 272 | return account.profileInfo.alias; |
| 273 | } |
| 274 | |
| 275 | std::string |
| 276 | Utils::bestIdForContact(const lrc::api::contact::Info& contact) |
| 277 | { |
| 278 | if (!contact.registeredName.empty()) { |
| 279 | return removeEndlines(contact.registeredName); |
| 280 | } |
| 281 | return removeEndlines(contact.profileInfo.uri); |
| 282 | } |
| 283 | |
| 284 | std::string |
| 285 | Utils::bestNameForContact(const lrc::api::contact::Info& contact) |
| 286 | { |
| 287 | auto alias = removeEndlines(contact.profileInfo.alias); |
| 288 | if (alias.length() == 0) { |
| 289 | return bestIdForContact(contact); |
| 290 | } |
| 291 | return alias; |
| 292 | } |
| 293 | |
| 294 | std::string |
| 295 | Utils::bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 296 | { |
| 297 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
| 298 | auto alias = removeEndlines(contact.profileInfo.alias); |
| 299 | if (alias.length() == 0) { |
| 300 | return bestIdForConversation(conv, model); |
| 301 | } |
| 302 | return alias; |
| 303 | } |
| 304 | |
Isa Nanic | 601de1d | 2018-10-23 11:37:26 -0400 | [diff] [blame] | 305 | // returns empty string if only infoHash is available, second best identifier otherwise |
| 306 | std::string |
| 307 | Utils::secondBestNameForAccount(const lrc::api::account::Info& account) |
| 308 | { |
| 309 | auto alias = removeEndlines(account.profileInfo.alias); |
| 310 | auto registeredName = removeEndlines(account.registeredName); |
| 311 | auto infoHash = account.profileInfo.uri; |
| 312 | |
| 313 | if (!alias.length() == 0) { // if alias exists |
| 314 | if (!registeredName.length() == 0) { // if registeredName exists |
| 315 | return registeredName; |
| 316 | } |
| 317 | else { |
| 318 | return infoHash; |
| 319 | } |
| 320 | } |
| 321 | else { |
| 322 | if (!registeredName.length() == 0) { // if registeredName exists |
| 323 | return infoHash; |
| 324 | } |
| 325 | else { |
| 326 | return ""; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 331 | lrc::api::profile::Type |
| 332 | Utils::profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 333 | { |
| 334 | try { |
| 335 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
| 336 | return contact.profileInfo.type; |
| 337 | } |
| 338 | catch (...) { |
| 339 | return lrc::api::profile::Type::INVALID; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | std::string |
| 344 | Utils::formatTimeString(const std::time_t& timestamp) |
| 345 | { |
| 346 | std::time_t now = std::time(nullptr); |
| 347 | char interactionDay[64]; |
| 348 | char nowDay[64]; |
| 349 | std::strftime(interactionDay, sizeof(interactionDay), "%D", std::localtime(×tamp)); |
| 350 | std::strftime(nowDay, sizeof(nowDay), "%D", std::localtime(&now)); |
| 351 | if (std::string(interactionDay) == std::string(nowDay)) { |
| 352 | char interactionTime[64]; |
| 353 | std::strftime(interactionTime, sizeof(interactionTime), "%R", std::localtime(×tamp)); |
| 354 | return interactionTime; |
| 355 | } |
| 356 | else { |
| 357 | return interactionDay; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | lrc::api::ConversationModel::ConversationQueue::const_iterator |
| 362 | Utils::getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) { |
| 363 | return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(), |
| 364 | [&](const lrc::api::conversation::Info& conv) { |
| 365 | return uid == conv.uid; |
| 366 | }); |
| 367 | } |
| 368 | |
| 369 | lrc::api::ConversationModel::ConversationQueue::const_iterator |
| 370 | Utils::getConversationFromUri(const std::string& uri, const lrc::api::ConversationModel& model) { |
| 371 | return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(), |
| 372 | [&](const lrc::api::conversation::Info& conv) { |
| 373 | return uri == conv.participants[0]; |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | bool |
| 378 | Utils::isInteractionGenerated(const lrc::api::interaction::Type& type) |
| 379 | { |
| 380 | return type == lrc::api::interaction::Type::CALL || |
| 381 | type == lrc::api::interaction::Type::CONTACT; |
| 382 | } |
| 383 | |
| 384 | bool |
| 385 | Utils::isContactValid(const std::string& contactUid, const lrc::api::ConversationModel& model) |
| 386 | { |
| 387 | auto contact = model.owner.contactModel->getContact(contactUid); |
| 388 | return (contact.profileInfo.type == lrc::api::profile::Type::PENDING || |
| 389 | contact.profileInfo.type == lrc::api::profile::Type::TEMPORARY || |
| 390 | contact.profileInfo.type == lrc::api::profile::Type::RING || |
| 391 | contact.profileInfo.type == lrc::api::profile::Type::SIP) && |
| 392 | !contact.profileInfo.uri.empty(); |
| 393 | } |
| 394 | |
| 395 | QImage |
| 396 | Utils::conversationPhoto(const std::string & convUid, const lrc::api::account::Info& accountInfo) |
| 397 | { |
Andreas Traczyk | 1cf20c7 | 2019-01-09 19:42:32 -0500 | [diff] [blame^] | 398 | auto convInfo = getConversationFromUid(convUid, false); |
| 399 | if (!convInfo.uid.empty()) { |
| 400 | return GlobalInstances::pixmapManipulator() |
| 401 | .decorationRole(convInfo, accountInfo) |
| 402 | .value<QImage>(); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 403 | } |
Andreas Traczyk | 1cf20c7 | 2019-01-09 19:42:32 -0500 | [diff] [blame^] | 404 | return QImage(); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 405 | } |
Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 406 | |
| 407 | QByteArray |
| 408 | Utils::QByteArrayFromFile(const QString& filename) |
| 409 | { |
| 410 | QFile file(filename); |
| 411 | if (file.open(QIODevice::ReadOnly)) { |
| 412 | return file.readAll(); |
| 413 | } else { |
| 414 | qDebug() << "can't open file"; |
| 415 | return QByteArray(); |
| 416 | } |
Andreas Traczyk | 6ace34f | 2018-12-14 14:31:23 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | QPixmap |
| 420 | Utils::generateTintedPixmap(const QString& filename, QColor color) |
| 421 | { |
| 422 | QPixmap px(filename); |
| 423 | QImage tmpImage = px.toImage(); |
| 424 | for (int y = 0; y < tmpImage.height(); y++) { |
| 425 | for (int x = 0; x < tmpImage.width(); x++) { |
| 426 | color.setAlpha(tmpImage.pixelColor(x, y).alpha()); |
| 427 | tmpImage.setPixelColor(x, y, color); |
| 428 | } |
| 429 | } |
| 430 | return QPixmap::fromImage(tmpImage); |
Andreas Traczyk | 6b5ad3e | 2019-01-02 17:04:36 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | std::string |
| 434 | Utils::getConversationFromCallId(const std::string& callId) |
| 435 | { |
| 436 | auto convModel = LRCInstance::getCurrentConversationModel(); |
| 437 | auto conversations = convModel->allFilteredConversations(); |
| 438 | std::string convUid; |
| 439 | for (auto conversation : conversations) { |
| 440 | if (conversation.callId == callId) { |
| 441 | return conversation.uid; |
| 442 | } |
| 443 | } |
| 444 | return ""; |
Andreas Traczyk | f946549 | 2019-01-09 13:53:37 -0500 | [diff] [blame] | 445 | } |
Andreas Traczyk | 1cf20c7 | 2019-01-09 19:42:32 -0500 | [diff] [blame^] | 446 | |
| 447 | lrc::api::conversation::Info |
| 448 | Utils::getSelectedConversation() |
| 449 | { |
| 450 | return getConversationFromUid(LRCInstance::getSelectedConvUid(), false); |
| 451 | } |
| 452 | |
| 453 | lrc::api::conversation::Info |
| 454 | Utils::getConversationFromUid(const std::string & convUid, bool filtered) |
| 455 | { |
| 456 | auto convModel = LRCInstance::getCurrentConversationModel(); |
| 457 | if (filtered) { |
| 458 | auto conversation = getConversationFromUid(convUid, *convModel); |
| 459 | } else { |
| 460 | using namespace lrc::api::profile; |
| 461 | for (int i = toUnderlyingValue(Type::RING); i <= toUnderlyingValue(Type::TEMPORARY); ++i) { |
| 462 | auto filter = toEnum<lrc::api::profile::Type>(i); |
| 463 | auto conversations = convModel->getFilteredConversations(filter); |
| 464 | auto conv = std::find_if(conversations.begin(), conversations.end(), |
| 465 | [&](const lrc::api::conversation::Info& conv) { |
| 466 | return convUid == conv.uid; |
| 467 | }); |
| 468 | if (conv != conversations.end()) { |
| 469 | return *conv; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | return lrc::api::conversation::Info(); |
| 474 | } |