blob: ffa29226c420eeb4a583a1187fc27806c7cd22ca [file] [log] [blame]
Edric Milaret4bba46d2015-04-29 16:33:38 -04001/***************************************************************************
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04002 * Copyright (C) 2015-2018 by Savoir-faire Linux *
Edric Milaret4bba46d2015-04-29 16:33:38 -04003 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
Andreas Traczykb8b13ba2018-08-21 16:30:16 -04004 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> *
Isa Nanic601de1d2018-10-23 11:37:26 -04005 * Author: Isa Nanic <isa.nanic@savoirfairelinux.com *
Edric Milaret4bba46d2015-04-29 16:33:38 -04006 * *
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 Milaret5f316da2015-09-28 11:57:42 -040021#pragma once
Edric Milaret4bba46d2015-04-29 16:33:38 -040022
Edric Milaretb37aa1f2015-07-09 16:39:04 -040023//Needed for OS detection
24#include <QtGlobal>
25
Edric Milaret36587362016-02-04 12:30:52 -050026#ifdef Q_OS_WIN
Edric Milaret4bba46d2015-04-29 16:33:38 -040027#include <windows.h>
Andreas Traczyke0a60b52018-07-10 18:16:15 -040028#include <ciso646>
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040029#undef OUT
30#undef IN
31#undef ERROR
Edric Milaretb37aa1f2015-07-09 16:39:04 -040032#else //LINUX
33#define LPCWSTR char*
34#endif
Edric Milaret4bba46d2015-04-29 16:33:38 -040035
36#include <string>
Edric Milaret031c3052015-04-29 18:14:18 -040037#include <QString>
Edric Milaret25236d92016-03-28 09:40:58 -040038#include <QImage>
Olivier SOLDANOc1ca2df2017-09-06 16:01:46 -040039#include <QStackedWidget>
Edric Milaret4bba46d2015-04-29 16:33:38 -040040
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040041#include "api/conversationmodel.h"
42#include "api/account.h"
43#include "api/contactmodel.h"
44#include "api/contact.h"
45
Olivier SOLDANOc1ca2df2017-09-06 16:01:46 -040046namespace Utils
Edric Milaret4bba46d2015-04-29 16:33:38 -040047{
Olivier SOLDANOc1ca2df2017-09-06 16:01:46 -040048 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 Traczykb8b13ba2018-08-21 16:30:16 -040060 void setStackWidget(QStackedWidget *stack, QWidget *widget);
61 void showSystemNotification(QWidget* widget, const QString& message, long delay = 5000);
Edric Milaret4bba46d2015-04-29 16:33:38 -040062
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040063 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 Nanic601de1d2018-10-23 11:37:26 -040069 std::string secondBestNameForAccount(const lrc::api::account::Info& account); // returns empty string if only infoHash is available
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040070 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}