1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/printing/print_dialog_gtk.h"
7 #include <gtk/gtkunixprint.h>
12 #include "base/bind.h"
13 #include "base/file_util.h"
14 #include "base/files/file_util_proxy.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/message_loop/message_loop_proxy.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "printing/metafile.h"
21 #include "printing/print_job_constants.h"
22 #include "printing/print_settings.h"
23 #include "printing/print_settings_initializer_gtk.h"
25 using content::BrowserThread
;
26 using printing::PageRanges
;
27 using printing::PrintSettings
;
31 // CUPS Duplex attribute and values.
32 const char kCUPSDuplex
[] = "cups-Duplex";
33 const char kDuplexNone
[] = "None";
34 const char kDuplexTumble
[] = "DuplexTumble";
35 const char kDuplexNoTumble
[] = "DuplexNoTumble";
37 class StickyPrintSettingGtk
{
39 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {
41 ~StickyPrintSettingGtk() {
42 NOTREACHED(); // Intended to be used with a Leaky LazyInstance.
45 GtkPrintSettings
* settings() {
46 return last_used_settings_
;
49 void SetLastUsedSettings(GtkPrintSettings
* settings
) {
50 DCHECK(last_used_settings_
);
51 g_object_unref(last_used_settings_
);
52 last_used_settings_
= gtk_print_settings_copy(settings
);
56 GtkPrintSettings
* last_used_settings_
;
58 DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk
);
61 base::LazyInstance
<StickyPrintSettingGtk
>::Leaky g_last_used_settings
=
62 LAZY_INSTANCE_INITIALIZER
;
64 // Helper class to track GTK printers.
65 class GtkPrinterList
{
67 GtkPrinterList() : default_printer_(NULL
) {
68 gtk_enumerate_printers(SetPrinter
, this, NULL
, TRUE
);
72 for (std::vector
<GtkPrinter
*>::iterator it
= printers_
.begin();
73 it
< printers_
.end(); ++it
) {
78 // Can return NULL if there's no default printer. E.g. Printer on a laptop
79 // is "home_printer", but the laptop is at work.
80 GtkPrinter
* default_printer() {
81 return default_printer_
;
84 // Can return NULL if the printer cannot be found due to:
85 // - Printer list out of sync with printer dialog UI.
86 // - Querying for non-existant printers like 'Print to PDF'.
87 GtkPrinter
* GetPrinterWithName(const std::string
& name
) {
91 for (std::vector
<GtkPrinter
*>::iterator it
= printers_
.begin();
92 it
< printers_
.end(); ++it
) {
93 if (gtk_printer_get_name(*it
) == name
) {
102 // Callback function used by gtk_enumerate_printers() to get all printer.
103 static gboolean
SetPrinter(GtkPrinter
* printer
, gpointer data
) {
104 GtkPrinterList
* printer_list
= reinterpret_cast<GtkPrinterList
*>(data
);
105 if (gtk_printer_is_default(printer
))
106 printer_list
->default_printer_
= printer
;
108 g_object_ref(printer
);
109 printer_list
->printers_
.push_back(printer
);
114 std::vector
<GtkPrinter
*> printers_
;
115 GtkPrinter
* default_printer_
;
121 printing::PrintDialogGtkInterface
* PrintDialogGtk::CreatePrintDialog(
122 PrintingContextLinux
* context
) {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
124 return new PrintDialogGtk(context
);
127 PrintDialogGtk::PrintDialogGtk(PrintingContextLinux
* context
)
135 PrintDialogGtk::~PrintDialogGtk() {
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
139 gtk_widget_destroy(dialog_
);
143 g_object_unref(gtk_settings_
);
144 gtk_settings_
= NULL
;
147 g_object_unref(page_setup_
);
151 g_object_unref(printer_
);
156 void PrintDialogGtk::UseDefaultSettings() {
157 DCHECK(!page_setup_
);
160 // |gtk_settings_| is a new copy.
162 gtk_print_settings_copy(g_last_used_settings
.Get().settings());
163 page_setup_
= gtk_page_setup_new();
165 PrintSettings settings
;
166 InitPrintSettings(&settings
);
169 bool PrintDialogGtk::UpdateSettings(printing::PrintSettings
* settings
) {
170 if (!gtk_settings_
) {
172 gtk_print_settings_copy(g_last_used_settings
.Get().settings());
175 scoped_ptr
<GtkPrinterList
> printer_list(new GtkPrinterList
);
176 printer_
= printer_list
->GetPrinterWithName(
177 base::UTF16ToUTF8(settings
->device_name()));
179 g_object_ref(printer_
);
180 gtk_print_settings_set_printer(gtk_settings_
,
181 gtk_printer_get_name(printer_
));
183 page_setup_
= gtk_printer_get_default_page_size(printer_
);
187 gtk_print_settings_set_n_copies(gtk_settings_
, settings
->copies());
188 gtk_print_settings_set_collate(gtk_settings_
, settings
->collate());
190 #if defined(USE_CUPS)
191 std::string color_value
;
192 std::string color_setting_name
;
193 printing::GetColorModelForMode(settings
->color(), &color_setting_name
,
195 gtk_print_settings_set(gtk_settings_
, color_setting_name
.c_str(),
196 color_value
.c_str());
198 if (settings
->duplex_mode() != printing::UNKNOWN_DUPLEX_MODE
) {
199 const char* cups_duplex_mode
= NULL
;
200 switch (settings
->duplex_mode()) {
201 case printing::LONG_EDGE
:
202 cups_duplex_mode
= kDuplexNoTumble
;
204 case printing::SHORT_EDGE
:
205 cups_duplex_mode
= kDuplexTumble
;
207 case printing::SIMPLEX
:
208 cups_duplex_mode
= kDuplexNone
;
210 default: // UNKNOWN_DUPLEX_MODE
214 gtk_print_settings_set(gtk_settings_
, kCUPSDuplex
, cups_duplex_mode
);
218 page_setup_
= gtk_page_setup_new();
220 gtk_print_settings_set_orientation(
222 settings
->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE
:
223 GTK_PAGE_ORIENTATION_PORTRAIT
);
225 InitPrintSettings(settings
);
229 void PrintDialogGtk::ShowDialog(
230 gfx::NativeView parent_view
,
232 const PrintingContextLinux::PrintSettingsCallback
& callback
) {
233 callback_
= callback
;
235 GtkWindow
* parent
= GTK_WINDOW(gtk_widget_get_toplevel(parent_view
));
236 // TODO(estade): We need a window title here.
237 dialog_
= gtk_print_unix_dialog_new(NULL
, parent
);
238 g_signal_connect(dialog_
, "delete-event",
239 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
242 // Set modal so user cannot focus the same tab and press print again.
243 gtk_window_set_modal(GTK_WINDOW(dialog_
), TRUE
);
245 // Since we only generate PDF, only show printers that support PDF.
246 // TODO(thestig) Add more capabilities to support?
247 GtkPrintCapabilities cap
= static_cast<GtkPrintCapabilities
>(
248 GTK_PRINT_CAPABILITY_GENERATE_PDF
|
249 GTK_PRINT_CAPABILITY_PAGE_SET
|
250 GTK_PRINT_CAPABILITY_COPIES
|
251 GTK_PRINT_CAPABILITY_COLLATE
|
252 GTK_PRINT_CAPABILITY_REVERSE
);
253 gtk_print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(dialog_
),
255 gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_
),
257 gtk_print_unix_dialog_set_support_selection(GTK_PRINT_UNIX_DIALOG(dialog_
),
259 gtk_print_unix_dialog_set_has_selection(GTK_PRINT_UNIX_DIALOG(dialog_
),
261 gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog_
),
263 g_signal_connect(dialog_
, "response", G_CALLBACK(OnResponseThunk
), this);
264 gtk_widget_show(dialog_
);
267 void PrintDialogGtk::PrintDocument(const printing::Metafile
* metafile
,
268 const base::string16
& document_name
) {
269 // This runs on the print worker thread, does not block the UI thread.
270 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI
));
272 // The document printing tasks can outlive the PrintingContext that created
277 if (!base::CreateTemporaryFile(&path_to_pdf_
)) {
278 LOG(ERROR
) << "Creating temporary file failed";
282 if (!error
&& !metafile
->SaveTo(path_to_pdf_
)) {
283 LOG(ERROR
) << "Saving metafile failed";
284 base::DeleteFile(path_to_pdf_
, false);
289 // Matches AddRef() above.
292 // No errors, continue printing.
293 BrowserThread::PostTask(
294 BrowserThread::UI
, FROM_HERE
,
295 base::Bind(&PrintDialogGtk::SendDocumentToPrinter
, this,
300 void PrintDialogGtk::AddRefToDialog() {
304 void PrintDialogGtk::ReleaseDialog() {
308 void PrintDialogGtk::OnResponse(GtkWidget
* dialog
, int response_id
) {
309 int num_matched_handlers
= g_signal_handlers_disconnect_by_func(
310 dialog_
, reinterpret_cast<gpointer
>(&OnResponseThunk
), this);
311 CHECK_EQ(1, num_matched_handlers
);
313 gtk_widget_hide(dialog_
);
315 switch (response_id
) {
316 case GTK_RESPONSE_OK
: {
318 g_object_unref(gtk_settings_
);
319 gtk_settings_
= gtk_print_unix_dialog_get_settings(
320 GTK_PRINT_UNIX_DIALOG(dialog_
));
323 g_object_unref(printer_
);
324 printer_
= gtk_print_unix_dialog_get_selected_printer(
325 GTK_PRINT_UNIX_DIALOG(dialog_
));
326 g_object_ref(printer_
);
329 g_object_unref(page_setup_
);
330 page_setup_
= gtk_print_unix_dialog_get_page_setup(
331 GTK_PRINT_UNIX_DIALOG(dialog_
));
332 g_object_ref(page_setup_
);
334 // Handle page ranges.
335 PageRanges ranges_vector
;
337 bool print_selection_only
= false;
338 switch (gtk_print_settings_get_print_pages(gtk_settings_
)) {
339 case GTK_PRINT_PAGES_RANGES
: {
340 GtkPageRange
* gtk_range
=
341 gtk_print_settings_get_page_ranges(gtk_settings_
, &num_ranges
);
343 for (int i
= 0; i
< num_ranges
; ++i
) {
344 printing::PageRange range
;
345 range
.from
= gtk_range
[i
].start
;
346 range
.to
= gtk_range
[i
].end
;
347 ranges_vector
.push_back(range
);
353 case GTK_PRINT_PAGES_SELECTION
:
354 print_selection_only
= true;
356 case GTK_PRINT_PAGES_ALL
:
357 // Leave |ranges_vector| empty to indicate print all pages.
359 case GTK_PRINT_PAGES_CURRENT
:
365 PrintSettings settings
;
366 settings
.set_ranges(ranges_vector
);
367 settings
.set_selection_only(print_selection_only
);
368 printing::PrintSettingsInitializerGtk::InitPrintSettings(
369 gtk_settings_
, page_setup_
, &settings
);
370 context_
->InitWithSettings(settings
);
371 callback_
.Run(PrintingContextLinux::OK
);
375 case GTK_RESPONSE_DELETE_EVENT
: // Fall through.
376 case GTK_RESPONSE_CANCEL
: {
377 callback_
.Run(PrintingContextLinux::CANCEL
);
381 case GTK_RESPONSE_APPLY
:
388 void PrintDialogGtk::SendDocumentToPrinter(
389 const base::string16
& document_name
) {
390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
392 // If |printer_| is NULL then somehow the GTK printer list changed out under
393 // us. In which case, just bail out.
395 // Matches AddRef() in PrintDocument();
400 // Save the settings for next time.
401 g_last_used_settings
.Get().SetLastUsedSettings(gtk_settings_
);
403 GtkPrintJob
* print_job
= gtk_print_job_new(
404 base::UTF16ToUTF8(document_name
).c_str(),
408 gtk_print_job_set_source_file(print_job
, path_to_pdf_
.value().c_str(), NULL
);
409 gtk_print_job_send(print_job
, OnJobCompletedThunk
, this, NULL
);
413 void PrintDialogGtk::OnJobCompletedThunk(GtkPrintJob
* print_job
,
416 static_cast<PrintDialogGtk
*>(user_data
)->OnJobCompleted(print_job
, error
);
419 void PrintDialogGtk::OnJobCompleted(GtkPrintJob
* print_job
, GError
* error
) {
421 LOG(ERROR
) << "Printing failed: " << error
->message
;
423 g_object_unref(print_job
);
424 base::FileUtilProxy::DeleteFile(
425 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(),
428 base::FileUtilProxy::StatusCallback());
429 // Printing finished. Matches AddRef() in PrintDocument();
433 void PrintDialogGtk::InitPrintSettings(PrintSettings
* settings
) {
434 printing::PrintSettingsInitializerGtk::InitPrintSettings(
435 gtk_settings_
, page_setup_
, settings
);
436 context_
->InitWithSettings(*settings
);