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.Dialogs where
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
2015-12-24 17:18:50 +00:00
|
|
|
import Control.Applicative
|
|
|
|
(
|
|
|
|
(<$>)
|
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
import Control.Exception
|
|
|
|
(
|
2015-12-28 02:04:02 +00:00
|
|
|
catch
|
2015-12-28 02:18:22 +00:00
|
|
|
, throw
|
2015-12-28 02:04:02 +00:00
|
|
|
, try
|
2015-12-19 15:13:48 +00:00
|
|
|
, SomeException
|
|
|
|
)
|
|
|
|
import Control.Monad
|
|
|
|
(
|
|
|
|
when
|
|
|
|
, void
|
|
|
|
)
|
2015-12-24 17:18:50 +00:00
|
|
|
import Data.Version
|
|
|
|
(
|
|
|
|
showVersion
|
|
|
|
)
|
|
|
|
import Distribution.Package
|
|
|
|
(
|
|
|
|
PackageIdentifier(..)
|
|
|
|
, PackageName(..)
|
|
|
|
)
|
|
|
|
import Distribution.PackageDescription
|
|
|
|
(
|
|
|
|
GenericPackageDescription(..)
|
|
|
|
, PackageDescription(..)
|
|
|
|
)
|
|
|
|
import Distribution.PackageDescription.Parse
|
|
|
|
(
|
|
|
|
readPackageDescription
|
|
|
|
)
|
|
|
|
import Distribution.Verbosity
|
|
|
|
(
|
|
|
|
silent
|
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
import Graphics.UI.Gtk
|
2016-03-30 18:16:34 +00:00
|
|
|
import HSFM.FileSystem.Errors
|
|
|
|
import HSFM.FileSystem.FileOperations
|
|
|
|
import HSFM.GUI.Gtk.Data
|
2015-12-28 23:48:54 +00:00
|
|
|
import Paths_hsfm
|
|
|
|
(
|
|
|
|
getDataFileName
|
|
|
|
)
|
2015-12-19 15:13:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
--[ Dialog popups ]--
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Pops up an error Dialog with the given String.
|
|
|
|
showErrorDialog :: String -> IO ()
|
|
|
|
showErrorDialog str = do
|
|
|
|
errorDialog <- messageDialogNew Nothing
|
|
|
|
[DialogDestroyWithParent]
|
|
|
|
MessageError
|
|
|
|
ButtonsClose
|
|
|
|
str
|
|
|
|
_ <- dialogRun errorDialog
|
|
|
|
widgetDestroy errorDialog
|
|
|
|
|
|
|
|
|
|
|
|
-- |Asks the user for confirmation and returns True/False.
|
|
|
|
showConfirmationDialog :: String -> IO Bool
|
|
|
|
showConfirmationDialog str = do
|
|
|
|
confirmDialog <- messageDialogNew Nothing
|
|
|
|
[DialogDestroyWithParent]
|
|
|
|
MessageQuestion
|
|
|
|
ButtonsYesNo
|
|
|
|
str
|
|
|
|
rID <- dialogRun confirmDialog
|
|
|
|
widgetDestroy confirmDialog
|
|
|
|
case rID of
|
|
|
|
ResponseYes -> return True
|
|
|
|
ResponseNo -> return False
|
|
|
|
_ -> return False
|
|
|
|
|
|
|
|
|
|
|
|
-- |Asks the user which directory copy mode he wants via dialog popup
|
|
|
|
-- and returns 'DirCopyMode'.
|
2015-12-28 02:04:02 +00:00
|
|
|
showCopyModeDialog :: IO CopyMode
|
|
|
|
showCopyModeDialog = do
|
2015-12-19 15:13:48 +00:00
|
|
|
chooserDialog <- messageDialogNew Nothing
|
|
|
|
[DialogDestroyWithParent]
|
|
|
|
MessageQuestion
|
|
|
|
ButtonsNone
|
2015-12-28 02:04:02 +00:00
|
|
|
"Target exists, how to proceed?"
|
|
|
|
dialogAddButton chooserDialog "Cancel" (ResponseUser 0)
|
2015-12-19 15:13:48 +00:00
|
|
|
dialogAddButton chooserDialog "Merge" (ResponseUser 1)
|
|
|
|
dialogAddButton chooserDialog "Replace" (ResponseUser 2)
|
|
|
|
rID <- dialogRun chooserDialog
|
|
|
|
widgetDestroy chooserDialog
|
|
|
|
case rID of
|
|
|
|
ResponseUser 0 -> return Strict
|
|
|
|
ResponseUser 1 -> return Merge
|
|
|
|
ResponseUser 2 -> return Replace
|
|
|
|
|
|
|
|
|
2015-12-28 02:04:02 +00:00
|
|
|
-- |Attempts to run the given function with the `Strict` copy mode.
|
|
|
|
-- If that raises a `FileDoesExist` or `DirDoesExist`, then it prompts
|
|
|
|
-- the user for action via `showCopyModeDialog` and then carries out
|
|
|
|
-- the given function again.
|
|
|
|
withCopyModeDialog :: (CopyMode -> IO ()) -> IO ()
|
|
|
|
withCopyModeDialog fa =
|
|
|
|
catch (fa Strict) $ \e ->
|
|
|
|
case e of
|
|
|
|
FileDoesExist _ -> doIt
|
|
|
|
DirDoesExist _ -> doIt
|
2015-12-28 02:18:22 +00:00
|
|
|
e -> throw e
|
2015-12-28 02:04:02 +00:00
|
|
|
where
|
|
|
|
doIt = do cm <- showCopyModeDialog
|
|
|
|
case cm of
|
|
|
|
Strict -> return () -- don't try again
|
|
|
|
_ -> fa cm
|
|
|
|
|
|
|
|
|
2015-12-24 16:44:55 +00:00
|
|
|
-- |Shows the about dialog from the help menu.
|
|
|
|
showAboutDialog :: IO ()
|
|
|
|
showAboutDialog = do
|
2015-12-24 17:18:50 +00:00
|
|
|
ad <- aboutDialogNew
|
2015-12-28 23:48:54 +00:00
|
|
|
lstr <- readFile =<< getDataFileName "LICENSE"
|
|
|
|
hsfmicon <- pixbufNewFromFile =<< getDataFileName "data/Gtk/icons/hsfm.png"
|
|
|
|
pdesc <- fmap packageDescription
|
|
|
|
(readPackageDescription silent
|
|
|
|
=<< getDataFileName "hsfm.cabal")
|
2015-12-24 16:44:55 +00:00
|
|
|
set ad
|
2015-12-24 17:18:50 +00:00
|
|
|
[ aboutDialogProgramName := (unPackageName . pkgName . package) pdesc
|
|
|
|
, aboutDialogName := (unPackageName . pkgName . package) pdesc
|
|
|
|
, aboutDialogVersion := (showVersion . pkgVersion . package) pdesc
|
|
|
|
, aboutDialogCopyright := copyright pdesc
|
|
|
|
, aboutDialogComments := description pdesc
|
|
|
|
, aboutDialogLicense := Just lstr
|
|
|
|
, aboutDialogWebsite := homepage pdesc
|
|
|
|
, aboutDialogAuthors := [author pdesc]
|
2015-12-24 16:44:55 +00:00
|
|
|
, aboutDialogLogo := Just hsfmicon
|
|
|
|
, aboutDialogWrapLicense := True
|
|
|
|
]
|
|
|
|
_ <- dialogRun ad
|
|
|
|
widgetDestroy ad
|
|
|
|
|
|
|
|
|
2015-12-19 15:13:48 +00:00
|
|
|
-- |Carry out an IO action with a confirmation dialog.
|
|
|
|
-- If the user presses "No", then do nothing.
|
|
|
|
withConfirmationDialog :: String -> IO () -> IO ()
|
|
|
|
withConfirmationDialog str io = do
|
|
|
|
run <- showConfirmationDialog str
|
|
|
|
when run io
|
|
|
|
|
|
|
|
|
|
|
|
-- |Execute the given IO action. If the action throws exceptions,
|
|
|
|
-- visualize them via 'showErrorDialog'.
|
|
|
|
withErrorDialog :: IO a -> IO ()
|
|
|
|
withErrorDialog io = do
|
|
|
|
r <- try io
|
|
|
|
either (\e -> showErrorDialog $ show (e :: SomeException))
|
|
|
|
(\_ -> return ())
|
|
|
|
r
|
|
|
|
|
2015-12-25 21:51:45 +00:00
|
|
|
|
|
|
|
-- |Asks the user which directory copy mode he wants via dialog popup
|
|
|
|
-- and returns 'DirCopyMode'.
|
2015-12-25 22:17:22 +00:00
|
|
|
textInputDialog :: String -> IO (Maybe String)
|
|
|
|
textInputDialog title = do
|
2015-12-25 21:51:45 +00:00
|
|
|
chooserDialog <- messageDialogNew Nothing
|
|
|
|
[DialogDestroyWithParent]
|
|
|
|
MessageQuestion
|
|
|
|
ButtonsNone
|
2015-12-25 22:17:22 +00:00
|
|
|
title
|
2015-12-25 21:51:45 +00:00
|
|
|
entry <- entryNew
|
|
|
|
cbox <- dialogGetActionArea chooserDialog
|
2015-12-25 22:17:22 +00:00
|
|
|
dialogAddButton chooserDialog "Ok" (ResponseUser 0)
|
2015-12-25 21:51:45 +00:00
|
|
|
dialogAddButton chooserDialog "Cancel" (ResponseUser 1)
|
|
|
|
boxPackStart (castToBox cbox) entry PackNatural 5
|
|
|
|
widgetShowAll chooserDialog
|
|
|
|
rID <- dialogRun chooserDialog
|
|
|
|
ret <- case rID of
|
|
|
|
-- TODO: make this more safe
|
|
|
|
ResponseUser 0 -> Just <$> entryGetText entry
|
|
|
|
ResponseUser 1 -> return Nothing
|
|
|
|
widgetDestroy chooserDialog
|
|
|
|
return ret
|