src: Yearly copyright bump
[quassel.git] / src / common / util.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <QList>
26 #include <QSet>
27 #include <QString>
28 #include <QVariant>
29
30 COMMON_EXPORT QString nickFromMask(const QString& mask);
31 COMMON_EXPORT QString userFromMask(const QString& mask);
32 COMMON_EXPORT QString hostFromMask(const QString& mask);
33 COMMON_EXPORT bool isChannelName(const QString& str);
34
35 //! Strip mIRC format codes
36 COMMON_EXPORT QString stripFormatCodes(QString);
37
38 //! Remove accelerator markers (&) from the string
39 COMMON_EXPORT QString stripAcceleratorMarkers(const QString&);
40
41 COMMON_EXPORT QString secondsToString(int timeInSeconds);
42
43 //! Take a string and decode it using the specified text codec, recognizing utf8.
44 /** This function takes a string and first checks if it is encoded in utf8, in which case it is
45  *  decoded appropriately. Otherwise, the specified text codec is used to transform the string.
46  *  \param input The input string containing encoded data
47  *  \param codec The text codec we use if the input is not utf8
48  *  \return The decoded string.
49  */
50 COMMON_EXPORT QString decodeString(const QByteArray& input, QTextCodec* codec = nullptr);
51
52 COMMON_EXPORT uint editingDistance(const QString& s1, const QString& s2);
53
54 template<typename T>
55 QSet<T> toQSet(const QList<T>& list)
56 {
57 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
58     return list.toSet();
59 #else
60     return {list.begin(), list.end()};
61 #endif
62 }
63
64 template<typename T>
65 QVariantList toVariantList(const QList<T>& list)
66 {
67     QVariantList variants;
68     for (int i = 0; i < list.count(); i++) {
69         variants << QVariant::fromValue(list[i]);
70     }
71     return variants;
72 }
73
74 template<typename T>
75 QList<T> fromVariantList(const QVariantList& variants)
76 {
77     QList<T> list;
78     for (int i = 0; i < variants.count(); i++) {
79         list << variants[i].value<T>();
80     }
81     return list;
82 }
83
84 COMMON_EXPORT QByteArray prettyDigest(const QByteArray& digest);
85
86 /**
87  * Format a string with %%<text>%% to current date/timestamp via QDateTime.
88  *
89  * @param[in] formatStr String with format codes
90  * @return String with current date/time substituted in via formatting codes
91  */
92 COMMON_EXPORT QString formatCurrentDateTimeInString(const QString& formatStr);
93
94 /**
95  * Try to localize a given date/time in seconds from Unix epoch, pass through string if invalid
96  *
97  * Allows compatibility with date/time fields that may or may not be in Unix epoch format,
98  * localizing if possible, leaving alone if not.
99  *
100  * @param possibleEpochDate Date/time that might be in seconds since Unix epoch format
101  * @param dateFormat        Desired format of the date/time string
102  * @param useUTC            If true, use UTC timezone, otherwise use local time
103  * @return Localized date/time if parse succeeded, otherwise the source string
104  */
105 COMMON_EXPORT QString tryFormatUnixEpoch(const QString& possibleEpochDate,
106                                          Qt::DateFormat dateFormat = Qt::DateFormat::TextDate,
107                                          bool useUTC = false);
108
109 /**
110  * Format the given date/time in ISO 8601 format with timezone offset
111  *
112  * @param dateTime Date/time of interest
113  * @return Date/time in ISO 8601 format with timezone offset
114  */
115 COMMON_EXPORT QString formatDateTimeToOffsetISO(const QDateTime& dateTime);
116
117 namespace detail {
118
119 template<typename... Args>
120 struct SelectOverloadHelper
121 {
122     template<typename R, typename C>
123     constexpr auto operator()(R (C::*func)(Args...)) const noexcept -> decltype(func)
124     {
125         return func;
126     }
127 };
128
129 }  // namespace detail
130
131 /**
132  * Helper for resolving ambiguous overloads when using the member function-based connect syntax.
133  *
134  * Example usage:
135  * @code
136  * connect(this, selectOverload<int, QString>(&MyClass::mySignal), other, &Other::mySlot);
137  * @endcode
138  *
139  * @tparam Args Argument types of the desired signature
140  */
141 template<typename... Args>
142 constexpr Q_DECL_UNUSED detail::SelectOverloadHelper<Args...> selectOverload = {};