xfce4-panel-plugin设置的读写

设置文件:

~/.config/xfce4/panel/*.rc

插件右键属性信号:

g_signal_connect (G_OBJECT (plugin), "configure-plugin",  G_CALLBACK (sample_configure), sample);

sample-dialogs.c

void sample_configure (XfcePanelPlugin *plugin, SamplePlugin *sample)
{
    dialog = xfce_titled_dialog_new_with_buttons(...);
    textView = gtk_text_view_new ();
    g_signal_connect (G_OBJECT(textView), "focus-out-event", G_CALLBACK (textView_change_cb), sample);
    g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK(sample_configure_response), sample);
}

static gboolean textView_change_cb(GtkWidget *widget, GdkEventFocus *ev, SamplePlugin *sample)
{
    GtkTextIter start, end;
    GtkTextBuffer *buffer;
    gchar *s;

    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
    gtk_text_buffer_get_bounds (buffer, &start, &end);
    s = gtk_text_iter_get_text (&start, &end);
    sample->setting1 = s;
    update(sample);
    return FALSE;
}

static void sample_configure_response (GtkWidget *dialog,  gint response, SamplePlugin *sample)
{
    gboolean result;

    if (response == GTK_RESPONSE_HELP) {
      result = g_spawn_command_line_async ("exo-open --launch WebBrowser " PLUGIN_WEBSITE, NULL);
      if (G_UNLIKELY (result == FALSE))
          g_warning (_("Unable to open the following url: %s"), PLUGIN_WEBSITE);
    }  else {
        /* remove the dialog data from the plugin */
        g_object_set_data (G_OBJECT (sample->plugin), "dialog", NULL);

        /* unlock the panel menu */
        xfce_panel_plugin_unblock_menu (sample->plugin);

        /* save the plugin */
        sample_save (sample->plugin, sample);

        /* destroy the properties dialog */
        gtk_widget_destroy (dialog);
    }
}

sample.c

void sample_save (XfcePanelPlugin *plugin, SamplePlugin *sample)
{
    XfceRc *rc;
    gchar  *file;

    /* get the config file location */
    file = xfce_panel_plugin_save_location (plugin, TRUE);

    if (G_UNLIKELY (file == NULL)) {
         DBG ("Failed to open config file");
         return;
    }

    /* open the config file, read/write */
    rc = xfce_rc_simple_open (file, FALSE);
    g_free (file);

    if (G_LIKELY (rc != NULL)) {
        /* save the settings */
        DBG(".");
        if (sample->setting1)
          xfce_rc_write_entry    (rc, "setting1", sample->setting1);

          xfce_rc_write_int_entry  (rc, "setting2", sample->setting2);
          xfce_rc_write_bool_entry (rc, "setting3", sample->setting3);

          /* close the rc file */
          xfce_rc_close (rc);
    }
}

static void sample_read (SamplePlugin *sample)
{
    XfceRc      *rc;
    gchar       *file;
    const gchar *value;

    /* get the plugin config file location */
    file = xfce_panel_plugin_save_location (sample->plugin, TRUE);

    if (G_LIKELY (file != NULL)) {
        /* open the config file, readonly */
        rc = xfce_rc_simple_open (file, TRUE);

        /* cleanup */
        g_free (file);

        if (G_LIKELY (rc != NULL)) {
            /* read the settings */
            value = xfce_rc_read_entry (rc, "setting1", "");
            sample->setting1 = g_strdup (value);

            sample->setting2 = xfce_rc_read_int_entry (rc, "setting2", DEFAULT_SETTING2);
            sample->setting3 = xfce_rc_read_bool_entry (rc, "setting3", DEFAULT_SETTING3);

            /* cleanup */
            xfce_rc_close (rc);

            /* leave the function, everything went well */
            return;
        }
    }

    /* something went wrong, apply default values */
    DBG ("Applying default settings");

    sample->setting1 = g_strdup (DEFAULT_SETTING1);
    sample->setting2 = DEFAULT_SETTING2;
    sample->setting3 = DEFAULT_SETTING3;
}

void update (SamplePlugin *sample)
{
    gtk_widget_set_tooltip_text(sample->label, sample->setting1);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值