• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
kmvirtualmanager.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "kmvirtualmanager.h"
21 #include "kmprinter.h"
22 #include "kmfactory.h"
23 #include "kmmanager.h"
24 #include "kprinter.h"
25 
26 #include <stdlib.h>
27 #include <tqfile.h>
28 #include <tqtextstream.h>
29 #include <tqdir.h>
30 #include <tqfileinfo.h>
31 #include <tdelocale.h>
32 #include <kstandarddirs.h>
33 #include <kurl.h>
34 #include <kdebug.h>
35 #include <tdemessagebox.h>
36 
37 #include <unistd.h>
38 
39 static TQString instanceName(const TQString& prname, const TQString& instname)
40 {
41  QString str(prname);
42  if (!instname.isEmpty())
43  str.append("/"+instname);
44  return str;
45 }
46 
47 KMVirtualManager::KMVirtualManager(KMManager *parent, const char *name)
48 : TQObject(parent,name), m_manager(parent)
49 {
50 }
51 
52 KMVirtualManager::~KMVirtualManager()
53 {
54 }
55 
56 KMPrinter* KMVirtualManager::findPrinter(const TQString& name)
57 {
58  return m_manager->findPrinter(name);
59 }
60 
61 KMPrinter* KMVirtualManager::findInstance(KMPrinter *p, const TQString& name)
62 {
63  QString instname(instanceName(p->printerName(),name));
64  return findPrinter(instname);
65 }
66 
67 void KMVirtualManager::addPrinter(KMPrinter *p)
68 {
69  if (p && p->isValid())
70  {
71  KMPrinter *other = findPrinter(p->name());
72  if (other)
73  {
74  other->copy(*p);
75  // Replace default options with the new loaded ones: this is needed
76  // if we want to handle 2 lpoptions correctly (system-wide and local).
77  // Anyway, the virtual printers will be reloaded only if something has
78  // changed in one of the files, so it's better to reset everything, to
79  // be sure to use the new changes. Edited options will be left unchanged.
80  other->setDefaultOptions(p->defaultOptions());
81  delete p;
82  }
83  else
84  m_manager->addPrinter(p);
85  }
86  else
87  delete p;
88 }
89 
90 void KMVirtualManager::setDefault(KMPrinter *p, bool saveflag)
91 {
92  m_manager->setSoftDefault(p);
93  m_defaultprinter = (p ? p->printerName() : TQString::null);
94  if (saveflag) triggerSave();
95 }
96 
97 bool KMVirtualManager::isDefault(KMPrinter *p, const TQString& name)
98 {
99  QString instname(instanceName(p->printerName(),name));
100  KMPrinter *printer = findPrinter(instname);
101  if (printer)
102  return printer->isSoftDefault();
103  else
104  return false;
105 }
106 
107 void KMVirtualManager::create(KMPrinter *p, const TQString& name)
108 {
109  QString 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);
115  if (!name.isEmpty())
116  printer->setType(p->type()|KMPrinter::Virtual);
117  // we need some options to know how to load the driver
118  if (p->isSpecial())
119  printer->setOptions(p->options());
120  m_manager->addPrinter(printer);
121  triggerSave();
122 }
123 
124 void KMVirtualManager::copy(KMPrinter *p, const TQString& src, const TQString& name)
125 {
126  QString 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);
135  triggerSave();
136 }
137 
138 void KMVirtualManager::remove(KMPrinter *p, const TQString& name)
139 {
140  QString instname = instanceName(p->printerName(),name);
141  KMPrinter *printer = findPrinter(instname);
142  if (!printer) return;
143  if (name.isEmpty())
144  { // remove default instance => only remove options, keep the KMPrinter object
145  printer->setDefaultOptions(TQMap<TQString,TQString>());
146  printer->setEditedOptions(TQMap<TQString,TQString>());
147  printer->setEdited(false);
148  }
149  else
150  m_manager->m_printers.removeRef(printer);
151  triggerSave();
152 }
153 
154 void KMVirtualManager::setAsDefault(KMPrinter *p, const TQString& name, TQWidget *parent)
155 {
156  QString instname(instanceName(p->printerName(),name));
157 
158  if ( p->isSpecial() )
159  {
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 )
167  return;
168  }
169 
170  KMPrinter *printer = findPrinter(instname);
171  if (!printer)
172  { // create it if necessary
173  create(p,name);
174  printer = findPrinter(instname);
175  }
176  if (printer)
177  setDefault(printer,true);
178 }
179 
180 void KMVirtualManager::refresh()
181 {
182  TQFileInfo fi(TQDir::homeDirPath() + TQFile::decodeName("/.cups/lpoptions"));
183  TQFileInfo fi2(TQFile::decodeName("/etc/cups/lpoptions"));
184 
185  // if root, then only use global file: trick -> use twice the same file
186  if (getuid() == 0)
187  fi.setFile(fi2.absFilePath());
188 
189  if (!m_checktime.isValid() || m_checktime < TQMAX(fi.lastModified(),fi2.lastModified()))
190  {
191  m_defaultprinter = TQString::null;
192  if (fi2.exists())
193  loadFile(fi2.absFilePath());
194  if (fi.exists() && fi.absFilePath() != fi2.absFilePath())
195  loadFile(fi.absFilePath());
196  m_checktime = TQMAX(fi.lastModified(),fi2.lastModified());
197  }
198  else
199  { // parse printers looking for instances -> undiscarded them, real printers
200  // are undiscarded by the manager itself. Also update printer status.
201  TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
202  for (;it.current();++it)
203  if (!it.current()->instanceName().isEmpty())
204  {
205  checkPrinter(it.current());
206  if (it.current()->isValid()) it.current()->setDiscarded(false);
207  }
208  }
209 }
210 
211 void KMVirtualManager::checkPrinter(KMPrinter *p)
212 {
213  KMPrinter *realprinter = m_manager->findPrinter(p->printerName());
214  if (!realprinter || realprinter->isDiscarded())
215  {
216  p->setType(KMPrinter::Invalid);
217  p->setState(KMPrinter::Unknown);
218  }
219  else
220  {
221  if (!p->instanceName().isEmpty())
222  p->setType(realprinter->type()|KMPrinter::Virtual);
223  p->setState(realprinter->state());
224  }
225 }
226 
227 TQString KMVirtualManager::defaultPrinterName()
228 {
229  return m_defaultprinter;
230 }
231 
232 void KMVirtualManager::virtualList(TQPtrList<KMPrinter>& list, const TQString& prname)
233 {
234  // load printers if necessary
235  refresh();
236 
237  // then look for instances
238  list.setAutoDelete(false);
239  list.clear();
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());
245 }
246 
247 void KMVirtualManager::loadFile(const TQString& filename)
248 {
249  TQFile f(filename);
250  if (f.exists() && f.open(IO_ReadOnly))
251  {
252  TQTextStream t(&f);
253 
254  TQString line;
255  TQStringList words;
256  TQStringList pair;
257  KMPrinter *printer, *realprinter;
258 
259  while (!t.eof())
260  {
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())
268  { // keep only instances corresponding to an existing and
269  // non discarded printer.
270  // "clone" the real printer and modify settings as needed
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)
275  {
276  printer->setInstanceName(KURL::decode_string(pair[1]));
277  printer->addType(KMPrinter::Virtual);
278  }
279  // parse options
280  for (uint i=2; i<words.count(); i++)
281  {
282  pair = TQStringList::split('=',words[i],false);
283  printer->setDefaultOption(pair[0],(pair.count() > 1 ? pair[1] : TQString::null));
284  }
285  // add printer to the manager
286  addPrinter(printer); // don't use "printer" after this point !!!
287  // check default state
288  if (words[0].lower().startsWith("default"))
289  setDefault(findPrinter(KURL::decode_string(words[1])),false);
290  }
291  }
292  }
293 }
294 
295 void KMVirtualManager::triggerSave()
296 {
297  QString filename;
298  if (getuid() == 0)
299  {
300  if (TDEStandardDirs::makeDir(TQFile::decodeName("/etc/cups")))
301  filename = TQFile::decodeName("/etc/cups/lpoptions");
302  }
303  else
304  {
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");
309  }
310 
311  if (!filename.isEmpty())
312  {
313  saveFile(filename);
314  m_checktime = TQFileInfo(filename).lastModified();
315  }
316 }
317 
318 void KMVirtualManager::saveFile(const TQString& filename)
319 {
320  TQFile f(filename);
321  if (f.open(IO_WriteOnly))
322  {
323  TQTextStream t(&f);
324  TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
325  for (;it.current();++it)
326  {
327  if (it.current()->isSpecial())
328  {
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() );
333  }
334  else
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)
338  {
339  t << ' ' << oit.key();
340  if (!oit.data().isEmpty())
341  t << '=' << oit.data();
342  }
343  t << endl;
344  }
345  }
346 }
347 
348 bool KMVirtualManager::testInstance(KMPrinter *p)
349 {
350  TQString testpage = KMManager::self()->testPage();
351  if (testpage.isEmpty())
352  return false;
353  else
354  {
355  KPrinter pr;
356  pr.setPrinterName(p->printerName());
357  pr.setSearchName(p->name());
358  pr.setOptions(p->defaultOptions());
359  return (pr.printFiles(testpage));
360  }
361 }
362 
363 void KMVirtualManager::reload()
364 {
365  reset();
366 }
367 
368 void KMVirtualManager::configChanged()
369 {
370  reset();
371 }
KPrinter::setOptions
void setOptions(const TQMap< TQString, TQString > &opts)
Sets the option set in one operation.
Definition: kprinter.cpp:622
KPrinter
This class is the main interface to access the TDE print framework.
Definition: kprinter.h:88
KPrinter::printFiles
bool printFiles(const TQStringList &files, bool removeafter=false, bool startviewer=true)
Prints the files given in argument.
Definition: kprinter.cpp:358
KPrinter::setPrinterName
void setPrinterName(const TQString &)
See TQPrinter::setPrinterName().
Definition: kprinter.cpp:867
KPrinter::setSearchName
void setSearchName(const TQString &n)
Sets the search name of the KPrinter object.
Definition: kprinter.cpp:903

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.8.8
This website is maintained by Timothy Pearson.