Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 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 | |
Edric Milaret | 5f316da | 2015-09-28 11:57:42 -0400 | [diff] [blame] | 21 | #pragma once |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 22 | |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 23 | //Needed for OS detection |
| 24 | #include <QtGlobal> |
| 25 | |
Edric Milaret | 3658736 | 2016-02-04 12:30:52 -0500 | [diff] [blame] | 26 | #ifdef Q_OS_WIN |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 27 | #include <windows.h> |
Andreas Traczyk | e0a60b5 | 2018-07-10 18:16:15 -0400 | [diff] [blame] | 28 | #include <ciso646> |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 29 | #undef OUT |
| 30 | #undef IN |
| 31 | #undef ERROR |
Edric Milaret | b37aa1f | 2015-07-09 16:39:04 -0400 | [diff] [blame] | 32 | #else //LINUX |
| 33 | #define LPCWSTR char* |
| 34 | #endif |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 35 | |
| 36 | #include <string> |
Edric Milaret | 031c305 | 2015-04-29 18:14:18 -0400 | [diff] [blame] | 37 | #include <QString> |
Edric Milaret | 25236d9 | 2016-03-28 09:40:58 -0400 | [diff] [blame] | 38 | #include <QImage> |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 39 | #include <QStackedWidget> |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 40 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 41 | #include "api/conversationmodel.h" |
| 42 | #include "api/account.h" |
| 43 | #include "api/contactmodel.h" |
| 44 | #include "api/contact.h" |
| 45 | |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 46 | namespace Utils |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 47 | { |
Olivier SOLDANO | c1ca2df | 2017-09-06 16:01:46 -0400 | [diff] [blame] | 48 | constexpr int animDuration_ = 200; // animation duration for sliding page in ms |
| 49 | |
| 50 | bool CreateStartupLink(); |
| 51 | void DeleteStartupLink(); |
| 52 | bool CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink); |
| 53 | bool CheckStartupLink(); |
| 54 | QString GetRingtonePath(); |
| 55 | QString GenGUID(); |
| 56 | QString GetISODate(); |
| 57 | QString GetCurrentUserName(); |
| 58 | void InvokeMailto(const QString& subject, const QString& body, const QString& attachement = QString()); |
| 59 | QImage getCirclePhoto(const QImage original, int sizePhoto); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 60 | void setStackWidget(QStackedWidget *stack, QWidget *widget); |
| 61 | void showSystemNotification(QWidget* widget, const QString& message, long delay = 5000); |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 62 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 63 | std::string bestIdForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model); |
| 64 | std::string bestIdForAccount(const lrc::api::account::Info & account); |
| 65 | std::string bestNameForAccount(const lrc::api::account::Info & account); |
| 66 | std::string bestIdForContact(const lrc::api::contact::Info & contact); |
| 67 | std::string bestNameForContact(const lrc::api::contact::Info & contact); |
| 68 | std::string bestNameForConversation(const lrc::api::conversation::Info & conv, const lrc::api::ConversationModel & model); |
Isa Nanic | 601de1d | 2018-10-23 11:37:26 -0400 | [diff] [blame] | 69 | std::string secondBestNameForAccount(const lrc::api::account::Info& account); // returns empty string if only infoHash is available |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 70 | lrc::api::profile::Type profileType(const lrc::api::conversation::Info & conv, const lrc::api::ConversationModel & model); |
| 71 | std::string formatTimeString(const std::time_t& timestamp); |
| 72 | lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model); |
| 73 | lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUri(const std::string& uri, const lrc::api::ConversationModel& model); |
| 74 | bool isInteractionGenerated(const lrc::api::interaction::Type& interaction); |
| 75 | bool isContactValid(const std::string& contactUid, const lrc::api::ConversationModel& model); |
| 76 | QImage conversationPhoto(const std::string& convUid, const lrc::api::account::Info& accountInfo); |
| 77 | |
| 78 | // helpers |
| 79 | template<typename E> |
| 80 | constexpr inline typename std::enable_if< std::is_enum<E>::value, |
| 81 | typename std::underlying_type<E>::type |
| 82 | >::type |
| 83 | toUnderlyingValue(E e) noexcept |
| 84 | { |
| 85 | return static_cast<typename std::underlying_type<E>::type >(e); |
| 86 | } |
| 87 | |
| 88 | template<typename E, typename T> |
| 89 | constexpr inline typename std::enable_if< std::is_enum<E>::value && std::is_integral<T>::value, |
| 90 | E |
| 91 | >::type |
| 92 | toEnum(T value) noexcept |
| 93 | { |
| 94 | return static_cast<E>(value); |
| 95 | } |
| 96 | |
| 97 | template<typename T> |
| 98 | ptrdiff_t |
| 99 | indexInVector(const std::vector<T>& vec, const T& item) |
| 100 | { |
| 101 | auto it = std::find(vec.begin(), vec.end(), item); |
| 102 | if (it == vec.end()) { |
| 103 | return -1; |
| 104 | } |
| 105 | return std::distance(vec.begin(), it); |
| 106 | } |
| 107 | } |