27 #include "plugins/engineplugin.h"
28 #include "plugins/pluginloader.h"
35 #include <QStringList>
38 const char Strings::RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE] =
40 'a',
'b',
'c',
'd',
'e',
'f',
'g',
41 'h',
'i',
'j',
'k',
'l',
'm',
'n',
42 'o',
'p',
'q',
'r',
's',
't',
'u',
43 'v',
'w',
'x',
'y',
'z',
'0',
'1',
44 '2',
'3',
'4',
'5',
'6',
'7',
'8',
50 static const char colorChart[22][7] =
78 for(
int i = 0;i < str.length();i++)
80 if(str[i] == ESCAPE_COLOR_CHAR)
85 QChar colorChar = str[i].toLower();
86 int color = colorChar.toAscii() - 97;
90 color = current == 0 ? 19 : current-1;
91 else if(colorChar ==
'*')
93 else if(colorChar ==
'!')
95 else if(colorChar ==
'[')
97 int end = str.indexOf(
']', i);
100 QString colorName = str.mid(i+1, end-i-1);
101 if(colorName.indexOf(
'"') == -1)
102 ret += QString(
"<span style=\"color: " + colorName +
"\">");
103 i += colorName.length()+1;
107 else if(colorChar ==
'-')
121 if(color >= 0 && color < 22)
123 ret += QString(
"<span style=\"color: #") + colorChart[color] +
"\">";
138 foreach (
const QString &s, fronts)
147 QString combinedPath;
150 if (pathFront.isEmpty())
155 if (pathEnd.isEmpty())
160 pathFront = Strings::trimr(pathFront,
"/\\");
161 pathEnd = Strings::triml(pathEnd,
"/\\");
163 combinedPath = pathFront +
"/" + pathEnd;
171 QString generatedString =
"";
172 for (
unsigned i = 0; i < numChars; ++i)
175 generatedString += RANDOM_CHAR_POOL[index];
178 return generatedString;
181 QString Strings::createRandomAlphaNumericStringWithNewLines(
unsigned numCharsPerLine,
unsigned numLines)
183 QString generatedString =
"";
184 for (
unsigned i = 0; i < numLines; ++i)
189 return generatedString;
193 static char escapeCharacters[] = {
'\\',
'"', 0};
196 for(
unsigned int i = 0;escapeCharacters[i] != 0;i++)
199 for(
int p = 0;p < str.length() && (p = str.indexOf(escapeCharacters[i], p)) != -1;p += 2)
206 const QString& Strings::unescape(QString &str)
208 for(
unsigned int i = 0;escapeCharacters[i] != 0;i++)
210 QString sequence =
"\\" + QString(escapeCharacters[i]);
211 for(
int p = 0;p < str.length() && (p = str.indexOf(sequence, p)) != -1;p++)
212 str.replace(str.indexOf(sequence, p), 2, escapeCharacters[i]);
221 float fBytes = (float)bytes;
222 fBytes = scaleDataUnit(fBytes, dataUnit);
224 QString formattedString = QString(
"%1 ").arg(fBytes, 0,
'f', 2);
228 formattedString +=
"B";
232 formattedString +=
"kB";
236 formattedString +=
"MB";
240 formattedString +=
"GB";
245 return "#ERR: Formatting data amount error.";
248 return formattedString;
255 speedInBytesPerSecond = scaleDataUnit(speedInBytesPerSecond, dataUnit);
257 QString formattedString = QString(
"%1 ").arg(speedInBytesPerSecond, 0,
'f', 2);
261 formattedString +=
"B/s";
265 formattedString +=
"kB/s";
269 formattedString +=
"MB/s";
273 formattedString +=
"GB/s";
278 return "#ERR: Formatting speed error.";
281 return formattedString;
288 return "#ERR: Formatting time error.";
291 seconds = ceil(seconds);
298 int remainingSeconds = 0;
300 if (seconds >= 3600.0f)
303 hours = seconds / 3600.0f;
304 seconds -= hours * 3600.0f;
307 if (seconds >= 60.0f)
310 minutes = seconds / 60.0f;
311 seconds -= minutes * 60.0f;
314 remainingSeconds = (int)seconds;
316 QString formattedString;
319 formattedString += QString(
"%1h ").arg(hours);
322 if (hours > 0 || minutes > 0)
324 formattedString += QString(
"%1min. ").arg(minutes);
327 formattedString += QString(
"%1s").arg(remainingSeconds);
329 return formattedString;
332 bool Strings::isCharOnCharList(
char c,
const QString& charList)
334 for (
int i = 0; i < charList.length(); ++i)
336 if (charList[i] == c)
345 QUrl urlObject = url;
347 QString scheme = urlObject.scheme();
349 bool bIsSafe1 = scheme.isEmpty();
350 bool bIsSafe2 = (scheme.compare(
"http", Qt::CaseInsensitive) == 0);
351 bool bIsSafe3 = (scheme.compare(
"ftp", Qt::CaseInsensitive) == 0);
354 return bIsSafe1 || bIsSafe2 || bIsSafe3;
359 path = QDir::fromNativeSeparators(path);
360 path = QDir::cleanPath(path);
368 bool bStopByteEncountered =
false;
370 while (!stream.atEnd() && !bStopByteEncountered)
376 if (rByte == stopByte)
378 bStopByteEncountered =
true;
385 float Strings::scaleDataUnit(
float bytes, DataUnit& outUnit)
387 const static float UPPER_BOUNDARY = 900.0f;
390 while (bytes > UPPER_BOUNDARY && outUnit != Gigabyte)
393 outUnit = (DataUnit)((
int)outUnit + 1);
399 QString Strings::timestamp(
const QString& format)
401 return QDateTime::currentDateTime().toString(format);
407 QStringList addressAndPort = addressString.split(
":");
408 QStringList defaultAddressAndPort = defaultAddress.split(
":");
410 if (addressAndPort.size() >= 1 && addressAndPort.size() <= 2)
412 hostname = addressAndPort[0];
413 if (addressAndPort.size() == 2)
415 port = addressAndPort[1].toUShort();
421 if (defaultAddressAndPort.size() >= 1)
423 hostname = defaultAddressAndPort[0];
427 if (port == 0 && defaultAddressAndPort.size() >= 2)
429 port = defaultAddressAndPort[1].toUShort();
433 QString& Strings::trimr(QString& str,
const QString& charList)
436 for (i = str.length() - 1; i >= 0; --i)
438 if (!isCharOnCharList(str[i].toAscii(), charList))
443 return str.remove(i, str.length() - i);
446 QString& Strings::triml(QString& str,
const QString& charList)
449 for (i = 0; i < str.length(); ++i)
451 if (!isCharOnCharList(str[i].toAscii(), charList))
455 return str.remove(0, i);
460 static QString pluginSchemes;
461 if(pluginSchemes.isEmpty())
463 pluginSchemes =
"zds";
465 for(
unsigned int i = 0;i < gPlugins->numPlugins();++i)
466 pluginSchemes = QString(
"%1|%2").arg(pluginSchemes)
467 .arg(gPlugins->plugin(i)->info()->data()->scheme);
470 QRegExp pattern(QString(
"("
472 "(http|https|ftp|%1)://"
474 ")[\\w\\-\\.,@?^=%&:/~\\+#\\(\\)]+"
475 ")").arg(pluginSchemes), Qt::CaseInsensitive);
476 QString newString = str;
480 while ((index = pattern.indexIn(newString, offset)) >= 0)
482 QString cap = pattern.cap(1);
483 int capLength = cap.length();
485 QString replacement = cap;
486 if (cap.startsWith(
"www.", Qt::CaseInsensitive))
488 replacement =
"http://" + cap;
491 replacement = QString(
"<a href=\"%1\">%2</a>").arg(replacement, cap);
493 newString.replace(index, capLength, replacement);
494 offset = index + replacement.length();
static unsigned short nextUShort(unsigned short max)
Generates a new random unsigned short.
static QString combinePaths(QString pathFront, QString pathEnd)
static void translateServerAddress(const QString &addressString, QString &hostname, unsigned short &port, const QString &defaultAddress)
Translates string in format "hostname:port" to atomic values.
static QString formatTime(float seconds)
Formats a numerical time value into a string.
static QString normalizePath(QString path)
Creates a clean path.
static QString wrapUrlsWithHtmlATags(const QString &str)
Detects all links within a given string and wraps them in <a href> tags.
static bool isUrlSafe(const QString &url)
Unsafe URLs begin with file:// and this functions returns false for such URLs.
static QString formatDataAmount(qint64 bytes)
Similar to formatDataSpeed().
static QString formatDataSpeed(float speedInBytesPerSecond)
Formats a numerical speed value into a string.
static QByteArray readUntilByte(QDataStream &stream, unsigned char stopByte)
Reads raw data from the current position of passed QDataStream until a specified byte is encountered...
static const QString & escape(QString &str)
Adds escape characters to a string.
static QStringList combineManyPaths(const QStringList &fronts, const QString &pathEnd)
Combines path suffix with all fronts, returns new list.
static QString colorizeString(const QString &str, int def=4)
static QString createRandomAlphaNumericString(unsigned numChars)
Creates a random string with specified length.