+QString secondsToString(int timeInSeconds)
+{
+ static QVector<std::pair<int, QString>> timeUnit{std::make_pair(365 * 24 * 60 * 60,
+ QCoreApplication::translate("Quassel::secondsToString()", "year")),
+ std::make_pair(24 * 60 * 60,
+ QCoreApplication::translate("Quassel::secondsToString()", "day")),
+ std::make_pair(60 * 60, QCoreApplication::translate("Quassel::secondsToString()", "h")),
+ std::make_pair(60, QCoreApplication::translate("Quassel::secondsToString()", "min")),
+ std::make_pair(1, QCoreApplication::translate("Quassel::secondsToString()", "sec"))};
+
+ if (timeInSeconds != 0) {
+ QStringList returnString;
+ for (const auto& tu : timeUnit) {
+ int n = timeInSeconds / tu.first;
+ if (n > 0) {
+ returnString += QString("%1 %2").arg(QString::number(n), tu.second);
+ }
+ timeInSeconds = timeInSeconds % tu.first;
+ }
+ return returnString.join(", ");
+ }
+
+ return QString("%1 %2").arg(QString::number(timeInSeconds), timeUnit.last().second);
+}
+
+QByteArray prettyDigest(const QByteArray& digest)
+{
+ QByteArray hexDigest = digest.toHex().toUpper();
+ QByteArray prettyDigest;
+ prettyDigest.fill(':', hexDigest.count() + (hexDigest.count() / 2) - 1);
+
+ for (int i = 0; i * 2 < hexDigest.count(); i++) {
+ prettyDigest.replace(i * 3, 2, hexDigest.mid(i * 2, 2));
+ }
+ return prettyDigest;
+}
+
+QString formatCurrentDateTimeInString(const QString& formatStr)
+{
+ // Work on a copy of the string to avoid modifying the input string
+ QString formattedStr = QString(formatStr);
+
+ // Exit early if there's nothing to format
+ if (formattedStr.isEmpty())
+ return formattedStr;
+
+ // Find %%<text>%% in string. Replace inside text formatted to QDateTime with the current
+ // timestamp, using %%%% as an escape for multiple %% signs.
+ // For example:
+ // Simple: "All Quassel clients vanished from the face of the earth... %%hh:mm:ss%%"
+ // > Result: "All Quassel clients vanished from the face of the earth... 23:20:34"
+ // Complex: "Away since %%hh:mm%% on %%dd.MM%% - %%%% not here %%%%"
+ // > Result: "Away since 23:20 on 21.05 - %% not here %%"
+ //
+ // Match groups of double % signs - Some text %%inside here%%, and even %%%%:
+ // %%(.*)%%
+ // (...) marks a capturing group
+ // .* matches zero or more characters, not including newlines
+ // Note that '\' must be escaped as '\\'
+ // Helpful interactive website for debugging and explaining: https://regex101.com/
+ QRegExp regExpMatchTime("%%(.*)%%");