Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

jobs.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Martin Mueller                                  *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "jobs.h"
00021 #include "jobs.moc"
00022 
00023 #include "../blokkalentry.h"
00024 #include "../blokkalblog.h"
00025 
00026 //---
00027 //Job
00028 //---
00029 Blokkal::Io::Job::Job( QObject * parent ):
00030 KCompositeJob( parent )
00031 {
00032 }
00033 
00034 Blokkal::Io::Job::~Job( void )
00035 {
00036 }
00037 
00038 void Blokkal::Io::Job::start( void )
00039 {
00040         const QList<KJob *> jobList = subjobs();
00041 
00042         if( jobList.isEmpty() ) {
00043                 emitResult();
00044                 return;
00045         }
00046         
00047         for( QList<KJob *>::ConstIterator it = jobList.begin();
00048              it !=jobList.end();
00049              ++it )
00050         {
00051                 (*it)->start();
00052         }
00053 }
00054 
00055 void Blokkal::Io::Job::slotResult( KJob * job )
00056 {
00057         KCompositeJob::slotResult( job );
00058 
00059         subjobFinished( job );
00060         
00061         if( !error() && subjobs().isEmpty() ) {
00062                 jobFinished();
00063                 emitResult();
00064         }
00065         else if( error() ) { //an error may be set in subjbFinished()
00066                 emitResult();
00067         }
00068 }
00069 
00070 void Blokkal::Io::Job::jobFinished( void )
00071 {}
00072 
00073 void Blokkal::Io::Job::subjobFinished( KJob * job )
00074 {
00075         Q_UNUSED( job );
00076 }
00077 
00078 bool Blokkal::Io::Job::doKill( void )
00079 {
00080         bool allKilled = TRUE;
00081         const QList<KJob *> jobList = subjobs();
00082         for( QList<KJob *>::ConstIterator it = jobList.begin();
00083              it !=jobList.end();
00084              ++it )
00085         {
00086                 allKilled &= (*it)->kill();
00087         }
00088 
00089         return allKilled;
00090 }
00091 
00092 void Blokkal::Io::Job::slotEmitPercent( void )
00093 {
00094         unsigned long percent = 0;
00095         const QList<KJob *> jobList = subjobs();
00096         for( QList<KJob *>::ConstIterator it = jobList.begin();
00097              it !=jobList.end();
00098              ++it )
00099         {
00100                 percent += (*it)->percent() / jobList.size();
00101         }
00102         
00103         setPercent( percent );
00104 }
00105 
00106 bool Blokkal::Io::Job::addSubjob( KJob * job )
00107 {
00108         bool added = KCompositeJob::addSubjob( job );
00109         if( added ) {
00110                 connect( job,
00111                          SIGNAL( percent( KJob*, unsigned long ) ),
00112                          SLOT( slotEmitPercent( void ) ) );
00113         }
00114         return added;
00115 }
00116 
00117 //--------
00118 //EntryJob
00119 //--------
00120 class Blokkal::Io::EntryJob::Private {
00121 public:
00122         Private( Blokkal::Entry * entry ):
00123         entry( entry ) {}
00124 
00125         Blokkal::Entry * const entry;
00126 };
00127 
00128 Blokkal::Io::EntryJob::EntryJob( Blokkal::Entry * entry ):
00129 Job( entry ),
00130 d( new Private( entry  ) )
00131 {
00132 }
00133 
00134 Blokkal::Io::EntryJob::~EntryJob( void )
00135 {
00136         delete d;
00137 }
00138 
00139 Blokkal::Entry * Blokkal::Io::EntryJob::entry( void ) const
00140 {
00141         return d->entry;
00142 }
00143 
00144 //--------
00145 //BlogJob
00146 //--------
00147 class Blokkal::Io::BlogJob::Private {
00148 public:
00149         Private( Blokkal::Blog * blog ):
00150         blog( blog ) {}
00151 
00152         Blokkal::Blog * const blog;
00153 };
00154 
00155 Blokkal::Io::BlogJob::BlogJob( Blokkal::Blog * blog ):
00156 Job( blog ),
00157 d( new Private( blog  ) )
00158 {
00159 }
00160 
00161 Blokkal::Io::BlogJob::~BlogJob( void )
00162 {
00163         delete d;
00164 }
00165 
00166 Blokkal::Blog * Blokkal::Io::BlogJob::blog( void ) const
00167 {
00168         return d->blog;
00169 }
00170 
00171 //------------
00172 //LoadEntryJob
00173 //------------
00174 
00175 Blokkal::Io::LoadEntryJob::LoadEntryJob( Blokkal::Blog * blog ):
00176 BlogJob( blog )
00177 {
00178 }
00179 
00180 Blokkal::Io::LoadEntryJob::~LoadEntryJob( void )
00181 {
00182 }
00183 
00184 //------------
00185 //LoadIndexJob
00186 //------------
00187 Blokkal::Io::LoadIndexJob::LoadIndexJob( Blokkal::Blog * blog ):
00188 BlogJob( blog )
00189 {
00190 }
00191 
00192 Blokkal::Io::LoadIndexJob::~LoadIndexJob( void )
00193 {
00194 }