-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
353 lines (312 loc) · 9.79 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// archivebox-quick-add
// 2022 emschu[aet]mailbox.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"image/color"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync/atomic"
"time"
)
// default http client
var httpClient = &http.Client{
Timeout: 10 * time.Second,
}
var archiveSearchHTTPClient = &http.Client{
Timeout: 30 * time.Second,
}
var fyneApplication fyne.App
var window fyne.Window
var windowSize = fyne.Size{Width: 600, Height: 200}
// widgets
var inputEntryWidget *URLInputField
var addToArchiveBtn *widget.Button
var infoLabel *widget.Label
var appConfig applicationConfiguration
var appSessionState sessionState
var isDebug = false
// all app-wide vars besides the archivebox session state belong here
type applicationConfiguration struct {
AppID string
AppName string
AppVersion string
AppLinkToGitHub string
InstanceURL string
Localizer *i18n.Localizer
}
// store archivebox session state, e.g. cookies
type sessionState struct {
IsConnected bool
CsrfToken *http.Cookie
SessionCookie *http.Cookie
CsrfMiddlewareToken string
ConnectionErr error // store latest error
IsSubmissionBlocked atomicBool
IsCloseBlocked atomicBool
}
const (
preferenceInstanceURL = "InstanceURL" // string
preferenceUsername = "Username" // string
preferencePassword = "Password" // string
preferenceBorderless = "Borderless" // bool
preferenceCheckAdd = "CheckAdd" // bool
preferenceCloseAfterAdd = "CloseAfterAdd" // bool
preferenceFirstRun = "FirstRun" // bool
)
func main() {
appConfig = applicationConfiguration{
AppID: "org.archivebox.go-quick-add",
AppName: "ArchiveBox Quick-Add",
AppVersion: "1.9",
AppLinkToGitHub: "https://github.com/emschu/archivebox-quick-add",
}
appSessionState = sessionState{}
appSessionState.IsConnected = false
appSessionState.IsSubmissionBlocked = *newAtomicBool(false)
appSessionState.IsCloseBlocked = *newAtomicBool(false)
appConfig.initI18n()
fyneApplication = app.NewWithID(appConfig.AppID)
fyneApplication.SetIcon(resourceIconPng)
// load archive box instance url
appConfig.InstanceURL = fyneApplication.Preferences().StringWithFallback(preferenceInstanceURL, "http://127.0.0.1:8000")
isFirstRun := fyneApplication.Preferences().BoolWithFallback(preferenceFirstRun, true)
if isFirstRun {
// initial preference setup
appConfig.doInitialPreferenceSetup()
}
isSplashScreen := fyneApplication.Preferences().BoolWithFallback(preferenceBorderless, true)
drv, ok := fyne.CurrentApp().Driver().(desktop.Driver)
if ok && isSplashScreen {
window = drv.CreateSplashWindow()
} else {
window = fyneApplication.NewWindow(appConfig.AppName)
}
window.CenterOnScreen()
window.SetFixedSize(false)
window.SetFullScreen(false)
window.SetMaster()
window.SetPadded(false)
window.Canvas().SetOnTypedKey(func(k *fyne.KeyEvent) {
if k.Name == fyne.KeyEscape {
appConfig.safeClose()
}
if k.Name == fyne.KeyReturn {
archiveURL(inputEntryWidget.Text)
}
})
logoTextItem := canvas.NewText(appConfig.AppName, color.White)
logoTextItem.Alignment = fyne.TextAlignCenter
logoTextItem.TextSize = 25
logoTextItem.TextStyle = fyne.TextStyle{
Bold: true,
Italic: false,
Monospace: false,
}
instanceInfoLabel := widget.NewLabel(t("ArchiveBoxInstanceURL"))
logoTextItem.Alignment = fyne.TextAlignCenter
logoTextItem.TextSize = 18
infoLabel = widget.NewLabel("") // empty at startup
infoLabel.Wrapping = fyne.TextWrapBreak
infoLabel.TextStyle = fyne.TextStyle{
Bold: true,
Italic: false,
Monospace: false,
}
defer func() {
doArchiveBoxLogout()
}()
parsedURL, err := url.Parse(appConfig.InstanceURL)
var instanceLink fyne.Widget
if err != nil {
log.Printf("No valid url to archivebox instance\n")
instanceLink = widget.NewLabel("")
} else {
instanceLink = widget.NewHyperlink(appConfig.InstanceURL, parsedURL)
}
inputEntryWidget = newURLInputField()
go setupArchiveBoxConnection()
addToArchiveBtn = widget.NewButtonWithIcon(t("AddToArchive"), theme.ContentAddIcon(), func() {})
cancelBtn := widget.NewButtonWithIcon(t("Close"), theme.CancelIcon(), func() {
appConfig.safeClose()
})
clipBoardBtn := widget.NewButtonWithIcon(t("PasteClipboard"), theme.ContentPasteIcon(), func() {
pasteClipboard()
})
inputEntryWidget.OnSubmitted = func(s string) {
archiveURL(s)
}
addToArchiveBtn.OnTapped = func() {
archiveURL(inputEntryWidget.Text)
}
settingsBtn := widget.NewButtonWithIcon(t("Settings"), theme.SettingsIcon(), func() {
showSettingsDialog()
})
infoBtn := widget.NewButtonWithIcon(t("Info"), theme.InfoIcon(), func() {
appSessionState.IsSubmissionBlocked.setTrue()
appSessionState.IsCloseBlocked.setTrue()
informationD := dialog.NewInformation(t("Information"),
fmt.Sprintf("%s\n%d - %s: %s\n%s: %s\n\n%s\n\n%s",
appConfig.AppName, time.Now().Year(), t("Version"), appConfig.AppVersion,
t("License"), "GNU Affero General Public License v3", appConfig.AppLinkToGitHub, t("InfoIndependence")), window)
informationD.SetOnClosed(func() {
appSessionState.IsSubmissionBlocked.setFalse()
appSessionState.IsCloseBlocked.setFalse()
})
informationD.Show()
})
window.SetContent(container.NewVBox(
container.NewHBox(
layout.NewSpacer(),
logoTextItem,
layout.NewSpacer(),
settingsBtn,
infoBtn,
),
container.NewHBox(
instanceInfoLabel,
instanceLink,
),
infoLabel,
inputEntryWidget,
addToArchiveBtn,
clipBoardBtn,
cancelBtn,
))
window.Resize(windowSize)
// called on startup
go func() {
time.Sleep(300 * time.Millisecond)
window.Canvas().Focus(inputEntryWidget)
pasteClipboard()
}()
window.ShowAndRun()
}
func (*applicationConfiguration) doInitialPreferenceSetup() {
fyneApplication.Preferences().SetString(preferenceInstanceURL, "http://127.0.0.1:8000")
fyneApplication.Preferences().SetString(preferenceUsername, "")
fyneApplication.Preferences().SetString(preferencePassword, "")
fyneApplication.Preferences().SetBool(preferenceBorderless, true)
fyneApplication.Preferences().SetBool(preferenceCloseAfterAdd, true)
fyneApplication.Preferences().SetBool(preferenceCheckAdd, true)
fyneApplication.Preferences().SetBool(preferenceFirstRun, false)
}
func (*applicationConfiguration) initI18n() {
var selectedLang = language.English
var langResource = resourceEnJson
// TODO extend language detection mechanism
for _, s := range os.Environ() {
envVar := strings.ToLower(s)
if strings.HasPrefix(envVar, "lang") && strings.Contains(envVar, "de_de") {
selectedLang = language.German
langResource = resourceDeJson
}
}
bundle := i18n.NewBundle(selectedLang)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
bundle.MustParseMessageFileBytes(langResource.StaticContent, langResource.StaticName)
appConfig.Localizer = i18n.NewLocalizer(bundle, selectedLang.String())
}
// central method to translate strings, supports up to two format args
func tWithArgs(s string, args interface{}) string {
localizeMessage, err := appConfig.Localizer.Localize(&i18n.LocalizeConfig{
MessageID: s,
TemplateData: args,
})
if err != nil {
log.Printf("Warning! '%s'\n", err.Error())
return s
}
if len(localizeMessage) == 0 {
return s
}
return localizeMessage
}
func t(s string) string {
message := &i18n.Message{
ID: s,
}
localizeMessage, err := appConfig.Localizer.LocalizeMessage(message)
if err != nil {
log.Printf("Cannot get message '%s'\n", err.Error())
return s
}
if len(localizeMessage) == 0 {
return s
}
return localizeMessage
}
func (*applicationConfiguration) safeClose() {
if appSessionState.IsCloseBlocked.isSet() {
return
}
if len(strings.TrimSpace(inputEntryWidget.Text)) > 5 {
// lock submission
appSessionState.IsCloseBlocked.setTrue()
appSessionState.IsSubmissionBlocked.setTrue()
confirmD := dialog.NewConfirm(t("Cancel"), t("DoYouReallyWantToClose"), func(decision bool) {
if decision { // = yes
fyneApplication.Quit()
}
}, window)
confirmD.SetOnClosed(func() {
appSessionState.IsSubmissionBlocked.setFalse()
appSessionState.IsCloseBlocked.setFalse()
})
confirmD.Show()
} else {
// close immediately if input is empty
fyneApplication.Quit()
}
}
func (*applicationConfiguration) disconnect() {
appSessionState.IsConnected = false
log.Printf("Warn: No connection could be established!\n")
infoLabel.Text = t("NoConnectionPossible")
if appSessionState.ConnectionErr != nil {
infoLabel.Text += " " + appSessionState.ConnectionErr.Error()
}
infoLabel.Refresh()
}
type atomicBool int32
func (b *atomicBool) isSet() bool { return atomic.LoadInt32((*int32)(b)) != 0 }
func (b *atomicBool) setTrue() { atomic.StoreInt32((*int32)(b), 1) }
func (b *atomicBool) setFalse() { atomic.StoreInt32((*int32)(b), 0) }
func newAtomicBool(startVal bool) *atomicBool {
var ab atomicBool
if startVal {
ab.setTrue()
} else {
ab.setFalse()
}
return &ab
}