lpqhelper.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 "lpqhelper.h"
00021 #include "kmjob.h"
00022 #include "kpipeprocess.h"
00023 #include "lprsettings.h"
00024 
00025 #include <kstandarddirs.h>
00026 #include <kprocess.h>
00027 #include <kdebug.h>
00028 
00029 LpqHelper::LpqHelper(QObject *parent, const char *name)
00030 : QObject(parent, name)
00031 {
00032     m_exepath = KStandardDirs::findExe("lpq");
00033 }
00034 
00035 LpqHelper::~LpqHelper()
00036 {
00037 }
00038 
00039 KMJob* LpqHelper::parseLineLpr(const QString& line)
00040 {
00041     QString rank = line.left(7);
00042     if (!rank[0].isDigit() && rank != "active")
00043         return NULL;
00044     KMJob   *job = new KMJob;
00045     job->setState((rank[0].isDigit() ? KMJob::Queued : KMJob::Printing));
00046     job->setOwner(line.mid(7, 11).stripWhiteSpace());
00047     job->setId(line.mid(18, 5).toInt());
00048     job->setName(line.mid(23, 38).stripWhiteSpace());
00049     int p = line.find(' ', 61);
00050     if (p != -1)
00051     {
00052         job->setSize(line.mid(61, p-61).toInt() / 1000);
00053     }
00054     return job;
00055 }
00056 
00057 KMJob* LpqHelper::parseLineLPRng(const QString& line)
00058 {
00059     QString rank = line.left(7).stripWhiteSpace();
00060     if (!rank[0].isDigit() && rank != "active" && rank != "hold")
00061         return NULL;
00062     KMJob   *job = new KMJob;
00063     job->setState((rank[0].isDigit() ? KMJob::Queued : (rank == "hold" ? KMJob::Held : KMJob::Printing)));
00064     int p = line.find('@', 7), q = line.find(' ', 7);
00065     job->setOwner(line.mid(7, QMIN(p,q)-7));
00066     while (line[q].isSpace())
00067         q++;
00068     q++;
00069     while (line[q].isSpace())
00070         q++;
00071     p = line.find(' ', q);
00072     job->setId(line.mid(q, p-q).toInt());
00073     while (line[p].isSpace())
00074         p++;
00075     q = p+25;
00076     while (line[q].isDigit())
00077         q--;
00078     job->setName(line.mid(p, q-p).stripWhiteSpace());
00079     job->setSize(line.mid(q+1, p+26-q).toInt() / 1000);
00080     return job;
00081 }
00082 
00083 void LpqHelper::listJobs(QPtrList<KMJob>& jobs, const QString& prname, int limit)
00084 {
00085     KPipeProcess    proc;
00086     if (!m_exepath.isEmpty() && proc.open(m_exepath + " -P " + KProcess::quote(prname)))
00087     {
00088         QTextStream t(&proc);
00089         QString     line;
00090         bool    lprng = (LprSettings::self()->mode() == LprSettings::LPRng);
00091         int count = 0;
00092 
00093         while (!t.atEnd())
00094         {
00095             line = t.readLine().stripWhiteSpace();
00096             if (line.startsWith("Rank"))
00097                 break;
00098         }
00099         while (!t.atEnd())
00100         {
00101             line = t.readLine();
00102             if ( limit > 0 && count >= limit )
00103                 continue;
00104             KMJob   *job = (lprng ? parseLineLPRng(line) : parseLineLpr(line));
00105             if (job)
00106             {
00107                 job->setPrinter(prname);
00108                 job->setUri("lpd://"+prname+"/"+QString::number(job->id()));
00109                 jobs.append(job);
00110                 count++;
00111             }
00112             else
00113                 break;
00114         }
00115         proc.close();
00116     }
00117 }
KDE Home | KDE Accessibility Home | Description of Access Keys