aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-format-html-print.c
blob: df12ddf856c397955df417b1917725b7d91645f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2004 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <libgnomeprint/gnome-print-job.h>
#include <libgnomeprintui/gnome-print-job-preview.h>

#include <gtkhtml/gtkhtml.h>
#include <gtk/gtkwindow.h>

#include <camel/camel-i18n.h>
#include "mail-ops.h"
#include "mail-mt.h"
#include "em-format-html-print.h"

static void efhp_builtin_init(EMFormatHTMLPrintClass *efhc);

static EMFormatHTMLClass *efhp_parent;

static void
efhp_init(GObject *o)
{
    EMFormatHTMLPrint *efhp = (EMFormatHTMLPrint *)o;
    GtkWidget *html = (GtkWidget *)efhp->formathtml.html;

    /* ?? */
    gtk_widget_set_name(html, "EvolutionMailPrintHTMLWidget");

    /* gtk widgets don't like to be realized outside top level widget
       so we put new html widget into gtk window */
    efhp->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_container_add((GtkContainer *)efhp->window, html);
    gtk_widget_realize(html);
    efhp->formathtml.show_rupert = FALSE;
}

static void
efhp_finalise(GObject *o)
{
    EMFormatHTMLPrint *efhp = (EMFormatHTMLPrint *)o;

    gtk_widget_destroy(efhp->window);
    if (efhp->config)
        g_object_unref(efhp->config);
    if (efhp->source)
        g_object_unref(efhp->source);

    ((GObjectClass *)efhp_parent)->finalize(o);
}

static void
efhp_base_init(EMFormatHTMLPrintClass *efhpklass)
{
    efhp_builtin_init(efhpklass);
}

static void
efhp_class_init(GObjectClass *klass)
{
    klass->finalize = efhp_finalise;
}

GType
em_format_html_print_get_type(void)
{
    static GType type = 0;

    if (type == 0) {
        static const GTypeInfo info = {
            sizeof(EMFormatHTMLPrintClass),
            (GBaseInitFunc)efhp_base_init, NULL,
            (GClassInitFunc)efhp_class_init,
            NULL, NULL,
            sizeof(EMFormatHTMLPrint), 0,
            (GInstanceInitFunc)efhp_init
        };
        efhp_parent = g_type_class_ref(em_format_html_get_type());
        type = g_type_register_static(em_format_html_get_type(), "EMFormatHTMLPrint", &info, 0);
    }

    return type;
}

EMFormatHTMLPrint *em_format_html_print_new(void)
{
    EMFormatHTMLPrint *efhp;

    efhp = g_object_new(em_format_html_print_get_type(), 0);

    return efhp;
}

struct footer_info {
    GnomeFont *local_font;
    gint page_num, pages;
};

static void
efhp_footer_cb(GtkHTML *html, GnomePrintContext *print_context, double x, double y, double width, double height, void *data)
{
    struct footer_info *info = data;

    /* do we want anything nicer here, like who its from, etc? */
    if (info->local_font) {
        char *text = g_strdup_printf (_("Page %d of %d"), info->page_num, info->pages);
        /*gdouble tw = gnome_font_get_width_string (info->local_font, text);*/
        /* FIXME: work out how to measure this */
        gdouble tw = strlen(text) * 8;
        
        gnome_print_gsave(print_context);
        gnome_print_newpath(print_context);
        gnome_print_setrgbcolor(print_context, .0, .0, .0);
        gnome_print_moveto(print_context, x + width - tw, y - gnome_font_get_ascender(info->local_font));
        gnome_print_setfont(print_context, info->local_font);
        gnome_print_show(print_context, text);
        gnome_print_grestore(print_context);
        
        g_free(text);
        info->page_num++;
    }
}

