roomListModel.go 3.08 KB
Newer Older
Black Hat's avatar
Black Hat committed
1
package ui
Black Hat's avatar
Black Hat committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

import (
	"github.com/therecipe/qt/core"
	"log"
)

type RoomListModel struct {
	core.QAbstractListModel

	_ *Controller `property:"controller"`

	_ func() `constructor:"init"`

	_ map[int]*core.QByteArray `property:"roles"`
	_ []*Room                  `property:"room"`

	_ func(*[]*Room) `signal:"doResetModel"`

20 21 22
	_ func(int) *Room `slot:"roomAt"`
	_ func(*Room)     `slot:"addRoom"`
	_ func(row int)   `slot:"removeRoom"`
Black Hat's avatar
Black Hat committed
23 24 25 26 27 28 29 30
}

func init() {
	Room_QRegisterMetaType()
}

func (m *RoomListModel) init() {
	m.SetRoles(map[int]*core.QByteArray{
Black Hat's avatar
Black Hat committed
31 32 33
		ID:     core.NewQByteArray2("id", len("id")),
		Name:   core.NewQByteArray2("name", len("name")),
		Topic:  core.NewQByteArray2("topic", len("topic")),
Black Hat's avatar
Black Hat committed
34 35 36 37 38 39 40
		Avatar: core.NewQByteArray2("avatar", len("avatar")),
	})

	m.ConnectData(m.data)
	m.ConnectRowCount(m.rowCount)
	m.ConnectColumnCount(m.columnCount)
	m.ConnectRoleNames(m.roleNames)
41
	m.ConnectRoomAt(m.roomAt)
Black Hat's avatar
Black Hat committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	m.ConnectDoResetModel(m.resetModel)
	m.ConnectAddRoom(m.addRoom)
	m.ConnectRemoveRoom(m.removeRoom)

	m.ConnectControllerChanged(func(controller *Controller) {
		m.Controller().ConnectIsLoginChanged(func(isLogin bool) {
			if isLogin {
				go m.fetchModel()
			}
		})
	})
}

func (m *RoomListModel) fetchModel() {
56 57
	defer m.Controller().recovery()

Black Hat's avatar
Black Hat committed
58 59
	m.Controller().SetBusy(true)

60
	resp, err := m.Controller().matrixCli.JoinedRooms()
61
	checkErr(err)
62

63
	roomChan := make(chan *Room)
64
	defer close(roomChan)
Black Hat's avatar
Black Hat committed
65
	for _, roomID := range resp.JoinedRooms {
66
		go func(id string) {
67 68
			defer m.Controller().recovery()

69 70 71 72 73 74 75 76 77 78
			room := NewRoom(nil)
			room.SetID(id)
			room.Init(m.Controller())
			roomChan <- room
		}(roomID)
	}

	var rooms []*Room
	for i := 0; i < len(resp.JoinedRooms); i++ {
		rooms = append(rooms, <-roomChan)
Black Hat's avatar
Black Hat committed
79
	}
Black Hat's avatar
Black Hat committed
80 81

	m.Controller().SetBusy(false)
Black Hat's avatar
Black Hat committed
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
	m.DoResetModel(&rooms)
}

func (m *RoomListModel) resetModel(rooms *[]*Room) {
	log.Println("Begin resetting roomlistmodel.")
	m.BeginResetModel()
	m.SetRoom(*rooms)
	m.EndResetModel()
	log.Println("Reset complete.")
}

func (m *RoomListModel) roomAt(row int) *Room {
	return m.Room()[row]
}

func (m *RoomListModel) data(index *core.QModelIndex, role int) *core.QVariant {
	if !index.IsValid() {
		return core.NewQVariant()
	}

	if index.Row() >= len(m.Room()) {
		return core.NewQVariant()
	}

	var r = m.Room()[index.Row()]

	switch role {
	case ID:
110
		return core.NewQVariant14(r.ID())
Black Hat's avatar
Black Hat committed
111 112

	case Name:
113
		return core.NewQVariant14(r.Name())
Black Hat's avatar
Black Hat committed
114 115

	case Topic:
116
		return core.NewQVariant14(r.Topic())
Black Hat's avatar
Black Hat committed
117 118

	case Avatar:
119
		return core.NewQVariant14(r.Avatar())
Black Hat's avatar
Black Hat committed
120 121

	default:
122
		return core.NewQVariant()
Black Hat's avatar
Black Hat committed
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
	}
}

func (m *RoomListModel) rowCount(parent *core.QModelIndex) int {
	return len(m.Room())
}

func (m *RoomListModel) columnCount(parent *core.QModelIndex) int {
	return 1
}

func (m *RoomListModel) roleNames() map[int]*core.QByteArray {
	return m.Roles()
}

func (m *RoomListModel) addRoom(r *Room) {
	m.BeginInsertRows(core.NewQModelIndex(), len(m.Room()), len(m.Room()))
	m.SetRoom(append(m.Room(), r))
	m.EndInsertRows()
}

func (m *RoomListModel) removeRoom(row int) {
	m.BeginRemoveRows(core.NewQModelIndex(), row, row)
	m.SetRoom(append(m.Room()[:row], m.Room()[row+1:]...))
	m.EndRemoveRows()
}