summaryrefslogtreecommitdiffhomepage
path: root/Plugins
diff options
context:
space:
mode:
authorSpencer Janssen <sjanssen@cse.unl.edu>2008-08-06 22:40:47 +0200
committerSpencer Janssen <sjanssen@cse.unl.edu>2008-08-06 22:40:47 +0200
commit25886ea48e9083240ce09f42755f6ef11455e51f (patch)
tree11be12e2673e2d66d491c6271ca9823b4fb08f51 /Plugins
parent171112160c38ea941cb16cfb04fb66fb3c9a5a6a (diff)
downloadxmobar-25886ea48e9083240ce09f42755f6ef11455e51f.tar.gz
xmobar-25886ea48e9083240ce09f42755f6ef11455e51f.tar.bz2
Add mail watcher plugin
darcs-hash:20080806204047-a5988-a8917d4a02c8ddf9afa34780e6794d3f141d16a0.gz
Diffstat (limited to 'Plugins')
-rw-r--r--Plugins/Mail.hs78
1 files changed, 78 insertions, 0 deletions
diff --git a/Plugins/Mail.hs b/Plugins/Mail.hs
new file mode 100644
index 0000000..0a8507a
--- /dev/null
+++ b/Plugins/Mail.hs
@@ -0,0 +1,78 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Plugins.Mail
+-- Copyright : (c) Spencer Janssen
+-- License : BSD-style (see LICENSE)
+--
+-- Maintainer : Spencer Janssen <sjanssen@cse.unl.edu>
+-- Stability : unstable
+-- Portability : unportable
+--
+-- A plugin for checking mail.
+--
+-----------------------------------------------------------------------------
+
+module Plugins.Mail where
+
+import Prelude hiding (catch)
+import System.IO
+import Plugins
+
+import Control.Monad
+import Control.Concurrent.STM
+
+import System.Directory
+import System.FilePath
+import System.INotify
+
+import Data.List (isPrefixOf)
+import Data.Set (Set)
+import qualified Data.Set as S
+
+data Mail = Mail [(String, FilePath)]
+ deriving (Read, Show)
+
+instance Exec Mail where
+ start (Mail ms) cb = do
+ vs <- mapM (const $ newTVarIO S.empty) ms
+
+ let ts = map fst ms
+ ds = map ((</> "new") . snd) ms
+ ev = [Move, MoveIn, MoveOut, Create, Delete]
+
+ i <- initINotify
+ zipWithM_ (\d v -> addWatch i ev d (handle v)) ds vs
+
+ forM (zip ds vs) $ \(d, v) -> do
+ s <- fmap (S.fromList . filter (not . isPrefixOf "."))
+ $ getDirectoryContents d
+ atomically $ modifyTVar v (S.union s)
+
+ changeLoop (mapM (fmap S.size . readTVar) vs) $ \ns -> do
+ cb . unwords $ [m ++ ":" ++ show n
+ | (m, n) <- zip ts ns
+ , n /= 0 ]
+
+modifyTVar :: TVar a -> (a -> a) -> STM ()
+modifyTVar v f = readTVar v >>= writeTVar v . f
+
+handle :: TVar (Set String) -> Event -> IO ()
+handle v e = atomically $ modifyTVar v $ case e of
+ Created {} -> create
+ MovedIn {} -> create
+ Deleted {} -> delete
+ MovedOut {} -> delete
+ _ -> id
+ where
+ delete = S.delete (filePath e)
+ create = S.insert (filePath e)
+
+changeLoop :: Eq a => STM a -> (a -> IO ()) -> IO ()
+changeLoop s f = atomically s >>= go
+ where
+ go old = do
+ f old
+ go =<< atomically (do
+ new <- s
+ guard (new /= old)
+ return new)