[glibmm] Add basic Gio::Settings example



commit 58f285601c07d8da32d1c6cdcb1b8454f6990f5b
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Wed Apr 21 21:34:02 2010 -0500

    Add basic Gio::Settings example

 .gitignore                                   |    4 +-
 ChangeLog                                    |    9 +++
 examples/Makefile.am                         |   14 ++++-
 examples/settings/org.gtkmm.demo.gschema.xml |   12 +++++
 examples/settings/settings.cc                |   68 ++++++++++++++++++++++++++
 gio/giomm.h                                  |    1 +
 6 files changed, 104 insertions(+), 4 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index e40bca5..5b5a11c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -59,8 +59,10 @@ giommconfig.h
 /examples/keyfile/example
 /examples/markup/parser
 /examples/options/example
-/examples/properties/example
+/examples/properties/properties_example
 /examples/regex/example
+/examples/settings/settings
+/examples/settings/gschemas.compiled
 /examples/thread/dispatcher
 /examples/thread/dispatcher2
 /examples/thread/thread
diff --git a/ChangeLog b/ChangeLog
index f026e67..43365eb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2010-04-21  Jonathon Jongsma  <jonathon jongsma collabora co uk>
+
+	* .gitignore:
+	* examples/Makefile.am:
+	* examples/settings/org.gtkmm.demo.gschema.xml: Added.
+	* examples/settings/settings.cc: Added.
+	* gio/giomm.h: include settings header here
+	Add a basic Gio::Settings example
+
 2010-04-20  Jonathon Jongsma  <jonathon jongsma collabora co uk>
 
 	* gio/src/filelist.am:
diff --git a/examples/Makefile.am b/examples/Makefile.am
index eb05e78..0e14925 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -29,17 +29,19 @@ check_PROGRAMS =			\
 	options/example			\
 	properties/example		\
 	regex/example			\
+	settings/settings		\
 	thread/dispatcher		\
 	thread/dispatcher2		\
 	thread/thread			\
-	thread/threadpool
+	thread/threadpool		\
+	gschemas.compiled
 
 glibmm_includes = -I$(top_builddir)/glib $(if $(srcdir:.=),-I$(top_srcdir)/glib)
 giomm_includes  = -I$(top_builddir)/gio $(if $(srcdir:.=),-I$(top_srcdir)/gio)
 local_cppflags  = -I$(top_builddir) $(glibmm_includes) $(giomm_includes)
 
-AM_CPPFLAGS = $(local_cppflags) $(GTHREAD_CFLAGS) $(GIOMM_CFLAGS)
-AM_CXXFLAGS = $(GLIBMM_WXXFLAGS)
+AM_CPPFLAGS = $(local_cppflags) $(GTHREAD_CFLAGS)
+AM_CXXFLAGS = $(GIOMM_CFLAGS) $(GLIBMM_WXXFLAGS)
 
 local_libglibmm = $(top_builddir)/glib/glibmm/libglibmm-$(GLIBMM_API_VERSION).la
 local_libgiomm  = $(top_builddir)/gio/giomm/libgiomm-$(GIOMM_API_VERSION).la
@@ -76,3 +78,9 @@ network_socket_client_SOURCES   = network/socket-client.cc
 network_socket_client_LDADD     = $(giomm_ldadd) $(GTHREAD_LIBS)
 network_socket_server_SOURCES   = network/socket-server.cc
 network_socket_server_LDADD     = $(giomm_ldadd) $(GTHREAD_LIBS)
+
+gschemas.compiled: $(wildcard settings/*.gschema.xml)
+	gschema-compile settings/
+
+settings_settings_SOURCES = settings/settings.cc
+settings_settings_LDADD   = $(giomm_ldadd)
diff --git a/examples/settings/org.gtkmm.demo.gschema.xml b/examples/settings/org.gtkmm.demo.gschema.xml
new file mode 100644
index 0000000..5e4654e
--- /dev/null
+++ b/examples/settings/org.gtkmm.demo.gschema.xml
@@ -0,0 +1,12 @@
+<schemalist>
+	<schema id="org.gtkmm.demo" path="/">
+        <key type="i" name="test-int">
+            <default>42</default>
+            <summary>An integer value</summary>
+        </key>
+        <key type="s" name="test-string">
+            <default>"Magnificent Frigatebird"</default>
+            <summary>A string value</summary>
+        </key>
+    </schema>
+</schemalist>
diff --git a/examples/settings/settings.cc b/examples/settings/settings.cc
new file mode 100644
index 0000000..c9da1de
--- /dev/null
+++ b/examples/settings/settings.cc
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ *
+ *  Copyright (c) 2010 Jonathon Jongsma
+ *
+ *  This file is part of gtkmm
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>
+ *
+ *******************************************************************************/
+
+#include <giomm.h>
+#include <iostream>
+
+const char* STRING_KEY = "test-string";
+const char* INT_KEY = "test-int";
+
+static void on_key_changed(const Glib::ustring& key, const Glib::RefPtr<Gio::Settings>& settings)
+{
+    std::cout << Glib::ustring::compose("'%1' changed\n", key);
+    if (key == STRING_KEY)
+        std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
+                                            key, settings->get_string(key));
+    else if (key == INT_KEY)
+        std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
+                                            key, settings->get_int(key));
+    else
+        std::cerr << "Unknown key\n";
+}
+
+int main(int argc, char** argv)
+{
+    Glib::init();
+
+    // this is only a demo so we don't want to rely on an installed schema.
+    // Instead we set some environment variables that allow us to test things
+    // from the source directory.  We need to strip off the .libs/ directory
+    // first (thus the '..').  Generally you would install your schemas to the system schema
+    // directory
+    std::string dirname = Glib::build_filename(Glib::path_get_dirname(argv[0]), "..");
+    Glib::setenv ("GSETTINGS_SCHEMA_DIR", dirname, TRUE);
+    Glib::setenv ("GSETTINGS_BACKEND", "memory", TRUE);
+
+    Glib::RefPtr<Gio::Settings> settings =
+        Gio::Settings::create("org.gtkmm.demo");
+
+    settings->signal_changed().connect(sigc::bind(sigc::ptr_fun(&on_key_changed), settings));
+
+    std::cout << Glib::ustring::compose("Initial value of '%1': '%2'\n",
+                                        STRING_KEY, settings->get_string(STRING_KEY));
+    settings->set_string(STRING_KEY, "Hoopoe");
+
+    std::cout << Glib::ustring::compose("Initial value of '%1': '%2'\n",
+                                        INT_KEY, settings->get_int(INT_KEY));
+    settings->set_int(INT_KEY, 18);
+
+    return 0;
+}
diff --git a/gio/giomm.h b/gio/giomm.h
index 99ed546..0b7016c 100644
--- a/gio/giomm.h
+++ b/gio/giomm.h
@@ -66,6 +66,7 @@
 #include <giomm/outputstream.h>
 #include <giomm/resolver.h>
 #include <giomm/seekable.h>
+#include <giomm/settings.h>
 #include <giomm/socket.h>
 #include <giomm/socketaddress.h>
 #include <giomm/socketaddressenumerator.h>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]