1 /*
2  *  Kaidan - A user-friendly XMPP client for every device!
3  *
4  *  Copyright (C) 2016-2021 Kaidan developers and contributors
5  *  (see the LICENSE file for a full list of copyright authors)
6  *
7  *  Kaidan 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  *  In addition, as a special exception, the author of Kaidan gives
13  *  permission to link the code of its release with the OpenSSL
14  *  project's "OpenSSL" library (or with modified versions of it that
15  *  use the same license as the "OpenSSL" library), and distribute the
16  *  linked executables. You must obey the GNU General Public License in
17  *  all respects for all of the code used other than "OpenSSL". If you
18  *  modify this file, you may extend this exception to your version of
19  *  the file, but you are not obligated to do so.  If you do not wish to
20  *  do so, delete this exception statement from your version.
21  *
22  *  Kaidan is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  *
27  *  You should have received a copy of the GNU General Public License
28  *  along with Kaidan.  If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef ENUMS_H
32 #define ENUMS_H
33 
34 #include <QtGlobal>
35 #include <QObject>
36 #include <QMetaEnum>
37 #include <QXmppClient.h>
38 
39 #define ENABLE_IF(...) typename std::enable_if<__VA_ARGS__>::type* = nullptr
40 
41 template <typename... Ts> struct make_void { typedef void type; };
42 template <typename... Ts> using void_t = typename make_void<Ts...>::type;
43 
44 // primary template handles types that have no nested ::enum_type member, like standard enum
45 template <typename, typename = void_t<>>
46 struct has_enum_type : std::false_type { };
47 
48 // specialization recognizes types that do have a nested ::enum_type member, like QFlags enum
49 template <typename T>
50 struct has_enum_type<T, void_t<typename T::enum_type>> : std::true_type { };
51 
52 namespace Enums {
53 	Q_NAMESPACE
54 
55 	/**
56 	 * Enumeration of possible connection states.
57 	 */
58 	enum class ConnectionState {
59 		StateDisconnected = QXmppClient::DisconnectedState,
60 		StateConnecting = QXmppClient::ConnectingState,
61 		StateConnected = QXmppClient::ConnectedState
62 	};
63 	Q_ENUM_NS(ConnectionState)
64 
65 	/**
66 	 * Enumeration of different media/message types
67 	 */
68 	enum class MessageType {
69 		MessageUnknown = -1,
70 		MessageText,
71 		MessageFile,
72 		MessageImage,
73 		MessageVideo,
74 		MessageAudio,
75 		MessageDocument,
76 		MessageGeoLocation
77 	};
78 	Q_ENUM_NS(MessageType)
79 
80 	/**
81 	 * Enumeration of different message delivery states
82 	 */
83 	enum class DeliveryState {
84 		Pending,
85 		Sent,
86 		Delivered,
87 		Error
88 	};
89 	Q_ENUM_NS(DeliveryState)
90 
91 	/**
92 	 * State which specifies how the XMPP login URI was used
93 	 */
94 	enum class LoginByUriState {
95 		Connecting,         ///< The JID and password are included in the URI and the client is connecting.
96 		PasswordNeeded,     ///< The JID is included in the URI but not the password.
97 		InvalidLoginUri     ///< The URI cannot be used to log in.
98 	};
99 	Q_ENUM_NS(LoginByUriState)
100 
101 	template <typename T, ENABLE_IF(!has_enum_type<T>::value && std::is_enum<T>::value)>
102 	QString toString(const T flag) {
103 		static const QMetaEnum e = QMetaEnum::fromType<T>();
104 		return QString::fromLatin1(e.valueToKey(static_cast<int>(flag)));
105 	}
106 
107 	template <typename T, ENABLE_IF(has_enum_type<T>::value)>
108 	QString toString(const T flags) {
109 		static const QMetaEnum e = QMetaEnum::fromType<T>();
110 		return QString::fromLatin1(e.valueToKeys(static_cast<int>(flags)));
111 	}
112 }
113 
114 // Needed workaround to trigger older CMake auto moc versions to generate moc
115 // sources for this file (it only contains Q_NAMESPACE, which is new).
116 #if 0
117 Q_OBJECT
118 #endif
119 
120 #endif // ENUMS_H
121