2006-02-07 04:12:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
#include "nsDeviceContextSpecG.h"
|
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
#include "mozilla/gfx/PrintTargetPDF.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2020-05-01 06:02:13 +03:00
|
|
|
#include "mozilla/Services.h"
|
2022-04-26 06:36:32 +03:00
|
|
|
#include "mozilla/GUniquePtr.h"
|
2022-01-05 17:55:56 +03:00
|
|
|
#include "mozilla/WidgetUtilsGtk.h"
|
2006-02-07 04:14:36 +03:00
|
|
|
|
2006-02-07 04:14:01 +03:00
|
|
|
#include "plstr.h"
|
1999-04-01 02:55:29 +04:00
|
|
|
#include "prenv.h" /* for PR_GetEnv */
|
1999-04-01 00:17:01 +04:00
|
|
|
|
2020-05-01 06:02:13 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIObserverService.h"
|
2006-02-07 04:13:51 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2022-05-20 20:33:21 +03:00
|
|
|
#include "nsQueryObject.h"
|
2006-02-07 04:13:53 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2016-07-22 20:23:07 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2020-08-06 01:15:18 +03:00
|
|
|
#include "nsCUPSShim.h"
|
|
|
|
#include "nsPrinterCUPS.h"
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
#include "nsPrintSettingsGTK.h"
|
2006-11-30 02:40:16 +03:00
|
|
|
|
2007-03-01 03:02:27 +03:00
|
|
|
#include "nsIFileStreams.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2009-01-18 23:14:14 +03:00
|
|
|
#include "nsTArray.h"
|
2016-07-11 23:08:43 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2007-03-01 03:02:27 +03:00
|
|
|
|
2011-05-27 12:15:20 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2020-10-27 19:23:26 +03:00
|
|
|
#include "mozilla/StaticPrefs_print.h"
|
2011-05-27 12:15:20 +04:00
|
|
|
|
2021-02-23 03:41:48 +03:00
|
|
|
#include <dlfcn.h>
|
2008-01-21 06:47:25 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2021-05-26 11:25:23 +03:00
|
|
|
#include <fcntl.h>
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2018-04-16 13:28:51 +03:00
|
|
|
// To check if we need to use flatpak portal for printing
|
|
|
|
#include "nsIGIOService.h"
|
|
|
|
|
2011-05-27 12:15:20 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
using mozilla::gfx::IntSize;
|
2016-06-03 13:27:31 +03:00
|
|
|
using mozilla::gfx::PrintTarget;
|
2016-06-06 18:10:28 +03:00
|
|
|
using mozilla::gfx::PrintTargetPDF;
|
2016-06-03 13:27:31 +03:00
|
|
|
|
2006-02-07 04:13:27 +03:00
|
|
|
nsDeviceContextSpecGTK::nsDeviceContextSpecGTK()
|
2020-07-05 22:00:43 +03:00
|
|
|
: mGtkPrintSettings(nullptr), mGtkPageSetup(nullptr) {}
|
1999-02-19 01:35:11 +03:00
|
|
|
|
2006-02-07 04:13:27 +03:00
|
|
|
nsDeviceContextSpecGTK::~nsDeviceContextSpecGTK() {
|
2008-02-13 14:03:43 +03:00
|
|
|
if (mGtkPageSetup) {
|
|
|
|
g_object_unref(mGtkPageSetup);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGtkPrintSettings) {
|
|
|
|
g_object_unref(mGtkPrintSettings);
|
|
|
|
}
|
2020-09-10 00:34:41 +03:00
|
|
|
|
|
|
|
if (mSpoolFile) {
|
|
|
|
mSpoolFile->Remove(false);
|
|
|
|
}
|
1999-02-19 01:35:11 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDeviceContextSpecGTK, nsIDeviceContextSpec)
|
2006-02-07 04:12:13 +03:00
|
|
|
|
2016-06-03 13:27:31 +03:00
|
|
|
already_AddRefed<PrintTarget> nsDeviceContextSpecGTK::MakePrintTarget() {
|
2007-07-17 03:16:48 +04:00
|
|
|
double width, height;
|
Bug 1669905 part 2: Add nsIPrintSettings APIs to handle the possibility that pages and sheets may have orthogonal orientation, in pages-per-sheet printouts. r=jfkthame
This patch does three things, all centered around these new APIs:
(1) Add the APIs themselves -- some nsIPrintSettings methods to reason about
the sheet-orientation being potentially flipped to accomodate 2 and 6
pages-per-sheet.
(2) Use these new APIs, where appropriate, in places where we previously used
the page-orientation to set up the platform-native print-settings objects and
print-target. Now, we'll use the *sheet* orientation instead of the page
orientation, to be sure we produce the appropriately-oriented platform-native
surfaces. Also, for symmetry, this patch adds similar logic to the reverse
codepaths, where we update an existing nsIPrintSettings object based on a
platform-native print-settings object.
(3) Update nsPrintJob's code that informs nsPresContext about the page-size.
This patch makes sure that this code uses the *page* size, rather than the
*sheet* size, in cases where they differ. (The code that consumes this
nsPresContext::GetPageSize API, e.g. our CSS media-query code, really does want
the page size, not the sheet size.)
Differential Revision: https://phabricator.services.mozilla.com/D100372
2021-01-05 05:24:00 +03:00
|
|
|
mPrintSettings->GetEffectiveSheetSize(&width, &height);
|
2008-04-08 04:32:48 +04:00
|
|
|
|
2006-02-10 23:44:48 +03:00
|
|
|
// convert twips to points
|
2008-03-19 23:51:42 +03:00
|
|
|
width /= TWIPS_PER_POINT_FLOAT;
|
|
|
|
height /= TWIPS_PER_POINT_FLOAT;
|
2006-02-10 23:44:48 +03:00
|
|
|
|
2015-03-06 00:05:33 +03:00
|
|
|
// We shouldn't be attempting to get a surface if we've already got a spool
|
|
|
|
// file.
|
|
|
|
MOZ_ASSERT(!mSpoolFile);
|
|
|
|
|
2022-03-30 21:51:58 +03:00
|
|
|
auto stream = [&]() -> nsCOMPtr<nsIOutputStream> {
|
|
|
|
if (mPrintSettings->GetOutputDestination() ==
|
|
|
|
nsIPrintSettings::kOutputDestinationStream) {
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
mPrintSettings->GetOutputStream(getter_AddRefs(out));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
// Spool file. Use Glib's temporary file function since we're
|
|
|
|
// already dependent on the gtk software stack.
|
|
|
|
gchar* buf;
|
|
|
|
gint fd = g_file_open_tmp("XXXXXX.tmp", &buf, nullptr);
|
|
|
|
if (-1 == fd) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if (NS_FAILED(NS_NewNativeLocalFile(nsDependentCString(buf), false,
|
|
|
|
getter_AddRefs(mSpoolFile)))) {
|
|
|
|
unlink(buf);
|
|
|
|
g_free(buf);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mSpoolName = buf;
|
2020-04-09 18:11:02 +03:00
|
|
|
g_free(buf);
|
2022-03-30 21:51:58 +03:00
|
|
|
mSpoolFile->SetPermissions(0600);
|
|
|
|
nsCOMPtr<nsIFileOutputStream> stream =
|
|
|
|
do_CreateInstance("@mozilla.org/network/file-output-stream;1");
|
|
|
|
if (NS_FAILED(stream->Init(mSpoolFile, -1, -1, 0))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}();
|
|
|
|
|
2021-12-30 14:41:28 +03:00
|
|
|
return PrintTargetPDF::CreateOrNull(stream, IntSize::Ceil(width, height));
|
2006-02-10 23:44:48 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 19:23:26 +03:00
|
|
|
#define DECLARE_KNOWN_MONOCHROME_SETTING(key_, value_) {"cups-" key_, value_},
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char* mKey;
|
|
|
|
const char* mValue;
|
|
|
|
} kKnownMonochromeSettings[] = {
|
2020-11-04 21:46:00 +03:00
|
|
|
CUPS_EACH_MONOCHROME_PRINTER_SETTING(DECLARE_KNOWN_MONOCHROME_SETTING)};
|
2020-10-27 19:23:26 +03:00
|
|
|
|
|
|
|
#undef DECLARE_KNOWN_MONOCHROME_SETTING
|
|
|
|
|
2021-02-23 03:41:48 +03:00
|
|
|
// https://developer.gnome.org/gtk3/stable/GtkPaperSize.html#gtk-paper-size-new-from-ipp
|
2022-04-26 06:36:32 +03:00
|
|
|
static GUniquePtr<GtkPaperSize> GtkPaperSizeFromIpp(const gchar* aIppName,
|
|
|
|
gdouble aWidth,
|
|
|
|
gdouble aHeight) {
|
2021-02-23 03:41:48 +03:00
|
|
|
static auto sPtr = (GtkPaperSize * (*)(const gchar*, gdouble, gdouble))
|
|
|
|
dlsym(RTLD_DEFAULT, "gtk_paper_size_new_from_ipp");
|
|
|
|
if (gtk_check_version(3, 16, 0)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-26 06:36:32 +03:00
|
|
|
return GUniquePtr<GtkPaperSize>(sPtr(aIppName, aWidth, aHeight));
|
2021-02-23 03:41:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool PaperSizeAlmostEquals(GtkPaperSize* aSize,
|
|
|
|
GtkPaperSize* aOtherSize) {
|
|
|
|
const double kEpsilon = 1.0; // millimetres
|
|
|
|
// GTK stores sizes internally in millimetres so just use that.
|
|
|
|
if (fabs(gtk_paper_size_get_height(aSize, GTK_UNIT_MM) -
|
|
|
|
gtk_paper_size_get_height(aOtherSize, GTK_UNIT_MM)) > kEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fabs(gtk_paper_size_get_width(aSize, GTK_UNIT_MM) -
|
|
|
|
gtk_paper_size_get_width(aOtherSize, GTK_UNIT_MM)) > kEpsilon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-26 06:36:32 +03:00
|
|
|
// Prefer the ppd name because some printers don't deal well even with standard
|
|
|
|
// ipp names.
|
|
|
|
static GUniquePtr<GtkPaperSize> PpdSizeFromIppName(const gchar* aIppName) {
|
|
|
|
static constexpr struct {
|
|
|
|
const char* mCups;
|
|
|
|
const char* mGtk;
|
|
|
|
} kMap[] = {
|
|
|
|
{CUPS_MEDIA_A3, GTK_PAPER_NAME_A3},
|
|
|
|
{CUPS_MEDIA_A4, GTK_PAPER_NAME_A4},
|
|
|
|
{CUPS_MEDIA_A5, GTK_PAPER_NAME_A5},
|
|
|
|
{CUPS_MEDIA_LETTER, GTK_PAPER_NAME_LETTER},
|
|
|
|
{CUPS_MEDIA_LEGAL, GTK_PAPER_NAME_LEGAL},
|
|
|
|
// Other gtk sizes with no standard CUPS constant: _EXECUTIVE and _B5
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& entry : kMap) {
|
|
|
|
if (!strcmp(entry.mCups, aIppName)) {
|
|
|
|
return GUniquePtr<GtkPaperSize>(gtk_paper_size_new(entry.mGtk));
|
|
|
|
}
|
2021-02-23 03:41:48 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 06:36:32 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a horrible workaround for some printer driver bugs that treat custom
|
|
|
|
// page sizes different to standard ones. If our paper object matches one of a
|
|
|
|
// standard one, use a standard paper size object instead.
|
|
|
|
//
|
|
|
|
// We prefer ppd to ipp to custom sizes.
|
|
|
|
//
|
|
|
|
// See bug 414314, bug 1691798, and bug 1717292 for more info.
|
|
|
|
static GUniquePtr<GtkPaperSize> GetStandardGtkPaperSize(
|
|
|
|
GtkPaperSize* aGeckoPaperSize) {
|
|
|
|
// We should get an ipp name from cups, try to get a ppd from that first.
|
|
|
|
const gchar* geckoName = gtk_paper_size_get_name(aGeckoPaperSize);
|
|
|
|
if (auto ppd = PpdSizeFromIppName(geckoName)) {
|
|
|
|
return ppd;
|
2021-02-23 03:41:48 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 06:36:32 +03:00
|
|
|
// We try gtk_paper_size_new_from_ipp next, because even though
|
|
|
|
// gtk_paper_size_new tries to deal with ipp, it has some rounding issues that
|
|
|
|
// the ipp equivalent doesn't have, see
|
|
|
|
// https://gitlab.gnome.org/GNOME/gtk/-/issues/3685.
|
|
|
|
if (auto ipp = GtkPaperSizeFromIpp(
|
|
|
|
geckoName, gtk_paper_size_get_width(aGeckoPaperSize, GTK_UNIT_POINTS),
|
|
|
|
gtk_paper_size_get_height(aGeckoPaperSize, GTK_UNIT_POINTS))) {
|
|
|
|
if (!gtk_paper_size_is_custom(ipp.get())) {
|
|
|
|
if (auto ppd = PpdSizeFromIppName(gtk_paper_size_get_name(ipp.get()))) {
|
|
|
|
return ppd;
|
|
|
|
}
|
|
|
|
return ipp;
|
|
|
|
}
|
2021-02-23 03:41:48 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 06:36:32 +03:00
|
|
|
GUniquePtr<GtkPaperSize> size(gtk_paper_size_new(geckoName));
|
2021-02-23 03:41:48 +03:00
|
|
|
// gtk_paper_size_is_equal compares just paper names. The name in Gecko
|
|
|
|
// might come from CUPS, which is an ipp size, and gets normalized by gtk.
|
|
|
|
// So check also for the same actual paper size.
|
2022-04-26 06:36:32 +03:00
|
|
|
if (gtk_paper_size_is_equal(size.get(), aGeckoPaperSize) ||
|
|
|
|
PaperSizeAlmostEquals(aGeckoPaperSize, size.get())) {
|
2021-02-23 03:41:48 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-04-26 06:36:32 +03:00
|
|
|
// Not the same after all, so use our custom paper sizes instead.
|
2021-02-23 03:41:48 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
1999-02-19 01:35:11 +03:00
|
|
|
/** -------------------------------------------------------
|
|
|
|
* Initialize the nsDeviceContextSpecGTK
|
|
|
|
* @update dc 2/15/98
|
1999-03-13 06:21:16 +03:00
|
|
|
* @update syd 3/2/99
|
1999-02-19 01:35:11 +03:00
|
|
|
*/
|
2006-12-09 20:18:56 +03:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecGTK::Init(nsIWidget* aWidget,
|
|
|
|
nsIPrintSettings* aPS,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsPrintPreview) {
|
2022-05-20 20:33:21 +03:00
|
|
|
RefPtr<nsPrintSettingsGTK> settings = do_QueryObject(aPS);
|
|
|
|
if (!settings) {
|
2021-02-23 03:41:48 +03:00
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
2022-05-20 20:33:21 +03:00
|
|
|
mPrintSettings = aPS;
|
2015-03-06 00:05:33 +03:00
|
|
|
|
2022-05-20 20:33:21 +03:00
|
|
|
mGtkPrintSettings = settings->GetGtkPrintSettings();
|
|
|
|
mGtkPageSetup = settings->GetGtkPageSetup();
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2021-02-23 03:41:48 +03:00
|
|
|
GtkPaperSize* geckoPaperSize = gtk_page_setup_get_paper_size(mGtkPageSetup);
|
2022-04-26 06:36:32 +03:00
|
|
|
GUniquePtr<GtkPaperSize> gtkPaperSize =
|
|
|
|
GetStandardGtkPaperSize(geckoPaperSize);
|
2008-02-13 14:03:43 +03:00
|
|
|
|
|
|
|
mGtkPageSetup = gtk_page_setup_copy(mGtkPageSetup);
|
|
|
|
mGtkPrintSettings = gtk_print_settings_copy(mGtkPrintSettings);
|
|
|
|
|
2020-10-27 19:23:26 +03:00
|
|
|
if (!aPS->GetPrintInColor() && StaticPrefs::print_cups_monochrome_enabled()) {
|
|
|
|
for (const auto& setting : kKnownMonochromeSettings) {
|
|
|
|
gtk_print_settings_set(mGtkPrintSettings, setting.mKey, setting.mValue);
|
|
|
|
}
|
2020-11-11 22:00:00 +03:00
|
|
|
auto applySetting = [&](const nsACString& aKey, const nsACString& aVal) {
|
|
|
|
nsAutoCString extra;
|
|
|
|
extra.AppendASCII("cups-");
|
|
|
|
extra.Append(aKey);
|
|
|
|
gtk_print_settings_set(mGtkPrintSettings, extra.get(),
|
|
|
|
nsAutoCString(aVal).get());
|
|
|
|
};
|
|
|
|
nsPrinterCUPS::ForEachExtraMonochromeSetting(applySetting);
|
2020-10-27 19:23:26 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 06:36:32 +03:00
|
|
|
GtkPaperSize* properPaperSize =
|
|
|
|
gtkPaperSize ? gtkPaperSize.get() : geckoPaperSize;
|
2008-02-13 14:03:43 +03:00
|
|
|
gtk_print_settings_set_paper_size(mGtkPrintSettings, properPaperSize);
|
|
|
|
gtk_page_setup_set_paper_size_and_default_margins(mGtkPageSetup,
|
|
|
|
properPaperSize);
|
2008-01-21 06:47:25 +03:00
|
|
|
return NS_OK;
|
1999-02-19 01:35:11 +03:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:35:34 +03:00
|
|
|
static void print_callback(GtkPrintJob* aJob, gpointer aData,
|
|
|
|
const GError* aError) {
|
2008-01-21 06:47:25 +03:00
|
|
|
g_object_unref(aJob);
|
2012-06-06 06:08:30 +04:00
|
|
|
((nsIFile*)aData)->Remove(false);
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
|
2015-03-06 00:05:33 +03:00
|
|
|
/* static */
|
|
|
|
gboolean nsDeviceContextSpecGTK::PrinterEnumerator(GtkPrinter* aPrinter,
|
|
|
|
gpointer aData) {
|
|
|
|
nsDeviceContextSpecGTK* spec = (nsDeviceContextSpecGTK*)aData;
|
|
|
|
|
|
|
|
// Find the printer whose name matches the one inside the settings.
|
2017-08-08 09:07:55 +03:00
|
|
|
nsString printerName;
|
2017-10-09 02:08:09 +03:00
|
|
|
nsresult rv = spec->mPrintSettings->GetPrinterName(printerName);
|
2017-08-08 09:07:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && !printerName.IsVoid()) {
|
2015-03-06 00:05:33 +03:00
|
|
|
NS_ConvertUTF16toUTF8 requestedName(printerName);
|
|
|
|
const char* currentName = gtk_printer_get_name(aPrinter);
|
|
|
|
if (requestedName.Equals(currentName)) {
|
2022-05-20 20:33:21 +03:00
|
|
|
nsPrintSettingsGTK::From(spec->mPrintSettings)->SetGtkPrinter(aPrinter);
|
2015-07-28 00:11:46 +03:00
|
|
|
|
|
|
|
// Bug 1145916 - attempting to kick off a print job for this printer
|
|
|
|
// during this tick of the event loop will result in the printer backend
|
|
|
|
// misunderstanding what the capabilities of the printer are due to a
|
|
|
|
// GTK bug (https://bugzilla.gnome.org/show_bug.cgi?id=753041). We
|
|
|
|
// sidestep this by deferring the print to the next tick.
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NewRunnableMethod("nsDeviceContextSpecGTK::StartPrintJob", spec,
|
|
|
|
&nsDeviceContextSpecGTK::StartPrintJob));
|
2015-03-06 00:05:33 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We haven't found it yet - keep searching...
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:11:46 +03:00
|
|
|
void nsDeviceContextSpecGTK::StartPrintJob() {
|
2022-05-20 20:33:21 +03:00
|
|
|
GtkPrintJob* job = gtk_print_job_new(
|
|
|
|
mTitle.get(), nsPrintSettingsGTK::From(mPrintSettings)->GetGtkPrinter(),
|
|
|
|
mGtkPrintSettings, mGtkPageSetup);
|
2022-02-28 13:47:50 +03:00
|
|
|
|
|
|
|
if (!gtk_print_job_set_source_file(job, mSpoolName.get(), nullptr)) return;
|
|
|
|
|
|
|
|
// Now gtk owns the print job, and will be released via our callback.
|
|
|
|
gtk_print_job_send(job, print_callback, mSpoolFile.forget().take(),
|
|
|
|
[](gpointer aData) {
|
|
|
|
auto* spoolFile = static_cast<nsIFile*>(aData);
|
|
|
|
NS_RELEASE(spoolFile);
|
|
|
|
});
|
2015-03-06 00:05:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsDeviceContextSpecGTK::EnumeratePrinters() {
|
|
|
|
gtk_enumerate_printers(&nsDeviceContextSpecGTK::PrinterEnumerator, this,
|
|
|
|
nullptr, TRUE);
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:57 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDeviceContextSpecGTK::BeginDocument(const nsAString& aTitle,
|
|
|
|
const nsAString& aPrintToFileName,
|
|
|
|
int32_t aStartPage, int32_t aEndPage) {
|
2017-10-13 09:48:39 +03:00
|
|
|
// Print job names exceeding 255 bytes are safe with GTK version 3.18.2 or
|
|
|
|
// newer. This is a workaround for old GTK.
|
|
|
|
if (gtk_check_version(3, 18, 2) != nullptr) {
|
|
|
|
PrintTarget::AdjustPrintJobNameForIPP(aTitle, mTitle);
|
|
|
|
} else {
|
|
|
|
CopyUTF16toUTF8(aTitle, mTitle);
|
|
|
|
}
|
|
|
|
|
2006-11-30 02:40:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-06-18 00:17:23 +04:00
|
|
|
|
2006-11-30 02:40:16 +03:00
|
|
|
NS_IMETHODIMP nsDeviceContextSpecGTK::EndDocument() {
|
2022-03-30 21:51:58 +03:00
|
|
|
switch (mPrintSettings->GetOutputDestination()) {
|
|
|
|
case nsIPrintSettings::kOutputDestinationPrinter: {
|
|
|
|
// At this point, we might have a GtkPrinter set up in nsPrintSettingsGTK,
|
|
|
|
// or we might not. In the single-process case, we probably will, as this
|
|
|
|
// is populated by the print settings dialog, or set to the default
|
|
|
|
// printer.
|
|
|
|
// In the multi-process case, we proxy the print settings dialog over to
|
|
|
|
// the parent process, and only get the name of the printer back on the
|
|
|
|
// content process side. In that case, we need to enumerate the printers
|
|
|
|
// on the content side, and find a printer with a matching name.
|
|
|
|
|
2022-05-20 20:33:21 +03:00
|
|
|
if (nsPrintSettingsGTK::From(mPrintSettings)->GetGtkPrinter()) {
|
2022-03-30 21:51:58 +03:00
|
|
|
// We have a printer, so we can print right away.
|
|
|
|
StartPrintJob();
|
|
|
|
} else {
|
|
|
|
// We don't have a printer. We have to enumerate the printers and find
|
|
|
|
// one with a matching name.
|
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NewRunnableMethod("nsDeviceContextSpecGTK::EnumeratePrinters", this,
|
|
|
|
&nsDeviceContextSpecGTK::EnumeratePrinters));
|
|
|
|
}
|
|
|
|
break;
|
2022-03-29 20:50:58 +03:00
|
|
|
}
|
2022-03-30 21:51:58 +03:00
|
|
|
case nsIPrintSettings::kOutputDestinationFile: {
|
|
|
|
// Handle print-to-file ourselves for the benefit of embedders
|
|
|
|
nsString targetPath;
|
|
|
|
nsCOMPtr<nsIFile> destFile;
|
|
|
|
mPrintSettings->GetToFileName(targetPath);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
NS_NewLocalFile(targetPath, false, getter_AddRefs(destFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString destLeafName;
|
|
|
|
rv = destFile->GetLeafName(destLeafName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> destDir;
|
|
|
|
rv = destFile->GetParent(getter_AddRefs(destDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mSpoolFile->MoveTo(destDir, destLeafName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mSpoolFile = nullptr;
|
|
|
|
|
|
|
|
// This is the standard way to get the UNIX umask. Ugh.
|
|
|
|
mode_t mask = umask(0);
|
|
|
|
umask(mask);
|
|
|
|
// If you're not familiar with umasks, they contain the bits of what NOT
|
|
|
|
// to set in the permissions (thats because files and directories have
|
|
|
|
// different numbers of bits for their permissions)
|
|
|
|
destFile->SetPermissions(0666 & ~(mask));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIPrintSettings::kOutputDestinationStream:
|
|
|
|
// Nothing to do, handled in MakePrintTarget.
|
|
|
|
MOZ_ASSERT(!mSpoolFile);
|
|
|
|
break;
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2006-11-30 02:40:16 +03:00
|
|
|
}
|