File: util.h

package info (click to toggle)
quassel 1:0.13.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,640 kB
  • sloc: cpp: 66,953; perl: 15,837; ansic: 4,418; sql: 1,225; sh: 328; xml: 263; python: 224; makefile: 24
file content (116 lines) | stat: -rw-r--r-- 4,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#pragma once

#include <QList>
#include <QSet>
#include <QString>
#include <QVariant>

QString nickFromMask(const QString &mask);
QString userFromMask(const QString &mask);
QString hostFromMask(const QString &mask);
bool isChannelName(const QString &str);

//! Strip mIRC format codes
QString stripFormatCodes(QString);

//! Remove accelerator markers (&) from the string
QString stripAcceleratorMarkers(const QString &);

QString secondsToString(int timeInSeconds);

//! Take a string and decode it using the specified text codec, recognizing utf8.
/** This function takes a string and first checks if it is encoded in utf8, in which case it is
 *  decoded appropriately. Otherwise, the specified text codec is used to transform the string.
 *  \param input The input string containing encoded data
 *  \param codec The text codec we use if the input is not utf8
 *  \return The decoded string.
 */
QString decodeString(const QByteArray &input, QTextCodec *codec = 0);

uint editingDistance(const QString &s1, const QString &s2);

template<typename T>
QVariantList toVariantList(const QList<T> &list)
{
    QVariantList variants;
    for (int i = 0; i < list.count(); i++) {
        variants << QVariant::fromValue(list[i]);
    }
    return variants;
}


template<typename T>
QSet<T> toQSet(const QList<T>& list)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
    return list.toSet();
#else
    return {list.begin(), list.end()};
#endif
}

template<typename T>
QList<T> fromVariantList(const QVariantList &variants)
{
    QList<T> list;
    for (int i = 0; i < variants.count(); i++) {
        list << variants[i].value<T>();
    }
    return list;
}


QByteArray prettyDigest(const QByteArray &digest);

/**
 * Format a string with %%<text>%% to current date/timestamp via QDateTime.
 *
 * @param[in] formatStr String with format codes
 * @return String with current date/time substituted in via formatting codes
 */
QString formatCurrentDateTimeInString(const QString &formatStr);

/**
 * Try to localize a given date/time in seconds from Unix epoch, pass through string if invalid
 *
 * Allows compatibility with date/time fields that may or may not be in Unix epoch format,
 * localizing if possible, leaving alone if not.
 *
 * @param possibleEpochDate Date/time that might be in seconds since Unix epoch format
 * @param dateFormat        Desired format of the date/time string
 * @param useUTC            If true, use UTC timezone, otherwise use local time
 * @return Localized date/time if parse succeeded, otherwise the source string
 */
QString tryFormatUnixEpoch(const QString &possibleEpochDate,
                           Qt::DateFormat dateFormat = Qt::DateFormat::TextDate,
                           bool useUTC = false);


/**
 * Format the given date/time in ISO 8601 format with timezone offset
 *
 * @param dateTime Date/time of interest
 * @return Date/time in ISO 8601 format with timezone offset
 */
QString formatDateTimeToOffsetISO(const QDateTime &dateTime);