1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "nsPrintSettingsServiceGTK.h"
7 #include "nsPrintSettingsGTK.h"
8 
9 using namespace mozilla::embedding;
10 
serialize_gtk_printsettings_to_printdata(const gchar * key,const gchar * value,gpointer aData)11 static void serialize_gtk_printsettings_to_printdata(const gchar* key,
12                                                      const gchar* value,
13                                                      gpointer aData) {
14   PrintData* data = (PrintData*)aData;
15   CStringKeyValue pair;
16   pair.key() = key;
17   pair.value() = value;
18   data->GTKPrintSettings().AppendElement(pair);
19 }
20 
21 NS_IMETHODIMP
SerializeToPrintData(nsIPrintSettings * aSettings,PrintData * data)22 nsPrintSettingsServiceGTK::SerializeToPrintData(nsIPrintSettings* aSettings,
23                                                 PrintData* data) {
24   nsresult rv = nsPrintSettingsService::SerializeToPrintData(aSettings, data);
25   NS_ENSURE_SUCCESS(rv, rv);
26 
27   nsCOMPtr<nsPrintSettingsGTK> settingsGTK(do_QueryInterface(aSettings));
28   NS_ENSURE_STATE(settingsGTK);
29 
30   GtkPrintSettings* gtkPrintSettings = settingsGTK->GetGtkPrintSettings();
31   NS_ENSURE_STATE(gtkPrintSettings);
32 
33   gtk_print_settings_foreach(gtkPrintSettings,
34                              serialize_gtk_printsettings_to_printdata, data);
35 
36   return NS_OK;
37 }
38 
39 NS_IMETHODIMP
DeserializeToPrintSettings(const PrintData & data,nsIPrintSettings * settings)40 nsPrintSettingsServiceGTK::DeserializeToPrintSettings(
41     const PrintData& data, nsIPrintSettings* settings) {
42   nsCOMPtr<nsPrintSettingsGTK> settingsGTK(do_QueryInterface(settings));
43   NS_ENSURE_STATE(settingsGTK);
44 
45   nsresult rv =
46       nsPrintSettingsService::DeserializeToPrintSettings(data, settings);
47   NS_ENSURE_SUCCESS(rv, rv);
48 
49   // Instead of re-using the GtkPrintSettings that nsIPrintSettings is
50   // wrapping, we'll create a new one to deserialize to and replace it
51   // within nsIPrintSettings.
52   GtkPrintSettings* newGtkPrintSettings = gtk_print_settings_new();
53 
54   for (uint32_t i = 0; i < data.GTKPrintSettings().Length(); ++i) {
55     CStringKeyValue pair = data.GTKPrintSettings()[i];
56     gtk_print_settings_set(newGtkPrintSettings, pair.key().get(),
57                            pair.value().get());
58   }
59 
60   settingsGTK->SetGtkPrintSettings(newGtkPrintSettings);
61 
62   // nsPrintSettingsGTK is holding a reference to newGtkPrintSettings
63   g_object_unref(newGtkPrintSettings);
64   newGtkPrintSettings = nullptr;
65   return NS_OK;
66 }
67 
_CreatePrintSettings(nsIPrintSettings ** _retval)68 nsresult nsPrintSettingsServiceGTK::_CreatePrintSettings(
69     nsIPrintSettings** _retval) {
70   *_retval = nullptr;
71   nsPrintSettingsGTK* printSettings =
72       new nsPrintSettingsGTK();  // does not initially ref count
73   NS_ENSURE_TRUE(printSettings, NS_ERROR_OUT_OF_MEMORY);
74 
75   NS_ADDREF(*_retval = printSettings);  // ref count
76 
77   return NS_OK;
78 }
79