wmix: fixed some typo's.
[dockapps.git] / wmcliphist / clipboard.c
blob9b1b6ed66fc424a30aef4d81a836a0499602e270
1 #include "wmcliphist.h"
2 #include <time.h>
4 /* when true, clipboard will be automatically taken up by wmcliphist */
5 gint auto_take_up = 0;
7 /* number of items to keep (may be overriden from command line) */
8 gint num_items_to_keep = 10;
10 /* number if items kept */
11 gint num_items = 0;
13 /* current number of locked items */
14 gint locked_count;
16 /* list of clipboard history items */
17 GList *history_items = NULL;
19 /* selected item */
20 HISTORY_ITEM *selected = NULL;
25 * dumps history list to stderr
26 * for debugging purposes only
28 void
29 dump_history_list_fn(char *header)
31 gint indent = 1, i;
32 GList *node = history_items;
33 HISTORY_ITEM *data;
34 gchar *converted;
36 fprintf(stderr, "%s\n", header);
37 while (node) {
38 data = (HISTORY_ITEM *)node->data;
39 for (i = 0; i < indent; i++)
40 putc('-', stderr);
41 converted = from_utf8(data->content);
42 fprintf(stderr, " %s\n", converted);
43 g_free(converted);
44 indent++;
45 node = node->next;
47 fprintf(stderr, "==========\n");
50 void
51 my_get_selection_text(GtkClipboard *cb, const gchar *text, gpointer
52 data)
54 /* previous clipboard content */
55 static gchar *old_content = "";
56 static gint has_old_content = 0;
57 gchar *content;
58 gchar *converted;
60 begin_func("my_get_selection_text");
62 if (text == NULL) {
63 return_void();
66 if (g_utf8_collate(text, old_content) != 0 &&
67 !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_app_clip_ignore))) {
68 /* new data in clipboard */
69 /* store new content for future comparation */
70 content = g_strdup(text);
72 converted = from_utf8(content);
73 /* fprintf(stderr, ">>> %s\n", converted); */
74 g_free(converted);
76 if (has_old_content > 0)
77 g_free(old_content);
78 old_content = content;
79 has_old_content = 1;
81 /* process item */
82 process_item(content, 0, TRUE);
85 /* when clipboard is locked, set selection owener to myself */
86 if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_app_clip_ignore)) ||
87 gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_app_clip_lock))) {
88 if (gtk_selection_owner_set(dock_app,
89 clipboard,
90 GDK_CURRENT_TIME) == 0)
91 selected = NULL;
94 return_void();
99 * get clipboard content - partialy inspired by Downloader for X
101 gboolean
102 my_get_xselection(GtkWidget *window, GdkEvent *event)
105 begin_func("my_get_xselection");
107 gtk_clipboard_request_text(gtk_clipboard_get(clipboard),
108 my_get_selection_text, NULL);
110 return_val(TRUE);
115 * clipboard conversion - inspired by Downloader for X too :)
117 gboolean
118 time_conv_select()
120 begin_func("time_conv_select");
122 gtk_selection_convert(main_window,
123 clipboard,
124 GDK_TARGET_STRING,
125 GDK_CURRENT_TIME);
126 return_val(TRUE);
131 * handles request for selection from other apps
133 gboolean
134 selection_handle(GtkWidget *widget,
135 GtkSelectionData *selection_data,
136 guint info,
137 guint time_stamp,
138 gpointer data)
140 static gchar *converted = NULL;
142 begin_func("selection_handle");
144 if (converted != NULL) {
145 g_free(converted);
148 if (selected) {
149 converted = from_utf8(selected->content);
150 gtk_selection_data_set(selection_data,
151 GDK_SELECTION_TYPE_STRING,
153 (guchar *) converted,
154 strlen(converted));
155 } else {
156 gtk_selection_data_set(selection_data,
157 GDK_SELECTION_TYPE_STRING,
159 (guchar *)"",
163 return_val(TRUE);