1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-dazzle is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.SettingsSandwich;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.Settings;
24 private import glib.ConstructionException;
25 private import glib.Str;
26 private import glib.Variant;
27 private import gobject.ObjectG;
28 
29 
30 /** */
31 public class SettingsSandwich : ObjectG
32 {
33 	/** the main Gtk struct */
34 	protected DzlSettingsSandwich* dzlSettingsSandwich;
35 
36 	/** Get the main Gtk struct */
37 	public DzlSettingsSandwich* getSettingsSandwichStruct(bool transferOwnership = false)
38 	{
39 		if (transferOwnership)
40 			ownedRef = false;
41 		return dzlSettingsSandwich;
42 	}
43 
44 	/** the main Gtk struct as a void* */
45 	protected override void* getStruct()
46 	{
47 		return cast(void*)dzlSettingsSandwich;
48 	}
49 
50 	/**
51 	 * Sets our main struct and passes it to the parent class.
52 	 */
53 	public this (DzlSettingsSandwich* dzlSettingsSandwich, bool ownedRef = false)
54 	{
55 		this.dzlSettingsSandwich = dzlSettingsSandwich;
56 		super(cast(GObject*)dzlSettingsSandwich, ownedRef);
57 	}
58 
59 
60 	/** */
61 	public static GType getType()
62 	{
63 		return dzl_settings_sandwich_get_type();
64 	}
65 
66 	/** */
67 	public this(string schemaId, string path)
68 	{
69 		auto p = dzl_settings_sandwich_new(Str.toStringz(schemaId), Str.toStringz(path));
70 
71 		if(p is null)
72 		{
73 			throw new ConstructionException("null returned by new");
74 		}
75 
76 		this(cast(DzlSettingsSandwich*) p, true);
77 	}
78 
79 	/** */
80 	public void append(Settings settings)
81 	{
82 		dzl_settings_sandwich_append(dzlSettingsSandwich, (settings is null) ? null : settings.getSettingsStruct());
83 	}
84 
85 	/** */
86 	public void bind(string key, void* object, string property, GSettingsBindFlags flags)
87 	{
88 		dzl_settings_sandwich_bind(dzlSettingsSandwich, Str.toStringz(key), object, Str.toStringz(property), flags);
89 	}
90 
91 	/**
92 	 * Creates a new binding similar to g_settings_bind_with_mapping() but applying
93 	 * from the resolved value via the settings sandwich.
94 	 *
95 	 * Params:
96 	 *     key = the settings key to bind.
97 	 *         @object (type GObject.Object): the target object.
98 	 *     property = the property on @object to apply.
99 	 *     flags = flags for the binding.
100 	 *     getMapping = the get mapping function
101 	 *     setMapping = the set mapping function
102 	 *     userData = user data for @get_mapping and @set_mapping.
103 	 *     destroy = destroy notify for @user_data.
104 	 */
105 	public void bindWithMapping(string key, void* object, string property, GSettingsBindFlags flags, GSettingsBindGetMapping getMapping, GSettingsBindSetMapping setMapping, void* userData, GDestroyNotify destroy)
106 	{
107 		dzl_settings_sandwich_bind_with_mapping(dzlSettingsSandwich, Str.toStringz(key), object, Str.toStringz(property), flags, getMapping, setMapping, userData, destroy);
108 	}
109 
110 	/** */
111 	public bool getBoolean(string key)
112 	{
113 		return dzl_settings_sandwich_get_boolean(dzlSettingsSandwich, Str.toStringz(key)) != 0;
114 	}
115 
116 	/** */
117 	public Variant getDefaultValue(string key)
118 	{
119 		auto p = dzl_settings_sandwich_get_default_value(dzlSettingsSandwich, Str.toStringz(key));
120 
121 		if(p is null)
122 		{
123 			return null;
124 		}
125 
126 		return new Variant(cast(GVariant*) p, true);
127 	}
128 
129 	/** */
130 	public double getDouble(string key)
131 	{
132 		return dzl_settings_sandwich_get_double(dzlSettingsSandwich, Str.toStringz(key));
133 	}
134 
135 	/** */
136 	public int getInt(string key)
137 	{
138 		return dzl_settings_sandwich_get_int(dzlSettingsSandwich, Str.toStringz(key));
139 	}
140 
141 	/** */
142 	public string getString(string key)
143 	{
144 		auto retStr = dzl_settings_sandwich_get_string(dzlSettingsSandwich, Str.toStringz(key));
145 
146 		scope(exit) Str.freeString(retStr);
147 		return Str.toString(retStr);
148 	}
149 
150 	/** */
151 	public uint getUint(string key)
152 	{
153 		return dzl_settings_sandwich_get_uint(dzlSettingsSandwich, Str.toStringz(key));
154 	}
155 
156 	/** */
157 	public Variant getUserValue(string key)
158 	{
159 		auto p = dzl_settings_sandwich_get_user_value(dzlSettingsSandwich, Str.toStringz(key));
160 
161 		if(p is null)
162 		{
163 			return null;
164 		}
165 
166 		return new Variant(cast(GVariant*) p, true);
167 	}
168 
169 	/** */
170 	public Variant getValue(string key)
171 	{
172 		auto p = dzl_settings_sandwich_get_value(dzlSettingsSandwich, Str.toStringz(key));
173 
174 		if(p is null)
175 		{
176 			return null;
177 		}
178 
179 		return new Variant(cast(GVariant*) p, true);
180 	}
181 
182 	/** */
183 	public void setBoolean(string key, bool val)
184 	{
185 		dzl_settings_sandwich_set_boolean(dzlSettingsSandwich, Str.toStringz(key), val);
186 	}
187 
188 	/** */
189 	public void setDouble(string key, double val)
190 	{
191 		dzl_settings_sandwich_set_double(dzlSettingsSandwich, Str.toStringz(key), val);
192 	}
193 
194 	/** */
195 	public void setInt(string key, int val)
196 	{
197 		dzl_settings_sandwich_set_int(dzlSettingsSandwich, Str.toStringz(key), val);
198 	}
199 
200 	/** */
201 	public void setString(string key, string val)
202 	{
203 		dzl_settings_sandwich_set_string(dzlSettingsSandwich, Str.toStringz(key), Str.toStringz(val));
204 	}
205 
206 	/** */
207 	public void setUint(string key, uint val)
208 	{
209 		dzl_settings_sandwich_set_uint(dzlSettingsSandwich, Str.toStringz(key), val);
210 	}
211 
212 	/** */
213 	public void setValue(string key, Variant value)
214 	{
215 		dzl_settings_sandwich_set_value(dzlSettingsSandwich, Str.toStringz(key), (value is null) ? null : value.getVariantStruct());
216 	}
217 
218 	/** */
219 	public void unbind(string property)
220 	{
221 		dzl_settings_sandwich_unbind(dzlSettingsSandwich, Str.toStringz(property));
222 	}
223 }