slight change to description text
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <unistd.h>
21
22 #include <glib.h>
23 #include <gtk/gtk.h>
24 #include <gdk/gdkx.h>
25
26 #include "common/plugin.h"
27 #include "common/utils.h"
28 #include "mimeview.h"
29
30 typedef struct _dilloViewer dilloViewer;
31
32 struct _dilloViewer
33 {
34         MimeViewer       mimeviewer;
35         GtkWidget       *widget;        
36         GtkWidget       *socket;
37         gchar           *filename;
38 };
39
40 static MimeViewerFactory dillo_viewer_factory;
41
42 static GtkWidget *dillo_get_widget(MimeViewer *_viewer)
43 {
44         dilloViewer *viewer = (dilloViewer *) _viewer;
45
46         debug_print("dillo_get_widget\n");
47
48         return GTK_WIDGET(viewer->widget);
49 }
50
51 static gboolean socket_destroy_cb(GtkObject *object, gpointer data)
52 {
53         dilloViewer *viewer = (dilloViewer *) data;
54         debug_print("Destroyed dillo socket %p\n", viewer->socket);
55         viewer->socket = NULL;
56         return FALSE;
57 }
58
59 static void dillo_show_mimepart(MimeViewer *_viewer, const gchar *infile, MimeInfo *partinfo)
60 {
61         dilloViewer *viewer = (dilloViewer *) _viewer;
62
63         debug_print("dillo_show_mimepart\n");
64
65         if (viewer->filename != NULL) {
66                 unlink(viewer->filename);
67                 g_free(viewer->filename);
68         }
69
70         viewer->filename = procmime_get_tmp_file_name(partinfo);
71         
72         if (!(procmime_get_part(viewer->filename, infile, partinfo) < 0)) {
73                 gchar *cmd;
74                 if (viewer->socket)
75                         gtk_widget_destroy(viewer->socket);
76                 viewer->socket = gtk_socket_new();
77                 debug_print("Adding dillo socket %p", viewer->socket);
78                 gtk_container_add(GTK_CONTAINER(viewer->widget),
79                                   viewer->socket);
80                 gtk_widget_realize(viewer->socket);
81                 gtk_widget_show(viewer->socket);
82                 gtk_signal_connect(GTK_OBJECT(viewer->socket), 
83                                    "destroy", 
84                                    GTK_SIGNAL_FUNC(socket_destroy_cb),
85                                    viewer);
86                 cmd = g_strdup_printf("dillo -f -l -x %d \"%s\"", 
87                                 (gint) GDK_WINDOW_XWINDOW(viewer->socket->window),
88                                 viewer->filename);
89                 execute_command_line(cmd, TRUE);
90                 g_free(cmd);
91         }
92 }
93
94 static void dillo_clear_viewer(MimeViewer *_viewer)
95 {
96         dilloViewer *viewer = (dilloViewer *) _viewer;
97         debug_print("dillo_clear_viewer\n");
98         debug_print("Removing dillo socket %p\n", viewer->socket);
99         if (viewer->socket) {
100                 gtk_widget_destroy(viewer->socket);
101         }
102                 
103 }
104
105 static void dillo_destroy_viewer(MimeViewer *_viewer)
106 {
107         dilloViewer *viewer = (dilloViewer *) _viewer;
108
109         debug_print("dillo_destroy_viewer\n");
110
111         gtk_widget_unref(GTK_WIDGET(viewer->widget));
112         unlink(viewer->filename);
113         g_free(viewer->filename);
114         g_free(viewer);
115 }
116
117 static MimeViewer *dillo_viewer_create()
118 {
119         dilloViewer *viewer;
120
121         debug_print("dillo_viewer_create\n");
122         
123         viewer = g_new0(dilloViewer, 1);
124         viewer->mimeviewer.factory = &dillo_viewer_factory;
125
126         viewer->mimeviewer.get_widget = dillo_get_widget;
127         viewer->mimeviewer.show_mimepart = dillo_show_mimepart;
128         viewer->mimeviewer.clear_viewer = dillo_clear_viewer;
129         viewer->mimeviewer.destroy_viewer = dillo_destroy_viewer;       
130
131         viewer->widget = gtk_event_box_new();
132         gtk_widget_show(viewer->widget);
133         gtk_widget_ref(viewer->widget);
134
135         viewer->filename = NULL;
136
137         return (MimeViewer *) viewer;
138 }
139
140 static MimeViewerFactory dillo_viewer_factory =
141 {
142         "text/html",
143         0,
144         
145         dillo_viewer_create,
146 };
147
148 gint plugin_init(gchar **error)
149 {
150         mimeview_register_viewer_factory(&dillo_viewer_factory);
151         return 0;       
152 }
153
154 void plugin_done()
155 {
156         mimeview_unregister_viewer_factory(&dillo_viewer_factory);
157 }
158
159 const gchar *plugin_name()
160 {
161         return "Dillo HTML Viewer";
162 }
163
164 const gchar *plugin_desc()
165 {
166         return "This plugin renders HTML mail using the Dillo "
167                 "web browser.";
168 }