-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
338 lines (258 loc) · 5.24 KB
/
handler.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"fmt"
"os"
"path/filepath"
"sync"
"github.com/fsnotify/fsnotify"
adb "github.com/zach-klippenstein/goadb"
)
type selection struct {
path string
smode ifaceMode
}
var (
selected bool
openLock sync.Mutex
selectLock sync.RWMutex
openFiles map[string]struct{}
multiselection map[string]ifaceMode
)
func opsHandler(selPane, auxPane *dirPane, key rune) {
if !selPane.getLock() {
return
}
defer selPane.setUnlock()
var opstmp opsMode
var overwrite bool
var srctmp []selection
switch key {
case 'p', 'P', 'm', 'd':
if len(multiselection) == 0 {
return
}
switch key {
case 'P':
overwrite = true
fallthrough
case 'p':
opstmp = opCopy
case 'm':
opstmp = opMove
case 'd':
opstmp = opDelete
}
srctmp = nil
case 'M', 'R':
var srcpath string
switch key {
case 'M':
opstmp = opMkdir
srcpath = filepath.Join(selPane.path, mrinput)
case 'R':
opstmp = opRename
selPane.updateRef(false)
srcpath = filepath.Join(selPane.path, selPane.entry.Name)
checkSelected(selPane.path, selPane.entry.Name, true)
mrinput = filepath.Join(selPane.path, mrinput)
}
srctmp = []selection{{srcpath, selPane.mode}}
}
confirmOperation(auxPane, selPane, opstmp, overwrite, srctmp)
}
func (p *dirPane) modeSwitchHandler() {
if !p.getLock() {
return
}
defer p.setUnlock()
switch p.mode {
case mAdb:
p.mode = mLocal
p.apath = p.path
p.path = p.dpath
case mLocal:
if !checkAdb() {
return
}
p.mode = mAdb
p.dpath = p.path
p.path = p.apath
}
p.ChangeDir(false, false)
}
func (p *dirPane) multiSelectHandler(all, inverse bool, totalrows int) {
if !p.getLock() {
return
}
defer p.setUnlock()
pos, _ := p.table.GetSelection()
selected = true
mselone := !all && !inverse
mselinv := !all && inverse
for i := 0; i < totalrows; i++ {
var dir *adb.DirEntry
if mselone {
i, _ = p.table.GetSelection()
}
cell := p.table.GetCell(i, 0)
if cell.Text == "" {
return
}
ref := cell.GetReference()
if ref == nil {
return
}
checksel := false
dir = ref.(*adb.DirEntry)
fullpath := filepath.Join(p.path, dir.Name)
if mselone || mselinv {
checksel = checkSelected(p.path, dir.Name, true)
}
if !checksel {
addmsel(fullpath, p.mode)
}
p.updateDirPane(i, !checksel, dir)
if mselone {
if i+1 < totalrows {
p.table.Select(i+1, 0)
return
}
break
}
}
p.table.Select(pos, 0)
}
func (p *dirPane) openFileHandler() {
p.updateRef(true)
if p.entry.Mode.IsDir() {
return
}
name := p.entry.Name
tpath := filepath.Join("/tmp", name)
fpath := filepath.Join(p.getPath(), name)
showInfoMsg(fmt.Sprintf("Transferring '%s', check operations view", name))
tmpdst, err := startOperation(
p,
&dirPane{path: tpath, mode: mLocal},
opCopy,
false,
[]selection{{fpath, p.mode}},
)
if err != nil {
showErrorMsg(
fmt.Errorf("Unable to open '%s': %s", name, err.Error()),
false,
)
return
}
defer os.Remove(tmpdst)
if checkOpen(fpath) {
showInfoMsg(fmt.Sprintf("Waiting for process to finish with '%s'", name))
return
}
setOpen(fpath, false)
defer setOpen(fpath, true)
showInfoMsg("Opening " + name)
watcher, err := fsnotify.NewWatcher()
if err != nil {
showErrorMsg(fmt.Errorf("Cannot monitor changes on '%s'", name), false)
return
}
err = watcher.Add(tmpdst)
if err != nil {
showErrorMsg(fmt.Errorf("Error monitoring '%s'", name), false)
return
}
defer watcher.Close()
modify := make(chan bool)
defer close(modify)
go func() {
select {
case _, ok := <-watcher.Events:
if !ok {
return
}
modify <- true
}
}()
cmd, err := execCmd(fmt.Sprintf("xdg-open '%s'", tmpdst), "Background", "Local")
if err != nil {
showErrorMsg(err, false)
return
}
err = cmd.Wait()
if err != nil {
showErrorMsg(err, false)
return
}
select {
case <-modify:
_, err = startOperation(
p,
&dirPane{path: fpath, mode: p.mode},
opCopy,
true,
[]selection{{tmpdst, mLocal}},
)
if err != nil {
showErrorMsg(
fmt.Errorf("Unable to save '%s': %s", name, err.Error()),
false,
)
return
}
showInfoMsg(fmt.Sprintf("Overwriting modified '%s'", name))
default:
}
}
func checkOpen(fpath string) bool {
openLock.Lock()
defer openLock.Unlock()
_, ok := openFiles[fpath]
return ok
}
func setOpen(fpath string, del bool) {
openLock.Lock()
defer openLock.Unlock()
if del {
delete(openFiles, fpath)
return
}
openFiles[fpath] = struct{}{}
}
func checkSelected(panepath, dirname string, rm bool) bool {
if !selected {
return false
}
fullpath := filepath.Join(panepath, dirname)
ok := checkmsel(fullpath)
if ok && rm {
delmsel(fullpath)
}
return ok
}
func delmsel(fullpath string) {
selectLock.Lock()
defer selectLock.Unlock()
delete(multiselection, fullpath)
}
func addmsel(fullpath string, mode ifaceMode) {
selectLock.Lock()
defer selectLock.Unlock()
multiselection[fullpath] = mode
}
func checkmsel(fullpath string) bool {
selectLock.RLock()
defer selectLock.RUnlock()
_, ok := multiselection[fullpath]
return ok
}
func getselection() []selection {
selectLock.RLock()
defer selectLock.RUnlock()
var s []selection
for path, smode := range multiselection {
s = append(s, selection{path, smode})
}
return s
}