1 #include "wmcliphist.h"
4 int autosave_period
= 120;
6 int exec_immediately
= 1;
9 * process new history item
12 process_item(char *content
, gint locked
, gboolean exec
)
16 gboolean processed
= FALSE
;
17 HISTORY_ITEM
*hist_item
;
19 begin_func("process_item");
21 list_node
= g_list_first(action_list
);
24 action
= (ACTION
*)list_node
->data
;
26 /* check if some action is requested */
27 if (regexec(&action
->expression
, content
, 0, NULL
, 0) != 0) {
28 list_node
= g_list_next(list_node
);
32 /* match - execute requested action */
34 if (action
->action
== ACT_IGNORE
) {
38 if (action
->action
== ACT_EXEC
&& exec_immediately
== TRUE
40 exec_item(content
, action
);
42 if (action
->action
== ACT_SUBMENU
) {
43 /* test if such item already exists in this menu */
46 /* add item to menu and item list */
47 hist_item
= menu_item_add(content
, locked
,
50 /* when auto_take_up is true, set selection owner to myself */
51 if (auto_take_up
== 1) {
53 if (gtk_selection_owner_set(dock_app
,
55 GDK_CURRENT_TIME
) == 0) {
60 dump_history_list("added item");
64 list_node
= g_list_next(list_node
);
67 if (processed
== FALSE
) {
68 hist_item
= menu_item_add(content
, locked
, menu_hist
);
70 /* when auto_take_up is true, set selection owner to myself */
71 if (auto_take_up
== 1) {
73 if (gtk_selection_owner_set(dock_app
,
75 GDK_CURRENT_TIME
) == 0) {
85 move_item_to_begin(HISTORY_ITEM
*item
) {
88 begin_func("menu_item_activated");
90 if (!(list_node
= g_list_find(history_items
, item
))) {
91 g_assert((list_node
!= NULL
));
94 gtk_menu_popdown(GTK_MENU(menu_hist
));
95 /* move previously stored item to beginning */
96 gtk_menu_reorder_child(GTK_MENU(item
->menu
),
98 history_items
= g_list_remove_link(history_items
, list_node
);
99 history_items
= g_list_concat(list_node
, history_items
);
101 if (gtk_selection_owner_set(dock_app
,
103 GDK_CURRENT_TIME
) == 0)
109 * Exec's an action on item.
112 exec_item(char *content
, ACTION
*action
)
114 int msg_result
= 0, res
;
119 converted
= from_utf8(content
);
121 /* If we're not given an action to perform, find the first matching
122 * exec action, and perform it */
126 list_node
= g_list_first(action_list
);
128 a
= (ACTION
*)list_node
->data
;
129 /* check if some action is requested */
130 if ((regexec(&a
->expression
, converted
, 0, NULL
, 0)
132 && (a
->action
== ACT_EXEC
)) {
136 list_node
= g_list_next(list_node
);
140 if (!action
|| action
->action
!= ACT_EXEC
) {
145 exec_buf
= g_new0(char, strlen(converted
) +
146 strlen(action
->command
) + 1);
147 sprintf(exec_buf
, action
->command
, converted
);
149 msg_buf
= g_new0(char, strlen(exec_buf
) + 256);
150 sprintf(msg_buf
, "Do you want to perform the "
151 "following action?\n\n%s",
153 msg_result
= show_message(msg_buf
,
154 "wmcliphist", "Yes", "No", NULL
);
158 /* create child and exec command */
159 if (msg_result
== 0 && fork() == 0) {
161 res
= system(exec_buf
);
163 fprintf(stderr
, "Cannot exec '%s'\n", exec_buf
);
165 fprintf(stderr
, "/bin/sh not found\n");
177 * loads history from file
180 history_load(gboolean dump_only
)
190 begin_func("history_load");
192 fname
= rcconfig_get_name(".data");
193 if (!(f
= fopen(fname
, "r"))) {
198 if (fread(&ver
, sizeof(gint
), 1, f
) != 1) {
203 /* delete old history file */
206 if (remove(rcconfig_get_name(".data"))) {
214 printf("<history>\n");
218 if (fread(&len
, sizeof(gint
), 1, f
) != 1)
221 if (num_items
== num_items_to_keep
&& !dump_only
) {
222 tmp_errno
= E_TOO_MUCH
;
226 buf
= g_new0(gchar
, len
+ 1);
227 if (fread(buf
, len
, 1, f
) != 1) {
229 tmp_errno
= E_INVALID
;
234 if (fread(&locked
, sizeof(gint
), 1, f
) != 1) {
236 tmp_errno
= E_INVALID
;
241 printf("<item>%s</item>\n", buf
);
243 process_item(buf
, locked
, FALSE
);
251 printf("</history>\n");
253 dump_history_list("load_history()");
266 * store history to file
272 gint version
= VERSION
;
274 HISTORY_ITEM
*hist_item
;
278 begin_func("history_save");
280 fname
= g_strdup(rcconfig_get_name(".data.tmp"));
282 if (!(f
= fopen(fname
, "w"))) {
289 if ((chmod(fname
, S_IRUSR
|S_IWUSR
)) != 0) {
298 if (fwrite(&version
, sizeof(gint
), 1, f
) != 1) {
299 perror("fwrite version");
307 list_node
= g_list_last(history_items
);
310 hist_item
= (HISTORY_ITEM
*)list_node
->data
;
311 length
= strlen(hist_item
->content
);
312 if (fwrite(&length
, sizeof(gint
), 1, f
) != 1) {
316 if (fwrite(hist_item
->content
, length
, 1, f
) != 1) {
320 if (fwrite(&hist_item
->locked
, sizeof(gint
), 1, f
) != 1) {
324 list_node
= g_list_previous(list_node
);
330 if (rename(fname
, rcconfig_get_name(".data")) != 0) {
355 HISTORY_ITEM
*hist_item
;
358 begin_func("history_free");
360 list_node
= g_list_last(history_items
);
362 hist_item
= (HISTORY_ITEM
*)list_node
->data
;
363 gtk_container_remove(GTK_CONTAINER(hist_item
->menu
),
364 hist_item
->menu_item
);
365 gtk_widget_destroy(hist_item
->menu_item
);
366 g_free(hist_item
->content
);
368 list_node
= g_list_previous(list_node
);
370 g_list_free(history_items
);
377 * autosave timer function
382 begin_func("history_autosave");