summaryrefslogtreecommitdiff
path: root/examples/settings/settings.cc
blob: 84df7a0e3cb05f2502c747ca3e737e58b35dc915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
 *
 *  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 *const STRING_KEY = "test-string";
const char *const 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)
  {
    Glib::ustring str = settings->get_string(key);
    std::cout << Glib::ustring::compose("New value of '%1': '%2'\n",
                      key, str);
                      
    //Or:
    Glib::Variant<Glib::ustring> variant;
    settings->get_value(key, variant);
    str = variant.get();
    std::cout << Glib::ustring::compose("New value, via variant, of '%1': '%2'\n",
                      key, str);
  }
  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";
}

static void on_key_changed_all(const Glib::ustring& key)
{
  std::cout << "on_key_changed_all(" << key << ")\n";
}

static void on_key_changed_int(const Glib::ustring& key)
{
  std::cout << "on_key_changed_int(" << key << ")\n";
  if (key != INT_KEY)
    std::cerr << "Unexpected key\n";
}

int main(int, char**)
{
  std::locale::global(std::locale(""));
  Gio::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
  Glib::setenv("GSETTINGS_SCHEMA_DIR", ".", true);
  Glib::setenv("GSETTINGS_BACKEND", "memory", true);

  const auto settings =
    Gio::Settings::create("org.gtkmm.demo");

  settings->signal_changed().connect(sigc::bind(sigc::ptr_fun(&on_key_changed), settings));
  settings->signal_changed("").connect(sigc::ptr_fun(&on_key_changed_all));
  settings->signal_changed(INT_KEY).connect(sigc::ptr_fun(&on_key_changed_int));

  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;
}