-
Notifications
You must be signed in to change notification settings - Fork 6
/
handle_openfile.go
132 lines (119 loc) · 3.18 KB
/
handle_openfile.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
package main
import (
"fmt"
"io"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"github.com/ProtonMail/gopenpgp/v2/crypto"
)
func (ed *EditorWindow) clickedOpenFile() {
fileOpen := dialog.NewFileOpen(ed.handleOpenFileCallback, ed.win)
fileOpen.SetFilter(storage.NewExtensionFileFilter(recognizedFileExtensions))
lastDir := ed.app.Preferences().StringWithFallback(PREF_LAST_DIR, "")
if lastDir != "" {
fileLister, err := storage.ListerForURI(storage.NewFileURI(lastDir))
if err != nil {
fmt.Println(err)
} else {
fileOpen.SetLocation(fileLister)
}
}
fileOpen.Show()
}
func (ed *EditorWindow) handleOpenFileCallback(frc fyne.URIReadCloser, err error) {
if err != nil {
fmt.Println(err)
return
}
if frc == nil || frc.URI() == nil {
return
}
// Ask for password
passwordEntry := widget.NewPasswordEntry()
passwordEntry.SetPlaceHolder("Password")
form := dialog.NewForm(
"File password",
"Ok",
"Cancel",
[]*widget.FormItem{
{Text: "Enter password", Widget: passwordEntry},
},
func(b bool) {
if !b {
return
}
openURI := frc.URI().String()
dir := openURI[0:strings.LastIndex(openURI, "/")]
ed.app.Preferences().SetString(PREF_LAST_DIR, strings.TrimPrefix(dir, "file://"))
ed.doOpenFile(frc, passwordEntry.Text)
},
ed.win,
)
passwordEntry.OnSubmitted = func(s string) {
form.Submit()
}
form.Resize(fyne.NewSize(350, 170))
form.Show()
time.Sleep(100 * time.Millisecond)
ed.win.Canvas().Focus(passwordEntry)
}
func (ed *EditorWindow) doOpenFile(frc fyne.URIReadCloser, password string) {
defer frc.Close()
bytesMsg, err := io.ReadAll(frc)
if err != nil {
dialog.ShowError(err, ed.win)
return
}
pgpMsg, err := crypto.NewPGPMessageFromArmored(string(bytesMsg))
if err != nil {
dialog.ShowError(err, ed.win)
return
}
msg, err := crypto.DecryptMessageWithPassword(pgpMsg, []byte(password))
if err != nil {
dialog.ShowError(err, ed.win)
return
}
fileName := strings.TrimPrefix(frc.URI().String(), "file://")
if ed.isChanged {
dialog.ShowConfirm("Save document?",
"There are unsaved changes. Do you wish to save the document?",
func(saveFile bool) {
if saveFile {
if ed.fileName != "" && ed.oldPassword != "" {
// Just save the file with the existing filename and password
ed.saveWithExistingFileAndPassword()
ed.setEditorFile(fileName, msg.GetString())
ed.oldPassword = password // new password
} else {
// Need to ask for filename and password,
// then save the old file,
// then load the new file data into the editor.
fileSave := ed.newSaveFileDialog(func(fwc fyne.URIWriteCloser, err error) {
ed.handleSaveFileCallbackGeneric(fwc, err, func() {
ed.setEditorFile(fileName, msg.GetString())
ed.oldPassword = password
})
})
fileSave.Show()
}
}
},
ed.win)
} else {
ed.setEditorFile(fileName, msg.GetString())
ed.oldPassword = password
}
}
func (ed *EditorWindow) setEditorFile(fileName, text string) {
ed.Reset()
ed.entry.SetText(text)
ed.fileName = fileName
ed.statusLabel.SetText(fileName)
ed.isChanged = false
ed.OnCursorChanged()
}