Akonadi

clearcachefoldersjob.cpp
1/*
2 This file is part of Akonadi
3
4 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8#include "clearcachefoldersjob.h"
9#include "akonadiwidgets_debug.h"
10#include "dbaccess.h"
11#include <KLocalizedString>
12#include <KMessageBox>
13#include <QSqlError>
14#include <QSqlQuery>
15
16using namespace Akonadi;
17ClearCacheFoldersJob::ClearCacheFoldersJob(const Akonadi::Collection &folder, QObject *parent)
18 : ClearCacheFoldersJob(Akonadi::Collection::List{folder}, parent)
19{
20}
21
22ClearCacheFoldersJob::ClearCacheFoldersJob(const Akonadi::Collection::List &folders, QObject *parent)
23 : QObject{parent}
24{
25 mCollections = folders;
26 mNumberJob = folders.length();
27 connect(this, &ClearCacheFoldersJob::clearNextFolder, this, &ClearCacheFoldersJob::slotClearNextFolder);
28}
29
30ClearCacheFoldersJob::~ClearCacheFoldersJob() = default;
31
32void ClearCacheFoldersJob::slotClearNextFolder()
33{
34 if (mCanceled) {
35 Q_EMIT clearCacheDone();
36 Q_EMIT finished(false);
38 return;
39 }
40 if (mNumberJob == -1) {
41 KMessageBox::information(mParentWidget, i18n("Collection cache cleared. You should restart Akonadi now."));
42 Q_EMIT clearCacheDone();
43 Q_EMIT finished(true);
45 return;
46 }
47 if (!mCollections.at(mNumberJob).isValid()) {
48 Q_EMIT clearCacheDone();
49 Q_EMIT finished(false);
51 return;
52 }
53
54 const auto akonadiId = mCollections.at(mNumberJob).id();
55 const auto ridCount = QStringLiteral("SELECT COUNT(*) FROM PimItemTable WHERE collectionId=%1 AND remoteId=''").arg(akonadiId);
56 QSqlQuery query(DbAccess::database());
57 if (!query.exec(ridCount)) {
58 qCWarning(AKONADIWIDGETS_LOG) << "Failed to execute query" << ridCount << ":" << query.lastError().text();
59 KMessageBox::error(mParentWidget, query.lastError().text());
60 Q_EMIT finished(false);
62 return;
63 }
64
65 query.next();
66 const int emptyRidCount = query.value(0).toInt();
67 if (emptyRidCount > 0) {
68 if (KMessageBox::warningContinueCancel(mParentWidget,
69 i18n("The collection '%1' contains %2 items without Remote ID. "
70 "Those items were likely never uploaded to the destination server, "
71 "so clearing this collection means that those <b>data will be lost</b>. "
72 "Are you sure you want to proceed?",
73 QString::number(akonadiId),
74 QString::number(emptyRidCount)),
75 QStringLiteral("POSSIBLE DATA LOSS!"))
77 Q_EMIT finished(false);
79 return;
80 }
81 }
82
83 const QString str = QStringLiteral("DELETE FROM PimItemTable WHERE collectionId=%1").arg(akonadiId);
84 // qCWarning(AKONADIWIDGETS_LOG) << str;
85 query = QSqlQuery(str, DbAccess::database());
86 if (query.exec()) {
87 const auto lastError = query.lastError();
88 if (lastError.isValid()) {
89 qCWarning(AKONADIWIDGETS_LOG) << lastError;
90 KMessageBox::error(mParentWidget, lastError.text());
91 }
92 }
93 mNumberJob--;
94 Q_EMIT clearNextFolder();
95}
96
97bool ClearCacheFoldersJob::canceled() const
98{
99 return mCanceled;
100}
101
102void ClearCacheFoldersJob::setCanceled(bool newCanceled)
103{
104 mCanceled = newCanceled;
105}
106
107void ClearCacheFoldersJob::start()
108{
109 if (!canStart()) {
110 deleteLater();
111 Q_EMIT finished(false);
112 return;
113 }
114 mNumberJob--;
115 Q_EMIT clearNextFolder();
116}
117
118bool ClearCacheFoldersJob::canStart() const
119{
120 return !mCollections.isEmpty();
121}
122
123QWidget *ClearCacheFoldersJob::parentWidget() const
124{
125 return mParentWidget;
126}
127
128void ClearCacheFoldersJob::setParentWidget(QWidget *newParentWidget)
129{
130 mParentWidget = newParentWidget;
131}
132
133#include "moc_clearcachefoldersjob.cpp"
Represents a collection of PIM items.
Definition collection.h:62
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
const_reference at(qsizetype i) const const
bool isEmpty() const const
qsizetype length() const const
T value(qsizetype i) const const
Q_EMITQ_EMIT
void deleteLater()
QString arg(Args &&... args) const const
QString number(double n, char format, int precision)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.