-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
234 lines (206 loc) · 7.68 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
const MicroservicePath = "password_microservice/save_user_password.py"
const MSCommPath = "password_microservice/user_password.txt"
const UserPasswordText = `
Please enter a username and password. You will have to reenter the username
and password to regain access to the file
`
const UserGuideText = `
Purpose: A Desktop application for securely writing, deleting, and saving text files.
Home page User Interface
Text box: Below the toolbar is a large text box for writing and deleting text.
Toolbar: On the top of the application is a toolbar for navigating the application.
The contents are detailed below from left to right.
Actions: A button that opens a pop down menu with the primary application actions.
User guide: A button that opens a pop up text box that details the user guide
for the application.
New: Opens a new, blank text file.
Open: Opens the file directory, the user can select any text file from the
local computer to open into the application. If a password has been set
for the requested file, then a pop up menu opens requesting the user to
enter their password.
Save: Opens the file directory, the user can select any folder in the local
computer to save the currently worked on text file.
Delete: Opens a pop up menu that confirms whether or not the user would like
to delete the current text file. If the user presses yes, then the current
file is deleted. If the user presses no, then the user returns to the
application.
Undo: Undoes the last action. For example, if a character was written last,
then delete that character. Or if a character was deleted last, then
rewrite that character.
Copy: Copies the selected text into the clipboard for temporary storage.
The copied text can be used later with the paste command.
Paste: Paste the previously copied text stored in the clipboard at the spot
selected by the cursor.
Set Password for file: Opens a pop up box that requests the user to enter and
confirm a password. Once the user has entered and confirmed a password,
the user can confirm and set the password for the file. Otherwise the user
can exit the pop up box and return to the application without setting a
password.
Exit button: Appears as an X. This button opens a pop up text box that asks the user to
confirm that they want to exit the application. If the user presses no, then the user
is returned to the application. If the user presses yes, then the application closes.
`
func main() {
// main loop for to run the application
myApp := app.New()
setUpMainWindow(myApp)
myApp.Run()
tidyUp()
}
func setUpMainWindow(myApp fyne.App) {
// sets up the main window for the given application
mainWindow := myApp.NewWindow("Encrypted Text Editor")
mainWindow.Resize(fyne.NewSize(1200, 700))
mainWindow.SetMainMenu(actionBar(myApp))
mainWindowCloseIntercept(myApp, mainWindow)
textBox := widget.NewMultiLineEntry()
textBox.AcceptsTab()
mainWindow.SetContent(textBox)
mainWindow.Show()
}
func mainWindowCloseIntercept(myApp fyne.App, mainWindow fyne.Window) {
// sets up a window to open upon closing for the given application
mainWindow.SetCloseIntercept(func() {
closeWindow := myApp.NewWindow("Confirm Exit")
closeWindowContent := makeCloseWindowContent(closeWindow, mainWindow)
closeWindow.SetContent(closeWindowContent)
closeWindow.Show()
})
}
func makeCloseWindowContent(closeWindow, mainWindow fyne.Window) *fyne.Container {
// Adds text and a confirmation button for the given close window
closeWindowContent := container.NewVBox(
widget.NewLabel("Please confirm that you would like to exit the application"),
widget.NewButton("Confirm", func() {
mainWindow.Close()
closeWindow.Close()
}),
)
return closeWindowContent
}
func actionBar(myApp fyne.App) *fyne.MainMenu {
// sets up and returns the buttons for the action bar main menu for the given app
buttons := []*fyne.MenuItem{
makeUserGuideButton(myApp), makeNewButton(myApp), makeOpenButton(myApp),
makeSaveButton(myApp), makeDeleteButton(myApp), makeUndoButton(myApp),
makeCopyButton(myApp), makePasteButton(myApp), makeSetPasswordButton(myApp),
}
actionPopDown := fyne.NewMenu("Actions", buttons...)
actions := fyne.NewMainMenu(actionPopDown)
return actions
}
func makeUserGuideButton(myApp fyne.App) *fyne.MenuItem {
userGuideButton := fyne.NewMenuItem("UserGuide", func() {
userGuideWindow := myApp.NewWindow("User Guide")
userGuideWindow.Resize(fyne.NewSize(400, 240))
userGuideText := widget.NewLabel(UserGuideText)
userGuideWindow.SetContent(userGuideText)
userGuideWindow.Show()
})
return userGuideButton
}
func makeNewButton(myApp fyne.App) *fyne.MenuItem {
newButton := fyne.NewMenuItem("New File", func() {fmt.Println("Pressed")})
return newButton
}
func makeOpenButton(myApp fyne.App) *fyne.MenuItem {
openButton := fyne.NewMenuItem("Open a File", func() {fmt.Println("Pressed")})
return openButton
}
func makeSaveButton(myApp fyne.App) *fyne.MenuItem {
saveButton := fyne.NewMenuItem("Save the File", func() {fmt.Println("Pressed")})
return saveButton
}
func makeDeleteButton(myApp fyne.App) *fyne.MenuItem {
deleteButton := fyne.NewMenuItem("Delete the File", func() {
deleteWindow := myApp.NewWindow("Delete")
deleteWindow.Resize(fyne.NewSize(400, 240))
deleteWidgets := makeDeleteWidgets()
deleteWindow.SetContent(deleteWidgets)
deleteWindow.Show()
})
return deleteButton
}
func makeDeleteWidgets() *fyne.Container {
deleteWidgets := container.NewVBox(
widget.NewButton("Confirm", func() {}),
widget.NewLabel("Please confirm that you would like to delete the file"),
)
return deleteWidgets
}
func makeUndoButton(myApp fyne.App) *fyne.MenuItem {
undoButton := fyne.NewMenuItem("Undo the Last action", func() {fmt.Println("Pressed")})
return undoButton
}
func makeCopyButton(myApp fyne.App) *fyne.MenuItem {
copyButton := fyne.NewMenuItem("Copy Selected Text", func() {fmt.Println("Pressed")})
return copyButton
}
func makePasteButton(myApp fyne.App) *fyne.MenuItem {
pasteButton := fyne.NewMenuItem("Paste Copied Text", func() {})
return pasteButton
}
func makeSetPasswordButton(myApp fyne.App) *fyne.MenuItem {
setPasswordButton := fyne.NewMenuItem("Set a Password", func() {
passwordWindow := myApp.NewWindow("Set a Password")
passwordWindow.Resize(fyne.NewSize(400, 240))
passwordWidgets := makePasswordWidgets(passwordWindow)
passwordWindow.SetContent(passwordWidgets)
passwordWindow.Show()
})
return setPasswordButton
}
func makePasswordWidgets(passwordWindow fyne.Window) *fyne.Container {
userName, password := widget.NewEntry(), widget.NewEntry()
userName.SetPlaceHolder("Enter username here")
password.SetPlaceHolder("Enter password here")
passwordWidgets := container.NewVBox(userName, password,
widget.NewButton("Confirm", func() {save(userName.Text, password.Text)
passwordWindow.Close()
}),
widget.NewLabel(UserPasswordText),
)
return passwordWidgets
}
func save(userName, password string) int {
// Saves the given username and password to a data.json file using a microservice
file, err := os.Create(MSCommPath)
checkFor(err)
n, err := file.WriteString(userName + "\n" + password)
checkFor(err)
file.Sync()
runMicroservice()
return n
}
func runMicroservice() {
// runs the microservice that saves the username and password
cmd := exec.Command("/usr/bin/python3", MicroservicePath)
if errors.Is(cmd.Err, exec.ErrDot) {
cmd.Err = nil
}
go func(){
if cmd.Run()!= nil{
panic("Error")
}
}()
}
func checkFor(err error) {
if err != nil {
log.Fatal(err)
}
}
func tidyUp() {
fmt.Println("Exited")
}