exefile.cpp
1 //------------------------------------------------------------------------------
2 // exefile.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2013 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "exefile.h"
24 
25 #include "configuration/doomseekerconfig.h"
26 #include "ini/inisection.h"
27 #include "ini/inivariable.h"
28 #include "message.h"
29 #include "plugins/engineplugin.h"
30 
31 #include <QFileInfo>
32 #include <QString>
33 
34 DClass<ExeFile>
35 {
36 public:
37  QString configKey;
38  QString exeTypeName;
39  QString programName;
40 };
41 
42 DPointered(ExeFile)
43 
45 {
46 }
47 
48 ExeFile::~ExeFile()
49 {
50 }
51 
52 const QString &ExeFile::configKey() const
53 {
54  return d->configKey;
55 }
56 
57 const QString &ExeFile::exeTypeName() const
58 {
59  return d->exeTypeName;
60 }
61 
62 Message ExeFile::install(QWidget *parent)
63 {
64  Q_UNUSED(parent);
65  return Message();
66 }
67 
68 QString ExeFile::pathToExe(Message &message)
69 {
70  IniSection config = gConfig.iniSectionForPlugin(programName());
71  IniVariable setting = config[configKey()];
72 
73  message = Message();
74  QString path = setting.value().toString();
75  QString error = "";
76  if (path.trimmed().isEmpty())
77  {
78  error = tr("No %1 executable specified for %2").arg(exeTypeName()).arg(programName());
79  message = Message::customError(error);
80  return QString();
81  }
82 
83  QFileInfo fi(path);
84 
85  if (!fi.exists() || (fi.isDir() && !fi.isBundle()))
86  {
87  // Remember: if the file doesn't exist
88  // canonicalFilePath() will return an empty string!
89  error = tr("Executable for %1 %2:\n%3\nis a directory or doesn't exist.")
90  .arg(programName()).arg(exeTypeName())
91  .arg(fi.absoluteFilePath());
92  message = Message::customError(error);
93  return QString();
94  }
95 
96  return fi.absoluteFilePath();
97 }
98 
99 const QString &ExeFile::programName() const
100 {
101  return d->programName;
102 }
103 
104 void ExeFile::setConfigKey(const QString &keyName)
105 {
106  d->configKey = keyName;
107 }
108 
109 void ExeFile::setExeTypeName(const QString &name)
110 {
111  d->exeTypeName = name;
112 }
113 
114 void ExeFile::setProgramName(const QString &name)
115 {
116  d->programName = name;
117 }
118 
120 {
121  QFileInfo fi(pathToExe(message));
122  return fi.absolutePath();
123 }
124 
126 
127 DClass<ExeFilePath>
128 {
129 public:
130  QString path;
131  QString workingDir;
132 };
133 
134 DPointered(ExeFilePath)
135 
137 {
138 }
139 
140 ExeFilePath::ExeFilePath(const QString &path)
141 {
142  d->path = path;
143  d->workingDir = QFileInfo(path).path();
144 }
145 
146 ExeFilePath::ExeFilePath(const ExeFilePath &other)
147 {
148  this->d = other.d;
149 }
150 
151 ExeFilePath::~ExeFilePath()
152 {
153 }
154 
155 ExeFilePath &ExeFilePath::operator=(const ExeFilePath &other)
156 {
157  if (this != &other)
158  this->d = other.d;
159  return *this;
160 }
161 
162 QString ExeFilePath::path() const
163 {
164  return d->path;
165 }
166 
167 ExeFilePath &ExeFilePath::setPath(const QString &path)
168 {
169  d->path = path;
170  return *this;
171 }
172 
173 QString ExeFilePath::workingDir() const
174 {
175  return d->workingDir;
176 }
177 
178 ExeFilePath &ExeFilePath::setWorkingDir(const QString &path)
179 {
180  d->workingDir = path;
181  return *this;
182 }