1 /***************************************************************************
2 * Copyright (C) 2005-09 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 <QLibraryInfo>
28 #include <QTranslator>
34 QString nickFromMask(QString mask) {
35 return mask.section('!', 0, 0);
38 QString userFromMask(QString mask) {
39 QString userhost = mask.section('!', 1);
40 if(userhost.isEmpty()) return QString();
41 return userhost.section('@', 0, 0);
44 QString hostFromMask(QString mask) {
45 QString userhost = mask.section('!', 1);
46 if(userhost.isEmpty()) return QString();
47 return userhost.section('@', 1);
50 bool isChannelName(QString str) {
51 return QString("#&!+").contains(str[0]);
54 QString stripFormatCodes(QString str) {
55 str.remove(QRegExp("\x03(\\d\\d?(,\\d\\d?)?)?"));
65 QString decodeString(const QByteArray &input, QTextCodec *codec) {
66 // First, we check if it's utf8. It is very improbable to encounter a string that looks like
67 // valid utf8, but in fact is not. This means that if the input string passes as valid utf8, it
68 // is safe to assume that it is.
69 // Q_ASSERT(sizeof(const char) == sizeof(quint8)); // In God we trust...
72 for(int i = 0; i < input.size(); i++) {
74 // We check a part of a multibyte char. These need to be of the form 10yyyyyy.
75 if((input[i] & 0xc0) != 0x80) { isUtf8 = false; break; }
79 if((input[i] & 0x80) == 0x00) continue; // 7 bit is always ok
80 if((input[i] & 0xf8) == 0xf0) { cnt = 3; continue; } // 4-byte char 11110xxx 10yyyyyy 10zzzzzz 10vvvvvv
81 if((input[i] & 0xf0) == 0xe0) { cnt = 2; continue; } // 3-byte char 1110xxxx 10yyyyyy 10zzzzzz
82 if((input[i] & 0xe0) == 0xc0) { cnt = 1; continue; } // 2-byte char 110xxxxx 10yyyyyy
83 isUtf8 = false; break; // 8 bit char, but not utf8!
85 if(isUtf8 && cnt == 0) {
86 QString s = QString::fromUtf8(input);
87 //qDebug() << "Detected utf8:" << s;
90 //QTextCodec *codec = QTextCodec::codecForName(encoding.toAscii());
91 if(!codec) return QString::fromAscii(input);
92 return codec->toUnicode(input);
95 uint editingDistance(const QString &s1, const QString &s2) {
98 QVector< QVector< uint > >matrix(n,QVector<uint>(m,0));
100 for(uint i = 0; i < n; i++)
103 for(uint i = 0; i < m; i++)
107 for(uint i = 1; i < n; i++) {
108 for(uint j = 1; j < m; j++) {
109 uint deleteChar = matrix[i-1][j] + 1;
110 uint insertChar = matrix[i][j-1] + 1;
112 if(deleteChar < insertChar)
117 if(s1[i-1] == s2[j-1]) {
118 uint inheritChar = matrix[i-1][j-1];
119 if(inheritChar < min)
126 return matrix[n-1][m-1];
129 void loadTranslation(const QLocale &locale) {
130 QTranslator *qtTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QtTr");
131 QTranslator *quasselTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QuasselTr");
134 qtTranslator = new QTranslator(qApp);
135 qtTranslator->setObjectName("QtTr");
136 qApp->installTranslator(qtTranslator);
138 if(!quasselTranslator) {
139 quasselTranslator = new QTranslator(qApp);
140 quasselTranslator->setObjectName("QuasselTr");
141 qApp->installTranslator(quasselTranslator);
144 QLocale::setDefault(locale);
146 if(locale.language() == QLocale::C)
149 bool success = qtTranslator->load(QString(":i18n/qt_%1").arg(locale.name()));
151 qtTranslator->load(QString("%2/qt_%1").arg(locale.name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)));
152 quasselTranslator->load(QString(":i18n/quassel_%1").arg(locale.name()));
155 QString secondsToString(int timeInSeconds) {
156 QList< QPair<int, QString> > timeUnit;
157 timeUnit.append(qMakePair(365*24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "year")));
158 timeUnit.append(qMakePair(24*60*60, QCoreApplication::translate("Quassel::secondsToString()", "day")));
159 timeUnit.append(qMakePair(60*60, QCoreApplication::translate("Quassel::secondsToString()", "h")));
160 timeUnit.append(qMakePair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")));
161 timeUnit.append(qMakePair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec")));
163 QStringList returnString;
164 for(int i=0; i < timeUnit.size(); i++) {
165 int n = timeInSeconds / timeUnit[i].first;
167 returnString += QString("%1 %2").arg(QString::number(n), timeUnit[i].second);
169 timeInSeconds = timeInSeconds % timeUnit[i].first;
171 return returnString.join(", ");