MythTV  master
configuration.h
Go to the documentation of this file.
1 // Program Name: configuration.h
3 // Created : Feb. 12, 2007
4 //
5 // Purpose : XML Configuration file Class
6 //
7 // Copyright (c) 2007 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #ifndef CONFIGURATION_H
14 #define CONFIGURATION_H
15 
16 #include <utility>
17 
18 #include <QDomDocument>
19 #include <QDomNode>
20 #include <QString>
21 #include <QStringList>
22 #include <QVariant>
23 
25 #include "libmythbase/mythchrono.h"
26 #include "libmythbase/mythdirs.h"
27 
29 //
30 // **NOTE: Performance Issue ***
31 //
32 // This class loads an XML file into a QDomDocument. All requests for
33 // getting or setting values navigates the DOM each time. All settings should
34 // only be read/written once if possible.
35 //
37 
39 {
40  protected:
41 
42  QString m_path {GetConfDir()};
43  QString m_fileName {kDefaultFilename};
44 
45  QDomDocument m_config;
46  QDomNode m_rootNode;
47 
48  QDomNode FindNode(const QString &setting, bool create);
49  QDomNode FindNode(QStringList &path, QDomNode &current, bool create);
50 
51  QString GetValue(const QString &setting);
52 
53  public:
54 
55  static constexpr auto kDefaultFilename = "config.xml";
56 
57  XmlConfiguration() { Load(); }
58  explicit XmlConfiguration(QString fileName)
59  : m_fileName(std::move(fileName))
60  {
61  Load();
62  }
63 
64  bool Load();
65  bool Save();
66 
67  bool FileExists();
68 
69  bool GetValue(const QString &setting, const bool defaultValue)
70  {
71  QString value = GetValue(setting);
72  return (!value.isNull()) ? QVariant(value).toBool() : defaultValue;
73  }
74  int GetValue(const QString &setting, const int defaultValue)
75  {
76  QString value = GetValue(setting);
77  return (!value.isNull()) ? QVariant(value).toInt() : defaultValue;
78  }
79  QString GetValue(const QString &setting, const QString &defaultValue)
80  {
81  QString value = GetValue(setting);
82  return (!value.isNull()) ? value : defaultValue;
83  }
87  QString GetValue(const QString &setting, const char* defaultValue)
88  {
89  QString value = GetValue(setting);
90  return (!value.isNull()) ? value : QString(defaultValue);
91  }
92 
93  void SetValue(const QString &setting, bool value)
94  {
95  SetValue(setting, QVariant(value).toString());
96  }
97  void SetValue(const QString &setting, int value)
98  {
99  SetValue(setting, QVariant(value).toString());
100  }
105  void SetValue(const QString &setting, const char* value)
106  {
107  SetValue(setting, QString(value));
108  }
109  void SetValue(const QString &setting, const QString &value);
110  void ClearValue(const QString &setting);
111 
112  template <typename T>
113  typename std::enable_if_t<std::chrono::__is_duration<T>::value, T>
114  GetDuration(const QString &setting, T defaultValue = T::zero())
115  {
116  return T(GetValue(setting, static_cast<int>(defaultValue.count())));
117  }
118 
119  template <typename T>
120  typename std::enable_if_t<std::chrono::__is_duration<T>::value>
121  SetDuration(const QString &setting, T value)
122  {
123  SetValue(setting, static_cast<int>(value.count()));
124  }
125 };
126 
127 #endif // CONFIGURATION_H
MythDate::toString
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:84
XmlConfiguration::GetValue
QString GetValue(const QString &setting, const char *defaultValue)
get the string value stored as setting with a default C string.
Definition: configuration.h:87
mythbaseexp.h
XmlConfiguration::GetValue
int GetValue(const QString &setting, const int defaultValue)
Definition: configuration.h:74
XmlConfiguration::XmlConfiguration
XmlConfiguration(QString fileName)
Definition: configuration.h:58
XmlConfiguration::GetDuration
std::enable_if_t< std::chrono::__is_duration< T >::value, T > GetDuration(const QString &setting, T defaultValue=T::zero())
Definition: configuration.h:114
XmlConfiguration::m_rootNode
QDomNode m_rootNode
Definition: configuration.h:46
XmlConfiguration::GetValue
bool GetValue(const QString &setting, const bool defaultValue)
Definition: configuration.h:69
mythdirs.h
XmlConfiguration::SetValue
void SetValue(const QString &setting, int value)
Definition: configuration.h:97
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
XmlConfiguration
Definition: configuration.h:38
XmlConfiguration::SetValue
void SetValue(const QString &setting, bool value)
Definition: configuration.h:93
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
XmlConfiguration::m_config
QDomDocument m_config
Definition: configuration.h:45
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:256
XmlConfiguration::XmlConfiguration
XmlConfiguration()
Definition: configuration.h:57
XmlConfiguration::SetDuration
std::enable_if_t< std::chrono::__is_duration< T >::value > SetDuration(const QString &setting, T value)
Definition: configuration.h:121
XmlConfiguration::GetValue
QString GetValue(const QString &setting, const QString &defaultValue)
Definition: configuration.h:79
XmlConfiguration::SetValue
void SetValue(const QString &setting, const char *value)
set value to a C string; just in case, for overload resolution
Definition: configuration.h:105
mythchrono.h