• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

kmthreadjob.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <tdeprint@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 "kmthreadjob.h"
00021 #include "kmjob.h"
00022 #include "kmjobmanager.h"
00023 
00024 #include <tqfile.h>
00025 #include <tqtextstream.h>
00026 #include <tqstringlist.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(TQObject *parent, const char *name)
00036 : TQObject(parent,name)
00037 {
00038     m_jobs.setAutoDelete(true);
00039 }
00040 
00041 KMThreadJob::~KMThreadJob()
00042 {
00043 }
00044 
00045 TQString KMThreadJob::jobFile()
00046 {
00047     QString f = locateLocal("data","tdeprint/printjobs");
00048     return f;
00049 }
00050 
00051 bool KMThreadJob::saveJobs()
00052 {
00053     TQFile  f(jobFile());
00054     if (f.open(IO_WriteOnly))
00055     {
00056         TQTextStream    t(&f);
00057         TQIntDictIterator<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     TQFile  f(jobFile());
00068     if (f.exists() && f.open(IO_ReadOnly))
00069     {
00070         TQTextStream    t(&f);
00071         TQString        line;
00072 
00073         m_jobs.clear();
00074         while (!t.eof())
00075         {
00076             line = t.readLine().stripWhiteSpace();
00077             if (line.isEmpty())
00078                 continue;
00079             TQStringList    ll = TQStringList::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 TQString& 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 TQString& printer, const TQString& name, const TQString& 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     TQIntDictIterator<KMJob>    it(m_jobs);
00165     for (;it.current();++it)
00166     {
00167         KMJob   *job = new KMJob(*(it.current()));
00168         mgr->addJob(job);
00169     }
00170 }

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.7.1
This website is maintained by Timothy Pearson.