GeSettings tutorial

If you are here, it's because you want a way to store your settings on disk less complicated that using GConf# and schemas. And I agree with you, that's why I created this library, to avoid all the burden of creating and deploying a GConf schema, that is:

This is just an example on how Mono can turn some things that were very complicated into very easy ones. This is an example of the use of my library.

using System;

interface IMySettings 
{
	[GeSettings.Property ("myMonoApp", 10)]
	int SomeNumber
	{
		get; 
		set;
	}
	event GConf.NotifyEventHandler SomeNumberChanged;

	[GeSettings.Property ("myMonoApp", 1.4)]
	double SomeDoubleNumber
	{
		get; 
		set;
	}
}

class GConfExample2
{
	public static void Main ()
	{
		IMySettings mySettings = (IMySettings) 
			GeSettings.Builder.Build (typeof(IMySettings));

		mySettings.SomeNumberChanged += 
			new GConf.NotifyEventHandler (OnSomeNumberChanged);

		Console.WriteLine ("Value of 'SomeNumber': {0}", 
				mySettings.SomeNumber);

		Console.WriteLine ("Value of 'SomeDoubleNumber': {0}", 
				mySettings.SomeDoubleNumber);

		mySettings.SomeNumber = 14;
		mySettings.SomeDoubleNumber = 3.1;
	}

	static void OnSomeNumberChanged (object o, GConf.NotifyEventArgs a)
	{
		Console.WriteLine ("SomeNumber changed");
	}
}

To compile this example you'll have to reference gconf-sharp.dll and ge_settings_gconf.dll. You can get the latter here.

What have we just done? Well, first we defined an interface with a few properties. All the properties have a GeSettings.Property attribute that indicates the name of the app and the default value. If you define an event of type GConf.NotifyEventHandler with the same of the property plus 'Changed', the delegates connected to this event will be triggered whenever GConf notify a change of the property value.
Then we passed the interface as the argument of a function that returns an implementation of that interface. This implementation uses GConf transparently, thus whenever we set or get the property, it will use GConf to retrieve or store the setting on disk.

Notice that we just used int and double data types transparently. This is because of Mono facilities to serialize primitives to strings. Just check before setting the value, that it is a valid one to the wanted type, otherwise you'll get a crash with FormatException.

And there is more! With another class I created (it is apart so one doesn't have to depend on Gtk# for other uses), one can do this:


interface IMySettings 
{
	[GeSettings.Property ("GnoMencoder",
				"/dev/cdroms/cdrom0",
				WidgetName = "dvddevice_entry")]
	string DVDDevice
	{
		get;
		set;
	}
}

And, with this function:

	IMySettings mySettings = (IMySettings) 
GeSettings.GladeBuilder.Build (typeof(IMySettings), gxml);

you'll have the property associated with the widget on the glade resource, which will update the GConf value on any changes to the widget and display any external modifications (it is not advisable to use this if you're using other types but string as it is very easy to get wrong format exceptions all the time if you don't check the input)