2013-11-12 18:54:02 +00:00
|
|
|
{- Win32-notify interface
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.Win32Notify where
|
|
|
|
|
|
|
|
import Common hiding (isDirectory)
|
|
|
|
import Utility.DirWatcher.Types
|
|
|
|
|
|
|
|
import System.Win32.Notify
|
2013-11-12 21:17:50 +00:00
|
|
|
import qualified System.PosixCompat.Files as Files
|
2013-11-12 18:54:02 +00:00
|
|
|
|
|
|
|
watchDir :: FilePath -> (FilePath -> Bool) -> WatchHooks -> IO WatchManager
|
|
|
|
watchDir dir ignored hooks = do
|
|
|
|
scan dir
|
|
|
|
wm <- initWatchManager
|
|
|
|
void $ watchDirectory wm dir True [Create, Delete, Modify, Move] handle
|
2013-11-12 21:17:50 +00:00
|
|
|
return wm
|
2013-11-12 18:54:02 +00:00
|
|
|
where
|
|
|
|
handle evt
|
|
|
|
| ignoredPath ignored (filePath evt) = noop
|
2013-11-12 21:17:50 +00:00
|
|
|
| otherwise = case evt of
|
|
|
|
(Deleted _ _)
|
2013-11-12 18:54:02 +00:00
|
|
|
| isDirectory evt -> runhook delDirHook Nothing
|
|
|
|
| otherwise -> runhook delHook Nothing
|
2013-11-12 21:17:50 +00:00
|
|
|
(Created _ _)
|
2013-11-12 18:54:02 +00:00
|
|
|
| isDirectory evt -> noop
|
|
|
|
| otherwise -> runhook addHook Nothing
|
2013-11-12 21:17:50 +00:00
|
|
|
(Modified _ _)
|
2013-11-12 18:54:02 +00:00
|
|
|
| isDirectory evt -> noop
|
|
|
|
{- Add hooks are run when a file is modified for
|
|
|
|
- compatability with INotify, which calls the add
|
|
|
|
- hook when a file is closed, and so tends to call
|
|
|
|
- both add and modify for file modifications. -}
|
|
|
|
| otherwise -> do
|
2013-11-12 21:17:50 +00:00
|
|
|
runhook addHook Nothing
|
|
|
|
runhook modifyHook Nothing
|
2013-11-12 18:54:02 +00:00
|
|
|
where
|
|
|
|
runhook h s = maybe noop (\a -> a (filePath evt) s) (h hooks)
|
|
|
|
|
|
|
|
scan d = unless (ignoredPath ignored d) $
|
|
|
|
mapM_ go =<< dirContentsRecursive d
|
|
|
|
where
|
|
|
|
go f
|
|
|
|
| ignoredPath ignored f = noop
|
|
|
|
| otherwise = do
|
|
|
|
ms <- getstatus f
|
|
|
|
case ms of
|
|
|
|
Nothing -> noop
|
|
|
|
Just s
|
|
|
|
| Files.isRegularFile s ->
|
|
|
|
runhook addHook ms
|
|
|
|
| otherwise ->
|
|
|
|
noop
|
|
|
|
where
|
|
|
|
runhook h s = maybe noop (\a -> a f s) (h hooks)
|
|
|
|
|
|
|
|
getstatus = catchMaybeIO . getFileStatus
|
|
|
|
|
|
|
|
{- Check each component of the path to see if it's ignored. -}
|
|
|
|
ignoredPath :: (FilePath -> Bool) -> FilePath -> Bool
|
|
|
|
ignoredPath ignored = any ignored . map dropTrailingPathSeparator . splitPath
|