[Goodies-commits] r6451 - in xfce4-clipman-plugin/branches/xfce-4-6: . panel-plugin
Mike Massonnet
mmassonnet at xfce.org
Tue Jan 13 20:51:00 CET 2009
Author: mmassonnet
Date: 2009-01-13 19:51:00 +0000 (Tue, 13 Jan 2009)
New Revision: 6451
Modified:
xfce4-clipman-plugin/branches/xfce-4-6/ChangeLog
xfce4-clipman-plugin/branches/xfce-4-6/TODO
xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.c
xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.h
xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/menu.c
xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/panel-plugin.c
Log:
Save the history on disk.
Modified: xfce4-clipman-plugin/branches/xfce-4-6/ChangeLog
===================================================================
--- xfce4-clipman-plugin/branches/xfce-4-6/ChangeLog 2009-01-13 15:51:13 UTC (rev 6450)
+++ xfce4-clipman-plugin/branches/xfce-4-6/ChangeLog 2009-01-13 19:51:00 UTC (rev 6451)
@@ -1,3 +1,16 @@
+2009-01-13 Mike Massonnet
+Save the history on disk.
+
+ - panel-plugin/history.c, panel-plugin/history.h:
+ New function clipman_history_get_list() and drop the functions
+ clipman_history_get_texts() and clipman_history_get_images().
+ - panel-plugin/menu.c(_clipman_menu_update_list):
+ Use the new function replacement from ClipmanHistory.
+ - panel-plugin/panel-plugin.c:
+ Implement new functions panel_plugin_load() and
+ panel_plugin_save() to respectively load the history (images
+ and texts) and save the history.
+
2009-01-09 Mike Massonnet
Some small fixes.
Modified: xfce4-clipman-plugin/branches/xfce-4-6/TODO
===================================================================
--- xfce4-clipman-plugin/branches/xfce-4-6/TODO 2009-01-13 15:51:13 UTC (rev 6450)
+++ xfce4-clipman-plugin/branches/xfce-4-6/TODO 2009-01-13 19:51:00 UTC (rev 6451)
@@ -1,3 +1,2 @@
-- Store the history
- Add a properties dialog
- Add actions
Modified: xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.c
===================================================================
--- xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.c 2009-01-13 15:51:13 UTC (rev 6450)
+++ xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.c 2009-01-13 19:51:00 UTC (rev 6451)
@@ -180,6 +180,15 @@
* Public methods
*/
+/**
+ * clipman_history_add_text:
+ * @history: a #ClipmanHistory
+ * @text: the text to add to the history
+ * @clipboard: the #GtkClipboard from where the text is originating from
+ *
+ * Stores a text inside the history. If the history is growing over the
+ * maximum number of items, it will delete the oldest text.
+ */
void
clipman_history_add_text (ClipmanHistory *history,
const gchar *text,
@@ -236,6 +245,15 @@
_clipman_history_add_item (history, item);
}
+/**
+ * clipman_history_add_image:
+ * @history: a #ClipmanHistory
+ * @image: the image to add to the history
+ * @clipboard: the #GtkClipboard from where the image is originating from
+ *
+ * Stores an image inside the history. If the history is growing over the
+ * maximum number of items, it will delete the oldest image.
+ */
void
clipman_history_add_image (ClipmanHistory *history,
const GdkPixbuf *image,
@@ -259,18 +277,20 @@
_clipman_history_add_item (history, item);
}
-const GSList *
-clipman_history_get_texts (ClipmanHistory *history)
+/**
+ * clipman_history_get_list:
+ * @history: a #ClipmanHistory
+ *
+ * Returns a unique list of the images and texts.
+ *
+ * Returns: a newly allocated #GSList that must be freed with g_slist_free()
+ */
+GSList *
+clipman_history_get_list (ClipmanHistory *history)
{
- return history->priv->texts;
+ return g_slist_concat (g_slist_copy (history->priv->images), g_slist_copy (history->priv->texts));
}
-const GSList *
-clipman_history_get_images (ClipmanHistory *history)
-{
- return history->priv->images;
-}
-
/**
* clipman_history_get_item_to_restore:
* @history: a #ClipmanHistory
Modified: xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.h
===================================================================
--- xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.h 2009-01-13 15:51:13 UTC (rev 6450)
+++ xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/history.h 2009-01-13 19:51:00 UTC (rev 6451)
@@ -89,8 +89,7 @@
void clipman_history_add_image (ClipmanHistory *history,
const GdkPixbuf *image,
GtkClipboard *clipboard);
-const GSList * clipman_history_get_texts (ClipmanHistory *history);
-const GSList * clipman_history_get_images (ClipmanHistory *history);
+GSList * clipman_history_get_list (ClipmanHistory *history);
const ClipmanHistoryItem * clipman_history_get_item_to_restore (ClipmanHistory *history);
void clipman_history_set_item_to_restore (ClipmanHistory *history,
const ClipmanHistoryItem *item);
Modified: xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/menu.c
===================================================================
--- xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/menu.c 2009-01-13 15:51:13 UTC (rev 6450)
+++ xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/menu.c 2009-01-13 19:51:00 UTC (rev 6451)
@@ -131,7 +131,7 @@
GtkWidget *mi, *image;
ClipmanHistoryItem *item;
const ClipmanHistoryItem *item_to_restore;
- const GSList *list;
+ GSList *list, *l;
gint pos = 0, i;
/* Get the most recent item in the history */
@@ -144,46 +144,40 @@
gtk_widget_set_sensitive (menu->priv->mi_clear_history, TRUE);
/* Insert an updated list of menu items */
- for (i = 0; i < 2; i++)
+ list = clipman_history_get_list (menu->priv->history);
+ for (l = list; l != NULL; l = l->next)
{
- if (i == 0)
- list = clipman_history_get_images (menu->priv->history);
- else if (i == 1)
- list = clipman_history_get_texts (menu->priv->history);
+ item = l->data;
- for (; list != NULL; list = list->next)
+ switch (item->type)
{
- item = list->data;
+ case CLIPMAN_HISTORY_TYPE_TEXT:
+ mi = gtk_image_menu_item_new_with_label (item->preview.text);
+ g_signal_connect_swapped (mi, "activate", G_CALLBACK (cb_set_clipboard), item);
+ break;
- switch (item->type)
- {
- case CLIPMAN_HISTORY_TYPE_TEXT:
- mi = gtk_image_menu_item_new_with_label (item->preview.text);
- g_signal_connect_swapped (mi, "activate", G_CALLBACK (cb_set_clipboard), item);
- break;
+ case CLIPMAN_HISTORY_TYPE_IMAGE:
+ mi = gtk_image_menu_item_new ();
+ image = gtk_image_new_from_pixbuf (item->preview.image);
+ gtk_container_add (GTK_CONTAINER (mi), image);
+ g_signal_connect_swapped (mi, "activate", G_CALLBACK (cb_set_clipboard), item);
+ break;
- case CLIPMAN_HISTORY_TYPE_IMAGE:
- mi = gtk_image_menu_item_new ();
- image = gtk_image_new_from_pixbuf (item->preview.image);
- gtk_container_add (GTK_CONTAINER (mi), image);
- g_signal_connect_swapped (mi, "activate", G_CALLBACK (cb_set_clipboard), item);
- break;
+ default:
+ g_assert_not_reached ();
+ }
- default:
- g_assert_not_reached ();
- }
-
- if (item == item_to_restore)
- {
- image = gtk_image_new_from_stock (GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_MENU);
- gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi), image);
- }
-
- menu->priv->list = g_slist_prepend (menu->priv->list, mi);
- gtk_menu_shell_insert (GTK_MENU_SHELL (menu), mi, pos++);
- gtk_widget_show_all (mi);
+ if (item == item_to_restore)
+ {
+ image = gtk_image_new_from_stock (GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_MENU);
+ gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi), image);
}
+
+ menu->priv->list = g_slist_prepend (menu->priv->list, mi);
+ gtk_menu_shell_insert (GTK_MENU_SHELL (menu), mi, pos++);
+ gtk_widget_show_all (mi);
}
+ g_slist_free (list);
if (pos == 0)
{
Modified: xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/panel-plugin.c
===================================================================
--- xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/panel-plugin.c 2009-01-13 15:51:13 UTC (rev 6450)
+++ xfce4-clipman-plugin/branches/xfce-4-6/panel-plugin/panel-plugin.c 2009-01-13 19:51:00 UTC (rev 6451)
@@ -63,6 +63,10 @@
static gboolean panel_plugin_set_size (XfcePanelPlugin *panel_plugin,
int size,
MyPlugin *plugin);
+static void panel_plugin_load (XfcePanelPlugin *panel_plugin,
+ MyPlugin *plugin);
+static void panel_plugin_save (XfcePanelPlugin *panel_plugin,
+ MyPlugin *plugin);
static void panel_plugin_free (XfcePanelPlugin *panel_plugin,
MyPlugin *plugin);
static void cb_button_toggled (GtkToggleButton *button,
@@ -133,9 +137,14 @@
/* Panel Plugin Signals */
g_signal_connect (panel_plugin, "size-changed",
G_CALLBACK (panel_plugin_set_size), plugin);
+ g_signal_connect (panel_plugin, "save",
+ G_CALLBACK (panel_plugin_save), plugin);
g_signal_connect (panel_plugin, "free-data",
G_CALLBACK (panel_plugin_free), plugin);
+ /* Load the data */
+ panel_plugin_load (panel_plugin, plugin);
+
gtk_widget_show_all (GTK_WIDGET (panel_plugin));
}
@@ -158,6 +167,118 @@
}
static void
+panel_plugin_load (XfcePanelPlugin *panel_plugin,
+ MyPlugin *plugin)
+{
+ GtkClipboard *clipboard;
+ GKeyFile *keyfile;
+ gchar **texts = NULL;
+ gchar *filename;
+ GdkPixbuf *image;
+ gint i = 0;
+
+ clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+
+ /* Load images */
+ while (TRUE)
+ {
+ filename = g_strdup_printf ("%s/xfce4/clipman/image%d", g_get_user_cache_dir (), i++);
+ DBG ("Loading image from cache file %s", filename);
+ image = gdk_pixbuf_new_from_file (filename, NULL);
+ g_unlink (filename);
+ g_free (filename);
+ if (image == NULL)
+ break;
+
+ clipman_history_add_image (plugin->history, image, clipboard);
+ g_object_unref (image);
+ }
+
+ /* Load texts */
+ filename = g_strdup_printf ("%s/xfce4/clipman/textsrc", g_get_user_cache_dir ());
+ DBG ("Loading texts from cache file %s", filename);
+ keyfile = g_key_file_new ();
+ if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
+ {
+ texts = g_key_file_get_string_list (keyfile, "texts", "texts", NULL, NULL);
+ for (i = 0; texts != NULL && texts[i] != NULL; i++)
+ clipman_history_add_text (plugin->history, texts[i], clipboard);
+ g_unlink (filename);
+ }
+
+ g_key_file_free (keyfile);
+ g_strfreev (texts);
+ g_free (filename);
+
+ /* Set no current item */
+ clipman_history_set_item_to_restore (plugin->history, NULL);
+}
+
+static void
+panel_plugin_save (XfcePanelPlugin *panel_plugin,
+ MyPlugin *plugin)
+{
+ GSList *list, *l;
+ const ClipmanHistoryItem *item;
+ GKeyFile *keyfile;
+ const gchar **texts;
+ gchar *data;
+ gchar *filename;
+ gint n_texts, n_images;
+
+ /* Create initial directory */
+ filename = xfce_resource_save_location (XFCE_RESOURCE_CACHE, "xfce4/clipman/", TRUE);
+ g_free (filename);
+
+ /* Save the history */
+ list = clipman_history_get_list (plugin->history);
+ if (list != NULL)
+ {
+ texts = g_malloc0 (g_slist_length (list));
+ for (n_texts = n_images = 0, l = list; l != NULL; l = l->next)
+ {
+ item = l->data;
+
+ switch (item->type)
+ {
+ case CLIPMAN_HISTORY_TYPE_TEXT:
+ texts[n_texts++] = item->content.text;
+ break;
+
+ case CLIPMAN_HISTORY_TYPE_IMAGE:
+ filename = g_strdup_printf ("%s/xfce4/clipman/image%d", g_get_user_cache_dir (), n_images++);
+ if (!gdk_pixbuf_save (item->content.image, filename, "png", NULL, NULL))
+ g_debug ("Failed to save image to cache file %s", filename);
+ else
+ DBG ("Saved image to cache file %s", filename);
+ g_free (filename);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+ }
+
+ if (n_texts > 0)
+ {
+ filename = g_strdup_printf ("%s/xfce4/clipman/textsrc", g_get_user_cache_dir ());
+ keyfile = g_key_file_new ();
+ g_key_file_set_string_list (keyfile, "texts", "texts", texts, n_texts);
+ data = g_key_file_to_data (keyfile, NULL, NULL);
+ g_file_set_contents (filename, data, -1, NULL);
+ DBG ("Saved texts to cache file %s", filename);
+
+ g_key_file_free (keyfile);
+ g_free (data);
+ g_free (filename);
+ g_free (texts);
+ }
+
+ g_slist_free (list);
+ }
+}
+
+static void
panel_plugin_free (XfcePanelPlugin *panel_plugin,
MyPlugin *plugin)
{
More information about the Goodies-commits
mailing list