20 #include "kmspecialmanager.h"
21 #include "kmmanager.h"
22 #include "kmprinter.h"
23 #include "tdeprintcheck.h"
24 #include "kxmlcommand.h"
28 #include <kstandarddirs.h>
29 #include <tdeglobal.h>
30 #include <ksimpleconfig.h>
31 #include <tdelocale.h>
35 #include <sys/types.h>
38 KMSpecialManager::KMSpecialManager(KMManager *parent,
const char *name)
39 : TQObject(parent,name), m_mgr(parent), m_loaded(false)
43 bool KMSpecialManager::savePrinters()
49 confname = locate(
"data",
"tdeprint/specials.desktop");
50 if (confname.startsWith(TDEGlobal::dirs()->localtdedir()))
53 m_mgr->setErrorMsg(i18n(
"A file share/tdeprint/specials.desktop was found in your "
54 "local TDE directory. This file probably comes from a previous TDE "
55 "release and should be removed in order to manage global pseudo "
61 confname = locateLocal(
"data",
"tdeprint/specials.desktop");
63 KSimpleConfig conf(confname);
66 conf.setGroup(
"General");
67 int n = conf.readNumEntry(
"Number",0);
69 conf.deleteGroup(TQString::fromLatin1(
"Printer %1").arg(i),
true);
73 TQPtrListIterator<KMPrinter> it(m_mgr->m_printers);
74 for (;it.current();++it)
76 if (!it.current()->isSpecial() || it.current()->isVirtual())
continue;
77 conf.setGroup(TQString::fromLatin1(
"Printer %1").arg(n));
78 conf.writeEntry(
"Name",it.current()->name());
79 conf.writeEntry(
"Description",it.current()->description());
80 conf.writeEntry(
"Comment",it.current()->location());
81 conf.writePathEntry(
"Command",it.current()->option(
"kde-special-command"));
82 conf.writePathEntry(
"File",it.current()->option(
"kde-special-file"));
83 conf.writeEntry(
"Icon",it.current()->pixmap());
84 conf.writeEntry(
"Extension",it.current()->option(
"kde-special-extension"));
85 conf.writeEntry(
"Mimetype",it.current()->option(
"kde-special-mimetype"));
86 conf.writeEntry(
"Require",it.current()->option(
"kde-special-require"));
89 conf.setGroup(
"General");
90 conf.writeEntry(
"Number",n);
96 ::chmod(TQFile::encodeName(confname), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
105 bool KMSpecialManager::loadPrinters()
107 if (m_loaded)
return true;
110 TQString localDir = TDEGlobal::dirs()->localtdedir();
111 TQStringList files = TDEGlobal::dirs()->findAllResources(
"data",
"tdeprint/specials.desktop");
114 TQStringList orderedFiles;
115 for (TQStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
117 if ((*it).startsWith(localDir))
118 orderedFiles.append(*it);
120 orderedFiles.prepend(*it);
123 for (TQStringList::ConstIterator it=orderedFiles.begin(); it!=orderedFiles.end() && result; ++it)
126 if (getuid() == 0 && (*it).startsWith(localDir))
129 result = loadDesktopFile(*it);
135 bool KMSpecialManager::loadDesktopFile(
const TQString& filename)
137 KSimpleConfig conf(filename);
138 conf.setGroup(
"General");
139 int n = conf.readNumEntry(
"Number",0);
140 for (
int i=0;i<n;i++)
142 TQString grpname = TQString::fromLatin1(
"Printer %1").arg(i);
143 if (!conf.hasGroup(grpname))
continue;
144 conf.setGroup(grpname);
145 KMPrinter *printer =
new KMPrinter;
146 printer->setName(conf.readEntry(
"Name"));
147 printer->setPrinterName(printer->name());
148 printer->setDescription(conf.readEntry(
"Description"));
149 printer->setLocation(conf.readEntry(
"Comment"));
150 printer->setOption(
"kde-special-command",conf.readPathEntry(
"Command"));
151 printer->setOption(
"kde-special-file",conf.readPathEntry(
"File"));
152 printer->setOption(
"kde-special-extension",conf.readEntry(
"Extension"));
153 printer->setOption(
"kde-special-mimetype",conf.readEntry(
"Mimetype"));
154 printer->setOption(
"kde-special-require",conf.readEntry(
"Require"));
155 printer->setPixmap(conf.readEntry(
"Icon",
"unknown"));
156 printer->setType(KMPrinter::Special);
157 if ( !KdeprintChecker::check( &conf ) ||
158 !KXmlCommandManager::self()->checkCommand( printer->option(
"kde-special-command" ),
159 KXmlCommandManager::None, KXmlCommandManager::None, 0 ) )
160 printer->addType(KMPrinter::Invalid);
161 printer->setState(KMPrinter::Idle);
162 printer->setAcceptJobs(
true);
163 m_mgr->addPrinter(printer);
169 void KMSpecialManager::refresh()
175 TQPtrListIterator<KMPrinter> it(m_mgr->m_printers);
176 for (;it.current();++it)
177 if (it.current()->isSpecial())
179 it.current()->setDiscarded(
false);
180 it.current()->setType(KMPrinter::Special);
181 if (KdeprintChecker::check(TQStringList::split(
',',it.current()->option(
"kde-special-require"),
false)))
182 it.current()->addType(KMPrinter::Invalid);
187 KXmlCommand* KMSpecialManager::loadCommand(KMPrinter *pr)
189 KXmlCommand *xmlCmd = loadCommand(pr->option(
"kde-special-command"));
190 if (xmlCmd && xmlCmd->driver())
191 xmlCmd->driver()->set(
"text", pr->printerName());
195 KXmlCommand* KMSpecialManager::loadCommand(
const TQString& xmlId)
197 return KXmlCommandManager::self()->loadCommand(xmlId,
true);
200 DrMain* KMSpecialManager::loadDriver(KMPrinter *pr)
205 if ((xmlCmd=loadCommand(pr)) != 0)
207 driver = xmlCmd->takeDriver();
214 TQString KMSpecialManager::setupCommand(
const TQString& cmd,
const TQMap<TQString,TQString>& opts)
219 KXmlCommand *xmlCmd = loadCommand(cmd);
222 s = xmlCmd->buildCommand(opts,
false,
false);