e7979501fd4e36ee9076d20d27f16ddf2419bfb3
[claws.git] / src / plugins / notification / notification_command.c
1 /* Notification plugin for Claws-Mail
2  * Copyright (C) 2005-2007 Holger Berndt
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "pluginconfig.h"
19
20 #ifdef NOTIFICATION_COMMAND
21
22 #include <string.h>
23 #include <glib.h>
24 #include "common/utils.h"
25 #include "folder.h"
26 #include "notification_command.h"
27 #include "notification_prefs.h"
28 #include "notification_foldercheck.h"
29
30 typedef struct {
31   gboolean blocked;
32   guint timeout_id;
33 } NotificationCommand;
34
35 static gboolean command_timeout_fun(gpointer data);
36
37 static NotificationCommand command;
38
39 G_LOCK_DEFINE_STATIC(command);
40
41 void notification_command_msg(MsgInfo *msginfo)
42 {
43   gchar *ret_str, *buf;
44   gsize by_read = 0, by_written = 0;
45   FolderType ftype;
46
47   if(!msginfo || !notify_config.command_enabled || !MSG_IS_NEW(msginfo->flags))
48     return;
49
50   if(!notify_config.command_enabled || !MSG_IS_NEW(msginfo->flags))
51     return;
52
53   if(notify_config.command_folder_specific) {
54     guint id;
55     GSList *list;
56     gchar *identifier;
57     gboolean found = FALSE;
58
59     if(!(msginfo->folder))
60       return;
61
62     identifier = folder_item_get_identifier(msginfo->folder);
63
64     id =
65       notification_register_folder_specific_list(COMMAND_SPECIFIC_FOLDER_ID_STR);
66     list = notification_foldercheck_get_list(id);
67     for(; (list != NULL) && !found; list = g_slist_next(list)) {
68       gchar *list_identifier;
69       FolderItem *list_item = (FolderItem*) list->data;
70       
71       list_identifier = folder_item_get_identifier(list_item);
72       if(!strcmp2(list_identifier, identifier))
73         found = TRUE;
74
75       g_free(list_identifier);
76     }
77     g_free(identifier);
78     
79     if(!found)
80       return;
81   } /* folder specific */
82
83   ftype = msginfo->folder->folder->klass->type;
84
85   buf = g_strdup(notify_config.command_line);
86
87   G_LOCK(command);
88
89   if(!command.blocked) {
90     /* execute command */
91     command.blocked = TRUE;
92     ret_str = g_locale_from_utf8(buf,strlen(buf),&by_read,&by_written,NULL);
93     if(ret_str && by_written) {
94       g_free(buf);
95       buf = ret_str;
96     }
97     execute_command_line(buf, TRUE);
98     g_free(buf);
99   }
100
101   /* block further execution for some time,
102      no matter if it was blocked or not */
103   if(command.timeout_id)
104     g_source_remove(command.timeout_id);
105   command.timeout_id = g_timeout_add(notify_config.command_timeout,
106                                      command_timeout_fun, NULL);    
107   G_UNLOCK(command);
108 }
109
110 static gboolean command_timeout_fun(gpointer data)
111 {
112   G_LOCK(command);
113   command.timeout_id = 0;
114   command.blocked = FALSE;
115   G_UNLOCK(command);
116   return FALSE;
117 }
118
119 #endif /* NOTIFICATION_COMMAND */