Some cleanups, debug output--
[quassel.git] / src / common / util.h
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef UTIL_H
22 #define UTIL_H
23
24 #include <QDir>
25 #include <QVariant>
26 #include <QString>
27 #include <QMetaMethod>
28
29
30 // TODO Use versions from Network instead
31 QString nickFromMask(QString mask);
32 QString userFromMask(QString mask);
33 QString hostFromMask(QString mask);
34 bool isChannelName(QString str);
35
36 //! Strip mIRC format codes
37 QString stripFormatCodes(QString);
38
39 QString secondsToString(int timeInSeconds);
40
41 //! Take a string and decode it using the specified text codec, recognizing utf8.
42 /** This function takes a string and first checks if it is encoded in utf8, in which case it is
43  *  decoded appropriately. Otherwise, the specified text codec is used to transform the string.
44  *  \param input The input string containing encoded data
45  *  \param codec The text codec we use if the input is not utf8
46  *  \return The decoded string.
47  */
48 QString decodeString(const QByteArray &input, QTextCodec *codec = 0);
49
50 uint editingDistance(const QString &s1, const QString &s2);
51
52 template<typename T>
53 QVariantList toVariantList(const QList<T> &list) {
54   QVariantList variants;
55   for(int i = 0; i < list.count(); i++) {
56     variants << QVariant::fromValue<T>(list[i]);
57   }
58   return variants;
59 }
60
61 template<typename T>
62 QList<T> fromVariantList(const QVariantList &variants) {
63   QList<T> list;
64   for(int i = 0; i < variants.count(); i++) {
65     list << variants[i].value<T>();
66   }
67   return list;
68 }
69
70 QByteArray prettyDigest(const QByteArray &digest);
71
72 #endif