2013-02-20 [colin] 3.9.0cvs95
[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 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #  include "claws-features.h"
21 #endif
22
23 #ifdef NOTIFICATION_COMMAND
24
25 #include <string.h>
26 #include <glib.h>
27 #include "common/utils.h"
28 #include "folder.h"
29 #include "notification_command.h"
30 #include "notification_prefs.h"
31 #include "notification_foldercheck.h"
32
33 typedef struct {
34   gboolean blocked;
35   guint timeout_id;
36 } NotificationCommand;
37
38 static gboolean command_timeout_fun(gpointer data);
39
40 static NotificationCommand command;
41
42 G_LOCK_DEFINE_STATIC(command);
43
44 void notification_command_msg(MsgInfo *msginfo)
45 {
46   gchar *ret_str, *buf;
47   gsize by_read = 0, by_written = 0;
48   FolderType ftype;
49
50   if(!msginfo || !notify_config.command_enabled || !MSG_IS_NEW(msginfo->flags))
51     return;
52
53   if(!notify_config.command_enabled || !MSG_IS_NEW(msginfo->flags))
54     return;
55
56   if(notify_config.command_folder_specific) {
57     guint id;
58     GSList *list;
59     gchar *identifier;
60     gboolean found = FALSE;
61
62     if(!(msginfo->folder))
63       return;
64
65     identifier = folder_item_get_identifier(msginfo->folder);
66
67     id =
68       notification_register_folder_specific_list(COMMAND_SPECIFIC_FOLDER_ID_STR);
69     list = notification_foldercheck_get_list(id);
70     for(; (list != NULL) && !found; list = g_slist_next(list)) {
71       gchar *list_identifier;
72       FolderItem *list_item = (FolderItem*) list->data;
73       
74       list_identifier = folder_item_get_identifier(list_item);
75       if(!strcmp2(list_identifier, identifier))
76         found = TRUE;
77
78       g_free(list_identifier);
79     }
80     g_free(identifier);
81     
82     if(!found)
83       return;
84   } /* folder specific */
85
86   ftype = msginfo->folder->folder->klass->type;
87
88   buf = g_strdup(notify_config.command_line);
89
90   G_LOCK(command);
91
92   if(!command.blocked) {
93     /* execute command */
94     command.blocked = TRUE;
95     ret_str = g_locale_from_utf8(buf,strlen(buf),&by_read,&by_written,NULL);
96     if(ret_str && by_written) {
97       g_free(buf);
98       buf = ret_str;
99     }
100     execute_command_line(buf, TRUE);
101     g_free(buf);
102   }
103
104   /* block further execution for some time,
105      no matter if it was blocked or not */
106   if(command.timeout_id)
107     g_source_remove(command.timeout_id);
108   command.timeout_id = g_timeout_add(notify_config.command_timeout,
109                                      command_timeout_fun, NULL);    
110   G_UNLOCK(command);
111 }
112
113 static gboolean command_timeout_fun(gpointer data)
114 {
115   G_LOCK(command);
116   command.timeout_id = 0;
117   command.blocked = FALSE;
118   G_UNLOCK(command);
119   return FALSE;
120 }
121
122 #endif /* NOTIFICATION_COMMAND */