27 #include "plugins/engineplugin.h" 28 #include "plugins/pluginloader.h" 29 #include "datastreamoperatorwrapper.h" 34 #include <QDataStream> 37 #include <QStringList> 40 const char Strings::RANDOM_CHAR_POOL[RANDOM_CHAR_POOL_SIZE] =
42 'a',
'b',
'c',
'd',
'e',
'f',
'g',
43 'h',
'i',
'j',
'k',
'l',
'm',
'n',
44 'o',
'p',
'q',
'r',
's',
't',
'u',
45 'v',
'w',
'x',
'y',
'z',
'0',
'1',
46 '2',
'3',
'4',
'5',
'6',
'7',
'8',
52 static const char colorChart[22][7] =
80 for(
int i = 0;i < str.length();i++)
82 if(str[i] == ESCAPE_COLOR_CHAR)
87 QChar colorChar = str[i].toLower();
88 int color = colorChar.toLatin1() - 97;
92 color = current == 0 ? 19 : current-1;
93 else if(colorChar ==
'*')
95 else if(colorChar ==
'!')
97 else if(colorChar ==
'[')
99 int end = str.indexOf(
']', i);
102 QString colorName = str.mid(i+1, end-i-1);
103 if(colorName.indexOf(
'"') == -1)
104 ret += QString(
"<span style=\"color: " + colorName +
"\">");
105 i += colorName.length()+1;
109 else if(colorChar ==
'-')
123 if(color >= 0 && color < 22)
125 ret += QString(
"<span style=\"color: #") + colorChart[color] +
"\">";
140 foreach (
const QString &s, fronts)
149 QString combinedPath;
152 if (pathFront.isEmpty())
157 if (pathEnd.isEmpty())
162 pathFront = Strings::trimr(pathFront,
"/\\");
163 pathEnd = Strings::triml(pathEnd,
"/\\");
165 combinedPath = pathFront +
"/" + pathEnd;
173 QString generatedString =
"";
174 for (
unsigned i = 0; i < numChars; ++i)
177 generatedString += RANDOM_CHAR_POOL[index];
180 return generatedString;
183 QString Strings::createRandomAlphaNumericStringWithNewLines(
unsigned numCharsPerLine,
unsigned numLines)
185 QString generatedString =
"";
186 for (
unsigned i = 0; i < numLines; ++i)
191 return generatedString;
195 static char escapeCharacters[] = {
'\\',
'"', 0};
198 for(
unsigned int i = 0;escapeCharacters[i] != 0;i++)
201 for(
int p = 0;p < str.length() && (p = str.indexOf(escapeCharacters[i], p)) != -1;p += 2)
208 const QString& Strings::unescape(QString &str)
210 for(
unsigned int i = 0;escapeCharacters[i] != 0;i++)
212 QString sequence =
"\\" + QString(escapeCharacters[i]);
213 for(
int p = 0;p < str.length() && (p = str.indexOf(sequence, p)) != -1;p++)
214 str.replace(str.indexOf(sequence, p), 2, escapeCharacters[i]);
223 float fBytes = (float)bytes;
224 fBytes = scaleDataUnit(fBytes, dataUnit);
226 QString formattedString = QString(
"%1 ").arg(fBytes, 0,
'f', 2);
230 formattedString +=
"B";
234 formattedString +=
"kB";
238 formattedString +=
"MB";
242 formattedString +=
"GB";
247 return "#ERR: Formatting data amount error.";
250 return formattedString;
257 speedInBytesPerSecond = scaleDataUnit(speedInBytesPerSecond, dataUnit);
259 QString formattedString = QString(
"%1 ").arg(speedInBytesPerSecond, 0,
'f', 2);
263 formattedString +=
"B/s";
267 formattedString +=
"kB/s";
271 formattedString +=
"MB/s";
275 formattedString +=
"GB/s";
280 return "#ERR: Formatting speed error.";
283 return formattedString;
290 return "#ERR: Formatting time error.";
293 seconds = ceil(seconds);
300 int remainingSeconds = 0;
302 if (seconds >= 3600.0f)
305 hours = seconds / 3600.0f;
306 seconds -= hours * 3600.0f;
309 if (seconds >= 60.0f)
312 minutes = seconds / 60.0f;
313 seconds -= minutes * 60.0f;
316 remainingSeconds = (int)seconds;
318 QString formattedString;
321 formattedString += QString(
"%1h ").arg(hours);
324 if (hours > 0 || minutes > 0)
326 formattedString += QString(
"%1min. ").arg(minutes);
329 formattedString += QString(
"%1s").arg(remainingSeconds);
331 return formattedString;
334 bool Strings::isCharOnCharList(
char c,
const QString& charList)
336 for (
int i = 0; i < charList.length(); ++i)
338 if (charList[i] == c)
347 QUrl urlObject = url;
349 QString scheme = urlObject.scheme();
351 bool bIsSafe1 = scheme.isEmpty();
352 bool bIsSafe2 = (scheme.compare(
"http", Qt::CaseInsensitive) == 0);
353 bool bIsSafe3 = (scheme.compare(
"ftp", Qt::CaseInsensitive) == 0);
356 return bIsSafe1 || bIsSafe2 || bIsSafe3;
361 path = QDir::fromNativeSeparators(path);
362 path = QDir::cleanPath(path);
373 float Strings::scaleDataUnit(
float bytes, DataUnit& outUnit)
375 const static float UPPER_BOUNDARY = 900.0f;
378 while (bytes > UPPER_BOUNDARY && outUnit != Gigabyte)
381 outUnit = (DataUnit)((
int)outUnit + 1);
387 QString Strings::timestamp(
const QString& format)
389 return QDateTime::currentDateTime().toString(format);
395 QStringList addressAndPort = addressString.split(
":");
396 QStringList defaultAddressAndPort = defaultAddress.split(
":");
398 if (addressAndPort.size() >= 1 && addressAndPort.size() <= 2)
400 hostname = addressAndPort[0];
401 if (addressAndPort.size() == 2)
403 port = addressAndPort[1].toUShort();
409 if (defaultAddressAndPort.size() >= 1)
411 hostname = defaultAddressAndPort[0];
415 if (port == 0 && defaultAddressAndPort.size() >= 2)
417 port = defaultAddressAndPort[1].toUShort();
421 QString& Strings::trimr(QString& str,
const QString& charList)
424 for (i = str.length() - 1; i >= 0; --i)
426 if (!isCharOnCharList(str[i].toLatin1(), charList))
431 return str.remove(i, str.length() - i);
434 QString& Strings::triml(QString& str,
const QString& charList)
437 for (i = 0; i < str.length(); ++i)
439 if (!isCharOnCharList(str[i].toLatin1(), charList))
443 return str.remove(0, i);
448 static QString pluginSchemes;
449 if(pluginSchemes.isEmpty())
451 pluginSchemes =
"zds";
453 for(
unsigned int i = 0;i < gPlugins->numPlugins();++i)
454 pluginSchemes = QString(
"%1|%2").arg(pluginSchemes)
455 .arg(gPlugins->plugin(i)->info()->data()->scheme);
458 QRegExp pattern(QString(
"(" 460 "(http|https|ftp|%1)://" 462 ")[\\w\\-\\.,@?^=%&:/~\\+#\\(\\)]+" 463 ")").arg(pluginSchemes), Qt::CaseInsensitive);
464 QString newString = str;
468 while ((index = pattern.indexIn(newString, offset)) >= 0)
470 QString cap = pattern.cap(1);
471 int capLength = cap.length();
473 QString replacement = cap;
474 if (cap.startsWith(
"www.", Qt::CaseInsensitive))
476 replacement =
"http://" + cap;
479 replacement = QString(
"<a href=\"%1\">%2</a>").arg(replacement, cap);
481 newString.replace(index, capLength, replacement);
482 offset = index + replacement.length();
static unsigned short nextUShort(unsigned short max)
Generates a new random unsigned short.
QByteArray readRawUntilByte(char stopByte)
Reads raw data from the current position of passed QDataStream until a specified byte is encountered...
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.
Wraps around QDataStream stream operators to provide cleaner reading interface.
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)
This method calls DataStreamOperatorWrapper::readRawUntilByte() .
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.