kmjobmanager.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kmjobmanager.h"
00021 #include "kmjob.h"
00022 #include "kmthreadjob.h"
00023 #include "kmfactory.h"
00024 
00025 #include <kaction.h>
00026 #include <kdebug.h>
00027 #include <kconfig.h>
00028 
00029 KMJobManager::KMJobManager(QObject *parent, const char *name)
00030 : QObject(parent,name)
00031 {
00032     m_jobs.setAutoDelete(true);
00033     m_threadjob = new KMThreadJob(this, "ThreadJob");
00034     m_filter.setAutoDelete(true);
00035 }
00036 
00037 KMJobManager::~KMJobManager()
00038 {
00039 }
00040 
00041 KMJobManager* KMJobManager::self()
00042 {
00043     return KMFactory::self()->jobManager();
00044 }
00045 
00046 void KMJobManager::discardAllJobs()
00047 {
00048     QPtrListIterator<KMJob> it(m_jobs);
00049     for (;it.current();++it)
00050         it.current()->setDiscarded(true);
00051 }
00052 
00053 void KMJobManager::removeDiscardedJobs()
00054 {
00055     for (uint i=0;i<m_jobs.count();i++)
00056         if (m_jobs.at(i)->isDiscarded())
00057         {
00058             m_jobs.remove(i);
00059             i--;
00060         }
00061 }
00062 
00063 /*KMJob* KMJobManager::findJob(int ID)
00064 {
00065     QPtrListIterator<KMJob> it(m_jobs);
00066     for (;it.current();++it)
00067         if (it.current()->id() == ID)
00068             return it.current();
00069     return 0;
00070 }*/
00071 
00072 KMJob* KMJobManager::findJob(const QString& uri)
00073 {
00074     QPtrListIterator<KMJob> it(m_jobs);
00075     for (;it.current();++it)
00076         if (it.current()->uri() == uri)
00077             return it.current();
00078     return 0;
00079 }
00080 
00081 void KMJobManager::addJob(KMJob *job)
00082 {
00083     // only keep it if "printer" is not empty, and in printer filter
00084     if (!job->uri().isEmpty() && !job->printer().isEmpty())
00085     {
00086         KMJob   *aJob = findJob(job->uri());
00087         if (aJob)
00088         {
00089             aJob->copy(*job);
00090             delete job;
00091         }
00092         else
00093         {
00094             job->setDiscarded(false);
00095             m_jobs.append(job);
00096         }
00097     }
00098     else
00099         delete job;
00100 }
00101 
00102 /*bool KMJobManager::sendCommand(int ID, int action, const QString& arg)
00103 {
00104     KMJob   *job = findJob(ID);
00105     if (job)
00106     {
00107         QPtrList<KMJob> l;
00108         l.setAutoDelete(false);
00109         l.append(job);
00110         return sendCommand(l,action,arg);
00111     }
00112     return false;
00113 }*/
00114 
00115 bool KMJobManager::sendCommand(const QString& uri, int action, const QString& arg)
00116 {
00117     KMJob   *job = findJob(uri);
00118     if (job)
00119     {
00120         QPtrList<KMJob> l;
00121         l.setAutoDelete(false);
00122         l.append(job);
00123         return sendCommand(l,action,arg);
00124     }
00125     return false;
00126 }
00127 
00128 bool KMJobManager::sendCommand(const QPtrList<KMJob>& jobs, int action, const QString& args)
00129 {
00130     // split jobs in 2 classes
00131     QPtrList<KMJob> csystem, cthread;
00132     csystem.setAutoDelete(false);
00133     cthread.setAutoDelete(false);
00134     QPtrListIterator<KMJob> it(jobs);
00135     for (;it.current();++it)
00136         if (it.current()->type() == KMJob::Threaded) cthread.append(it.current());
00137         else csystem.append(it.current());
00138 
00139     // perform operation on both classes
00140     if (cthread.count() > 0 && !sendCommandThreadJob(cthread, action, args))
00141         return false;
00142     if (csystem.count() > 0 && !sendCommandSystemJob(csystem, action, args))
00143         return false;
00144     return true;
00145 }
00146 
00147 bool KMJobManager::sendCommandSystemJob(const QPtrList<KMJob>&, int, const QString&)
00148 {
00149     return false;
00150 }
00151 
00152 bool KMJobManager::sendCommandThreadJob(const QPtrList<KMJob>& jobs, int action, const QString&)
00153 {
00154     if (action != KMJob::Remove)
00155         return false;
00156 
00157     QPtrListIterator<KMJob> it(jobs);
00158     bool    result(true);
00159     for (;it.current() && result; ++it)
00160         result = m_threadjob->removeJob(it.current()->id());
00161     return result;
00162 }
00163 
00164 bool KMJobManager::listJobs(const QString&, KMJobManager::JobType, int)
00165 {
00166     return true;
00167 }
00168 
00169 const QPtrList<KMJob>& KMJobManager::jobList(bool reload)
00170 {
00171     if (reload || m_jobs.count() == 0)
00172     {
00173         discardAllJobs();
00174         QDictIterator<JobFilter>    it(m_filter);
00175         int joblimit = limit();
00176         bool threadjobs_updated = false;
00177         for (; it.current(); ++it)
00178         {
00179             if ( it.current()->m_isspecial )
00180             {
00181                 if ( !threadjobs_updated )
00182                 {
00183                     threadJob()->updateManager( this );
00184                     threadjobs_updated = true;
00185                 }
00186             }
00187             else
00188             {
00189                 if (it.current()->m_type[ActiveJobs] > 0)
00190                     listJobs(it.currentKey(), ActiveJobs, joblimit);
00191                 if (it.current()->m_type[CompletedJobs] > 0)
00192                     listJobs(it.currentKey(), CompletedJobs, joblimit);
00193             }
00194         }
00195         m_threadjob->updateManager(this);
00196         removeDiscardedJobs();
00197     }
00198     return m_jobs;
00199 }
00200 
00201 int KMJobManager::actions()
00202 {
00203     return 0;
00204 }
00205 
00206 QValueList<KAction*> KMJobManager::createPluginActions(KActionCollection*)
00207 {
00208     return QValueList<KAction*>();
00209 }
00210 
00211 void KMJobManager::validatePluginActions(KActionCollection*, const QPtrList<KMJob>&)
00212 {
00213 }
00214 
00215 void KMJobManager::addPrinter(const QString& pr, KMJobManager::JobType type, bool isSpecial)
00216 {
00217     struct JobFilter    *jf = m_filter.find(pr);
00218     if (!jf)
00219     {
00220         jf = new JobFilter;
00221         m_filter.insert(pr, jf);
00222     }
00223     jf->m_type[type]++;
00224     jf->m_isspecial = isSpecial;
00225 }
00226 
00227 void KMJobManager::removePrinter(const QString& pr, KMJobManager::JobType type)
00228 {
00229     struct JobFilter    *jf = m_filter.find(pr);
00230     if (jf)
00231     {
00232         jf->m_type[type] = QMAX(0, jf->m_type[type]-1);
00233         if (!jf->m_type[0] && !jf->m_type[1])
00234             m_filter.remove(pr);
00235     }
00236 }
00237 
00238 bool KMJobManager::doPluginAction(int, const QPtrList<KMJob>&)
00239 {
00240     return true;
00241 }
00242 
00243 void KMJobManager::setLimit(int val)
00244 {
00245     KConfig *conf = KMFactory::self()->printConfig();
00246     conf->setGroup("Jobs");
00247     conf->writeEntry("Limit", val);
00248 }
00249 
00250 int KMJobManager::limit()
00251 {
00252     KConfig *conf = KMFactory::self()->printConfig();
00253     conf->setGroup("Jobs");
00254     return conf->readNumEntry("Limit", 0);
00255 }
00256 
00257 #include "kmjobmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys