2015-12-24 17:25:05 +00:00
|
|
|
{--
|
|
|
|
HSFM, a filemanager written in Haskell.
|
|
|
|
Copyright (C) 2015 Julian Ospald
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
version 2 as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
--}
|
|
|
|
|
2015-12-17 03:42:22 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
2015-12-19 15:13:48 +00:00
|
|
|
module GUI.Gtk.Callbacks where
|
|
|
|
|
|
|
|
|
|
|
|
import Control.Applicative
|
|
|
|
(
|
|
|
|
(<$>)
|
|
|
|
, (<*>)
|
|
|
|
)
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
(
|
2015-12-30 16:53:16 +00:00
|
|
|
readTVarIO
|
2015-12-19 15:13:48 +00:00
|
|
|
)
|
2015-12-27 17:17:33 +00:00
|
|
|
import Control.Exception
|
|
|
|
(
|
|
|
|
throw
|
|
|
|
)
|
2015-12-22 13:15:48 +00:00
|
|
|
import Control.Monad
|
|
|
|
(
|
|
|
|
void
|
2015-12-27 17:17:33 +00:00
|
|
|
, forM_
|
2015-12-22 13:15:48 +00:00
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
import Control.Monad.IO.Class
|
|
|
|
(
|
|
|
|
liftIO
|
|
|
|
)
|
|
|
|
import Data.DirTree
|
2015-12-26 02:04:28 +00:00
|
|
|
import Data.Foldable
|
|
|
|
(
|
|
|
|
for_
|
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
import Graphics.UI.Gtk
|
|
|
|
import GUI.Gtk.Data
|
|
|
|
import GUI.Gtk.Dialogs
|
2015-12-30 16:53:16 +00:00
|
|
|
import GUI.Gtk.MyView
|
2015-12-19 15:13:48 +00:00
|
|
|
import GUI.Gtk.Utils
|
2015-12-27 17:17:33 +00:00
|
|
|
import IO.Error
|
2015-12-19 15:13:48 +00:00
|
|
|
import IO.File
|
|
|
|
import IO.Utils
|
|
|
|
import System.FilePath
|
|
|
|
(
|
|
|
|
isAbsolute
|
|
|
|
, (</>)
|
|
|
|
)
|
|
|
|
import System.Glib.UTFString
|
|
|
|
(
|
|
|
|
glibToString
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-----------------
|
|
|
|
--[ Callbacks ]--
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Set callbacks, on hotkeys, events and stuff.
|
|
|
|
setCallbacks :: MyGUI -> MyView -> IO ()
|
|
|
|
setCallbacks mygui myview = do
|
2015-12-30 16:53:16 +00:00
|
|
|
view' <- readTVarIO $ view myview
|
|
|
|
case view' of
|
|
|
|
FMTreeView treeView -> setTreeViewCallbacks treeView
|
|
|
|
FMIconView iconView -> return ()
|
|
|
|
menubarCallbacks
|
|
|
|
where
|
|
|
|
menubarCallbacks = do
|
|
|
|
-- menubar-file
|
|
|
|
_ <- menubarFileQuit mygui `on` menuItemActivated $ mainQuit
|
|
|
|
_ <- menubarFileOpen mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview open
|
|
|
|
_ <- menubarFileExecute mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview execute
|
|
|
|
_ <- menubarFileNew mygui `on` menuItemActivated $
|
|
|
|
liftIO $ newFile mygui myview
|
|
|
|
|
|
|
|
-- menubar-edit
|
|
|
|
_ <- menubarEditCut mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview moveInit
|
|
|
|
_ <- menubarEditCopy mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview copyInit
|
|
|
|
_ <- menubarEditRename mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview renameF
|
|
|
|
_ <- menubarEditPaste mygui `on` menuItemActivated $
|
|
|
|
liftIO $ operationFinal mygui myview
|
|
|
|
_ <- menubarEditDelete mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview del
|
|
|
|
|
|
|
|
-- mewnubar-view
|
|
|
|
_ <- menubarViewIcon mygui `on` menuItemActivated $
|
|
|
|
liftIO $ switchView mygui myview createIconView
|
|
|
|
_ <- menubarViewTree mygui `on` menuItemActivated $
|
|
|
|
liftIO $ switchView mygui myview createTreeView
|
|
|
|
|
|
|
|
-- menubar-help
|
|
|
|
_ <- menubarHelpAbout mygui `on` menuItemActivated $
|
|
|
|
liftIO showAboutDialog
|
|
|
|
return ()
|
|
|
|
setTreeViewCallbacks treeView = do
|
|
|
|
-- GUI events
|
|
|
|
_ <- urlBar mygui `on` entryActivated $ urlGoTo mygui myview
|
|
|
|
_ <- treeView `on` rowActivated
|
|
|
|
$ (\_ _ -> withItems mygui myview open)
|
|
|
|
_ <- refreshViewB mygui `on` buttonActivated $ do
|
|
|
|
cdir <- liftIO $ getCurrentDir myview
|
|
|
|
refreshView' mygui myview cdir
|
|
|
|
_ <- clearStatusBar mygui `on` buttonActivated $ do
|
|
|
|
popStatusbar mygui
|
|
|
|
writeTVarIO (operationBuffer myview) None
|
|
|
|
|
|
|
|
-- key events
|
|
|
|
_ <- rootWin mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"q" <- fmap glibToString eventKeyName
|
|
|
|
liftIO mainQuit
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"h" <- fmap glibToString eventKeyName
|
|
|
|
cdir <- liftIO $ getCurrentDir myview
|
|
|
|
liftIO $ modifyTVarIO (settings mygui)
|
|
|
|
(\x -> x { showHidden = not . showHidden $ x})
|
|
|
|
>> refreshView' mygui myview cdir
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Alt] <- eventModifier
|
|
|
|
"Up" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ upDir mygui myview
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
"Delete" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withItems mygui myview del
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[] <- eventModifier
|
|
|
|
"Return" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withItems mygui myview open
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"c" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withItems mygui myview copyInit
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"x" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withItems mygui myview moveInit
|
|
|
|
_ <- treeView `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"v" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ operationFinal mygui myview
|
|
|
|
|
|
|
|
-- righ-click
|
|
|
|
_ <- treeView `on` buttonPressEvent $ do
|
|
|
|
eb <- eventButton
|
|
|
|
t <- eventTime
|
|
|
|
case eb of
|
|
|
|
RightButton -> liftIO $ menuPopup (rcMenu mygui)
|
|
|
|
$ Just (RightButton, t)
|
|
|
|
_ -> return ()
|
|
|
|
return False
|
|
|
|
_ <- rcFileOpen mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview open
|
|
|
|
_ <- rcFileExecute mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview execute
|
|
|
|
_ <- rcFileNew mygui `on` menuItemActivated $
|
|
|
|
liftIO $ newFile mygui myview
|
|
|
|
_ <- rcFileCopy mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview copyInit
|
|
|
|
_ <- rcFileRename mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview renameF
|
|
|
|
_ <- rcFilePaste mygui `on` menuItemActivated $
|
|
|
|
liftIO $ operationFinal mygui myview
|
|
|
|
_ <- rcFileDelete mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview del
|
|
|
|
_ <- rcFileCut mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withItems mygui myview moveInit
|
|
|
|
|
|
|
|
return ()
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Go to the url given at the 'urlBar' and visualize it in the given
|
|
|
|
-- treeView.
|
|
|
|
urlGoTo :: MyGUI -> MyView -> IO ()
|
2015-12-24 04:52:46 +00:00
|
|
|
urlGoTo mygui myview = withErrorDialog $ do
|
2015-12-19 15:13:48 +00:00
|
|
|
fp <- entryGetText (urlBar mygui)
|
|
|
|
let abs = isAbsolute fp
|
|
|
|
exists <- (||) <$> doesDirectoryExist fp <*> doesFileExist fp
|
|
|
|
-- TODO: more explicit error handling?
|
2015-12-30 16:53:16 +00:00
|
|
|
refreshView mygui myview (Just fp)
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-27 17:17:33 +00:00
|
|
|
-- |Supposed to be used with 'withRows'. Opens a file or directory.
|
2015-12-30 16:53:16 +00:00
|
|
|
open :: [Item] -> MyGUI -> MyView -> IO ()
|
|
|
|
open [item] mygui myview = withErrorDialog $
|
|
|
|
case item of
|
2015-12-26 21:00:08 +00:00
|
|
|
ADirOrSym r -> do
|
2015-12-20 23:41:02 +00:00
|
|
|
nv <- Data.DirTree.readFile $ fullPath r
|
2015-12-30 16:53:16 +00:00
|
|
|
refreshView' mygui myview nv
|
2015-12-22 13:15:48 +00:00
|
|
|
r ->
|
2015-12-22 16:56:37 +00:00
|
|
|
void $ openFile r
|
2015-12-27 17:17:33 +00:00
|
|
|
-- this throws on the first error that occurs
|
|
|
|
open (FileLikeList fs) mygui myview = withErrorDialog $
|
|
|
|
forM_ fs $ \f -> void $ openFile f
|
|
|
|
open _ _ _ = withErrorDialog
|
|
|
|
. throw $ InvalidOperation
|
|
|
|
"Operation not supported on multiple files"
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-24 13:41:06 +00:00
|
|
|
-- |Execute a given file.
|
2015-12-30 16:53:16 +00:00
|
|
|
execute :: [Item] -> MyGUI -> MyView -> IO ()
|
|
|
|
execute [item] mygui myview = withErrorDialog $
|
|
|
|
void $ executeFile item []
|
2015-12-27 17:17:33 +00:00
|
|
|
execute _ _ _ = withErrorDialog
|
|
|
|
. throw $ InvalidOperation
|
|
|
|
"Operation not supported on multiple files"
|
2015-12-24 13:41:06 +00:00
|
|
|
|
|
|
|
|
2015-12-27 17:17:33 +00:00
|
|
|
-- |Supposed to be used with 'withRows'. Deletes a file or directory.
|
2015-12-30 16:53:16 +00:00
|
|
|
del :: [Item] -> MyGUI -> MyView -> IO ()
|
|
|
|
del [item] mygui myview = withErrorDialog $ do
|
|
|
|
let cmsg = "Really delete \"" ++ fullPath item ++ "\"?"
|
2015-12-22 16:56:37 +00:00
|
|
|
withConfirmationDialog cmsg
|
2015-12-30 16:53:16 +00:00
|
|
|
$ easyDelete item
|
2015-12-27 17:17:33 +00:00
|
|
|
-- this throws on the first error that occurs
|
2015-12-30 16:53:16 +00:00
|
|
|
del items@(_:_) mygui myview = withErrorDialog $ do
|
|
|
|
let cmsg = "Really delete " ++ show (length items) ++ " files?"
|
2015-12-27 17:17:33 +00:00
|
|
|
withConfirmationDialog cmsg
|
2015-12-30 16:53:16 +00:00
|
|
|
$ forM_ items $ \item -> easyDelete item
|
2015-12-27 17:17:33 +00:00
|
|
|
del _ _ _ = withErrorDialog
|
|
|
|
. throw $ InvalidOperation
|
|
|
|
"Operation not supported on multiple files"
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-23 15:09:37 +00:00
|
|
|
-- |Initializes a file move operation.
|
2015-12-30 16:53:16 +00:00
|
|
|
moveInit :: [Item] -> MyGUI -> MyView -> IO ()
|
|
|
|
moveInit [item] mygui myview = do
|
|
|
|
writeTVarIO (operationBuffer myview) (FMove . MP1 $ item)
|
|
|
|
let sbmsg = "Move buffer: " ++ fullPath item
|
2015-12-28 02:20:29 +00:00
|
|
|
popStatusbar mygui
|
2015-12-28 01:02:06 +00:00
|
|
|
void $ pushStatusBar mygui sbmsg
|
2015-12-27 17:17:33 +00:00
|
|
|
moveInit _ _ _ = withErrorDialog
|
|
|
|
. throw $ InvalidOperation
|
|
|
|
"Operation not supported on multiple files"
|
2015-12-23 15:09:37 +00:00
|
|
|
|
2015-12-27 17:17:33 +00:00
|
|
|
-- |Supposed to be used with 'withRows'. Initializes a file copy operation.
|
2015-12-30 16:53:16 +00:00
|
|
|
copyInit :: [Item] -> MyGUI -> MyView -> IO ()
|
|
|
|
copyInit [item] mygui myview = do
|
|
|
|
writeTVarIO (operationBuffer myview) (FCopy . CP1 $ item)
|
|
|
|
let sbmsg = "Copy buffer: " ++ fullPath item
|
2015-12-28 02:20:29 +00:00
|
|
|
popStatusbar mygui
|
2015-12-28 01:02:06 +00:00
|
|
|
void $ pushStatusBar mygui sbmsg
|
2015-12-27 17:17:33 +00:00
|
|
|
copyInit _ _ _ = withErrorDialog
|
|
|
|
. throw $ InvalidOperation
|
|
|
|
"Operation not supported on multiple files"
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-23 15:09:37 +00:00
|
|
|
-- |Finalizes a file operation, such as copy or move.
|
|
|
|
operationFinal :: MyGUI -> MyView -> IO ()
|
|
|
|
operationFinal mygui myview = withErrorDialog $ do
|
2015-12-19 15:13:48 +00:00
|
|
|
op <- readTVarIO (operationBuffer myview)
|
2015-12-25 21:51:45 +00:00
|
|
|
cdir <- getCurrentDir myview
|
2015-12-19 15:13:48 +00:00
|
|
|
case op of
|
2015-12-23 15:09:37 +00:00
|
|
|
FMove (MP1 s) -> do
|
|
|
|
let cmsg = "Really move \"" ++ fullPath s
|
|
|
|
++ "\"" ++ " to \"" ++ fullPath cdir ++ "\"?"
|
2015-12-28 02:04:02 +00:00
|
|
|
withConfirmationDialog cmsg . withCopyModeDialog
|
|
|
|
$ \cm -> void $ runFileOp (FMove . MC s cdir $ cm)
|
2015-12-23 15:09:37 +00:00
|
|
|
return ()
|
2015-12-22 13:15:48 +00:00
|
|
|
FCopy (CP1 s) -> do
|
|
|
|
let cmsg = "Really copy \"" ++ fullPath s
|
2015-12-23 15:09:37 +00:00
|
|
|
++ "\"" ++ " to \"" ++ fullPath cdir ++ "\"?"
|
2015-12-28 02:04:02 +00:00
|
|
|
withConfirmationDialog cmsg . withCopyModeDialog
|
|
|
|
$ \cm -> void $ runFileOp (FCopy . CC s cdir $ cm)
|
2015-12-22 13:15:48 +00:00
|
|
|
return ()
|
2015-12-19 15:13:48 +00:00
|
|
|
_ -> return ()
|
|
|
|
|
|
|
|
|
|
|
|
-- |Go up one directory and visualize it in the treeView.
|
|
|
|
upDir :: MyGUI -> MyView -> IO ()
|
2015-12-22 16:56:37 +00:00
|
|
|
upDir mygui myview = withErrorDialog $ do
|
2015-12-25 21:51:45 +00:00
|
|
|
cdir <- getCurrentDir myview
|
2015-12-19 15:13:48 +00:00
|
|
|
rawModel' <- readTVarIO $ rawModel myview
|
|
|
|
sortedModel' <- readTVarIO $ sortedModel myview
|
2015-12-23 15:09:51 +00:00
|
|
|
nv <- goUp cdir
|
2015-12-30 16:53:16 +00:00
|
|
|
refreshView' mygui myview nv
|
2015-12-25 21:51:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Go up one directory and visualize it in the treeView.
|
|
|
|
newFile :: MyGUI -> MyView -> IO ()
|
|
|
|
newFile mygui myview = withErrorDialog $ do
|
2015-12-25 22:17:22 +00:00
|
|
|
mfn <- textInputDialog "Enter file name"
|
2015-12-26 02:04:28 +00:00
|
|
|
for_ mfn $ \fn -> do
|
|
|
|
cdir <- getCurrentDir myview
|
2015-12-25 21:51:45 +00:00
|
|
|
createFile cdir fn
|
2015-12-26 02:04:28 +00:00
|
|
|
|
|
|
|
|
2015-12-30 16:53:16 +00:00
|
|
|
renameF :: [Item] -> MyGUI -> MyView -> IO ()
|
|
|
|
renameF [item] mygui myview = withErrorDialog $ do
|
2015-12-26 02:04:28 +00:00
|
|
|
mfn <- textInputDialog "Enter new file name"
|
|
|
|
for_ mfn $ \fn -> do
|
2015-12-30 16:53:16 +00:00
|
|
|
let cmsg = "Really rename \"" ++ fullPath item
|
|
|
|
++ "\"" ++ " to \"" ++ anchor item </> fn ++ "\"?"
|
|
|
|
withConfirmationDialog cmsg $ IO.File.renameFile item fn
|
2015-12-27 17:17:33 +00:00
|
|
|
renameF _ _ _ = withErrorDialog
|
|
|
|
. throw $ InvalidOperation
|
|
|
|
"Operation not supported on multiple files"
|