# kdeprint Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# kmthreadjob.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., 59 Temple Place - Suite 330,
    00017  *  Boston, MA 02111-1307, USA.
    00018  **/
    00019
    00020 #include "kmthreadjob.h"
    00021 #include "kmjob.h"
    00022 #include "kmjobmanager.h"
    00023
    00024 #include <qfile.h>
    00025 #include <qtextstream.h>
    00026 #include <qstringlist.h>
    00027 #include <kstandarddirs.h>
    00028
    00029 #include <errno.h>
    00030 #include <sys/types.h>
    00031 #include <signal.h>
    00032
    00033 #define CHARSEP '$'
    00034
    00035 KMThreadJob::KMThreadJob(QObject *parent, const char *name)
    00036 : QObject(parent,name)
    00037 {
    00038     m_jobs.setAutoDelete(true);
    00039 }
    00040
    00041 KMThreadJob::~KMThreadJob()
    00042 {
    00043 }
    00044
    00045 QString KMThreadJob::jobFile()
    00046 {
    00047     QString f = locateLocal("data","kdeprint/printjobs");
    00048     return f;
    00049 }
    00050
    00051 bool KMThreadJob::saveJobs()
    00052 {
    00053     QFile   f(jobFile());
    00054     if (f.open(IO_WriteOnly))
    00055     {
    00056         QTextStream t(&f);
    00057         QIntDictIterator<KMJob> it(m_jobs);
    00058         for (;it.current();++it)
    00059             t << it.current()->id() << CHARSEP << it.current()->name() << CHARSEP << it.current()->printer() << CHARSEP << it.current()->owner() << CHARSEP << it.current()->size() << endl;
    00060         return true;
    00061     }
    00062     return false;
    00063 }
    00064
    00065 bool KMThreadJob::loadJobs()
    00066 {
    00067     QFile   f(jobFile());
    00068     if (f.exists() && f.open(IO_ReadOnly))
    00069     {
    00070         QTextStream t(&f);
    00071         QString     line;
    00072
    00073         m_jobs.clear();
    00074         while (!t.eof())
    00075         {
    00076             line = t.readLine().stripWhiteSpace();
    00077             if (line.isEmpty())
    00078                 continue;
    00079             QStringList ll = QStringList::split(CHARSEP,line,true);
    00080             if (ll.count() == 5)
    00081             {
    00082                 KMJob   *job = new KMJob();
    00083                 job->setId(ll[0].toInt());
    00084                 job->setName(ll[1]);
    00085                 job->setPrinter(ll[2]);
    00086                 job->setOwner(ll[3]);
    00087                 job->setSize(ll[4].toInt());
    00088                 job->setState(KMJob::Printing);
    00089                 job->setType(KMJob::Threaded);
    00090                 job->setUri("proc:/"+ll[0]);
    00091                 if (job->id() > 0 && checkJob(job->id()))
    00092                     m_jobs.insert(job->id(),job);
    00093                 else
    00094                     delete job;
    00095             }
    00096         }
    00097         return true;
    00098     }
    00099     return false;
    00100 }
    00101
    00102 bool KMThreadJob::checkJob(int ID)
    00103 {
    00104     return (kill((pid_t)ID,0) == 0 || errno == EPERM);
    00105 }
    00106
    00107 KMJob* KMThreadJob::findJob(int ID)
    00108 {
    00109     return m_jobs.find(ID);
    00110 }
    00111
    00112 KMJob* KMThreadJob::findJob(const QString& uri)
    00113 {
    00114     if (uri.startsWith("proc:/"))
    00115     {
    00116         int pid = uri.mid(6).toInt();
    00117         if (pid > 0)
    00118             return m_jobs.find(pid);
    00119     }
    00120     return NULL;
    00121 }
    00122
    00123 bool KMThreadJob::removeJob(int ID)
    00124 {
    00125     if (!checkJob(ID) || kill((pid_t)ID, SIGTERM) == 0)
    00126     {
    00127         m_jobs.remove(ID);
    00128         saveJobs();
    00129         return true;
    00130     }
    00131     else
    00132         return false;
    00133 }
    00134
    00135 void KMThreadJob::createJob(int ID, const QString& printer, const QString& name, const QString& owner, int size)
    00136 {
    00137     KMThreadJob mth(0);
    00138     KMJob   *job = new KMJob();
    00139     job->setId(ID);
    00140     job->setPrinter(printer);
    00141     job->setName(name);
    00142     job->setOwner(owner);
    00143     job->setSize(size);
    00144     job->setType(KMJob::Threaded);
    00145     mth.createJob(job);
    00146 }
    00147
    00148 void KMThreadJob::createJob(KMJob *job)
    00149 {
    00150     if (job->id() > 0)
    00151     {
    00152         loadJobs();
    00153         if (!m_jobs.find(job->id()))
    00154         {
    00155             m_jobs.insert(job->id(),job);
    00156             saveJobs();
    00157         }
    00158     }
    00159 }
    00160
    00161 void KMThreadJob::updateManager(KMJobManager *mgr)
    00162 {
    00163     loadJobs();
    00164     QIntDictIterator<KMJob> it(m_jobs);
    00165     for (;it.current();++it)
    00166     {
    00167         KMJob   *job = new KMJob(*(it.current()));
    00168         mgr->addJob(job);
    00169     }
    00170 }

This file is part of the documentation for kdeprint Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:03 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003