2005-12-02 [paul] 1.9.100cvs55
[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-2003 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->widget = gtk_event_box_new();
145
146         gtk_widget_show(viewer->widget);
147         gtk_widget_ref(viewer->widget);
148
149         viewer->filename = NULL;
150
151         return (MimeViewer *) viewer;
152 }
153
154 static gchar *content_types[] = 
155         {"text/html", NULL};
156
157 static MimeViewerFactory dillo_viewer_factory =
158 {
159         content_types,  
160         0,
161
162         dillo_viewer_create
163 };
164
165 gint plugin_init(gchar **error)
166 {
167         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
168                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the Dillo plugin was built with");
169                 return -1;
170         }
171
172         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
173                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the Dillo plugin");
174                 return -1;
175         }
176
177         dillo_prefs_init();
178
179         mimeview_register_viewer_factory(&dillo_viewer_factory);
180
181         return 0;       
182 }
183
184 void plugin_done(void)
185 {
186         mimeview_unregister_viewer_factory(&dillo_viewer_factory);
187
188         dillo_prefs_done();
189 }
190
191 const gchar *plugin_name(void)
192 {
193         return _("Dillo HTML Viewer");
194 }
195
196 const gchar *plugin_desc(void)
197 {
198         return _("This plugin renders HTML mail using the Dillo "
199                 "web browser.");
200 }
201
202 const gchar *plugin_type(void)
203 {
204         return "GTK2";
205 }