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 <QString>
32 #include <QFileInfo>
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  return Message();
65 }
66 
67 QString ExeFile::pathToExe(Message& message)
68 {
69  IniSection config = gConfig.iniSectionForPlugin(programName());
70  IniVariable setting = config[configKey()];
71 
72  message = Message();
73  QString path = setting.value().toString();
74  QString error = "";
75  if (path.trimmed().isEmpty())
76  {
77  error = tr("No %1 executable specified for %2").arg(exeTypeName()).arg(programName());
78  message = Message::customError(error);
79  return QString();
80  }
81 
82  QFileInfo fi(path);
83 
84  if (!fi.exists() || (fi.isDir() && !fi.isBundle()))
85  {
86  // Remember: if the file doesn't exist
87  // canonicalFilePath() will return an empty string!
88  error = tr("Executable for %1 %2:\n%3\nis a directory or doesn't exist.")
89  .arg(programName()).arg(exeTypeName())
90  .arg(fi.absoluteFilePath());
91  message = Message::customError(error);
92  return QString();
93  }
94 
95  return fi.absoluteFilePath();
96 }
97 
98 const QString& ExeFile::programName() const
99 {
100  return d->programName;
101 }
102 
103 void ExeFile::setConfigKey(const QString& keyName)
104 {
105  d->configKey = keyName;
106 }
107 
108 void ExeFile::setExeTypeName(const QString& name)
109 {
110  d->exeTypeName = name;
111 }
112 
113 void ExeFile::setProgramName(const QString& name)
114 {
115  d->programName = name;
116 }
117 
119 {
120  QFileInfo fi(pathToExe(message));
121  return fi.absolutePath();
122 }
123 
125 
126 DClass<ExeFilePath>
127 {
128 public:
129  QString path;
130  QString workingDir;
131 };
132 
133 DPointered(ExeFilePath);
134 
135 ExeFilePath::ExeFilePath()
136 {
137 }
138 
139 ExeFilePath::ExeFilePath(const QString &path)
140 {
141  d->path = path;
142  d->workingDir = QFileInfo(path).path();
143 }
144 
145 ExeFilePath::~ExeFilePath()
146 {
147 }
148 
149 QString ExeFilePath::path() const
150 {
151  return d->path;
152 }
153 
154 ExeFilePath &ExeFilePath::setPath(const QString &path)
155 {
156  d->path = path;
157  return *this;
158 }
159 
160 QString ExeFilePath::workingDir() const
161 {
162  return d->workingDir;
163 }
164 
165 ExeFilePath &ExeFilePath::setWorkingDir(const QString &path)
166 {
167  d->workingDir = path;
168  return *this;
169 }
void setProgramName(const QString &name)
Plugin setter for programName().
Definition: exefile.cpp:113
const QString & programName() const
Name of the program this executable belongs to (ex. "Odamex").
Definition: exefile.cpp:98
INI variable representation.
Definition: inivariable.h:41
void setExeTypeName(const QString &name)
Plugin setter for exeTypeName().
Definition: exefile.cpp:108
const QString & exeTypeName() const
Name of the type of the executable (server, client, etc.).
Definition: exefile.cpp:57
Message object used to pass messages throughout the Doomseeker&#39;s system.
Definition: message.h:63
virtual Message install(QWidget *parent)
Attempts to install the binary.
Definition: exefile.cpp:62
void setConfigKey(const QString &keyName)
Plugin setter for configKey().
Definition: exefile.cpp:103
QVariant value() const
Extracts the value as QVariant.
const QString & configKey() const
Config key where executable path on current system can be remembered.
Definition: exefile.cpp:52
virtual QString pathToExe(Message &message)
Returns the path to the executable file.
Definition: exefile.cpp:67
A simple executable path & working dir structure.
Definition: exefile.h:147
INI section representation.
Definition: inisection.h:40
virtual QString workingDirectory(Message &message)
Path to this executable working directory.
Definition: exefile.cpp:118
static Message customError(const QString &content)
Convenience method. Sets type to Type::CUSTOM_ERROR.
Definition: message.h:123
Access to external program executables (game clients, servers, and so on).
Definition: exefile.h:48