]> git.sur5r.net Git - minitube/blob - src/datautils.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / datautils.cpp
1 #include "datautils.h"
2
3 QString DataUtils::stringToFilename(const QString &s) {
4     QString f = s;
5     f.replace('(', '[');
6     f.replace(')', ']');
7     f.replace('/', ' ');
8     f.replace('\\', ' ');
9     f.replace('<', ' ');
10     f.replace('>', ' ');
11     f.replace(':', ' ');
12     f.replace('"', ' ');
13     f.replace('|', ' ');
14     f.replace('?', ' ');
15     f.replace('*', ' ');
16     f = f.simplified();
17
18     if (!f.isEmpty() && f.at(0) == '.') f = f.midRef(1).trimmed().toString();
19
20     return f;
21 }
22
23 QString DataUtils::regioneCode(const QLocale &locale) {
24     QString name = locale.name();
25     int index = name.indexOf('_');
26     if (index == -1) return QString();
27     return name.right(index);
28 }
29
30 QString DataUtils::systemRegioneCode() {
31     return regioneCode(QLocale::system());
32 }
33
34 uint DataUtils::parseIsoPeriod(const QString &isoPeriod) {
35     uint days = 0, hours = 0, minutes = 0, seconds = 0;
36
37     const int len = isoPeriod.length();
38     int digitStart = -1;
39     for (int i = 0; i < len; ++i) {
40         const QChar c = isoPeriod.at(i);
41         if (c.isDigit()) {
42             if (digitStart == -1) digitStart = i;
43         } else if (digitStart != -1) {
44             if (c == 'H') {
45                 hours = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt();
46             } else if (c == 'M') {
47                 minutes = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt();
48             } else if (c == 'S') {
49                 seconds = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt();
50             }
51             digitStart = -1;
52         }
53     }
54
55     uint period = ((days * 24 + hours) * 60 + minutes) * 60 + seconds;
56     return period;
57 }
58
59 QString DataUtils::formatDateTime(const QDateTime &dt) {
60     const qint64 seconds = dt.secsTo(QDateTime::currentDateTime());
61     QString s;
62     int f = 60;
63     if (seconds < f) {
64         s = QCoreApplication::translate("DataUtils", "Just now");
65     } else if (seconds < (f *= 60)) {
66         s = QCoreApplication::translate("DataUtils", "%n minute(s) ago", Q_NULLPTR, seconds / 60);
67     } else if (seconds < (f *= 24)) {
68         int n = seconds / (60 * 60);
69         s = QCoreApplication::translate("DataUtils", "%n hour(s) ago", Q_NULLPTR, n);
70     } else if (seconds < (f *= 7)) {
71         int n = seconds / (60 * 60 * 24);
72         s = QCoreApplication::translate("DataUtils", "%n day(s) ago", Q_NULLPTR, n);
73     } else if (seconds < (f = 60 * 60 * 24 * 30)) {
74         int n = seconds / (60 * 60 * 24 * 7);
75         s = QCoreApplication::translate("DataUtils", "%n week(s) ago", Q_NULLPTR, n);
76     } else if (seconds < (f = 60 * 60 * 24 * 365)) {
77         int n = seconds / (60 * 60 * 24 * 30);
78         s = QCoreApplication::translate("DataUtils", "%n month(s) ago", Q_NULLPTR, n);
79     } else {
80         s = dt.date().toString(Qt::DefaultLocaleShortDate);
81     }
82     return s;
83 }
84
85 QString DataUtils::formatDuration(uint secs) {
86     uint d = secs;
87     QString res;
88     uint seconds = d % 60;
89     d /= 60;
90     uint minutes = d % 60;
91     d /= 60;
92     uint hours = d % 24;
93     if (hours == 0) return res.sprintf("%d:%02d", minutes, seconds);
94     return res.sprintf("%d:%02d:%02d", hours, minutes, seconds);
95 }
96
97 QString DataUtils::formatCount(int c) {
98     QString s;
99     int f = 1;
100     if (c < 1) {
101         return s;
102     } else if (c < (f *= 1000)) {
103         s = QString::number(c);
104     } else if (c < (f *= 1000)) {
105         int n = c / 1000;
106         s = QString::number(n) +
107             QCoreApplication::translate("DataUtils", "K", "K as in Kilo, i.e. thousands");
108     } else if (c < (f *= 1000)) {
109         int n = c / (1000 * 1000);
110         s = QString::number(n) +
111             QCoreApplication::translate("DataUtils", "M", "M stands for Millions");
112     } else {
113         int n = c / (1000 * 1000 * 1000);
114         s = QString::number(n) +
115             QCoreApplication::translate("DataUtils", "B", "B stands for Billions");
116     }
117
118     return QCoreApplication::translate("DataUtils", "%1 views").arg(s);
119 }