-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodels.go
372 lines (317 loc) · 7.29 KB
/
models.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"io"
"io/ioutil"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gosexy/gettext"
"github.com/janimo/textsecure"
"gopkg.in/qml.v1"
"gopkg.in/yaml.v2"
)
// Model for the contacts
type Contacts struct {
contacts []textsecure.Contact
Len int
}
func (c *Contacts) Contact(i int) textsecure.Contact {
if i == -1 {
return textsecure.Contact{}
}
return c.contacts[i]
}
//HACK
func telToName(tel string) string {
if g, ok := groups[tel]; ok {
return g.Name
}
for _, c := range contactsModel.contacts {
if c.Tel == tel {
return c.Name
}
}
if tel == config.Tel {
return "Me"
}
return tel
}
var contactsModel *Contacts = &Contacts{}
func refreshContacts() {
c, err := textsecure.GetRegisteredContacts()
if err != nil {
showError(err)
}
contactsModel.contacts = c
contactsModel.Len = len(c)
qml.Changed(contactsModel, &contactsModel.Len)
}
func getContactForTel(tel string) *textsecure.Contact {
for _, c := range contactsModel.contacts {
if c.Tel == tel {
return &c
}
}
return nil
}
func (api *textsecureAPI) FilterContacts(sub string) {
sub = strings.ToUpper(sub)
fc := []textsecure.Contact{}
for _, c := range contactsModel.contacts {
if strings.Contains(strings.ToUpper(telToName(c.Tel)), sub) {
fc = append(fc, c)
}
}
cm := &Contacts{fc, len(fc)}
engine.Context().SetVar("contactsModel", cm)
}
// Model for application settings
type Settings struct {
SendByEnter bool `yaml:"sendByEnter"`
}
var settingsModel *Settings
func loadSettings() (*Settings, error) {
s := &Settings{}
b, err := ioutil.ReadFile(settingsFile)
if err != nil {
return s, err
}
err = yaml.Unmarshal(b, s)
if err != nil {
return s, err
}
return s, nil
}
func saveSettings(s *Settings) error {
b, err := yaml.Marshal(s)
if err != nil {
return err
}
return ioutil.WriteFile(settingsFile, b, 0600)
}
func (api *textsecureAPI) SaveSettings() error {
return saveSettings(settingsModel)
}
var (
msgFlagGroupNew = 1
msgFlagGroupUpdate = 2
msgFlagGroupLeave = 4
msgFlagResetSession = 8
)
// Model for existing chat sessions
type Message struct {
ID int64
SID int64
Source string
Message string
Outgoing bool
SentAt uint64
ReceivedAt uint64
HTime string
CType int
Attachment string
IsSent bool
IsRead bool
Flags int
}
func (m *Message) Info() string {
timeFormat := time.RFC1123Z
s := gettext.Gettext("Sent") + ": " + time.Unix(int64(m.SentAt/1000), 0).Format(timeFormat)
if m.ReceivedAt != 0 {
s += "\n" + gettext.Gettext("Received") + ": " + time.Unix(int64(m.ReceivedAt/1000), 0).Format(timeFormat)
}
return s
}
func (m *Message) Name() string {
return telToName(m.Source)
}
type Session struct {
ID int64
Name string
Tel string
IsGroup bool
Last string
Timestamp uint64
When string
CType int
messages []*Message
Unread int
Active bool
Len int
}
type Sessions struct {
sessions []*Session
Len int
}
func (s *Sessions) Session(i int) *Session {
return s.sessions[i]
}
func (s *Sessions) GetIndex(tel string) int {
for i, ses := range s.sessions {
if ses.Tel == tel {
return i
}
}
return -1
}
func (s *Sessions) Get(tel string) *Session {
for _, ses := range s.sessions {
if ses.Tel == tel {
return ses
}
}
ses := &Session{Tel: tel, Name: telToName(tel), Active: true, IsGroup: tel[0] != '+'}
s.sessions = append(s.sessions, ses)
s.Len++
qml.Changed(s, &s.Len)
saveSession(ses)
return ses
}
func (s *Session) MarkRead() {
s.Unread = 0
qml.Changed(s, &s.Unread)
updateSession(s)
}
var sessionsModel = &Sessions{
sessions: make([]*Session, 0),
}
func (s *Session) Messages(i int) *Message {
//FIXME when is index -1 ?
if i == -1 || i >= len(s.messages) {
return &Message{}
}
return s.messages[i]
}
type GroupRecord struct {
ID int64
GroupID string
Name string
Members string
Avatar []byte
Active bool
}
func (api *textsecureAPI) FilterSessions(sub string) {
sub = strings.ToUpper(sub)
sm := &Sessions{
sessions: make([]*Session, 0),
}
for _, s := range sessionsModel.sessions {
if strings.Contains(strings.ToUpper(telToName(s.Tel)), sub) {
sm.sessions = append(sm.sessions, s)
sm.Len++
}
}
engine.Context().SetVar("sessionsModel", sm)
}
//Mirror the Ubuntu.Content QML library constants
//type ContentType int
const (
ContentTypeMessage int = iota
ContentTypeDocuments
ContentTypePictures
ContentTypeMusic
ContentTypeContacts
ContentTypeVideos
ContentTypeLinks
)
func mimeTypeToContentType(mt string) int {
ct := ContentTypeMessage
if strings.HasPrefix(mt, "image") {
ct = ContentTypePictures
}
if strings.HasPrefix(mt, "video") {
ct = ContentTypeVideos
}
if strings.HasPrefix(mt, "audio") {
ct = ContentTypeMusic
}
return ct
}
// contentType gets the content type (message, picture, video) of an attachment by sniffing its MIME type
func contentType(att io.Reader, mt string) int {
if att == nil {
return ContentTypeMessage
}
if mt == "" {
mt, _ = textsecure.MIMETypeFromReader(att)
}
return mimeTypeToContentType(mt)
}
func (s *Session) Add(text string, source string, file string, mimetype string, outgoing bool) *Message {
ctype := ContentTypeMessage
if file != "" {
f, _ := os.Open(file)
ctype = contentType(f, mimetype)
}
message := &Message{Message: text,
SID: s.ID,
Outgoing: outgoing,
Source: source,
CType: ctype,
Attachment: file,
HTime: "Now",
SentAt: uint64(time.Now().UnixNano() / 1000000),
}
s.messages = append(s.messages, message)
s.Last = text
s.Len++
s.CType = ctype
qml.Changed(s, &s.Last)
qml.Changed(s, &s.Len)
qml.Changed(s, &s.CType)
if !outgoing && api.ActiveSessionID != s.Tel {
s.Unread++
qml.Changed(s, &s.Unread)
}
updateSession(s)
s.moveToTop()
return message
}
var topSession string
// moveToTop makes sure the most recently updated session gets to the top of the session list
// it is hacky due to the way models in Go-QML cannot be mutated in a straightforward way
func (s *Session) moveToTop() {
if topSession == s.Tel {
return
}
index := sessionsModel.GetIndex(s.Tel)
sessionsModel.sessions = append([]*Session{s}, append(sessionsModel.sessions[:index], sessionsModel.sessions[index+1:]...)...)
// force a length change update
sessionsModel.Len--
qml.Changed(sessionsModel, &sessionsModel.Len)
sessionsModel.Len++
qml.Changed(sessionsModel, &sessionsModel.Len)
topSession = s.Tel
}
// updateTimestamps keeps the timestamps of the last message of each session
// updated in human readable form.
// FIXME: make this lazier, to only update timestamps the user sees at the moment
func updateTimestamps() {
for {
time.Sleep(1 * time.Minute)
for _, s := range sessionsModel.sessions {
if s.Len == 0 {
continue
}
for _, m := range s.messages {
m.HTime = humanizeTimestamp(m.SentAt)
qml.Changed(m, &m.HTime)
}
s.When = s.messages[len(s.messages)-1].HTime
qml.Changed(s, &s.When)
}
}
}
// initModels exports the Go models to QML
func initModels() {
var err error
settingsModel, err = loadSettings()
if err != nil {
log.Println(err)
}
engine.Context().SetVar("contactsModel", contactsModel)
engine.Context().SetVar("settingsModel", settingsModel)
engine.Context().SetVar("sessionsModel", sessionsModel)
go updateTimestamps()
}