2015-12-24 17:25:05 +00:00
|
|
|
{--
|
|
|
|
HSFM, a filemanager written in Haskell.
|
2016-03-30 22:28:23 +00:00
|
|
|
Copyright (C) 2016 Julian Ospald
|
2015-12-24 17:25:05 +00:00
|
|
|
|
|
|
|
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-19 15:13:48 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
2016-03-30 18:16:34 +00:00
|
|
|
module HSFM.GUI.Gtk.Utils where
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2016-04-15 12:23:41 +00:00
|
|
|
import Control.Concurrent.MVar
|
|
|
|
(
|
|
|
|
readMVar
|
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
(
|
2015-12-30 16:53:16 +00:00
|
|
|
readTVarIO
|
2015-12-19 15:13:48 +00:00
|
|
|
)
|
|
|
|
import Data.Maybe
|
|
|
|
(
|
2015-12-27 17:17:33 +00:00
|
|
|
catMaybes
|
2015-12-21 04:41:12 +00:00
|
|
|
, fromJust
|
2015-12-19 15:13:48 +00:00
|
|
|
)
|
|
|
|
import Data.Traversable
|
|
|
|
(
|
|
|
|
forM
|
|
|
|
)
|
|
|
|
import Graphics.UI.Gtk
|
2016-03-30 18:16:34 +00:00
|
|
|
import HSFM.FileSystem.FileType
|
|
|
|
import HSFM.GUI.Gtk.Data
|
2016-04-15 12:23:41 +00:00
|
|
|
import Prelude hiding(getContents)
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-----------------
|
|
|
|
--[ Utilities ]--
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
2015-12-30 16:53:16 +00:00
|
|
|
getSelectedTreePaths :: MyGUI -> MyView -> IO [TreePath]
|
|
|
|
getSelectedTreePaths _ myview = do
|
|
|
|
view' <- readTVarIO $ view myview
|
|
|
|
case view' of
|
|
|
|
FMTreeView treeView -> do
|
|
|
|
tvs <- treeViewGetSelection treeView
|
|
|
|
treeSelectionGetSelectedRows tvs
|
|
|
|
FMIconView iconView ->
|
|
|
|
iconViewGetSelectedItems iconView
|
|
|
|
|
|
|
|
|
|
|
|
-- |Gets the currently selected item of the treeView, if any.
|
|
|
|
getSelectedItems :: MyGUI
|
|
|
|
-> MyView
|
|
|
|
-> IO [Item]
|
|
|
|
getSelectedItems mygui myview = do
|
|
|
|
tps <- getSelectedTreePaths mygui myview
|
2016-04-17 22:51:45 +00:00
|
|
|
catMaybes <$> mapM (rawPathToItem myview) tps
|
2015-12-30 16:53:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Carry out an action on the currently selected item.
|
2015-12-19 15:13:48 +00:00
|
|
|
--
|
2015-12-30 16:53:16 +00:00
|
|
|
-- If there is no item selected, does nothing.
|
|
|
|
withItems :: MyGUI
|
|
|
|
-> MyView
|
|
|
|
-> ( [Item]
|
|
|
|
-> MyGUI
|
|
|
|
-> MyView
|
|
|
|
-> IO ()) -- ^ action to carry out
|
|
|
|
-> IO ()
|
|
|
|
withItems mygui myview io = do
|
|
|
|
items <- getSelectedItems mygui myview
|
|
|
|
io items mygui myview
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create the 'ListStore' of files/directories from the current directory.
|
|
|
|
-- This is the function which maps the Data.DirTree data structures
|
|
|
|
-- into the GTK+ data structures.
|
2016-04-15 12:23:41 +00:00
|
|
|
fileListStore :: Item -- ^ current dir
|
2015-12-19 15:13:48 +00:00
|
|
|
-> MyView
|
2015-12-30 16:53:16 +00:00
|
|
|
-> IO (ListStore Item)
|
2016-03-31 14:19:31 +00:00
|
|
|
fileListStore dt _ = do
|
2016-04-15 12:23:41 +00:00
|
|
|
cs <- getContents getFileInfo dt
|
2015-12-20 23:41:02 +00:00
|
|
|
listStoreNew cs
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2016-04-15 12:23:41 +00:00
|
|
|
-- |Currently unsafe. This is used to obtain any item, which will
|
|
|
|
-- fail if there is none.
|
2015-12-30 16:53:16 +00:00
|
|
|
getFirstItem :: MyView
|
2016-04-15 12:23:41 +00:00
|
|
|
-> IO Item
|
2015-12-30 16:53:16 +00:00
|
|
|
getFirstItem myview = do
|
2015-12-21 04:41:12 +00:00
|
|
|
rawModel' <- readTVarIO $ rawModel myview
|
|
|
|
iter <- fromJust <$> treeModelGetIterFirst rawModel'
|
2015-12-22 13:15:48 +00:00
|
|
|
treeModelGetRow rawModel' iter
|
2015-12-21 04:41:12 +00:00
|
|
|
|
|
|
|
|
2016-04-15 12:23:41 +00:00
|
|
|
-- |Reads the current directory from MyView.
|
2016-04-24 16:38:25 +00:00
|
|
|
--
|
|
|
|
-- This reads the MVar and may block the main thread if it's
|
|
|
|
-- empty.
|
2015-12-25 21:51:45 +00:00
|
|
|
getCurrentDir :: MyView
|
2016-04-15 12:23:41 +00:00
|
|
|
-> IO Item
|
|
|
|
getCurrentDir myview = readMVar (cwd myview)
|
2015-12-26 19:27:29 +00:00
|
|
|
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
-- |Push a message to the status bar.
|
|
|
|
pushStatusBar :: MyGUI -> String -> IO (ContextId, MessageId)
|
|
|
|
pushStatusBar mygui str = do
|
|
|
|
let sb = statusBar mygui
|
|
|
|
cid <- statusbarGetContextId sb "FM Status"
|
|
|
|
mid <- statusbarPush sb cid str
|
|
|
|
return (cid, mid)
|
2015-12-28 01:02:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Pop a message from the status bar.
|
|
|
|
popStatusbar :: MyGUI -> IO ()
|
|
|
|
popStatusbar mygui = do
|
|
|
|
let sb = statusBar mygui
|
|
|
|
cid <- statusbarGetContextId sb "FM Status"
|
|
|
|
statusbarPop sb cid
|
2016-04-17 22:51:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Turn a path on the rawModel into a path that we can
|
|
|
|
-- use at the outermost model layer.
|
|
|
|
rawPathToIter :: MyView -> TreePath -> IO (Maybe TreeIter)
|
|
|
|
rawPathToIter myview tp = do
|
|
|
|
fmodel <- readTVarIO (filteredModel myview)
|
|
|
|
smodel <- readTVarIO (sortedModel myview)
|
|
|
|
msiter <- treeModelGetIter smodel tp
|
|
|
|
forM msiter $ \siter -> do
|
|
|
|
cIter <- treeModelSortConvertIterToChildIter smodel siter
|
|
|
|
treeModelFilterConvertIterToChildIter fmodel cIter
|
|
|
|
|
|
|
|
|
|
|
|
-- |Turn a path on the rawModel into the corresponding item
|
|
|
|
-- that we can use at the outermost model layer.
|
|
|
|
rawPathToItem :: MyView -> TreePath -> IO (Maybe Item)
|
|
|
|
rawPathToItem myview tp = do
|
|
|
|
rawModel' <- readTVarIO $ rawModel myview
|
|
|
|
miter <- rawPathToIter myview tp
|
|
|
|
forM miter $ \iter -> treeModelGetRow rawModel' iter
|
|
|
|
|