Better wording
[kdepim.git] / libkdepim / agentprogressmonitor.cpp
blob1f39a3472a08c6c66f61ddf939b654e977c0d39c
1 /*
2 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
20 #include "agentprogressmonitor.h"
22 #include <Akonadi/AgentManager>
24 #include <KDebug>
26 #include <QWeakPointer>
28 using namespace Akonadi;
29 using namespace KPIM;
31 class AgentProgressMonitor::Private
33 public:
34 Private( AgentProgressMonitor *qq, const AgentInstance &agnt, ProgressItem *itm )
35 : q( qq )
36 , agent( agnt )
37 , item ( itm )
41 // slots:
42 void abort();
43 void instanceProgressChanged( const AgentInstance &instance );
44 void instanceStatusChanged( const AgentInstance &instance );
45 void instanceRemoved( const Akonadi::AgentInstance& instance );
46 void instanceNameChanged( const Akonadi::AgentInstance& instance );
47 AgentProgressMonitor *const q;
48 AgentInstance agent;
49 QWeakPointer<ProgressItem> const item;
52 void AgentProgressMonitor::Private::abort()
54 agent.abortCurrentTask();
58 void AgentProgressMonitor::Private::instanceRemoved( const Akonadi::AgentInstance &instance )
60 Q_UNUSED( instance );
62 if ( !item.data() ) {
63 return;
66 item.data()->disconnect( q ); // avoid abort call
67 item.data()->cancel();
68 if( item.data() ) {
69 item.data()->setComplete();
73 void AgentProgressMonitor::Private::instanceProgressChanged( const AgentInstance &instance )
75 if ( !item.data() ) {
76 return;
79 if ( agent == instance ) {
80 //Why ? agent = instance if agent == instance.
81 agent = instance;
82 const int progress = agent.progress();
83 if ( progress >= 0 ) {
84 item.data()->setProgress( progress );
89 void AgentProgressMonitor::Private::instanceStatusChanged( const AgentInstance &instance )
91 if ( !item.data() )
92 return;
94 if ( agent == instance ) {
95 //Why ? agent = instance if agent == instance.
96 agent = instance;
97 item.data()->setStatus( agent.statusMessage() );
98 switch ( agent.status() ) {
99 case AgentInstance::Idle:
100 if( item.data() )
101 item.data()->setComplete();
102 break;
103 case AgentInstance::Running:
104 break;
105 case AgentInstance::Broken:
106 item.data()->disconnect( q ); // avoid abort call
107 item.data()->cancel();
108 if( item.data() )
109 item.data()->setComplete();
110 break;
111 default:
112 Q_ASSERT( false );
117 void AgentProgressMonitor::Private::instanceNameChanged( const Akonadi::AgentInstance& instance )
119 if ( !item.data() )
120 return;
121 item.data()->setLabel(instance.name());
126 AgentProgressMonitor::AgentProgressMonitor( const AgentInstance &agent,
127 ProgressItem *item )
128 : QObject( item )
129 , d( new Private( this, agent, item ) )
131 connect( AgentManager::self(), SIGNAL(instanceProgressChanged(Akonadi::AgentInstance)),
132 this, SLOT(instanceProgressChanged(Akonadi::AgentInstance)) );
133 connect( AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)),
134 this, SLOT(instanceStatusChanged(Akonadi::AgentInstance)) );
135 connect( Akonadi::AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)),
136 this, SLOT(instanceRemoved(Akonadi::AgentInstance)) );
137 connect( Akonadi::AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)),
138 this, SLOT(instanceNameChanged(Akonadi::AgentInstance)) );
139 // TODO connect to instanceError, instanceWarning, instanceOnline ?
140 // and do what?
142 connect( item, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
143 this, SLOT(abort()) );
145 // TODO handle offline case
148 AgentProgressMonitor::~AgentProgressMonitor()
150 delete d;
153 #include "agentprogressmonitor.moc"