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 #include "templatedpathresolver.h"
31 
32 #include <QFileInfo>
33 #include <QString>
34 
35 DClass<ExeFile>
36 {
37 public:
38  QString configKey;
39  QString exeTypeName;
40  QString programName;
41 };
42 
43 DPointered(ExeFile)
44 
46 {
47 }
48 
49 ExeFile::~ExeFile()
50 {
51 }
52 
53 const QString &ExeFile::configKey() const
54 {
55  return d->configKey;
56 }
57 
58 const QString &ExeFile::exeTypeName() const
59 {
60  return d->exeTypeName;
61 }
62 
63 Message ExeFile::install(QWidget *parent)
64 {
65  Q_UNUSED(parent);
66  return Message();
67 }
68 
69 QString ExeFile::pathToExe(Message &message)
70 {
71  IniSection config = gConfig.iniSectionForPlugin(programName());
72  IniVariable setting = config[configKey()];
73 
74  message = Message();
75  QString path = setting.value().toString();
76  QString error = "";
77  if (path.trimmed().isEmpty())
78  {
79  error = tr("The %1 executable is not set for %2.").arg(exeTypeName()).arg(programName());
80  message = Message::customError(error);
81  return QString();
82  }
83 
84  QFileInfo fi(gDoomseekerTemplatedPathResolver().resolve(path));
85  if (!fi.exists())
86  {
87  error = tr("The %1 executable for %2 doesn't exist:\n%3")
88  .arg(exeTypeName())
89  .arg(programName())
90  .arg(fi.absoluteFilePath());
91  message = Message::customError(error);
92  return QString();
93  }
94 
95  if (fi.isDir() && !fi.isBundle())
96  {
97  error = tr("The path to %1 executable for %2 is a directory:\n%3")
98  .arg(exeTypeName())
99  .arg(programName())
100  .arg(fi.absoluteFilePath());
101  message = Message::customError(error);
102  return QString();
103  }
104 
105  return fi.absoluteFilePath();
106 }
107 
108 const QString &ExeFile::programName() const
109 {
110  return d->programName;
111 }
112 
113 void ExeFile::setConfigKey(const QString &keyName)
114 {
115  d->configKey = keyName;
116 }
117 
118 void ExeFile::setExeTypeName(const QString &name)
119 {
120  d->exeTypeName = name;
121 }
122 
123 void ExeFile::setProgramName(const QString &name)
124 {
125  d->programName = name;
126 }
127 
129 {
130  QFileInfo fi(pathToExe(message));
131  return fi.path();
132 }
133 
135 
136 DClass<ExeFilePath>
137 {
138 public:
139  QString path;
140  QString workingDir;
141 };
142 
143 DPointered(ExeFilePath)
144 
146 {
147 }
148 
149 ExeFilePath::ExeFilePath(const QString &path)
150 {
151  d->path = path;
152  d->workingDir = QFileInfo(path).path();
153 }
154 
155 ExeFilePath::ExeFilePath(const ExeFilePath &other)
156 {
157  this->d = other.d;
158 }
159 
160 ExeFilePath::~ExeFilePath()
161 {
162 }
163 
164 ExeFilePath &ExeFilePath::operator=(const ExeFilePath &other)
165 {
166  if (this != &other)
167  this->d = other.d;
168  return *this;
169 }
170 
171 QString ExeFilePath::path() const
172 {
173  return d->path;
174 }
175 
176 ExeFilePath &ExeFilePath::setPath(const QString &path)
177 {
178  d->path = path;
179  return *this;
180 }
181 
182 QString ExeFilePath::workingDir() const
183 {
184  return d->workingDir;
185 }
186 
187 ExeFilePath &ExeFilePath::setWorkingDir(const QString &path)
188 {
189  d->workingDir = path;
190  return *this;
191 }