-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
217 lines (176 loc) · 5.58 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
package main
import (
"embed"
"encoding/json"
"os"
"path/filepath"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
type Settings struct {
TargetFolder string `json:"targetFolder"`
FilesToDelete []string `json:"filesToDelete"`
}
type SettingsNew struct {
TargetFolder string `json:"targetFolder"`
FilesToDelete []SettingsFilesGroup `json:"filesToDelete"`
}
type SettingsFilesGroup struct {
Name string `json:"name"`
Files []string `json:"files"`
}
//go:embed *.json
var settingFile embed.FS
var settings Settings
var settingsNew SettingsNew
var filesToDelete []string
func (s *Settings) read() string {
var logText string
settingsData, err := settingFile.ReadFile("settings.json")
if err != nil {
logText += "❌ Error reading the settings file => " + err.Error() + "\n"
} else {
logText += "✅ Reading settings.json successfull\n"
if err := json.Unmarshal(settingsData, s); err != nil {
logText += "❌ Error parsing settings from settings.json => " + err.Error() + "\n"
} else {
logText += "🔄 The settings have been parsed successfully\n"
}
}
return logText
}
func (s *SettingsNew) read() string {
var logText string
settingsData, err := settingFile.ReadFile("settings.new.json")
if err != nil {
logText += "❌ Error reading the settings file => " + err.Error() + "\n"
} else {
logText += "✅ Reading NEW settings.new.json successfull\n"
if err := json.Unmarshal(settingsData, s); err != nil {
logText += "❌ Error parsing settings from settings.json => " + err.Error() + "\n"
} else {
logText += "🔄 The NEW settings have been parsed successfully\n"
}
}
return logText
}
func createLogArea() (*widget.Label, *container.Scroll) {
logsOutput := widget.NewLabel("Welcome to XIV Translation Cleaner\n\n")
logsOutput.Wrapping = fyne.TextWrapBreak
logContainer := container.NewScroll(logsOutput)
return logsOutput, logContainer
}
func createFolderSelectionEntry() *widget.Entry {
folderEntry := widget.NewEntry()
folderEntry.SetPlaceHolder("Enter the path")
folderEntry.SetText(settings.TargetFolder)
return folderEntry
}
func createCheckboxes(logsOutput *widget.Label) *fyne.Container {
checkboxesLabel := widget.NewLabel("Select the translation that you want to delete:")
checkboxesLabel.TextStyle.Bold = true
checkboxesLayer := container.NewVBox(
checkboxesLabel,
)
for _, data := range settingsNew.FilesToDelete {
cbData := &SettingsFilesGroup{Name: data.Name, Files: data.Files}
cb := widget.NewCheck(cbData.Name, func(checked bool) {
if checked {
logsOutput.SetText(logsOutput.Text + "Checked Files: " + strings.Join(cbData.Files, ", ") + "\n")
filesToDelete = append(filesToDelete, cbData.Files...)
}
})
checkboxesLayer.Add(cb)
}
return checkboxesLayer
}
// func getOnlySelected() {
// }
func createDeleteButton(logsOutput *widget.Label, logContainer *container.Scroll, folderEntry *widget.Entry, myWindow fyne.Window) *widget.Button {
deleteButton := widget.NewButton("Delete selected translation", func() {
targetFolder := folderEntry.Text
// Check if the target folder exists
if _, err := os.Stat(targetFolder); os.IsNotExist(err) {
logsOutput.SetText(logsOutput.Text + "Target folder does not exist: " + targetFolder + "\n")
logContainer.ScrollToBottom()
return
}
// Confirm with the user before proceeding
confirm := dialog.NewConfirm("Confirmation", "Do you want to delete selected translation?", func(accept bool) {
if accept {
deleteSelectedItems(logsOutput, logContainer, targetFolder)
}
}, myWindow)
confirm.SetDismissText("Cancel")
confirm.Show()
})
return deleteButton
}
func deleteSelectedItems(logsOutput *widget.Label, logContainer *container.Scroll, targetFolder string) {
// Loop through the list of files to delete and delete them
for _, fileToDelete := range settings.FilesToDelete {
filePath := filepath.Join(targetFolder, "exd", fileToDelete)
fileInfo, err := os.Stat(filePath)
if err != nil {
logsOutput.SetText(logsOutput.Text + "❌ " + err.Error() + "\n")
logContainer.ScrollToBottom()
continue
}
if fileInfo.IsDir() {
err = os.RemoveAll(filePath)
} else {
err = os.Remove(filePath)
}
if err != nil {
logsOutput.SetText(logsOutput.Text + "❌ " + err.Error() + "\n")
logContainer.ScrollToBottom()
} else {
logsOutput.SetText(logsOutput.Text + "✅ Deleted: " + fileToDelete + "\n")
logContainer.Refresh()
}
}
logsOutput.SetText(logsOutput.Text + "\n---- Selected items were deleted ----\n\n")
logContainer.Refresh()
}
func main() {
myApp := app.New()
// myApp.Settings().SetTheme(theme.DarkTheme())
myWindow := myApp.NewWindow("XIV Translation Cleaner")
logsOutput, logContainer := createLogArea()
// Init Settings
logsOutput.SetText(logsOutput.Text + settings.read())
logsOutput.SetText(logsOutput.Text + settingsNew.read())
logContainer.ScrollToBottom()
folderEntry := createFolderSelectionEntry()
checkboxes := createCheckboxes(logsOutput)
deleteButton := createDeleteButton(logsOutput, logContainer, folderEntry, myWindow)
// Layout
topRow := container.New(
layout.NewFormLayout(),
widget.NewLabel("Path to the trasnlation folder: "),
folderEntry,
)
middleRow := container.NewVBox(
checkboxes,
container.New(
layout.NewCenterLayout(),
deleteButton,
),
)
// Create main container
content := container.NewVSplit(
container.NewVBox(
topRow,
middleRow,
),
logContainer,
)
myWindow.SetContent(content)
myWindow.Resize(fyne.NewSize(900, 600))
myWindow.ShowAndRun()
}