< prev index next >

src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp

Print this page

        

@@ -42,11 +42,10 @@
 
 /* constants for DeviceCapability buffer lengths */
 #define PAPERNAME_LENGTH 64
 #define TRAYNAME_LENGTH 24
 
-
 static BOOL IsSupportedLevel(HANDLE hPrinter, DWORD dwLevel) {
     BOOL isSupported = FALSE;
     DWORD cbBuf = 0;
     LPBYTE pPrinter = NULL;
 

@@ -115,10 +114,66 @@
     }
 
     CATCH_BAD_ALLOC_RET(NULL);
 }
 
+JNIEXPORT jlong JNICALL
+Java_sun_print_PrintServiceLookupProvider_notifyFirstPrinterChange(JNIEnv *env,
+                                                                jobject peer,
+                                                                jstring printer) {
+    HANDLE hPrinter;
+
+    LPTSTR printerName = NULL;
+    if (printer != NULL) {
+        printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
+                                                         printer,
+                                                         NULL);
+        JNU_ReleaseStringPlatformChars(env, printer, printerName);
+    }
+
+    // printerName - "Win NT/2K/XP: If NULL, it indicates the local printer
+    // server" - MSDN.   Win9x : OpenPrinter returns 0.
+    BOOL ret = OpenPrinter(printerName, &hPrinter, NULL);
+    if (!ret) {
+      return (jlong)-1;
+    }
+
+    // PRINTER_CHANGE_PRINTER = PRINTER_CHANGE_ADD_PRINTER |
+    //                          PRINTER_CHANGE_SET_PRINTER |
+    //                          PRINTER_CHANGE_DELETE_PRINTER |
+    //                          PRINTER_CHANGE_FAILED_CONNECTION_PRINTER
+    HANDLE chgObj = FindFirstPrinterChangeNotification(hPrinter,
+                                                       PRINTER_CHANGE_PRINTER,
+                                                       0,
+                                                       NULL);
+    return (chgObj == INVALID_HANDLE_VALUE) ? (jlong)-1 : (jlong)chgObj;
+}
+
+
+
+JNIEXPORT void JNICALL
+Java_sun_print_PrintServiceLookupProvider_notifyClosePrinterChange(JNIEnv *env,
+                                                                jobject peer,
+                                                                jlong chgObject) {
+    FindClosePrinterChangeNotification((HANDLE)chgObject);
+}
+
+
+JNIEXPORT jint JNICALL
+Java_sun_print_PrintServiceLookupProvider_notifyPrinterChange(JNIEnv *env,
+                                                           jobject peer,
+                                                           jlong chgObject) {
+    DWORD dwChange;
+
+    DWORD ret = WaitForSingleObject((HANDLE)chgObject, INFINITE);
+    if (ret == WAIT_OBJECT_0) {
+        return(FindNextPrinterChangeNotification((HANDLE)chgObject,
+                                                  &dwChange, NULL, NULL));
+    } else {
+        return 0;
+    }
+}
 
 JNIEXPORT jobjectArray JNICALL
 Java_sun_print_PrintServiceLookupProvider_getAllPrinterNames(JNIEnv *env,
                                                           jobject peer)
 {

@@ -172,69 +227,82 @@
     return nameArray;
 
     CATCH_BAD_ALLOC_RET(NULL);
 }
 
