Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / libgtk2ui / print_dialog_gtk2.cc
blob422c9cfeedee2ad12c601f7692a9bcd3af472eb0
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>
9 #include <string>
10 #include <vector>
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;
31 namespace {
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 {
40 public:
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);
57 private:
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 {
68 public:
69 GtkPrinterList() : default_printer_(NULL) {
70 gtk_enumerate_printers(SetPrinter, this, NULL, TRUE);
73 ~GtkPrinterList() {
74 for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
75 it < printers_.end(); ++it) {
76 g_object_unref(*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) {
90 if (name.empty())
91 return NULL;
93 for (std::vector<GtkPrinter*>::iterator it = printers_.begin();
94 it < printers_.end(); ++it) {
95 if (gtk_printer_get_name(*it) == name) {
96 return *it;
100 return NULL;
103 private:
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);
113 return FALSE;
116 std::vector<GtkPrinter*> printers_;
117 GtkPrinter* default_printer_;
120 } // namespace
122 // static
123 printing::PrintDialogGtkInterface* PrintDialogGtk2::CreatePrintDialog(
124 PrintingContextLinux* context) {
125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
126 return new PrintDialogGtk2(context);
129 PrintDialogGtk2::PrintDialogGtk2(PrintingContextLinux* context)
130 : context_(context),
131 dialog_(NULL),
132 gtk_settings_(NULL),
133 page_setup_(NULL),
134 printer_(NULL) {
137 PrintDialogGtk2::~PrintDialogGtk2() {
138 DCHECK_CURRENTLY_ON(BrowserThread::UI);
140 if (dialog_) {
141 aura::Window* parent = libgtk2ui::GetAuraTransientParent(dialog_);
142 if (parent) {
143 parent->RemoveObserver(this);
144 libgtk2ui::ClearAuraTransientParent(dialog_);
146 gtk_widget_destroy(dialog_);
147 dialog_ = NULL;
149 if (gtk_settings_) {
150 g_object_unref(gtk_settings_);
151 gtk_settings_ = NULL;
153 if (page_setup_) {
154 g_object_unref(page_setup_);
155 page_setup_ = NULL;
157 if (printer_) {
158 g_object_unref(printer_);
159 printer_ = NULL;
163 void PrintDialogGtk2::UseDefaultSettings() {
164 DCHECK(!page_setup_);
165 DCHECK(!printer_);
167 // |gtk_settings_| is a new copy.
168 gtk_settings_ =
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_) {
178 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()));
185 if (printer_) {
186 g_object_ref(printer_);
187 gtk_print_settings_set_printer(gtk_settings_,
188 gtk_printer_get_name(printer_));
189 if (!page_setup_) {
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,
201 &color_value);
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;
210 break;
211 case printing::SHORT_EDGE:
212 cups_duplex_mode = kDuplexTumble;
213 break;
214 case printing::SIMPLEX:
215 cups_duplex_mode = kDuplexNone;
216 break;
217 default: // UNKNOWN_DUPLEX_MODE
218 NOTREACHED();
219 break;
221 gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode);
223 #endif
224 if (!page_setup_)
225 page_setup_ = gtk_page_setup_new();
227 gtk_print_settings_set_orientation(
228 gtk_settings_,
229 settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE :
230 GTK_PAGE_ORIENTATION_PORTRAIT);
232 InitPrintSettings(settings);
233 return true;
236 void PrintDialogGtk2::ShowDialog(
237 gfx::NativeView parent_view,
238 bool has_selection,
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_),
262 cap);
263 gtk_print_unix_dialog_set_embed_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_),
264 TRUE);
265 gtk_print_unix_dialog_set_support_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
266 TRUE);
267 gtk_print_unix_dialog_set_has_selection(GTK_PRINT_UNIX_DIALOG(dialog_),
268 has_selection);
269 gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog_),
270 gtk_settings_);
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
281 // this dialog.
282 AddRef();
284 bool error = false;
285 if (!base::CreateTemporaryFile(&path_to_pdf_)) {
286 LOG(ERROR) << "Creating temporary file failed";
287 error = true;
290 if (!error && !metafile->SaveTo(path_to_pdf_)) {
291 LOG(ERROR) << "Saving metafile failed";
292 base::DeleteFile(path_to_pdf_, false);
293 error = true;
296 if (error) {
297 // Matches AddRef() above.
298 Release();
299 } else {
300 // No errors, continue printing.
301 BrowserThread::PostTask(
302 BrowserThread::UI, FROM_HERE,
303 base::Bind(&PrintDialogGtk2::SendDocumentToPrinter, this,
304 document_name));
308 void PrintDialogGtk2::AddRefToDialog() {
309 AddRef();
312 void PrintDialogGtk2::ReleaseDialog() {
313 Release();
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: {
325 if (gtk_settings_)
326 g_object_unref(gtk_settings_);
327 gtk_settings_ = gtk_print_unix_dialog_get_settings(
328 GTK_PRINT_UNIX_DIALOG(dialog_));
330 if (printer_)
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_);
336 if (page_setup_)
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;
344 gint num_ranges;
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);
350 if (gtk_range) {
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);
357 g_free(gtk_range);
359 break;
361 case GTK_PRINT_PAGES_SELECTION:
362 print_selection_only = true;
363 break;
364 case GTK_PRINT_PAGES_ALL:
365 // Leave |ranges_vector| empty to indicate print all pages.
366 break;
367 case GTK_PRINT_PAGES_CURRENT:
368 default:
369 NOTREACHED();
370 break;
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);
379 callback_.Reset();
380 return;
382 case GTK_RESPONSE_DELETE_EVENT: // Fall through.
383 case GTK_RESPONSE_CANCEL: {
384 callback_.Run(PrintingContextLinux::CANCEL);
385 callback_.Reset();
386 return;
388 case GTK_RESPONSE_APPLY:
389 default: {
390 NOTREACHED();
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.
401 if (!printer_) {
402 // Matches AddRef() in PrintDocument();
403 Release();
404 return;
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(),
412 printer_,
413 gtk_settings_,
414 page_setup_);
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);
419 // static
420 void PrintDialogGtk2::OnJobCompletedThunk(GtkPrintJob* print_job,
421 gpointer user_data,
422 GError* error) {
423 static_cast<PrintDialogGtk2*>(user_data)->OnJobCompleted(print_job, error);
426 void PrintDialogGtk2::OnJobCompleted(GtkPrintJob* print_job, GError* error) {
427 if (error)
428 LOG(ERROR) << "Printing failed: " << error->message;
429 if (print_job)
430 g_object_unref(print_job);
431 base::FileUtilProxy::DeleteFile(
432 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(),
433 path_to_pdf_,
434 false,
435 base::FileUtilProxy::StatusCallback());
436 // Printing finished. Matches AddRef() in PrintDocument();
437 Release();
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);
450 Release();