acd22262e3c509ac62dcb46c0decabfda8a5b61c
[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., 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 <gtkmathview/gtkmathview.h>
25
26 #include "common/plugin.h"
27 #include "common/utils.h"
28 #include "mimeview.h"
29
30 typedef struct _MathMLViewer MathMLViewer;
31
32 struct _MathMLViewer
33 {
34         MimeViewer mimeviewer;
35         
36         GtkScrolledWindow       *scrollwin;;
37         GtkMathView             *mathview;
38         gchar                   *filename;
39 };
40
41 static MimeViewerFactory mathml_viewer_factory;
42
43 static GtkWidget *mathml_get_widget(MimeViewer *_viewer)
44 {
45         MathMLViewer *viewer = (MathMLViewer *) _viewer;
46
47         debug_print("mathml_get_widget\n");
48
49         return GTK_WIDGET(viewer->scrollwin);
50 }
51
52 static void mathml_show_mimepart(MimeViewer *_viewer, const gchar *infile, MimeInfo *partinfo)
53 {
54         MathMLViewer *viewer = (MathMLViewer *) _viewer;
55
56         debug_print("mathml_show_mimepart\n");
57
58         if (viewer->filename != NULL) {
59                 unlink(viewer->filename);
60                 g_free(viewer->filename);
61         }
62
63         viewer->filename = procmime_get_tmp_file_name(partinfo);
64         
65         if (!(procmime_get_part(viewer->filename, infile, partinfo) < 0)) {
66                 gchar *uri;
67                 
68                 uri = g_strconcat("file://", viewer->filename, NULL);
69                 gtk_math_view_load_uri(GTK_MATH_VIEW(viewer->mathview), uri);
70                 g_free(uri);
71         }
72 }
73
74 static void mathml_clear_viewer(MimeViewer *_viewer)
75 {
76         MathMLViewer *viewer = (MathMLViewer *) _viewer;
77
78         debug_print("mathml_clear_viewer\n");
79
80         gtk_math_view_unload(viewer->mathview);
81 }
82
83 static void mathml_destroy_viewer(MimeViewer *_viewer)
84 {
85         MathMLViewer *viewer = (MathMLViewer *) _viewer;
86
87         debug_print("mathml_destroy_viewer\n");
88
89         gtk_widget_unref(GTK_WIDGET(viewer->scrollwin));
90         unlink(viewer->filename);
91         g_free(viewer->filename);
92         g_free(viewer);
93 }
94
95 static MimeViewer *mathml_viewer_create()
96 {
97         MathMLViewer *viewer;
98
99         debug_print("mathml_viewer_create\n");
100         
101         viewer = g_new0(MathMLViewer, 1);
102         viewer->mimeviewer.factory = &mathml_viewer_factory;
103
104         viewer->mimeviewer.get_widget = mathml_get_widget;
105         viewer->mimeviewer.show_mimepart = mathml_show_mimepart;
106         viewer->mimeviewer.clear_viewer = mathml_clear_viewer;
107         viewer->mimeviewer.destroy_viewer = mathml_destroy_viewer;      
108
109         viewer->scrollwin = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
110         gtk_widget_show(GTK_WIDGET(viewer->scrollwin));
111         gtk_widget_ref(GTK_WIDGET(viewer->scrollwin));
112         gtk_scrolled_window_set_policy(viewer->scrollwin, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
113
114         viewer->mathview = GTK_MATH_VIEW(gtk_math_view_new(NULL, NULL));
115         gtk_widget_show(GTK_WIDGET(viewer->mathview));
116         viewer->filename = NULL;
117         gtk_container_add(GTK_CONTAINER(viewer->scrollwin), GTK_WIDGET(viewer->mathview));
118
119         return (MimeViewer *) viewer;
120 }
121
122 static MimeViewerFactory mathml_viewer_factory =
123 {
124         "text/mathml",
125         0,
126         
127         mathml_viewer_create,
128 };
129
130 gint plugin_init(gchar **error)
131 {
132         mimeview_register_viewer_factory(&mathml_viewer_factory);
133         return 0;       
134 }
135
136 void plugin_done()
137 {
138 }
139
140 const gchar *plugin_name()
141 {
142         return "MathML Viewer";
143 }
144
145 const gchar *plugin_desc()
146 {
147         return "";
148 }