20 #include "kmvirtualmanager.h"
21 #include "kmprinter.h"
22 #include "kmfactory.h"
23 #include "kmmanager.h"
28 #include <tqtextstream.h>
30 #include <tqfileinfo.h>
31 #include <tdelocale.h>
32 #include <kstandarddirs.h>
35 #include <tdemessagebox.h>
39 static TQString instanceName(
const TQString& prname,
const TQString& instname)
42 if (!instname.isEmpty())
43 str.append(
"/"+instname);
47 KMVirtualManager::KMVirtualManager(KMManager *parent,
const char *name)
48 : TQObject(parent,name), m_manager(parent)
52 KMVirtualManager::~KMVirtualManager()
56 KMPrinter* KMVirtualManager::findPrinter(
const TQString& name)
58 return m_manager->findPrinter(name);
61 KMPrinter* KMVirtualManager::findInstance(KMPrinter *p,
const TQString& name)
63 TQString instname(instanceName(p->printerName(),name));
64 return findPrinter(instname);
67 void KMVirtualManager::addPrinter(KMPrinter *p)
69 if (p && p->isValid())
71 KMPrinter *other = findPrinter(p->name());
80 other->setDefaultOptions(p->defaultOptions());
84 m_manager->addPrinter(p);
90 void KMVirtualManager::setDefault(KMPrinter *p,
bool saveflag)
92 m_manager->setSoftDefault(p);
93 m_defaultprinter = (p ? p->printerName() : TQString::null);
94 if (saveflag) triggerSave();
97 bool KMVirtualManager::isDefault(KMPrinter *p,
const TQString& name)
99 TQString instname(instanceName(p->printerName(),name));
100 KMPrinter *printer = findPrinter(instname);
102 return printer->isSoftDefault();
107 void KMVirtualManager::create(KMPrinter *p,
const TQString& name)
109 TQString instname = instanceName(p->printerName(),name);
110 if (findPrinter(instname) != NULL)
return;
111 KMPrinter *printer =
new KMPrinter;
112 printer->setName(instname);
113 printer->setPrinterName(p->printerName());
114 printer->setInstanceName(name);
116 printer->setType(p->type()|KMPrinter::Virtual);
119 printer->setOptions(p->options());
120 m_manager->addPrinter(printer);
124 void KMVirtualManager::copy(KMPrinter *p,
const TQString& src,
const TQString& name)
126 TQString instsrc(instanceName(p->printerName(),src)), instname(instanceName(p->printerName(),name));
127 KMPrinter *prsrc = findPrinter(instsrc);
128 if (!prsrc || findPrinter(instname) != NULL)
return;
129 KMPrinter *printer =
new KMPrinter;
130 printer->copy(*prsrc);
131 printer->setName(instname);
132 printer->setInstanceName(name);
133 printer->setDefaultOptions(prsrc->defaultOptions());
134 m_manager->addPrinter(printer);
138 void KMVirtualManager::remove(KMPrinter *p,
const TQString& name)
140 TQString instname = instanceName(p->printerName(),name);
141 KMPrinter *printer = findPrinter(instname);
142 if (!printer)
return;
145 printer->setDefaultOptions(TQMap<TQString,TQString>());
146 printer->setEditedOptions(TQMap<TQString,TQString>());
147 printer->setEdited(
false);
150 m_manager->m_printers.removeRef(printer);
154 void KMVirtualManager::setAsDefault(KMPrinter *p,
const TQString& name, TQWidget *parent)
156 TQString instname(instanceName(p->printerName(),name));
158 if ( p->isSpecial() )
160 if ( KMessageBox::warningContinueCancel( parent,
161 i18n(
"<qt>You are about to set a pseudo-printer as your personal default. "
162 "This setting is specific to TDE and will not be available outside TDE "
163 "applications. Note that this will only make your personal default printer "
164 "as undefined for non-TDE applications and should not prevent you from "
165 "printing normally. Do you really want to set <b>%1</b> as your personal default?</qt>" ).arg( instname ),
166 TQString::null, i18n(
"Set as Default"),
"setSpecialAsDefault" ) == KMessageBox::No )
170 KMPrinter *printer = findPrinter(instname);
174 printer = findPrinter(instname);
177 setDefault(printer,
true);
180 void KMVirtualManager::refresh()
182 TQFileInfo fi(TQDir::homeDirPath() + TQFile::decodeName(
"/.cups/lpoptions"));
183 TQFileInfo fi2(TQFile::decodeName(
"/etc/cups/lpoptions"));
187 fi.setFile(fi2.absFilePath());
189 if (!m_checktime.isValid() || m_checktime < TQMAX(fi.lastModified(),fi2.lastModified()))
191 m_defaultprinter = TQString::null;
193 loadFile(fi2.absFilePath());
194 if (fi.exists() && fi.absFilePath() != fi2.absFilePath())
195 loadFile(fi.absFilePath());
196 m_checktime = TQMAX(fi.lastModified(),fi2.lastModified());
201 TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
202 for (;it.current();++it)
203 if (!it.current()->instanceName().isEmpty())
205 checkPrinter(it.current());
206 if (it.current()->isValid()) it.current()->setDiscarded(
false);
211 void KMVirtualManager::checkPrinter(KMPrinter *p)
213 KMPrinter *realprinter = m_manager->findPrinter(p->printerName());
214 if (!realprinter || realprinter->isDiscarded())
216 p->setType(KMPrinter::Invalid);
217 p->setState(KMPrinter::Unknown);
221 if (!p->instanceName().isEmpty())
222 p->setType(realprinter->type()|KMPrinter::Virtual);
223 p->setState(realprinter->state());
227 TQString KMVirtualManager::defaultPrinterName()
229 return m_defaultprinter;
232 void KMVirtualManager::virtualList(TQPtrList<KMPrinter>& list,
const TQString& prname)
238 list.setAutoDelete(
false);
240 kdDebug(500) <<
"KMVirtualManager::virtualList() prname=" << prname << endl;
241 TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
242 for (;it.current();++it)
243 if (it.current()->printerName() == prname)
244 list.append(it.current());
247 void KMVirtualManager::loadFile(
const TQString& filename)
250 if (f.exists() && f.open(IO_ReadOnly))
257 KMPrinter *printer, *realprinter;
261 line = t.readLine().stripWhiteSpace();
262 if (line.isEmpty())
continue;
263 words = TQStringList::split(
' ',line,
false);
264 if (words.count() < 2)
continue;
265 pair = TQStringList::split(
'/',words[1],
false);
266 realprinter = m_manager->findPrinter(KURL::decode_string(pair[0]));
267 if (realprinter && !realprinter->isDiscarded())
271 printer =
new KMPrinter(*realprinter);
272 printer->setName(KURL::decode_string(words[1]));
273 printer->setPrinterName(KURL::decode_string(pair[0]));
274 if (pair.count() > 1)
276 printer->setInstanceName(KURL::decode_string(pair[1]));
277 printer->addType(KMPrinter::Virtual);
280 for (uint i=2; i<words.count(); i++)
282 pair = TQStringList::split(
'=',words[i],
false);
283 printer->setDefaultOption(pair[0],(pair.count() > 1 ? pair[1] : TQString::null));
288 if (words[0].lower().startsWith(
"default"))
289 setDefault(findPrinter(KURL::decode_string(words[1])),
false);
295 void KMVirtualManager::triggerSave()
300 if (TDEStandardDirs::makeDir(TQFile::decodeName(
"/etc/cups")))
301 filename = TQFile::decodeName(
"/etc/cups/lpoptions");
305 TQDir cupsDir(TQDir::home().absPath()+
"/.cups");
306 if (!cupsDir.exists())
307 cupsDir.mkdir(TQDir::home().absPath()+
"/.cups");
308 filename = TQDir::homeDirPath() + TQFile::decodeName(
"/.cups/lpoptions");
311 if (!filename.isEmpty())
314 m_checktime = TQFileInfo(filename).lastModified();
318 void KMVirtualManager::saveFile(
const TQString& filename)
321 if (f.open(IO_WriteOnly))
324 TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
325 for (;it.current();++it)
327 if (it.current()->isSpecial())
329 t << ( it.current()->isSoftDefault() ?
"DefaultSpecial " :
"Special " );
330 t << KURL::encode_string_no_slash( it.current()->printerName() );
331 if ( !it.current()->instanceName().isEmpty() )
332 t <<
"/" << KURL::encode_string_no_slash( it.current()->instanceName() );
335 t << (it.current()->isSoftDefault() ?
"Default " :
"Dest ") << it.current()->name();
336 TQMap<TQString,TQString> opts = it.current()->defaultOptions();
337 for (TQMap<TQString,TQString>::ConstIterator oit=opts.begin(); oit!=opts.end(); ++oit)
339 t <<
' ' << oit.key();
340 if (!oit.data().isEmpty())
341 t <<
'=' << oit.data();
348 bool KMVirtualManager::testInstance(KMPrinter *p)
350 TQString testpage = KMManager::self()->testPage();
351 if (testpage.isEmpty())
363 void KMVirtualManager::reload()
368 void KMVirtualManager::configChanged()
This class is the main interface to access the TDE print framework.
void setPrinterName(const TQString &)
See TQPrinter::setPrinterName().
bool printFiles(const TQStringList &files, bool removeafter=false, bool startviewer=true)
Prints the files given in argument.
void setOptions(const TQMap< TQString, TQString > &opts)
Sets the option set in one operation.
void setSearchName(const TQString &n)
Sets the search name of the KPrinter object.