summaryrefslogtreecommitdiff
path: root/forummodel.h
blob: 33a718eec7b0e55ea1c698397e271b3b2a18967e (plain)
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
#ifndef FORUMMODEL_H
#define FORUMMODEL_H

#include <QtCore/QAbstractListModel>
#include <QtCore/QDateTime>
#include <QtSql/QSqlQuery>

class Board;

class ForumModel : public QAbstractListModel
{
	Q_OBJECT
	Q_PROPERTY(Board * board READ board WRITE setBoard NOTIFY boardChanged)
	Q_PROPERTY(int forumId READ forumId WRITE setForumId NOTIFY forumIdChanged)

public:
	ForumModel(QObject *parent = 0);

	enum DataRoles {
		TitleRole = Qt::DisplayRole,
		IconRole = Qt::DecorationRole,

		TopicIdRole = Qt::UserRole,
		TopicTypeRole,
		NumRepliesRole,
		UnreadRole
	};

	Board * board() const;
	void setBoard(Board *board);

	int forumId() const;
	void setForumId(const int id);

	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	QVariant data(const QModelIndex &index, int role) const;

	bool canFetchMore(const QModelIndex &parent = QModelIndex()) const;
	void fetchMore(const QModelIndex &parent = QModelIndex());

public slots:
	void refresh();

signals:
	void boardChanged();
	void forumIdChanged();

protected:
	struct Topic {
		int topic_id;
		QString title;
		int num_replies;
		bool unread;
		QDateTime last_reply_time;
		QDateTime last_update_time;
	};

private:
	static QDateTime parseDateTime(const QVariant& v);
	static QDateTime oldestPostUpdate(const QList<Topic>& topics);
	QDateTime lastTopPostUpdate();
	QList<Topic> loadTopics(int start, int end);
	void clearModel();

private slots:
	void handleForumTopicsChanged(int forumId, int start, int end);
	void handleForumTopicChanged(int forumId, int topicId);
	void update();
	void reload();

private:
	Board *_board;
	int _forumId;
	QList<Topic> _data;
	bool _eof;
};


#endif // FORUMMODEL_H