Far mostrare sulla Scrivania i messaggi della specifica Desktop Notifications mediante le funzioni esterne del API di Libnotify

Da Gambas-it.org - Wikipedia.

La libreria Libnotify è un'implementazione della specifica Desktop Notifications. Essa permette di mostrare le classiche piccole finestre popup passive sul desktop che informano l'utente di particolari eventi.

Per poter fruire in Gambas delle sue risorse, è necessario installare ed utilizzare la libreria condivisa: "libnotify.so.4.0.0 ".

Mostriamo un semplice esempio dimostrativo di base:

Library "libnotify:4.0.0"

Private Enum NOTIFY_URGENCY_LOW = 0, NOTIFY_URGENCY_NORMAL, NOTIFY_URGENCY_CRITICAL

' gboolean notify_init (const char *app_name)
' Initialized libnotify.
Private Extern notify_init(app_name As String) As Boolean

' NotifyNotification * notify_notification_new (const char *summary, const char *body, const char *icon)
' Creates a new NotifyNotification.
Private Extern notify_notification_new(summary As String, body As String, icon As String) As Pointer

' void notify_notification_set_urgency (NotifyNotification *notification, NotifyUrgency urgency)
' Sets the urgency level of this notification. 
Private Extern notify_notification_set_urgency(notification As Pointer, urgency As Integer)

' gboolean notify_notification_show (NotifyNotification *notification, GError **error)
' Tells the notification server to display the notification on the screen.
Private Extern notify_notification_show(notification As Pointer, gerror 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 nome As String
 Dim notify As Pointer
 
 nome = "Notifica"
 
 notify_init(nome)
   
 notify = notify_notification_new(nome, "Testo della notifica", Null)
 If notify == 0 Then Error.Raise("Errore !")
   
' Imposta il livello di urgenza della notifica:
 notify_notification_set_urgency(notify, NOTIFY_URGENCY_NORMAL)
 notify_notification_show(notify, 0)
   
 g_object_unref(notify)

End

Da rilevare che riguardo al 2° argomento della funzione esterna "notify_notification_new()":
- il testo, in esso contenuto, può essere manipolato con i seguenti tag html:

<b>....</b>
<i>....</i>
<u>....</u>

- esso può contenere anche un collegamento web, cliccando sul quale sarà aperta nel webbrowser di sistema la pagina ivi indicata:

notify = notify_notification_new(..., "https://www.gambas-it.org", ...)

Mostrare un'icona

E' possibile anche mostrare un'icona all'interno della finestra della notifica.

Sono possibili due modalità.

1a modalità:

Library "libnotify:4.0.0"

Private Enum NOTIFY_URGENCY_LOW = 0, NOTIFY_URGENCY_NORMAL, NOTIFY_URGENCY_CRITICAL

' gboolean notify_init (const char *app_name)
' Initialized libnotify.
Private Extern notify_init(app_name As String) As Boolean

' NotifyNotification * notify_notification_new (const char *summary, const char *body, const char *icon)
' Creates a new NotifyNotification.
Private Extern notify_notification_new(summary As String, body As String, icon As String) As Pointer

' void notify_notification_set_urgency (NotifyNotification *notification, NotifyUrgency urgency)
' Sets the urgency level of this notification. 
Private Extern notify_notification_set_urgency(notification As Pointer, urgency As Integer)

' gboolean notify_notification_show (NotifyNotification *notification, GError **error)
' Tells the notification server to display the notification on the screen.
Private Extern notify_notification_show(notification As Pointer, gerror 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 nome As String
 Dim notify As Pointer

 nome = "Notifica"

 notify_init(nome)

' Nel 3° argomento della seguente funzione esterna si inserirà il percorso del file dell'immagine da mostrare:
 notify = notify_notification_new(nome, "Testo della notifica", "/percorso/del/file/immagine")
 If notify == 0 Then Error.Raise("Errore !")
   
' Imposta il livello di urgenza della notifica:
 notify_notification_set_urgency(notify, NOTIFY_URGENCY_NORMAL)
 notify_notification_show(notify, 0)
   
 g_object_unref(notify)

End

2a modalità:

Library "libnotify:4.0.0"

Private Enum NOTIFY_URGENCY_LOW = 0, NOTIFY_URGENCY_NORMAL, NOTIFY_URGENCY_CRITICAL

' gboolean notify_init (const char *app_name)
' Initialized libnotify.
Private Extern notify_init(app_name As String) As Boolean

' NotifyNotification * notify_notification_new (const char *summary, const char *body, const char *icon)
' Creates a new NotifyNotification.
Private Extern notify_notification_new(summary As String, body As String, icon As String) As Pointer

' GVariant* g_variant_new_string (const gchar* string)
' Creates a string GVariant with the contents of string.
Private Extern g_variant_new_string(_string As String) As Pointer

' void notify_notification_set_hint (NotifyNotification* notification, const char* key, GVariant* value)
' Sets a hint for key with value value.
Private Extern notify_notification_set_hint(notification As Pointer, key As String, value As Pointer)

' void notify_notification_set_urgency (NotifyNotification *notification, NotifyUrgency urgency)
' Sets the urgency level of this notification. 
Private Extern notify_notification_set_urgency(notification As Pointer, urgency As Integer)

' gboolean notify_notification_show (NotifyNotification *notification, GError **error)
' Tells the notification server to display the notification on the screen.
Private Extern notify_notification_show(notification As Pointer, gerror 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 nome As String
 Dim notify As Pointer
 
 nome = "Notifica"
 
 notify_init(nome)
   
 notify = notify_notification_new(nome, "Testo della notifica", Null)
 If notify == 0 Then Error.Raise("Errore !")

 gv = g_variant_new_string("/percorso/del/file/immagine")
 notify_notification_set_hint(notify, "image-path", gv)
   
' Imposta il livello di urgenza della notifica:
 notify_notification_set_urgency(notify, NOTIFY_URGENCY_NORMAL)
 notify_notification_show(notify, 0)
   
 g_object_unref(notify)

End


Riferimenti