deletesubscriptioncommand.cpp 4.73 KB
Newer Older
1 2 3
/*
    This file is part of Akregator.

4
    SPDX-FileCopyrightText: 2008 Frank Osterfeld <osterfeld@kde.org>
5

6
    SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
*/

#include "deletesubscriptioncommand.h"

#include "feed.h"
#include "feedlist.h"
#include "folder.h"
#include "subscriptionlistjobs.h"
#include "treenodevisitor.h"

#include <KLocalizedString>
#include <KMessageBox>

#include <QPointer>
#include <QTimer>

using namespace Akregator;

Laurent Montel's avatar
Laurent Montel committed
25
namespace {
26 27
class DeleteNodeVisitor : public TreeNodeVisitor
{
Laurent Montel's avatar
Laurent Montel committed
28
public:
Laurent Montel's avatar
Laurent Montel committed
29
    explicit DeleteNodeVisitor(QWidget *parent) : m_widget(parent)
Laurent Montel's avatar
Laurent Montel committed
30
        , m_job(nullptr)
Laurent Montel's avatar
Laurent Montel committed
31 32
    {
    }
33

Laurent Montel's avatar
Laurent Montel committed
34 35
    bool visitFolder(Folder *node) override
    {
Laurent Montel's avatar
Laurent Montel committed
36
        const QString msg = node->title().isEmpty()
Laurent Montel's avatar
Laurent Montel committed
37 38
                            ? i18n("<qt>Are you sure you want to delete this folder and its feeds and subfolders?</qt>")
                            : i18n("<qt>Are you sure you want to delete folder <b>%1</b> and its feeds and subfolders?</qt>", node->title());
Laurent Montel's avatar
Laurent Montel committed
39 40

        if (KMessageBox::warningContinueCancel(m_widget,
Laurent Montel's avatar
Laurent Montel committed
41 42 43 44 45
                                               msg,
                                               i18n("Delete Folder"),
                                               KStandardGuiItem::del(),
                                               KStandardGuiItem::cancel(),
                                               QStringLiteral("Disable delete folder confirmation")) != KMessageBox::Continue) {
46 47
            return true;
        }
Laurent Montel's avatar
Laurent Montel committed
48 49 50 51 52
        m_job = reallyCreateJob(node);
        //TODO: port focus
        //m_widget->m_feedListView->setFocus();
        return true;
    }
53

Laurent Montel's avatar
Laurent Montel committed
54 55
    bool visitFeed(Feed *node) override
    {
Laurent Montel's avatar
Laurent Montel committed
56
        QString msg;
Laurent Montel's avatar
Laurent Montel committed
57
        if (node->title().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
58 59 60 61
            msg = i18n("<qt>Are you sure you want to delete this feed?</qt>");
        } else {
            msg = i18n("<qt>Are you sure you want to delete feed <b>%1</b>?</qt>", node->title());
        }
62

Laurent Montel's avatar
Laurent Montel committed
63
        if (KMessageBox::warningContinueCancel(m_widget,
Laurent Montel's avatar
Laurent Montel committed
64 65 66 67 68
                                               msg,
                                               i18n("Delete Feed"),
                                               KStandardGuiItem::del(),
                                               KStandardGuiItem::cancel(),
                                               QStringLiteral("Disable delete feed confirmation")) != KMessageBox::Continue) {
Laurent Montel's avatar
Laurent Montel committed
69
            return true;
70
        }
Laurent Montel's avatar
Laurent Montel committed
71 72 73 74 75
        m_job = reallyCreateJob(node);
        //TODO: port focus
        // m_widget->m_feedListView->setFocus();
        return true;
    }
76

Laurent Montel's avatar
Laurent Montel committed
77 78
    DeleteSubscriptionJob *createJob(TreeNode *node)
    {
Laurent Montel's avatar
Laurent Montel committed
79
        m_job = nullptr;
Laurent Montel's avatar
Laurent Montel committed
80 81
        if (node) {
            visit(node);
82
        }
Laurent Montel's avatar
Laurent Montel committed
83 84
        return m_job;
    }
85

Laurent Montel's avatar
Laurent Montel committed
86 87 88
private:
    static DeleteSubscriptionJob *reallyCreateJob(TreeNode *node)
    {
Laurent Montel's avatar
Laurent Montel committed
89
        auto *job = new DeleteSubscriptionJob;
Laurent Montel's avatar
Laurent Montel committed
90 91 92 93 94 95 96
        job->setSubscriptionId(node->id());
        return job;
    }

private:
    QPointer<QWidget> m_widget;
    QPointer<DeleteSubscriptionJob> m_job;
97 98 99 100 101
};
}

class DeleteSubscriptionCommand::Private
{
Laurent Montel's avatar
Laurent Montel committed
102
    DeleteSubscriptionCommand *const q;
103
public:
Laurent Montel's avatar
Laurent Montel committed
104
    explicit Private(DeleteSubscriptionCommand *qq);
105
    ~Private();
106

107 108
    void startDelete();
    void jobFinished();
109

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
110
    QWeakPointer<FeedList> m_list;
111
    int m_subscriptionId = -1;
112 113
};

Laurent Montel's avatar
Laurent Montel committed
114 115
DeleteSubscriptionCommand::Private::Private(DeleteSubscriptionCommand *qq) : q(qq)
    , m_list()
116 117 118 119 120 121 122
{
}

DeleteSubscriptionCommand::Private::~Private()
{
}

Laurent Montel's avatar
Laurent Montel committed
123 124
DeleteSubscriptionCommand::DeleteSubscriptionCommand(QObject *parent) : Command(parent)
    , d(new Private(this))
125 126 127 128 129 130 131 132
{
}

DeleteSubscriptionCommand::~DeleteSubscriptionCommand()
{
    delete d;
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
133
void DeleteSubscriptionCommand::setSubscription(const QWeakPointer<FeedList> &feedList, int subId)
134 135 136 137 138 139 140 141 142 143
{
    d->m_list = feedList;
    d->m_subscriptionId = subId;
}

int DeleteSubscriptionCommand::subscriptionId() const
{
    return d->m_subscriptionId;
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
144
QWeakPointer<FeedList> DeleteSubscriptionCommand::feedList() const
145 146 147
{
    return d->m_list;
}
148

149 150
void DeleteSubscriptionCommand::doStart()
{
Laurent Montel's avatar
Laurent Montel committed
151 152 153
    QTimer::singleShot(0, this, [this]() {
        d->startDelete();
    });
154 155 156 157 158 159 160 161 162
}

void DeleteSubscriptionCommand::Private::jobFinished()
{
    q->done();
}

void DeleteSubscriptionCommand::Private::startDelete()
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
163
    const QSharedPointer<FeedList> list = m_list.lock();
Laurent Montel's avatar
Laurent Montel committed
164
    if (!list) {
165 166 167
        q->done();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
168 169 170 171
    TreeNode *const node = list->findByID(m_subscriptionId);
    DeleteNodeVisitor visitor(q->parentWidget());
    DeleteSubscriptionJob *job = visitor.createJob(node);
    if (!job) {
172 173 174
        q->done();
        return;
    }
175

Laurent Montel's avatar
Laurent Montel committed
176 177 178
    QObject::connect(job, &DeleteSubscriptionJob::finished, q, [this]() {
        jobFinished();
    });
179 180 181 182 183 184 185
    job->start();
}

void DeleteSubscriptionCommand::doAbort()
{
}

Stephen Kelly's avatar
Stephen Kelly committed
186
#include "moc_deletesubscriptioncommand.cpp"