' Gambas class file PUBLIC sPathInfo AS String = "/var/tmp/update-watch-info." & System.User.Id PRIVATE sPathRpm AS String = "/var/lib/rpm/Packages" PRIVATE sPathApt AS String = "/var/lib/update-watch/run-apt-update" PRIVATE dtInfo AS Date ' hold last modified date of sPathInfo PRIVATE dtRpm AS Date ' hold last modified date of sPathRpm PRIVATE dtApt AS Date ' hold last modified date of sPathApt PRIVATE dtInfoPrev AS Date ' hold previous date of dtInfo to reduce info file parsing PRIVATE sTip AS String ' hold tray icon's tooltip. *Note: not set temporary (warnig or error) text here PRIVATE bReboot AS Boolean = FALSE ' set TRUE if deciede that reboot is required. PUBLIC SUB tmTimer_Timer() DIM hUpdateInfo AS File ' handler to read update-watch-info file DIM sLine AS String DIM iNew, iUpgrade, iRemove, iHold AS Integer ' should stop timer wihle precessing to avoid error tmTimer.Stop IF CheckRebootRequired() = FALSE AND IF GetLastModifiedDates() = TRUE THEN ' check upgrade-able packages if rpm DB or apt cache is update IF dtInfo <= dtApt OR IF dtInfo <= dtRpm THEN IF IsSynAptRunning() = FALSE THEN SHELL "apt-get script --simulate /usr/lib/update-watch/check-upgrades.lua > /dev/null 2>&1" WAIT dtInfo = Stat(sPathInfo).LastModified ENDIF ENDIF IF Stat(sPathInfo).Size > 0 THEN IF dtInfo <> dtInfoPrev THEN ' read update-watch-info file if any updates exist and the file is updated hUpdateInfo = OPEN sPathInfo FOR READ WHILE NOT Eof(hUpdateInfo) LINE INPUT #hUpdateInfo, sLine SELECT CASE Split(sLine, ":")[0] CASE "new" iNew += 1 CASE "upgrade" iUpgrade += 1 CASE "remove" iRemove += 1 CASE "hold" iHold += 1 END SELECT WEND sTip = "" ' need to clear tooltip text for the case of holded pacakges only exist IF iUpgrade + inew + iRemove >= 1 THEN sTip = CStr(iUpgrade + inew + iRemove) & (" updates found") IF iHold >= 1 THEN sTip = sTip & "\n" ENDIF IF iHold >= 1 THEN sTip = sTip & ("Also not upgraded ") & iHold & (" packages are found\nPlease run package manager to update it") ENDIF dtInfoPrev = dtInfo ' show notification panel to notify updates EXEC ["notify-send", "-u", "normal", "-i", "/usr/share/pixmaps/update-watch-red.png", ("Updates notification"), sTIp] ENDIF tiTrayIcon.Tooltip = sTip tiTrayicon.Picture = Picture["red.png"] hMenuApply.Enabled = TRUE hMenuShow.Enabled = TRUE ELSE sTip = ("No updates found") tiTrayIcon.Tooltip = sTip tiTrayicon.Picture = Picture["blue.png"] hMenuApply.Enabled = FALSE hMenuShow.Enabled = FALSE ENDIF ENDIF IsSynAptRunning() tmTimer.Start END PUBLIC SUB tiTrayIcon_Menu() ' show context menu when tray icon is right clicked ' and disable menu items if apt-get or synaptic is running IsSynAptRunning() hMenu.Popup() END PUBLIC SUB hMenuApply_Click() ' run synaptic to exec dist-upgrade only if apt-get or synaptic is not running IF IsSynAptRunning() = FALSE THEN SHELL "gksu -D synaptic \"synaptic --upgrade-mode --non-interactive --hide-main-window\" > /dev/null 2>&1" END PUBLIC SUB hMenuCheck_Click() ' run synaptic to exec apt-get update only if apt-get or synaptic is not running. ' also execute update-watch.lua script to update run-apt-update file ' because synaptic does not handle Script::AptGet::Update::Post script slot. IF IsSynAptRunning() = FALSE THEN SHELL "gksu -D synaptic \"synaptic --update-at-startup --non-interactive --hide-main-window; apt-get script /usr/share/apt/scripts/update-watch.lua\" > /dev/null 2>&1" END PUBLIC SUB hMenuShow_Click() ' show list of updates FList.Show() END PUBLIC SUB hMenuRun_Click() ' run synaptic only if apt-get or synaptic is not running IF IsSynAptRunning() = FALSE THEN SHELL "gksu synaptic > /dev/null 2>&1" END PUBLIC SUB hMenuHelp_Click() ' show html help EXEC ["xdg-open", "/usr/share/doc/update-watch-" & Application.Version & "/help/index.html"] END PUBLIC SUB hMenuQuit_Click() ' should stop timer wihle precessing to avoid error tmTimer.Stop ME.Close() END PUBLIC SUB tiTrayIcon_DblClick() ' show list of updates hMenuShow_Click() END PRIVATE FUNCTION CheckRebootRequired() AS Boolean DIM fUptime, fModifiedtime AS Float DIM sLine AS String ' check trigger file and prompt reboot if the file exists IF Exist("/var/run/reboot-required") = TRUE THEN sTip = ("To complete the update of your system, restart your system") tiTrayIcon.Tooltip = sTip tiTrayIcon.Picture = Stock["24/error"] IF bReboot = FALSE THEN ' should show reboot notification only once EXEC ["notify-send", "-u", "critical", "-t", "0", "-i", "error", ("Restart required"), ("To complete the update of your system, restart your system")] bReboot = TRUE ENDIF RETURN TRUE ELSE RETURN FALSE ENDIF END PRIVATE FUNCTION GetLastModifiedDates() AS Boolean ' set oldest date if sPathInfo file is not exist. (will be created later) IF Exist(sPathInfo) = TRUE THEN dtInfo = Stat(sPathInfo).LastModified ELSE dtInfo = 0 ENDIF ' check necessary files are existing and get last modfiled date IF Exist(sPathRpm) = TRUE AND IF Exist(sPathApt) = TRUE THEN dtRpm = Stat(sPathRpm).LastModified dtApt = Stat(sPathApt).LastModified RETURN TRUE ELSE ' if any of necessary files are missing, show error message in tooltip tiTrayIcon.Tooltip = ("ERROR: any of essential files are missing\nPlease confirm following filees are exist\n") tiTrayIcon.Tooltip = tiTrayIcon.Tooltip & " " & sPathRpm & "\n" tiTrayIcon.Tooltip = tiTrayIcon.Tooltip & " " & sPathApt tiTrayIcon.Picture = Picture["red.png"] RETURN FALSE ENDIF END PRIVATE FUNCTION IsSynAptRunning() AS Boolean ' check apt-get or synaptic is already running or not using pgrep ' if others are running, disable menu items and change tray icon DIM sOutSyn AS String DIM sOutApt AS String SHELL "pgrep synaptic" TO sOutSyn SHELL "pgrep apt-get" TO sOutApt IF Split(Trim$(sOutSyn), "\n").Count >= 1 OR IF Split(Trim$(sOutApt), "\n").Count >= 1 THEN hMenuApply.Enabled = FALSE hMenuCheck.Enabled = FALSE hMenuRun.Enabled = FALSE tiTrayIcon.Picture = Picture["gray.png"] tiTrayIcon.Tooltip = ("Synaptic package manager or \napt-get command is running") RETURN TRUE ELSE hMenuCheck.Enabled = TRUE hMenuRun.Enabled = TRUE tiTrayIcon.Tooltip = sTip RETURN FALSE END IF END