Closed Bug 377096 Opened 16 years ago Closed 16 years ago

Factor out nsIPrintOptions::AvailablePrinters


(Core :: Printing: Output, enhancement)

Not set





(Reporter: kherron+mozilla, Assigned: kherron+mozilla)



(1 file, 3 obsolete files)

Attached patch patch v1 (obsolete) — Splinter Review
This is an attempt to simplify the interface between the cross-platform print dialog and the system's printing support. The print dialog (among others) calls nsIPrintOptions::AvailablePrinters() to get a printer list, but the AvailablePrinters implementation is just a wrapper around nsIPrinterEnumerator::EumeratePrinters. nsIPrinterEumerator is also scriptable, so callers could just call it directly.

Aside from removing AvailablePrinters, I've also changed nsIPrinterEnumerator::EnumeratePrinters to make it simpler. The current design requires a lot of manual memory allocations and frees. I've changed it to construct and return a string enumerator, and renamed it GetPrinterNameList.

One of the callers for AvailablePrinters was nsPrintEngine::CheckForPrinters. This function makes sure a newly created nsPrintSettings object includes a printer setting. The current nsPrintSettings factory always sets a printer, so this function is unnecessary and I've removed it.

I've tested this on GTK and MacOSX (which doesn't actually use any of this code). The only platform-specific code is the nsIPrinterEnumerator implementations; there are about six of them in the tree for different platforms. Most of them are virtually identical to the GTK version. I haven't included a patch for the Photon version because it's significantly different.
Attachment #261202 - Flags: review?(vladimir)
Attachment #261202 - Flags: review?(vladimir) → review?(pavlov)
Comment on attachment 261202 [details] [diff] [review]
patch v1

>   PRInt32 printerInx = 0;
>-  while( count < numItems ) {
>+  while( printerInx < numPrinters ) {
>     LPTSTR name = GlobalPrinters::GetInstance()->GetItemFromList(printerInx++);
>-    nsAutoString newName; 
>-    NS_CopyNativeToUnicode(nsDependentCString(name), newName);
>-    PRUnichar *str = ToNewUnicode(newName);
>-    if (!str) {
>-      CleanupArray(array, count);
>-      return NS_ERROR_OUT_OF_MEMORY;
>-    }
>-    array[count++] = str;
>+    printers->AppendString(NS_ConvertUTF8toUTF16(name));
>   }

This new code is almost certainly wrong.  Windows doesn't use UTF8 for anything.
Attached patch patch v2 (obsolete) — Splinter Review
You're right. I based that on the beos version of NS_CopyNativeToUnicode() without realizing that each platform had its own implementation of that function. It looks like there's no substitute for calling NS_CopyNativeToUnicode() here.
Attachment #261202 - Attachment is obsolete: true
Attachment #262237 - Flags: review?(pavlov)
Attachment #261202 - Flags: review?(pavlov)
Attachment #262237 - Flags: review?(pavlov) → review+
Attachment #262237 - Flags: superreview?(cbiesinger)
Comment on attachment 262237 [details] [diff] [review]
patch v2

> The current nsPrintSettings factory always sets a printer, so
>this function is unnecessary and I've removed it.

I think you're wrong. It seems you're referring to this code:

However, a few platforms have their own implementation of that function:
That one doesn't seem to set the printer name.


In getPrinters:
I'd prefer a codestyle like this (I hope bugzilla won't wrap it; the dots are supposed to be aligned with each other)

+    printerEnumerator = Components.classes[";1"];
+                                  .getService(Components.interfaces.nsIPrinterEnumerator)
+                                  .printerNameList;

This also leads to slightly shorter lines.

(also in xpfe/global/resources/content/printdialog.js)

+++ widget/src/beos/nsDeviceContextSpecB.cpp	20 Apr 2007 11:15:46 -0000

+  NS_ENSURE_ARG_POINTER(aPrinterNameList);

I see no point in nullchecking out parameters... (but then, this code used to do that. oh well.)

In the Xlib code:
   *aCount = count;
   *aResult = array;

you need to remove those :)

Looks good otherwise.
Attachment #262237 - Flags: superreview?(cbiesinger) → superreview-
Attached patch Restore CheckForPrinters (obsolete) — Splinter Review
I think this addresses your comments. I restored nsIPrintOptions::CheckForPrinters, but rewrote it in terms of the print settings service default printer.

The current version of CheckForPrinters would enumerate printers and fetch the first printer from the list before checking whether the print settings object already had a printer set. I've reversed the order of these actions. Also, the current version has special-case logic for MacOSX to return NS_OK if it can't enumerate printers. In fact, Mac just uses the system's print dialog and doesn't bother with printer names, so I just made the whole function a no-op there.
Attachment #262237 - Attachment is obsolete: true
Attachment #262958 - Flags: superreview?(cbiesinger)
Comment on attachment 262958 [details] [diff] [review]
Restore CheckForPrinters

nsDeviceContextSpecXlib.cpp was removed from the tree, so you can ignore that part of the patch.
Comment on attachment 262958 [details] [diff] [review]
Restore CheckForPrinters

+#if defined(XP_MAC) || defined(XP_MACOSX)
+    // Mac doesn't support retrieving a printer list.
+    return NS_OK;

this should be indented only two spaces

I'd use an nsXPIDLString and getter_Copies here, instead of manually freeing the printer name.

in the two .js files:
+        Components.classes[";1"]
+        .getService(Components.interfaces.nsIPrinterEnumerator)

I meant putting the . from .getService under the dot from .classes
Attachment #262958 - Flags: superreview?(cbiesinger) → superreview+
* Fixed the formatting issues.
* Rewrote CheckForPrinters using nsXPIDLString.
* Removed the Xlib section of the patch.
Attachment #262958 - Attachment is obsolete: true
Checked in.
Closed: 16 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.