Commit 7fb9d633 authored by Black Hat's avatar Black Hat

Fix roomAt cross thread operation.

parent d480c58d
...@@ -32,6 +32,10 @@ Page { ...@@ -32,6 +32,10 @@ Page {
Layout.maximumWidth: 360 Layout.maximumWidth: 360
listModel: roomListModel listModel: roomListModel
onCurrentIndexChanged: {
roomForm.setRoom(roomListModel.roomAt(currentIndex));
}
} }
RoomForm { RoomForm {
...@@ -40,7 +44,8 @@ Page { ...@@ -40,7 +44,8 @@ Page {
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true Layout.fillHeight: true
currentRoom: roomListForm.currentItem
// currentRoom: roomListForm.currentIndex != -1 ? roomListModel.roomAt(roomListForm.currentIndex) : null
} }
} }
} }
...@@ -10,7 +10,7 @@ import "qrc:/qml/component" ...@@ -10,7 +10,7 @@ import "qrc:/qml/component"
Item { Item {
property alias listModel: delegateModel.model property alias listModel: delegateModel.model
property alias currentItem: listView.currentItem property alias currentIndex: listView.currentIndex
readonly property bool mini: width <= 80 // Used as an indicator of whether the listform should be displayed as "Mini mode". readonly property bool mini: width <= 80 // Used as an indicator of whether the listform should be displayed as "Mini mode".
DelegateModel { DelegateModel {
...@@ -23,8 +23,6 @@ Item { ...@@ -23,8 +23,6 @@ Item {
filterOnGroup: "filterGroup" filterOnGroup: "filterGroup"
delegate: ItemDelegate { delegate: ItemDelegate {
property variant myData: model
width: parent.width width: parent.width
height: 80 height: 80
onClicked: listView.currentIndex = index onClicked: listView.currentIndex = index
...@@ -37,7 +35,7 @@ Item { ...@@ -37,7 +35,7 @@ Item {
ImageStatus { ImageStatus {
width: parent.height width: parent.height
height: parent.height height: parent.height
source: model.avatar == null || model.avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + model.avatar source: avatar == null || avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + avatar
opaqueBackground: true opaqueBackground: true
} }
...@@ -48,13 +46,13 @@ Item { ...@@ -48,13 +46,13 @@ Item {
Text { Text {
width: parent.width width: parent.width
text: { text: {
if (model.name != "") { if (name != "") {
return model.name; return name;
} }
if (model.alias != "") { if (alias != "") {
return model.alias; return alias;
} }
return model.id return id
} }
color: "#424242" color: "#424242"
font.pointSize: 16 font.pointSize: 16
...@@ -63,7 +61,7 @@ Item { ...@@ -63,7 +61,7 @@ Item {
} }
Text { Text {
width: parent.width width: parent.width
text: model.topic === "" ? "No topic yet." : model.topic text: topic === "" ? "No topic yet." : topic
color: "#424242" color: "#424242"
elide: Text.ElideRight elide: Text.ElideRight
wrapMode: Text.NoWrap wrapMode: Text.NoWrap
......
...@@ -6,7 +6,12 @@ import QtGraphicalEffects 1.0 ...@@ -6,7 +6,12 @@ import QtGraphicalEffects 1.0
import "qrc:/qml/component" import "qrc:/qml/component"
Item { Item {
property Item currentRoom: null property var currentRoom
function setRoom(room) {
console.log("RoomView setting room: "+ room.id);
currentRoom = room;
}
Pane { Pane {
anchors.fill: parent anchors.fill: parent
...@@ -52,7 +57,7 @@ Item { ...@@ -52,7 +57,7 @@ Item {
ImageStatus { ImageStatus {
Layout.preferredWidth: parent.height Layout.preferredWidth: parent.height
Layout.fillHeight: true Layout.fillHeight: true
source: currentRoom == null || currentRoom.myData.avatar == null || currentRoom.myData.avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + currentRoom.myData.avatar source: currentRoom == null || currentRoom.avatar == null || currentRoom.avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + currentRoom.avatar
} }
ColumnLayout { ColumnLayout {
...@@ -61,7 +66,7 @@ Item { ...@@ -61,7 +66,7 @@ Item {
Text { Text {
Layout.fillWidth: true Layout.fillWidth: true
text: currentRoom != null ? currentRoom.myData.name : "" text: currentRoom != null ? currentRoom.name : ""
font.pointSize: 18 font.pointSize: 18
color: "#424242" color: "#424242"
elide: Text.ElideRight elide: Text.ElideRight
...@@ -69,7 +74,7 @@ Item { ...@@ -69,7 +74,7 @@ Item {
} }
Text { Text {
Layout.fillWidth: true Layout.fillWidth: true
text: currentRoom != null ? currentRoom.myData.topic : "" text: currentRoom != null ? currentRoom.topic : ""
color: "#424242" color: "#424242"
elide: Text.ElideRight elide: Text.ElideRight
wrapMode: Text.NoWrap wrapMode: Text.NoWrap
......
package ui package ui
import ( import (
"log"
"strings"
"github.com/therecipe/qt/core" "github.com/therecipe/qt/core"
"matrique/matrix"
) )
type Room struct { type Room struct {
core.QObject core.QObject
controller *Controller matrix.Room
_ string `property:"ID"` _ string `property:"id"`
_ string `property:"alias"` _ string `property:"alias"`
_ string `property:"name"` _ string `property:"name"`
_ string `property:"topic"` _ string `property:"topic"`
_ string `property:"avatar"` _ string `property:"avatar"`
} }
func (r *Room) Init(controller *Controller) { func (r *Room) Init() {
r.SetController(controller) r.ConnectId(func() string { return r.Room.ID })
r.GetAlias() r.ConnectSetId(func(id string) { r.Room.ID = id })
r.GetName() r.ConnectAlias(func() string { return r.Room.Alias })
r.GetTopic() r.ConnectSetAlias(func(alias string) { r.Room.Alias = alias })
r.GetAvatar() r.ConnectName(func() string { return r.Room.Name })
} r.ConnectSetName(func(name string) { r.Room.Name = name })
r.ConnectTopic(func() string { return r.Room.Topic })
func (r *Room) SetController(controller *Controller) { r.ConnectSetTopic(func(topic string) { r.Room.Topic = topic })
r.controller = controller r.ConnectAvatar(func() string { return r.Room.Avatar })
} r.ConnectSetAvatar(func(avatar string) { r.Room.Avatar = avatar })
func (r *Room) GetAlias() {
type RespRoomAlias struct {
Alias string `json:"alias"`
}
resp := new(RespRoomAlias)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.canonical_alias", "", resp)
if err == nil && resp.Alias != "" {
r.SetAlias(resp.Alias)
return
}
}
func (r *Room) GetName() {
type RespRoomName struct {
Name string `json:"name"`
}
resp := new(RespRoomName)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.name", "", resp)
if err == nil && resp.Name != "" {
r.SetName(resp.Name)
return
}
respM, _ := r.controller.matrixCli.JoinedMembers(r.ID())
memberSlice := respM.Joined
if len(memberSlice) == 2 {
for eachID, eachMember := range memberSlice {
if eachID == r.controller.matrixCli.UserID {
continue
}
if eachMember.DisplayName != nil {
r.SetName(*eachMember.DisplayName)
return
}
break
}
}
}
func (r *Room) GetTopic() {
type RespRoomTopic struct {
Topic string `json:"topic"`
}
resp := new(RespRoomTopic)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.topic", "", resp)
if err != nil {
log.Println("Error when fetching topic of room", r.ID(), ":", err.Error())
return
}
r.SetTopic(resp.Topic)
} }
func (r *Room) GetAvatar() { func (r *Room) FromRoom(room *matrix.Room) {
type RespRoomAvatar struct { r.SetId(room.ID)
Avatar string `json:"url"` r.SetAlias(room.Alias)
} r.SetName(room.Name)
resp := new(RespRoomAvatar) r.SetTopic(room.Topic)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.avatar", "", resp) r.SetAvatar(room.Avatar)
if err == nil && resp.Avatar != "" {
r.SetAvatar(resp.Avatar)
return
}
respM, _ := r.controller.matrixCli.JoinedMembers(r.ID())
memberSlice := respM.Joined
if len(memberSlice) == 2 {
for eachID, eachMember := range memberSlice {
if eachID == r.controller.matrixCli.UserID {
continue
}
if eachMember.AvatarURL != nil {
r.SetAvatar(*eachMember.AvatarURL)
return
}
break
}
}
r.SetAvatar(strings.ToUpper(r.Name()))
} }
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"log" "log"
"github.com/therecipe/qt/core" "github.com/therecipe/qt/core"
"matrique/matrix"
) )
type RoomListModel struct { type RoomListModel struct {
...@@ -16,7 +17,7 @@ type RoomListModel struct { ...@@ -16,7 +17,7 @@ type RoomListModel struct {
_ map[int]*core.QByteArray `property:"roles"` _ map[int]*core.QByteArray `property:"roles"`
_ []*Room `property:"room"` _ []*Room `property:"room"`
_ func(*[]*Room) `signal:"doResetModel"` _ func(interface{}) `signal:"doResetModel"`
_ func(int) *Room `slot:"roomAt"` _ func(int) *Room `slot:"roomAt"`
_ func(*Room) `slot:"addRoom"` _ func(*Room) `slot:"addRoom"`
...@@ -24,7 +25,6 @@ type RoomListModel struct { ...@@ -24,7 +25,6 @@ type RoomListModel struct {
} }
func init() { func init() {
Room_QRegisterMetaType()
} }
func (m *RoomListModel) init() { func (m *RoomListModel) init() {
...@@ -62,32 +62,42 @@ func (m *RoomListModel) fetchModel() { ...@@ -62,32 +62,42 @@ func (m *RoomListModel) fetchModel() {
resp, err := m.Controller().matrixCli.JoinedRooms() resp, err := m.Controller().matrixCli.JoinedRooms()
checkErr(err) checkErr(err)
roomChan := make(chan *Room) roomChan := make(chan *matrix.Room)
defer close(roomChan) defer close(roomChan)
for _, roomID := range resp.JoinedRooms { for _, roomID := range resp.JoinedRooms {
go func(id string) { go func(id string) {
log.Println("ID:", id)
defer m.Controller().recovery() defer m.Controller().recovery()
room := NewRoom(nil) room := matrix.Room{ID: id}
room.SetID(id) room.Init(m.Controller().matrixCli)
room.Init(m.Controller()) log.Println(room)
roomChan <- room roomChan <- &room
}(roomID) }(roomID)
} }
var rooms []*Room var rooms []*matrix.Room
for i := 0; i < len(resp.JoinedRooms); i++ { for i := 0; i < len(resp.JoinedRooms); i++ {
rooms = append(rooms, <-roomChan) rooms = append(rooms, <-roomChan)
} }
m.Controller().SetBusy(false) m.Controller().SetBusy(false)
m.DoResetModel(&rooms) log.Println("BEFORE:", rooms)
m.DoResetModel(rooms)
} }
func (m *RoomListModel) resetModel(rooms *[]*Room) { func (m *RoomListModel) resetModel(roomsInterface interface{}) {
rooms := roomsInterface.([]*matrix.Room)
log.Println("Begin resetting roomlistmodel.") log.Println("Begin resetting roomlistmodel.")
m.BeginResetModel() m.BeginResetModel()
m.SetRoom(*rooms)
var qmlRooms []*Room
for _, room := range rooms {
qmlRoom := NewRoom(nil)
qmlRoom.FromRoom(room)
qmlRooms = append(qmlRooms, qmlRoom)
}
m.SetRoom(qmlRooms)
m.EndResetModel() m.EndResetModel()
log.Println("Reset complete.") log.Println("Reset complete.")
} }
...@@ -109,7 +119,7 @@ func (m *RoomListModel) data(index *core.QModelIndex, role int) *core.QVariant { ...@@ -109,7 +119,7 @@ func (m *RoomListModel) data(index *core.QModelIndex, role int) *core.QVariant {
switch role { switch role {
case ID: case ID:
return core.NewQVariant14(r.ID()) return core.NewQVariant14(r.Id())
case Name: case Name:
return core.NewQVariant14(r.Name()) return core.NewQVariant14(r.Name())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment