Estrarre l'icona di un tipo di file

Da Gambas-it.org - Wikipedia.

Quando un tipo di file è associato ad una determinata icona, tutti i file appartenenti a quel tipo saranno mostrati sul desktop con quell'icona.

Per estrarre l'icona associata ad un tipo di file, si potranno utilizzare due modalità: una con le funzioni proprie di Gambas, l'altra con le funzioni esterne delle librerie libgio-2.0, libgtk-3 e libgdk_pixbuf-2.0.


Uso delle funzioni di Gambas

L'uso delle funzioni di Gambas prevede l'attivazione dei componenti gb.desktop e gb.desktop.x11, con il quale si potranno utilizzare due suoi metodi.


Uso del Metodo "Desktop.GetFileIcon()"

Il Metodo "Desktop.GetFileIcon()" ritorna l'icona, come Picture, associata ad un tipo di file.

Nel seguente semplice esempio poniamo sul Form dell'applicazione una PictureBox, nella quale verrà mostrata l'icona associata al tipo di file prescelto:

Public Sub Form_Open()

  PictureBox1.Picture = Desktop.GetFileIcon("/percorso/del/file", 48)

End

Il secondo parametro, che nell'esempio abbiamo posto con valore 48, rappresenta la dimensione espressa in pixel, con la quale sarà mostrata l'icona nella PictureBox.


Uso della Classe DesktopMime

Della la Classe DesktopMime useremo i Metodi ".FromFile()" e ".GetIcon()".

Mostriamo due semplici modalità.
Attiviamo il Componente gb.desktop e poniamo sul Form dell'applicazione un Button e una PictureBox, nella quale sarà mostrata l'icona del tipo di file individuato.

Prima modalità:

Public Sub Button1_Click()

 PictureBox1.Image = DesktopMime.FromFile("/percorso/del/file").GetIcon(48)

End

Anche in quest'altro caso il valore richiesto dal Metodo ".GetIcon()", che nell'esempio abbiamo posto con valore 48, rappresenta la dimensione espressa in pixel, con la quale sarà mostrata l'icona nella PictureBox.

Seconda modalità:

Public Sub Button1_Click()

 Dim tp As String
 Dim dm As DesktopMime

 tp = DesktopMime.FromFile("/percorso/del/file").Type
 
 dm = DesktopMime[tp]
 
 PictureBox1.Image = dm.GetIcon(32)
 
End


Uso delle librerie esterne libgio-2.0, libgtk-3 e libgdk_pixbuf-2.0

Quest'altra soluzione presuppone l'esistenza nel sistema delle librerie esterne libgtk-3.so.0.2411.32, libgio-2.0.so.0.8000.4 e libgdk_pixbuf-2.0.so.0.4200.12 .

Di seguito mostriamo due modalità legate a detta soluzione, delle quali saranno mostrati due esempi che richiedono entrambi:

  • sia attivato comunque il componente gb.gtk3 (servirà per mostrare l'icona associata al tipo del file);
  • sia posta sul Form una PictureBox.

Prima modalità

Library "libgtk-3:0.2411.32"

Private Enum GTK_ICON_LOOKUP_NO_SVG = 1,
             GTK_ICON_LOOKUP_FORCE_SVG = 2,
             GTK_ICON_LOOKUP_USE_BUILTIN = 4,
             GTK_ICON_LOOKUP_GENERIC_FALLBACK = 8,
             GTK_ICON_LOOKUP_FORCE_SIZE = 16

' void gtk_init(int * argc, utf8 * argv)
' Initializes GTK library.
Private Extern gtk_init(argc As Pointer, argv As Pointer)

' GtkIconTheme* gtk_icon_theme_get_default (void)
' Gets the icon theme for the default screen.
Private Extern gtk_icon_theme_get_default() As Pointer

' GtkIconInfo* gtk_icon_theme_lookup_by_gicon (GtkIconTheme *self, GIcon* icon, gint size, GtkIconLookupFlags flags)
' Looks up an icon and returns a structure containing information such as the filename of the icon.
Private Extern gtk_icon_theme_lookup_by_gicon(self As Pointer, icon As Pointer, size As Integer, flags As Integer) As Pointer

' const gchar* gtk_icon_info_get_filename (GtkIconInfo * icon_info)
' Gets the filename for the icon.
Private Extern gtk_icon_info_get_filename(icon_info As Pointer) As String


Library "libgio-2.0:0.8000.4"

' gchar * g_content_type_guess (const gchar *filename, const guchar *data, gsize data_size, gboolean *result_uncertain)
' Guesses the content type based on example data.
Private Extern g_content_type_guess(filename As String, data As Pointer, data_size As Integer, result_uncertain As Pointer) As Pointer

' GIcon * g_content_type_get_icon (const gchar *type)
' Gets the icon for a content type.
Private Extern g_content_type_get_icon(gtype As Pointer) As Pointer


Public Sub Form_Open()

 Dim tipo, bo, ico, info As Pointer
 Dim nomefile As String

 tipo = g_content_type_guess("/percorso/del/file", 0, 0, bo)
 Print "Il mime-type del file è: \e[1m"; String@(tipo)

 gtk_init(0, 0)
 ico = g_content_type_get_icon(tipo)
 If ico == 0 Then Error.Raise("Impossibile ottenere l'icona del mime-type !")

 info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), ico, 48, GTK_ICON_LOOKUP_GENERIC_FALLBACK)

 nomefile = gtk_icon_info_get_filename(info)
 Print " \e[0mIl file immagine specifico dell'icona associata al tipo di file è: "; nomefile

 PictureBox1.Image = Image.Load(nomefile)

