kmrlprmanager.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 "kmrlprmanager.h"
00021 #include "kmprinter.h"
00022 
00023 #include <qfile.h>
00024 #include <qfileinfo.h>
00025 #include <qtextstream.h>
00026 #include <qmap.h>
00027 
00028 #include <kstandarddirs.h>
00029 #include <klocale.h>
00030 
00031 KMRlprManager::KMRlprManager(QObject *parent, const char *name, const QStringList & /*args*/)
00032 : KMManager(parent,name)
00033 {
00034     setHasManagement(true);
00035     setPrinterOperationMask(KMManager::PrinterCreation|KMManager::PrinterRemoval|KMManager::PrinterTesting);
00036 }
00037 
00038 KMRlprManager::~KMRlprManager()
00039 {
00040 }
00041 
00042 bool KMRlprManager::createPrinter(KMPrinter *p)
00043 {
00044     if (p->name().isEmpty())
00045         setErrorMsg(i18n("Empty printer name."));
00046     else if (p->option("host").isEmpty())
00047         setErrorMsg(i18n("Empty host name."));
00048     else if (p->option("queue").isEmpty())
00049         setErrorMsg(i18n("Empty queue name."));
00050     else
00051     {
00052         KMPrinter   *pr = new KMPrinter(*p);
00053         addPrinter(pr);
00054         savePrinters();
00055         return true;
00056     }
00057     return false;
00058 }
00059 
00060 bool KMRlprManager::removePrinter(KMPrinter *p)
00061 {
00062     if (m_printers.findRef(p) == -1)
00063         setErrorMsg(i18n("Printer not found."));
00064     else
00065     {
00066         m_printers.removeRef(p);
00067         savePrinters();
00068         return true;
00069     }
00070     return false;
00071 }
00072 
00073 bool KMRlprManager::testPrinter(KMPrinter *)
00074 {
00075     setErrorMsg(i18n("Not implemented yet."));
00076     return false;
00077 }
00078 
00079 void KMRlprManager::listPrinters()
00080 {
00081     QFileInfo   pfi(printerFile());
00082     if (pfi.exists() && (!m_checktime.isValid() || m_checktime < pfi.lastModified()))
00083     {
00084         loadPrintersConf(pfi.absFilePath());
00085         m_checktime = pfi.lastModified();
00086     }
00087     else
00088         discardAllPrinters(false);
00089 }
00090 
00091 void KMRlprManager::loadPrintersConf(const QString& filename)
00092 {
00093     QFile   f(filename);
00094     if (f.exists() && f.open(IO_ReadOnly))
00095     {
00096         QTextStream t(&f);
00097         QString     line;
00098         while (!t.eof())
00099         {
00100             line = t.readLine().stripWhiteSpace();
00101             if (line.isEmpty() || line[0] == '#')
00102                 continue;
00103             QStringList w = QStringList::split('\t',line,true);
00104             if (w.count() < 3)
00105                 continue;
00106 
00107             KMPrinter   *printer = new KMPrinter;
00108             printer->setName(w[0]);
00109             printer->setPrinterName(w[0]);
00110             printer->setType(KMPrinter::Printer);
00111             printer->setOption("host",w[1]);
00112             printer->setOption("queue",w[2]);
00113             if (w.count() > 3)
00114             {
00115                 printer->setDescription(w[3]);
00116                 if (w.count() > 4) printer->setLocation(w[4]);
00117             }
00118             printer->setState(KMPrinter::Idle);
00119             printer->setDevice(QString::fromLatin1("lpd://%1/%2").arg(w[1]).arg(w[2]));
00120 
00121             addPrinter(printer);
00122         }
00123     }
00124 }
00125 
00126 void KMRlprManager::savePrinters()
00127 {
00128     savePrintersConf(printerFile());
00129 }
00130 
00131 void KMRlprManager::savePrintersConf(const QString& filename)
00132 {
00133     QFile   f(filename);
00134     if (f.open(IO_WriteOnly))
00135     {
00136         QTextStream t(&f);
00137         t << "# File generated by KDE print system. Don't edit by hand." << endl;
00138         QPtrListIterator<KMPrinter> it(m_printers);
00139         for (;it.current();++it)
00140         {
00141             if (!it.current()->name().isEmpty() && it.current()->instanceName().isEmpty())
00142             {
00143                 QString host = it.current()->option("host");
00144                 QString queue = it.current()->option("queue");
00145                 if (!host.isEmpty() && !queue.isEmpty())
00146                 {
00147                     t << it.current()->name() << '\t' << host << '\t' << queue;
00148                     t << '\t' << it.current()->description() << '\t' << it.current()->location() << endl;
00149                 }
00150             }
00151         }
00152     }
00153 }
00154 
00155 QString KMRlprManager::printerFile()
00156 {
00157     return locateLocal("data","kdeprint/printers.conf");
00158 }
KDE Home | KDE Accessibility Home | Description of Access Keys