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"
|
2016-06-06 18:10:28 +03:00
|
|
|
#include "mozilla/gfx/PrintTargetPS.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.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
|
|
|
|
2006-02-07 04:13:51 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2006-02-07 04:13:53 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2007-05-06 15:29:41 +04:00
|
|
|
#include "nsStringEnumerator.h"
|
2017-12-19 13:38:59 +03:00
|
|
|
#include "nsIServiceManager.h"
|
2016-07-22 20:23:07 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2006-02-07 04:15:08 +03:00
|
|
|
#include "nsPSPrinters.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"
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
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-06 18:10:28 +03:00
|
|
|
using mozilla::gfx::PrintTargetPS;
|
2016-06-03 13:27:31 +03:00
|
|
|
|
2016-05-18 22:55:42 +03:00
|
|
|
static LazyLogModule sDeviceContextSpecGTKLog("DeviceContextSpecGTK");
|
2006-02-07 04:13:44 +03:00
|
|
|
/* Macro to make lines shorter */
|
2016-05-18 22:55:42 +03:00
|
|
|
#define DO_PR_DEBUG_LOG(x) \
|
|
|
|
MOZ_LOG(sDeviceContextSpecGTKLog, mozilla::LogLevel::Debug, x)
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2006-02-07 04:13:27 +03:00
|
|
|
// The printer data is shared between the PrinterEnumerator and the
|
2006-02-07 04:13:12 +03:00
|
|
|
// nsDeviceContextSpecGTK The PrinterEnumerator creates the printer info but the
|
2006-02-07 04:13:27 +03:00
|
|
|
// nsDeviceContextSpecGTK cleans it up If it gets created (via the Page Setup
|
2006-02-07 04:13:12 +03:00
|
|
|
// Dialog) but the user never prints anything then it will never be delete, so
|
|
|
|
// this class takes care of that.
|
|
|
|
class GlobalPrinters {
|
|
|
|
public:
|
|
|
|
static GlobalPrinters* GetInstance() { return &mGlobalPrinters; }
|
|
|
|
~GlobalPrinters() { FreeGlobalPrinters(); }
|
|
|
|
|
|
|
|
void FreeGlobalPrinters();
|
|
|
|
nsresult InitializeGlobalPrinters();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
bool PrintersAreAllocated() { return mGlobalPrinterList != nullptr; }
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetNumPrinters() {
|
2009-01-18 23:14:14 +03:00
|
|
|
return mGlobalPrinterList ? mGlobalPrinterList->Length() : 0;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
nsString* GetStringAt(int32_t aInx) {
|
|
|
|
return &mGlobalPrinterList->ElementAt(aInx);
|
|
|
|
}
|
2017-10-09 02:08:09 +03:00
|
|
|
void GetDefaultPrinterName(nsAString& aDefaultPrinterName);
|
2006-02-07 04:13:12 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
GlobalPrinters() {}
|
|
|
|
|
|
|
|
static GlobalPrinters mGlobalPrinters;
|
2009-01-18 23:14:14 +03:00
|
|
|
static nsTArray<nsString>* mGlobalPrinterList;
|
2006-02-07 04:13:12 +03:00
|
|
|
};
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//---------------
|
|
|
|
// static members
|
|
|
|
GlobalPrinters GlobalPrinters::mGlobalPrinters;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsTArray<nsString>* GlobalPrinters::mGlobalPrinterList = nullptr;
|
2006-02-07 04:13:12 +03:00
|
|
|
//---------------
|
2006-02-07 04:13:09 +03:00
|
|
|
|
2006-02-07 04:13:27 +03:00
|
|
|
nsDeviceContextSpecGTK::nsDeviceContextSpecGTK()
|
2015-03-06 00:05:33 +03:00
|
|
|
: mGtkPrintSettings(nullptr), mGtkPageSetup(nullptr) {
|
2006-02-07 04:13:44 +03:00
|
|
|
DO_PR_DEBUG_LOG(("nsDeviceContextSpecGTK::nsDeviceContextSpecGTK()\n"));
|
1999-02-19 01:35:11 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:27 +03:00
|
|
|
nsDeviceContextSpecGTK::~nsDeviceContextSpecGTK() {
|
2006-02-07 04:13:44 +03:00
|
|
|
DO_PR_DEBUG_LOG(("nsDeviceContextSpecGTK::~nsDeviceContextSpecGTK()\n"));
|
2008-02-13 14:03:43 +03:00
|
|
|
|
|
|
|
if (mGtkPageSetup) {
|
|
|
|
g_object_unref(mGtkPageSetup);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mGtkPrintSettings) {
|
|
|
|
g_object_unref(mGtkPrintSettings);
|
|
|
|
}
|
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;
|
|
|
|
mPrintSettings->GetEffectivePageSize(&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
|
|
|
|
2017-11-28 11:58:58 +03:00
|
|
|
DO_PR_DEBUG_LOG(
|
|
|
|
("Making PrintTarget: width = %f, height = %f\n", width, height));
|
2008-01-21 06:47:25 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
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);
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
// Spool file. Use Glib's temporary file function since we're
|
|
|
|
// already dependent on the gtk software stack.
|
|
|
|
gchar* buf;
|
2012-07-30 18:20:58 +04:00
|
|
|
gint fd = g_file_open_tmp("XXXXXX.tmp", &buf, nullptr);
|
2016-06-03 13:27:31 +03:00
|
|
|
if (-1 == fd) return nullptr;
|
2008-01-21 06:47:25 +03:00
|
|
|
close(fd);
|
|
|
|
|
2011-10-03 11:56:21 +04:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(buf), false,
|
2008-01-21 06:47:25 +03:00
|
|
|
getter_AddRefs(mSpoolFile));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
unlink(buf);
|
2016-06-03 13:27:31 +03:00
|
|
|
return nullptr;
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
2006-02-10 23:44:48 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
mSpoolName = buf;
|
|
|
|
g_free(buf);
|
2007-06-18 00:17:23 +04:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
mSpoolFile->SetPermissions(0600);
|
2007-03-01 03:02:27 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFileOutputStream> stream =
|
|
|
|
do_CreateInstance("@mozilla.org/network/file-output-stream;1");
|
2008-01-21 06:47:25 +03:00
|
|
|
rv = stream->Init(mSpoolFile, -1, -1, 0);
|
2007-03-01 03:02:27 +03:00
|
|
|
if (NS_FAILED(rv)) return nullptr;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t format;
|
2007-08-22 22:07:18 +04:00
|
|
|
mPrintSettings->GetOutputFormat(&format);
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
// Determine the real format with some GTK magic
|
|
|
|
if (format == nsIPrintSettings::kOutputFormatNative) {
|
|
|
|
if (mIsPPreview) {
|
2017-12-07 17:52:25 +03:00
|
|
|
// There is nothing to detect on Print Preview, use PDF.
|
|
|
|
format = nsIPrintSettings::kOutputFormatPDF;
|
2008-01-21 06:47:25 +03:00
|
|
|
} else {
|
2016-06-03 13:27:31 +03:00
|
|
|
return nullptr;
|
2015-03-06 00:05:33 +03:00
|
|
|
}
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
|
2016-07-26 17:48:30 +03:00
|
|
|
IntSize size = IntSize::Truncate(width, height);
|
2016-06-06 18:10:28 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
if (format == nsIPrintSettings::kOutputFormatPDF) {
|
2016-06-06 18:10:28 +03:00
|
|
|
return PrintTargetPDF::CreateOrNull(stream, size);
|
2007-08-22 22:07:18 +04:00
|
|
|
}
|
|
|
|
|
2016-06-06 18:10:28 +03:00
|
|
|
int32_t orientation;
|
|
|
|
mPrintSettings->GetOrientation(&orientation);
|
|
|
|
return PrintTargetPS::CreateOrNull(
|
|
|
|
stream, size,
|
|
|
|
orientation == nsIPrintSettings::kPortraitOrientation
|
|
|
|
? PrintTargetPS::PORTRAIT
|
|
|
|
: PrintTargetPS::LANDSCAPE);
|
2006-02-10 23:44:48 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2006-02-07 04:13:53 +03:00
|
|
|
DO_PR_DEBUG_LOG(("nsDeviceContextSpecGTK::Init(aPS=%p)\n", aPS));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
if (gtk_major_version < 2 ||
|
|
|
|
(gtk_major_version == 2 && gtk_minor_version < 10))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE; // I'm so sorry bz
|
2006-02-07 04:12:18 +03:00
|
|
|
|
2015-03-06 00:05:33 +03:00
|
|
|
mPrintSettings = do_QueryInterface(aPS);
|
|
|
|
if (!mPrintSettings) return NS_ERROR_NO_INTERFACE;
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
mIsPPreview = aIsPrintPreview;
|
2006-02-07 04:13:09 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
// This is only set by embedders
|
2011-09-29 10:19:26 +04:00
|
|
|
bool toFile;
|
2008-01-21 06:47:25 +03:00
|
|
|
aPS->GetPrintToFile(&toFile);
|
2008-01-17 09:27:50 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
mToPrinter = !toFile && !aIsPrintPreview;
|
2006-02-07 04:12:41 +03:00
|
|
|
|
2015-03-06 00:05:33 +03:00
|
|
|
mGtkPrintSettings = mPrintSettings->GetGtkPrintSettings();
|
|
|
|
mGtkPageSetup = mPrintSettings->GetGtkPageSetup();
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2008-02-13 14:03:43 +03:00
|
|
|
// 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. See bug
|
2008-02-13 14:05:09 +03:00
|
|
|
// 414314 for more info.
|
2008-02-13 14:03:43 +03:00
|
|
|
GtkPaperSize* geckosHackishPaperSize =
|
|
|
|
gtk_page_setup_get_paper_size(mGtkPageSetup);
|
|
|
|
GtkPaperSize* standardGtkPaperSize =
|
|
|
|
gtk_paper_size_new(gtk_paper_size_get_name(geckosHackishPaperSize));
|
|
|
|
|
|
|
|
mGtkPageSetup = gtk_page_setup_copy(mGtkPageSetup);
|
|
|
|
mGtkPrintSettings = gtk_print_settings_copy(mGtkPrintSettings);
|
|
|
|
|
|
|
|
GtkPaperSize* properPaperSize;
|
|
|
|
if (gtk_paper_size_is_equal(geckosHackishPaperSize, standardGtkPaperSize)) {
|
|
|
|
properPaperSize = standardGtkPaperSize;
|
|
|
|
} else {
|
|
|
|
properPaperSize = geckosHackishPaperSize;
|
|
|
|
}
|
|
|
|
gtk_print_settings_set_paper_size(mGtkPrintSettings, properPaperSize);
|
|
|
|
gtk_page_setup_set_paper_size_and_default_margins(mGtkPageSetup,
|
|
|
|
properPaperSize);
|
2011-03-25 06:38:59 +03:00
|
|
|
gtk_paper_size_free(standardGtkPaperSize);
|
2008-02-13 14:03:43 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
return NS_OK;
|
1999-02-19 01:35:11 +03:00
|
|
|
}
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
static void
|
2018-01-09 13:51:07 +03:00
|
|
|
#ifdef MOZ_WIDGET_GTK
|
2013-06-13 16:24:31 +04:00
|
|
|
print_callback(GtkPrintJob* aJob, gpointer aData, const GError* aError) {
|
|
|
|
#else
|
2008-01-21 06:47:25 +03:00
|
|
|
print_callback(GtkPrintJob* aJob, gpointer aData, GError* aError) {
|
2013-06-13 16:24:31 +04:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void ns_release_macro(gpointer aData) {
|
2012-06-06 06:08:30 +04:00
|
|
|
nsIFile* spoolFile = (nsIFile*)aData;
|
2008-01-21 06:47:25 +03:00
|
|
|
NS_RELEASE(spoolFile);
|
|
|
|
}
|
|
|
|
|
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)) {
|
2015-07-28 00:11:46 +03:00
|
|
|
spec->mPrintSettings->SetGtkPrinter(aPrinter);
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
GtkPrintJob* job =
|
|
|
|
gtk_print_job_new(mTitle.get(), mPrintSettings->GetGtkPrinter(),
|
|
|
|
mGtkPrintSettings, mGtkPageSetup);
|
|
|
|
|
|
|
|
if (!gtk_print_job_set_source_file(job, mSpoolName.get(), nullptr)) return;
|
2015-03-06 00:05:33 +03:00
|
|
|
|
2015-07-28 00:11:46 +03:00
|
|
|
NS_ADDREF(mSpoolFile.get());
|
|
|
|
gtk_print_job_send(job, print_callback, mSpoolFile, ns_release_macro);
|
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() {
|
2008-01-21 06:47:25 +03:00
|
|
|
if (mToPrinter) {
|
2015-03-06 00:05:33 +03:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
GtkPrinter* printer = mPrintSettings->GetGtkPrinter();
|
|
|
|
if (printer) {
|
|
|
|
// We have a printer, so we can print right away.
|
2015-07-28 00:11:46 +03:00
|
|
|
StartPrintJob();
|
2015-03-06 00:05:33 +03:00
|
|
|
} else {
|
|
|
|
// We don't have a printer. We have to enumerate the printers and find
|
|
|
|
// one with a matching name.
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NewRunnableMethod("nsDeviceContextSpecGTK::EnumeratePrinters", this,
|
|
|
|
&nsDeviceContextSpecGTK::EnumeratePrinters));
|
2015-03-06 00:05:33 +03:00
|
|
|
}
|
2008-01-21 06:47:25 +03:00
|
|
|
} else {
|
|
|
|
// Handle print-to-file ourselves for the benefit of embedders
|
2017-08-08 09:07:55 +03:00
|
|
|
nsString targetPath;
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> destFile;
|
2017-10-09 02:08:09 +03:00
|
|
|
mPrintSettings->GetToFileName(targetPath);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2017-01-21 09:45:17 +03:00
|
|
|
nsresult rv = NS_NewLocalFile(targetPath, false, getter_AddRefs(destFile));
|
2008-01-21 06:47:25 +03:00
|
|
|
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);
|
|
|
|
|
|
|
|
// 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));
|
2018-04-16 13:28:51 +03:00
|
|
|
|
|
|
|
// Notify flatpak printing portal that file is completely written
|
|
|
|
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
|
|
|
|
bool shouldUsePortal;
|
|
|
|
if (giovfs) {
|
|
|
|
giovfs->ShouldUseFlatpakPortal(&shouldUsePortal);
|
|
|
|
if (shouldUsePortal) {
|
|
|
|
// Use the name of the file for printing to match with
|
|
|
|
// nsFlatpakPrintPortal
|
|
|
|
nsCOMPtr<nsIObserverService> os =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
// Pass filename to be sure that observer process the right data
|
|
|
|
os->NotifyObservers(nullptr, "print-to-file-finished",
|
|
|
|
targetPath.get());
|
|
|
|
}
|
|
|
|
}
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2006-11-30 02:40:16 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:09 +03:00
|
|
|
// Printer Enumerator
|
|
|
|
nsPrinterEnumeratorGTK::nsPrinterEnumeratorGTK() {}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsPrinterEnumeratorGTK, nsIPrinterEnumerator)
|
2006-02-07 04:13:09 +03:00
|
|
|
|
2007-05-06 15:29:41 +04:00
|
|
|
NS_IMETHODIMP nsPrinterEnumeratorGTK::GetPrinterNameList(
|
|
|
|
nsIStringEnumerator** aPrinterNameList) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrinterNameList);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPrinterNameList = nullptr;
|
2017-12-19 13:38:59 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString>* printers = new nsTArray<nsString>(numPrinters);
|
2007-05-06 15:29:41 +04:00
|
|
|
if (!printers) {
|
2006-02-07 04:13:12 +03:00
|
|
|
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
|
2006-02-07 04:13:09 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|
2017-12-19 13:38:59 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = 0;
|
2006-02-07 04:13:12 +03:00
|
|
|
while (count < numPrinters) {
|
2009-01-18 23:14:14 +03:00
|
|
|
printers->AppendElement(
|
|
|
|
*GlobalPrinters::GetInstance()->GetStringAt(count++));
|
2006-02-07 04:13:09 +03:00
|
|
|
}
|
2006-02-07 04:13:12 +03:00
|
|
|
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
|
2006-02-07 04:13:09 +03:00
|
|
|
|
2007-05-06 15:29:41 +04:00
|
|
|
return NS_NewAdoptingStringEnumerator(aPrinterNameList, printers);
|
2006-02-07 04:12:41 +03:00
|
|
|
}
|
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
NS_IMETHODIMP nsPrinterEnumeratorGTK::GetDefaultPrinterName(
|
|
|
|
nsAString& aDefaultPrinterName) {
|
2006-02-07 04:13:44 +03:00
|
|
|
DO_PR_DEBUG_LOG(("nsPrinterEnumeratorGTK::GetDefaultPrinterName()\n"));
|
|
|
|
|
|
|
|
GlobalPrinters::GetInstance()->GetDefaultPrinterName(aDefaultPrinterName);
|
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
DO_PR_DEBUG_LOG(("GetDefaultPrinterName(): default printer='%s'.\n",
|
|
|
|
NS_ConvertUTF16toUTF8(aDefaultPrinterName).get()));
|
2006-02-07 04:13:34 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrinterEnumeratorGTK::InitPrintSettingsFromPrinter(
|
|
|
|
const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
|
2006-02-07 04:13:44 +03:00
|
|
|
DO_PR_DEBUG_LOG(("nsPrinterEnumeratorGTK::InitPrintSettingsFromPrinter()"));
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrintSettings);
|
|
|
|
|
2017-11-28 11:58:43 +03:00
|
|
|
// Set a default file name.
|
|
|
|
nsAutoString filename;
|
|
|
|
nsresult rv = aPrintSettings->GetToFileName(filename);
|
|
|
|
if (NS_FAILED(rv) || filename.IsEmpty()) {
|
|
|
|
const char* path = PR_GetEnv("PWD");
|
|
|
|
if (!path) {
|
|
|
|
path = PR_GetEnv("HOME");
|
|
|
|
}
|
2015-06-10 10:11:49 +03:00
|
|
|
|
2017-11-28 11:58:43 +03:00
|
|
|
if (path) {
|
2018-07-06 10:44:43 +03:00
|
|
|
CopyUTF8toUTF16(MakeStringSpan(path), filename);
|
2017-11-28 11:58:43 +03:00
|
|
|
filename.AppendLiteral("/mozilla.pdf");
|
|
|
|
} else {
|
|
|
|
filename.AssignLiteral("mozilla.pdf");
|
|
|
|
}
|
|
|
|
|
|
|
|
DO_PR_DEBUG_LOG(("Setting default filename to '%s'\n",
|
|
|
|
NS_ConvertUTF16toUTF8(filename).get()));
|
|
|
|
aPrintSettings->SetToFileName(filename);
|
|
|
|
}
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2011-10-03 11:56:21 +04:00
|
|
|
aPrintSettings->SetIsInitializedFromPrinter(true);
|
2006-02-07 04:14:23 +03:00
|
|
|
|
2017-12-19 13:38:59 +03:00
|
|
|
return NS_OK;
|
2006-02-07 04:13:32 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
nsresult GlobalPrinters::InitializeGlobalPrinters() {
|
|
|
|
if (PrintersAreAllocated()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-02-07 04:13:09 +03:00
|
|
|
|
2009-01-18 23:14:14 +03:00
|
|
|
mGlobalPrinterList = new nsTArray<nsString>();
|
2006-02-07 04:14:54 +03:00
|
|
|
|
2006-02-07 04:15:08 +03:00
|
|
|
nsPSPrinterList psMgr;
|
2011-08-25 12:46:02 +04:00
|
|
|
if (psMgr.Enabled()) {
|
2006-02-07 04:14:42 +03:00
|
|
|
/* Get the list of PostScript-module printers */
|
2009-01-22 07:15:34 +03:00
|
|
|
// XXX: this function is the only user of GetPrinterList
|
|
|
|
// So it may be interesting to convert the nsCStrings
|
|
|
|
// in this function, we would save one loop here
|
|
|
|
nsTArray<nsCString> printerList;
|
2006-02-07 04:15:08 +03:00
|
|
|
psMgr.GetPrinterList(printerList);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < printerList.Length(); i++) {
|
2009-01-22 07:15:34 +03:00
|
|
|
mGlobalPrinterList->AppendElement(NS_ConvertUTF8toUTF16(printerList[i]));
|
|
|
|
}
|
2006-02-07 04:13:09 +03:00
|
|
|
}
|
2010-06-28 21:36:17 +04:00
|
|
|
|
2006-02-07 04:14:34 +03:00
|
|
|
/* If there are no printers available after all checks, return an error */
|
2009-01-18 23:14:14 +03:00
|
|
|
if (!mGlobalPrinterList->Length()) {
|
2006-02-07 04:14:34 +03:00
|
|
|
/* Make sure we do not cache an empty printer list */
|
|
|
|
FreeGlobalPrinters();
|
|
|
|
|
|
|
|
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
|
|
|
|
}
|
2006-02-07 04:13:09 +03:00
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
return NS_OK;
|
2006-02-07 04:13:09 +03:00
|
|
|
}
|
|
|
|
|
2006-02-07 04:13:12 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void GlobalPrinters::FreeGlobalPrinters() {
|
2006-02-07 04:13:44 +03:00
|
|
|
if (mGlobalPrinterList) {
|
|
|
|
delete mGlobalPrinterList;
|
2012-07-30 18:20:58 +04:00
|
|
|
mGlobalPrinterList = nullptr;
|
2017-12-19 13:38:59 +03:00
|
|
|
}
|
2006-02-07 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
void GlobalPrinters::GetDefaultPrinterName(nsAString& aDefaultPrinterName) {
|
|
|
|
aDefaultPrinterName.Truncate();
|
2017-12-19 13:38:59 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allocate = !GlobalPrinters::GetInstance()->PrintersAreAllocated();
|
2017-12-19 13:38:59 +03:00
|
|
|
|
2006-02-07 04:13:44 +03:00
|
|
|
if (allocate) {
|
|
|
|
nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ASSERTION(GlobalPrinters::GetInstance()->PrintersAreAllocated(),
|
|
|
|
"no GlobalPrinters");
|
|
|
|
|
|
|
|
if (GlobalPrinters::GetInstance()->GetNumPrinters() == 0) return;
|
2017-12-19 13:38:59 +03:00
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
aDefaultPrinterName = *GlobalPrinters::GetInstance()->GetStringAt(0);
|
2006-02-07 04:13:44 +03:00
|
|
|
|
2017-12-19 13:38:59 +03:00
|
|
|
if (allocate) {
|
2006-02-07 04:13:44 +03:00
|
|
|
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
|
2017-12-19 13:38:59 +03:00
|
|
|
}
|
2006-02-07 04:13:12 +03:00
|
|
|
}
|