Back to index

lightning-sunbird  0.9+nobinonly
nsDeviceContextSpecOS2.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Mozilla OS/2 libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * John Fairhurst, <john_fairhurst@iname.com>.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Pierre Phaneuf <pp@ludusdesign.com>
00023  *   Peter Weilbacher <mozilla@weilbacher.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdlib.h>
00040 #include "nsDeviceContextSpecOS2.h"
00041 
00042 #include "nsReadableUtils.h"
00043 #include "nsISupportsArray.h"
00044 
00045 #include "nsIPref.h"
00046 #include "prenv.h" /* for PR_GetEnv */
00047 
00048 #include "nsPrintfCString.h"
00049 #include "nsIServiceManager.h"
00050 #include "nsUnicharUtils.h"
00051 #include "nsStringFwd.h"
00052 
00053 #include "nsOS2Uni.h"
00054 
00055 PRINTDLG nsDeviceContextSpecOS2::PrnDlg;
00056 
00057 //----------------------------------------------------------------------------------
00058 // The printer data is shared between the PrinterEnumerator and the nsDeviceContextSpecOS2
00059 // The PrinterEnumerator creates the printer info
00060 // but the nsDeviceContextSpecOS2 cleans it up
00061 // If it gets created (via the Page Setup Dialog) but the user never prints anything
00062 // then it will never be delete, so this class takes care of that.
00063 class GlobalPrinters {
00064 public:
00065   static GlobalPrinters* GetInstance()   { return &mGlobalPrinters; }
00066   ~GlobalPrinters()                      { FreeGlobalPrinters(); }
00067 
00068   void      FreeGlobalPrinters();
00069   nsresult  InitializeGlobalPrinters();
00070 
00071   PRBool    PrintersAreAllocated()       { return mGlobalPrinterList != nsnull; }
00072   PRInt32   GetNumPrinters()             { return mGlobalNumPrinters; }
00073   nsString* GetStringAt(PRInt32 aInx)    { return mGlobalPrinterList->StringAt(aInx); }
00074   void      GetDefaultPrinterName(PRUnichar*& aDefaultPrinterName);
00075 
00076 protected:
00077   GlobalPrinters() {}
00078 
00079   static GlobalPrinters mGlobalPrinters;
00080   static nsStringArray* mGlobalPrinterList;
00081   static ULONG          mGlobalNumPrinters;
00082 
00083 };
00084 //---------------
00085 // static members
00086 GlobalPrinters GlobalPrinters::mGlobalPrinters;
00087 nsStringArray* GlobalPrinters::mGlobalPrinterList = nsnull;
00088 ULONG          GlobalPrinters::mGlobalNumPrinters = 0;
00089 //---------------
00090 
00095 nsDeviceContextSpecOS2 :: nsDeviceContextSpecOS2()
00096 {
00097   mQueue = nsnull;
00098 }
00099 
00104 nsDeviceContextSpecOS2 :: ~nsDeviceContextSpecOS2()
00105 {
00106   if( mQueue)
00107      PrnClosePrinter( mQueue);
00108 }
00109 
00110 static NS_DEFINE_IID(kIDeviceContextSpecIID, NS_IDEVICE_CONTEXT_SPEC_IID);
00111 #ifdef USE_XPRINT
00112 static NS_DEFINE_IID(kIDeviceContextSpecXPIID, NS_IDEVICE_CONTEXT_SPEC_XP_IID);
00113 #endif
00114 
00115 void SetupDevModeFromSettings(ULONG printer, nsIPrintSettings* aPrintSettings)
00116 {
00117   if (aPrintSettings) {
00118     int bufferSize = 3 * sizeof(DJP_ITEM);
00119     PBYTE pDJP_Buffer = new BYTE[bufferSize];
00120     memset(pDJP_Buffer, 0, bufferSize);
00121     PDJP_ITEM pDJP = (PDJP_ITEM) pDJP_Buffer;
00122 
00123     HDC hdc = nsDeviceContextSpecOS2::PrnDlg.GetDCHandle(printer);
00124     char* driver = nsDeviceContextSpecOS2::PrnDlg.GetDriverType(printer);
00125 
00126     // Setup Orientation
00127     PRInt32 orientation;
00128     aPrintSettings->GetOrientation(&orientation);
00129     if (!strcmp(driver, "LASERJET"))
00130       pDJP->lType = DJP_ALL;
00131     else
00132       pDJP->lType = DJP_CURRENT;
00133     pDJP->cb = sizeof(DJP_ITEM);
00134     pDJP->ulNumReturned = 1;
00135     pDJP->ulProperty = DJP_SJ_ORIENTATION;
00136     pDJP->ulValue = orientation == nsIPrintSettings::kPortraitOrientation?DJP_ORI_PORTRAIT:DJP_ORI_LANDSCAPE;
00137     pDJP++;
00138 
00139     // Setup Number of Copies
00140     PRInt32 copies;
00141     aPrintSettings->GetNumCopies(&copies);
00142     pDJP->cb = sizeof(DJP_ITEM);
00143     pDJP->lType = DJP_CURRENT;
00144     pDJP->ulNumReturned = 1;
00145     pDJP->ulProperty = DJP_SJ_COPIES;
00146     pDJP->ulValue = copies;
00147     pDJP++;
00148 
00149     pDJP->cb = sizeof(DJP_ITEM);
00150     pDJP->lType = DJP_NONE;
00151     pDJP->ulNumReturned = 1;
00152     pDJP->ulProperty = 0;
00153     pDJP->ulValue = 0;
00154 
00155     LONG driverSize = nsDeviceContextSpecOS2::PrnDlg.GetPrintDriverSize(printer);
00156     GreEscape (hdc, DEVESC_SETJOBPROPERTIES, bufferSize, pDJP_Buffer, 
00157                &driverSize, PBYTE(nsDeviceContextSpecOS2::PrnDlg.GetPrintDriver(printer)));
00158 
00159     delete [] pDJP_Buffer;
00160     DevCloseDC(hdc);
00161   }
00162 }
00163 
00164 nsresult nsDeviceContextSpecOS2::SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings, ULONG printer)
00165 {
00166   if (aPrintSettings == nsnull)
00167     return NS_ERROR_FAILURE;
00168 
00169   int bufferSize = 3 * sizeof(DJP_ITEM);
00170   PBYTE pDJP_Buffer = new BYTE[bufferSize];
00171   memset(pDJP_Buffer, 0, bufferSize);
00172   PDJP_ITEM pDJP = (PDJP_ITEM) pDJP_Buffer;
00173 
00174   HDC hdc = nsDeviceContextSpecOS2::PrnDlg.GetDCHandle(printer);
00175 
00176   //Get Number of Copies from Job Properties
00177   pDJP->lType = DJP_CURRENT;
00178   pDJP->cb = sizeof(DJP_ITEM);
00179   pDJP->ulNumReturned = 1;
00180   pDJP->ulProperty = DJP_SJ_COPIES;
00181   pDJP->ulValue = 1;
00182   pDJP++;
00183 
00184   //Get Orientation from Job Properties
00185   pDJP->lType = DJP_CURRENT;
00186   pDJP->cb = sizeof(DJP_ITEM);
00187   pDJP->ulNumReturned = 1;
00188   pDJP->ulProperty = DJP_SJ_ORIENTATION;
00189   pDJP->ulValue = 1;
00190   pDJP++;
00191 
00192   pDJP->lType = DJP_NONE;
00193   pDJP->cb = sizeof(DJP_ITEM);
00194   pDJP->ulNumReturned = 1;
00195   pDJP->ulProperty = 0;
00196   pDJP->ulValue = 0;
00197 
00198   LONG driverSize = nsDeviceContextSpecOS2::PrnDlg.GetPrintDriverSize(printer);
00199   LONG rc = GreEscape(hdc, DEVESC_QUERYJOBPROPERTIES, bufferSize, pDJP_Buffer, 
00200                       &driverSize, PBYTE(nsDeviceContextSpecOS2::PrnDlg.GetPrintDriver(printer)));
00201 
00202   pDJP = (PDJP_ITEM) pDJP_Buffer;
00203   if ((rc == DEV_OK) || (rc == DEV_WARNING)) { 
00204     while (pDJP->lType != DJP_NONE) {
00205       if ((pDJP->ulProperty == DJP_SJ_ORIENTATION) && (pDJP->lType > 0)){
00206         if ((pDJP->ulValue == DJP_ORI_PORTRAIT) || (pDJP->ulValue == DJP_ORI_REV_PORTRAIT))
00207           aPrintSettings->SetOrientation(nsIPrintSettings::kPortraitOrientation);
00208         else
00209          aPrintSettings->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
00210       }
00211       if ((pDJP->ulProperty == DJP_SJ_COPIES) && (pDJP->lType > 0)){
00212         aPrintSettings->SetNumCopies(PRInt32(pDJP->ulValue));
00213       }
00214       pDJP = DJP_NEXT_STRUCTP(pDJP);
00215     }
00216   }
00217   
00218   delete [] pDJP_Buffer;
00219   DevCloseDC(hdc);  
00220   return NS_OK;
00221 }
00222 
00223 NS_IMETHODIMP nsDeviceContextSpecOS2 :: QueryInterface(REFNSIID aIID, void** aInstancePtr)
00224 {
00225   if (nsnull == aInstancePtr)
00226     return NS_ERROR_NULL_POINTER;
00227 
00228   if (aIID.Equals(kIDeviceContextSpecIID))
00229   {
00230     nsIDeviceContextSpec* tmp = this;
00231     *aInstancePtr = (void*) tmp;
00232     NS_ADDREF_THIS();
00233     return NS_OK;
00234   }
00235 
00236 #ifdef USE_XPRINT
00237   if (aIID.Equals(kIDeviceContextSpecXPIID))
00238   {
00239     nsIDeviceContextSpecXp *tmp = this;
00240     *aInstancePtr = (void*) tmp;
00241     NS_ADDREF_THIS();
00242     return NS_OK;
00243   }
00244 #endif /* USE_XPRINT */
00245 
00246   static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
00247 
00248   if (aIID.Equals(kISupportsIID))
00249   {
00250     nsIDeviceContextSpec* tmp = this;
00251     nsISupports* tmp2 = tmp;
00252     *aInstancePtr = (void*) tmp2;
00253     NS_ADDREF_THIS();
00254     return NS_OK;
00255   }
00256 
00257   return NS_NOINTERFACE;
00258 }
00259 
00260 NS_IMPL_ADDREF(nsDeviceContextSpecOS2)
00261 NS_IMPL_RELEASE(nsDeviceContextSpecOS2)
00262 
00263   
00283 NS_IMETHODIMP nsDeviceContextSpecOS2::Init(nsIPrintSettings* aPS, PRBool aIsPrintPreview)
00284 {
00285   nsresult rv = NS_ERROR_FAILURE;
00286 
00287   mPrintSettings = aPS;
00288   NS_ASSERTION(aPS, "Must have a PrintSettings!");
00289 
00290   rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
00291   if (NS_FAILED(rv)) {
00292     return rv;
00293   }
00294  
00295   if (aPS) {
00296     PRBool     tofile         = PR_FALSE;
00297     PRInt32    copies         = 1;
00298     PRUnichar *printer        = nsnull;
00299     PRUnichar *printfile      = nsnull;
00300 
00301     mPrintSettings->GetPrinterName(&printer);
00302     mPrintSettings->GetToFileName(&printfile);
00303     mPrintSettings->GetPrintToFile(&tofile);
00304     mPrintSettings->GetNumCopies(&copies);
00305 
00306     if ((copies == 0)  ||  (copies > 999)) {
00307        GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00308        return NS_ERROR_FAILURE;
00309     }
00310 
00311     if (printfile != nsnull) {
00312       // ToDo: Use LocalEncoding instead of UTF-8 (see bug 73446)
00313       strcpy(mPrData.path,    NS_ConvertUCS2toUTF8(printfile).get());
00314     }
00315     if (printer != nsnull) 
00316       strcpy(mPrData.printer, NS_ConvertUCS2toUTF8(printer).get());  
00317 
00318     if (aIsPrintPreview) 
00319       mPrData.destination = printPreview; 
00320     else if (tofile)  
00321       mPrData.destination = printToFile;
00322     else  
00323       mPrData.destination = printToPrinter;
00324     mPrData.copies = copies;
00325 
00326     rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
00327     if (NS_FAILED(rv))
00328       return rv;
00329 
00330     const nsAFlatString& printerUCS2 = NS_ConvertUTF8toUCS2(mPrData.printer);
00331     ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
00332     if (numPrinters) {
00333        for(ULONG i = 0; (i < numPrinters) && !mQueue; i++) {
00334           if ((GlobalPrinters::GetInstance()->GetStringAt(i)->Equals(printerUCS2, nsCaseInsensitiveStringComparator()))) {
00335              SetupDevModeFromSettings(i, aPS);
00336              mQueue = PrnDlg.SetPrinterQueue(i);
00337           }
00338        }
00339     }
00340 
00341     if (printfile != nsnull) 
00342       nsMemory::Free(printfile);
00343   
00344     if (printer != nsnull) 
00345       nsMemory::Free(printer);
00346   }
00347 
00348   GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00349   return rv;
00350 }
00351 
00352 
00353 NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetDestination( int &aDestination )     
00354 {
00355   aDestination = mPrData.destination;
00356   return NS_OK;
00357 }
00358 
00359 NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetPrinterName ( char **aPrinter )
00360 {
00361    *aPrinter = &mPrData.printer[0];
00362    return NS_OK;
00363 }
00364 
00365 NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetCopies ( int &aCopies )
00366 {
00367    aCopies = mPrData.copies;
00368    return NS_OK;
00369 }
00370 
00371 NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetPath ( char **aPath )      
00372 {
00373   *aPath = &mPrData.path[0];
00374   return NS_OK;
00375 }
00376 
00377 NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetUserCancelled( PRBool &aCancel )     
00378 {
00379   aCancel = mPrData.cancel;
00380   return NS_OK;
00381 }
00382 
00387 NS_IMETHODIMP nsDeviceContextSpecOS2 :: ClosePrintManager()
00388 {
00389   return NS_OK;
00390 }
00391 
00392 nsresult nsDeviceContextSpecOS2::GetPRTQUEUE( PRTQUEUE *&p)
00393 {
00394    p = mQueue;
00395    return NS_OK;
00396 }
00397 
00398 //  Printer Enumerator
00399 nsPrinterEnumeratorOS2::nsPrinterEnumeratorOS2()
00400 {
00401 }
00402 
00403 NS_IMPL_ISUPPORTS1(nsPrinterEnumeratorOS2, nsIPrinterEnumerator)
00404 
00405 NS_IMETHODIMP nsPrinterEnumeratorOS2::EnumeratePrinters(PRUint32* aCount, PRUnichar*** aResult)
00406 {
00407   NS_ENSURE_ARG(aCount);
00408   NS_ENSURE_ARG_POINTER(aResult);
00409 
00410   if (aCount) 
00411     *aCount = 0;
00412   else 
00413     return NS_ERROR_NULL_POINTER;
00414   
00415   if (aResult) 
00416     *aResult = nsnull;
00417   else 
00418     return NS_ERROR_NULL_POINTER;
00419 
00420   nsDeviceContextSpecOS2::PrnDlg.RefreshPrintQueue();
00421   
00422   nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
00423   if (NS_FAILED(rv)) {
00424     return rv;
00425   }
00426 
00427   ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
00428 
00429   PRUnichar** array = (PRUnichar**) nsMemory::Alloc(numPrinters * sizeof(PRUnichar*));
00430   if (!array && numPrinters > 0) {
00431     GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00432     return NS_ERROR_OUT_OF_MEMORY;
00433   }
00434 
00435   ULONG count = 0;
00436   while( count < numPrinters )
00437   {
00438     PRUnichar *str = ToNewUnicode(*GlobalPrinters::GetInstance()->GetStringAt(count));
00439 
00440     if (!str) {
00441       for (ULONG i = 0 ; i < count ; i++)
00442         nsMemory::Free(array[i]);
00443       
00444       nsMemory::Free(array);
00445 
00446       GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00447       return NS_ERROR_OUT_OF_MEMORY;
00448     }
00449     array[count++] = str;
00450     
00451   }
00452   *aCount = count;
00453   *aResult = array;
00454   GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00455 
00456   return NS_OK;
00457 }
00458 
00459 NS_IMETHODIMP nsPrinterEnumeratorOS2::GetDefaultPrinterName(PRUnichar * *aDefaultPrinterName)
00460 {
00461   NS_ENSURE_ARG_POINTER(aDefaultPrinterName);
00462   GlobalPrinters::GetInstance()->GetDefaultPrinterName(*aDefaultPrinterName);
00463   return NS_OK;
00464 }
00465 
00466 /* void initPrintSettingsFromPrinter (in wstring aPrinterName, in nsIPrintSettings aPrintSettings); */
00467 NS_IMETHODIMP nsPrinterEnumeratorOS2::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings)
00468 {
00469    NS_ENSURE_ARG_POINTER(aPrinterName);
00470    NS_ENSURE_ARG_POINTER(aPrintSettings);
00471 
00472    if (!*aPrinterName) 
00473      return NS_OK;
00474 
00475   if (NS_FAILED(GlobalPrinters::GetInstance()->InitializeGlobalPrinters())) 
00476     return NS_ERROR_FAILURE;
00477 
00478   ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
00479   for(ULONG i = 0; i < numPrinters; i++) {
00480     if ((GlobalPrinters::GetInstance()->GetStringAt(i)->Equals(aPrinterName, nsCaseInsensitiveStringComparator()))) 
00481       nsDeviceContextSpecOS2::SetPrintSettingsFromDevMode(aPrintSettings, i);
00482   }
00483 
00484   // Free them, we won't need them for a while
00485   GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00486   aPrintSettings->SetIsInitializedFromPrinter(PR_TRUE);
00487   return NS_OK;
00488 }
00489 
00490 NS_IMETHODIMP nsPrinterEnumeratorOS2::DisplayPropertiesDlg(const PRUnichar *aPrinter, nsIPrintSettings *aPrintSettings)
00491 {
00492   nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
00493   if (NS_FAILED(rv)) {
00494     return rv;
00495   }
00496 
00497   ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
00498   for(ULONG i = 0; i < numPrinters; i++) {
00499     if ((GlobalPrinters::GetInstance()->GetStringAt(i)->Equals(aPrinter, nsCaseInsensitiveStringComparator()))) {
00500        SetupDevModeFromSettings(i, aPrintSettings);
00501        if ( nsDeviceContextSpecOS2::PrnDlg.ShowProperties(i) ) {
00502           nsDeviceContextSpecOS2::SetPrintSettingsFromDevMode(aPrintSettings, i);
00503           return NS_OK;
00504        } else {
00505           return NS_ERROR_FAILURE;
00506        }
00507     }
00508   }
00509   return NS_ERROR_FAILURE;
00510 }
00511 
00512 nsresult GlobalPrinters::InitializeGlobalPrinters ()
00513 {
00514   if (PrintersAreAllocated()) 
00515     return NS_OK;
00516 
00517   mGlobalNumPrinters = 0;
00518   mGlobalNumPrinters = nsDeviceContextSpecOS2::PrnDlg.GetNumPrinters();
00519   if (!mGlobalNumPrinters) 
00520     return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE; 
00521 
00522   mGlobalPrinterList = new nsStringArray();
00523   if (!mGlobalPrinterList) 
00524      return NS_ERROR_OUT_OF_MEMORY;
00525 
00526   nsresult rv;
00527   nsCOMPtr<nsIPref> pPrefs = do_GetService(NS_PREF_CONTRACTID, &rv);
00528   BOOL prefFailed = NS_FAILED(rv); // don't return on failure, optional feature
00529 
00530   for (ULONG i = 0; i < mGlobalNumPrinters; i++) {
00531     nsXPIDLCString printer;
00532     nsDeviceContextSpecOS2::PrnDlg.GetPrinter(i, getter_Copies(printer));
00533 
00534     nsAutoChar16Buffer printerName;
00535     PRInt32 printerNameLength;
00536     rv = MultiByteToWideChar(0, printer, strlen(printer),
00537                              printerName, printerNameLength);
00538     mGlobalPrinterList->AppendString(nsDependentString(printerName.get()));
00539 
00540     // store printer description in prefs for the print dialog
00541     if (!prefFailed) {
00542        nsCAutoString printerDescription;
00543        printerDescription = nsCAutoString(nsDeviceContextSpecOS2::PrnDlg.GetPrintDriver(i)->szDeviceName);
00544        printerDescription += " (";
00545        printerDescription += nsCAutoString(nsDeviceContextSpecOS2::PrnDlg.GetDriverType(i));
00546        printerDescription += ")";
00547        pPrefs->SetCharPref(nsPrintfCString(256,
00548                                            "print.printer_%s.printer_description",
00549                                            printer.get()).get(),
00550                            printerDescription.get());
00551     }
00552   } 
00553   return NS_OK;
00554 }
00555 
00556 void GlobalPrinters::GetDefaultPrinterName(PRUnichar*& aDefaultPrinterName)
00557 {
00558   aDefaultPrinterName = nsnull;
00559 
00560   nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
00561   if (NS_FAILED(rv)) 
00562      return;
00563 
00564   if (GetNumPrinters() == 0)
00565      return;
00566 
00567   // the default printer is always index 0
00568   nsXPIDLCString printer;
00569   nsDeviceContextSpecOS2::PrnDlg.GetPrinter(0, getter_Copies(printer));
00570 
00571   nsAutoChar16Buffer printerName;
00572   PRInt32 printerNameLength;
00573   MultiByteToWideChar(0, printer, strlen(printer), printerName,
00574                       printerNameLength);
00575   aDefaultPrinterName = ToNewUnicode(nsDependentString(printerName.get()));
00576 
00577   GlobalPrinters::GetInstance()->FreeGlobalPrinters();
00578 }
00579 
00580 void GlobalPrinters::FreeGlobalPrinters()
00581 {
00582   delete mGlobalPrinterList;
00583   mGlobalPrinterList = nsnull;
00584   mGlobalNumPrinters = 0;
00585 }
00586 
00587 
00588 //---------------------------------------------------------------------------
00589 // OS/2 Printing   - was in libprint.cpp
00590 //---------------------------------------------------------------------------
00591 static HMODULE hmodRes;
00592 
00593 #define SHIFT_PTR(ptr,offset) ( *((LONG*)&ptr) += offset )
00594 
00595 
00596 class PRTQUEUE
00597 {
00598 public:
00599    PRTQUEUE (const PRQINFO3* pPQI3)  { InitWithPQI3 (pPQI3); }
00600    PRTQUEUE (const PRTQUEUE& PQInfo);
00601   ~PRTQUEUE (void) { free (mpPQI3); }
00602 
00603    PRQINFO3& PQI3 () const { return *mpPQI3; }
00604    const char* DriverName () const { return mDriverName; }
00605    const char* DeviceName () const { return mDeviceName; }
00606    const char* PrinterName() const { return mPrinterName; }
00607    const char* QueueName  () const { return mpPQI3->pszComment; }
00608    
00609 private:
00610    PRTQUEUE& operator = (const PRTQUEUE& z);        // prevent copying
00611    void InitWithPQI3 (const PRQINFO3* pInfo);
00612 
00613    PRQINFO3* mpPQI3;
00614    unsigned  mPQI3BufSize;
00615    char mDriverName  [DRIV_NAME_SIZE + 1];          // Driver name
00616    char mDeviceName  [DRIV_DEVICENAME_SIZE + 1];    // Device name
00617    char mPrinterName [PRINTERNAME_SIZE + 1];        // Printer name
00618 };
00619 
00620 
00621 PRTQUEUE::PRTQUEUE (const PRTQUEUE& PQInfo)
00622 {
00623    mPQI3BufSize = PQInfo.mPQI3BufSize;
00624    mpPQI3 = (PRQINFO3*)malloc (mPQI3BufSize);
00625    memcpy (mpPQI3, PQInfo.mpPQI3, mPQI3BufSize);    // Copy entire buffer
00626 
00627    long Diff = (long)mpPQI3 - (long)PQInfo.mpPQI3;  // Calculate the difference between addresses
00628    SHIFT_PTR (mpPQI3->pszName,       Diff);         // Modify internal pointers accordingly
00629    SHIFT_PTR (mpPQI3->pszSepFile,    Diff);
00630    SHIFT_PTR (mpPQI3->pszPrProc,     Diff);
00631    SHIFT_PTR (mpPQI3->pszParms,      Diff);
00632    SHIFT_PTR (mpPQI3->pszComment,    Diff);
00633    SHIFT_PTR (mpPQI3->pszPrinters,   Diff);
00634    SHIFT_PTR (mpPQI3->pszDriverName, Diff);
00635    SHIFT_PTR (mpPQI3->pDriverData,   Diff);
00636 
00637    strcpy (mDriverName, PQInfo.mDriverName);
00638    strcpy (mDeviceName, PQInfo.mDeviceName);
00639    strcpy (mPrinterName, PQInfo.mPrinterName);
00640 }
00641 
00642 void PRTQUEUE::InitWithPQI3(const PRQINFO3* pInfo)
00643 {
00644    // Make local copy of PPRQINFO3 object
00645    ULONG SizeNeeded;
00646    ::SplQueryQueue (NULL, pInfo->pszName, 3, NULL, 0, &SizeNeeded);
00647    mpPQI3 = (PRQINFO3*)malloc (SizeNeeded);
00648    ::SplQueryQueue (NULL, pInfo->pszName, 3, mpPQI3, SizeNeeded, &SizeNeeded);
00649 
00650    mPQI3BufSize = SizeNeeded;
00651 
00652    PCHAR sep = strchr (pInfo->pszDriverName, '.');
00653 
00654    if (sep)
00655    {
00656       *sep = '\0';
00657       strcpy (mDriverName, pInfo->pszDriverName);
00658       strcpy (mDeviceName, sep + 1);
00659       *sep = '.';
00660    } else
00661    {
00662       strcpy (mDriverName, pInfo->pszDriverName);
00663       mDeviceName [0] = '\0';
00664    }
00665 
00666 
00667    sep = strchr (pInfo->pszPrinters, ',');
00668 
00669    if (sep)
00670    {
00671       *sep = '\0';
00672       strcpy (mPrinterName, pInfo->pszPrinters);
00673       *sep = '.';
00674    } else
00675    {
00676       strcpy (mPrinterName, pInfo->pszPrinters);
00677    }
00678 }
00679 
00680 
00681 //===========================================================================
00682 
00683 PRINTDLG::PRINTDLG()
00684 {
00685   mQueueCount = 0;
00686 
00687   ULONG TotalQueues = 0;
00688   ULONG MemNeeded = 0;
00689   SPLERR rc;
00690   
00691   rc = ::SplEnumQueue(NULL, 3, NULL, 0, &mQueueCount, &TotalQueues, &MemNeeded, NULL);
00692   PRQINFO3* pPQI3Buf = (PRQINFO3*) malloc (MemNeeded);
00693   rc = ::SplEnumQueue(NULL, 3, pPQI3Buf, MemNeeded, &mQueueCount, &TotalQueues, &MemNeeded, NULL);
00694 
00695   if (mQueueCount > MAX_PRINT_QUEUES)
00696     mQueueCount = MAX_PRINT_QUEUES;
00697 
00698   ULONG defaultQueue = 0;
00699   for (ULONG cnt = 0; cnt < mQueueCount; cnt++) {
00700     if (pPQI3Buf[cnt].fsType & PRQ3_TYPE_APPDEFAULT)
00701       defaultQueue = cnt;
00702     mPQBuf[cnt] = new PRTQUEUE(&pPQI3Buf[cnt]);
00703   }
00704 
00705   // move the entry for the default printer to index 0 (if necessary)
00706   if (defaultQueue > 0) {
00707     PRTQUEUE* temp = mPQBuf[0];
00708     mPQBuf[0] = mPQBuf[defaultQueue];
00709     mPQBuf[defaultQueue] = temp;
00710   }
00711 
00712   free(pPQI3Buf);
00713 }
00714 
00715 PRINTDLG::~PRINTDLG()
00716 {
00717   for (ULONG index = 0; index < mQueueCount; index++)
00718     delete mPQBuf[index];
00719 }
00720 
00721 void PRINTDLG::RefreshPrintQueue()
00722 {
00723   ULONG newQueueCount = 0;
00724   ULONG TotalQueues = 0;
00725   ULONG MemNeeded = 0;
00726   SPLERR rc;
00727   
00728   rc = ::SplEnumQueue(NULL, 3, NULL, 0, &newQueueCount, &TotalQueues, &MemNeeded, NULL);
00729   PRQINFO3* pPQI3Buf = (PRQINFO3*)malloc(MemNeeded);
00730   rc = ::SplEnumQueue(NULL, 3, pPQI3Buf, MemNeeded, &newQueueCount, &TotalQueues, &MemNeeded, NULL);
00731 
00732   if (newQueueCount > MAX_PRINT_QUEUES)
00733     newQueueCount = MAX_PRINT_QUEUES;
00734 
00735   PRTQUEUE* tmpBuf[MAX_PRINT_QUEUES];
00736 
00737   ULONG defaultQueue = 0;
00738   for (ULONG cnt = 0; cnt < newQueueCount; cnt++) {
00739     if (pPQI3Buf[cnt].fsType & PRQ3_TYPE_APPDEFAULT)
00740       defaultQueue = cnt;
00741 
00742     BOOL found = FALSE;
00743     for (ULONG index = 0; index < mQueueCount && !found; index++) {
00744        //Compare printer from requeried list with what's already in Mozilla's printer list(mPQBuf)
00745        //If printer is already there, use current properties; otherwise create a new printer in list
00746        if (mPQBuf[index] != 0) {
00747          if ((strcmp(pPQI3Buf[cnt].pszPrinters, mPQBuf[index]->PrinterName()) == 0) && 
00748              (strcmp(pPQI3Buf[cnt].pszDriverName, mPQBuf[index]->PQI3().pszDriverName) == 0)) {
00749            found = TRUE;
00750            tmpBuf[cnt] = mPQBuf[index];
00751            mPQBuf[index] = 0;
00752          }
00753        }
00754     }
00755     if (!found) 
00756        tmpBuf[cnt] = new PRTQUEUE(&pPQI3Buf[cnt]); 
00757   }
00758 
00759   for (ULONG index = 0; index < newQueueCount; index++) {
00760     if (mPQBuf[index] != 0)
00761       delete(mPQBuf[index]);
00762     mPQBuf[index] = tmpBuf[index];
00763   }
00764 
00765   if (mQueueCount > newQueueCount)
00766     for (ULONG index = newQueueCount; index < mQueueCount; index++)
00767        if (mPQBuf[index] != 0)
00768          delete(mPQBuf[index]);
00769 
00770   mQueueCount = newQueueCount;
00771 
00772   // move the entry for the default printer to index 0 (if necessary)
00773   if (defaultQueue > 0) {
00774     PRTQUEUE* temp = mPQBuf[0];
00775     mPQBuf[0] = mPQBuf[defaultQueue];
00776     mPQBuf[defaultQueue] = temp;
00777   }
00778 
00779   free(pPQI3Buf);
00780 }
00781 
00782 ULONG PRINTDLG::GetNumPrinters()
00783 {
00784    return mQueueCount;
00785 }
00786 
00787 void PRINTDLG::GetPrinter(ULONG printerNdx, char** printerName)
00788 {
00789    if (printerNdx >= mQueueCount)
00790       return;
00791  
00792    nsCAutoString pName(mPQBuf[printerNdx]->QueueName());
00793  
00794    pName.ReplaceChar('\r', ' ');
00795    pName.StripChars("\n");
00796    *printerName = ToNewCString(pName);
00797 }
00798 
00799 PRTQUEUE* PRINTDLG::SetPrinterQueue(ULONG printerNdx)
00800 {
00801    PRTQUEUE *pPQ = NULL;
00802 
00803    if (printerNdx >= mQueueCount)
00804       return NULL;
00805 
00806    pPQ = mPQBuf[printerNdx];
00807 
00808    return new PRTQUEUE(*pPQ);
00809 }
00810 
00811 LONG PRINTDLG::GetPrintDriverSize(ULONG printerNdx)
00812 {
00813    return mPQBuf[printerNdx]->PQI3().pDriverData->cb;
00814 }
00815 
00816 PDRIVDATA PRINTDLG::GetPrintDriver(ULONG printerNdx)
00817 {
00818    if (printerNdx >= mQueueCount)
00819       return NULL;
00820 
00821    return mPQBuf[printerNdx]->PQI3().pDriverData;
00822 }
00823 
00824 HDC PRINTDLG::GetDCHandle(ULONG printerNdx)
00825 {
00826     HDC hdc = 0;
00827     DEVOPENSTRUC dop;
00828 
00829     dop.pszLogAddress      = 0; 
00830     dop.pszDriverName      = (char *)mPQBuf[printerNdx]->DriverName();
00831     dop.pdriv              = mPQBuf[printerNdx]->PQI3().pDriverData;
00832     dop.pszDataType        = 0; 
00833     dop.pszComment         = 0;
00834     dop.pszQueueProcName   = 0;     
00835     dop.pszQueueProcParams = 0;   
00836     dop.pszSpoolerParams   = 0;     
00837     dop.pszNetworkParams   = 0;     
00838 
00839     hdc = ::DevOpenDC(0, OD_INFO, "*", 9, (PDEVOPENDATA) &dop, NULLHANDLE);
00840     return hdc;
00841 }
00842 
00843 char* PRINTDLG::GetDriverType(ULONG printerNdx)
00844 {
00845   return (char *)mPQBuf[printerNdx]->DriverName ();
00846 }
00847 
00848 BOOL PRINTDLG::ShowProperties(ULONG printerNdx)
00849 {
00850     BOOL          rc = FALSE;
00851     LONG          devrc = FALSE;
00852     PDRIVDATA     pOldDrivData;
00853     PDRIVDATA     pNewDrivData = NULL;
00854     LONG          buflen;
00855 
00856 /* check size of buffer required for job properties */
00857     buflen = DevPostDeviceModes( 0 /*hab*/,
00858                                  NULL,
00859                                  mPQBuf[printerNdx]->DriverName (),
00860                                  mPQBuf[printerNdx]->DeviceName (),
00861                                  mPQBuf[printerNdx]->PrinterName (),
00862                                  DPDM_POSTJOBPROP);
00863 
00864 /* return error to caller */
00865     if (buflen <= 0)
00866         return(buflen);
00867 
00868 /* allocate some memory for larger job properties and */
00869 /* return error to caller */
00870 
00871     if (buflen != mPQBuf[printerNdx]->PQI3().pDriverData->cb)
00872     {
00873         if (DosAllocMem((PPVOID)&pNewDrivData,buflen,fALLOC))
00874             return(FALSE); // DPDM_ERROR
00875     
00876 /* copy over old data so driver can use old job */
00877 /* properties as base for job properties dialog */
00878         pOldDrivData = mPQBuf[printerNdx]->PQI3().pDriverData;
00879         mPQBuf[printerNdx]->PQI3().pDriverData = pNewDrivData;
00880         memcpy( (PSZ)pNewDrivData, (PSZ)pOldDrivData, pOldDrivData->cb );
00881     }
00882 
00883 /* display job properties dialog and get updated */
00884 /* job properties from driver */
00885 
00886     devrc = DevPostDeviceModes( 0 /*hab*/,
00887                                 mPQBuf[printerNdx]->PQI3().pDriverData,
00888                                 mPQBuf[printerNdx]->DriverName (),
00889                                 mPQBuf[printerNdx]->DeviceName (),
00890                                 mPQBuf[printerNdx]->PrinterName (),
00891                                 DPDM_POSTJOBPROP);
00892     rc = (devrc != DPDM_ERROR);
00893     return rc;
00894 }
00895 
00896 /****************************************************************************/
00897 /*  Job management                                                          */
00898 /****************************************************************************/
00899 
00900 HDC PrnOpenDC( PRTQUEUE *pInfo, PSZ pszApplicationName, int copies, int destination, char *file )
00901 {
00902    HDC hdc = 0;
00903    PSZ pszLogAddress;
00904    PSZ pszDataType;
00905    LONG dcType;
00906    DEVOPENSTRUC dop;
00907 
00908    if (!pInfo || !pszApplicationName)
00909       return hdc;
00910 
00911    if ( destination ) {
00912       pszLogAddress = pInfo->PQI3 ().pszName;
00913       pszDataType = "PM_Q_STD";
00914       if ( destination == 2 )
00915          dcType = OD_METAFILE;
00916       else
00917          dcType = OD_QUEUED;
00918    } else {
00919       if (file && *file)
00920          pszLogAddress = (PSZ) file;
00921       else    
00922          pszLogAddress = "FILE";
00923       pszDataType = "PM_Q_RAW";
00924       dcType = OD_DIRECT;
00925    } 
00926 
00927    dop.pszLogAddress      = pszLogAddress; 
00928    dop.pszDriverName      = (char*)pInfo->DriverName ();
00929    dop.pdriv              = pInfo->PQI3 ().pDriverData;
00930    dop.pszDataType        = pszDataType; 
00931    dop.pszComment         = pszApplicationName;
00932    dop.pszQueueProcName   = pInfo->PQI3 ().pszPrProc;     
00933    dop.pszQueueProcParams = 0;
00934    dop.pszSpoolerParams   = 0;     
00935    dop.pszNetworkParams   = 0;     
00936 
00937    hdc = ::DevOpenDC( 0, dcType, "*", 9, (PDEVOPENDATA) &dop, NULLHANDLE);
00938 
00939 #ifdef DEBUG
00940    if (hdc == 0)
00941    {
00942       ULONG ErrorCode = ERRORIDERROR (::WinGetLastError (0));
00943       printf ("!ERROR! - Can't open DC for printer %04lX\a\n", ErrorCode);
00944    }   
00945 #endif
00946 
00947    return hdc;
00948 }
00949 
00950 /* find the selected form */
00951 BOOL PrnQueryHardcopyCaps( HDC hdc, PHCINFO pHCInfo)
00952 {
00953    BOOL rc = FALSE;
00954 
00955    if( hdc && pHCInfo)
00956    {
00957       PHCINFO pBuffer;
00958       long    lAvail, i;
00959 
00960       /* query how many forms are available */
00961       lAvail = ::DevQueryHardcopyCaps( hdc, 0, 0, NULL);
00962 
00963       pBuffer = (PHCINFO) malloc( lAvail * sizeof(HCINFO));
00964 
00965       ::DevQueryHardcopyCaps( hdc, 0, lAvail, pBuffer);
00966 
00967       for( i = 0; i < lAvail; i++)
00968          if( pBuffer[ i].flAttributes & HCAPS_CURRENT)
00969          {
00970             memcpy( pHCInfo, pBuffer + i, sizeof(HCINFO));
00971             rc = TRUE;
00972             break;
00973          }
00974 
00975       free( pBuffer);
00976    }
00977 
00978    return rc;
00979 }
00980 
00981 
00982 /****************************************************************************/
00983 /*  Library-level data and functions    -Printing                           */
00984 /****************************************************************************/
00985 
00986 BOOL PrnInitialize( HMODULE hmodResources)
00987 {
00988    hmodRes = hmodResources;
00989    return TRUE;
00990 }
00991 
00992 BOOL PrnTerminate()
00993 {
00994    /* nop for now, may do something eventually */
00995    return TRUE;
00996 }
00997 
00998 BOOL PrnClosePrinter( PRTQUEUE *pPrintQueue)
00999 {
01000    BOOL rc = FALSE;
01001 
01002    if (pPrintQueue)
01003    {
01004       delete pPrintQueue;
01005       rc = TRUE;
01006    }
01007 
01008    return rc;
01009 }
01010