This branch introduces a new sync method which should use less bandwidth and might...
[quassel.git] / src / common / util.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QCoreApplication>
22 #include <QDebug>
23 #include <QTextCodec>
24
25 #include "util.h"
26
27 class QMetaMethod;
28
29 QString nickFromMask(QString mask) {
30   return mask.section('!', 0, 0);
31 }
32
33 QString userFromMask(QString mask) {
34   QString userhost = mask.section('!', 1);
35   if(userhost.isEmpty()) return QString();
36   return userhost.section('@', 0, 0);
37 }
38
39 QString hostFromMask(QString mask) {
40   QString userhost = mask.section('!', 1);
41   if(userhost.isEmpty()) return QString();
42   return userhost.section('@', 1);
43 }
44
45 bool isChannelName(QString str) {
46   return QString("#&!+").contains(str[0]);
47 }
48
49 QString decodeString(const QByteArray &input, QTextCodec *codec) {
50   // First, we check if it's utf8. It is very improbable to encounter a string that looks like
51   // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
52   // is safe to assume that it is.
53   // Q_ASSERT(sizeof(const char) == sizeof(quint8));  // In God we trust...
54   bool isUtf8 = true;
55   int cnt = 0;
56   for(int i = 0; i < input.size(); i++) {
57     if(cnt) {
58       // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
59       if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
60       cnt--;
61       continue;
62     }
63     if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
64     if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; }  // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
65     if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; }  // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
66     if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; }  // 2-byte char 110xxxxx 10yyyyyy
67     isUtf8 = false; break;  // 8 bit char, but not utf8!
68   }
69   if(isUtf8 && cnt == 0) {
70     QString s = QString::fromUtf8(input);
71     //qDebug() << "Detected utf8:" << s;
72     return s;
73   }
74   //QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
75   if(!codec) return QString::fromAscii(input);
76   return codec->toUnicode(input);
77 }
78
79 /* not needed anymore
80 void writeDataToDevice(QIODevice *dev, const QVariant &item) {
81   QByteArray block;
82   QDataStream out(&block, QIODevice::WriteOnly);
83   out.setVersion(QDataStream::Qt_4_2);
84   out << (quint32)0 << item;
85   out.device()->seek(0);
86   out << (quint32)(block.size() - sizeof(quint32));
87   dev->write(block);
88 }
89
90 bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item) {
91   QDataStream in(dev);
92   in.setVersion(QDataStream::Qt_4_2);
93
94   if(blockSize == 0) {
95     if(dev->bytesAvailable() < (int)sizeof(quint32)) return false;
96     in >> blockSize;
97   }
98   if(dev->bytesAvailable() < blockSize) return false;
99   in >> item;
100   return true;
101 }
102 */
103
104 uint editingDistance(const QString &s1, const QString &s2) {
105   uint n = s1.size()+1;
106   uint m = s2.size()+1;
107   //uint matrix[n][m];
108   QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
109
110   for(uint i = 0; i < n; i++)
111     matrix[i][0] = i;
112
113   for(uint i = 0; i < m; i++)
114     matrix[0][i] = i;
115
116   uint min;
117   for(uint i = 1; i < n; i++) {
118     for(uint j = 1; j < m; j++) {
119       uint deleteChar = matrix[i-1][j] + 1;
120       uint insertChar = matrix[i][j-1] + 1;
121
122       if(deleteChar < insertChar)
123         min = deleteChar;
124       else
125         min = insertChar;
126       
127       if(s1[i-1] == s2[j-1]) {
128         uint inheritChar = matrix[i-1][j-1];
129         if(inheritChar < min)
130           min = inheritChar;
131       }
132
133       matrix[i][j] = min;
134     }
135   }
136   return matrix[n-1][m-1];
137 }
138
139 QByteArray methodName(const QMetaMethod &method) {
140   QByteArray sig(method.signature());
141   return sig.left(sig.indexOf("("));
142 }
143
144 QDir quasselDir() {
145   // kinda ugly, but I currently see no other way to do that
146 #ifdef Q_OS_WIN32
147   QString quasselDir = qgetenv("APPDATA") + "/quassel/";
148 #else
149   QString quasselDir = QDir::homePath() + "/.quassel/";
150 #endif
151
152   QDir qDir(quasselDir);
153   if(!qDir.exists(quasselDir)) {
154     if(!qDir.mkpath(quasselDir)) {
155       qCritical() << "Unable to create Quassel data directory:" << qPrintable(qDir.absolutePath());
156     }
157   }
158
159   return qDir;
160 }
161
162
163 QString secondsToString(int timeInSeconds) {
164     QList< QPair<int, QString> > timeUnit;
165     timeUnit.append(qMakePair(365*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")));
166     timeUnit.append(qMakePair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")));
167     timeUnit.append(qMakePair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")));
168     timeUnit.append(qMakePair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")));
169     timeUnit.append(qMakePair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec")));
170
171     QString returnString;
172     for(int i=0; i < timeUnit.size(); i++) {
173       int n = timeInSeconds / timeUnit[i].first;
174       if(n > 0) {
175         returnString += QString("%1 %2 ").arg(QString::number(n), timeUnit[i].second);
176       }
177       timeInSeconds = timeInSeconds % timeUnit[i].first;
178     }
179     return returnString;
180 }