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
20 #include "agentprogressmonitor.h"
22 #include <Akonadi/AgentManager>
26 #include <QWeakPointer>
28 using namespace Akonadi
;
31 class AgentProgressMonitor::Private
34 Private( AgentProgressMonitor
*qq
, const AgentInstance
&agnt
, ProgressItem
*itm
)
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
;
49 QWeakPointer
<ProgressItem
> const item
;
52 void AgentProgressMonitor::Private::abort()
54 agent
.abortCurrentTask();
58 void AgentProgressMonitor::Private::instanceRemoved( const Akonadi::AgentInstance
&instance
)
66 item
.data()->disconnect( q
); // avoid abort call
67 item
.data()->cancel();
69 item
.data()->setComplete();
73 void AgentProgressMonitor::Private::instanceProgressChanged( const AgentInstance
&instance
)
79 if ( agent
== instance
) {
80 //Why ? agent = instance if 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
)
94 if ( agent
== instance
) {
95 //Why ? agent = instance if agent == instance.
97 item
.data()->setStatus( agent
.statusMessage() );
98 switch ( agent
.status() ) {
99 case AgentInstance::Idle
:
101 item
.data()->setComplete();
103 case AgentInstance::Running
:
105 case AgentInstance::Broken
:
106 item
.data()->disconnect( q
); // avoid abort call
107 item
.data()->cancel();
109 item
.data()->setComplete();
117 void AgentProgressMonitor::Private::instanceNameChanged( const Akonadi::AgentInstance
& instance
)
121 item
.data()->setLabel(instance
.name());
126 AgentProgressMonitor::AgentProgressMonitor( const AgentInstance
&agent
,
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 ?
142 connect( item
, SIGNAL(progressItemCanceled(KPIM::ProgressItem
*)),
143 this, SLOT(abort()) );
145 // TODO handle offline case
148 AgentProgressMonitor::~AgentProgressMonitor()
153 #include "agentprogressmonitor.moc"