File indexing completed on 2024-05-05 05:13:00

0001 /*
0002     This file is part of Akregator.
0003 
0004     SPDX-FileCopyrightText: 2008 Frank Osterfeld <osterfeld@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0007 */
0008 
0009 #include "deletesubscriptioncommand.h"
0010 
0011 #include "feed.h"
0012 #include "feedlist.h"
0013 #include "folder.h"
0014 #include "subscriptionlistjobs.h"
0015 #include "treenodevisitor.h"
0016 
0017 #include <KLocalizedString>
0018 #include <KMessageBox>
0019 
0020 #include <QPointer>
0021 #include <QTimer>
0022 
0023 using namespace Akregator;
0024 
0025 namespace
0026 {
0027 class DeleteNodeVisitor : public TreeNodeVisitor
0028 {
0029 public:
0030     explicit DeleteNodeVisitor(QWidget *parent)
0031         : m_widget(parent)
0032         , m_job(nullptr)
0033     {
0034     }
0035 
0036     bool visitFolder(Folder *node) override
0037     {
0038         const QString msg = node->title().isEmpty()
0039             ? i18n("<qt>Are you sure you want to delete this folder and its feeds and subfolders?</qt>")
0040             : i18n("<qt>Are you sure you want to delete folder <b>%1</b> and its feeds and subfolders?</qt>", node->title());
0041 
0042         if (KMessageBox::warningContinueCancel(m_widget,
0043                                                msg,
0044                                                i18nc("@title:window", "Delete Folder"),
0045                                                KStandardGuiItem::del(),
0046                                                KStandardGuiItem::cancel(),
0047                                                QStringLiteral("Disable delete folder confirmation"))
0048             != KMessageBox::Continue) {
0049             return true;
0050         }
0051         m_job = reallyCreateJob(node);
0052         // TODO: port focus
0053         // m_widget->m_feedListView->setFocus();
0054         return true;
0055     }
0056 
0057     bool visitFeed(Akregator::Feed *node) override
0058     {
0059         QString msg;
0060         if (node->title().isEmpty()) {
0061             msg = i18n("<qt>Are you sure you want to delete this feed?</qt>");
0062         } else {
0063             msg = i18n("<qt>Are you sure you want to delete feed <b>%1</b>?</qt>", node->title());
0064         }
0065 
0066         if (KMessageBox::warningContinueCancel(m_widget,
0067                                                msg,
0068                                                i18nc("@title:window", "Delete Feed"),
0069                                                KStandardGuiItem::del(),
0070                                                KStandardGuiItem::cancel(),
0071                                                QStringLiteral("Disable delete feed confirmation"))
0072             != KMessageBox::Continue) {
0073             return true;
0074         }
0075         m_job = reallyCreateJob(node);
0076         // TODO: port focus
0077         // m_widget->m_feedListView->setFocus();
0078         return true;
0079     }
0080 
0081     DeleteSubscriptionJob *createJob(TreeNode *node)
0082     {
0083         m_job = nullptr;
0084         if (node) {
0085             visit(node);
0086         }
0087         return m_job;
0088     }
0089 
0090 private:
0091     static DeleteSubscriptionJob *reallyCreateJob(TreeNode *node)
0092     {
0093         auto job = new DeleteSubscriptionJob;
0094         job->setSubscriptionId(node->id());
0095         return job;
0096     }
0097 
0098 private:
0099     QPointer<QWidget> m_widget;
0100     QPointer<DeleteSubscriptionJob> m_job;
0101 };
0102 }
0103 
0104 class Akregator::DeleteSubscriptionCommandPrivate
0105 {
0106     DeleteSubscriptionCommand *const q;
0107 
0108 public:
0109     explicit DeleteSubscriptionCommandPrivate(DeleteSubscriptionCommand *qq);
0110     ~DeleteSubscriptionCommandPrivate();
0111 
0112     void startDelete();
0113     void jobFinished();
0114 
0115     QWeakPointer<FeedList> m_list;
0116     uint m_subscriptionId = 0;
0117 };
0118 
0119 DeleteSubscriptionCommandPrivate::DeleteSubscriptionCommandPrivate(DeleteSubscriptionCommand *qq)
0120     : q(qq)
0121     , m_list()
0122 {
0123 }
0124 
0125 DeleteSubscriptionCommandPrivate::~DeleteSubscriptionCommandPrivate() = default;
0126 
0127 DeleteSubscriptionCommand::DeleteSubscriptionCommand(QObject *parent)
0128     : Command(parent)
0129     , d(new DeleteSubscriptionCommandPrivate(this))
0130 {
0131 }
0132 
0133 DeleteSubscriptionCommand::~DeleteSubscriptionCommand() = default;
0134 
0135 void DeleteSubscriptionCommand::setSubscription(const QWeakPointer<FeedList> &feedList, uint subId)
0136 {
0137     d->m_list = feedList;
0138     d->m_subscriptionId = subId;
0139 }
0140 
0141 uint DeleteSubscriptionCommand::subscriptionId() const
0142 {
0143     return d->m_subscriptionId;
0144 }
0145 
0146 QWeakPointer<FeedList> DeleteSubscriptionCommand::feedList() const
0147 {
0148     return d->m_list;
0149 }
0150 
0151 void DeleteSubscriptionCommand::doStart()
0152 {
0153     QTimer::singleShot(0, this, [this]() {
0154         d->startDelete();
0155     });
0156 }
0157 
0158 void DeleteSubscriptionCommandPrivate::jobFinished()
0159 {
0160     q->done();
0161 }
0162 
0163 void DeleteSubscriptionCommandPrivate::startDelete()
0164 {
0165     const QSharedPointer<FeedList> list = m_list.lock();
0166     if (!list) {
0167         q->done();
0168         return;
0169     }
0170     TreeNode *const node = list->findByID(m_subscriptionId);
0171     DeleteNodeVisitor visitor(q->parentWidget());
0172     DeleteSubscriptionJob *job = visitor.createJob(node);
0173     if (!job) {
0174         q->done();
0175         return;
0176     }
0177 
0178     QObject::connect(job, &DeleteSubscriptionJob::finished, q, [this]() {
0179         jobFinished();
0180     });
0181     job->start();
0182 }
0183 
0184 void DeleteSubscriptionCommand::doAbort()
0185 {
0186 }
0187 
0188 #include "moc_deletesubscriptioncommand.cpp"