/* Copyright (c) 2016 Michael Bohlender Copyright (c) 2016 Christian Mollekopf This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "folderlistmodel.h" #include #include using namespace Sink; using namespace Sink::ApplicationDomain; FolderListModel::FolderListModel(QObject *parent) : QSortFilterProxyModel() { setDynamicSortFilter(true); sort(0, Qt::AscendingOrder); Query query; query.setFlags(Sink::Query::LiveQuery); query.request().request().request(); query.requestTree(); runQuery(query); } FolderListModel::~FolderListModel() { } QHash< int, QByteArray > FolderListModel::roleNames() const { QHash roles; roles[Name] = "name"; roles[Icon] = "icon"; roles[Id] = "id"; roles[DomainObject] = "domainObject"; return roles; } QVariant FolderListModel::data(const QModelIndex &idx, int role) const { auto srcIdx = mapToSource(idx); switch (role) { case Name: return srcIdx.sibling(srcIdx.row(), 0).data(Qt::DisplayRole).toString(); case Icon: return srcIdx.sibling(srcIdx.row(), 1).data(Qt::DisplayRole).toString(); case Id: return srcIdx.data(Store::DomainObjectBaseRole).value()->identifier(); case DomainObject: return srcIdx.data(Store::DomainObjectRole); } return QSortFilterProxyModel::data(idx, role); } void FolderListModel::runQuery(const Query &query) { mModel = Store::loadModel(query); setSourceModel(mModel.data()); } void FolderListModel::setAccountId(const QVariant &accountId) { const auto account = accountId.toString().toUtf8(); //Get all folders of an account auto query = Query(); query.resourceFilter(account); query.setFlags(Sink::Query::LiveQuery); query.requestTree(); query.request() .request() .request(); query.requestTree(); runQuery(query); } static int getPriority(const Sink::ApplicationDomain::Folder &folder) { auto specialPurpose = folder.getSpecialPurpose(); if (specialPurpose.contains(Sink::ApplicationDomain::SpecialPurpose::Mail::inbox)) { return 10; } if (!specialPurpose.isEmpty()) { return 9; } return 0; } bool FolderListModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { const auto leftFolder = left.data(Sink::Store::DomainObjectRole).value(); const auto rightFolder = right.data(Sink::Store::DomainObjectRole).value(); if (getPriority(*leftFolder) < getPriority(*rightFolder)) { return true; } return leftFolder->getName() < rightFolder->getName(); } QVariant FolderListModel::accountId() const { return QVariant(); }