File: OpenTwoDialog.hs

package info (click to toggle)
bustle 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 720 kB
  • sloc: haskell: 3,938; ansic: 939; makefile: 110; sh: 8
file content (92 lines) | stat: -rw-r--r-- 3,560 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{-
Bustle.UI.OpenTwoDialog: a dialog to prompt the user to open two log files
Copyright © 2008–2012 Collabora Ltd.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-}
module Bustle.UI.OpenTwoDialog
  (
    setupOpenTwoDialog
  )
where

import Data.Maybe (isJust, isNothing, fromJust)
import Control.Monad (when, void)

import Graphics.UI.Gtk

import Bustle.Util
import Paths_bustle

-- Propagates changes to d1's currently-selected folder to d2, if and only if
-- d2 doesn't have a currently-selected file (otherwise, choosing a file
-- from a different directory in the second chooser would unselect a
-- previously-selected file in the first).
propagateCurrentFolder :: FileChooserClass chooser
                       => chooser
                       -> chooser
                       -> IO (ConnectId chooser)
propagateCurrentFolder d1 d2 = d1 `on` currentFolderChanged $ do
    f1 <- fileChooserGetCurrentFolder d1
    f2 <- fileChooserGetCurrentFolder d2
    otherFile <- fileChooserGetFilename d2
    when (isNothing otherFile && f1 /= f2 && isJust f1) $ void $
        fileChooserSetCurrentFolder d2 (fromJust f1)

setupOpenTwoDialog :: Window
                   -> (FilePath -> FilePath -> IO ())
                   -> IO Dialog
setupOpenTwoDialog parent callback = do
    builder <- builderNew
    builderAddFromFile builder =<< getDataFileName "data/OpenTwoDialog.ui"

    dialog <- builderGetObject builder castToDialog "openTwoDialog"
    [sessionBusChooser, systemBusChooser] <-
        mapM (builderGetObject builder castToFileChooserButton)
            ["sessionBusChooser", "systemBusChooser"]
    openTwoOpenButton <- builderGetObject builder castToButton "openTwoOpenButton"

    dialog `set` [ windowTransientFor := parent ]
    dialog `on` deleteEvent $ tryEvent $ io $ widgetHide dialog

    propagateCurrentFolder sessionBusChooser systemBusChooser
    propagateCurrentFolder systemBusChooser sessionBusChooser

    let hideMyself = do
            widgetHide dialog
            fileChooserUnselectAll sessionBusChooser
            fileChooserUnselectAll systemBusChooser

    let updateOpenSensitivity = do
            sessionLogFile <- fileChooserGetFilename sessionBusChooser
            systemLogFile <- fileChooserGetFilename systemBusChooser

            widgetSetSensitive openTwoOpenButton $
              case (sessionLogFile, systemLogFile) of
                (Just _, Just _) -> True
                _                -> False
    connectGeneric "file-set" False sessionBusChooser updateOpenSensitivity
    connectGeneric "file-set" False systemBusChooser updateOpenSensitivity
    updateOpenSensitivity

    dialog `after` response $ \resp -> do
      when (resp == ResponseAccept) $ do
          Just f1 <- fileChooserGetFilename sessionBusChooser
          Just f2 <- fileChooserGetFilename systemBusChooser
          callback f1 f2

      hideMyself

    return dialog