modernize: Pass arguments by value and move in constructors
[quassel.git] / src / common / util.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 <QString>
27 #include <QVariant>
28
29 COMMON_EXPORT QString nickFromMask(const QString &mask);
30 COMMON_EXPORT QString userFromMask(const QString &mask);
31 COMMON_EXPORT QString hostFromMask(const QString &mask);
32 COMMON_EXPORT bool isChannelName(const QString &str);
33
34 //! Strip mIRC format codes
35 COMMON_EXPORT QString stripFormatCodes(QString);
36
37 //! Remove accelerator markers (&) from the string
38 COMMON_EXPORT QString stripAcceleratorMarkers(const QString &);
39
40 COMMON_EXPORT QString secondsToString(int timeInSeconds);
41
42 //! Take a string and decode it using the specified text codec, recognizing utf8.
43 /** This function takes a string and first checks if it is encoded in utf8, in which case it is
44  *  decoded appropriately. Otherwise, the specified text codec is used to transform the string.
45  *  \param input The input string containing encoded data
46  *  \param codec The text codec we use if the input is not utf8
47  *  \return The decoded string.
48  */
49 COMMON_EXPORT QString decodeString(const QByteArray &input, QTextCodec *codec = nullptr);
50
51 COMMON_EXPORT uint editingDistance(const QString &s1, const QString &s2);
52
53 template<typename T>
54 QVariantList toVariantList(const QList<T> &list)
55 {
56     QVariantList variants;
57     for (int i = 0; i < list.count(); i++) {
58         variants << QVariant::fromValue<T>(list[i]);
59     }
60     return variants;
61 }
62
63
64 template<typename T>
65 QList<T> fromVariantList(const QVariantList &variants)
66 {
67     QList<T> list;
68     for (int i = 0; i < variants.count(); i++) {
69         list << variants[i].value<T>();
70     }
71     return list;
72 }
73
74
75 COMMON_EXPORT QByteArray prettyDigest(const QByteArray &digest);
76
77 /**
78  * Format a string with %%<text>%% to current date/timestamp via QDateTime.
79  *
80  * @param[in] formatStr String with format codes
81  * @return String with current date/time substituted in via formatting codes
82  */
83 COMMON_EXPORT QString formatCurrentDateTimeInString(const QString &formatStr);
84
85 /**
86  * Try to localize a given date/time in seconds from Unix epoch, pass through string if invalid
87  *
88  * Allows compatibility with date/time fields that may or may not be in Unix epoch format,
89  * localizing if possible, leaving alone if not.
90  *
91  * @param possibleEpochDate Date/time that might be in seconds since Unix epoch format
92  * @param dateFormat        Desired format of the date/time string
93  * @param useUTC            If true, use UTC timezone, otherwise use local time
94  * @return Localized date/time if parse succeeded, otherwise the source string
95  */
96 COMMON_EXPORT QString tryFormatUnixEpoch(const QString &possibleEpochDate,
97                                          Qt::DateFormat dateFormat = Qt::DateFormat::TextDate,
98                                          bool useUTC = false);
99
100
101 /**
102  * Format the given date/time in ISO 8601 format with timezone offset
103  *
104  * @param dateTime Date/time of interest
105  * @return Date/time in ISO 8601 format with timezone offset
106  */
107 COMMON_EXPORT QString formatDateTimeToOffsetISO(const QDateTime &dateTime);