Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / src / filebrowse / globals.cpp
1 #include "globals.h"
2 #include <QLocale>
3 #include <QProcess>
4
5 QString suffixToIconName(QString suffix)
6 {
7 // only formats that are understood by File Browser or Sailfish get a special icon
8 if (suffix == "txt")
9 return "file-txt";
10 if (suffix == "rpm")
11 return "file-rpm";
12 if (suffix == "apk")
13 return "file-apk";
14 if (suffix == "png" || suffix == "jpeg" || suffix == "jpg" ||
15 suffix == "gif")
16 return "file-image";
17 if (suffix == "wav" || suffix == "mp3" || suffix == "flac" ||
18 suffix == "aac" || suffix == "ogg" || suffix == "m4a")
19 return "file-audio";
20 if (suffix == "mp4" || suffix == "m4v")
21 return "file-video";
22
23 return "file";
24 }
25
26 QString permissionsToString(QFile::Permissions permissions)
27 {
28 char str[] = "---------";
29 if (permissions & 0x4000) str[0] = 'r';
30 if (permissions & 0x2000) str[1] = 'w';
31 if (permissions & 0x1000) str[2] = 'x';
32 if (permissions & 0x0040) str[3] = 'r';
33 if (permissions & 0x0020) str[4] = 'w';
34 if (permissions & 0x0010) str[5] = 'x';
35 if (permissions & 0x0004) str[6] = 'r';
36 if (permissions & 0x0002) str[7] = 'w';
37 if (permissions & 0x0001) str[8] = 'x';
38 return QString::fromLatin1(str);
39 }
40
41 QString filesizeToString(qint64 filesize)
42 {
43 // convert to kB, MB, GB: use 1000 instead of 1024 as divisor because it seems to be
44 // the usual way to display file size (like on Ubuntu)
45 QLocale locale;
46 if (filesize < 1000LL)
47 return locale.toString(filesize)+" bytes";
48
49 if (filesize < 1000000LL)
50 return locale.toString((double)filesize/1000.0, 'f', 2)+" kB";
51
52 if (filesize < 1000000000LL)
53 return locale.toString((double)filesize/1000000.0, 'f', 2)+" MB";
54
55 return locale.toString((double)filesize/1000000000.0, 'f', 2)+" GB";
56 }
57
58 QString datetimeToString(QDateTime datetime)
59 {
60 QLocale locale;
61
62 // return time for today or date for older
63 if (datetime.date() == QDate::currentDate())
64 return locale.toString(datetime.time(), QLocale::NarrowFormat);
65
66 return locale.toString(datetime.date(), QLocale::NarrowFormat);
67 }
68
69 QString infoToFileKind(QFileInfo info)
70 {
71 if (info.isDir()) return "d";
72 if (info.isSymLink()) return "l";
73 if (info.isFile()) return "-";
74 return "?";
75 }
76
77 QString infoToIconName(QFileInfo info)
78 {
79 if (info.isDir()) return "folder";
80 if (info.isSymLink()) return "link";
81 if (info.isFile()) {
82 QString suffix = info.suffix().toLower();
83 return suffixToIconName(suffix);
84 }
85 return "file";
86 }
87
88 int access(QString filename, int how)
89 {
90 QByteArray fab = filename.toUtf8();
91 char *fn = fab.data();
92 return access(fn, how);
93 }
94
95 QString execute(QString command, QStringList arguments, bool mergeErrorStream)
96 {
97 QProcess process;
98 process.setReadChannel(QProcess::StandardOutput);
99 if (mergeErrorStream)
100 process.setProcessChannelMode(QProcess::MergedChannels);
101 process.start(command, arguments);
102 if (!process.waitForStarted())
103 return QString();
104 if (!process.waitForFinished())
105 return QString();
106
107 QByteArray result = process.readAll();
108 return QString::fromUtf8(result);
109 }