aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/EphyUtils.cpp
blob: 60b5e01afe2b3b20ff6dfe5ce5be3d74c9e85813 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 *  Copyright (C) 2004 Marco Pesenti Gritti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1, 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 Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "mozilla-config.h"
#include "config.h"

#include <gtkmozembed.h>
#include <nsCOMPtr.h>
#include <nsIDOMWindow.h>
#include <nsIEmbeddingSiteWindow.h>
#include <nsIFile.h>
#include <nsIIOService.h>
#include <nsIPrintSettings.h>
#include <nsIServiceManager.h>
#include <nsIURI.h>
#include <nsIWebBrowserChrome.h>
#include <nsIWindowWatcher.h>
#include <nsServiceManagerUtils.h>
#include <nsStringAPI.h>
#include <nsXPCOM.h>

#include "ephy-embed-shell.h"
#include "ephy-embed-single.h"
#include "ephy-file-helpers.h"
#include "print-dialog.h"

#include "EphyUtils.h"

nsresult
EphyUtils::GetIOService (nsIIOService **ioService)
{
    nsresult rv;

    nsCOMPtr<nsIServiceManager> mgr; 
    NS_GetServiceManager (getter_AddRefs (mgr));
    if (!mgr) return NS_ERROR_FAILURE;

    rv = mgr->GetServiceByContractID ("@mozilla.org/network/io-service;1",
                      NS_GET_IID (nsIIOService),
                      (void **)ioService);
    return rv;
}

nsresult
EphyUtils::NewURI (nsIURI **result,
           const nsAString &spec,
           const char *charset,
           nsIURI *baseURI)
{
    nsCString cSpec;
    NS_UTF16ToCString (spec, NS_CSTRING_ENCODING_UTF8, cSpec);

    return NewURI (result, cSpec, charset, baseURI);
}

nsresult
EphyUtils::NewURI (nsIURI **result,
           const nsACString &spec,
           const char *charset,
           nsIURI *baseURI)
{
    nsresult rv;
    nsCOMPtr<nsIIOService> ioService;
    rv = EphyUtils::GetIOService (getter_AddRefs (ioService));
    NS_ENSURE_SUCCESS (rv, rv);

    return ioService->NewURI (spec, charset, baseURI, result);
}

nsresult
EphyUtils::NewFileURI (nsIURI **result,
               nsIFile *spec)
{
    nsresult rv;
    nsCOMPtr<nsIIOService> ioService;
    rv = EphyUtils::GetIOService (getter_AddRefs (ioService));
    NS_ENSURE_SUCCESS (rv, rv);

    return ioService->NewFileURI (spec, result);
}

GtkWidget *
EphyUtils::FindEmbed (nsIDOMWindow *aDOMWindow)
{
    if (!aDOMWindow) return nsnull;

    nsCOMPtr<nsIWindowWatcher> wwatch
        (do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
    NS_ENSURE_TRUE (wwatch, nsnull);

    /* this DOM window may belong to some inner frame, we need
     * to get the topmost DOM window to get the embed
     */
    nsCOMPtr<nsIDOMWindow> topWindow;
    aDOMWindow->GetTop (getter_AddRefs (topWindow));
    if (!topWindow) return nsnull;
    
    nsCOMPtr<nsIWebBrowserChrome> windowChrome;
    wwatch->GetChromeForWindow (topWindow, getter_AddRefs(windowChrome));
    NS_ENSURE_TRUE (windowChrome, nsnull);

    nsCOMPtr<nsIEmbeddingSiteWindow> window (do_QueryInterface(windowChrome));
    NS_ENSURE_TRUE (window, nsnull);

    nsresult rv;
    GtkWidget *mozembed;
    rv = window->GetSiteWindow ((void **)&mozembed);
    NS_ENSURE_SUCCESS (rv, nsnull);

    return mozembed;
}

GtkWidget *
EphyUtils::FindGtkParent (nsIDOMWindow *aDOMWindow)
{
    GtkWidget *embed = FindEmbed (aDOMWindow);
    NS_ENSURE_TRUE (embed, nsnull);

    GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (embed));
    if (!GTK_WIDGET_TOPLEVEL (toplevel)) return nsnull;

    return toplevel;
}

