59ab0b0c8233205aee068137966cac219d5a4cf3
[claws.git] / src / plugins / mathml_viewer / mathml_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 <gtkmathview/gtkmathview.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 typedef struct _MathMLViewer MathMLViewer;
38
39 struct _MathMLViewer
40 {
41         MimeViewer mimeviewer;
42         
43         GtkScrolledWindow       *scrollwin;;
44         GtkMathView             *mathview;
45         gchar                   *filename;
46 };
47
48 static MimeViewerFactory mathml_viewer_factory;
49
50 static GtkWidget *mathml_get_widget(MimeViewer *_viewer)
51 {
52         MathMLViewer *viewer = (MathMLViewer *) _viewer;
53
54         debug_print("mathml_get_widget\n");
55
56         return GTK_WIDGET(viewer->scrollwin);
57 }
58
59 static void mathml_show_mimepart(MimeViewer *_viewer, const gchar *infile, MimeInfo *partinfo)
60 {
61         MathMLViewer *viewer = (MathMLViewer *) _viewer;
62
63         debug_print("mathml_show_mimepart\n");
64
65         if (viewer->filename != NULL) {
66                 g_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, partinfo) < 0)) {
73                 gchar *uri;
74                 
75                 uri = g_strconcat("file://", viewer->filename, NULL);
76                 gtk_math_view_load_uri(GTK_MATH_VIEW(viewer->mathview), uri);
77                 g_free(uri);
78         }
79 }
80
81 static void mathml_clear_viewer(MimeViewer *_viewer)
82 {
83         MathMLViewer *viewer = (MathMLViewer *) _viewer;
84
85         debug_print("mathml_clear_viewer\n");
86
87         gtk_math_view_unload(viewer->mathview);
88 }
89
90 static void mathml_destroy_viewer(MimeViewer *_viewer)
91 {
92         MathMLViewer *viewer = (MathMLViewer *) _viewer;
93
94         debug_print("mathml_destroy_viewer\n");
95
96         gtk_widget_unref(GTK_WIDGET(viewer->scrollwin));
97         g_unlink(viewer->filename);
98         g_free(viewer->filename);
99         g_free(viewer);
100 }
101
102 static MimeViewer *mathml_viewer_create(void)
103 {
104         MathMLViewer *viewer;
105
106         debug_print("mathml_viewer_create\n");
107         
108         viewer = g_new0(MathMLViewer, 1);
109         viewer->mimeviewer.factory = &mathml_viewer_factory;
110
111         viewer->mimeviewer.get_widget = mathml_get_widget;
112         viewer->mimeviewer.show_mimepart = mathml_show_mimepart;
113         viewer->mimeviewer.clear_viewer = mathml_clear_viewer;
114         viewer->mimeviewer.destroy_viewer = mathml_destroy_viewer;      
115
116         viewer->scrollwin = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
117         gtk_widget_show(GTK_WIDGET(viewer->scrollwin));
118         gtk_widget_ref(GTK_WIDGET(viewer->scrollwin));
119         gtk_scrolled_window_set_policy(viewer->scrollwin, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
120
121         viewer->mathview = GTK_MATH_VIEW(gtk_math_view_new(NULL, NULL));
122         gtk_widget_show(GTK_WIDGET(viewer->mathview));
123         viewer->filename = NULL;
124         gtk_container_add(GTK_CONTAINER(viewer->scrollwin), GTK_WIDGET(viewer->mathview));
125
126         return (MimeViewer *) viewer;
127 }
128
129 static gchar *content_types[] =
130         {"text/mathml", NULL};
131
132 static MimeViewerFactory mathml_viewer_factory =
133 {
134         content_types,
135         0,
136         
137         mathml_viewer_create,
138 };
139
140 gint plugin_init(gchar **error)
141 {
142         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
143                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the MathML plugin was built with");
144                 return -1;
145         }
146
147         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
148                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the MathML plugin");
149                 return -1;
150         }
151
152         mimeview_register_viewer_factory(&mathml_viewer_factory);
153         return 0;       
154 }
155
156 void plugin_done(void)
157 {
158         mimeview_unregister_viewer_factory(&mathml_viewer_factory);
159 }
160
161 const gchar *plugin_name(void)
162 {
163         return _("MathML Viewer");
164 }
165
166 const gchar *plugin_desc(void)
167 {
168         return _("This plugin uses the GtkMathView widget to render "
169                  "MathML attachments (Content-Type: text/mathml)");
170 }
171
172 const gchar *plugin_type(void)
173 {
174         return "GTK2";
175 }