Torc  0.1
torcsetting.cpp
Go to the documentation of this file.
1 /* Class TorcSetting
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2013-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclocalcontext.h"
25 #include "torccoreutils.h"
26 #include "torcsetting.h"
27 
50 TorcSetting::TorcSetting(TorcSetting *Parent, const QString &DBName, const QString &UIName,
51  Type SettingType, Roles SettingRoles, const QVariant &Default)
52  : QObject(),
53  TorcHTTPService(this, TORC_SETTINGS_DIR + DBName, (SettingRoles & Public) ? DBName : QStringLiteral(""),
54  TorcSetting::staticMetaObject, QStringLiteral("SetActive,SetTrue,SetFalse")),
55  m_parent(Parent),
56  type(SettingType),
57  settingType(TorcCoreUtils::EnumToLowerString<TorcSetting::Type>(SettingType)),
58  roles(SettingRoles),
59  m_dbName(DBName),
60  uiName(UIName),
61  helpText(),
62  value(),
63  defaultValue(Default),
64  selections(),
65  m_begin(0),
66  m_end(1),
67  m_step(1),
68  isActive(false),
69  m_active(0),
70  m_activeThreshold(1),
71  m_children()
72 {
73  setObjectName(DBName);
74 
75  qRegisterMetaType<QString>("QString&");
76 
77  if (m_parent)
78  m_parent->AddChild(this);
79 
80  QVariant::Type vtype = defaultValue.type();
81 
82  if (vtype == QVariant::Int && type == Integer)
83  {
84  value = (roles & Persistent) ? gLocalContext->GetSetting(m_dbName, (int)defaultValue.toInt()) : defaultValue.toInt();
85  }
86  else if (vtype == QVariant::Bool && type == Bool)
87  {
88  value = (roles & Persistent) ? gLocalContext->GetSetting(m_dbName, (bool)defaultValue.toBool()) : defaultValue.toBool();
89  }
90  else if (vtype == QVariant::String)
91  {
92  value = (roles & Persistent) ? gLocalContext->GetSetting(m_dbName, (QString)defaultValue.toString()) : defaultValue.toString();
93  }
94  else if (vtype == QVariant::StringList)
95  {
96  if (roles & Persistent)
97  {
98  QString svalue = gLocalContext->GetSetting(m_dbName, (QString)defaultValue.toString());
99  value = QVariant(svalue.split(','));
100  }
101  else
102  {
103  value = defaultValue.toStringList();
104  }
105  }
106  else
107  {
108  if (vtype != QVariant::Invalid)
109  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Unsupported setting data type for %1 (%2)").arg(m_dbName).arg(vtype));
110  value = QVariant();
111  }
112 }
113 
114 void TorcSetting::SubscriberDeleted(QObject *Subscriber)
115 {
117 }
118 
119 QVariantMap TorcSetting::GetChildList(void)
120 {
121  QReadLocker locker(&m_httpServiceLock);
122 
123  QVariantMap result;
124  (void)GetChildList(result);
125  return result;
126 }
127 
128 QString TorcSetting::GetChildList(QVariantMap &Children)
129 {
130  QReadLocker locker(&m_httpServiceLock);
131 
132  Children.insert(QStringLiteral("name"), m_dbName);
133  Children.insert(QStringLiteral("uiname"), uiName);
134  Children.insert(QStringLiteral("type"), TorcCoreUtils::EnumToLowerString<TorcSetting::Type>(type));
135  QVariantMap children;
136  foreach (TorcSetting* child, m_children)
137  {
138  QVariantMap childd;
139  QString name = child->GetChildList(childd);
140  children.insert(name, childd);
141  }
142  Children.insert(QStringLiteral("children"), children);
143  return m_dbName;
144 }
145 
146 QVariantMap TorcSetting::GetSelections(void)
147 {
148  QReadLocker locker(&m_httpServiceLock);
149  return selections;
150 }
151 
152 void TorcSetting::SetSelections(QVariantMap &Selections)
153 {
154  QWriteLocker locker(&m_httpServiceLock);
155  selections = Selections;
156 }
157 
159 {
160  QWriteLocker locker(&m_httpServiceLock);
161  if (Child)
162  {
163  m_children.append(Child);
164  Child->UpRef();
165  }
166 }
168 {
169  QWriteLocker locker(&m_httpServiceLock);
170  if (Child)
171  {
172  {
173  for (int i = 0; i < m_children.size(); ++i)
174  {
175  if (m_children.at(i) == Child)
176  {
177  m_children.removeAt(i);
178  break;
179  }
180  }
181  }
182 
183  Child->DownRef();
184  }
185 }
186 
188 {
189  QWriteLocker locker(&m_httpServiceLock);
190  if (m_parent)
191  m_parent->RemoveChild(this);
192 
193  emit Removed();
194 }
195 
196 TorcSetting* TorcSetting::FindChild(const QString &Child, bool Recursive /*=false*/)
197 {
198  QReadLocker locker(&m_httpServiceLock);
199 
200  foreach (TorcSetting* setting, m_children)
201  if (setting->objectName() == Child)
202  return setting;
203 
204  if (Recursive)
205  {
206  foreach (TorcSetting* setting, m_children)
207  {
208  TorcSetting *result = setting->FindChild(Child, true);
209  if (result)
210  return result;
211  }
212  }
213 
214  return nullptr;
215 }
216 
217 QSet<TorcSetting*> TorcSetting::GetChildren(void)
218 {
219  QSet<TorcSetting*> result;
220 
221  m_httpServiceLock.lockForWrite();
222  foreach (TorcSetting* setting, m_children)
223  {
224  result << setting;
225  setting->UpRef();
226  }
227  m_httpServiceLock.unlock();
228 
229  return result;
230 }
231 
233 {
234  QReadLocker locker(&m_httpServiceLock);
235  return isActive;
236 }
237 
239 {
240  QReadLocker locker(&m_httpServiceLock);
241  return uiName;
242 }
243 
245 {
246  QReadLocker locker(&m_httpServiceLock);
247  return helpText;
248 }
249 
251 {
252  QReadLocker locker(&m_httpServiceLock);
253  return defaultValue;
254 }
255 
257 {
258  QReadLocker locker(&m_httpServiceLock);
259  return settingType;
260 }
261 
263 {
264  QReadLocker locker(&m_httpServiceLock);
265  return m_begin;
266 }
267 
269 {
270  QReadLocker locker(&m_httpServiceLock);
271  return m_end;
272 }
273 
275 {
276  QReadLocker locker(&m_httpServiceLock);
277  return m_step;
278 }
279 
280 void TorcSetting::SetActive(bool Value)
281 {
282  m_httpServiceLock.lockForWrite();
283  bool wasactive = isActive;
284  m_active += Value ? 1 : -1;
285  isActive = m_active >= m_activeThreshold;
286  bool changed = wasactive != isActive;
287  bool newactive = isActive;
288  m_httpServiceLock.unlock();
289 
290  if (changed)
291  emit ActiveChanged(newactive);
292 }
293 
295 {
296  m_httpServiceLock.lockForWrite();
297  bool wasactive = isActive;
298  m_activeThreshold = Threshold;
299  isActive = m_active >= m_activeThreshold;
300  bool changed = wasactive != isActive;
301  bool newactive = isActive;
302  m_httpServiceLock.unlock();
303 
304  if (changed)
305  emit ActiveChanged(newactive);
306 }
307 
308 bool TorcSetting::SetValue(const QVariant &Value)
309 {
310  QWriteLocker locker(&m_httpServiceLock);
311  if (value == Value)
312  return true;
313 
314  QVariant::Type vtype = defaultValue.type();
315  if (vtype == QVariant::Int)
316  {
317  int ivalue = Value.toInt();
318  bool valid = false;
319  if (!selections.isEmpty())
320  {
321  valid = selections.contains(QString::number(ivalue));
322  }
323  else if (ivalue >= m_begin && ivalue <= m_end)
324  {
325  valid = true;
326  }
327 
328  if (valid)
329  {
330  if (roles & Persistent)
331  gLocalContext->SetSetting(m_dbName, (int)ivalue);
332  value = Value;
333  locker.unlock();
334  emit ValueChanged(ivalue);
335  return true;
336  }
337  }
338  else if (vtype == QVariant::Bool)
339  {
340  bool bvalue = Value.toBool();
341  if (roles & Persistent)
342  gLocalContext->SetSetting(m_dbName, (bool)bvalue);
343  value = Value;
344  locker.unlock();
345  emit ValueChanged(bvalue);
346  return true;
347  }
348  else if (vtype == QVariant::String)
349  {
350  QString svalue = Value.toString();
351  if (selections.isEmpty() ? true : selections.contains(svalue))
352  {
353  if (roles & Persistent)
354  gLocalContext->SetSetting(m_dbName, svalue);
355  value = Value;
356  locker.unlock();
357  emit ValueChanged(svalue);
358  return true;
359  }
360  }
361  else if (vtype == QVariant::StringList)
362  {
363  QStringList svalue = Value.toStringList();
364  if (roles & Persistent)
365  gLocalContext->SetSetting(m_dbName, svalue.join(','));
366  value = Value;
367  locker.unlock();
368  emit ValueChanged(svalue);
369  return true;
370  }
371  return false;
372 }
373 
374 void TorcSetting::SetRange(int Begin, int End, int Step)
375 {
376  QWriteLocker locker(&m_httpServiceLock);
377  if (type != Integer)
378  return;
379 
380  if (Begin >= End || Step < 1)
381  {
382  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Invalid setting range: begin %1 end %2 step %3")
383  .arg(Begin).arg(End).arg(Step));
384  return;
385  }
386 
387  m_begin = Begin;
388  m_end = End;
389  m_step = Step;
390 }
391 
392 void TorcSetting::SetHelpText(const QString &HelpText)
393 {
394  QWriteLocker locker(&m_httpServiceLock);
395  helpText = HelpText;
396 }
397 
398 QVariant TorcSetting::GetValue(void)
399 {
400  QReadLocker locker(&m_httpServiceLock);
401  switch (type)
402  {
403  case Integer: return value.toInt();
404  case Bool: return value.toBool();
405  case String: return value.toString();
406  case StringList: return value.toStringList();
407  case Group: return QVariant();
408  }
409 
410  return value;
411 }
412 
419 TorcSettingGroup::TorcSettingGroup(TorcSetting *Parent, const QString &UIName)
420  : TorcSetting(Parent, UIName, UIName, Group, Public, QVariant())
421 {
423 }
QVariant GetDefaultValue(void)
TorcSettingGroup(TorcSetting *Parent, const QString &UIName)
QVariantMap GetSelections(void)
QString GetSettingType(void)
void SetHelpText(const QString &HelpText)
void Removed(void)
QVariantMap selections
Definition: torcsetting.h:52
TorcLocalContext * gLocalContext
QVariant GetValue(void)
#define TORC_SETTINGS_DIR
Definition: torclocaldefs.h:16
QString GetHelpText(void)
int GetEnd(void)
QString settingType
Definition: torcsetting.h:51
void SubscriberDeleted(QObject *Subscriber)
A wrapper around a database setting.
Definition: torcsetting.h:15
QString uiName
Definition: torcsetting.h:47
void RemoveChild(TorcSetting *Child)
TorcSetting * FindChild(const QString &Child, bool Recursive=false)
virtual bool DownRef(void)
void SetSetting(const QString &Name, const QString &Value)
void Remove(void)
TorcSetting(TorcSetting *Parent, const QString &DBName, const QString &UIName, Type SettingType, Roles SettingRoles, const QVariant &Default)
Definition: torcsetting.cpp:50
QSet< TorcSetting * > GetChildren(void)
QString GetUiName(void)
bool isActive
Definition: torcsetting.h:50
void AddChild(TorcSetting *Child)
int GetStep(void)
void HandleSubscriberDeleted(QObject *Subscriber)
QString GetSetting(const QString &Name, const QString &DefaultValue)
QReadWriteLock m_httpServiceLock
QVariant value
Definition: torcsetting.h:46
bool SetValue(const QVariant &Value)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
int GetBegin(void)
void ActiveChanged(bool Active)
QVariant defaultValue
Definition: torcsetting.h:49
void SetActiveThreshold(int Threshold)
void SetRange(int Begin, int End, int Step)
void SetSelections(QVariantMap &Selections)
void ValueChanged(int Value)
bool GetIsActive(void)
QString EnumToLowerString(T Value)
Definition: torccoreutils.h:18
QVariantMap GetChildList(void)
QString helpText
Definition: torcsetting.h:48
void SetActive(bool Value)