2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / plugins / dillo_viewer / dillo_viewer.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <unistd.h>
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #if GTK_CHECK_VERSION(3, 0, 0)
30 #include <gtk/gtkx.h>
31 #endif
32 #include <gdk/gdkx.h>
33
34 #include "common/claws.h"
35 #include "common/version.h"
36 #include "plugin.h"
37 #include "utils.h"
38 #include "mimeview.h"
39 #include "addr_compl.h"
40
41 #include "dillo_prefs.h"
42
43 #define PLUGIN_NAME (_("Dillo HTML Viewer"))
44
45 typedef struct _DilloViewer DilloViewer;
46
47 struct _DilloViewer
48 {
49         MimeViewer       mimeviewer;
50         GtkWidget       *widget;        
51         GtkWidget       *socket;
52         gchar           *filename;
53 };
54
55 static MimeViewerFactory dillo_viewer_factory;
56
57 static GtkWidget *dillo_get_widget(MimeViewer *_viewer)
58 {
59         DilloViewer *viewer = (DilloViewer *) _viewer;
60
61         debug_print("dillo_get_widget\n");
62
63         return GTK_WIDGET(viewer->widget);
64 }
65
66 #if !GTK_CHECK_VERSION(3, 0, 0)
67 static gboolean socket_destroy_cb(GtkObject *object, gpointer data)
68 #else
69 static gboolean socket_destroy_cb(GtkObject *object, gpointer data)
70 #endif
71 {
72         DilloViewer *viewer = (DilloViewer *) data;
73         debug_print("Destroyed dillo socket %p\n", viewer->socket);
74         viewer->socket = NULL;
75         return FALSE;
76 }
77
78 static gboolean found_in_addressbook(const gchar *address)
79 {
80         gchar *addr = NULL;
81         gboolean found = FALSE;
82         gint num_addr = 0;
83         
84         if (!address)
85                 return FALSE;
86         
87         addr = g_strdup(address);
88         extract_address(addr);
89         num_addr = complete_address(addr);
90         if (num_addr > 1) {
91                 /* skip first item (this is the search string itself) */
92                 int i = 1;
93                 for (; i < num_addr && !found; i++) {
94                         gchar *caddr = get_complete_address(i);
95                         extract_address(caddr);
96                         if (strcasecmp(caddr, addr) == 0)
97                                 found = TRUE;
98                         g_free(caddr);
99                 }
100         }
101         g_free(addr);
102         return found;
103 }
104
105 static gboolean load_images(DilloViewer *viewer)
106 {
107         MessageView *messageview = ((MimeViewer *)viewer)->mimeview 
108                                         ? ((MimeViewer *)viewer)->mimeview->messageview 
109                                         : NULL;
110         MsgInfo *msginfo = NULL;
111         gchar *ab_folderpath = NULL;
112
113         if (messageview == NULL)
114                 return FALSE;
115         
116         msginfo = messageview->msginfo;
117         
118         if (msginfo == NULL)
119                 return FALSE;
120
121         /* don't load remote images, period. */
122         if (dillo_prefs.local)
123                 return FALSE;
124         
125         /* don't do whitelisting -> load images */
126         if (!dillo_prefs.whitelist_ab)
127                 return TRUE;
128
129         if (*dillo_prefs.whitelist_ab_folder != '\0' &&
130             strcasecmp(dillo_prefs.whitelist_ab_folder, _("Any")) != 0)
131                 ab_folderpath = dillo_prefs.whitelist_ab_folder;
132
133         start_address_completion(ab_folderpath);
134
135         /* do whitelisting -> check sender */
136         if (found_in_addressbook(msginfo->from)) {
137                 end_address_completion();
138                 return TRUE;
139         }
140         
141         end_address_completion();
142         return FALSE;
143 }
144
145 static void dillo_show_mimepart(MimeViewer *_viewer,
146                                 const gchar *infile,
147                                 MimeInfo *partinfo)
148 {
149         DilloViewer *viewer = (DilloViewer *) _viewer;
150
151         debug_print("dillo_show_mimepart\n");
152
153         if (viewer->filename != NULL) {
154                 claws_unlink(viewer->filename);
155                 g_free(viewer->filename);
156         }
157
158         viewer->filename = procmime_get_tmp_file_name(partinfo);
159         
160         if (!(procmime_get_part(viewer->filename, partinfo) < 0)) {
161                 gchar *cmd;
162
163                 if (viewer->socket)
164                         gtk_widget_destroy(viewer->socket);
165                 viewer->socket = gtk_socket_new();
166                 debug_print("Adding dillo socket %p", viewer->socket);
167                 gtk_container_add(GTK_CONTAINER(viewer->widget),
168                                   viewer->socket);
169                 gtk_widget_realize(viewer->socket);
170                 gtk_widget_show(viewer->socket);
171                 g_signal_connect(G_OBJECT(viewer->socket), "destroy", 
172                                  G_CALLBACK(socket_destroy_cb), viewer);
173
174                 cmd = g_strdup_printf("dillo %s%s-x %d \"%s\"",
175                                       (!load_images(viewer) ? "-l " : ""),
176                                       (dillo_prefs.full ? "-f " : ""),
177                                       (gint) GDK_WINDOW_XWINDOW(viewer->socket->window),
178                                       viewer->filename);
179
180                 execute_command_line(cmd, TRUE);
181                 g_free(cmd);
182         }
183 }
184
185 static void dillo_clear_viewer(MimeViewer *_viewer)
186 {
187         DilloViewer *viewer = (DilloViewer *) _viewer;
188
189         debug_print("dillo_clear_viewer\n");
190         debug_print("Removing dillo socket %p\n", viewer->socket);
191
192         if (viewer->socket) {
193                 gtk_widget_destroy(viewer->socket);
194         }
195 }
196
197 static void dillo_destroy_viewer(MimeViewer *_viewer)
198 {
199         DilloViewer *viewer = (DilloViewer *) _viewer;
200
201         debug_print("dillo_destroy_viewer\n");
202
203         if (viewer->socket) {
204                 gtk_widget_destroy(viewer->socket);
205         }
206
207         g_object_unref(GTK_WIDGET(viewer->widget));
208         claws_unlink(viewer->filename);
209         g_free(viewer->filename);
210         g_free(viewer);
211 }
212
213 static MimeViewer *dillo_viewer_create(void)
214 {
215         DilloViewer *viewer;
216
217         debug_print("dillo_viewer_create\n");
218         
219         viewer = g_new0(DilloViewer, 1);
220         viewer->mimeviewer.factory = &dillo_viewer_factory;
221         viewer->mimeviewer.get_widget = dillo_get_widget;
222         viewer->mimeviewer.show_mimepart = dillo_show_mimepart;
223         viewer->mimeviewer.clear_viewer = dillo_clear_viewer;
224         viewer->mimeviewer.destroy_viewer = dillo_destroy_viewer;       
225         viewer->mimeviewer.get_selection = NULL;
226         viewer->widget = gtk_event_box_new();
227
228         gtk_widget_show(viewer->widget);
229         g_object_ref(viewer->widget);
230
231         viewer->filename = NULL;
232
233         return (MimeViewer *) viewer;
234 }
235
236 static gchar *content_types[] = 
237         {"text/html", NULL};
238
239 static MimeViewerFactory dillo_viewer_factory =
240 {
241         content_types,  
242         0,
243
244         dillo_viewer_create,
245 };
246
247 gint plugin_init(gchar **error)
248 {
249         gchar *dillo_path = NULL;
250         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
251                                 VERSION_NUMERIC, PLUGIN_NAME, error))
252                 return -1;
253
254         if ((dillo_path = g_find_program_in_path("dillo")) == NULL) {
255                 *error = g_strdup(_("Can't find the dillo executable in PATH. Is it installed?"));
256                 return -1;
257         }
258         g_free(dillo_path);
259
260         dillo_prefs_init();
261
262         mimeview_register_viewer_factory(&dillo_viewer_factory);
263
264         return 0;       
265 }
266
267 gboolean plugin_done(void)
268 {
269         mimeview_unregister_viewer_factory(&dillo_viewer_factory);
270
271         dillo_prefs_done();
272         return TRUE;
273 }
274
275 const gchar *plugin_name(void)
276 {
277         return PLUGIN_NAME;
278 }
279
280 const gchar *plugin_desc(void)
281 {
282         return _("This plugin renders HTML mail using the Dillo "
283                 "web browser.\n"
284                 "\n"
285                 "Options can be found in /Configuration/Preferences/Plugins/Dillo Browser");
286 }
287
288 const gchar *plugin_type(void)
289 {
290         return "GTK2";
291 }
292
293 const gchar *plugin_licence(void)
294 {
295         return "GPL3+";
296 }
297
298 const gchar *plugin_version(void)
299 {
300         return VERSION;
301 }
302
303 struct PluginFeature *plugin_provides(void)
304 {
305         static struct PluginFeature features[] = 
306                 { {PLUGIN_MIMEVIEWER, "text/html"},
307                   {PLUGIN_NOTHING, NULL}};
308         return features;
309 }