Vala + GTK + 3 and Granite (save values ​​in dconf)

Programming in Vala and GTK +, we have all ever wondered how to make an application written in Vala and GTK + be able to save the states of the window size parameters and others? They are of these typical cases that we use gschemas to save these values ​​(this well-known application dconf-editor) that graphically manages these schemes.

Now I show you how to do this in simple steps and without complications using the Granite.Services.Settings class from the Granite library.

What do we need?

First we need to have the GTK +, Granite and Vala libraries installed in order to compile our code. To do this in Ubuntu / ElementaryOS we use the command:

sudo apt-get install libgtk-3-dev libgranite-dev granite-demo valac

And we will also need any text editor that supports Vala / GTK + syntax and XML such as Scratch Text Editor (ElementaryOS default), GEdit, Sublime Text + Plugins, etc.

The code

Here are the Example files. For better understanding I leave the code in PASTE with comments in them:

main.vala
tutorial.vala
settings.vala
org.tutorial.gschema.xml

Explanations

The Schema file, if or if it must be XML and be called ORG.APLICACION.GSCHEMA.XML. That is, both the ORG and GSCHEMA.XML must be there for dconf to recognize it as a schema. In addition, these diagrams must be copied to the route:

/usr/share/glib-2.0/schemas/

and they need to be compiled for DCONF to take them, for this we execute the command:

sudo glib-compile-schemas /usr/share/glib-2.0/schemas/

Compiling

To compile our program we have to launch the following command in a terminal:

valac --pkg gtk+-3.0 --pkg granite main.vala tutorial.vala settings.vala

And run the binary with the command:

./main

Conclusions

It is one of the best methods to save the state of our APP in GSCHEMA using GLIB. Any kind of question I will be happy to answer.

Here is a screenshot of my Ubuntu and the Example:

snap-TUtorial


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Raul P. said

    Good article, a few GTK tutorials in C ++ are not bad. Things have changed a lot, the official GTK site recommends books from 2002, I made the mistake of buying it.

    DevHelp helps more than any book.

    1.    Raul P. said

      Sorry I meant C, not C ++. When I open the codeblocks, I always choose "A project in C ++", but I program in C.

  2.   threat said

    Good tutor, I would like to know and I know that it is not the case, but in truth I have given myself enough head blows to be able to carry out this approach, I need to customize the background and the login screen with an institutional image of where I work on the teams users, they have a Venezuelan distro called Canaima, based on Debian 7 Wheezy, users authenticate via LDAP and several users can work on the same PC, so it may be the case that there are several sessions in which they must owning the wallpaper, the teams are customized by the script to facilitate the work of the technicians and then the policies are injected by the puppetmaster server.

    I have tried with gsettings but it only works with the user who is logging in, apparently it saves the information in a local database per user which I have not yet achieved, I am looking for some configuration that applies to the entire system and not to a single user.

    It is worth mentioning that the image is constantly changing due to national dates and any other reason, so it is not just an image, they are uploaded by the puppetmaster to each client.

    I have no idea if something like this can be applied to my case.
    Greetings.