Individuare le stampanti presenti nel proprio sistema

Da Gambas-it.org - Wikipedia.

Se si vogliono individuare le stampanti presenti e riconosciute nel proprio sistema, nonché la stampante impostata come predefinita, si possono adottare alcune modalità.

Uso della Classe Printer

La Classe Printer ci fornisce alcune proprietà statiche che assolvono alla funzione di cui in oggetto.

Vediamo un esempio pratico:

Public Sub Form_Open()

 Dim stampanti As String
 
 Print "Le stampanti installate sono:"
   
 For Each stampanti In Printer.List
   Print stampanti
 Next
   
 Print "\nLa stampante predefinita è:"
 Print Printer.Default
  
End

Leggere i file presenti nella cartella /etc/cups/ppd/

La seconda modalità prevede di leggere i file, creati per ciascuna di esse, nella cartella: /etc/cups/ppd/ [ Nota 1 ]. Ciascuno di tali file riporta il nome della stampante, alla quale si riferisce, nonché l'estensione .ppd. Pertanto, il compito è quello di prendere il solo nome del file, che corrisponde anche alla singola stampante, escludendo l'estensione.

Potremo dunque utilizzare questo semplice codice:

Public Sub Button1_Click()

 Dim stampante As String

 For Each stampante In Dir("/etc/cups/ppd/", "*.ppd").Sort() 
   Print File.BaseName(stampante)     
 Next

End

Usando alcune risorse della libreria esterna libcups.so.2

La terza modalità prevede l'uso di alcune risorse della libreria esterna libcups.so.2:

Library "libcups:2"

' int cupsGetDests(cups_dest_t **dests)
' Get the list of destinations from the default server.
Private Extern cupsGetDests(dests As Pointer) As Integer


Public Sub Main()

 Dim stamp_Def, predef, stampa As String
 Dim num_dests As Integer
 Dim p As Pointer
 Dim b As Byte

 num_dests = cupsGetDests(VarPtr(p))

 Print "Sono state riscontrate installate nel sistema num. "; num_dests; " stampanti:\n"

' Dereferenziando la variabile di tipo "Puntatore", si ottengono i nomi delle stampanti installate.
' In particolare ciascun nome è uguale a quello del file .ppd afferente alla stampante installata, presente nella cartella "/etc/cups/ppd" e privo della sua estensione.
 For b = 1 To num_dests
   stampa = String@(Pointer@(p))
   p = p + 16
   If Byte@(p) = 1 Then
     stamp_Def = stampa
     predef = " - Default\n"
   Endif
' Mostra in console il nome delle stampanti trovate installate nel sistema:
   Print b, stampa & predef
   predef = Null
   p = p + 16
 Next

End

Conoscere quale è la stampante impostata come predefinita

Se si desidera conoscere la stampante impostata come predefinita (default), potremo utilizzare quest'altro codice (si dovrà attivare il componente gb.desktop; e si dovrà aver installato nel proprio sistema l'applicazione xterm):

Public Sub Button1_Click()
 
 Dim fl As File  
 Dim s As String  
 Dim ss As New String[]  

' E' necessario avere il pieno accesso alla lettura del seguente file:
 Desktop.RunAsRoot("chmod 444 /etc/cups/printers.conf.O")
   
' Resta in attesa fino a che i permessi del file in questione non saranno cambiati:
 Repeat  
   Wait 0.01  
 Until Stat("/etc/cups/printers.conf.O").Auth = "r--r--r--"  
    
 fl = Open "/etc/cups/printers.conf.O" For Input
    
 While Not Eof(fl)  
   Input #fl, s
' Carica nell'array di tipo stringa tutte le informazioni, suddivise parola per parola, presenti nel file:
   ss.Add(Trim(s))
   If ss.Max > 0 Then
' Individua la stampante predefinita e ne mostra il nome in console:
     If ss[ss.Max - 1] = "<DefaultPrinter" Then Print "La stampante predefinita è: "; Replace(ss[ss.Max], ">", "")  
   Endif  
 Wend
  
 fl.Close
  
End

Si potrà utilizzare anche quest'altro codice [ nota 1 ], più breve, che non necessita di inserire alcuna password:

Public Sub Button1_Click()

 Dim fl As File    
 Dim s As String    
 Dim ss As New String[]      
      
 fl = Open "/etc/printcap" For Input    
       
 While Not Eof(fl)    
   Input #fl, s  
   ss.Add(Trim(s))    
   If ss.Max = 23 Then Print "La stampante predefinita è: "; Split(ss[23], "|")[0]  
 Wend
  
 fl.Close
  
End

Uso della libreria esterna libprintbackend-file.so

Si potrà fare uso della particolare libreria esterna "libprintbackend-file.so", facente parte del gruppo di risorse di GTK+, con la quale è possibile non solo individuare le stampanti presenti nel proprio sistema, ma anche sapere quale stampate è impostata come "predefinita".

Mostriamo un semplice esempio:

Library "/usr/lib/x86_64-linux-gnu/gtk-3.0/3.0.0/printbackends/libprintbackend-file"

' void gtk_init (int *argc, char ***argv)
' Call this function before using any other GTK+ functions in your GUI applications.
Private Extern gtk_init(argc As Pointer, argv As Pointer)

' void gtk_enumerate_printers (GtkPrinterFunc func, gpointer data, GDestroyNotify destroy, gboolean wait)
' Calls a function for all GtkPrinters.
Private Extern gtk_enumerate_printers(func As Pointer, data As Pointer, destroy As Pointer, gwait As Boolean)

' GtkPrinter * gtk_printer_new (const gchar *name, GtkPrintBackend *backend, gboolean virtual_)
' Creates a new GtkPrinter.
Private Extern gtk_printer_new(name As String, backend As Pointer, virtual As Boolean) As Pointer

' gboolean gtk_printer_is_default (GtkPrinter *printer)
' Returns whether the printer is the default printer.
Private Extern gtk_printer_is_default(gprinter As Pointer) As Boolean

' const gchar * gtk_printer_get_name (GtkPrinter *printer)
' Returns the name of the printer.
Private Extern gtk_printer_get_name(gprinter As Pointer) As String


Public Sub Main()
 
 gtk_init(0, 0)
 
 gtk_enumerate_printers(Mostra, 0, 0, True)
 
End


Private Procedure Mostra(stmp As Pointer, unused As Pointer)
 
 Dim stampante As String
 
 stampante = gtk_printer_get_name(stmp)
 
 Write "\e[0mStampante:    \e[31m" & stampante
 
 If gtk_printer_is_default(stmp) Then Write " - \e[34mpredefinita\e[0m"
 Print
 
End


Note

[1] Testato su Ubuntu.