Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / src / filebrowse / fileinfo.cpp
1 #include "fileinfo.h"
2 #include <QDir>
3 #include <QDateTime>
4 #include <QProcess>
5 #include "globals.h"
6
7 FileInfo::FileInfo(QObject *parent) :
8 QObject(parent)
9 {
10 m_file = "";
11 }
12
13 FileInfo::~FileInfo()
14 {
15 }
16
17 void FileInfo::setFile(QString file)
18 {
19 if (m_file == file)
20 return;
21
22 m_file = file;
23 readFile();
24 }
25
26 bool FileInfo::isDir() const
27 {
28 return m_fileInfo.isDir();
29 }
30
31 QString FileInfo::kind() const
32 {
33 if (m_fileInfo.isSymLink()) return "l";
34 if (m_fileInfo.isDir()) return "d";
35 if (m_fileInfo.isFile()) return "-";
36 return "?";
37 }
38
39 QString FileInfo::icon() const
40 {
41 if (m_fileInfo.isSymLink() && m_fileInfo.isDir()) return "folder-link";
42 if (m_fileInfo.isDir()) return "folder";
43 if (m_fileInfo.isSymLink()) return "link";
44 if (m_fileInfo.isFile()) {
45 QString suffix = m_fileInfo.suffix().toLower();
46 return suffixToIconName(suffix);
47 }
48 return "file";
49 }
50
51 QString FileInfo::permissions() const
52 {
53 return permissionsToString(m_fileInfo.permissions());
54 }
55
56 QString FileInfo::owner() const
57 {
58 QString owner = m_fileInfo.owner();
59 if (owner.isEmpty())
60 owner = QString::number(m_fileInfo.ownerId());
61 return owner;
62 }
63
64 QString FileInfo::group() const
65 {
66 QString group = m_fileInfo.group();
67 if (group.isEmpty())
68 group = QString::number(m_fileInfo.groupId());
69 return group;
70 }
71
72 QString FileInfo::size() const
73 {
74 if (m_fileInfo.isDir()) return "-";
75 return filesizeToString(m_fileInfo.size());
76 }
77
78 QString FileInfo::modified() const
79 {
80 return datetimeToString(m_fileInfo.lastModified());
81 }
82
83 QString FileInfo::created() const
84 {
85 return datetimeToString(m_fileInfo.created());
86 }
87
88 QString FileInfo::absolutePath() const
89 {
90 return m_fileInfo.absolutePath();
91 }
92
93 QString FileInfo::name() const
94 {
95 return m_fileInfo.fileName();
96 }
97
98 QString FileInfo::suffix() const
99 {
100 return m_fileInfo.suffix().toLower();
101 }
102
103 QString FileInfo::symLinkTarget() const
104 {
105 return m_fileInfo.symLinkTarget();
106 }
107
108 QString FileInfo::errorMessage() const
109 {
110 return m_errorMessage;
111 }
112
113 QString FileInfo::processOutput() const
114 {
115 return m_processOutput;
116 }
117
118 void FileInfo::refresh()
119 {
120 readFile();
121 }
122
123 void FileInfo::executeCommand(QString command, QStringList arguments)
124 {
125 m_processOutput.clear();
126 emit processOutputChanged();
127
128 // process is killed when Page is closed - should run this in bg thread to allow command finish(?)
129 m_process = new QProcess(this);
130 m_process->setReadChannel(QProcess::StandardOutput);
131 m_process->setProcessChannelMode(QProcess::MergedChannels); // merged stderr channel with stdout channel
132 connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessChannels()));
133 connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleProcessFinish(int, QProcess::ExitStatus)));
134 connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleProcessError(QProcess::ProcessError)));
135 m_process->start(command, arguments);
136 }
137
138 void FileInfo::readProcessChannels()
139 {
140 while (m_process->canReadLine()) {
141 QString line = m_process->readLine();
142 m_processOutput += line;
143 }
144 emit processOutputChanged();
145 }
146
147 void FileInfo::handleProcessFinish(int exitCode, QProcess::ExitStatus status)
148 {
149 if (status == QProcess::CrashExit) // if it crashed, then use some error exit code
150 exitCode = -99999;
151 emit processExited(exitCode);
152 }
153
154 void FileInfo::handleProcessError(QProcess::ProcessError error)
155 {
156 Q_UNUSED(error);
157 emit processExited(-88888); // if error, then use some error exit code
158 }
159
160 void FileInfo::readFile()
161 {
162 m_errorMessage = "";
163
164 m_fileInfo = QFileInfo(m_file);
165 if (!m_fileInfo.exists())
166 m_errorMessage = tr("File does not exist");
167
168 emit fileChanged();
169 emit isDirChanged();
170 emit kindChanged();
171 emit iconChanged();
172 emit permissionsChanged();
173 emit ownerChanged();
174 emit groupChanged();
175 emit sizeChanged();
176 emit modifiedChanged();
177 emit createdChanged();
178 emit absolutePathChanged();
179 emit nameChanged();
180 emit suffixChanged();
181 emit symLinkTargetChanged();
182 emit errorMessageChanged();
183 }