TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
11KB

  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. #include "kmvirtualmanager.h"
  20. #include "kmprinter.h"
  21. #include "kmfactory.h"
  22. #include "kmmanager.h"
  23. #include "kprinter.h"
  24. #include <stdlib.h>
  25. #include <tqfile.h>
  26. #include <tqtextstream.h>
  27. #include <tqdir.h>
  28. #include <tqfileinfo.h>
  29. #include <klocale.h>
  30. #include <kstandarddirs.h>
  31. #include <kurl.h>
  32. #include <kdebug.h>
  33. #include <kmessagebox.h>
  34. #include <unistd.h>
  35. static TQString instanceName(const TQString& prname, const TQString& instname)
  36. {
  37. QString str(prname);
  38. if (!instname.isEmpty())
  39. str.append("/"+instname);
  40. return str;
  41. }
  42. KMVirtualManager::KMVirtualManager(KMManager *parent, const char *name)
  43. : TQObject(parent,name), m_manager(parent)
  44. {
  45. }
  46. KMVirtualManager::~KMVirtualManager()
  47. {
  48. }
  49. KMPrinter* KMVirtualManager::findPrinter(const TQString& name)
  50. {
  51. return m_manager->findPrinter(name);
  52. }
  53. KMPrinter* KMVirtualManager::findInstance(KMPrinter *p, const TQString& name)
  54. {
  55. QString instname(instanceName(p->printerName(),name));
  56. return findPrinter(instname);
  57. }
  58. void KMVirtualManager::addPrinter(KMPrinter *p)
  59. {
  60. if (p && p->isValid())
  61. {
  62. KMPrinter *other = findPrinter(p->name());
  63. if (other)
  64. {
  65. other->copy(*p);
  66. // Replace default options with the new loaded ones: this is needed
  67. // if we want to handle 2 lpoptions correctly (system-wide and local).
  68. // Anyway, the virtual printers will be reloaded only if something has
  69. // changed in one of the files, so it's better to reset everything, to
  70. // be sure to use the new changes. Edited options will be left unchanged.
  71. other->setDefaultOptions(p->defaultOptions());
  72. delete p;
  73. }
  74. else
  75. m_manager->addPrinter(p);
  76. }
  77. else
  78. delete p;
  79. }
  80. void KMVirtualManager::setDefault(KMPrinter *p, bool saveflag)
  81. {
  82. m_manager->setSoftDefault(p);
  83. m_defaultprinter = (p ? p->printerName() : TQString::null);
  84. if (saveflag) triggerSave();
  85. }
  86. bool KMVirtualManager::isDefault(KMPrinter *p, const TQString& name)
  87. {
  88. QString instname(instanceName(p->printerName(),name));
  89. KMPrinter *printer = findPrinter(instname);
  90. if (printer)
  91. return printer->isSoftDefault();
  92. else
  93. return false;
  94. }
  95. void KMVirtualManager::create(KMPrinter *p, const TQString& name)
  96. {
  97. QString instname = instanceName(p->printerName(),name);
  98. if (findPrinter(instname) != NULL) return;
  99. KMPrinter *printer = new KMPrinter;
  100. printer->setName(instname);
  101. printer->setPrinterName(p->printerName());
  102. printer->setInstanceName(name);
  103. if (!name.isEmpty())
  104. printer->setType(p->type()|KMPrinter::Virtual);
  105. // we need some options to know how to load the driver
  106. if (p->isSpecial())
  107. printer->setOptions(p->options());
  108. m_manager->addPrinter(printer);
  109. triggerSave();
  110. }
  111. void KMVirtualManager::copy(KMPrinter *p, const TQString& src, const TQString& name)
  112. {
  113. QString instsrc(instanceName(p->printerName(),src)), instname(instanceName(p->printerName(),name));
  114. KMPrinter *prsrc = findPrinter(instsrc);
  115. if (!prsrc || findPrinter(instname) != NULL) return;
  116. KMPrinter *printer = new KMPrinter;
  117. printer->copy(*prsrc);
  118. printer->setName(instname);
  119. printer->setInstanceName(name);
  120. printer->setDefaultOptions(prsrc->defaultOptions());
  121. m_manager->addPrinter(printer);
  122. triggerSave();
  123. }
  124. void KMVirtualManager::remove(KMPrinter *p, const TQString& name)
  125. {
  126. QString instname = instanceName(p->printerName(),name);
  127. KMPrinter *printer = findPrinter(instname);
  128. if (!printer) return;
  129. if (name.isEmpty())
  130. { // remove default instance => only remove options, keep the KMPrinter object
  131. printer->setDefaultOptions(TQMap<TQString,TQString>());
  132. printer->setEditedOptions(TQMap<TQString,TQString>());
  133. printer->setEdited(false);
  134. }
  135. else
  136. m_manager->m_printers.removeRef(printer);
  137. triggerSave();
  138. }
  139. void KMVirtualManager::setAsDefault(KMPrinter *p, const TQString& name, TQWidget *parent)
  140. {
  141. QString instname(instanceName(p->printerName(),name));
  142. if ( p->isSpecial() )
  143. {
  144. if ( KMessageBox::warningContinueCancel( parent,
  145. i18n( "<qt>You are about to set a pseudo-printer as your personal default. "
  146. "This setting is specific to TDE and will not be available outside TDE "
  147. "applications. Note that this will only make your personal default printer "
  148. "as undefined for non-TDE applications and should not prevent you from "
  149. "printing normally. Do you really want to set <b>%1</b> as your personal default?</qt>" ).arg( instname ),
  150. TQString::null, i18n("Set as Default"), "setSpecialAsDefault" ) == KMessageBox::No )
  151. return;
  152. }
  153. KMPrinter *printer = findPrinter(instname);
  154. if (!printer)
  155. { // create it if necessary
  156. create(p,name);
  157. printer = findPrinter(instname);
  158. }
  159. if (printer)
  160. setDefault(printer,true);
  161. }
  162. void KMVirtualManager::refresh()
  163. {
  164. TQFileInfo fi(TQDir::homeDirPath() + TQFile::decodeName("/.cups/lpoptions"));
  165. TQFileInfo fi2(TQFile::decodeName("/etc/cups/lpoptions"));
  166. // if root, then only use global file: trick -> use twice the same file
  167. if (getuid() == 0)
  168. fi.setFile(fi2.absFilePath());
  169. if (!m_checktime.isValid() || m_checktime < QMAX(fi.lastModified(),fi2.lastModified()))
  170. {
  171. m_defaultprinter = TQString::null;
  172. if (fi2.exists())
  173. loadFile(fi2.absFilePath());
  174. if (fi.exists() && fi.absFilePath() != fi2.absFilePath())
  175. loadFile(fi.absFilePath());
  176. m_checktime = QMAX(fi.lastModified(),fi2.lastModified());
  177. }
  178. else
  179. { // parse printers looking for instances -> undiscarded them, real printers
  180. // are undiscarded by the manager itself. Also update printer status.
  181. TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
  182. for (;it.current();++it)
  183. if (!it.current()->instanceName().isEmpty())
  184. {
  185. checkPrinter(it.current());
  186. if (it.current()->isValid()) it.current()->setDiscarded(false);
  187. }
  188. }
  189. }
  190. void KMVirtualManager::checkPrinter(KMPrinter *p)
  191. {
  192. KMPrinter *realprinter = m_manager->findPrinter(p->printerName());
  193. if (!realprinter || realprinter->isDiscarded())
  194. {
  195. p->setType(KMPrinter::Invalid);
  196. p->setState(KMPrinter::Unknown);
  197. }
  198. else
  199. {
  200. if (!p->instanceName().isEmpty())
  201. p->setType(realprinter->type()|KMPrinter::Virtual);
  202. p->setState(realprinter->state());
  203. }
  204. }
  205. TQString KMVirtualManager::defaultPrinterName()
  206. {
  207. return m_defaultprinter;
  208. }
  209. void KMVirtualManager::virtualList(TQPtrList<KMPrinter>& list, const TQString& prname)
  210. {
  211. // load printers if necessary
  212. refresh();
  213. // then look for instances
  214. list.setAutoDelete(false);
  215. list.clear();
  216. kdDebug(500) << "KMVirtualManager::virtualList() prname=" << prname << endl;
  217. TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
  218. for (;it.current();++it)
  219. if (it.current()->printerName() == prname)
  220. list.append(it.current());
  221. }
  222. void KMVirtualManager::loadFile(const TQString& filename)
  223. {
  224. TQFile f(filename);
  225. if (f.exists() && f.open(IO_ReadOnly))
  226. {
  227. TQTextStream t(&f);
  228. TQString line;
  229. TQStringList words;
  230. TQStringList pair;
  231. KMPrinter *printer, *realprinter;
  232. while (!t.eof())
  233. {
  234. line = t.readLine().stripWhiteSpace();
  235. if (line.isEmpty()) continue;
  236. words = TQStringList::split(' ',line,false);
  237. if (words.count() < 2) continue;
  238. pair = TQStringList::split('/',words[1],false);
  239. realprinter = m_manager->findPrinter(KURL::decode_string(pair[0]));
  240. if (realprinter && !realprinter->isDiscarded())
  241. { // keep only instances corresponding to an existing and
  242. // non discarded printer.
  243. // "clone" the real printer and modify settings as needed
  244. printer = new KMPrinter(*realprinter);
  245. printer->setName(KURL::decode_string(words[1]));
  246. printer->setPrinterName(KURL::decode_string(pair[0]));
  247. if (pair.count() > 1)
  248. {
  249. printer->setInstanceName(KURL::decode_string(pair[1]));
  250. printer->addType(KMPrinter::Virtual);
  251. }
  252. // parse options
  253. for (uint i=2; i<words.count(); i++)
  254. {
  255. pair = TQStringList::split('=',words[i],false);
  256. printer->setDefaultOption(pair[0],(pair.count() > 1 ? pair[1] : TQString::null));
  257. }
  258. // add printer to the manager
  259. addPrinter(printer); // don't use "printer" after this point !!!
  260. // check default state
  261. if (words[0].lower().startsWith("default"))
  262. setDefault(findPrinter(KURL::decode_string(words[1])),false);
  263. }
  264. }
  265. }
  266. }
  267. void KMVirtualManager::triggerSave()
  268. {
  269. QString filename;
  270. if (getuid() == 0)
  271. {
  272. if (KStandardDirs::makeDir(TQFile::decodeName("/etc/cups")))
  273. filename = TQFile::decodeName("/etc/cups/lpoptions");
  274. }
  275. else
  276. {
  277. TQDir cupsDir(TQDir::home().absPath()+"/.cups");
  278. if (!cupsDir.exists())
  279. cupsDir.mkdir(TQDir::home().absPath()+"/.cups");
  280. filename = TQDir::homeDirPath() + TQFile::decodeName("/.cups/lpoptions");
  281. }
  282. if (!filename.isEmpty())
  283. {
  284. saveFile(filename);
  285. m_checktime = TQFileInfo(filename).lastModified();
  286. }
  287. }
  288. void KMVirtualManager::saveFile(const TQString& filename)
  289. {
  290. TQFile f(filename);
  291. if (f.open(IO_WriteOnly))
  292. {
  293. TQTextStream t(&f);
  294. TQPtrListIterator<KMPrinter> it(m_manager->m_printers);
  295. for (;it.current();++it)
  296. {
  297. if (it.current()->isSpecial())
  298. {
  299. t << ( it.current()->isSoftDefault() ? "DefaultSpecial " : "Special " );
  300. t << KURL::encode_string_no_slash( it.current()->printerName() );
  301. if ( !it.current()->instanceName().isEmpty() )
  302. t << "/" << KURL::encode_string_no_slash( it.current()->instanceName() );
  303. }
  304. else
  305. t << (it.current()->isSoftDefault() ? "Default " : "Dest ") << it.current()->name();
  306. TQMap<TQString,TQString> opts = it.current()->defaultOptions();
  307. for (TQMap<TQString,TQString>::ConstIterator oit=opts.begin(); oit!=opts.end(); ++oit)
  308. {
  309. t << ' ' << oit.key();
  310. if (!oit.data().isEmpty())
  311. t << '=' << oit.data();
  312. }
  313. t << endl;
  314. }
  315. }
  316. }
  317. bool KMVirtualManager::testInstance(KMPrinter *p)
  318. {
  319. TQString testpage = KMManager::self()->testPage();
  320. if (testpage.isEmpty())
  321. return false;
  322. else
  323. {
  324. KPrinter pr;
  325. pr.setPrinterName(p->printerName());
  326. pr.setSearchName(p->name());
  327. pr.setOptions(p->defaultOptions());
  328. return (pr.printFiles(testpage));
  329. }
  330. }
  331. void KMVirtualManager::reload()
  332. {
  333. reset();
  334. }
  335. void KMVirtualManager::configChanged()
  336. {
  337. reset();
  338. }