summaryrefslogtreecommitdiff
path: root/topicmodel.h
blob: 7b07c1b5929834c6c8301df0afaebc6a195c5268 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef TOPICMODEL_H
#define TOPICMODEL_H

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

class Board;

class TopicModel : public QAbstractListModel
{
	Q_OBJECT
	Q_PROPERTY(Board * board READ board WRITE setBoard NOTIFY boardChanged)
	Q_PROPERTY(int topicId READ topicId WRITE setTopicId NOTIFY topicIdChanged)
	Q_PROPERTY(int firstUnreadPost READ firstUnreadPost NOTIFY firstUnreadPostChanged)

public:
	TopicModel(QObject *parent = 0);

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

		PostIdRole = Qt::UserRole,
		UserIdRole,
		UserNameRole,
		DateTimeRole,
		HumanDateRole,
		HumanTimeRole,
		UnreadRole
	};

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

	int topicId() const;
	void setTopicId(const int id);

	int firstUnreadPost() const;

	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();
	void markAsRead();

signals:
	void boardChanged();
	void topicIdChanged();
	void firstUnreadPostChanged();

protected:
	struct Post {
		/** Set 'post_id' to -1 for "not yet fetched" */
		int post_id;
		QString title;
		QString content;
		QUrl icon;
		int user_id;
		QString user_name;
		QDateTime time;
		QDateTime last_update_time;
	};

private:
	static QDateTime parseDbDateTime(const QVariant& v);
	static QDateTime oldestPostUpdate(const QList<Post>& posts);
	QDateTime lastTopPostUpdate();
	QList<Post> loadPosts(int start, int end);
	void fetchPost(int position) const; // const because data() calls this
	void enlargeModel(int end);
	void clearModel();

private slots:
	void handleTopicPostsChanged(int topicId, int start, int end);
	void handleTopicPostsUnread(int topicId, int position);
	void update();
	void reload();

private:
	Board *_board;
	int _topicId;
	QList<Post> _data;
	bool _eof;
	int _firstUnread;
};

#endif // TOPICMODEL_H