Пример #1
0
QVariantList QGSettings::choices(const QString &key) const
{
    Q_D(const QGSettings);

    QVariantList choices;

    if (!d->valid)
        return choices;

    gchar *keyName = Utils::fromCamelCase(key);
    GSettingsSchemaKey *schemaKey = g_settings_schema_get_key(d->schema, keyName);
    GVariant *range = g_settings_schema_key_get_range(schemaKey);
    g_settings_schema_key_unref(schemaKey);
    g_free(keyName);

    if (!range)
        return choices;

    const gchar *type;
    GVariant *value;
    g_variant_get(range, "(&sv)", &type, &value);

    if (g_str_equal(type, "enum")) {
        GVariantIter iter;
        g_variant_iter_init(&iter, value);

        GVariant *child;
        while ((child = g_variant_iter_next_value(&iter))) {
            choices.append(Utils::toQVariant(child));
            g_variant_unref(child);
        }
    }

    g_variant_unref(value);
    g_variant_unref(range);

    return choices;
}
Пример #2
0
static NimfSettingsPage *
nimf_settings_page_new (NimfSettings *nsettings,
                        const gchar  *schema_id)
{
  NimfSettingsPage *page;
  GSettingsSchema  *schema;
  GList            *key_list = NULL;
  gchar           **keys;
  GList            *l;
  gint              i;

  page = g_slice_new0 (NimfSettingsPage);
  page->gsettings = g_settings_new (schema_id);
  page->label = nimf_settings_page_build_label (page, schema_id);
  page->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 15);
  page->page_keys = g_ptr_array_new_with_free_func ((GDestroyNotify) nimf_settings_page_key_free);

#if GTK_CHECK_VERSION (3, 12, 0)
  gtk_widget_set_margin_start  (page->box, 15);
  gtk_widget_set_margin_end    (page->box, 15);
#else
  gtk_widget_set_margin_left   (page->box, 15);
  gtk_widget_set_margin_right  (page->box, 15);
#endif

  gtk_widget_set_margin_top    (page->box, 15);
  gtk_widget_set_margin_bottom (page->box, 15);

  schema = g_settings_schema_source_lookup (nsettings->schema_source,
                                            schema_id, TRUE);
#if GLIB_CHECK_VERSION (2, 46, 0)
  keys = g_settings_schema_list_keys (schema);
#else
  keys = g_settings_list_keys (page->gsettings);
#endif

  for (i = 0; keys[i] != NULL; i++)
    key_list = g_list_prepend (key_list, keys[i]);

  key_list = g_list_sort (key_list, (GCompareFunc) on_comparison);

  for (i = 0, l = key_list; l != NULL; l = l->next, i++)
  {
    GVariant            *variant;
    GSettingsSchemaKey  *schema_key = NULL;
    NimfSettingsPageKey *page_key;
    const GVariantType  *type;
    const gchar         *key;
    const gchar         *summary;
    const gchar         *desc;

    key = l->data;

    if (g_str_has_prefix (key, "hidden-"))
      continue;

    variant = g_settings_get_value (page->gsettings, key);
    type = g_variant_get_type (variant);
    schema_key = g_settings_schema_get_key (schema, key);
    summary = g_settings_schema_key_get_summary     (schema_key);
    desc    = g_settings_schema_key_get_description (schema_key);

    page_key = nimf_settings_page_key_new (page->gsettings, key, summary, desc);
    g_ptr_array_add (page->page_keys, page_key);

    if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
    {
      GtkWidget *item;
      item = nimf_settings_page_key_build_boolean (page_key);
      gtk_box_pack_start (GTK_BOX (page->box), item, FALSE, FALSE, 0);
    }
    else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
    {
      GtkWidget *item;
      item = nimf_settings_page_key_build_string (page_key, schema_id, key_list);
      gtk_box_pack_start (GTK_BOX (page->box), item, FALSE, FALSE, 0);
    }
    else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING_ARRAY))
    {
      GtkWidget *item;
      item = nimf_settings_page_key_build_string_array (page_key);
      gtk_box_pack_start (GTK_BOX (page->box), item, FALSE, FALSE, 0);
    }
    else
      g_error (G_STRLOC ": %s: not supported variant type: \"%s\"",
               G_STRFUNC, (gchar *) type);

    g_settings_schema_key_unref (schema_key);
    g_variant_unref (variant);
  }

  g_strfreev (keys);
  g_list_free (key_list);
  g_settings_schema_unref (schema);

  return page;
}