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
|
|
|
|
(
|
|
|
|
TVar
|
|
|
|
, newTVarIO
|
|
|
|
, readTVarIO
|
|
|
|
)
|
2015-12-22 13:15:48 +00:00
|
|
|
import Control.Monad
|
|
|
|
(
|
|
|
|
void
|
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
import Control.Monad.IO.Class
|
|
|
|
(
|
|
|
|
liftIO
|
|
|
|
)
|
|
|
|
import Data.DirTree
|
|
|
|
import Graphics.UI.Gtk
|
|
|
|
import GUI.Gtk.Data
|
|
|
|
import GUI.Gtk.Dialogs
|
|
|
|
import GUI.Gtk.Utils
|
|
|
|
import IO.File
|
|
|
|
import IO.Utils
|
|
|
|
import System.Directory
|
|
|
|
(
|
|
|
|
doesFileExist
|
|
|
|
, doesDirectoryExist
|
|
|
|
)
|
|
|
|
import System.FilePath
|
|
|
|
(
|
|
|
|
isAbsolute
|
|
|
|
, (</>)
|
|
|
|
)
|
|
|
|
import System.Glib.UTFString
|
|
|
|
(
|
|
|
|
glibToString
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-----------------
|
|
|
|
--[ Callbacks ]--
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Set callbacks, on hotkeys, events and stuff.
|
|
|
|
--
|
|
|
|
-- Interaction with mutable references:
|
|
|
|
--
|
|
|
|
-- * 'settings mygui' modifies
|
|
|
|
setCallbacks :: MyGUI -> MyView -> IO ()
|
|
|
|
setCallbacks mygui myview = do
|
|
|
|
_ <- rootWin mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"q" <- fmap glibToString eventKeyName
|
|
|
|
liftIO mainQuit
|
|
|
|
_ <- treeView mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"h" <- fmap glibToString eventKeyName
|
2015-12-25 21:51:45 +00:00
|
|
|
cdir <- liftIO $ getCurrentDir myview
|
2015-12-19 15:13:48 +00:00
|
|
|
liftIO $ modifyTVarIO (settings mygui)
|
|
|
|
(\x -> x { showHidden = not . showHidden $ x})
|
2015-12-25 22:26:47 +00:00
|
|
|
>> refreshTreeView' mygui myview cdir
|
2015-12-19 15:13:48 +00:00
|
|
|
_ <- treeView mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Alt] <- eventModifier
|
|
|
|
"Up" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ upDir mygui myview
|
|
|
|
_ <- treeView mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
"Delete" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withRow mygui myview del
|
|
|
|
_ <- treeView mygui `on` rowActivated $ (\_ _ -> withRow mygui myview open)
|
|
|
|
_ <- urlBar mygui `on` entryActivated $ urlGoTo mygui myview
|
|
|
|
_ <- treeView mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"c" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withRow mygui myview copyInit
|
2015-12-23 15:09:37 +00:00
|
|
|
_ <- treeView mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"x" <- fmap glibToString eventKeyName
|
|
|
|
liftIO $ withRow mygui myview moveInit
|
2015-12-19 15:13:48 +00:00
|
|
|
_ <- treeView mygui `on` keyPressEvent $ tryEvent $ do
|
|
|
|
[Control] <- eventModifier
|
|
|
|
"v" <- fmap glibToString eventKeyName
|
2015-12-23 15:09:37 +00:00
|
|
|
liftIO $ operationFinal mygui myview
|
2015-12-24 13:41:06 +00:00
|
|
|
|
2015-12-25 22:24:43 +00:00
|
|
|
_ <- refreshView mygui `on` buttonActivated $ do
|
|
|
|
cdir <- liftIO $ getCurrentDir myview
|
|
|
|
refreshTreeView' mygui myview cdir
|
|
|
|
|
2015-12-24 13:41:06 +00:00
|
|
|
-- menubar-file
|
|
|
|
_ <- menubarFileQuit mygui `on` menuItemActivated $ mainQuit
|
|
|
|
_ <- menubarFileOpen mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview open
|
|
|
|
_ <- menubarFileExecute mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview execute
|
2015-12-25 21:51:45 +00:00
|
|
|
_ <- menubarFileNew mygui `on` menuItemActivated $
|
|
|
|
liftIO $ newFile mygui myview
|
2015-12-24 13:41:06 +00:00
|
|
|
|
|
|
|
-- menubar-edit
|
|
|
|
_ <- menubarEditCopy mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview copyInit
|
2015-12-25 21:51:45 +00:00
|
|
|
_ <- menubarEditMove mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview moveInit
|
2015-12-24 13:41:06 +00:00
|
|
|
_ <- menubarEditPaste mygui `on` menuItemActivated $
|
|
|
|
liftIO $ operationFinal mygui myview
|
|
|
|
_ <- menubarEditDelete mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview del
|
|
|
|
_ <- menubarEditCut mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview moveInit
|
|
|
|
|
2015-12-24 16:44:55 +00:00
|
|
|
-- menubar-help
|
|
|
|
_ <- menubarHelpAbout mygui `on` menuItemActivated $
|
|
|
|
liftIO showAboutDialog
|
|
|
|
|
2015-12-24 14:36:21 +00:00
|
|
|
-- righ-click
|
|
|
|
_ <- treeView mygui `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 $ withRow mygui myview open
|
|
|
|
_ <- rcFileExecute mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview execute
|
2015-12-25 21:51:45 +00:00
|
|
|
_ <- rcFileNew mygui `on` menuItemActivated $
|
|
|
|
liftIO $ newFile mygui myview
|
2015-12-24 14:36:21 +00:00
|
|
|
_ <- rcFileCopy mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview copyInit
|
2015-12-25 21:51:45 +00:00
|
|
|
_ <- rcFileMove mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview moveInit
|
2015-12-24 14:36:21 +00:00
|
|
|
_ <- rcFilePaste mygui `on` menuItemActivated $
|
|
|
|
liftIO $ operationFinal mygui myview
|
|
|
|
_ <- rcFileDelete mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview del
|
|
|
|
_ <- rcFileCut mygui `on` menuItemActivated $
|
|
|
|
liftIO $ withRow mygui myview moveInit
|
|
|
|
|
|
|
|
|
2015-12-19 15:13:48 +00:00
|
|
|
return ()
|
|
|
|
|
|
|
|
|
|
|
|
-- |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?
|
|
|
|
refreshTreeView mygui myview (Just fp)
|
|
|
|
|
|
|
|
|
|
|
|
-- |Supposed to be used with 'withRow'. Opens a file or directory.
|
|
|
|
open :: Row -> MyGUI -> MyView -> IO ()
|
2015-12-22 16:56:37 +00:00
|
|
|
open row mygui myview = withErrorDialog $
|
2015-12-20 23:41:02 +00:00
|
|
|
case row of
|
2015-12-24 02:11:17 +00:00
|
|
|
SADir r -> do
|
2015-12-20 23:41:02 +00:00
|
|
|
nv <- Data.DirTree.readFile $ fullPath r
|
|
|
|
refreshTreeView' 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-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-24 13:41:06 +00:00
|
|
|
-- |Execute a given file.
|
|
|
|
execute :: Row -> MyGUI -> MyView -> IO ()
|
|
|
|
execute row mygui myview = withErrorDialog $
|
|
|
|
void $ executeFile row []
|
|
|
|
|
|
|
|
|
2015-12-19 15:13:48 +00:00
|
|
|
-- |Supposed to be used with 'withRow'. Deletes a file or directory.
|
|
|
|
del :: Row -> MyGUI -> MyView -> IO ()
|
2015-12-22 16:56:37 +00:00
|
|
|
del row mygui myview = withErrorDialog $ do
|
2015-12-22 13:15:48 +00:00
|
|
|
let cmsg = "Really delete \"" ++ fullPath row ++ "\"?"
|
2015-12-22 16:56:37 +00:00
|
|
|
withConfirmationDialog cmsg
|
2015-12-22 13:15:48 +00:00
|
|
|
$ easyDelete row >> refreshTreeView mygui myview Nothing
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-23 15:09:37 +00:00
|
|
|
-- |Initializes a file move operation.
|
|
|
|
--
|
|
|
|
-- Interaction with mutable references:
|
|
|
|
--
|
|
|
|
-- * 'operationBuffer' writes
|
|
|
|
moveInit :: Row -> MyGUI -> MyView -> IO ()
|
|
|
|
moveInit row mygui myview =
|
|
|
|
writeTVarIO (operationBuffer myview) (FMove . MP1 $ row)
|
|
|
|
|
|
|
|
|
2015-12-19 15:13:48 +00:00
|
|
|
-- |Supposed to be used with 'withRow'. Initializes a file copy operation.
|
|
|
|
--
|
|
|
|
-- Interaction with mutable references:
|
|
|
|
--
|
|
|
|
-- * 'operationBuffer' writes
|
|
|
|
copyInit :: Row -> MyGUI -> MyView -> IO ()
|
2015-12-20 23:41:02 +00:00
|
|
|
copyInit row mygui myview =
|
2015-12-22 13:15:48 +00:00
|
|
|
writeTVarIO (operationBuffer myview) (FCopy . CP1 $ row)
|
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.
|
2015-12-19 15:13:48 +00:00
|
|
|
--
|
|
|
|
-- Interaction with mutable references:
|
|
|
|
--
|
|
|
|
-- * 'operationBuffer' reads
|
2015-12-23 15:09:37 +00:00
|
|
|
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 ++ "\"?"
|
|
|
|
withConfirmationDialog cmsg
|
|
|
|
(runFileOp (FMove . MC s $ cdir)
|
|
|
|
>> refreshTreeView mygui myview Nothing)
|
|
|
|
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-22 18:50:07 +00:00
|
|
|
cm <- showCopyModeChooserDialog
|
2015-12-22 16:56:37 +00:00
|
|
|
withConfirmationDialog cmsg
|
2015-12-23 15:09:37 +00:00
|
|
|
(runFileOp (FCopy . CC s cdir $ cm)
|
2015-12-22 16:56:37 +00:00
|
|
|
>> refreshTreeView mygui myview Nothing)
|
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.
|
|
|
|
--
|
|
|
|
-- Interaction with mutable references:
|
|
|
|
--
|
|
|
|
-- * 'rawModel' reads
|
|
|
|
-- * 'sortedModel' reads
|
|
|
|
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-20 23:41:02 +00:00
|
|
|
refreshTreeView' 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
|
|
|
cdir <- getCurrentDir myview
|
|
|
|
mfn <- textInputDialog "Enter file name"
|
2015-12-25 21:51:45 +00:00
|
|
|
maybe (return ()) (\fn -> do
|
|
|
|
createFile cdir fn
|
|
|
|
refreshTreeView' mygui myview cdir
|
|
|
|
) mfn
|