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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 149 150 151 152
|
/*
* This file is part of buteo-syncfw package
*
* Copyright (C) 2020-2021 Damien Caliste.
*
* Contact: Damien Caliste <dcaliste@free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "multisyncresultmodel.h"
#include <QDebug>
using namespace Buteo;
MultiSyncResultModel::MultiSyncResultModel(QObject *parent)
: SyncResultModelBase(parent)
, mSortOption(MultiSyncResultModel::ByDate)
{
const QList<SyncProfile*> profiles = mManager.allSyncProfiles();
for (SyncProfile *profile : profiles) {
QSharedPointer<SyncProfile> ptr(profile);
addProfileResults(ptr);
addProfileToFilter(*ptr);
}
sort();
sortFilterList();
connect(mSyncClient.data(), &SyncClientInterface::profileChanged,
this, &MultiSyncResultModel::onProfileChanged);
}
MultiSyncResultModel::~MultiSyncResultModel()
{
}
void MultiSyncResultModel::addProfileToFilter(const SyncProfile &profile)
{
if (!profile.isEnabled()) {
return;
}
const Profile *client = profile.clientProfile();
mFilterList << ProfileEntry{profile.name(),
profile.displayname(), client ? client->name() : QString()};
}
QVariantList MultiSyncResultModel::filterList() const
{
QVariantList list;
for (const ProfileEntry &entry : mFilterList) {
list << QVariant::fromValue(entry);
}
return list;
}
void MultiSyncResultModel::sortFilterList()
{
std::sort(mFilterList.begin(), mFilterList.end());
}
QString MultiSyncResultModel::filter() const
{
return mProfileName;
}
void MultiSyncResultModel::setFilter(const QString &filter)
{
if (filter == mProfileName) {
return;
}
mProfileName = filter;
emit filterChanged();
beginResetModel();
mResults.clear();
const QList<SyncProfile*> profiles = mManager.allSyncProfiles();
for (SyncProfile *profile : profiles) {
QSharedPointer<SyncProfile> ptr(profile);
addProfileResults(ptr);
}
sort();
endResetModel();
}
void MultiSyncResultModel::sort()
{
switch (mSortOption) {
case MultiSyncResultModel::ByDate:
SyncResultModelBase::sort();
break;
case MultiSyncResultModel::ByAccount:
std::sort(mResults.begin(), mResults.end(),
[](const SyncResultEntry &a, const SyncResultEntry &b) {
return a.profile->key("accountid") < b.profile->key("accountid");
});
break;
}
}
MultiSyncResultModel::SortOptions MultiSyncResultModel::sorting() const
{
return mSortOption;
}
void MultiSyncResultModel::setSorting(MultiSyncResultModel::SortOptions option)
{
if (option == mSortOption) {
return;
}
mSortOption = option;
emit sortingChanged();
beginResetModel();
sort();
endResetModel();
}
void MultiSyncResultModel::onProfileChanged(QString aProfileName, int aChangeType , QString aProfileAsXml)
{
Q_UNUSED(aProfileAsXml);
Q_UNUSED(aChangeType);
if (!mProfileName.isEmpty() && aProfileName != mProfileName) {
return;
}
mFilterList.erase(std::remove_if(mFilterList.begin(), mFilterList.end(),
[aProfileName] (const ProfileEntry &entry)
{return entry.id == aProfileName;}),
mFilterList.end());
SyncProfile* profile = mManager.syncProfile(aProfileName);
if (profile) {
addProfileToFilter(*profile);
sortFilterList();
}
delete profile;
emit filterListChanged();
}
|