2006-08-23 [colin] 2.4.0cvs78
[claws.git] / src / plugins / dillo_viewer / dillo_viewer.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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/sylpheed.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 typedef struct _DilloViewer DilloViewer;
40
41 struct _DilloViewer
42 {
43         MimeViewer       mimeviewer;
44         GtkWidget       *widget;        
45         GtkWidget       *socket;
46         gchar           *filename;
47 };
48
49 static MimeViewerFactory dillo_viewer_factory;
50
51 static GtkWidget *dillo_get_widget(MimeViewer *_viewer)
52 {
53         DilloViewer *viewer = (DilloViewer *) _viewer;
54
55         debug_print("dillo_get_widget\n");
56
57         return GTK_WIDGET(viewer->widget);
58 }
59
60 static gboolean socket_destroy_cb(GtkObject *object, gpointer data)
61 {
62         DilloViewer *viewer = (DilloViewer *) data;
63         debug_print("Destroyed dillo socket %p\n", viewer->socket);
64         viewer->socket = NULL;
65         return FALSE;
66 }
67
68 static void dillo_show_mimepart(MimeViewer *_viewer,
69                                 const gchar *infile,
70                                 MimeInfo *partinfo)
71 {
72         DilloViewer *viewer = (DilloViewer *) _viewer;
73
74         debug_print("dillo_show_mimepart\n");
75
76         if (viewer->filename != NULL) {
77                 g_unlink(viewer->filename);
78                 g_free(viewer->filename);
79         }
80
81         viewer->filename = procmime_get_tmp_file_name(partinfo);
82         
83         if (!(procmime_get_part(viewer->filename, partinfo) < 0)) {
84                 gchar *cmd;
85
86                 if (viewer->socket)
87                         gtk_widget_destroy(viewer->socket);
88                 viewer->socket = gtk_socket_new();
89                 debug_print("Adding dillo socket %p", viewer->socket);
90                 gtk_container_add(GTK_CONTAINER(viewer->widget),
91                                   viewer->socket);
92                 gtk_widget_realize(viewer->socket);
93                 gtk_widget_show(viewer->socket);
94                 g_signal_connect(G_OBJECT(viewer->socket), "destroy", 
95                                  G_CALLBACK(socket_destroy_cb), viewer);
96
97                 cmd = g_strdup_printf("dillo %s%s-x %d \"%s\"",
98                                       (dillo_prefs.local ? "-l " : ""),
99                                       (dillo_prefs.full ? "-f " : ""),
100                                       (gint) GDK_WINDOW_XWINDOW(viewer->socket->window),
101                                       viewer->filename);
102
103                 execute_command_line(cmd, TRUE);
104                 g_free(cmd);
105         }
106 }
107
108 static void dillo_clear_viewer(MimeViewer *_viewer)
109 {
110         DilloViewer *viewer = (DilloViewer *) _viewer;
111
112         debug_print("dillo_clear_viewer\n");
113         debug_print("Removing dillo socket %p\n", viewer->socket);
114
115         if (viewer->socket) {
116                 gtk_widget_destroy(viewer->socket);
117         }
118 }
119
120 static void dillo_destroy_viewer(MimeViewer *_viewer)
121 {
122         DilloViewer *viewer = (DilloViewer *) _viewer;
123
124         debug_print("dillo_destroy_viewer\n");
125
126         gtk_widget_unref(GTK_WIDGET(viewer->widget));
127         g_unlink(viewer->filename);
128         g_free(viewer->filename);
129         g_free(viewer);
130 }
131
132 static MimeViewer *dillo_viewer_create(void)
133 {
134         DilloViewer *viewer;
135
136         debug_print("dillo_viewer_create\n");
137         
138         viewer = g_new0(DilloViewer, 1);
139         viewer->mimeviewer.factory = &dillo_viewer_factory;
140         viewer->mimeviewer.get_widget = dillo_get_widget;
141         viewer->mimeviewer.show_mimepart = dillo_show_mimepart;
142         viewer->mimeviewer.clear_viewer = dillo_clear_viewer;
143         viewer->mimeviewer.destroy_viewer = dillo_destroy_viewer;       
144         viewer->mimeviewer.get_selection = NULL;
145         viewer->widget = gtk_event_box_new();
146
147         gtk_widget_show(viewer->widget);
148         gtk_widget_ref(viewer->widget);
149
150         viewer->filename = NULL;
151
152         return (MimeViewer *) viewer;
153 }
154
155 static gchar *content_types[] = 
156         {"text/html", NULL};
157
158 static MimeViewerFactory dillo_viewer_factory =
159 {
160         content_types,  
161         0,
162
163         dillo_viewer_create,
164 };
165
166 gint plugin_init(gchar **error)
167 {
168         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
169                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the Dillo plugin was built with");
170                 return -1;
171         }
172
173         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
174                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the Dillo plugin");
175                 return -1;
176         }
177
178         dillo_prefs_init();
179
180         mimeview_register_viewer_factory(&dillo_viewer_factory);
181
182         return 0;       
183 }
184
185 void plugin_done(void)
186 {
187         mimeview_unregister_viewer_factory(&dillo_viewer_factory);
188
189         dillo_prefs_done();
190 }
191
192 const gchar *plugin_name(void)
193 {
194         return _("Dillo HTML Viewer");
195 }
196
197 const gchar *plugin_desc(void)
198 {
199         return _("This plugin renders HTML mail using the Dillo "
200                 "web browser.\n"
201                 "\n"
202                 "Options can be found in /Configuration/Preferences/Plugins/Dillo Browser");
203 }
204
205 const gchar *plugin_type(void)
206 {
207         return "GTK2";
208 }
209
210 const gchar *plugin_licence(void)
211 {
212         return "GPL";
213 }
214
215 const gchar *plugin_version(void)
216 {
217         return VERSION;
218 }