settingsproviderqt.cpp
1 //------------------------------------------------------------------------------
2 // settingsproviderqt.cpp
3 //------------------------------------------------------------------------------
4 // Copyright 2011 - 2013 Zalewa <zalewapl@gmail.com>. All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in
14 // the documentation and/or other materials provided with
15 // the distribution.
16 //
17 // THIS SOFTWARE IS PROVIDED BY ZALEWA ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 // EVENT SHALL ZALEWA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
26 // WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 // OF SUCH DAMAGE.
28 //
29 // The views and conclusions contained in the software and documentation are
30 // those of the authors and should not be interpreted as representing official
31 // policies, either expressed or implied, of Zalewa.
32 //------------------------------------------------------------------------------
33 // Copyright (C) 2013 "Zalewa" <zalewapl@gmail.com>
34 //------------------------------------------------------------------------------
35 #include "settingsproviderqt.h"
36 
37 #include <cassert>
38 
39 DClass<SettingsProviderQt>
40 {
41 public:
42  QSettings *target;
43 
49  QString exactKey(const QString &key) const
50  {
51  assert(target != nullptr);
52  for (const QString &candidate : target->allKeys())
53  {
54  if (candidate.compare(key, Qt::CaseInsensitive) == 0)
55  return candidate;
56  }
57  return key;
58  }
59 };
60 
61 DPointered(SettingsProviderQt)
62 
63 SettingsProviderQt::SettingsProviderQt(QSettings *target)
64 {
65  d->target = target;
66 }
67 
68 SettingsProviderQt::~SettingsProviderQt()
69 {
70 }
71 
72 QStringList SettingsProviderQt::allKeys() const
73 {
74  assert(d->target != nullptr);
75  return d->target->allKeys();
76 }
77 
78 QStringList SettingsProviderQt::allSections() const
79 {
80  assert(d->target != nullptr);
81  return d->target->childGroups();
82 }
83 
84 bool SettingsProviderQt::hasKey(const QString &key) const
85 {
86  return d->target->contains(d->exactKey(key));
87 }
88 
89 void SettingsProviderQt::remove(const QString &key)
90 {
91  d->target->remove(d->exactKey(key));
92 }
93 
94 void SettingsProviderQt::setValue(const QString &key, const QVariant &value)
95 {
96  assert(d->target != nullptr);
97  d->target->setValue(d->exactKey(key), value);
98 }
99 
100 QVariant SettingsProviderQt::value(const QString &key, QVariant defValue) const
101 {
102  assert(d->target != nullptr);
103  return d->target->value(d->exactKey(key), defValue);
104 }