-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommit.hs
More file actions
203 lines (171 loc) · 6.94 KB
/
Commit.hs
File metadata and controls
203 lines (171 loc) · 6.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
--
-- Module : Main
-- Copyright : 2011 Stephan Fortelny, Harald Jagenteufel
-- License : GPL
--
-- Maintainer : stephanfortelny at gmail.com, h.jagenteufel at gmail.com
-- Stability :
-- Portability :
--
-- TODO select all files checkbox + TODOs below
--
-- | Functions to show a commit window. This mostly hides the window-building tasks from the specific VCS implementation.
-----------------------------------------------------------------------------
module VCSGui.Common.Commit (
SCFile(..)
,Option
,showCommitGUI
,selected
,filePath
,status
,isLocked
) where
import qualified VCSWrapper.Common as Wrapper
import qualified VCSGui.Common.GtkHelper as H
import Control.Monad.Trans(liftIO)
import Control.Monad
import Control.Monad.Reader
import Data.Maybe
import Paths_vcsgui(getDataFileName)
import qualified Data.Text as T (unpack, pack)
import Data.Text (Text)
import GI.Gtk.Objects.TreeView (TreeView(..))
import Data.GI.Gtk.ModelView.SeqStore
(seqStoreAppend, seqStoreClear, seqStoreToList, SeqStore(..))
import GI.Gtk.Objects.Action (onActionActivate)
import GI.Gtk.Objects.Widget (widgetShowAll)
import GI.Gtk.Objects.Builder (builderGetObject, Builder(..))
import Data.GI.Base.BasicTypes
(ManagedPtr(..), GObject)
import Data.GI.Base.ManagedPtr (unsafeCastTo)
--
-- glade path and object accessors
--
getGladepath = getDataFileName "data/guiCommonCommit.glade"
accessorWindowCommit = "windowCommit"
accessorTreeViewFiles = "treeViewFiles"
accessorActCommit = "actCommit"
accessorActCancel = "actCancel"
accessorActTxtViewMsg = "txtViewMsg"
--
-- types
--
-- | This function will be called after the ok action is called.
type OkCallBack = Text -- ^ Commit message as specified in the GUI.
-> [FilePath] -- ^ List of 'FilePath's of the files that were selected.
-> [Option] -- ^ options (this is currently not implemented i.e. '[]' is passed)
-> Wrapper.Ctx ()
-- | fn to set seqStore model for treeview
type TreeViewSetter = TreeView
-> Wrapper.Ctx (SeqStore SCFile)
data CommitGUI = CommitGUI {
windowCommit :: H.WindowItem
, treeViewFiles :: H.TreeViewItem SCFile
, actCommit :: H.ActionItem
, actCancel :: H.ActionItem
, txtViewMsg :: H.TextViewItem
}
-- | Represents a file which can be selected for commiting.
data SCFile = GITSCFile Bool FilePath Text |
SVNSCFile Bool FilePath Text Bool
deriving (Show)
-- | Return 'True' if the 'SCFile' is flagged as selected.
selected :: SCFile -> Bool
selected (GITSCFile s _ _) = s
selected (SVNSCFile s _ _ _) = s
-- | Return the 'FilePath' of this file.
filePath :: SCFile -> FilePath
filePath (GITSCFile _ fp _ ) = fp
filePath (SVNSCFile _ fp _ _) = fp
-- | Return the status of this file.
status :: SCFile -> Text
status (GITSCFile _ _ s) = s
status (SVNSCFile _ _ s _) = s
-- | Return 'True' if this file is locked. For Git, this returns always 'False'.
isLocked :: SCFile -> Bool
isLocked (SVNSCFile _ _ _ l) = l
isLocked _ = False
-- | Options to the 'OkCallBack'.
type Option = Text
-- | Display a window to enter a commit message and select files to be commited.
showCommitGUI :: TreeViewSetter
-> OkCallBack
-> Wrapper.Ctx()
showCommitGUI setUpTreeView okCallback = do
liftIO $ putStrLn "Starting gui ..."
gui <- loadCommitGUI setUpTreeView
-- connect actions
liftIO $ H.registerClose $ windowCommit gui
liftIO $ H.registerCloseAction (actCancel gui) (windowCommit gui)
config <- ask
liftIO $ onActionActivate (H.getItem (actCommit gui)) $ do
let (store,_) = H.getItem (treeViewFiles gui)
selectedFiles <- getSelectedFiles store
mbMsg <- H.get (txtViewMsg gui)
case selectedFiles of
[] -> return() -- TODO err-message, selected files are empty
_ -> do
case mbMsg of
Nothing -> return() --TODO err-message, message is empty
Just msg -> Wrapper.runVcs config $ okCallback msg selectedFiles [] -- TODO implement Options
H.closeWin (windowCommit gui)
-- present window
liftIO $ widgetShowAll $ H.getItem $ windowCommit gui
return ()
loadCommitGUI :: TreeViewSetter -- ^ fn to set seqStore model for treeview
-> Wrapper.Ctx CommitGUI
loadCommitGUI setUpTreeView = do
gladepath <- liftIO getGladepath
builder <- liftIO $ H.openGladeFile gladepath
win <- liftIO $ H.getWindowFromGlade builder accessorWindowCommit
treeViewFiles <- getTreeViewFromGladeCustomStore builder accessorTreeViewFiles setUpTreeView
actCommit <- liftIO $ H.getActionFromGlade builder accessorActCommit
actCancel <- liftIO $ H.getActionFromGlade builder accessorActCancel
txtViewMsg <- liftIO $ H.getTextViewFromGlade builder accessorActTxtViewMsg
return $ CommitGUI win treeViewFiles actCommit actCancel txtViewMsg
----
---- HELPERS
----
getSelectedFiles :: SeqStore SCFile -> IO [FilePath]
getSelectedFiles seqStore = do
listedFiles <- seqStoreToList seqStore
let selectedFiles = map (\scf -> filePath scf )
$ filter (\scf -> selected scf) listedFiles
return (selectedFiles)
getTreeViewFromGladeCustomStore :: Builder
-> Text
-> TreeViewSetter
-> Wrapper.Ctx (H.TreeViewItem SCFile)
getTreeViewFromGladeCustomStore builder name setupSeqStore = do
(_, tView) <- liftIO $ wrapWidget builder TreeView name
store <- setupSeqStore tView
let getter = getFromSeqStore (store, tView)
setter = setToSeqStore (store, tView)
return (name, (store, tView), (getter, setter))
---
--- same as gtkhelper, but avoiding exposing it
---
wrapWidget :: GObject objClass =>
Builder
-> (ManagedPtr objClass -> objClass)
-> Text -> IO (Text, objClass)
wrapWidget builder constructor name = do
putStrLn $ " cast " ++ T.unpack name
gobj <- builderGetObject builder name >>= unsafeCastTo constructor . fromJust
return (name, gobj)
getFromSeqStore :: (SeqStore a, TreeView)
-> IO (Maybe [a])
getFromSeqStore (store, _) = do
list <- seqStoreToList store
if null list
then return Nothing
else return $ Just list
setToSeqStore :: (SeqStore a, TreeView)
-> [a]
-> IO ()
setToSeqStore (store, view) newList = do
seqStoreClear store
mapM_ (seqStoreAppend store) newList
return ()