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