kdeprint Library API Documentation

kmspecialmanager.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 "kmspecialmanager.h" 00021 #include "kmmanager.h" 00022 #include "kmprinter.h" 00023 #include "kdeprintcheck.h" 00024 #include "kxmlcommand.h" 00025 #include "driver.h" 00026 00027 #include <qfile.h> 00028 #include <kstandarddirs.h> 00029 #include <kglobal.h> 00030 #include <ksimpleconfig.h> 00031 #include <klocale.h> 00032 #include <kdebug.h> 00033 00034 #include <unistd.h> 00035 #include <sys/types.h> 00036 #include <sys/stat.h> 00037 00038 KMSpecialManager::KMSpecialManager(KMManager *parent, const char *name) 00039 : QObject(parent,name), m_mgr(parent), m_loaded(false) 00040 { 00041 } 00042 00043 bool KMSpecialManager::savePrinters() 00044 { 00045 // for root, use a global location. 00046 QString confname; 00047 if (getuid() == 0) 00048 { 00049 confname = locate("data", "kdeprint/specials.desktop"); 00050 if (confname.startsWith(KGlobal::dirs()->localkdedir())) 00051 { 00052 // seems there's a problem here 00053 m_mgr->setErrorMsg(i18n("A file share/kdeprint/specials.desktop was found in your " 00054 "local KDE directory. This file probably comes from a previous KDE " 00055 "release and should be removed in order to manage global pseudo " 00056 "printers.")); 00057 return false; 00058 } 00059 } 00060 else 00061 confname = locateLocal("data","kdeprint/specials.desktop"); 00062 00063 KSimpleConfig conf(confname); 00064 00065 // first clear existing groups 00066 conf.setGroup("General"); 00067 int n = conf.readNumEntry("Number",0); 00068 for (int i=0;i<n;i++) 00069 conf.deleteGroup(QString::fromLatin1("Printer %1").arg(i),true); 00070 00071 // then add printers 00072 n = 0; 00073 QPtrListIterator<KMPrinter> it(m_mgr->m_printers); 00074 for (;it.current();++it) 00075 { 00076 if (!it.current()->isSpecial() || it.current()->isVirtual()) continue; 00077 conf.setGroup(QString::fromLatin1("Printer %1").arg(n)); 00078 conf.writeEntry("Name",it.current()->name()); 00079 conf.writeEntry("Description",it.current()->description()); 00080 conf.writeEntry("Comment",it.current()->location()); 00081 conf.writePathEntry("Command",it.current()->option("kde-special-command")); 00082 conf.writePathEntry("File",it.current()->option("kde-special-file")); 00083 conf.writeEntry("Icon",it.current()->pixmap()); 00084 conf.writeEntry("Extension",it.current()->option("kde-special-extension")); 00085 conf.writeEntry("Mimetype",it.current()->option("kde-special-mimetype")); 00086 conf.writeEntry("Require",it.current()->option("kde-special-require")); 00087 n++; 00088 } 00089 conf.setGroup("General"); 00090 conf.writeEntry("Number",n); 00091 00092 // set read access for anybody in case of global location 00093 if (getuid() == 0) 00094 { 00095 conf.sync(); 00096 ::chmod(QFile::encodeName(confname), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 00097 } 00098 00099 // force reload on next update 00100 m_loaded = false; 00101 00102 return true; 00103 } 00104 00105 bool KMSpecialManager::loadPrinters() 00106 { 00107 if (m_loaded) return true; 00108 00109 bool result(true); 00110 QString localDir = KGlobal::dirs()->localkdedir(); 00111 QStringList files = KGlobal::dirs()->findAllResources("data", "kdeprint/specials.desktop"); 00112 // local files should processed last, so we need to reorder the list 00113 // and put local files at the end 00114 QStringList orderedFiles; 00115 for (QStringList::ConstIterator it=files.begin(); it!=files.end(); ++it) 00116 { 00117 if ((*it).startsWith(localDir)) 00118 orderedFiles.append(*it); 00119 else 00120 orderedFiles.prepend(*it); 00121 } 00122 // then parse the ordered list 00123 for (QStringList::ConstIterator it=orderedFiles.begin(); it!=orderedFiles.end() && result; ++it) 00124 { 00125 // skip the local file for root 00126 if (getuid() == 0 && (*it).startsWith(localDir)) 00127 break; 00128 else 00129 result = loadDesktopFile(*it); 00130 } 00131 00132 return result; 00133 } 00134 00135 bool KMSpecialManager::loadDesktopFile(const QString& filename) 00136 { 00137 KSimpleConfig conf(filename); 00138 conf.setGroup("General"); 00139 int n = conf.readNumEntry("Number",0); 00140 for (int i=0;i<n;i++) 00141 { 00142 QString grpname = QString::fromLatin1("Printer %1").arg(i); 00143 if (!conf.hasGroup(grpname)) continue; 00144 conf.setGroup(grpname); 00145 KMPrinter *printer = new KMPrinter; 00146 printer->setName(conf.readEntry("Name")); 00147 printer->setPrinterName(printer->name()); 00148 printer->setDescription(conf.readEntry("Description")); 00149 printer->setLocation(conf.readEntry("Comment")); 00150 printer->setOption("kde-special-command",conf.readPathEntry("Command")); 00151 printer->setOption("kde-special-file",conf.readPathEntry("File")); 00152 printer->setOption("kde-special-extension",conf.readEntry("Extension")); 00153 printer->setOption("kde-special-mimetype",conf.readEntry("Mimetype")); 00154 printer->setOption("kde-special-require",conf.readEntry("Require")); 00155 printer->setPixmap(conf.readEntry("Icon","unknown")); 00156 printer->setType(KMPrinter::Special); 00157 if ( !KdeprintChecker::check( &conf ) || 00158 !KXmlCommandManager::self()->checkCommand( printer->option( "kde-special-command" ), 00159 KXmlCommandManager::None, KXmlCommandManager::None, 0 ) ) 00160 printer->addType(KMPrinter::Invalid); 00161 printer->setState(KMPrinter::Idle); 00162 printer->setAcceptJobs(true); 00163 m_mgr->addPrinter(printer); 00164 } 00165 00166 return true; 00167 } 00168 00169 void KMSpecialManager::refresh() 00170 { 00171 if (!m_loaded) 00172 loadPrinters(); 00173 else 00174 { 00175 QPtrListIterator<KMPrinter> it(m_mgr->m_printers); 00176 for (;it.current();++it) 00177 if (it.current()->isSpecial()) 00178 { 00179 it.current()->setDiscarded(false); 00180 it.current()->setType(KMPrinter::Special); 00181 if (KdeprintChecker::check(QStringList::split(',',it.current()->option("kde-special-require"),false))) 00182 it.current()->addType(KMPrinter::Invalid); 00183 } 00184 } 00185 } 00186 00187 KXmlCommand* KMSpecialManager::loadCommand(KMPrinter *pr) 00188 { 00189 KXmlCommand *xmlCmd = loadCommand(pr->option("kde-special-command")); 00190 if (xmlCmd && xmlCmd->driver()) 00191 xmlCmd->driver()->set("text", pr->printerName()); 00192 return xmlCmd; 00193 } 00194 00195 KXmlCommand* KMSpecialManager::loadCommand(const QString& xmlId) 00196 { 00197 return KXmlCommandManager::self()->loadCommand(xmlId, true); 00198 } 00199 00200 DrMain* KMSpecialManager::loadDriver(KMPrinter *pr) 00201 { 00202 KXmlCommand *xmlCmd; 00203 DrMain *driver(0); 00204 00205 if ((xmlCmd=loadCommand(pr)) != 0) 00206 { 00207 driver = xmlCmd->takeDriver(); 00208 delete xmlCmd; 00209 } 00210 00211 return driver; 00212 } 00213 00214 QString KMSpecialManager::setupCommand(const QString& cmd, const QMap<QString,QString>& opts) 00215 { 00216 QString s(cmd); 00217 if (!s.isEmpty()) 00218 { 00219 KXmlCommand *xmlCmd = loadCommand(cmd); 00220 if (xmlCmd) 00221 { 00222 s = xmlCmd->buildCommand(opts, false, false); 00223 delete xmlCmd; 00224 } 00225 } 00226 00227 return s; 00228 }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 20 09:50:00 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003