QElectroTech  0.70
qetarguments.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "qetarguments.h"
20 
25 QETArguments::QETArguments(QObject *parent) :
26  QObject(parent),
27  print_help_(false),
28  print_license_(false),
29  print_version_(false)
30 {
31 }
32 
38 QETArguments::QETArguments(const QList<QString> &args, QObject *parent) :
39  QObject(parent),
40  print_help_(false),
41  print_license_(false),
42  print_version_(false)
43 {
44  parseArguments(args);
45 }
46 
52  QObject(qet_arguments.parent()),
53  project_files_(qet_arguments.project_files_),
54  element_files_(qet_arguments.element_files_),
55  tbt_files_(qet_arguments.tbt_files_),
56  options_(qet_arguments.options_),
57  unknown_options_(qet_arguments.unknown_options_),
58 #ifdef QET_ALLOW_OVERRIDE_CED_OPTION
59  common_elements_dir_(qet_arguments.common_elements_dir_),
60 #endif
61 #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
62 common_tbt_dir_(qet_arguments.common_tbt_dir_),
63 #endif
64 #ifdef QET_ALLOW_OVERRIDE_CD_OPTION
65  config_dir_(qet_arguments.config_dir_),
66 #endif
67  lang_dir_(qet_arguments.lang_dir_),
68  print_help_(qet_arguments.print_help_),
69  print_license_(qet_arguments.print_license_),
70  print_version_(qet_arguments.print_version_)
71 {
72 }
73 
79  project_files_ = qet_arguments.project_files_;
80  element_files_ = qet_arguments.element_files_;
81  tbt_files_ = qet_arguments.tbt_files_;
82  options_ = qet_arguments.options_;
83  unknown_options_ = qet_arguments.unknown_options_;
84 #ifdef QET_ALLOW_OVERRIDE_CED_OPTION
85  common_elements_dir_ = qet_arguments.common_elements_dir_;
86 #endif
87 #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
88  common_tbt_dir_ = qet_arguments.common_tbt_dir_;
89 #endif
90 #ifdef QET_ALLOW_OVERRIDE_CD_OPTION
91  config_dir_ = qet_arguments.config_dir_;
92 #endif
93  lang_dir_ = qet_arguments.lang_dir_;
94  print_help_ = qet_arguments.print_help_;
95  print_license_ = qet_arguments.print_license_;
96  print_version_ = qet_arguments.print_version_;
97  return(*this);
98 }
99 
104 }
105 
111 void QETArguments::setArguments(const QList<QString> &args) {
112  parseArguments(args);
113 }
114 
120 QList<QString> QETArguments::arguments() const {
122 }
123 
128 QList<QString> QETArguments::files() const {
130 }
131 
135 QList<QString> QETArguments::projectFiles() const {
136  return(project_files_);
137 }
138 
142 QList<QString> QETArguments::elementFiles() const {
143  return(element_files_);
144 }
145 
150  return(tbt_files_);
151 }
152 
156 QList<QString> QETArguments::options() const {
157  return(options_);
158 }
159 
163 QList<QString> QETArguments::unknownOptions() const {
164  return(unknown_options_);
165 }
166 
171  project_files_.clear();
172  element_files_.clear();
173  options_.clear();
174  unknown_options_.clear();
175 #ifdef QET_ALLOW_OVERRIDE_CED_OPTION
176  common_elements_dir_.clear();
177 #endif
178 #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
179  common_tbt_dir_.clear();
180 #endif
181 #ifdef QET_ALLOW_OVERRIDE_CD_OPTION
182  config_dir_.clear();
183 #endif
184 }
185 
191 void QETArguments::parseArguments(const QList<QString> &arguments) {
192  // oublie les eventuels arguments precedents
193  clear();
194 
195  // separe les fichiers des options
196  foreach(QString argument, arguments) {
197  QFileInfo argument_info(argument);
198  if (argument_info.exists()) {
199  // on exprime les chemins des fichiers en absolu
200  QString can_argument = argument_info.canonicalFilePath();
201  handleFileArgument(can_argument);
202  } else {
203  handleOptionArgument(argument);
204  }
205  }
206 }
207 
211 void QETArguments::handleFileArgument(const QString &file) {
212  if (file.endsWith(".elmt")) {
213  if (!element_files_.contains(file)) {
214  element_files_ << file;
215  }
216  } else if (file.endsWith(TITLEBLOCKS_FILE_EXTENSION)) {
217  if (!tbt_files_.contains(file)) {
218  tbt_files_ << file;
219  }
220  } else {
221  if (!project_files_.contains(file)) {
222  project_files_ << file;
223  }
224  }
225 }
226 
239 void QETArguments::handleOptionArgument(const QString &option) {
240  if (option == QString("--help")) {
241  print_help_ = true;
242  options_ << option;
243  return;
244  } else if (option == QString("--version") || option == QString("-v")) {
245  print_version_ = true;
246  options_ << option;
247  return;
248  } else if (option == QString("--license")) {
249  print_license_ = true;
250  options_ << option;
251  return;
252  }
253 
254 #ifdef QET_ALLOW_OVERRIDE_CED_OPTION
255  QString ced_arg("--common-elements-dir=");
256  if (option.startsWith(ced_arg)) {
257  common_elements_dir_ = option.mid(ced_arg.length());
258  return;
259  }
260 
261 #endif
262 #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
263  QString ctbtd_arg("--common-tbt-dir=");
264  if (option.startsWith(ctbtd_arg)) {
265  common_tbt_dir_ = option.mid(ctbtd_arg.length());
266  return;
267  }
268 #endif
269 #ifdef QET_ALLOW_OVERRIDE_CD_OPTION
270  QString cd_arg("--config-dir=");
271  if (option.startsWith(cd_arg)) {
272  config_dir_ = option.mid(cd_arg.length());
273  return;
274  }
275 
276 #endif
277 
278  QString ld_arg("--lang-dir=");
279  if (option.startsWith(ld_arg)) {
280  lang_dir_ = option.mid(ld_arg.length());
281  return;
282  }
283 
284  // a ce stade, l'option est inconnue
285  unknown_options_ << option;
286 }
287 
288 #ifdef QET_ALLOW_OVERRIDE_CED_OPTION
289 
293 bool QETArguments::commonElementsDirSpecified() const {
294  return(!common_elements_dir_.isEmpty());
295 }
296 
301 QString QETArguments::commonElementsDir() const {
302  return(common_elements_dir_);
303 }
304 #endif
305 
306 #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
307 
311 bool QETArguments::commonTitleBlockTemplatesDirSpecified() const {
312  return(!common_tbt_dir_.isEmpty());
313 }
314 
319 QString QETArguments::commonTitleBlockTemplatesDir() const {
320  return(common_tbt_dir_);
321 }
322 #endif
323 
324 #ifdef QET_ALLOW_OVERRIDE_CD_OPTION
325 
328 bool QETArguments::configDirSpecified() const {
329  return(!config_dir_.isEmpty());
330 }
331 
336 QString QETArguments::configDir() const {
337  return(config_dir_);
338 }
339 #endif
340 
345  return(!lang_dir_.isEmpty());
346 }
347 
352 QString QETArguments::langDir() const {
353  return(lang_dir_);
354 }
355 
361  return(print_help_);
362 }
363 
369  return(print_license_);
370 }
371 
377  return(print_version_);
378 }
QList< QString > project_files_
Definition: qetarguments.h:73
QString lang_dir_
Definition: qetarguments.h:87
QList< QString > options_
Definition: qetarguments.h:76
virtual QList< QString > projectFiles() const
void parseArguments(const QList< QString > &)
virtual QList< QString > arguments() const
void handleOptionArgument(const QString &)
virtual QList< QString > elementFiles() const
QList< QString > tbt_files_
Definition: qetarguments.h:75
QList< QString > element_files_
Definition: qetarguments.h:74
void handleFileArgument(const QString &)
~QETArguments() override
virtual bool printVersionRequested() const
virtual bool printLicenseRequested() const
virtual bool langDirSpecified() const
QETArguments(QObject *=nullptr)
virtual QList< QString > options() const
virtual QString langDir() const
QETArguments & operator=(const QETArguments &)
#define TITLEBLOCKS_FILE_EXTENSION
bool print_license_
Definition: qetarguments.h:89
virtual void setArguments(const QList< QString > &)
virtual QList< QString > titleBlockTemplateFiles() const
QList< QString > unknown_options_
Definition: qetarguments.h:77
bool print_version_
Definition: qetarguments.h:90
virtual bool printHelpRequested() const
bool print_help_
Definition: qetarguments.h:88
virtual QList< QString > unknownOptions() const
virtual QList< QString > files() const