Another Notify Module for Linux

Share your advanced PureBasic knowledge/code with the community.
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Another Notify Module for Linux

Post by StarBootics »

Hello everyone,

A simple module to create notification on Linux based on a code snippet created by Oma (http://www.purebasic.fr/english/viewtop ... 13&t=62138)

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Linux Notify - Module
; File Name : Linux Notify - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 08-05-2015
; Last Update : 08-05-2015
; PureBasic code : V5.31
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programming Notes
;
; This module is based on the original code created by Oma (english forum)
; Source : http://www.purebasic.fr/english/viewtopic.php?f=13&t=62138
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule Notify
  
  Enumeration
    
    #URGENCY_LOW
    #URGENCY_NORMAL
    #URGENCY_CRITICAL
    
    #ICON_ERROR
    #ICON_INFORMATION
    #ICON_QUESTION
    #ICON_WARNING
    
  EndEnumeration
  
  Declare SetSummary(P_Summary.s)
  Declare SetUrgency(P_Urgency.b = -1)
  Declare SetIcon(P_Icon.b = -1)
  Declare SetBody(P_Body.s)
  Declare SetExpireTime(P_ExpireTime.l = 5000)
  Declare Reset()
  Declare Send()
  
EndDeclareModule

Module Notify

  Structure Instance

    Summary.s
    Body.s
    ExpireTime.l
    UrgencyString.s
    IconString.s

  EndStructure

  Global Instance.Instance

  Procedure SetSummary(P_Summary.s)

    Instance\Summary = P_Summary

  EndProcedure

  Procedure SetUrgency(P_Urgency.b = -1)

    Select P_Urgency
        
      Case #URGENCY_LOW
        Instance\UrgencyString = "--urgency=low"
        
      Case #URGENCY_NORMAL
        Instance\UrgencyString = "--urgency=normal"
        
      Case #URGENCY_CRITICAL
        Instance\UrgencyString = "--urgency=critical"
        
      Default 
        Instance\UrgencyString = "--urgency=normal"
        
    EndSelect
    
  EndProcedure

  Procedure SetIcon(P_Icon.b = -1)

    Select P_Icon
        
      Case #ICON_ERROR
        Instance\IconString = "--icon=dialog-error"
        
      Case #ICON_INFORMATION
        Instance\IconString = "--icon=dialog-information"
        
      Case #ICON_QUESTION
        Instance\IconString = "--icon=dialog-question"
        
      Case #ICON_WARNING
        Instance\IconString = "--icon=dialog-warning"
        
      Default
        Instance\IconString = "--icon=dialog-information"
        
    EndSelect

  EndProcedure

  Procedure SetBody(P_Body.s)

    Instance\Body = P_Body

  EndProcedure

  Procedure SetExpireTime(P_ExpireTime.l = 5000)

    Instance\ExpireTime = P_ExpireTime

  EndProcedure

  Procedure Reset()
    
    Instance\Summary = ""
    Instance\Body = ""
    Instance\ExpireTime = 0
    Instance\UrgencyString = ""
    Instance\IconString = ""
    
  EndProcedure
  
  Procedure Send()
    
    If Not RunProgram("notify-send", #DQUOTE$ + Instance\Summary + #DQUOTE$ + " " + #DQUOTE$ + Instance\Body + #DQUOTE$ + Instance\IconString + " " + Instance\UrgencyString + " --expire-time=" + Str(Instance\ExpireTime), "")
      Debug "'notify-send' not available!"
      Debug "install: sudo apt-get install libnotify-bin"
    EndIf
    
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Notify::SetSummary("My Application 2")
  Notify::SetUrgency(Notify::#URGENCY_NORMAL)
  Notify::SetIcon(Notify::#ICON_QUESTION)
  Notify::SetBody("A Desktop-Notification with 'notify-send'\nand a 2. line")
  Notify::SetExpireTime(10000)
  
  Notify::Send()
  Notify::Reset()
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !