File indexing completed on 2024-12-22 05:01:08

0001 /*
0002     SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003     SPDX-License-Identifier: GPL-2.0-only
0004 */
0005 #include "historyclosedreadermanager.h"
0006 
0007 HistoryClosedReaderManager::HistoryClosedReaderManager(QObject *parent)
0008     : QObject{parent}
0009 {
0010 }
0011 
0012 HistoryClosedReaderManager::~HistoryClosedReaderManager() = default;
0013 
0014 HistoryClosedReaderManager *HistoryClosedReaderManager::self()
0015 {
0016     static HistoryClosedReaderManager s_self;
0017     return &s_self;
0018 }
0019 
0020 bool HistoryClosedReaderManager::isEmpty() const
0021 {
0022     return mClosedReaderInfos.isEmpty();
0023 }
0024 
0025 void HistoryClosedReaderManager::addInfo(const HistoryClosedReaderInfo &info)
0026 {
0027     if (info.isValid()) {
0028         if (mClosedReaderInfos.count() >= 10) {
0029             mClosedReaderInfos.takeFirst();
0030         }
0031         mClosedReaderInfos.append(info);
0032         Q_EMIT historyClosedReaderChanged();
0033     }
0034 }
0035 
0036 HistoryClosedReaderInfo HistoryClosedReaderManager::lastInfo()
0037 {
0038     if (mClosedReaderInfos.isEmpty()) {
0039         return {};
0040     }
0041     const auto lastElement = mClosedReaderInfos.takeLast();
0042     if (mClosedReaderInfos.isEmpty()) {
0043         Q_EMIT historyClosedReaderChanged();
0044     }
0045     return lastElement;
0046 }
0047 
0048 void HistoryClosedReaderManager::clear()
0049 {
0050     if (mClosedReaderInfos.isEmpty()) {
0051         return;
0052     }
0053     mClosedReaderInfos.clear();
0054     Q_EMIT historyClosedReaderChanged();
0055 }
0056 
0057 void HistoryClosedReaderManager::removeItem(Akonadi::Item::Id id)
0058 {
0059     if (mClosedReaderInfos.isEmpty()) {
0060         return;
0061     }
0062     auto infoIt = std::find_if(mClosedReaderInfos.cbegin(), mClosedReaderInfos.cend(), [&id](const HistoryClosedReaderInfo &info) {
0063         return info.item() == id;
0064     });
0065     if (infoIt != mClosedReaderInfos.cend()) {
0066         mClosedReaderInfos.removeAll(*infoIt);
0067         Q_EMIT historyClosedReaderChanged();
0068     }
0069 }
0070 
0071 int HistoryClosedReaderManager::count() const
0072 {
0073     return mClosedReaderInfos.count();
0074 }
0075 
0076 QList<HistoryClosedReaderInfo> HistoryClosedReaderManager::closedReaderInfos() const
0077 {
0078     return mClosedReaderInfos;
0079 }
0080 
0081 #include "moc_historyclosedreadermanager.cpp"