diff options
author | Javier S. Pedro <maemo@javispedro.com> | 2013-04-01 15:04:58 +0200 |
---|---|---|
committer | Javier S. Pedro <maemo@javispedro.com> | 2013-04-01 15:04:58 +0200 |
commit | 5ef8b38e55c1883224fe1f01f47aba45b7b42666 (patch) | |
tree | 67a873c6a7c5263d202793314c3b3a61543fbb40 /boardmodel.cpp | |
download | tapasboard-5ef8b38e55c1883224fe1f01f47aba45b7b42666.tar.gz tapasboard-5ef8b38e55c1883224fe1f01f47aba45b7b42666.zip |
initial import
Diffstat (limited to 'boardmodel.cpp')
-rw-r--r-- | boardmodel.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/boardmodel.cpp b/boardmodel.cpp new file mode 100644 index 0000000..77778ad --- /dev/null +++ b/boardmodel.cpp @@ -0,0 +1,141 @@ +#include <QtCore/QDebug> +#include <QtSql/QSqlError> + +#include "global.h" +#include "board.h" +#include "boardmodel.h" + +BoardModel::BoardModel(QObject *parent) : + QAbstractListModel(parent), + _rootForumId(0) +{ + QHash<int, QByteArray> roles = roleNames(); + roles[NameRole] = QByteArray("title"); + roles[LogoRole] = QByteArray("logo"); + roles[DescriptionRole] = QByteArray("subtitle"); + roles[ForumIdRole] = QByteArray("forumId"); + roles[ParentIdRole] = QByteArray("parentId"); + roles[CategoryRole] = QByteArray("category"); + setRoleNames(roles); +} + +QString BoardModel::boardUrl() const +{ + return _boardUrl; +} + +void BoardModel::setBoardUrl(const QString &url) +{ + if (_boardUrl != url) { + disconnect(this, SLOT(reload())); + _boardUrl = url; + reload(); + emit boardUrlChanged(); + } +} + +int BoardModel::rootForumId() const +{ + return _rootForumId; +} + +void BoardModel::setRootForumId(const int id) +{ + if (_rootForumId != id) { + _rootForumId = id; + reload(); + emit rootForumIdChanged(); + } +} + +int BoardModel::rowCount(const QModelIndex &parent) const +{ + return parent.isValid() ? 0 : _records; +} + +QVariant BoardModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) return QVariant(); + if (!_query.seek(index.row())) { + qWarning() << "Could not seek to" << index.row(); + return QVariant(); + } + + switch (role) { + case NameRole: + return _query.value(1); + break; + case DescriptionRole: + return _query.value(4); + break; + case ForumIdRole: + return _query.value(0); + break; + case CategoryRole: + return _query.value(5); + break; + } + + return QVariant(); +} + +bool BoardModel::canFetchMore(const QModelIndex &parent) const +{ + return parent.isValid() || !_query.isValid() ? false : !_eof; +} + +void BoardModel::fetchMore(const QModelIndex &parent) +{ + if (parent.isValid()) return; + if (_eof) return; + + const int prefetch_count = 20; + int new_num_records; + if (_query.seek(_records + prefetch_count)) { + new_num_records = _query.at() + 1; + } else if (_query.previous()) { + // We hit the last record and just went back + new_num_records = _query.at() + 1; + _eof = true; + } else { + // There are no records or other error + new_num_records = 0; + _eof = true; + } + + + if (new_num_records <= 0) { + return; // No records! + } else if (new_num_records > _records) { + beginInsertRows(QModelIndex(), _records, new_num_records - 1); + _records = new_num_records; + endInsertRows(); + } +} + +void BoardModel::reload() +{ + beginResetModel(); + _eof = false; + _records = 0; + _query.clear(); + + if (!_boardUrl.isEmpty()) { + Board *board = board_manager->getBoard(_boardUrl); + connect(board, SIGNAL(forumsChanged()), SLOT(reload())); + _query = QSqlQuery(board->database()); + _query.prepare("SELECT f1.forum_id,f1.forum_name,f1.parent_id,f1.logo_url,f1.description,f2.forum_name AS cat_name FROM forums f1 " + "LEFT JOIN forums f2 ON f2.forum_id = f1.parent_id " + "WHERE f1.sub_only=0 AND (f1.parent_id=:parent_id_1 OR f1.parent_id IN " + " (SELECT forum_id from forums WHERE parent_id=:parent_id_2 AND sub_only=1)) " + "ORDER by f1.sort_index ASC;"); + _query.bindValue(0, _rootForumId); + _query.bindValue(1, _rootForumId); + if (!_query.exec()) { + qWarning() << "Coult not select forums: " << _query.lastError().text(); + } + } + + endResetModel(); + fetchMore(); +} |