Pimcommon

agentprogressmonitor.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "agentprogressmonitor.h"
8
9#include <Akonadi/AgentManager>
10
11using namespace Akonadi;
12using namespace PimCommon;
13
14AgentProgressMonitor::AgentProgressMonitor(const AgentInstance &agent, KPIM::ProgressItem *item)
15 : QObject(item)
16 , mAgent(agent)
17 , mItem(item)
18{
19 connect(AgentManager::self(), &AgentManager::instanceProgressChanged, this, &AgentProgressMonitor::instanceProgressChanged);
20 connect(AgentManager::self(), &AgentManager::instanceStatusChanged, this, &AgentProgressMonitor::instanceStatusChanged);
21 connect(Akonadi::AgentManager::self(), &AgentManager::instanceRemoved, this, &AgentProgressMonitor::instanceRemoved);
22 connect(Akonadi::AgentManager::self(), &AgentManager::instanceNameChanged, this, &AgentProgressMonitor::instanceNameChanged);
23 // TODO connect to instanceError, instanceWarning, instanceOnline ?
24 // and do what?
25
26 connect(item, &KPIM::ProgressItem::progressItemCanceled, this, &AgentProgressMonitor::abort);
27
28 // TODO handle offline case
29}
30
31AgentProgressMonitor::~AgentProgressMonitor() = default;
32
33void AgentProgressMonitor::abort()
34{
35 mAgent.abortCurrentTask();
36}
37
38void AgentProgressMonitor::instanceRemoved(const Akonadi::AgentInstance &instance)
39{
40 Q_UNUSED(instance)
41
42 if (!mItem.data()) {
43 return;
44 }
45
46 mItem.data()->disconnect(this); // avoid abort call
47 mItem.data()->cancel();
48 if (mItem.data()) {
49 mItem.data()->setComplete();
50 }
51}
52
53void AgentProgressMonitor::instanceProgressChanged(const AgentInstance &instance)
54{
55 if (!mItem.data()) {
56 return;
57 }
58
59 if (mAgent == instance) { // compares identifiers
60 mAgent = instance; // keeps copy of current status
61 const int progress = mAgent.progress();
62 if (progress >= 0) {
63 mItem.data()->setProgress(progress);
64 }
65 }
66}
67
68void AgentProgressMonitor::instanceStatusChanged(const AgentInstance &instance)
69{
70 if (!mItem.data()) {
71 return;
72 }
73
74 if (mAgent == instance) { // compares identifiers
75 mAgent = instance; // keeps copy of current status
76 mItem.data()->setStatus(mAgent.statusMessage());
77 switch (mAgent.status()) {
78 case AgentInstance::Idle:
79 if (mItem.data()) {
80 mItem.data()->setComplete();
81 }
82 break;
83 case AgentInstance::Running:
84 break;
85 case AgentInstance::Broken:
86 mItem.data()->disconnect(this); // avoid abort call
87 mItem.data()->cancel();
88 if (mItem.data()) {
89 mItem.data()->setComplete();
90 }
91 break;
92 default:
93 Q_ASSERT(false);
94 }
95 }
96}
97
98void AgentProgressMonitor::instanceNameChanged(const Akonadi::AgentInstance &instance)
99{
100 if (!mItem.data()) {
101 return;
102 }
103 mItem.data()->setLabel(instance.name());
104}
105
106#include "moc_agentprogressmonitor.cpp"
Status status() const
void abortCurrentTask() const
QString statusMessage() const
QString name() const
static AgentManager * self()
void progressItemCanceled(KPIM::ProgressItem *)
folderdialogacltab.h
T * data() const const
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 Sat Dec 21 2024 17:02:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.