#include "gconf-wrapper.h"
-GConfClientWrapper::GConfClientWrapper() {
- myClient = gconf_client_get_default();
+GConfClient *GConfItem::ourClient = NULL;
+
+GConfItem::GConfItem() {
+ validateClient();
+}
+
+void GConfItem::validateClient() {
+ if (ourClient == NULL) {
+ ourClient = gconf_client_get_default();
+ }
+}
+
+GConfKey::GConfKey(const std::string& path): myKeyPath(path) {
+}
+
+GConfStringValue::GConfStringValue(const GConfKey& key, const std::string& name, const std::string& defaultValue):
+ GConfOption(key, name),
+ myDefaultValue(defaultValue) {
+}
+
+const std::string& GConfStringValue::value() const {
+ if (!myIsSynchronized) {
+ GConfValue *value = getGConfValue(GCONF_VALUE_STRING);
+
+ if (value == NULL) {
+ myValue = myDefaultValue;
+ } else {
+ myValue = gconf_value_get_string(value);
+
+ gconf_value_free(value);
+ }
+
+ myIsSynchronized = true;
+ }
+
+ return myValue;
+}
+
+const std::string& GConfStringValue::setValue(const std::string& newValue) {
+ if (!myIsSynchronized || (myValue != newValue)) {
+ myValue = newValue;
+
+ if (myValue == myDefaultValue) {
+ unsetGConfValue();
+ } else {
+ GConfValue *value = gconf_value_new(GCONF_VALUE_STRING);
+
+ gconf_value_set_string(value, myValue.c_str());
+
+ setGConfValue(value);
+
+ gconf_value_free(value);
+ }
+
+ myIsSynchronized = true;
+ }
+
+ return myValue;
+}
+
+GConfBooleanValue::GConfBooleanValue(const GConfKey& key, const std::string& name, bool defaultValue):
+ GConfOption(key, name),
+ myDefaultValue(defaultValue) {
+}
+
+bool GConfBooleanValue::value() const {
+ if (!myIsSynchronized) {
+ GConfValue *value = getGConfValue(GCONF_VALUE_BOOL);
+
+ if (value == NULL) {
+ myValue = myDefaultValue;
+ } else {
+ myValue = gconf_value_get_bool(value);
+
+ gconf_value_free(value);
+ }
+
+ myIsSynchronized = true;
+ }
+
+ return myValue;
+}
+
+bool GConfBooleanValue::setValue(bool newValue) {
+ if (!myIsSynchronized || (myValue != newValue)) {
+ myValue = newValue;
+
+ if (myValue == myDefaultValue) {
+ unsetGConfValue();
+ } else {
+ GConfValue *value = gconf_value_new(GCONF_VALUE_BOOL);
+
+ gconf_value_set_bool(value, myValue);
+
+ setGConfValue(value);
+
+ gconf_value_free(value);
+ }
+
+ myIsSynchronized = true;
+ }
+
+ return myValue;
}
-GConfClientWrapper::~GConfClientWrapper() {
+GConfIntegerValue::GConfIntegerValue(const GConfKey& key, const std::string& name, int defaultValue):
+ GConfOption(key, name),
+ myDefaultValue(defaultValue) {
}
-GConfKey GConfClientWrapper::getKey(const std::string& path) {
- // FIXME: check if path points to a good place :)
- return GConfKey(*this, path);
+int GConfIntegerValue::value() const {
+ if (!myIsSynchronized) {
+ GConfValue *value = getGConfValue(GCONF_VALUE_INT);
+
+ if (value == NULL) {
+ myValue = myDefaultValue;
+ } else {
+ myValue = gconf_value_get_int(value);
+
+ gconf_value_free(value);
+ }
+
+ myIsSynchronized = true;
+ }
+
+ return myValue;
}
-GConfKey::GConfKey(GConfClientWrapper& wrapper, const std::string& path) : myWrapper(wrapper), myPath(path) {
+int GConfIntegerValue::setValue(int newValue) {
+ if (!myIsSynchronized || (myValue != newValue)) {
+ myValue = newValue;
+
+ if (myValue == myDefaultValue) {
+ unsetGConfValue();
+ } else {
+ GConfValue *value = gconf_value_new(GCONF_VALUE_INT);
+
+ gconf_value_set_int(value, myValue);
+
+ setGConfValue(value);
+
+ gconf_value_free(value);
+ }
+
+ myIsSynchronized = true;
+ }
+
+ return myValue;
}
+
+// vim:ts=2:sw=2:et
#include <gconf/gconf-client.h>
-class GConfClientWrapper {
- friend class GConfKey;
+class GConfItem {
+public:
+ virtual ~GConfItem() {}
+
+protected:
+ GConfItem(); // I do not want to create instances of this class
+ static void validateClient();
+
+protected:
+ static GConfClient *ourClient;
+};
+
+class GConfKey : public GConfItem {
public:
- GConfClientWrapper();
- ~GConfClientWrapper();
+ GConfKey(const std::string&);
+ GConfKey(const GConfKey&, const std::string&);
+ ~GConfKey() { }
+
+ const std::string& path() const { return myKeyPath; }
+
+ static std::string mergePath(const std::string&, const std::string);
+
+private:
+ std::string myKeyPath;
+};
+
+class GConfOption : public GConfItem {
+protected:
+ GConfOption(const GConfKey& key, const std::string& path): myIsSynchronized(false), myPath(GConfKey::mergePath(key.path(), path)) { }
- GConfKey getKey(const std::string&);
+ void setGConfValue(const GConfValue *);
+ GConfValue *getGConfValue(GConfValueType) const;
+ void unsetGConfValue();
protected:
- bool getBool(const std::string& name);
- void setBool(const std::string& name, bool);
+ mutable bool myIsSynchronized;
+ std::string myPath;
+};
- int getInt(const std::string& name);
- void setInt(const std::string& name, int);
+class GConfStringValue : public GConfOption {
+public:
+ GConfStringValue(const GConfKey&, const std::string&, const std::string& = "");
+ ~GConfStringValue();
+
+ const std::string& value() const;
+ const std::string& setValue(const std::string& newValue);
private:
- GConfClient *myClient;
+ mutable std::string myValue;
+ std::string myDefaultValue;
};
-class GConfKey {
+class GConfBooleanValue : public GConfOption {
public:
- GConfKey(GConfClientWrapper&, const std::string&);
- GConfKey(const GConfKey& what) : myWrapper(what.myWrapper), myPath(what.myPath) { }
- ~GConfKey();
+ GConfBooleanValue(const GConfKey&, const std::string&, bool = false);
+ ~GConfBooleanValue();
- GConfKey& operator = (const GConfKey& what) {
- myWrapper = what.myWrapper;
- myPath = what.myPath;
+ bool value() const;
+ bool setValue(bool newValue);
- return *this;
- }
+private:
+ mutable bool myValue;
+ bool myDefaultValue;
+};
- bool getBool(const std::string& name, bool defvalue = false);
- void setBool(const std::string& name, bool value);
+class GConfIntegerValue : public GConfOption {
+public:
+ GConfIntegerValue(const GConfKey&, const std::string&, int = false);
+ ~GConfIntegerValue();
- int getInt(const std::string& name, int defvalue = 0);
- void setInt(const std::string& name, int value);
+ int value() const;
+ int setValue(int newValue);
private:
- GConfClientWrapper& myWrapper;
- std::string myPath;
+ mutable int myValue;
+ int myDefaultValue;
};
#endif