Interface properties

GObject interfaces can also have properties. Declaration of the interface properties is similar to declaring the properties of ordinary GObject types as explained in the section called “Object properties”, except that g_object_interface_install_property is used to declare the properties instead of g_object_class_install_property.

To include a property named 'autosave-frequency' of type gdouble in the ViewerEditable interface example code above, we only need to add one call in viewer_editable_default_init as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
static void
viewer_editable_default_init (ViewerEditableInterface *iface)
{
  g_object_interface_install_property (iface,
                                       g_param_spec_double ("autosave-frequency",
                                                            "Autosave frequency",
                                                            "Frequency (in per-seconds) to autosave backups of the editable content at. "
                                                            "Or zero to disable autosaves.",
                                                            0.0,  /* minimum */
                                                            G_MAXDOUBLE,  /* maximum */
                                                            0.0,  /* default */
                                                            G_PARAM_READWRITE));
}

One point worth noting is that the declared property wasn't assigned an integer ID. The reason being that integer IDs of properties are used only inside the get_property and set_property virtual methods. Since interfaces declare but do not implement properties, there is no need to assign integer IDs to them.

An implementation declares and defines its properties in the usual way as explained in the section called “Object properties”, except for one small change: it can declare the properties of the interface it implements using g_object_class_override_property instead of g_object_class_install_property. The following code snippet shows the modifications needed in the ViewerFile declaration and implementation above:

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
struct _ViewerFile
{
  GObject parent_instance;

  gdouble autosave_frequency;
};

enum
{
  PROP_AUTOSAVE_FREQUENCY = 1,
  N_PROPERTIES
};

static void
viewer_file_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  ViewerFile *file = VIEWER_FILE (object);

  switch (prop_id)
    {
    case PROP_AUTOSAVE_FREQUENCY:
      file->autosave_frequency = g_value_get_double (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
viewer_file_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  ViewerFile *file = VIEWER_FILE (object);

  switch (prop_id)
    {
    case PROP_AUTOSAVE_FREQUENCY:
      g_value_set_double (value, file->autosave_frequency);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
viewer_file_class_init (ViewerFileClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = viewer_file_set_property;
  object_class->get_property = viewer_file_get_property;

  g_object_class_override_property (object_class, PROP_AUTOSAVE_FREQUENCY, "autosave-frequency");
}