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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/printing/print_dialog_gtk.h"
#include <fcntl.h>
#include <gtk/gtkpagesetupunixdialog.h>
#include <gtk/gtkprintjob.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <vector>
#include "base/file_util.h"
#include "base/file_util_proxy.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "printing/metafile.h"
#include "printing/print_job_constants.h"
#include "printing/print_settings_initializer_gtk.h"
using printing::PageRanges;
using printing::PrintSettings;
namespace {
// CUPS ColorModel attribute and values.
const char kCMYK[] = "CMYK";
const char kCUPSColorModel[] = "cups-ColorModel";
const char kColor[] = "Color";
const char kGrayscale[] = "Grayscale";
// CUPS Duplex attribute and values.
const char kCUPSDuplex[] = "cups-Duplex";
const char kDuplexNone[] = "None";
const char kDuplexTumble[] = "DuplexTumble";
const char kDuplexNoTumble[] = "DuplexNoTumble";
// Helper class to track GTK printers.
class GtkPrinterList {
public:
GtkPrinterList() : default_printer_(NULL) {
gtk_enumerate_printers((GtkPrinterFunc)SetPrinter, this, NULL, TRUE);
}
~GtkPrinterList() {
for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
it < printers_.end(); ++it) {
g_object_unref(*it);
}
}
// Can return NULL if there's no default printer. E.g. Printer on a laptop
// is "home_printer", but the laptop is at work.
GtkPrinter* default_printer() {
return default_printer_;
}
// Can return NULL if the printer cannot be found due to:
// - Printer list out of sync with printer dialog UI.
// - Querying for non-existant printers like 'Print to PDF'.
GtkPrinter* GetPrinterWithName(const char* name) {
if (!name || !*name)
return NULL;
for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
it < printers_.end(); ++it) {
if (strcmp(name, gtk_printer_get_name(*it)) == 0) {
return *it;
}
}
return NULL;
}
private:
// Callback function used by gtk_enumerate_printers() to get all printer.
static bool SetPrinter(GtkPrinter* printer, GtkPrinterList* printer_list) {
if (gtk_printer_is_default(printer))
printer_list->default_printer_ = printer;
g_object_ref(printer);
printer_list->printers_.push_back(printer);
return false;
}
std::vector<GtkPrinter*> printers_;
GtkPrinter* default_printer_;
};
} // namespace
// static
printing::PrintDialogGtkInterface* PrintDialogGtk::CreatePrintDialog(
PrintingContextCairo* context) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return new PrintDialogGtk(context);
}
PrintDialogGtk::PrintDialogGtk(PrintingContextCairo* context)
: callback_(NULL),
context_(context),
dialog_(NULL),
gtk_settings_(NULL),
page_setup_(NULL),
printer_(NULL) {
}
PrintDialogGtk::~PrintDialogGtk() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (dialog_) {
gtk_widget_destroy(dialog_);
dialog_ = NULL;
}
if (gtk_settings_) {
g_object_unref(gtk_settings_);
gtk_settings_ = NULL;
}
if (page_setup_) {
g_object_unref(page_setup_);
page_setup_ = NULL;
}
if (printer_) {
g_object_unref(printer_);
printer_ = NULL;
}
}
void PrintDialogGtk::UseDefaultSettings() {
DCHECK(!page_setup_);
// |gtk_settings_| is a new object.
gtk_settings_ = gtk_print_settings_new();
scoped_ptr<GtkPrinterList> printer_list(new GtkPrinterList);
printer_ = printer_list->default_printer();
if (printer_) {
g_object_ref(printer_);
gtk_print_settings_set_printer(gtk_settings_,
gtk_printer_get_name(printer_));
page_setup_ = gtk_printer_get_default_page_size(printer_);
}
if (!page_setup_)
page_setup_ = gtk_page_setup_new();
// No page range to initialize for default settings.
PageRanges ranges_vector;
InitPrintSettings(ranges_vector);
}
bool PrintDialogGtk::UpdateSettings(const DictionaryValue& settings,
const printing::PageRanges& ranges) {
bool collate;
int color;
bool landscape;
bool print_to_pdf;
int copies;
int duplex_mode;
std::string device_name;
if (!settings.GetBoolean(printing::kSettingLandscape, &landscape) ||
!settings.GetBoolean(printing::kSettingCollate, &collate) ||
!settings.GetInteger(printing::kSettingColor, &color) ||
!settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf) ||
!settings.GetInteger(printing::kSettingDuplexMode, &duplex_mode) ||
!settings.GetInteger(printing::kSettingCopies, &copies) ||
!settings.GetString(printing::kSettingDeviceName, &device_name)) {
return false;
}
if (!gtk_settings_)
gtk_settings_ = gtk_print_settings_new();
if (!print_to_pdf) {
scoped_ptr<GtkPrinterList> printer_list(new GtkPrinterList);
printer_ = printer_list->GetPrinterWithName(device_name.c_str());
if (printer_) {
g_object_ref(printer_);
gtk_print_settings_set_printer(gtk_settings_,
gtk_printer_get_name(printer_));
if (!page_setup_) {
page_setup_ = gtk_printer_get_default_page_size(printer_);
}
}
gtk_print_settings_set_n_copies(gtk_settings_, copies);
gtk_print_settings_set_collate(gtk_settings_, collate);
const char* color_mode;
switch (color) {
case printing::COLOR:
color_mode = kColor;
break;
case printing::CMYK:
color_mode = kCMYK;
break;
default:
color_mode = kGrayscale;
break;
}
gtk_print_settings_set(gtk_settings_, kCUPSColorModel, color_mode);
if (duplex_mode != printing::UNKNOWN_DUPLEX_MODE) {
const char* cups_duplex_mode = NULL;
switch (duplex_mode) {
case printing::LONG_EDGE:
cups_duplex_mode = kDuplexNoTumble;
break;
case printing::SHORT_EDGE:
cups_duplex_mode = kDuplexTumble;
break;
case printing::SIMPLEX:
cups_duplex_mode = kDuplexNone;
break;
default: // UNKNOWN_DUPLEX_MODE
NOTREACHED();
break;
}
gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode);
}
}
if (!page_setup_)
page_setup_ = gtk_page_setup_new();
gtk_print_settings_set_orientation(
gtk_settings_,
landscape ? GTK_PAGE_ORIENTATION_LANDSCAPE :
GTK_PAGE_ORIENTATION_PORTRAIT);
InitPrintSettings(ranges);
return true;
}
void PrintDialogGtk::ShowDialog(
PrintingContextCairo::PrintSettingsCallback* callback) {
callback_ = callback;
GtkWindow* parent = BrowserList::GetLastActive()->window()->GetNativeHandle();
// TODO(estade): We need a window title here.
dialog_ = gtk_print_unix_dialog_new(NULL, parent);
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
// Set modal so user cannot focus the same tab and press print again.
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
// Since we only generate PDF, only show printers that support PDF.
// TODO(thestig) Add more capabilities to support?
GtkPrintCapabilities cap = static_cast<GtkPrintCapabilities>(
GTK_PRINT_CAPABILITY_GENERATE_PDF |
GTK_PRINT_CAPABILITY_PAGE_SET |
GTK_PRINT_CAPABILITY_COPIES |
GTK_PRINT_CAPABILITY_COLLATE |
GTK_PRINT_CAPABILITY_REVERSE);
gtk_print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(dialog_),
cap);
gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_),
TRUE);
g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this);
gtk_widget_show(dialog_);
}
void PrintDialogGtk::PrintDocument(const printing::Metafile* metafile,
const string16& document_name) {
// This runs on the print worker thread, does not block the UI thread.
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
// The document printing tasks can outlive the PrintingContext that created
// this dialog.
AddRef();
bool error = false;
if (!file_util::CreateTemporaryFile(&path_to_pdf_)) {
LOG(ERROR) << "Creating temporary file failed";
error = true;
}
if (!error && !metafile->SaveTo(path_to_pdf_)) {
LOG(ERROR) << "Saving metafile failed";
file_util::Delete(path_to_pdf_, false);
error = true;
}
if (error) {
// Matches AddRef() above.
Release();
} else {
// No errors, continue printing.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this,
&PrintDialogGtk::SendDocumentToPrinter,
document_name));
}
}
void PrintDialogGtk::AddRefToDialog() {
AddRef();
}
void PrintDialogGtk::ReleaseDialog() {
Release();
}
void PrintDialogGtk::OnResponse(GtkWidget* dialog, int response_id) {
gtk_widget_hide(dialog_);
switch (response_id) {
case GTK_RESPONSE_OK: {
if (gtk_settings_)
g_object_unref(gtk_settings_);
gtk_settings_ = gtk_print_unix_dialog_get_settings(
GTK_PRINT_UNIX_DIALOG(dialog_));
if (printer_)
g_object_unref(printer_);
printer_ = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog_));
g_object_ref(printer_);
if (page_setup_)
g_object_unref(page_setup_);
page_setup_ = gtk_print_unix_dialog_get_page_setup(
GTK_PRINT_UNIX_DIALOG(dialog_));
g_object_ref(page_setup_);
// Handle page ranges.
PageRanges ranges_vector;
gint num_ranges;
GtkPageRange* gtk_range =
gtk_print_settings_get_page_ranges(gtk_settings_, &num_ranges);
if (gtk_range) {
for (int i = 0; i < num_ranges; ++i) {
printing::PageRange range;
range.from = gtk_range[i].start;
range.to = gtk_range[i].end;
ranges_vector.push_back(range);
}
g_free(gtk_range);
}
PrintSettings settings;
printing::PrintSettingsInitializerGtk::InitPrintSettings(
gtk_settings_, page_setup_, ranges_vector, false, &settings);
context_->InitWithSettings(settings);
callback_->Run(PrintingContextCairo::OK);
callback_ = NULL;
return;
}
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: {
callback_->Run(PrintingContextCairo::CANCEL);
callback_ = NULL;
return;
}
case GTK_RESPONSE_APPLY:
default: {
NOTREACHED();
}
}
}
void PrintDialogGtk::SendDocumentToPrinter(const string16& document_name) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// If |printer_| is NULL then somehow the GTK printer list changed out under
// us. In which case, just bail out.
if (!printer_) {
// Matches AddRef() in PrintDocument();
Release();
return;
}
GtkPrintJob* print_job = gtk_print_job_new(
UTF16ToUTF8(document_name).c_str(),
printer_,
gtk_settings_,
page_setup_);
gtk_print_job_set_source_file(print_job, path_to_pdf_.value().c_str(), NULL);
gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL);
}
// static
void PrintDialogGtk::OnJobCompletedThunk(GtkPrintJob* print_job,
gpointer user_data,
GError* error) {
static_cast<PrintDialogGtk*>(user_data)->OnJobCompleted(print_job, error);
}
void PrintDialogGtk::OnJobCompleted(GtkPrintJob* print_job, GError* error) {
if (error)
LOG(ERROR) << "Printing failed: " << error->message;
if (print_job)
g_object_unref(print_job);
base::FileUtilProxy::Delete(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
path_to_pdf_,
false,
NULL);
// Printing finished. Matches AddRef() in PrintDocument();
Release();
}
void PrintDialogGtk::InitPrintSettings(const PageRanges& page_ranges) {
PrintSettings settings;
printing::PrintSettingsInitializerGtk::InitPrintSettings(
gtk_settings_, page_setup_, page_ranges, false, &settings);
context_->InitWithSettings(settings);
}
|