End

Seconda modalità

Library "libgtk-3:0.2411.32"

Private Enum GTK_ICON_LOOKUP_NO_SVG = 1,
             GTK_ICON_LOOKUP_FORCE_SVG = 2,
             GTK_ICON_LOOKUP_USE_BUILTIN = 4,
             GTK_ICON_LOOKUP_GENERIC_FALLBACK = 8,
             GTK_ICON_LOOKUP_FORCE_SIZE = 16

' void gtk_init(int * argc, utf8 * argv)
' Initializes GTK library.
Private Extern gtk_init(argc As Pointer, argv As Pointer)

' GtkIconTheme* gtk_icon_theme_get_default (void)
' Gets the icon theme for the default screen.
Private Extern gtk_icon_theme_get_default() As Pointer

' GtkIconInfo* gtk_icon_theme_lookup_by_gicon (GtkIconTheme *self, GIcon* icon, gint size, GtkIconLookupFlags flags)
' Looks up an icon and returns a structure containing information such as the filename of the icon.
Private Extern gtk_icon_theme_lookup_by_gicon(self As Pointer, icon As Pointer, size As Integer, flags As Integer) As Pointer

' const gchar* gtk_icon_info_get_filename (GtkIconInfo * icon_info)
' Gets the filename for the icon.
Private Extern gtk_icon_info_get_filename(icon_info As Pointer) As String


Library "libgio-2.0:0.8000.4"

Private Enum G_FILE_QUERY_INFO_NONE = 0, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS

' GFile * g_file_new_for_path (const char *path)
' Constructs a GFile for a given path.
Private Extern g_file_new_for_path(path 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

' GIcon * g_file_info_get_icon (GFileInfo *info)
' Gets the icon for a file.
Private Extern g_file_info_get_icon(GFileInfo As Pointer) As Pointer

' gchar * g_icon_to_string (GIcon *icon)
' Generates a textual representation of icon that can be used for serialization.
Private Extern g_icon_to_string(GIcon As Pointer) As String


Public Sub Form_Open()

 Dim percorso, ico, nomefile As String
 Dim gfl, gfi, gic, info As Pointer

 percorso = "/percorso/del/file"
 
 gfl = g_file_new_for_path(percorso)
 If gfl == 0 Then Error.Raise("Impossibile caricare il file !")

 gfi = g_file_query_info(gfl, "standard::icon", G_FILE_QUERY_INFO_NONE, 0, 0)
 If gfi == 0 Then Error.Raise("Impossibile richiedere informazioni sul file !")

 gic = g_file_info_get_icon(gfi)
 If gic == 0 Then Error.Raise("Impossibile ottenere l'icona del file caricato !")

 ico = g_icon_to_string(gic)
 Print "La rappresentazione testuale dell'icona del file è:", ico
  
 gtk_init(0, 0)
  
 info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), gic, 48, GTK_ICON_LOOKUP_GENERIC_FALLBACK)
  
 nomefile = gtk_icon_info_get_filename(info)
 Print "Il file immagine dell'icona associata al tipo di file è: "; nomefile

 PictureBox1.Image = Image.Load(nomefile)

End

Terza modalità

Library "libgtk-3:0.2411.32"

