- DownloadView columns can be now hidden by user preference. Settings are persistent
[qtrapids] / src / include / qtrapids / format.hpp
1 #ifndef _QTRAPIDS_FORMAT_HPP_
2 #define _QTRAPIDS_FORMAT_HPP_
3
4 #include <qtrapids/info.hpp>
5 #include <QtCore/QString>
6
7 #include <QDebug>
8
9 namespace qtrapids
10 {
11
12
13 static inline QString formatProgress(uint progress)
14 {
15         return QString::number(progress / torrent_progress_percent);
16 }
17
18 namespace {
19
20 static const qulonglong size_KB = 1024;
21 static const qulonglong size_MB = size_KB << 10;
22 static const qulonglong size_GB = size_MB << 10;
23
24 static char const* size_names[] = {
25         "GB",
26         "MB",
27         "KB",
28         "B"
29 };
30
31 const qulonglong SECONDS_IN_DAY = 60*60*24;
32 }
33
34 static inline QString formatSize(qulonglong size)
35 {
36         qulonglong unit = size_GB;
37         char const ** unit_name = &size_names[0];
38         QString ret("");
39         for (unit = size_GB; unit > 0; unit >>= 10, ++unit_name) {
40                 if (size & (~(unit - 1))) {
41                         ret += (QString::number(size / unit) + *unit_name);
42                         return ret;
43                 }
44         }
45         ret = QString::number(size) + "B";
46         return ret;
47 }
48
49
50 inline QString formatElapsedTime(qulonglong seconds)
51 {
52         qulonglong hours = 0, minutes = 0, secsLeft = 0;
53         QString dayStr, hourStr, minStr, secStr;
54         dayStr = hourStr = minStr = secStr = QString::number(0);
55         
56         //result.reserve(8);
57         
58         hours = seconds / 3600;
59         secsLeft = seconds % 3600;
60         minutes = secsLeft / 60;
61         secsLeft = secsLeft % 60;
62
63         // If more than 24 hours, format time as days, hours.
64         // Otherwise hours : mins : secs
65         if (hours >= 24) {
66                 dayStr = QString::number(hours / 24);
67                 hourStr = QString::number(hours % 24);
68                 return dayStr + 'd' + ' ' + hourStr + 'h';
69         } else if (hours < 10) {
70                 hourStr.append(QString::number(hours));
71         } else {
72                 hourStr = QString::number(hours);
73         }
74         
75         if (minutes < 10) {
76                 minStr.append(QString::number(minutes));
77         } else {
78                 minStr = QString::number(minutes);
79         }
80         
81         if (secsLeft < 10) {
82                 secStr.append(QString::number(secsLeft));
83         } else {
84                 secStr = QString::number(secsLeft);
85         }
86                 
87         return hourStr + ':' + minStr + ':' + secStr;
88 }
89
90 } // namespace qtrapids
91
92 #endif // _QTRAPIDS_FORMAT_HPP_
93