/* perform preview, or print */
/* returns GNOME_PRINT_OK on success */
static void
emfhp_complete(EMFormatHTMLPrint *efhp, void *data)
{
    GnomePrintContext *print_context;
    GnomePrintJob *print_job;
    gdouble line = 0.0;
    struct footer_info info;
    int res = GNOME_PRINT_OK;

    print_job = gnome_print_job_new(efhp->config);
    print_context = gnome_print_job_get_context(print_job);

    gtk_html_print_set_master(efhp->formathtml.html, print_job);
    info.local_font = gnome_font_find_closest("Sans Regular", 10.0);
    if (info.local_font) {
        line = gnome_font_get_ascender(info.local_font) - gnome_font_get_descender(info.local_font);    
        info.page_num = 1;
        info.pages = gtk_html_print_get_pages_num(efhp->formathtml.html, print_context, 0.0, line);
        gtk_html_print_with_header_footer(efhp->formathtml.html, print_context, 0.0, line, NULL, efhp_footer_cb, &info);
        gnome_font_unref(info.local_font);
    } else {
        gtk_html_print(efhp->formathtml.html, print_context);
    }
    gtk_html_print_set_master(efhp->formathtml.html, NULL);

    gnome_print_job_close(print_job);

    if (efhp->preview)
        gtk_widget_show(gnome_print_job_preview_new(print_job, _("Print Preview")));
    else
        res = gnome_print_job_print(print_job);

    g_object_unref(print_job);
    g_object_unref(efhp);
}

int em_format_html_print_print(EMFormatHTMLPrint *efhp, EMFormatHTML *source, struct _GnomePrintConfig *print_config, int preview)
{
    EMFormat *emfs = (EMFormat *)source;

    efhp->config = print_config;
    if (print_config)
        g_object_ref(print_config);
    efhp->preview = preview;

    ((EMFormatHTML *)efhp)->load_http = source->load_http_now;

    g_signal_connect(efhp, "complete", G_CALLBACK(emfhp_complete), efhp);

    g_object_ref(efhp);
    em_format_format_clone((EMFormat *)efhp, emfs->folder, emfs->uid, emfs->message, (EMFormat *)source);

    return 0;       /* damn async ... */
}

static void
emfhp_got_message(struct _CamelFolder *folder, const char *uid, struct _CamelMimeMessage *msg, void *data)
{
    EMFormatHTMLPrint *efhp = data;

    if (msg) {
        if (efhp->source)
            ((EMFormatHTML *)efhp)->load_http = efhp->source->load_http_now;
        g_signal_connect(efhp, "complete", G_CALLBACK(emfhp_complete), efhp);
        em_format_format_clone((EMFormat *)efhp, folder, uid, msg, (EMFormat *)efhp->source);
    } else {
        g_object_unref(efhp);
    }
}

int em_format_html_print_message(EMFormatHTMLPrint *efhp, EMFormatHTML *source, struct _GnomePrintConfig *print_config, struct _CamelFolder *folder, const char *uid, int preview)
{
    efhp->config = print_config;
    if (print_config)
        g_object_ref(print_config);
    efhp->preview = preview;
    efhp->source = source;
    if (source)
        g_object_ref(source);
    g_object_ref(efhp);

    mail_get_message(folder, uid, emfhp_got_message, efhp, mail_thread_new);

    return 0;       /* damn async ... */
}

int em_format_html_print_raw_message(EMFormatHTMLPrint *efhp, struct _GnomePrintConfig *print_config, struct _CamelMimeMessage *msg, int preview)
{
    efhp->config = print_config;
    if (print_config)
        g_object_ref(print_config);
    efhp->source = NULL;
    efhp->preview = preview;
    g_object_ref(efhp);

    emfhp_got_message(NULL, NULL, msg, efhp);

    return 0;
}

/* ********************************************************************** */

/* if only ... but i doubt this is possible with gnome print/gtkhtml */
static EMFormatHandler type_builtin_table[] = {
    /*{ "application/postscript", (EMFormatFunc)efhp_application_postscript },*/
};

static void
efhp_builtin_init(EMFormatHTMLPrintClass *efhc)
{
    int i;

    for (i=0;i<sizeof(type_builtin_table)/sizeof(type_builtin_table[0]);i++)
        em_format_class_add_handler((EMFormatClass *)efhc, &type_builtin_table[i]);
}