fixing a bug where quit messages from different networks were shown if the user has...
[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 "util.h"
22
23 #include <QCoreApplication>
24 #include <QDebug>
25 #include <QTextCodec>
26 #include <QTranslator>
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 decodeString(const QByteArray &input, QTextCodec *codec) {
53   // First, we check if it's utf8. It is very improbable to encounter a string that looks like
54   // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
55   // is safe to assume that it is.
56   // Q_ASSERT(sizeof(const char) == sizeof(quint8));  // In God we trust...
57   bool isUtf8 = true;
58   int cnt = 0;
59   for(int i = 0; i < input.size(); i++) {
60     if(cnt) {
61       // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
62       if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
63       cnt--;
64       continue;
65     }
66     if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
67     if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; }  // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
68     if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; }  // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
69     if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; }  // 2-byte char 110xxxxx 10yyyyyy
70     isUtf8 = false; break;  // 8 bit char, but not utf8!
71   }
72   if(isUtf8 && cnt == 0) {
73     QString s = QString::fromUtf8(input);
74     //qDebug() << "Detected utf8:" << s;
75     return s;
76   }
77   //QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
78   if(!codec) return QString::fromAscii(input);
79   return codec->toUnicode(input);
80 }
81
82 /* not needed anymore
83 void writeDataToDevice(QIODevice *dev, const QVariant &item) {
84   QByteArray block;
85   QDataStream out(&block, QIODevice::WriteOnly);
86   out.setVersion(QDataStream::Qt_4_2);
87   out << (quint32)0 << item;
88   out.device()->seek(0);
89   out << (quint32)(block.size() - sizeof(quint32));
90   dev->write(block);
91 }
92
93 bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item) {
94   QDataStream in(dev);
95   in.setVersion(QDataStream::Qt_4_2);
96
97   if(blockSize == 0) {
98     if(dev->bytesAvailable() < (int)sizeof(quint32)) return false;
99     in >> blockSize;
100   }
101   if(dev->bytesAvailable() < blockSize) return false;
102   in >> item;
103   return true;
104 }
105 */
106
107 uint editingDistance(const QString &s1, const QString &s2) {
108   uint n = s1.size()+1;
109   uint m = s2.size()+1;
110   QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
111
112   for(uint i = 0; i < n; i++)
113     matrix[i][0] = i;
114
115   for(uint i = 0; i < m; i++)
116     matrix[0][i] = i;
117
118   uint min;
119   for(uint i = 1; i < n; i++) {
120     for(uint j = 1; j < m; j++) {
121       uint deleteChar = matrix[i-1][j] + 1;
122       uint insertChar = matrix[i][j-1] + 1;
123
124       if(deleteChar < insertChar)
125         min = deleteChar;
126       else
127         min = insertChar;
128
129       if(s1[i-1] == s2[j-1]) {
130         uint inheritChar = matrix[i-1][j-1];
131         if(inheritChar < min)
132           min = inheritChar;
133       }
134
135       matrix[i][j] = min;
136     }
137   }
138   return matrix[n-1][m-1];
139 }
140
141 QByteArray methodName(const QMetaMethod &method) {
142   QByteArray sig(method.signature());
143   return sig.left(sig.indexOf("("));
144 }
145
146 QDir quasselDir() {
147   QString quasselDir;
148   if(Quassel::isOptionSet("datadir")) {
149     quasselDir = Quassel::optionValue("datadir");
150   } else {
151     // FIXME use QDesktopServices
152 #ifdef Q_OS_WIN32
153     quasselDir = qgetenv("APPDATA") + "/quassel/";
154 #elif defined Q_WS_MAC
155     quasselDir = QDir::homePath() + "/Library/Application Support/Quassel/";
156 #else
157     quasselDir = QDir::homePath() + "/.quassel/";
158 #endif
159   }
160
161   QDir qDir(quasselDir);
162   if(!qDir.exists(quasselDir)) {
163     if(!qDir.mkpath(quasselDir)) {
164       qCritical() << "Unable to create Quassel data directory:" << qPrintable(qDir.absolutePath());
165     }
166   }
167
168   return qDir;
169 }
170
171 void loadTranslation(const QLocale &locale) {
172   QTranslator *qtTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QtTr");
173   QTranslator *quasselTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QuasselTr");
174   Q_ASSERT(qtTranslator);
175   Q_ASSERT(quasselTranslator);
176
177   QLocale::setDefault(locale);
178
179   QCoreApplication::removeTranslator(qtTranslator);
180   QCoreApplication::removeTranslator(quasselTranslator);
181
182   if(locale.language() == QLocale::C)
183     return;
184
185   qtTranslator->load(QString(":i18n/qt_%1").arg(locale.name()));
186   quasselTranslator->load(QString(":i18n/quassel_%1").arg(locale.name()));
187
188   QCoreApplication::installTranslator(qtTranslator);
189   QCoreApplication::installTranslator(quasselTranslator);
190 }
191
192 QString secondsToString(int timeInSeconds) {
193     QList< QPair<int, QString> > timeUnit;
194     timeUnit.append(qMakePair(365*24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")));
195     timeUnit.append(qMakePair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")));
196     timeUnit.append(qMakePair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")));
197     timeUnit.append(qMakePair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")));
198     timeUnit.append(qMakePair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec")));
199
200     QStringList returnString;
201     for(int i=0; i < timeUnit.size(); i++) {
202       int n = timeInSeconds / timeUnit[i].first;
203       if(n > 0) {
204         returnString += QString("%1 %2").arg(QString::number(n), timeUnit[i].second);
205       }
206       timeInSeconds = timeInSeconds % timeUnit[i].first;
207     }
208     return returnString.join(", ");
209 }