added missing macros
[quassel.git] / src / common / util.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 #include "util.h"
22
23 #include <QCoreApplication>
24 #include <QDebug>
25 #include <QFile>
26 #include <QTextCodec>
27
28 #include "quassel.h"
29
30 class QMetaMethod;
31
32 QString nickFromMask(QString mask) {
33   return mask.section('!', 0, 0);
34 }
35
36 QString userFromMask(QString mask) {
37   QString userhost = mask.section('!', 1);
38   if(userhost.isEmpty()) return QString();
39   return userhost.section('@', 0, 0);
40 }
41
42 QString hostFromMask(QString mask) {
43   QString userhost = mask.section('!', 1);
44   if(userhost.isEmpty()) return QString();
45   return userhost.section('@', 1);
46 }
47
48 bool isChannelName(QString str) {
49   return QString("#&!+").contains(str[0]);
50 }
51
52 QString stripFormatCodes(QString str) {
53   str.remove(QRegExp("\x03(\\d\\d?(,\\d\\d?)?)?"));
54   str.remove('\x02');
55   str.remove('\x0f');
56   str.remove('\x12');
57   str.remove('\x16');
58   str.remove('\x1d');
59   str.remove('\x1f');
60   return str;
61 }
62
63 QString decodeString(const QByteArray &input, QTextCodec *codec) {
64   // First, we check if it's utf8. It is very improbable to encounter a string that looks like
65   // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
66   // is safe to assume that it is.
67   // Q_ASSERT(sizeof(const char) == sizeof(quint8));  // In God we trust...
68   bool isUtf8 = true;
69   int cnt = 0;
70   for(int i = 0; i < input.size(); i++) {
71     if(cnt) {
72       // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
73       if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
74       cnt--;
75       continue;
76     }
77     if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
78     if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; }  // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
79     if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; }  // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
80     if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; }  // 2-byte char 110xxxxx 10yyyyyy
81     isUtf8 = false; break;  // 8 bit char, but not utf8!
82   }
83   if(isUtf8 && cnt == 0) {
84     QString s = QString::fromUtf8(input);
85     //qDebug() << "Detected utf8:" << s;
86     return s;
87   }
88   //QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
89   if(!codec) return QString::fromAscii(input);
90   return codec->toUnicode(input);
91 }
92
93 uint editingDistance(const QString &s1, const QString &s2) {
94   uint n = s1.size()+1;
95   uint m = s2.size()+1;
96   QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
97
98   for(uint i = 0; i < n; i++)
99     matrix[i][0] = i;
100
101   for(uint i = 0; i < m; i++)
102     matrix[0][i] = i;
103
104   uint min;
105   for(uint i = 1; i < n; i++) {
106     for(uint j = 1; j < m; j++) {
107       uint deleteChar = matrix[i-1][j] + 1;
108       uint insertChar = matrix[i][j-1] + 1;
109
110       if(deleteChar < insertChar)
111         min = deleteChar;
112       else
113         min = insertChar;
114
115       if(s1[i-1] == s2[j-1]) {
116         uint inheritChar = matrix[i-1][j-1];
117         if(inheritChar < min)
118           min = inheritChar;
119       }
120
121       matrix[i][j] = min;
122     }
123   }
124   return matrix[n-1][m-1];
125 }
126
127 QString secondsToString(int timeInSeconds) {
128     QList< QPair<int, QString> > timeUnit;
129     timeUnit.append(qMakePair(365*24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")));
130     timeUnit.append(qMakePair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")));
131     timeUnit.append(qMakePair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")));
132     timeUnit.append(qMakePair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")));
133     timeUnit.append(qMakePair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec")));
134
135     QStringList returnString;
136     for(int i=0; i < timeUnit.size(); i++) {
137       int n = timeInSeconds / timeUnit[i].first;
138       if(n > 0) {
139         returnString += QString("%1 %2").arg(QString::number(n), timeUnit[i].second);
140       }
141       timeInSeconds = timeInSeconds % timeUnit[i].first;
142     }
143     return returnString.join(", ");
144 }
145
146 QByteArray prettyDigest(const QByteArray &digest) {
147   QByteArray hexDigest = digest.toHex();
148   QByteArray prettyDigest;
149   prettyDigest.fill(':', hexDigest.count() + (hexDigest.count() / 2) - 1);
150
151   for(int i = 0; i * 2 < hexDigest.count(); i++) {
152     prettyDigest.replace(i * 3, 2, hexDigest.mid(i * 2, 2));
153   }
154   return prettyDigest;
155 }