/
qmldict.go
147 lines (128 loc) · 2.77 KB
/
qmldict.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
// "runtime/pprof"
"strings"
// "fmt"
"gopkg.in/v0/qml"
)
const (
DICT_PATH = "./dictionaries"
)
type Control struct {
Root qml.Object
DictionariesList []string
Len int
SearchResult string
Busy bool
}
var ctrl = &Control{}
var busy chan bool
func (c *Control) DictName(i int) string {
return c.DictionariesList[i]
}
func (c *Control) Search(searchstring string, mode string, active_dicts []string) {
go func() {
busy <- true
c.SearchResult = ""
spacing := " "
if mode == "startswith" {
spacing = ""
}
// log.Printf(
// "Active dictionaries: %v\nSearch string: %s\nSearch mode: %s\n",
// active_dicts,
// searchstring,
// mode,
// )
for _, d := range active_dicts {
data, err := ioutil.ReadFile(path.Join(DICT_PATH, d))
if err != nil {
log.Fatal(err)
}
for _, entry := range strings.Split(string(data), "\n") {
if strings.HasPrefix(strings.ToLower(entry), searchstring+spacing) {
// log.Println(entry)
c.SearchResult += fmt.Sprintf("<b>%s:</b><br />%s<br /><br />", d, entry)
}
}
}
qml.Changed(c, &c.SearchResult)
busy <- false
}()
}
func GetDictList(path string) []string {
var dict_files []string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
dict_files = append(dict_files, f.Name())
}
return dict_files
}
func (c *Control) Load(dictionary string) {
go func() {
// log.Println("LOADING", dictionary)
c.SearchResult = ""
busy <- true
data, err := ioutil.ReadFile(path.Join(DICT_PATH, dictionary))
if err != nil {
log.Fatal(err)
}
c.SearchResult = string(data)
// log.Println("LOADED!")
qml.Changed(c, &c.SearchResult)
busy <- false
}()
}
func (c *Control) Save(text string, filename string) {
// log.Println("SAVING! to", filename)
f, err := os.Create(path.Join(DICT_PATH, filename))
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.WriteString(text)
if err != nil {
log.Fatal(err)
}
}
func main() {
// Profiling -->
// f, err := os.Create("cpu.prof")
// if err != nil {
// log.Fatal(err)
// }
// pprof.StartCPUProfile(f)
// defer pprof.StopCPUProfile()
// <-- Profiling
// Watching busy status
busy = make(chan bool)
go func() {
for {
ctrl.Busy = <-busy
qml.Changed(ctrl, &ctrl.Busy)
}
}()
qml.Init(nil)
engine := qml.NewEngine()
component, err := engine.LoadFile("qmldict.qml")
if err != nil {
panic(err)
}
ctrl.DictionariesList = GetDictList(DICT_PATH)
ctrl.Len = len(ctrl.DictionariesList)
context := engine.Context()
context.SetVar("ctrl", ctrl)
// log.Println(ctrl.DictionariesList)
window := component.CreateWindow(nil)
ctrl.Root = window.Root()
window.Show()
window.Wait()
}