1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2012-2016, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #ifndef _LUMINA_LIBRARY_UTILS_H
8 #define _LUMINA_LIBRARY_UTILS_H
9 
10 #include <QCoreApplication>
11 #include <QProcess>
12 #include <QTextStream>
13 #include <QTextCodec>
14 #include <QFile>
15 #include <QDir>
16 #include <QString>
17 #include <QStringList>
18 #include <QFile>
19 #include <QFileInfo>
20 #include <QObject>
21 #include <QTranslator>
22 #include <QDebug>
23 #include <QDesktopWidget>
24 #include <QImageReader>
25 #include <QRegExp>
26 #include <QFuture>
27 #include <QScreen>
28 #include <QSettings>
29 
30 class LUtils{
31 public:
32 	enum StandardDir {Desktop, Documents, Downloads, Music, Pictures, PublicShare, Templates, Videos};
33 
34 	//Return the path to one of the XDG standard directories
35 	static QString standardDirectory(StandardDir dir, bool createAsNeeded = true);
36 
37 	//Run an external command and return output & exit code
38 	static QString runCommand(bool &success, QString command, QStringList arguments = QStringList(), QString workdir = "", QStringList env = QStringList());
39 
40 	//Run an external command and return the exit code
41 	static int runCmd(QString cmd, QStringList args = QStringList());
42 	//Run an external command and return any text output (one line per entry)
43 	static QStringList getCmdOutput(QString cmd, QStringList args = QStringList());
44 
45 	//Read a text file
46 	static QStringList readFile(QString filepath);
47 	//Write a text file
48 	static bool writeFile(QString filepath, QStringList contents, bool overwrite=false);
49 
50 	//Check whether a file/path is a valid binary
51 	static bool isValidBinary(QString& bin); //full path or name only
isValidBinary(const char * bin)52 	static bool isValidBinary(const char *bin){
53 	  QString bins(bin);
54 	  return isValidBinary(bins); //overload for a "junk" binary variable input
55 	}
56 
57 	//Open the right settings file (user/permissions aware)
58 	static QSettings* openSettings(QString org, QString name, QObject *parent = 0);
59 
60 	//Return all the dirs on the system which contain .desktop files
61         static QStringList systemApplicationDirs();
62 
63 	//Create the exec string to open a terminal in a particular directory
64 	static QString GenerateOpenTerminalExec(QString term, QString dirpath);
65 
66 	//List all the sub-directories of a parent dir (recursive)
67 	static QStringList listSubDirectories(QString dir, bool recursive = true);
68 
69 	//Convert an input file/dir path to an absolute file path
70 	static QString PathToAbsolute(QString path); //This is primarily for CLI usage (relative paths)
71 	static QString AppToAbsolute(QString path); //This is for looking up a binary/ *.desktop path
72 
73 	//Get the list of all file extensions which Qt can read (lowercase)
74 	static QStringList imageExtensions(bool wildcards = false);
75 	static QStringList videoExtensions();
76 
77 	//Load a translation file for a Lumina Project
78 	static QTranslator* LoadTranslation(QApplication *app, QString appname, QString locale = "", QTranslator *cTrans = 0);
79 	//Other localization shortcuts
80 	static QStringList knownLocales(); //Note: This only lists locales known to Lumina (so the i18n files need to be installed)
81 	static void setLocaleEnv(QString lang, QString msg="", QString time="", QString num="" ,QString money="",QString collate="", QString ctype="");
82 	static QString currentLocale();
83 
84 	//Number format conversions
85 	static double DisplaySizeToBytes(QString num); //Turn a display size (like 50M or 50KB) into a double for calculations (bytes)
86 	static QString BytesToDisplaySize(qint64 bytes); //convert into a readable size (like 50M or 50KB)
87 
88 	static QString SecondsToDisplay(int secs); //convert into a readable time
89 };
90 #endif
91