7ac7a508e1481a348eb73dab9b8780c23bae0271
[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-2007 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 #include <gdk/gdkx.h>
30
31 #include "common/claws.h"
32 #include "common/version.h"
33 #include "plugin.h"
34 #include "utils.h"
35 #include "mimeview.h"
36
37 #include "dillo_prefs.h"
38
39 #define PLUGIN_NAME (_("Dillo HTML Viewer"))
40
41 typedef struct _DilloViewer DilloViewer;
42
43 struct _DilloViewer
44 {
45         MimeViewer       mimeviewer;
46         GtkWidget       *widget;        
47         GtkWidget       *socket;
48         gchar           *filename;
49 };
50
51 static MimeViewerFactory dillo_viewer_factory;
52
53 static GtkWidget *dillo_get_widget(MimeViewer *_viewer)
54 {
55         DilloViewer *viewer = (DilloViewer *) _viewer;
56
57         debug_print("dillo_get_widget\n");
58
59         return GTK_WIDGET(viewer->widget);
60 }
61
62 static gboolean socket_destroy_cb(GtkObject *object, gpointer data)
63 {
64         DilloViewer *viewer = (DilloViewer *) data;
65         debug_print("Destroyed dillo socket %p\n", viewer->socket);
66         viewer->socket = NULL;
67         return FALSE;
68 }
69
70 static void dillo_show_mimepart(MimeViewer *_viewer,
71                                 const gchar *infile,
72                                 MimeInfo *partinfo)
73 {
74         DilloViewer *viewer = (DilloViewer *) _viewer;
75
76         debug_print("dillo_show_mimepart\n");
77
78         if (viewer->filename != NULL) {
79                 g_unlink(viewer->filename);
80                 g_free(viewer->filename);
81         }
82
83         viewer->filename = procmime_get_tmp_file_name(partinfo);
84         
85         if (!(procmime_get_part(viewer->filename, partinfo) < 0)) {
86                 gchar *cmd;
87
88                 if (viewer->socket)
89                         gtk_widget_destroy(viewer->socket);
90                 viewer->socket = gtk_socket_new();
91                 debug_print("Adding dillo socket %p", viewer->socket);
92                 gtk_container_add(GTK_CONTAINER(viewer->widget),
93                                   viewer->socket);
94                 gtk_widget_realize(viewer->socket);
95                 gtk_widget_show(viewer->socket);
96                 g_signal_connect(G_OBJECT(viewer->socket), "destroy", 
97                                  G_CALLBACK(socket_destroy_cb), viewer);
98
99                 cmd = g_strdup_printf("dillo %s%s-x %d \"%s\"",
100                                       (dillo_prefs.local ? "-l " : ""),
101                                       (dillo_prefs.full ? "-f " : ""),
102                                       (gint) GDK_WINDOW_XWINDOW(viewer->socket->window),
103                                       viewer->filename);
104
105                 execute_command_line(cmd, TRUE);
106                 g_free(cmd);
107         }
108 }
109
110 static void dillo_clear_viewer(MimeViewer *_viewer)
111 {
112         DilloViewer *viewer = (DilloViewer *) _viewer;
113
114         debug_print("dillo_clear_viewer\n");
115         debug_print("Removing dillo socket %p\n", viewer->socket);
116
117         if (viewer->socket) {
118                 gtk_widget_destroy(viewer->socket);
119         }
120 }
121
122 static void dillo_destroy_viewer(MimeViewer *_viewer)
123 {
124         DilloViewer *viewer = (DilloViewer *) _viewer;
125
126         debug_print("dillo_destroy_viewer\n");
127
128         gtk_widget_unref(GTK_WIDGET(viewer->widget));
129         g_unlink(viewer->filename);
130         g_free(viewer->filename);
131         g_free(viewer);
132 }
133
134 static MimeViewer *dillo_viewer_create(void)
135 {
136         DilloViewer *viewer;
137
138         debug_print("dillo_viewer_create\n");
139         
140         viewer = g_new0(DilloViewer, 1);
141         viewer->mimeviewer.factory = &dillo_viewer_factory;
142         viewer->mimeviewer.get_widget = dillo_get_widget;
143         viewer->mimeviewer.show_mimepart = dillo_show_mimepart;
144         viewer->mimeviewer.clear_viewer = dillo_clear_viewer;
145         viewer->mimeviewer.destroy_viewer = dillo_destroy_viewer;       
146         viewer->mimeviewer.get_selection = NULL;
147         viewer->widget = gtk_event_box_new();
148
149         gtk_widget_show(viewer->widget);
150         gtk_widget_ref(viewer->widget);
151
152         viewer->filename = NULL;
153
154         return (MimeViewer *) viewer;
155 }
156
157 static gchar *content_types[] = 
158         {"text/html", NULL};
159
160 static MimeViewerFactory dillo_viewer_factory =
161 {
162         content_types,  
163         0,
164
165         dillo_viewer_create,
166 };
167
168 gint plugin_init(gchar **error)
169 {
170         gchar *dillo_path = NULL;
171         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
172                                 VERSION_NUMERIC, PLUGIN_NAME, error))
173                 return -1;
174
175         if ((dillo_path = g_find_program_in_path("dillo")) == NULL) {
176                 *error = g_strdup(_("Can't find the dillo executable in PATH. Is it installed?"));
177                 return -1;
178         }
179         g_free(dillo_path);
180
181         dillo_prefs_init();
182
183         mimeview_register_viewer_factory(&dillo_viewer_factory);
184
185         return 0;       
186 }
187
188 gboolean plugin_done(void)
189 {
190         mimeview_unregister_viewer_factory(&dillo_viewer_factory);
191
192         dillo_prefs_done();
193         return TRUE;
194 }
195
196 const gchar *plugin_name(void)
197 {
198         return PLUGIN_NAME;
199 }
200
201 const gchar *plugin_desc(void)
202 {
203         return _("This plugin renders HTML mail using the Dillo "
204                 "web browser.\n"
205                 "\n"
206                 "Options can be found in /Configuration/Preferences/Plugins/Dillo Browser");
207 }
208
209 const gchar *plugin_type(void)
210 {
211         return "GTK2";
212 }
213
214 const gchar *plugin_licence(void)
215 {
216         return "GPL3+";
217 }
218
219 const gchar *plugin_version(void)
220 {
221         return VERSION;
222 }
223
224 struct PluginFeature *plugin_provides(void)
225 {
226         static struct PluginFeature features[] = 
227                 { {PLUGIN_MIMEVIEWER, N_("text/html")},
228                   {PLUGIN_NOTHING, NULL}};
229         return features;
230 }