+JNIEXPORT jobjectArray JNICALL
+Java_sun_print_PrintServiceLookupProvider_GetRemotePrintersNames(JNIEnv *env,
+                                                           jobject peer)
+{
+    TRY;
 
-JNIEXPORT jlong JNICALL
-Java_sun_print_PrintServiceLookupProvider_notifyFirstPrinterChange(JNIEnv *env,
-                                                                jobject peer,
-                                                                jstring printer) {
-    HANDLE hPrinter;
-
-    LPTSTR printerName = NULL;
-    if (printer != NULL) {
-        printerName = (LPTSTR)JNU_GetStringPlatformChars(env,
-                                                         printer,
-                                                         NULL);
-        JNU_ReleaseStringPlatformChars(env, printer, printerName);
-    }
+    int remotePrintersCount = 0;
+    DWORD cbNeeded = 0;
+    DWORD cReturned = 0;
+    LPBYTE pPrinterEnum = NULL;
+    LPBYTE pNetworkPrinterLoc = NULL;
 
-    // printerName - "Win NT/2K/XP: If NULL, it indicates the local printer
-    // server" - MSDN.   Win9x : OpenPrinter returns 0.
-    BOOL ret = OpenPrinter(printerName, &hPrinter, NULL);
-    if (!ret) {
-      return (jlong)-1;
+    jstring utf_str;
+    jclass clazz = env->FindClass("java/lang/String");
+    if (clazz == NULL) {
+        return NULL;
     }
+    jobjectArray nameArray;
 
-    // PRINTER_CHANGE_PRINTER = PRINTER_CHANGE_ADD_PRINTER |
-    //                          PRINTER_CHANGE_SET_PRINTER |
-    //                          PRINTER_CHANGE_DELETE_PRINTER |
-    //                          PRINTER_CHANGE_FAILED_CONNECTION_PRINTER
-    HANDLE chgObj = FindFirstPrinterChangeNotification(hPrinter,
-                                                       PRINTER_CHANGE_PRINTER,
-                                                       0,
-                                                       NULL);
-    return (chgObj == INVALID_HANDLE_VALUE) ? (jlong)-1 : (jlong)chgObj;
-}
+    try {
+        ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
+                       NULL, 4, NULL, 0, &cbNeeded, &cReturned);
+        pPrinterEnum = new BYTE[cbNeeded];
+        pNetworkPrinterLoc = new BYTE[cbNeeded/sizeof(PRINTER_INFO_4)];
+        ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
+                       NULL, 4, pPrinterEnum, cbNeeded, &cbNeeded,
+                       &cReturned);
 
+        if (cReturned > 0) {
+            for (DWORD i = 0; i < cReturned; i++) {
+                PRINTER_INFO_4 *info4 = (PRINTER_INFO_4 *) (pPrinterEnum + i * sizeof(PRINTER_INFO_4));
 
+                // PRINTER_ATTRIBUTE_NETWORK = 0x00000010
+                // Store the network printers indexes
+                if(info4->Attributes & 0x00000010) {
+                    pNetworkPrinterLoc[remotePrintersCount++] = i;
+                }
+            }
 
-JNIEXPORT void JNICALL
-Java_sun_print_PrintServiceLookupProvider_notifyClosePrinterChange(JNIEnv *env,
-                                                                jobject peer,
-                                                                jlong chgObject) {
-    FindClosePrinterChangeNotification((HANDLE)chgObject);
-}
+            // Allocate space only for the network type printers
+            nameArray = env->NewObjectArray(remotePrintersCount, clazz, NULL);
+            if (nameArray == NULL) {
+                throw std::bad_alloc();
+            }
+        } else {
+            nameArray = NULL;
+        }
 
+        // Loop thro' network printers list only
+        for (int i = 0; i < remotePrintersCount; i++) {
+            PRINTER_INFO_4 *info4 = (PRINTER_INFO_4 *)
+                (pPrinterEnum + pNetworkPrinterLoc[i] * sizeof(PRINTER_INFO_4));
+            utf_str = JNU_NewStringPlatform(env, info4->pPrinterName);
+            if (utf_str == NULL) {
+                throw std::bad_alloc();
+            }
+            env->SetObjectArrayElement(nameArray, i, utf_str);
+            env->DeleteLocalRef(utf_str);
+        }
+    } catch (std::bad_alloc&) {
+        delete [] pPrinterEnum;
+        delete [] pNetworkPrinterLoc;
+        throw;
+    }
 
-JNIEXPORT jint JNICALL
-Java_sun_print_PrintServiceLookupProvider_notifyPrinterChange(JNIEnv *env,
-                                                           jobject peer,
-                                                           jlong chgObject) {
-    DWORD dwChange;
+    delete [] pPrinterEnum;
+    delete [] pNetworkPrinterLoc;
+    return nameArray;
 
-    DWORD ret = WaitForSingleObject((HANDLE)chgObject, INFINITE);
-    if (ret == WAIT_OBJECT_0) {
-        return(FindNextPrinterChangeNotification((HANDLE)chgObject,
-                                                  &dwChange, NULL, NULL));
-    } else {
-        return 0;
-    }
+    CATCH_BAD_ALLOC_RET(NULL);
 }
 
-
 JNIEXPORT jfloatArray JNICALL
 Java_sun_print_Win32PrintService_getMediaPrintableArea(JNIEnv *env,
                                                   jobject peer,
                                                   jstring printer,
                                                   jint  papersize)
< prev index next >