Use statusbar for notification. Begin print implementation
[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 *infole,
87                 MimeInfo *partinfo)
88 {
89         debug_print("LH: show_mimepart\n");
90         LHViewer *viewer = (LHViewer *)_viewer;
91
92         gchar *msgfile = procmime_get_tmp_file_name(partinfo);
93         debug_print("LH: msgfile '%s'\n", msgfile);
94
95         if (procmime_get_part(msgfile, partinfo) < 0) {
96                 debug_print("LH: couldn't get MIME part file\n");
97                 g_free(msgfile);
98                 return;
99         }
100
101         gchar *contents, *utf8;
102         gsize length;
103         GError *error = NULL;
104         if (!g_file_get_contents(msgfile, &contents, &length, &error)) {
105                 g_warning("LiteHTML viewer: couldn't read contents of file '%s': %s",
106                                 msgfile, error->message);
107                 g_clear_error(&error);
108                 return;
109         } else {
110                 utf8 = get_utf8_string(contents);
111                 g_free(contents);
112         }
113
114         g_free(msgfile);
115
116         lh_widget_open_html(viewer->widget, utf8);
117         g_free(utf8);
118 }
119
120 static void lh_clear_viewer(MimeViewer *_viewer)
121 {
122         debug_print("LH: clear_viewer\n");
123         LHViewer *viewer = (LHViewer *)_viewer;
124         lh_widget_clear(viewer->widget);
125 }
126
127 static void lh_destroy_viewer(MimeViewer *_viewer)
128 {
129         debug_print("LH: destroy_viewer\n");
130
131         /* Just in case. */
132         lh_clear_viewer(_viewer);
133
134 //      LHViewer *viewer = (LHViewer *)_viewer;
135 //      lh_widget_destroy(viewer->widget);
136 }
137
138 static void lh_print_viewer (MimeViewer *_viewer)
139 {
140     debug_print("LH: print_viewer\n");
141     
142     LHViewer* viewer = (LHViewer *) _viewer;
143     lh_widget_print(viewer->widget);    
144 }
145
146 /***************************************************************/
147 MimeViewer *lh_viewer_create()
148 {
149         debug_print("LH: viewer_create\n");
150
151         LHViewer *viewer = g_new0(LHViewer, 1);
152         viewer->mimeviewer.factory = &lh_viewer_factory;
153         viewer->widget = lh_widget_new();
154
155         viewer->mimeviewer.get_widget = lh_get_widget;
156         viewer->mimeviewer.show_mimepart = lh_show_mimepart;
157
158         viewer->mimeviewer.clear_viewer = lh_clear_viewer;
159         viewer->mimeviewer.destroy_viewer = lh_destroy_viewer;
160         
161         viewer->mimeviewer.print = lh_print_viewer;
162
163         viewer->vbox = gtk_vbox_new(FALSE, 0);
164
165         GtkWidget *w = lh_widget_get_widget(viewer->widget);
166         gtk_box_pack_start(GTK_BOX(viewer->vbox), w,
167                         TRUE, TRUE, 1);
168
169         gtk_widget_show_all(viewer->vbox);
170
171         return (MimeViewer *)viewer;
172 }
173
174 void lh_widget_statusbar_push(gchar* msg)
175 {
176         MainWindow *mainwin = mainwindow_get_mainwindow();
177         STATUSBAR_PUSH(mainwin, msg);
178         g_free(msg);
179 }
180
181 void lh_widget_statusbar_pop()
182 {
183         MainWindow *mainwin = mainwindow_get_mainwindow();
184         STATUSBAR_POP(mainwin);
185 }