telephant/messagemodel.go

266 lines
5.5 KiB
Go
Raw Normal View History

2017-08-29 05:09:17 +00:00
package main
import (
"time"
humanize "github.com/dustin/go-humanize"
"github.com/therecipe/qt/core"
)
// maxMessageCount defines the max amount of messages stored in a model
const (
maxMessageCount = 500
)
// Model Roles
const (
Name = int(core.Qt__UserRole) + 1<<iota
MessageID
PostURL
2017-08-29 05:09:17 +00:00
Author
AuthorURL
2017-08-29 05:09:17 +00:00
Avatar
Body
CreatedAt
Actor
ActorName
Reply
ReplyToID
ReplyToAuthor
Forward
Mention
Like
Media
2017-08-29 05:09:17 +00:00
Editing
)
// MessageModel holds a collection of messages
type MessageModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*Message `property:"messages"`
_ func(*Message) `slot:"addMessage"`
_ func(row int) `slot:"removeMessage"`
}
// Message represents a single message
type Message struct {
core.QObject
Name string
2019-05-01 15:15:56 +00:00
MessageID string
PostURL string
2017-08-29 05:09:17 +00:00
Author string
AuthorURL string
2017-08-29 05:09:17 +00:00
Avatar string
Body string
CreatedAt time.Time
Actor string
ActorName string
Reply bool
2019-05-01 15:15:56 +00:00
ReplyToID string
2017-08-29 05:09:17 +00:00
ReplyToAuthor string
Forward bool
Mention bool
Like bool
Media string
2017-08-29 05:09:17 +00:00
Editing bool
}
func (m *MessageModel) init() {
m.SetRoles(map[int]*core.QByteArray{
Name: core.NewQByteArray2("name", -1),
MessageID: core.NewQByteArray2("messageid", -1),
PostURL: core.NewQByteArray2("posturl", -1),
2017-08-29 05:09:17 +00:00
Author: core.NewQByteArray2("author", -1),
AuthorURL: core.NewQByteArray2("authorurl", -1),
2017-08-29 05:09:17 +00:00
Avatar: core.NewQByteArray2("avatar", -1),
Body: core.NewQByteArray2("body", -1),
CreatedAt: core.NewQByteArray2("createdat", -1),
Actor: core.NewQByteArray2("actor", -1),
ActorName: core.NewQByteArray2("actorname", -1),
Reply: core.NewQByteArray2("reply", -1),
ReplyToID: core.NewQByteArray2("replytoid", -1),
ReplyToAuthor: core.NewQByteArray2("replytoauthor", -1),
Forward: core.NewQByteArray2("forward", -1),
Mention: core.NewQByteArray2("mention", -1),
Like: core.NewQByteArray2("like", -1),
Media: core.NewQByteArray2("media", -1),
2017-08-29 05:09:17 +00:00
Editing: core.NewQByteArray2("editing", -1),
})
m.ConnectData(m.data)
m.ConnectSetData(m.setData)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectRoleNames(m.roleNames)
m.ConnectAddMessage(m.addMessage)
m.ConnectRemoveMessage(m.removeMessage)
// keep time stamps ("1 minute ago") updated
go func() {
for {
time.Sleep(1 * time.Minute)
m.updateMessageTime()
}
}()
}
func (m *MessageModel) setData(index *core.QModelIndex, value *core.QVariant, role int) bool {
if !index.IsValid() {
return false
}
var p = m.Messages()[len(m.Messages())-1-index.Row()]
p.Editing = true
m.DataChanged(index, index, []int{Editing})
return true
}
func (m *MessageModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(m.Messages()) {
return core.NewQVariant()
}
var p = m.Messages()[len(m.Messages())-1-index.Row()]
switch role {
case Name:
{
return core.NewQVariant14(p.Name)
}
case MessageID:
{
2019-05-01 15:15:56 +00:00
return core.NewQVariant14(p.MessageID)
2017-08-29 05:09:17 +00:00
}
case PostURL:
{
return core.NewQVariant14(p.PostURL)
}
2017-08-29 05:09:17 +00:00
case Author:
{
return core.NewQVariant14(p.Author)
}
case AuthorURL:
{
return core.NewQVariant14(p.AuthorURL)
}
2017-08-29 05:09:17 +00:00
case Avatar:
{
return core.NewQVariant14(p.Avatar)
}
case Body:
{
return core.NewQVariant14(p.Body)
}
case CreatedAt:
{
return core.NewQVariant14(humanize.Time(p.CreatedAt))
}
case Actor:
{
return core.NewQVariant14(p.Actor)
}
case ActorName:
{
return core.NewQVariant14(p.ActorName)
}
case Reply:
{
return core.NewQVariant11(p.Reply)
}
case ReplyToID:
{
2019-05-01 15:15:56 +00:00
return core.NewQVariant14(p.ReplyToID)
2017-08-29 05:09:17 +00:00
}
case ReplyToAuthor:
{
return core.NewQVariant14(p.ReplyToAuthor)
}
case Forward:
{
return core.NewQVariant11(p.Forward)
}
case Mention:
{
return core.NewQVariant11(p.Mention)
}
case Like:
{
return core.NewQVariant11(p.Like)
}
case Media:
{
return core.NewQVariant14(p.Media)
}
2017-08-29 05:09:17 +00:00
case Editing:
{
return core.NewQVariant11(p.Editing)
}
default:
{
return core.NewQVariant()
}
}
}
func (m *MessageModel) rowCount(parent *core.QModelIndex) int {
return len(m.Messages())
}
func (m *MessageModel) columnCount(parent *core.QModelIndex) int {
return 1
}
func (m *MessageModel) roleNames() map[int]*core.QByteArray {
return m.Roles()
}
func (m *MessageModel) addMessage(p *Message) {
m.BeginInsertRows(core.NewQModelIndex(), 0, 0)
m.SetMessages(append(m.Messages(), p))
m.EndInsertRows()
if len(m.Messages()) > maxMessageCount {
2017-08-29 07:21:25 +00:00
m.removeMessage(len(m.Messages()) - 1)
2017-08-29 05:09:17 +00:00
}
}
func (m *MessageModel) removeMessage(row int) {
2017-08-29 07:21:25 +00:00
trow := len(m.Messages()) - 1 - row
2017-08-29 05:09:17 +00:00
m.BeginRemoveRows(core.NewQModelIndex(), row, row)
2017-08-29 07:21:25 +00:00
m.SetMessages(append(m.Messages()[:trow], m.Messages()[trow+1:]...))
2017-08-29 05:09:17 +00:00
m.EndRemoveRows()
}
func (m *MessageModel) updateMessageTime() {
if len(m.Messages()) > 0 {
var fIndex = m.Index(0, 0, core.NewQModelIndex())
var lIndex = m.Index(len(m.Messages())-1, 0, core.NewQModelIndex())
m.DataChanged(fIndex, lIndex, []int{CreatedAt})
}
}
/*
func (m *MessageModel) editMessage(row int, param string) {
var p = m.Messages()[row]
var pIndex = m.Index(row, 0, core.NewQModelIndex())
m.DataChanged(pIndex, pIndex, []int{roles})
}
*/
func init() {
Message_QRegisterMetaType()
}