From ce4a32fe52ef09d8f5ff1dd22c001110902b60a2 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kdeprint/rlpr/kmrlprmanager.cpp | 158 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 kdeprint/rlpr/kmrlprmanager.cpp (limited to 'kdeprint/rlpr/kmrlprmanager.cpp') diff --git a/kdeprint/rlpr/kmrlprmanager.cpp b/kdeprint/rlpr/kmrlprmanager.cpp new file mode 100644 index 000000000..eafb1a497 --- /dev/null +++ b/kdeprint/rlpr/kmrlprmanager.cpp @@ -0,0 +1,158 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2001 Michael Goffioul + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "kmrlprmanager.h" +#include "kmprinter.h" + +#include +#include +#include +#include + +#include +#include + +KMRlprManager::KMRlprManager(QObject *parent, const char *name, const QStringList & /*args*/) +: KMManager(parent,name) +{ + setHasManagement(true); + setPrinterOperationMask(KMManager::PrinterCreation|KMManager::PrinterRemoval|KMManager::PrinterTesting); +} + +KMRlprManager::~KMRlprManager() +{ +} + +bool KMRlprManager::createPrinter(KMPrinter *p) +{ + if (p->name().isEmpty()) + setErrorMsg(i18n("Empty printer name.")); + else if (p->option("host").isEmpty()) + setErrorMsg(i18n("Empty host name.")); + else if (p->option("queue").isEmpty()) + setErrorMsg(i18n("Empty queue name.")); + else + { + KMPrinter *pr = new KMPrinter(*p); + addPrinter(pr); + savePrinters(); + return true; + } + return false; +} + +bool KMRlprManager::removePrinter(KMPrinter *p) +{ + if (m_printers.findRef(p) == -1) + setErrorMsg(i18n("Printer not found.")); + else + { + m_printers.removeRef(p); + savePrinters(); + return true; + } + return false; +} + +bool KMRlprManager::testPrinter(KMPrinter *) +{ + setErrorMsg(i18n("Not implemented yet.")); + return false; +} + +void KMRlprManager::listPrinters() +{ + QFileInfo pfi(printerFile()); + if (pfi.exists() && (!m_checktime.isValid() || m_checktime < pfi.lastModified())) + { + loadPrintersConf(pfi.absFilePath()); + m_checktime = pfi.lastModified(); + } + else + discardAllPrinters(false); +} + +void KMRlprManager::loadPrintersConf(const QString& filename) +{ + QFile f(filename); + if (f.exists() && f.open(IO_ReadOnly)) + { + QTextStream t(&f); + QString line; + while (!t.eof()) + { + line = t.readLine().stripWhiteSpace(); + if (line.isEmpty() || line[0] == '#') + continue; + QStringList w = QStringList::split('\t',line,true); + if (w.count() < 3) + continue; + + KMPrinter *printer = new KMPrinter; + printer->setName(w[0]); + printer->setPrinterName(w[0]); + printer->setType(KMPrinter::Printer); + printer->setOption("host",w[1]); + printer->setOption("queue",w[2]); + if (w.count() > 3) + { + printer->setDescription(w[3]); + if (w.count() > 4) printer->setLocation(w[4]); + } + printer->setState(KMPrinter::Idle); + printer->setDevice(QString::fromLatin1("lpd://%1/%2").arg(w[1]).arg(w[2])); + + addPrinter(printer); + } + } +} + +void KMRlprManager::savePrinters() +{ + savePrintersConf(printerFile()); +} + +void KMRlprManager::savePrintersConf(const QString& filename) +{ + QFile f(filename); + if (f.open(IO_WriteOnly)) + { + QTextStream t(&f); + t << "# File generated by KDE print system. Don't edit by hand." << endl; + QPtrListIterator it(m_printers); + for (;it.current();++it) + { + if (!it.current()->name().isEmpty() && it.current()->instanceName().isEmpty()) + { + QString host = it.current()->option("host"); + QString queue = it.current()->option("queue"); + if (!host.isEmpty() && !queue.isEmpty()) + { + t << it.current()->name() << '\t' << host << '\t' << queue; + t << '\t' << it.current()->description() << '\t' << it.current()->location() << endl; + } + } + } + } +} + +QString KMRlprManager::printerFile() +{ + return locateLocal("data","kdeprint/printers.conf"); +} -- cgit v1.2.1