1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include <QCoreApplication>
26 #include <QTranslator>
32 QString nickFromMask(QString mask) {
33 return mask.section('!', 0, 0);
36 QString userFromMask(QString mask) {
37 QString userhost = mask.section('!', 1);
38 if(userhost.isEmpty()) return QString();
39 return userhost.section('@', 0, 0);
42 QString hostFromMask(QString mask) {
43 QString userhost = mask.section('!', 1);
44 if(userhost.isEmpty()) return QString();
45 return userhost.section('@', 1);
48 bool isChannelName(QString str) {
49 return QString("#&!+").contains(str[0]);
52 QString stripFormatCodes(QString str) {
53 str.remove(QRegExp("\x03(\\d\\d?(,\\d\\d?)?)?"));
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...
70 for(int i = 0; i < input.size(); i++) {
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; }
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!
83 if(isUtf8 && cnt == 0) {
84 QString s = QString::fromUtf8(input);
85 //qDebug() << "Detected utf8:" << s;
88 //QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
89 if(!codec) return QString::fromAscii(input);
90 return codec->toUnicode(input);
94 void writeDataToDevice(QIODevice *dev, const QVariant &item) {
96 QDataStream out(&block, QIODevice::WriteOnly);
97 out.setVersion(QDataStream::Qt_4_2);
98 out << (quint32)0 << item;
99 out.device()->seek(0);
100 out << (quint32)(block.size() - sizeof(quint32));
104 bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item) {
106 in.setVersion(QDataStream::Qt_4_2);
109 if(dev->bytesAvailable() < (int)sizeof(quint32)) return false;
112 if(dev->bytesAvailable() < blockSize) return false;
118 uint editingDistance(const QString &s1, const QString &s2) {
119 uint n = s1.size()+1;
120 uint m = s2.size()+1;
121 QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
123 for(uint i = 0; i < n; i++)
126 for(uint i = 0; i < m; i++)
130 for(uint i = 1; i < n; i++) {
131 for(uint j = 1; j < m; j++) {
132 uint deleteChar = matrix[i-1][j] + 1;
133 uint insertChar = matrix[i][j-1] + 1;
135 if(deleteChar < insertChar)
140 if(s1[i-1] == s2[j-1]) {
141 uint inheritChar = matrix[i-1][j-1];
142 if(inheritChar < min)
149 return matrix[n-1][m-1];
152 QByteArray methodName(const QMetaMethod &method) {
153 QByteArray sig(method.signature());
154 return sig.left(sig.indexOf("("));
159 if(Quassel::isOptionSet("datadir")) {
160 quasselDir = Quassel::optionValue("datadir");
162 // FIXME use QDesktopServices
164 quasselDir = qgetenv("APPDATA") + "/quassel/";
165 #elif defined Q_WS_MAC
166 quasselDir = QDir::homePath() + "/Library/Application Support/Quassel/";
168 quasselDir = QDir::homePath() + "/.quassel/";
172 QDir qDir(quasselDir);
173 if(!qDir.exists(quasselDir)) {
174 if(!qDir.mkpath(quasselDir)) {
175 qCritical() << "Unable to create Quassel data directory:" << qPrintable(qDir.absolutePath());
182 void loadTranslation(const QLocale &locale) {
183 QTranslator *qtTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QtTr");
184 QTranslator *quasselTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QuasselTr");
185 Q_ASSERT(qtTranslator);
186 Q_ASSERT(quasselTranslator);
188 QLocale::setDefault(locale);
190 QCoreApplication::removeTranslator(qtTranslator);
191 QCoreApplication::removeTranslator(quasselTranslator);
193 if(locale.language() == QLocale::C)
196 qtTranslator->load(QString(":i18n/qt_%1").arg(locale.name()));
197 quasselTranslator->load(QString(":i18n/quassel_%1").arg(locale.name()));
199 QCoreApplication::installTranslator(qtTranslator);
200 QCoreApplication::installTranslator(quasselTranslator);
203 QString secondsToString(int timeInSeconds) {
204 QList< QPair<int, QString> > timeUnit;
205 timeUnit.append(qMakePair(365*24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")));
206 timeUnit.append(qMakePair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")));
207 timeUnit.append(qMakePair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")));
208 timeUnit.append(qMakePair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")));
209 timeUnit.append(qMakePair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec")));
211 QStringList returnString;
212 for(int i=0; i < timeUnit.size(); i++) {
213 int n = timeInSeconds / timeUnit[i].first;
215 returnString += QString("%1 %2").arg(QString::number(n), timeUnit[i].second);
217 timeInSeconds = timeInSeconds % timeUnit[i].first;
219 return returnString.join(", ");