Simplify Litehtml plugin's show_mimepart
[claws.git] / src / plugins / litehtml_viewer / lh_viewer.c
1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 1999-2015 the Claws Mail Team
4  * == Fancy Plugin ==
5  * This file Copyright (C) 2009-2015 Salvatore De Paolis
6  * <iwkse@claws-mail.org> and the Claws Mail Team
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write tothe 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 #include "claws-features.h"
23 #endif
24
25 #include <codeconv.h>
26 #include "common/utils.h"
27 #include "mainwindow.h"
28 #include "statusbar.h"
29 #include "lh_viewer.h"
30
31 static gchar *content_types[] = { "text/html", NULL };
32
33 MimeViewer *lh_viewer_create();
34
35 MimeViewerFactory lh_viewer_factory = {
36         content_types,
37         0,
38         lh_viewer_create
39 };
40
41 static GtkWidget *lh_get_widget(MimeViewer *_viewer)
42 {
43         debug_print("LH: get_widget\n");
44         LHViewer *viewer = (LHViewer *)_viewer;
45         return viewer->vbox;
46 }
47
48 static gchar *get_utf8_string(const gchar *string) {
49         gchar *utf8 = NULL;
50         gsize length;
51         GError *error = NULL;
52         gchar *locale = NULL;
53
54         if (!g_utf8_validate(string, -1, NULL)) {
55                 const gchar *cur_locale = conv_get_current_locale();
56                 gchar* split = g_strstr_len(cur_locale, -1, ".");
57                 if (split) {
58                     locale = ++split;
59                 } else {
60                     locale = (gchar *) cur_locale;
61                 }
62                 debug_print("Try converting to UTF-8 from %s\n", locale);
63                 if (g_ascii_strcasecmp("utf-8", locale) != 0) {
64                     utf8 = g_convert(string, -1, "utf-8", locale, NULL, &length, &error);
65                     if (error) {
66                             debug_print("Failed convertion to current locale: %s\n", error->message);
67                             g_clear_error(&error);
68                         }
69             }
70             if (!utf8) {
71                 debug_print("Use iso-8859-1 as last resort\n");
72                         utf8 = g_convert(string, -1, "utf-8", "iso-8859-1", NULL, &length, &error);
73                         if (error) {
74                                 debug_print("Charset detection failed. Use text as is\n");
75                                 utf8 = g_strdup(string);
76                                 g_clear_error(&error);
77                         }
78                 }
79         } else {
80                 utf8 = g_strdup(string);
81         }
82
83         return utf8;
84 }
85
86 static void lh_show_mimepart(MimeViewer *_viewer, const gchar *infile,
87                 MimeInfo *partinfo)
88 {
89         debug_print("LH: show_mimepart\n");
90         LHViewer *viewer = (LHViewer *)_viewer;
91         gchar *utf8 = procmime_get_part_as_string(partinfo);
92
93         if (utf8 == NULL) {
94                 g_warning("LH: couldn't get MIME part file\n");
95                 return;
96         }
97
98         lh_widget_open_html(viewer->widget, utf8);
99         g_free(utf8);
100 }
101
102 static void lh_clear_viewer(MimeViewer *_viewer)
103 {
104         debug_print("LH: clear_viewer\n");
105         LHViewer *viewer = (LHViewer *)_viewer;
106         lh_widget_clear(viewer->widget);
107 }
108
109 static void lh_destroy_viewer(MimeViewer *_viewer)
110 {
111         debug_print("LH: destroy_viewer\n");
112
113         /* Just in case. */
114         lh_clear_viewer(_viewer);
115
116 //      LHViewer *viewer = (LHViewer *)_viewer;
117 //      lh_widget_destroy(viewer->widget);
118 }
119
120 static void lh_print_viewer (MimeViewer *_viewer)
121 {
122     debug_print("LH: print_viewer\n");
123     
124     LHViewer* viewer = (LHViewer *) _viewer;
125     lh_widget_print(viewer->widget);    
126 }
127
128 /***************************************************************/
129 MimeViewer *lh_viewer_create()
130 {
131         debug_print("LH: viewer_create\n");
132
133         LHViewer *viewer = g_new0(LHViewer, 1);
134         viewer->mimeviewer.factory = &lh_viewer_factory;
135         viewer->widget = lh_widget_new();
136
137         viewer->mimeviewer.get_widget = lh_get_widget;
138         viewer->mimeviewer.show_mimepart = lh_show_mimepart;
139
140         viewer->mimeviewer.clear_viewer = lh_clear_viewer;
141         viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
142         
143         viewer->mimeviewer.print = lh_print_viewer;
144
145         viewer->vbox = gtk_vbox_new(FALSE, 0);
146
147         GtkWidget *w = lh_widget_get_widget(viewer->widget);
148         gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
149                         TRUE, TRUE, 1);
150
151         gtk_widget_show_all(viewer->vbox);
152
153         return (MimeViewer *)viewer;
154 }
155
156 void lh_widget_statusbar_push(const gchar* msg)
157 {
158         MainWindow *mainwin = mainwindow_get_mainwindow();
159         STATUSBAR_PUSH(mainwin, msg);
160 }
161
162 void lh_widget_statusbar_pop()
163 {
164         MainWindow *mainwin = mainwindow_get_mainwindow();
165         STATUSBAR_POP(mainwin);
166 }