1 // Copyright 2014 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/ui/libgtk2ui/print_dialog_gtk2.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 "chrome/browser/ui/libgtk2ui/gtk2_util.h"
21 #include "chrome/browser/ui/libgtk2ui/printing_gtk2_util.h"
22 #include "printing/metafile.h"
23 #include "printing/print_job_constants.h"
24 #include "printing/print_settings.h"
25 #include "ui/aura/window.h"
27 using content::BrowserThread
;
28 using printing::PageRanges
;
29 using printing::PrintSettings
;
33 // CUPS Duplex attribute and values.
34 const char kCUPSDuplex
[] = "cups-Duplex";
35 const char kDuplexNone
[] = "None";
36 const char kDuplexTumble
[] = "DuplexTumble";
37 const char kDuplexNoTumble
[] = "DuplexNoTumble";
39 class StickyPrintSettingGtk
{
41 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {
43 ~StickyPrintSettingGtk() {
44 NOTREACHED(); // Intended to be used with a Leaky LazyInstance.
47 GtkPrintSettings
* settings() {
48 return last_used_settings_
;
51 void SetLastUsedSettings(GtkPrintSettings
* settings
) {
52 DCHECK(last_used_settings_
);
53 g_object_unref(last_used_settings_
);
54 last_used_settings_
= gtk_print_settings_copy(settings
);
58 GtkPrintSettings
* last_used_settings_
;
60 DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk
);
63 base::LazyInstance
<StickyPrintSettingGtk
>::Leaky g_last_used_settings
=
64 LAZY_INSTANCE_INITIALIZER
;
66 // Helper class to track GTK printers.
67 class GtkPrinterList
{
69 GtkPrinterList() : default_printer_(NULL
) {
70 gtk_enumerate_printers(SetPrinter
, this, NULL
, TRUE
);
74 for (std::vector
<GtkPrinter
*>::iterator it
= printers_
.begin();
75 it
< printers_
.end(); ++it
) {
80 // Can return NULL if there's no default printer. E.g. Printer on a laptop
81 // is "home_printer", but the laptop is at work.
82 GtkPrinter
* default_printer() {
83 return default_printer_
;
86 // Can return NULL if the printer cannot be found due to:
87 // - Printer list out of sync with printer dialog UI.
88 // - Querying for non-existant printers like 'Print to PDF'.
89 GtkPrinter
* GetPrinterWithName(const std::string
& name
) {
93 for (std::vector
<GtkPrinter
*>::iterator it
= printers_
.begin();
94 it
< printers_
.end(); ++it
) {
95 if (gtk_printer_get_name(*it
) == name
) {
104 // Callback function used by gtk_enumerate_printers() to get all printer.
105 static gboolean
SetPrinter(GtkPrinter
* printer
, gpointer data
) {
106 GtkPrinterList
* printer_list
= reinterpret_cast<GtkPrinterList
*>(data
);
107 if (gtk_printer_is_default(printer
))
108 printer_list
->default_printer_
= printer
;
110 g_object_ref(printer
);
111 printer_list
->printers_
.push_back(printer
);
116 std::vector
<GtkPrinter
*> printers_
;
117 GtkPrinter
* default_printer_
;
123 printing::PrintDialogGtkInterface
* PrintDialogGtk2::CreatePrintDialog(
124 PrintingContextLinux
* context
) {
125 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
126 return new PrintDialogGtk2(context
);
129 PrintDialogGtk2::PrintDialogGtk2(PrintingContextLinux
* context
)
137 PrintDialogGtk2::~PrintDialogGtk2() {
138 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
141 aura::Window
* parent
= libgtk2ui::GetAuraTransientParent(dialog_
);
143 parent
->RemoveObserver(this);
144 libgtk2ui::ClearAuraTransientParent(dialog_
);
146 gtk_widget_destroy(dialog_
);
150 g_object_unref(gtk_settings_
);
151 gtk_settings_
= NULL
;
154 g_object_unref(page_setup_
);
158 g_object_unref(printer_
);
163 void PrintDialogGtk2::UseDefaultSettings() {
164 DCHECK(!page_setup_
);
167 // |gtk_settings_| is a new copy.
169 gtk_print_settings_copy(g_last_used_settings
.Get().settings());
170 page_setup_
= gtk_page_setup_new();
172 PrintSettings settings
;
173 InitPrintSettings(&settings
);
176 bool PrintDialogGtk2::UpdateSettings(printing::PrintSettings
* settings
) {
177 if (!gtk_settings_
) {
179 gtk_print_settings_copy(g_last_used_settings
.Get().settings());
182 scoped_ptr
<GtkPrinterList
> printer_list(new GtkPrinterList
);
183 printer_
= printer_list
->GetPrinterWithName(
184 base::UTF16ToUTF8(settings
->device_name()));
186 g_object_ref(printer_
);
187 gtk_print_settings_set_printer(gtk_settings_
,
188 gtk_printer_get_name(printer_
));
190 page_setup_
= gtk_printer_get_default_page_size(printer_
);
194 gtk_print_settings_set_n_copies(gtk_settings_
, settings
->copies());
195 gtk_print_settings_set_collate(gtk_settings_
, settings
->collate());
197 #if defined(USE_CUPS)
198 std::string color_value
;
199 std::string color_setting_name
;
200 printing::GetColorModelForMode(settings
->color(), &color_setting_name
,
202 gtk_print_settings_set(gtk_settings_
, color_setting_name
.c_str(),
203 color_value
.c_str());
205 if (settings
->duplex_mode() != printing::UNKNOWN_DUPLEX_MODE
) {
206 const char* cups_duplex_mode
= NULL
;
207 switch (settings
->duplex_mode()) {
208 case printing::LONG_EDGE
:
209 cups_duplex_mode
= kDuplexNoTumble
;
211 case printing::SHORT_EDGE
:
212 cups_duplex_mode
= kDuplexTumble
;
214 case printing::SIMPLEX
:
215 cups_duplex_mode
= kDuplexNone
;
217 default: // UNKNOWN_DUPLEX_MODE
221 gtk_print_settings_set(gtk_settings_
, kCUPSDuplex
, cups_duplex_mode
);
225 page_setup_
= gtk_page_setup_new();
227 gtk_print_settings_set_orientation(
229 settings
->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE
:
230 GTK_PAGE_ORIENTATION_PORTRAIT
);
232 InitPrintSettings(settings
);
236 void PrintDialogGtk2::ShowDialog(
237 gfx::NativeView parent_view
,
239 const PrintingContextLinux::PrintSettingsCallback
& callback
) {
240 callback_
= callback
;
242 // TODO(mukai): take care of parent as select_file_dialog_impl_gtk2.
243 dialog_
= gtk_print_unix_dialog_new(NULL
, NULL
);
244 libgtk2ui::SetGtkTransientForAura(dialog_
, parent_view
);
245 parent_view
->AddObserver(this);
246 g_signal_connect(dialog_
, "delete-event",
247 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
250 // Set modal so user cannot focus the same tab and press print again.
251 gtk_window_set_modal(GTK_WINDOW(dialog_
), TRUE
);
253 // Since we only generate PDF, only show printers that support PDF.
254 // TODO(thestig) Add more capabilities to support?
255 GtkPrintCapabilities cap
= static_cast<GtkPrintCapabilities
>(
256 GTK_PRINT_CAPABILITY_GENERATE_PDF
|
257 GTK_PRINT_CAPABILITY_PAGE_SET
|
258 GTK_PRINT_CAPABILITY_COPIES
|
259 GTK_PRINT_CAPABILITY_COLLATE
|
260 GTK_PRINT_CAPABILITY_REVERSE
);
261 gtk_print_unix_dialog_set_manual_capabilities(GTK_PRINT_UNIX_DIALOG(dialog_
),
263 gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_
),
265 gtk_print_unix_dialog_set_support_selection(GTK_PRINT_UNIX_DIALOG(dialog_
),
267 gtk_print_unix_dialog_set_has_selection(GTK_PRINT_UNIX_DIALOG(dialog_
),
269 gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog_
),
271 g_signal_connect(dialog_
, "response", G_CALLBACK(OnResponseThunk
), this);
272 gtk_widget_show(dialog_
);
275 void PrintDialogGtk2::PrintDocument(const printing::Metafile
* metafile
,
276 const base::string16
& document_name
) {
277 // This runs on the print worker thread, does not block the UI thread.
278 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI
));
280 // The document printing tasks can outlive the PrintingContext that created
285 if (!base::CreateTemporaryFile(&path_to_pdf_
)) {
286 LOG(ERROR
) << "Creating temporary file failed";
290 if (!error
&& !metafile
->SaveTo(path_to_pdf_
)) {
291 LOG(ERROR
) << "Saving metafile failed";
292 base::DeleteFile(path_to_pdf_
, false);
297 // Matches AddRef() above.
300 // No errors, continue printing.
301 BrowserThread::PostTask(
302 BrowserThread::UI
, FROM_HERE
,
303 base::Bind(&PrintDialogGtk2::SendDocumentToPrinter
, this,
308 void PrintDialogGtk2::AddRefToDialog() {
312 void PrintDialogGtk2::ReleaseDialog() {
316 void PrintDialogGtk2::OnResponse(GtkWidget
* dialog
, int response_id
) {
317 int num_matched_handlers
= g_signal_handlers_disconnect_by_func(
318 dialog_
, reinterpret_cast<gpointer
>(&OnResponseThunk
), this);
319 CHECK_EQ(1, num_matched_handlers
);
321 gtk_widget_hide(dialog_
);
323 switch (response_id
) {
324 case GTK_RESPONSE_OK
: {
326 g_object_unref(gtk_settings_
);
327 gtk_settings_
= gtk_print_unix_dialog_get_settings(
328 GTK_PRINT_UNIX_DIALOG(dialog_
));
331 g_object_unref(printer_
);
332 printer_
= gtk_print_unix_dialog_get_selected_printer(
333 GTK_PRINT_UNIX_DIALOG(dialog_
));
334 g_object_ref(printer_
);
337 g_object_unref(page_setup_
);
338 page_setup_
= gtk_print_unix_dialog_get_page_setup(
339 GTK_PRINT_UNIX_DIALOG(dialog_
));
340 g_object_ref(page_setup_
);
342 // Handle page ranges.
343 PageRanges ranges_vector
;
345 bool print_selection_only
= false;
346 switch (gtk_print_settings_get_print_pages(gtk_settings_
)) {
347 case GTK_PRINT_PAGES_RANGES
: {
348 GtkPageRange
* gtk_range
=
349 gtk_print_settings_get_page_ranges(gtk_settings_
, &num_ranges
);
351 for (int i
= 0; i
< num_ranges
; ++i
) {
352 printing::PageRange range
;
353 range
.from
= gtk_range
[i
].start
;
354 range
.to
= gtk_range
[i
].end
;
355 ranges_vector
.push_back(range
);
361 case GTK_PRINT_PAGES_SELECTION
:
362 print_selection_only
= true;
364 case GTK_PRINT_PAGES_ALL
:
365 // Leave |ranges_vector| empty to indicate print all pages.
367 case GTK_PRINT_PAGES_CURRENT
:
373 PrintSettings settings
;
374 settings
.set_ranges(ranges_vector
);
375 settings
.set_selection_only(print_selection_only
);
376 InitPrintSettingsGtk(gtk_settings_
, page_setup_
, &settings
);
377 context_
->InitWithSettings(settings
);
378 callback_
.Run(PrintingContextLinux::OK
);
382 case GTK_RESPONSE_DELETE_EVENT
: // Fall through.
383 case GTK_RESPONSE_CANCEL
: {
384 callback_
.Run(PrintingContextLinux::CANCEL
);
388 case GTK_RESPONSE_APPLY
:
395 void PrintDialogGtk2::SendDocumentToPrinter(
396 const base::string16
& document_name
) {
397 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
399 // If |printer_| is NULL then somehow the GTK printer list changed out under
400 // us. In which case, just bail out.
402 // Matches AddRef() in PrintDocument();
407 // Save the settings for next time.
408 g_last_used_settings
.Get().SetLastUsedSettings(gtk_settings_
);
410 GtkPrintJob
* print_job
= gtk_print_job_new(
411 base::UTF16ToUTF8(document_name
).c_str(),
415 gtk_print_job_set_source_file(print_job
, path_to_pdf_
.value().c_str(), NULL
);
416 gtk_print_job_send(print_job
, OnJobCompletedThunk
, this, NULL
);
420 void PrintDialogGtk2::OnJobCompletedThunk(GtkPrintJob
* print_job
,
423 static_cast<PrintDialogGtk2
*>(user_data
)->OnJobCompleted(print_job
, error
);
426 void PrintDialogGtk2::OnJobCompleted(GtkPrintJob
* print_job
, GError
* error
) {
428 LOG(ERROR
) << "Printing failed: " << error
->message
;
430 g_object_unref(print_job
);
431 base::FileUtilProxy::DeleteFile(
432 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(),
435 base::FileUtilProxy::StatusCallback());
436 // Printing finished. Matches AddRef() in PrintDocument();
440 void PrintDialogGtk2::InitPrintSettings(PrintSettings
* settings
) {
441 InitPrintSettingsGtk(gtk_settings_
, page_setup_
, settings
);
442 context_
->InitWithSettings(*settings
);
445 void PrintDialogGtk2::OnWindowDestroying(aura::Window
* window
) {
446 DCHECK_EQ(libgtk2ui::GetAuraTransientParent(dialog_
), window
);
448 libgtk2ui::ClearAuraTransientParent(dialog_
);
449 window
->RemoveObserver(this);