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