Private Enum GTK_ICON_SIZE_INVALID = 0, GTK_ICON_SIZE_MENU, GTK_ICON_SIZE_SMALL_TOOLBAR, GTK_ICON_SIZE_LARGE_TOOLBAR,
             GTK_ICON_SIZE_BUTTON, GTK_ICON_SIZE_DND, GTK_ICON_SIZE_DIALOG
            
Private Enum GTK_ICON_LOOKUP_NO_SVG = 0, GTK_ICON_LOOKUP_FORCE_SVG, GTK_ICON_LOOKUP_USE_BUILTIN, GTK_ICON_LOOKUP_GENERIC_FALLBACK, GTK_ICON_LOOKUP_FORCE_SIZE,
             GTK_ICON_LOOKUP_FORCE_REGULAR, GTK_ICON_LOOKUP_FORCE_SYMBOLIC, GTK_ICON_LOOKUP_DIR_LTR, GTK_ICON_LOOKUP_DIR_RTL

' GtkIconTheme * gtk_icon_theme_get_default (void)
' Gets the icon theme for the default screen.
Private Extern gtk_icon_theme_new() As Pointer

' gboolean gtk_icon_size_lookup (GtkIconSize size, gint *width, gint *height)
' Obtains the pixel size of a semantic icon size size.
Private Extern gtk_icon_size_lookup(size As Integer, width As Pointer, height As Pointer) As Boolean

' GtkIconInfo * gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme, GIcon *icon, gint size, GtkIconLookupFlags flags)
' Looks up an icon and returns a GtkIconInfo containing information such as the filename of the icon.
Private Extern gtk_icon_theme_lookup_by_gicon(icon_theme As Pointer, icon As Pointer, size As Integer, flags As Integer) As Pointer

' GdkPixbuf * gtk_icon_info_load_icon (GtkIconInfo *icon_info, GError **error)
' Renders an icon previously looked up in an icon theme using gtk_icon_theme_lookup_icon().
Private Extern gtk_icon_info_load_icon(icon_info As Pointer, gerror As Pointer) As Pointer


Library "libgio-2.0:0.8000.4"

Private Enum G_FILE_QUERY_INFO_NONE = 0, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
Private Const G_FILE_ATTRIBUTE_STANDARD_ICON As String = "standard::icon"

' GFile * g_file_new_for_path (const char *path)
' Constructs a GFile for a given path.
Private Extern g_file_new_for_path(path 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

' GIcon * g_file_info_get_icon (GFileInfo *info)
' Gets the icon for a file.
Private Extern g_file_info_get_icon(info As Pointer) As Pointer

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


Library "libgdk_pixbuf-2.0:0.4200.12"

' gboolean gdk_pixbuf_save (GdkPixbuf *pixbuf, const char *filename, const char *type, GError **error, ...)
' Saves pixbuf to a file in format type.
Private Extern gdk_pixbuf_save(pixbuf As Pointer, filename As String, type As String, gerror As Pointer, ptr As Pointer) As Boolean


Public Sub Main()
 
 Dim gf, gfi, ico As Pointer
 Dim git, gii, pbuf As Pointer
 Dim w As Integer
 
 gf = g_file_new_for_path("/percorso/del/file")
 If gf == 0 Then Error.Raise("ERRORE !")
 
 gfi = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, 0, 0)
 If gfi == 0 Then Error.Raise("ERRORE !")
 
 ico = g_file_info_get_icon(gfi)
 If ico == 0 Then Error.Raise("ERRORE !")
 
 git = gtk_icon_theme_new()
 If git == 0 Then Error.Raise("ERRORE !")

 gtk_icon_size_lookup(GTK_ICON_SIZE_DIALOG, VarPtr(w), 0)
 
 gii = gtk_icon_theme_lookup_by_gicon(git, ico, w, GTK_ICON_LOOKUP_USE_BUILTIN)
 If gii == 0 Then Error.Raise("ERRORE !")

 pbuf = gtk_icon_info_load_icon(gii, 0)
 If pbuf == 0 Then Error.Raise("ERRORE !")

' Salva l'immagine dell'icona in un file immagine:
 gdk_pixbuf_save(pbuf, "/tmp/test.png", "png", 0, 0)
 
' Libera la memoria utilizzata con la libreria Libgio-2.0:
 g_object_unref(pbuf)
 g_object_unref(gii)
 g_object_unref(gfi)
 g_object_unref(gf)
 
End


Riferimenti