Ottenere informazioni sulle icone presenti sulla Scrivania

Da Gambas-it.org - Wikipedia.
Versione del 23 nov 2024 alle 09:30 di Vuott (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

In via generale, per ottenere informazioni sui file e anche sulle icone a essi correlate, si potranno usare alcune funzioni esterne della libreria: "libgio-2.0.so.0.8000.5 ".

Mostriamo un semplice codice, con il quale si conosceranno varie informazioni su un file e sulla sua icona correlata:

Library "libgio-2.0:0.8000.5"

' GFile * g_file_new_for_commandline_arg (const char *arg)
' Creates a GFile with the given argument from the command line.
Private Extern g_file_new_for_commandline_arg(arg As String) As Pointer

' GFileInfo * g_file_query_info (GFile *file, const char *attributes, GFileQueryInfoFlags flags, GCancellable *cancellable, GError **error )
' Gets the requested information about specified file.
Private Extern g_file_query_info(gfile As Pointer, attributes As String, flags As Integer, cancellable As Pointer, gerror As Pointer) As Pointer

' char ** g_file_info_list_attributes (GFileInfo *info, const char *name_space)
' Lists the file info structure's attributes.
Private Extern g_file_info_list_attributes(info As Pointer, name_space As String) As Pointer

' char * g_file_info_get_attribute_as_string (GFileInfo *info, const char *attribute)
' Gets the value of a attribute, formatted as a string.
Private Extern g_file_info_get_attribute_as_string(info As Pointer, attribute As String) As Pointer

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(gobject As Pointer)


Public Sub Main()
 
 Dim percorso As String
 Dim fl, info As Pointer
 
 percorso = "/percorso/del/file"
 
 fl = g_file_new_for_commandline_arg(percorso)
 If fl == 0 Then Error.Raise("Errore !")
 
 info = g_file_query_info(fl, "*", 0, Null, Null)
 If info == 0 Then Error.Raise("Errore !")
   
 MostraAttributi(info)
   
 g_object_unref(info)
 g_object_unref(fl)
  
End


Private Procedure MostraAttributi(inf As Pointer)
 
 Dim attr, p As Pointer
 Dim i As Integer
 Dim s As String
 
 attr = g_file_info_list_attributes(inf, Null)
 
 Print "ATTRIBUTI:\n"
 s = String@(Pointer@(attr + (i * 8)))
 Repeat 
   p = g_file_info_get_attribute_as_string(inf, s)
   Print String@(Pointer@(attr + (i * 8))); ":  \e[31m"; String@(p); "\e[0m"
   Inc i
   Wait 0.1
   s = String@(Pointer@(attr + (i * 8)))
 Until IsNull(s)
 
End


Individuare le icone poste sulla Scrivania passandoci sopra con il puntatore del mouse

Mostriamo di seguito un codice che, utilizzando nuovamente le già viste funzioni esterne della libreria "libgio-2.0", permette di individuare le icone poste sulla Scrivania, mentre si sorvola su di esse con il puntatore del mouse, e di ottenere informazioni sulle icone stesse e sui file, ai quali esse sono collegate.
E' necessario attivare anche il Componente "gb.desktop".

Public Struct IconaFileDesktop
  nome As String
  x As Short
  y As Short
  rt As Rect
End Struct
Private icone As New IconaFileDesktop[]
Private PERCORSO As String = Desktop.GetDirectory("DESKTOP")
Private TImer1 As Timer


Public Sub Form_Open()
 
 Dim nomefile, s As String
 Dim ifd As IconaFileDesktop
 
' Carica tutti i file corrispondenti alle icone presenti sulla Scrivania:
 For Each nomefile In Dir(PERCORSO, "*", gb.File)
' Usa alcune funzioni esterne della libreria "libgio-2.0":
   s = LeggeInfo(PERCORSO &/ nomefile)
   If IsNull(s) Then Continue 
' Carica nella "Struttura" i dati utili del file individuato dalla funzione "Dir()":
   With ifd = New IconaFileDesktop
     .nome = nomefile
     .x = Val(Scan(s, "*,*")[0])
     .y = Val(Scan(s, "*,*")[1])
' Stabilisce un'area quadrata standard di 48x48 pixel per ciascuna icona di file individuato:
     .rt = New Rect(.x, .y, 48, 48)
   End With
   icone.Push(ifd)
 Next
 
 With timer1 = New Timer As "Timer1"
   .Delay = 50
   .Start
 End With
 
End


Public Sub Timer1_Timer()
 
 Dim c As Short
 
 TextArea1.Clear
 
 For c = 0 To icone.Max
' Se le coordinate x,y in pixel correnti in cui si trova il puntatore del mouse rientrano in un'area quadrata caricata, va a vedere a quale icona e file appartiene:
   If icone[c].rt.Contains(Mouse.ScreenX, Mouse.ScreenY) Then 
' Mostra alcune caratteristiche del file al quale corrisponde l'icona sorvolata dal puntatore del mouse:
     TextArea1.Text = "== Caratteristiche del file ==\n" &
                      "\nPercorso:        " & Stat(PERCORSO &/ icone[c].nome).Path &
                      "\nDimensione:      " & Stat(PERCORSO &/ icone[c].nome).Size & " Byte" &
                      "\nUltimo Accesso:  " & Stat(PERCORSO &/ icone[c].nome).LastAccess &
                      "\nUltima modifica: " & Stat(PERCORSO &/ icone[c].nome).LastModified &
                      "\nPermessi:        " & Stat(PERCORSO &/ icone[c].nome).Auth &
                      "\nUtente:          " & Stat(PERCORSO &/ icone[c].nome).User &
                      "\nGruppo:          " & Stat(PERCORSO &/ icone[c].nome).Group &
                      "\nMimetype icona:  " & DesktopMime.FromFile(Stat(PERCORSO &/ icone[c].nome).Path).Type
   Endif
 Next
 
End


 Library "libgio-2.0:0.8000.5"

' GFile * g_file_new_for_commandline_arg (const char *arg)
' Creates a GFile with the given argument from the command line.
Private Extern g_file_new_for_commandline_arg(arg As String) As Pointer

' GFileInfo * g_file_query_info (GFile *file, const char *attributes, GFileQueryInfoFlags flags, GCancellable *cancellable, GError **error )
' Gets the requested information about specified file.
Private Extern g_file_query_info(gfile As Pointer, attributes As String, flags As Integer, cancellable As Pointer, gerror As Pointer) As Pointer

' char ** g_file_info_list_attributes (GFileInfo *info, const char *name_space)
' Lists the file info structure's attributes.
Private Extern g_file_info_list_attributes(info As Pointer, name_space As String) As Pointer

' char * g_file_info_get_attribute_as_string (GFileInfo *info, const char *attribute)
' Gets the value of a attribute, formatted as a string.
Private Extern g_file_info_get_attribute_as_string(info As Pointer, attribute As String) As Pointer

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(gobject As Pointer)


Private Function LeggeInfo(percfile As String) As String
 
 Dim fl, info As Pointer
 
 fl = g_file_new_for_commandline_arg(percfile)
 If fl == 0 Then Error.Raise("Errore !")
   
 info = g_file_query_info(fl, "*", 0, Null, Null)
 If info == 0 Then Error.Raise("Errore !")
   
 percfile = EstraeAttributi(info)
   
 g_object_unref(info)
 g_object_unref(fl)
 
 Return percfile
 
End


Private Function EstraeAttributi(inf As Pointer) As String
 
 Dim attr, p As Pointer
 Dim i As Integer = -1
 Dim s As String
 
 attr = g_file_info_list_attributes(inf, Null)
 
 Repeat 
   Inc i
   s = String@(Pointer@(attr + (i * SizeOf(gb.Pointer))))
   p = g_file_info_get_attribute_as_string(inf, s)
 Until s == Trim("metadata::nemo-icon-position")
 
 Return String@(p)
 
End