/
interface.go
326 lines (304 loc) · 12.3 KB
/
interface.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
package main
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/skratchdot/open-golang/open"
controller "github.com/amlwwalker/wingit/packages/controller"
utils "github.com/amlwwalker/wingit/packages/utils"
"github.com/therecipe/qt/core"
)
type QmlUser struct {
core.QObject
_ string `property:"name"`
_ string `property:"username"`
_ string `property:"service"`
_ string `property:"picture"`
_ string `property:"apiKey"`
}
/*
type User struct {
Id string `"json":"user_id"`
Email string `"json":"email"`
Name string `"json":"name"`
Picture string `"json":"picture"`
Locale string `"json":"locale"`
ApiKey string `"json":"-"`
}
*/
type QmlBridge struct {
core.QObject
hotLoader HotLoader
business BusinessInterface
RefreshTicker *time.Ticker
TickerEnabled bool `property:"tickerEnabled"`
User *QmlUser `property:"qmlUser"`
//qml properties:
// UserName string `property:"username"`
// UsersEmail string `property:"usersEmail"`
// UserPic string `property:"userPic"`
_ func(p string) `signal:"updateLoader"`
//updates the progress bars on the front end
_ func(c float64, indeterminate bool) `signal:"updateProcessStatus"`
_ func(message string) `signal:"popUpToast"`
_ func(message string) `signal:"setMessage"`
_ func(username, error string) `signal:"signalLogin"`
_ func() `signal:"signalLogout"`
//requests from qml
//searchFor processes the searching request from the front end
_ func(regex string) `slot:"searchFor"`
_ func() `slot:"checkForUser"`
_ func() bool `slot:"toggleAutoSync"`
_ func() bool `slot:"toggleState"`
_ func(title, message string) `slot:"createNotification"`
_ func(pendingUser, fileUrl string) string `slot:"updatePendingUpload"`
_ func() `slot:"deleteContact"`
_ func(searchResult string, index int) `slot:"addResultToContacts"`
_ func(userId string) `slot:"updatePendingUser"`
_ func(fileName string) `slot:"beginFileDownload"`
_ func() `slot:"getDownloads"`
_ func(path string) `slot:"openFile"`
_ func() `slot:"openDowloadsDirectory"`
_ func() `slot:"syncFiles"`
_ func(apiKey string) `slot:"loginUser"`
_ func() `slot:"logout"`
}
//setup functions to communicate between front end and back end
//example of receiving data from frontend and returning a result
func (q *QmlBridge) ConfigureBridge(config utils.Config) {
//1. configure the hotloader
q.business = BusinessInterface{}
q.business.configureInterface(q.SignalLogin, q.UpdateProcessStatus, config)
q.hotLoader = HotLoader{} //may not need it, specified in main.go
q.business.CONTROLLER.StoreStatusMessage("business configured", 1)
q.RefreshTicker = time.NewTicker(60 * time.Second)
q.User = NewQmlUser(nil) //create a person to add to the contacts
//periodical functionality
go func() {
for {
<-q.RefreshTicker.C
fmt.Println("auto sync enabled ? ", q.TickerEnabled)
if q.TickerEnabled {
q.business.SynchronizeWithServer(q.SetMessage)
}
}
}()
q.ConnectToggleAutoSync(func() bool {
q.TickerEnabled = !q.TickerEnabled
return q.TickerEnabled
})
q.ConnectToggleState(func() bool {
return q.TickerEnabled
})
//2. Configure signals
q.ConnectSearchFor(func(regex string) {
//in here we are going to add matches to the search model
//that way the front end will be updated live
//inform front end work has begun
fmt.Println("requested search for: " + regex)
q.UpdateProcessStatus(0.0, true) //required as no idea where it is so needs a start point
q.business.searchForMatches(regex, q.UpdateProcessStatus)
})
q.ConnectAddResultToContacts(func(searchResult string, index int) {
//in here we are going to add matches to the search model
//that way the front end will be updated live
//inform front end work has begun
fmt.Println("adding to contacts: " + searchResult)
p := q.business.CONTROLLER.CreatePersonFromIdentifier(searchResult)
q.business.CONTROLLER.AddContactToList(p)
fmt.Println("deleting key for "+q.business.CONTROLLER.CRYPTO.KeyFolder, p.Name+".pem")
q.business.CONTROLLER.CRYPTO.DeletePublickKeyForContact(q.business.CONTROLLER.CRYPTO.KeyFolder, p.Name+".pem")
//when we add a search result we want to remove them from the search results
//and add them to our cotacts
//we need to clear the list and redisplay it so we don't get repeats
q.business.sModel.removePerson(index)
q.business.pModel.ClearPeople()
for _, v := range q.business.CONTROLLER.Contacts.People {
// fmt.Println("after updating files, adding: ", v.UserId)
addPersonToList(*v, q.business.pModel)
}
// q.business.pModel.AddPerson(qP)
// q.business.sModel.removePerson(index)
})
q.ConnectSyncFiles(func() {
q.TickerEnabled = !q.TickerEnabled
//manuall synchronising with the server will toggle auto sync
q.business.SynchronizeWithServer(q.SetMessage)
q.SetMessage("SyncK'd!")
})
q.ConnectUpdatePendingUser(func(pendingUser string) {
q.business.CurrentSelectedContact = pendingUser
//if the files have been synced already, we just need to update
//the file list to the files owned by the currently selected contact
//need to clear the list first
q.business.fModel.ClearFiles()
fmt.Println("pending user: " + pendingUser)
fmt.Println("files: ", q.business.CONTROLLER.Contacts.People[pendingUser])
for _, v := range q.business.CONTROLLER.Contacts.People[pendingUser].Files {
//now we add each one to the file list
var f = NewFile(nil)
f.SetFilePath(v.FileName)
f.SetFileSize(strconv.Itoa(v.FileSize))
f.SetFileSource(v.UserID)
q.business.fModel.AddFile(f)
}
})
q.ConnectUpdatePendingUpload(func(pendingUser, fileUrl string) string {
fmt.Println("user ", q.business.CONTROLLER.User, " uploading for ", pendingUser)
//checks
if q.business.CONTROLLER.User.Username == "" {
fmt.Println("blocking upload as no username")
q.PopUpToast("Please set a username")
q.business.CONTROLLER.SERVER.UploadProgress(1.0, errors.New("Anonymous users cannot upload files"))
return "Please set a username"
}
if !q.business.CONTROLLER.User.Verified && !q.business.CONTROLLER.User.Enabled {
fmt.Println("blocking upload as user is anonymous")
q.PopUpToast("Anonymous users cannot upload files")
q.business.CONTROLLER.SERVER.UploadProgress(1.0, errors.New("Anonymous users cannot upload files"))
return "Anonymous users cannot upload files"
}
q.business.CurrentSelectedContact = pendingUser
q.business.PendingUpload = fileUrl
fmt.Println("received: ", q.business.PendingUpload)
fmt.Println("for: ", q.business.CurrentSelectedContact)
//however now we just need to upload the file to the server
fmt.Println("beginning upload")
q.business.CONTROLLER.SERVER.UploadProgress(0.0, nil)
q.business.CONTROLLER.UploadFileToContact(pendingUser, fileUrl)
return "Uploading..."
})
q.ConnectDeleteContact(func() {
//q.business.CurrentSelectedContact
//so this we will have the current contacts name
//we have to search them
//contacts can do this...
fmt.Println("requested to delete ", q.business.CurrentSelectedContact)
q.business.CONTROLLER.DeleteContactFromList(q.business.CurrentSelectedContact)
q.business.pModel.ClearPeople()
for _, v := range q.business.CONTROLLER.Contacts.People {
// fmt.Println("after updating files, adding: ", v.UserId)
addPersonToList(*v, q.business.pModel)
}
})
q.ConnectBeginFileDownload(func(fileName string) {
fmt.Println("beginning file download")
q.PopUpToast("File Download Beginning")
q.UpdateProcessStatus(0.0, false) //to show the progress bar
q.business.CONTROLLER.DownloadFileFromContact(q.business.CurrentSelectedContact, fileName)
})
q.ConnectOpenFile(func(path string) {
q.business.CONTROLLER.OpenDownloadedFile(path)
})
q.ConnectOpenDowloadsDirectory(func() {
fmt.Println("opening downloads directory at " + q.business.CONTROLLER.SERVER.DownloadFolder)
open.Run(q.business.CONTROLLER.SERVER.DownloadFolder)
})
q.ConnectGetDownloads(func() {
//this shows the user all the downloads they have
if files, err := q.business.CONTROLLER.GetDownloadedFiles(); err != nil {
//couldn't retrieve errors
fmt.Println("error retrieving previously downloaded files", err)
} else {
q.business.dModel.ClearFiles()
for _, v := range files {
var f = NewFile(nil)
f.SetFilePath(v.FileName)
f.SetFileSize(strconv.Itoa(v.FileSize))
q.business.dModel.AddFile(f)
}
}
})
q.ConnectCheckForUser(func() {
//here we can test if the database has a user in it.
//if it does, signal the user to the front end
//if it finds a result, it will call UserAuthenticated
q.business.CONTROLLER.StoreStatusMessage("checking if user exists: ", 1)
if err := q.business.CONTROLLER.CheckForExistingUser(); err != nil {
//there was no user, return
return
}
//sets the user on the accounts page on the front end
q.User.SetName(q.business.CONTROLLER.User.Name)
q.User.SetUsername(q.business.CONTROLLER.User.Username)
q.User.SetService(q.business.CONTROLLER.User.Service) //authentication mechanism
q.User.SetPicture(q.business.CONTROLLER.User.Picture)
q.User.SetApiKey(q.business.CONTROLLER.User.ApiKey)
//checks for any files for this user
q.business.SynchronizeWithServer(q.SetMessage)
q.TickerEnabled = true
})
q.ConnectLoginUser(func(apiKey string) {
//this calls the server to open the login
//but once the server has authd the user
//an asynchronous response will tell the user that
//login was successful
//this function results in a callback running
//so that this signal doesn't end up dealing with all post auth
//logic
// q.business.notifier.Push("Authentication", "Attempting Login")
fmt.Println("api key received: ", apiKey)
if u, err := q.business.CONTROLLER.Authorize(apiKey); err != nil {
fmt.Println("Auth failed; ", err)
} else {
fmt.Println("user: ", u)
//at this point, we can create the users contact bucket
if err := controller.CreateUserContactBucket(u.UserId.String(), q.business.CONTROLLER.Db); err != nil {
fmt.Println("could not create a contact bucket for the user: ", err)
}
//sets the user on the accounts page on the front end
q.User.SetName(u.Name)
q.User.SetUsername(u.Username)
q.User.SetService(u.Service) //authentication mechanism
q.User.SetPicture(u.Picture)
q.User.SetApiKey(u.ApiKey)
//checks for any files for this user
q.business.SynchronizeWithServer(q.SetMessage)
q.TickerEnabled = true
}
})
q.ConnectCreateNotification(func(title, message string) {
q.business.notifier.Push(title, message)
})
//we might need to clear the contacts and files
q.ConnectLogout(func() {
//this calls the server to open the login
//but once the server has authd the user
//an asynchronous response will tell the user that
//login was successful
q.business.CONTROLLER.Logout(func() {
//blank the user
fmt.Println("cleared user: ", q.business.CONTROLLER.User)
//need to clear current user settings out of the DB
// q.UserName = "" //clear for frontend
//be good to get user pics and store them in db
// q.UserPic = ""
//clear the contacts object
//later there should be a function to reset the controller in its entirety
q.business.CurrentSelectedContact = "" //clear the user that was selected
for k := range q.business.CONTROLLER.Contacts.People {
delete(q.business.CONTROLLER.Contacts.People, k)
}
//clear all front end lists
q.business.pModel.ClearPeople()
q.business.sModel.ClearPeople()
q.business.fModel.ClearFiles()
q.business.dModel.ClearFiles()
//tell the UI logout has occured
q.SignalLogout()
})
})
q.business.CONTROLLER.StoreStatusMessage("bridge configured", 1)
}
// helper function to add people to one of the interface models
func addPersonToList(tmp controller.Person, model *PersonModel) {
fmt.Println("about to add: ", tmp.UserId, " to list with ", tmp.Len, " files")
var p = NewPerson(nil)
p.SetFirstName(tmp.Name)
p.SetLastName("")
p.SetEmail(tmp.UserId)
p.SetFileCount(strconv.Itoa(tmp.Len))
fmt.Println("adding person to front end: " + tmp.UserId)
model.AddPerson(p)
}