nsresult
EphyUtils::CollatePrintSettings (EmbedPrintInfo *info,
                 nsIPrintSettings *options,
                 gboolean preview)
{
    NS_ENSURE_ARG (options);

    /* FIXME: for CUPS printers, print directly instead of to a tmp file? */
    const static PRUnichar pName[] = { 'P', 'o', 's', 't', 'S', 'c', 'r', 'i', 'p', 't', '/', 'd', 'e', 'f', 'a', 'u', 'l', 't', '\0' };
    options->SetPrinterName(nsString(pName).get());

    const static int frame_types[] = {
        nsIPrintSettings::kFramesAsIs,
        nsIPrintSettings::kSelectedFrame,
        nsIPrintSettings::kEachFrameSep
    };

    switch (info->range)
    {
    case GNOME_PRINT_RANGE_CURRENT:
    case GNOME_PRINT_RANGE_SELECTION_UNSENSITIVE:
    case GNOME_PRINT_RANGE_ALL:
        options->SetPrintRange (nsIPrintSettings::kRangeAllPages);
        break;
    case GNOME_PRINT_RANGE_RANGE:
        options->SetPrintRange (nsIPrintSettings::kRangeSpecifiedPageRange);
        options->SetStartPageRange (info->from_page);
        options->SetEndPageRange (info->to_page);
        break;
    case GNOME_PRINT_RANGE_SELECTION:
        options->SetPrintRange (nsIPrintSettings::kRangeSelection);
        break;
    }
    
    const GnomePrintUnit *unit, *inch, *mm;
    double value;

    mm = gnome_print_unit_get_by_abbreviation ((const guchar *) "mm");
    inch = gnome_print_unit_get_by_abbreviation ((const guchar *) "in");
    g_assert (mm != NULL && inch != NULL);

    /* top margin */
    if (gnome_print_config_get_length (info->config,
                       (const guchar *) GNOME_PRINT_KEY_PAGE_MARGIN_TOP,
                       &value, &unit)
        && gnome_print_convert_distance (&value, unit, inch))
    {
        options->SetMarginTop (value);
    }

    /* bottom margin */
    if (gnome_print_config_get_length (info->config,
                       (const guchar *) GNOME_PRINT_KEY_PAGE_MARGIN_BOTTOM,
                       &value, &unit)
        && gnome_print_convert_distance (&value, unit, inch))
    {
        options->SetMarginBottom (value);
    }

    /* left margin */
    if (gnome_print_config_get_length (info->config,
                       (const guchar *) GNOME_PRINT_KEY_PAGE_MARGIN_LEFT,
                       &value, &unit)
        && gnome_print_convert_distance (&value, unit, inch))
    {
        options->SetMarginLeft (value);
    }

    /* right margin */
    if (gnome_print_config_get_length (info->config,
                       (const guchar *) GNOME_PRINT_KEY_PAGE_MARGIN_RIGHT,
                       &value, &unit)
        && gnome_print_convert_distance (&value, unit, inch))
    {
        options->SetMarginRight (value);
    }


    nsString tmp;

    NS_CStringToUTF16 (nsCString(info->header_left_string),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetHeaderStrLeft (tmp.get());

    NS_CStringToUTF16 (nsCString(info->header_center_string),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetHeaderStrCenter (tmp.get());

    NS_CStringToUTF16 (nsCString(info->header_right_string),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetHeaderStrRight (tmp.get());

    NS_CStringToUTF16 (nsCString(info->footer_left_string),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetFooterStrLeft (tmp.get());

    NS_CStringToUTF16 (nsCString(info->footer_center_string),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetFooterStrCenter(tmp.get());

    NS_CStringToUTF16 (nsCString(info->footer_right_string),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetFooterStrRight(tmp.get());

    options->SetPrintToFile (PR_FALSE);
    
    if (preview)
    {
        options->SetPrintToFile (PR_FALSE);
    }
    else
    {
        char *base;
        const char *temp_dir;
        
        temp_dir = ephy_file_tmp_dir ();
        base = g_build_filename (temp_dir, "printXXXXXX", NULL);
        info->tempfile = ephy_file_tmp_filename (base, "ps");
        g_free (base);
        
        if (info->tempfile == NULL) return NS_ERROR_FAILURE;
        
        NS_CStringToUTF16 (nsCString(info->tempfile),
                   NS_CSTRING_ENCODING_UTF8, tmp);
        options->SetPrintToFile (PR_TRUE);
        options->SetToFileName (tmp.get());
    }

    /* paper size */
    options->SetPaperSize (nsIPrintSettings::kPaperSizeDefined);
    options->SetPaperSizeUnit (nsIPrintSettings::kPaperSizeMillimeters);

    if (gnome_print_config_get_length (info->config,
                       (const guchar *) GNOME_PRINT_KEY_PAPER_WIDTH,
                       &value, &unit)
       && gnome_print_convert_distance (&value, unit, mm))
    {
        options->SetPaperWidth (value); 
    }

    if (gnome_print_config_get_length (info->config,
                       (const guchar *) GNOME_PRINT_KEY_PAPER_HEIGHT,
                       &value, &unit)
       && gnome_print_convert_distance (&value, unit, mm))
    {
        options->SetPaperHeight (value);    
    }

    /* Mozilla bug https://bugzilla.mozilla.org/show_bug.cgi?id=307404
     * means that we cannot actually use any paper sizes except mozilla's
     * builtin list, and we must refer to them *by name*!
     */
#ifndef HAVE_GECKO_1_9
    /* Gnome-Print names some papers differently than what moz understands */
    static const struct
    {
        const char *gppaper;
        const char *mozpaper;
    }
    paper_table [] =
    {
        { "USLetter", "Letter" },
        { "USLegal", "Legal" }
    };
#endif /* !HAVE_GECKO_1_9 */

    /* paper name */
    char *string = (char *) gnome_print_config_get (info->config,
                          (const guchar *) GNOME_PRINT_KEY_PAPER_SIZE);
    const char *paper = string;

#ifndef HAVE_GECKO_1_9
    for (PRUint32 i = 0; i < G_N_ELEMENTS (paper_table); i++)
    {
        if (string != NULL &&
            g_ascii_strcasecmp (paper_table[i].gppaper, string) == 0)
        {
            paper = paper_table[i].mozpaper;
            break;
        }
    }
#endif /* !HAVE_GECKO_1_9 */

    NS_CStringToUTF16 (nsCString(paper),
               NS_CSTRING_ENCODING_UTF8, tmp);
    options->SetPaperName (tmp.get());
    g_free (string);

    /* paper orientation */
    string = (char *) gnome_print_config_get (info->config,
                          (const guchar *) GNOME_PRINT_KEY_ORIENTATION);
    if (string == NULL) string = g_strdup ("R0");

    if (strncmp (string, "R90", 3) == 0 || strncmp (string, "R270", 4) == 0)
    {
        options->SetOrientation (nsIPrintSettings::kLandscapeOrientation);
    }
    else
    {
        options->SetOrientation (nsIPrintSettings::kPortraitOrientation);
    }
    g_free (string);

    options->SetPrintInColor (info->print_color);
    options->SetPrintFrameType (frame_types[info->frame_type]);

    return NS_OK;
}