From e7f9e5a834af89b35a8a2a1e5f3788ef5c1736fb Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 20 Dec 2021 12:24:28 +0000 Subject: [PATCH] only use notify-send when present (#163) --- src/Simplex/Chat/Notification.hs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Simplex/Chat/Notification.hs b/src/Simplex/Chat/Notification.hs index 6339d62d6c..3f4883205e 100644 --- a/src/Simplex/Chat/Notification.hs +++ b/src/Simplex/Chat/Notification.hs @@ -10,10 +10,10 @@ import Control.Monad (void) import Data.List (isInfixOf) import Data.Map (Map, fromList) import qualified Data.Map as M -import Data.Maybe (fromMaybe) +import Data.Maybe (fromMaybe, isJust) import Data.Text (Text) import qualified Data.Text as T -import System.Directory (createDirectoryIfMissing, doesFileExist, getAppUserDataDirectory) +import System.Directory (createDirectoryIfMissing, doesFileExist, findExecutable, getAppUserDataDirectory) import System.FilePath (combine) import System.Info (os) import System.Process (readCreateProcess, shell) @@ -27,17 +27,25 @@ initializeNotifications = "mingw32" -> initWinNotify "linux" -> doesFileExist "/proc/sys/kernel/osrelease" >>= \case - False -> pure $ notify linuxScript + False -> initLinuxNotify True -> do v <- readFile "/proc/sys/kernel/osrelease" if "Microsoft" `isInfixOf` v || "WSL" `isInfixOf` v then initWslNotify - else pure $ notify linuxScript - _ -> pure . const $ pure () + else initLinuxNotify + _ -> pure noNotifications + +noNotifications :: Notification -> IO () +noNotifications _ = pure () hideException :: (a -> IO ()) -> (a -> IO ()) hideException f a = f a `catch` \(_ :: SomeException) -> pure () +initLinuxNotify :: IO (Notification -> IO ()) +initLinuxNotify = do + found <- isJust <$> findExecutable "notify-send" + pure $ if found then notify linuxScript else noNotifications + notify :: (Notification -> Text) -> Notification -> IO () notify script notification = void $ readCreateProcess (shell . T.unpack $ script notification) ""