20#include "emptytrashcommand_p.h"
22#include "imapsettings.h"
25#include <KLocalizedString>
28#include "akonadi/entitytreemodel.h"
29#include "akonadi/kmime/specialmailcollections.h"
30#include "akonadi/itemfetchjob.h"
31#include "akonadi/itemdeletejob.h"
32#include "akonadi/agentmanager.h"
33#include "kmime/kmime_message.h"
35EmptyTrashCommand::EmptyTrashCommand(
const QAbstractItemModel *model, QObject *parent)
38 , the_trashCollectionFolder(-1)
39 , mNumberOfTrashToEmpty(0)
46 , the_trashCollectionFolder(-1)
48 , mNumberOfTrashToEmpty(0)
52void EmptyTrashCommand::execute()
54 if (!mFolder.isValid() && !mModel) {
59 if (!mFolder.isValid()) {
60 const QString title = i18n(
"Empty Trash");
61 const QString text = i18n(
"Are you sure you want to empty the trash folders of all accounts?");
62 if (KMessageBox::warningContinueCancel(0, text, title,
63 KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
64 QLatin1String(
"confirm_empty_trash"))
65 != KMessageBox::Continue) {
69 Akonadi::Collection trash = trashCollectionFolder();
70 QList<Akonadi::Collection> trashFolder;
74 foreach (
const Akonadi::AgentInstance &type, lst) {
75 if (type.identifier().contains(IMAP_RESOURCE_IDENTIFIER)) {
79 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface(type.identifier());
80 if (iface->isValid()) {
81 const int trashImap = iface->trashCollection();
82 if (trashImap != trash.
id()) {
83 trashFolder << Akonadi::Collection(trashImap);
89 mNumberOfTrashToEmpty = trashFolder.count();
90 for (
int i = 0; i < mNumberOfTrashToEmpty; ++i) {
91 expunge(trashFolder.at(i));
94 if (folderIsTrash(mFolder)) {
95 mNumberOfTrashToEmpty++;
107 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob(col,
this);
108 connect(job, SIGNAL(result(KJob*)),
this, SLOT(slotExpungeJob(KJob*)));
110 kDebug() <<
" Try to expunge an invalid collection :" << col;
115void EmptyTrashCommand::slotExpungeJob(KJob *job)
118 Util::showJobError(job);
122 Akonadi::ItemFetchJob *fjob =
dynamic_cast<Akonadi::ItemFetchJob *
>(job);
127 const Akonadi::Item::List lstItem = fjob->
items();
128 if (lstItem.isEmpty()) {
132 Akonadi::ItemDeleteJob *jobDelete =
new Akonadi::ItemDeleteJob(lstItem,
this);
133 connect(jobDelete, SIGNAL(result(KJob*)),
this, SLOT(slotDeleteJob(KJob*)));
137void EmptyTrashCommand::slotDeleteJob(KJob *job)
140 Util::showJobError(job);
150 if (instance.
type().
mimeTypes().contains(KMime::Message::mimeType()) &&
153 relevantInstances << instance;
156 return relevantInstances;
162 mModel, Akonadi::Collection(
id)
169 if (the_trashCollectionFolder < 0) {
172 return collectionFromId(the_trashCollectionFolder);
181 foreach (
const Akonadi::AgentInstance &type, lst) {
185 if (type.identifier().contains(IMAP_RESOURCE_IDENTIFIER)) {
186 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface(type.identifier());
187 if (iface->isValid()) {
188 if (iface->trashCollection() == col.
id()) {
199void EmptyTrashCommand::emitResult(Result value)
202 mNumberOfTrashToEmpty--;
203 if (mNumberOfTrashToEmpty <= 0) {
AgentType type() const
Returns the agent type of this instance.
QList< AgentInstance > List
Describes a list of agent instances.
@ Broken
The agent instance encountered an error state.
static AgentManager * self()
Returns the global instance of the agent manager.
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
Represents a collection of PIM items.
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
@ CollectionRole
The collection.
bool isValid() const
Returns whether the entity is valid.
Id id() const
Returns the unique identifier of the entity.
qint64 Id
Describes the unique id type.
Item::List items() const
Returns the fetched items.
@ Trash
The trash collection.
static SpecialMailCollections * self()
Returns the global SpecialMailCollections instance.
Akonadi::Collection defaultCollection(Type type) const
Returns the special mail collection of given type in the default resource, or an invalid collection i...