blob: c13abde9fdb75ed14fa5eab39aaf7476e1c38a23 [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>
Andreas Traczyk43c08232018-10-31 13:42:09 -040040#include <QTextDocument>
41#include <QItemDelegate>
Edric Milaret4bba46d2015-04-29 16:33:38 -040042
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040043#include "api/conversationmodel.h"
44#include "api/account.h"
45#include "api/contactmodel.h"
46#include "api/contact.h"
47
Olivier SOLDANOc1ca2df2017-09-06 16:01:46 -040048namespace Utils
Edric Milaret4bba46d2015-04-29 16:33:38 -040049{
Olivier SOLDANOc1ca2df2017-09-06 16:01:46 -040050 constexpr int animDuration_ = 200; // animation duration for sliding page in ms
51
52 bool CreateStartupLink();
53 void DeleteStartupLink();
54 bool CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink);
55 bool CheckStartupLink();
56 QString GetRingtonePath();
57 QString GenGUID();
58 QString GetISODate();
59 QString GetCurrentUserName();
60 void InvokeMailto(const QString& subject, const QString& body, const QString& attachement = QString());
61 QImage getCirclePhoto(const QImage original, int sizePhoto);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040062 void setStackWidget(QStackedWidget *stack, QWidget *widget);
63 void showSystemNotification(QWidget* widget, const QString& message, long delay = 5000);
Edric Milaret4bba46d2015-04-29 16:33:38 -040064
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040065 std::string bestIdForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model);
66 std::string bestIdForAccount(const lrc::api::account::Info & account);
67 std::string bestNameForAccount(const lrc::api::account::Info & account);
68 std::string bestIdForContact(const lrc::api::contact::Info & contact);
69 std::string bestNameForContact(const lrc::api::contact::Info & contact);
70 std::string bestNameForConversation(const lrc::api::conversation::Info & conv, const lrc::api::ConversationModel & model);
Isa Nanic601de1d2018-10-23 11:37:26 -040071 std::string secondBestNameForAccount(const lrc::api::account::Info& account); // returns empty string if only infoHash is available
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040072 lrc::api::profile::Type profileType(const lrc::api::conversation::Info & conv, const lrc::api::ConversationModel & model);
73 std::string formatTimeString(const std::time_t& timestamp);
74 lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model);
75 lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUri(const std::string& uri, const lrc::api::ConversationModel& model);
76 bool isInteractionGenerated(const lrc::api::interaction::Type& interaction);
77 bool isContactValid(const std::string& contactUid, const lrc::api::ConversationModel& model);
78 QImage conversationPhoto(const std::string& convUid, const lrc::api::account::Info& accountInfo);
Andreas Traczyk43c08232018-10-31 13:42:09 -040079 QByteArray QByteArrayFromFile(const QString& filename);
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040080
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040081 template<typename E>
82 constexpr inline typename std::enable_if< std::is_enum<E>::value,
83 typename std::underlying_type<E>::type
84 >::type
85 toUnderlyingValue(E e) noexcept
86 {
87 return static_cast<typename std::underlying_type<E>::type >(e);
88 }
89
90 template<typename E, typename T>
91 constexpr inline typename std::enable_if< std::is_enum<E>::value && std::is_integral<T>::value,
92 E
93 >::type
94 toEnum(T value) noexcept
95 {
96 return static_cast<E>(value);
97 }
98
99 template<typename T>
100 ptrdiff_t
101 indexInVector(const std::vector<T>& vec, const T& item)
102 {
103 auto it = std::find(vec.begin(), vec.end(), item);
104 if (it == vec.end()) {
105 return -1;
106 }
107 return std::distance(vec.begin(), it);
108 }
Andreas Traczyk43c08232018-10-31 13:42:09 -0400109}