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

tdeprint

  • tdeprint
  • cups
  • cupsdconf2
cupsdconf.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 <config.h>
21 #include "cupsdconf.h"
22 
23 #include <tqfile.h>
24 #include <tqregexp.h>
25 #include <tdelocale.h>
26 #include <kdebug.h>
27 #include <tdeconfig.h>
28 
29 #include <stdlib.h>
30 #include <cups/cups.h>
31 #include <cups/ipp.h>
32 #include <cups/language.h>
33 
34 TQString findDir(const TQStringList& list)
35 {
36  for (TQStringList::ConstIterator it=list.begin(); it!=list.end(); ++it)
37  if (TQFile::exists(*it))
38  return *it;
39  // if nothing found, just use the first as default value
40  return list[0];
41 }
42 
43 void splitSizeSpec(const TQString& s, int& sz, int& suff)
44 {
45  int p = s.find(TQRegExp("\\D"));
46  sz = s.mid(0, p).toInt();
47  if (p != -1)
48  {
49  switch (s[p].latin1())
50  {
51  case 'k': suff = UNIT_KB; break;
52  default:
53  case 'm': suff = UNIT_MB; break;
54  case 'g': suff = UNIT_GB; break;
55  case 't': suff = UNIT_TILE; break;
56  }
57  }
58  else
59  suff = UNIT_MB;
60 }
61 
62 CupsdConf::CupsdConf()
63 {
64  // start by trying to find CUPS directories (useful later)
65  datadir_ = findDir(TQStringList("/usr/share/cups")
66  << "/usr/local/share/cups"
67  << "/opt/share/cups"
68  << "/opt/local/share/cups");
69  documentdir_ = findDir(TQStringList(datadir_+"/doc-root")
70  << datadir_.left(datadir_.length()-5)+"/doc/cups");
71  //fontpath_ << (datadir_+"/fonts");
72  requestdir_ = findDir(TQStringList("/var/spool/cups")
73  << "/var/cups");
74  serverbin_ = findDir(TQStringList()
75  << "/usr/" SYSTEM_LIBDIR "/cups"
76  << "/usr/lib/cups"
77  << "/usr/local/" SYSTEM_LIBDIR "/cups"
78  << "/usr/local/lib/cups"
79  << "/opt/" SYSTEM_LIBDIR "/cups"
80  << "/opt/lib/cups"
81  << "/opt/local/" SYSTEM_LIBDIR "/cups"
82  << "/opt/local/lib/cups");
83  serverfiles_ = findDir(TQStringList("/etc/cups")
84  << "/usr/local/etc/cups");
85  tmpfiles_ = requestdir_+"/tmp";
86 
87  // other options
88  servername_ = TQString::null;
89  serveradmin_ = TQString::null;
90  classification_ = CLASS_NONE;
91  otherclassname_ = TQString::null;
92  classoverride_ = false;
93  charset_ = "utf-8";
94  language_ = "en";
95  printcap_ = "/etc/printcap";
96  printcapformat_ = PRINTCAP_BSD;
97  remoteroot_ = "remroot";
98  systemgroup_ = "lpadmin";
99  encryptcert_ = serverfiles_+"/ssl/server.crt";
100  encryptkey_ = serverfiles_+"/ssl/server.key";
101  hostnamelookup_ = HOSTNAME_OFF;
102  keepalive_ = true;
103  keepalivetimeout_ = 60;
104  maxclients_ = 100;
105  maxrequestsize_ = "0";
106  clienttimeout_ = 300;
107  // listenaddresses_
108  TQString logdir = findDir(TQStringList("/var/log/cups")
109  << "/var/spool/cups/log"
110  << "/var/cups/log");
111  accesslog_ = logdir+"/access_log";
112  errorlog_ = logdir+"/error_log";
113  pagelog_ = logdir+"/page_log";
114  maxlogsize_ = "1m";
115  loglevel_ = LOGLEVEL_INFO;
116  keepjobhistory_ = true;
117  keepjobfiles_ = false;
118  autopurgejobs_ = false;
119  maxjobs_ = 0;
120  maxjobsperprinter_ = 0;
121  maxjobsperuser_ = 0;
122  user_ = "lp";
123  group_ = "lpadmin";
124  ripcache_ = "8m";
125  filterlimit_ = 0;
126  browsing_ = true;
127  browseprotocols_ << "CUPS";
128  browseport_ = ippPort();
129  browseinterval_ = 30;
130  browsetimeout_ = 300;
131  // browseaddresses_
132  browseorder_ = ORDER_ALLOW_DENY;
133  useimplicitclasses_ = true;
134  hideimplicitmembers_ = true;
135  useshortnames_ = true;
136  useanyclasses_ = false;
137 
138  loadAvailableResources();
139 }
140 
141 CupsdConf::~CupsdConf()
142 {
143 }
144 
145 bool CupsdConf::loadFromFile(const TQString& filename)
146 {
147  TQFile f(filename);
148  if (!f.exists() || !f.open(IO_ReadOnly)) return false;
149  else
150  {
151  TQTextStream t(&f);
152  TQString line;
153  bool done(false), value(true);
154  while (!done && value)
155  {
156  line = t.readLine().simplifyWhiteSpace();
157  if (line.isEmpty())
158  {
159  if (t.atEnd()) done = true;
160  else continue;
161  }
162  else if (line[0] == '#') continue;
163  else if (line.left(9).lower() == "<location")
164  {
165  CupsLocation *location = new CupsLocation();
166  locations_.append(location);
167  if (!location->parseResource(line) || !parseLocation(location, t))
168  value = false;
169  // search corresponding resource
170  for (resources_.first();resources_.current();resources_.next())
171  if (resources_.current()->path_ == location->resourcename_)
172  location->resource_ = resources_.current();
173  }
174  else value = parseOption(line);
175  }
176  f.close();
177  return value;
178  }
179 }
180 
181 bool CupsdConf::saveToFile(const TQString& filename)
182 {
183  TQFile f(filename);
184  if (!f.open(IO_WriteOnly))
185  return false;
186  else
187  {
188  TQTextStream t(&f);
189  t << comments_["header"] << endl;
190  t << "# Server" << endl << endl;
191 
192  t << comments_["servername"] << endl;
193  if ( !servername_.isEmpty() )
194  t << "ServerName " << servername_ << endl;
195 
196  t << endl << comments_["serveradmin"] << endl;
197  if ( !serveradmin_.isEmpty() )
198  t << "ServerAdmin " << serveradmin_ << endl;
199 
200  t << endl << comments_["classification"] << endl;
201  t << "Classification ";
202  switch (classification_)
203  {
204  default:
205  case CLASS_NONE: t << "none"; break;
206  case CLASS_CLASSIFIED: t << "classified"; break;
207  case CLASS_CONFIDENTIAL: t << "confidential"; break;
208  case CLASS_SECRET: t << "secret"; break;
209  case CLASS_TOPSECRET: t << "topsecret"; break;
210  case CLASS_UNCLASSIFIED: t << "unclassified"; break;
211  case CLASS_OTHER: t << otherclassname_; break;
212  }
213  t << endl;
214 
215  t << endl << comments_["classifyoverride"] << endl;
216  if (classification_ != CLASS_NONE) t << "ClassifyOverride " << (classoverride_ ? "Yes" : "No") << endl;
217 
218  t << endl << comments_["defaultcharset"] << endl;
219  t << "DefaultCharset " << charset_.upper() << endl;
220 
221  t << endl << comments_["defaultlanguage"] << endl;
222  t << "DefaultLanguage " << language_.lower() << endl;
223 
224  t << endl << comments_["printcap"] << endl;
225  t << "Printcap " << printcap_ << endl;
226 
227  t << endl << comments_["printcapformat"] << endl;
228  t << "PrintcapFormat " << (printcapformat_ == PRINTCAP_SOLARIS ? "Solaris" : "BSD") << endl;
229 
230  t << endl << "# Security" << endl;
231  t << endl << comments_["remoteroot"] << endl;
232  t << "RemoteRoot " << remoteroot_ << endl;
233 
234  t << endl << comments_["systemgroup"] << endl;
235  t << "SystemGroup " << systemgroup_ << endl;
236 
237  t << endl << comments_["servercertificate"] << endl;
238  t << "ServerCertificate " << encryptcert_ << endl;
239 
240  t << endl << comments_["serverkey"] << endl;
241  t << "ServerKey " << encryptkey_ << endl;
242 
243  t << endl << comments_["locations"] << endl;
244  for (locations_.first(); locations_.current(); locations_.next())
245  {
246  CupsLocation *loc = locations_.current();
247  t << "<Location " << loc->resourcename_ << ">" << endl;
248  if (loc->authtype_ != AUTHTYPE_NONE)
249  {
250  t << "AuthType ";
251  switch (loc->authtype_)
252  {
253  case AUTHTYPE_BASIC: t << "Basic"; break;
254  case AUTHTYPE_DIGEST: t << "Digest"; break;
255  }
256  t << endl;
257  }
258  if (loc->authclass_ != AUTHCLASS_ANONYMOUS)
259  {
260  switch (loc->authclass_)
261  {
262  case AUTHCLASS_USER:
263  if (!loc->authname_.isEmpty())
264  t << "Require user " << loc->authname_ << endl;
265  else
266  t << "AuthClass User" << endl;
267  break;
268  case AUTHCLASS_GROUP:
269  if (!loc->authname_.isEmpty())
270  t << "Require group " << loc->authname_ << endl;
271  else
272  t << "AuthClass Group" << endl;
273  break;
274  case AUTHCLASS_SYSTEM:
275  t << "AuthClass System" << endl;
276  break;
277  }
278  }
279  t << "Encryption ";
280  switch (loc->encryption_)
281  {
282  case ENCRYPT_ALWAYS: t << "Always"; break;
283  case ENCRYPT_NEVER: t << "Never"; break;
284  case ENCRYPT_REQUIRED: t << "Required"; break;
285  default:
286  case ENCRYPT_IFREQUESTED: t << "IfRequested"; break;
287  }
288  t << endl;
289  t << "Satisfy " << (loc->satisfy_ == SATISFY_ALL ? "All" : "Any") << endl;
290  t << "Order " << (loc->order_ == ORDER_ALLOW_DENY ? "allow,deny" : "deny,allow") << endl;
291  for (TQStringList::ConstIterator it=loc->addresses_.begin(); it!=loc->addresses_.end(); ++it)
292  t << *it << endl;
293  t << "</Location>" << endl;
294  }
295 
296  t << endl << "# Network" << endl;
297  t << endl << comments_["hostnamelookups"] << endl;
298  t << "HostnameLookups ";
299  switch (hostnamelookup_)
300  {
301  default:
302  case HOSTNAME_OFF: t << "Off"; break;
303  case HOSTNAME_ON: t << "On"; break;
304  case HOSTNAME_DOUBLE: t << "Double"; break;
305  }
306  t << endl;
307 
308  t << endl << comments_["keepalive"] << endl;
309  t << "KeepAlive " << (keepalive_ ? "On" : "Off") << endl;
310 
311  t << endl << comments_["keepalivetimeout"] << endl;
312  t << "KeepAliveTimeout " << keepalivetimeout_ << endl;
313 
314  t << endl << comments_["maxclients"] << endl;
315  t << "MaxClients " << maxclients_ << endl;
316 
317  t << endl << comments_["maxrequestsize"] << endl;
318  t << "MaxRequestSize " << maxrequestsize_ << endl;
319 
320  t << endl << comments_["timeout"] << endl;
321  t << "Timeout " << clienttimeout_ << endl;
322 
323  t << endl << comments_["listen"] << endl;
324  for (TQStringList::ConstIterator it=listenaddresses_.begin(); it!=listenaddresses_.end(); ++it)
325  t << *it << endl;
326 
327  t << endl << "# Log" << endl;
328  t << endl << comments_["accesslog"] << endl;
329  t << "AccessLog " << accesslog_ << endl;
330 
331  t << endl << comments_["errorlog"] << endl;
332  t << "ErrorLog " << errorlog_ << endl;
333 
334  t << endl << comments_["pagelog"] << endl;
335  t << "PageLog " << pagelog_ << endl;
336 
337  t << endl << comments_["maxlogsize"] << endl;
338  //t << "MaxLogSize " << maxlogsize_ << "m" << endl;
339  t << "MaxLogSize " << maxlogsize_ << endl;
340 
341  t << endl << comments_["loglevel"] << endl;
342  t << "LogLevel ";
343  switch (loglevel_)
344  {
345  case LOGLEVEL_NONE: t << "none"; break;
346  default:
347  case LOGLEVEL_INFO: t << "info"; break;
348  case LOGLEVEL_ERROR: t << "error"; break;
349  case LOGLEVEL_WARN: t << "warn"; break;
350  case LOGLEVEL_DEBUG: t << "debug"; break;
351  case LOGLEVEL_DEBUG2: t << "debug2"; break;
352  }
353  t << endl;
354 
355  t << endl << "# Jobs" << endl;
356  t << endl << comments_["preservejobhistory"] << endl;
357  t << "PreserveJobHistory " << (keepjobhistory_ ? "On" : "Off") << endl;
358 
359  t << endl << comments_["preservejobfiles"] << endl;
360  if (keepjobhistory_) t << "PreserveJobFiles " << (keepjobfiles_ ? "On" : "Off") << endl;
361 
362  t << endl << comments_["autopurgejobs"] << endl;
363  if (keepjobhistory_) t << "AutoPurgeJobs " << (autopurgejobs_ ? "Yes" : "No") << endl;
364 
365  t << endl << comments_["maxjobs"] << endl;
366  t << "MaxJobs " << maxjobs_ << endl;
367 
368  t << endl << comments_["maxjobsperprinter"] << endl;
369  t << "MaxJobsPerPrinter " << maxjobsperprinter_ << endl;
370 
371  t << endl << comments_["maxjobsperuser"] << endl;
372  t << "MaxJobsPerUser " << maxjobsperuser_ << endl;
373 
374  t << endl << "# Filter" << endl;
375  t << endl << comments_["user"] << endl;
376  t << "User " << user_ << endl;
377 
378  t << endl << comments_["group"] << endl;
379  t << "Group " << group_ << endl;
380 
381  t << endl << comments_["ripcache"] << endl;
382  t << "RIPCache " << ripcache_ << endl;
383 
384  t << endl << comments_["filterlimit"] << endl;
385  t << "FilterLimit " << filterlimit_ << endl;
386 
387  t << endl << "# Directories" << endl;
388  t << endl << comments_["datadir"] << endl;
389  t << "DataDir " << datadir_ << endl;
390 
391  t << endl << comments_["documentroot"] << endl;
392  t << "DocumentRoot " << documentdir_ << endl;
393 
394  t << endl << comments_["fontpath"] << endl;
395  for (TQStringList::ConstIterator it=fontpath_.begin(); it!=fontpath_.end(); ++it)
396  t << "FontPath " << *it << endl;
397 
398  t << endl << comments_["requestroot"] << endl;
399  t << "RequestRoot " << requestdir_ << endl;
400 
401  t << endl << comments_["serverbin"] << endl;
402  t << "ServerBin " << serverbin_ << endl;
403 
404  t << endl << comments_["serverroot"] << endl;
405  t << "ServerRoot " << serverfiles_ << endl;
406 
407  t << endl << comments_["tempdir"] << endl;
408  t << "TempDir " << tmpfiles_ << endl;
409 
410  t << endl << "# Browsing" << endl;
411  t << endl << comments_["browsing"] << endl;
412  t << "Browsing " << (browsing_ ? "On" : "Off") << endl;
413 
414  t << endl << comments_["browseprotocols"] << endl;
415  if (browsing_)
416  {
417  t << "BrowseProtocols ";
418  for (TQStringList::ConstIterator it=browseprotocols_.begin(); it!=browseprotocols_.end(); ++it)
419  t << (*it).upper() << " ";
420  t << endl;
421  }
422 
423  t << endl << comments_["browseport"] << endl;
424  if (browsing_) t << "BrowsePort " << browseport_ << endl;
425 
426  t << endl << comments_["browseinterval"] << endl;
427  if (browsing_) t << "BrowseInterval " << browseinterval_ << endl;
428 
429  t << endl << comments_["browsetimeout"] << endl;
430  if (browsing_) t << "BrowseTimeout " << browsetimeout_ << endl;
431 
432  t << endl << comments_["browseaddress"] << endl;
433  if (browsing_)
434  for (TQStringList::ConstIterator it=browseaddresses_.begin(); it!=browseaddresses_.end(); ++it)
435  if ((*it).startsWith("Send"))
436  t << "BrowseAddress " << (*it).mid(5) << endl;
437  else
438  t << "Browse" << (*it) << endl;
439 
440  t << endl << comments_["browseorder"] << endl;
441  if (browsing_) t << "BrowseOrder " << (browseorder_ == ORDER_ALLOW_DENY ? "allow,deny" : "deny,allow") << endl;
442 
443  t << endl << comments_["implicitclasses"] << endl;
444  if (browsing_) t << "ImplicitClasses " << (useimplicitclasses_ ? "On" : "Off") << endl;
445 
446  t << endl << comments_["implicitanyclasses"] << endl;
447  if (browsing_) t << "ImplicitAnyClasses " << (useanyclasses_ ? "On" : "Off") << endl;
448 
449  t << endl << comments_["hideimplicitmembers"] << endl;
450  if (browsing_) t << "HideImplicitMembers " << (hideimplicitmembers_ ? "Yes" : "No") << endl;
451 
452  t << endl << comments_["browseshortnames"] << endl;
453  if (browsing_) t << "BrowseShortNames " << (useshortnames_ ? "Yes" : "No") << endl;
454 
455  t << endl << "# Unknown" << endl;
456  for (TQValueList< TQPair<TQString,TQString> >::ConstIterator it=unknown_.begin(); it!=unknown_.end(); ++it)
457  t << (*it).first << " " << (*it).second << endl;
458 
459  return true;
460  }
461 }
462 
463 bool CupsdConf::parseLocation(CupsLocation *location, TQTextStream& file)
464 {
465  TQString line;
466  bool done(false);
467  bool value(true);
468  while (!done && value)
469  {
470  line = file.readLine().simplifyWhiteSpace();
471  if (line.isEmpty())
472  {
473  if (file.atEnd())
474  {
475  value = false;
476  done = true;
477  }
478  else continue;
479  }
480  else if (line[0] == '#') continue;
481  else if (line.lower() == "</location>") done = true;
482  else value = location->parseOption(line);
483  }
484  return value;
485 }
486 
487 bool CupsdConf::parseOption(const TQString& line)
488 {
489  int p(-1);
490  TQString keyword, value, l(line.simplifyWhiteSpace());
491 
492  if ((p=l.find(' ')) != -1)
493  {
494  keyword = l.left(p).lower();
495  value = l.mid(p+1);
496  }
497  else
498  {
499  keyword = l.lower();
500  }
501 
502  //kdDebug() << "cupsd.conf keyword=" << keyword << endl;
503  if (keyword == "accesslog") accesslog_ = value;
504  else if (keyword == "autopurgejobs") autopurgejobs_ = (value.lower() == "yes");
505  else if (keyword == "browseaddress") browseaddresses_.append("Send "+value);
506  else if (keyword == "browseallow") browseaddresses_.append("Allow "+value);
507  else if (keyword == "browsedeny") browseaddresses_.append("Deny "+value);
508  else if (keyword == "browseinterval") browseinterval_ = value.toInt();
509  else if (keyword == "browseorder") browseorder_ = (value.lower() == "deny,allow" ? ORDER_DENY_ALLOW : ORDER_ALLOW_DENY);
510  else if (keyword == "browsepoll") browseaddresses_.append("Poll "+value);
511  else if (keyword == "browseport") browseport_ = value.toInt();
512  else if (keyword == "browseprotocols")
513  {
514  browseprotocols_.clear();
515  TQStringList prots = TQStringList::split(TQRegExp("\\s"), value, false);
516  if (prots.find("all") != prots.end())
517  browseprotocols_ << "CUPS" << "SLP";
518  else
519  for (TQStringList::ConstIterator it=prots.begin(); it!=prots.end(); ++it)
520  browseprotocols_ << (*it).upper();
521  }
522  else if (keyword == "browserelay") browseaddresses_.append("Relay "+value);
523  else if (keyword == "browseshortnames") useshortnames_ = (value.lower() != "no");
524  else if (keyword == "browsetimeout") browsetimeout_ = value.toInt();
525  else if (keyword == "browsing") browsing_ = (value.lower() != "off");
526  else if (keyword == "classification")
527  {
528  TQString cl = value.lower();
529  if (cl == "none") classification_ = CLASS_NONE;
530  else if (cl == "classified") classification_ = CLASS_CLASSIFIED;
531  else if (cl == "confidential") classification_ = CLASS_CONFIDENTIAL;
532  else if (cl == "secret") classification_ = CLASS_SECRET;
533  else if (cl == "topsecret") classification_ = CLASS_TOPSECRET;
534  else if (cl == "unclassified") classification_ = CLASS_UNCLASSIFIED;
535  else
536  {
537  classification_ = CLASS_OTHER;
538  otherclassname_ = cl;
539  }
540  }
541  else if (keyword == "classifyoverride") classoverride_ = (value.lower() == "yes");
542  else if (keyword == "datadir") datadir_ = value;
543  else if (keyword == "defaultcharset") charset_ = value;
544  else if (keyword == "defaultlanguage") language_ = value;
545  else if (keyword == "documentroot") documentdir_ = value;
546  else if (keyword == "errorlog") errorlog_ = value;
547  else if (keyword == "filterlimit") filterlimit_ = value.toInt();
548  else if (keyword == "fontpath") fontpath_ += TQStringList::split(':', value, false);
549  else if (keyword == "group") group_ = value;
550  else if (keyword == "hideimplicitmembers") hideimplicitmembers_ = (value.lower() != "no");
551  else if (keyword == "hostnamelookups")
552  {
553  TQString h = value.lower();
554  if (h == "on") hostnamelookup_ = HOSTNAME_ON;
555  else if (h == "double") hostnamelookup_ = HOSTNAME_DOUBLE;
556  else hostnamelookup_ = HOSTNAME_OFF;
557  }
558  else if (keyword == "implicitclasses") useimplicitclasses_ = (value.lower() != "off");
559  else if (keyword == "implicitanyclasses") useanyclasses_ = (value.lower() == "on");
560  else if (keyword == "keepalive") keepalive_ = (value.lower() != "off");
561  else if (keyword == "keepalivetimeout") keepalivetimeout_ = value.toInt();
562  else if (keyword == "listen") listenaddresses_.append("Listen "+value);
563  else if (keyword == "loglevel")
564  {
565  TQString ll = value.lower();
566  if (ll == "none") loglevel_ = LOGLEVEL_NONE;
567  else if (ll == "error") loglevel_ = LOGLEVEL_ERROR;
568  else if (ll == "warn") loglevel_ = LOGLEVEL_WARN;
569  else if (ll == "info") loglevel_ = LOGLEVEL_INFO;
570  else if (ll == "debug") loglevel_ = LOGLEVEL_DEBUG;
571  else if (ll == "debug2") loglevel_ = LOGLEVEL_DEBUG2;
572  }
573  else if (keyword == "maxclients") maxclients_ = value.toInt();
574  else if (keyword == "maxjobs") maxjobs_ = value.toInt();
575  else if (keyword == "maxjobsperprinter") maxjobsperprinter_ = value.toInt();
576  else if (keyword == "maxjobsperuser") maxjobsperuser_ = value.toInt();
577  else if (keyword == "maxrequestsize") maxrequestsize_ = value;
578  else if (keyword == "maxlogsize") maxlogsize_ = value;
579  /*{
580  // FIXME: support for suffixes
581  int suffix;
582  splitSizeSpec( value, maxlogsize_, suffix );
583  }*/
584  else if (keyword == "pagelog") pagelog_ = value;
585  else if (keyword == "port") listenaddresses_.append("Listen *:"+value);
586  else if (keyword == "preservejobhistory") keepjobhistory_ = (value != "off");
587  else if (keyword == "preservejobfiles") keepjobfiles_ = (value == "on");
588  else if (keyword == "printcap") printcap_ = value;
589  else if (keyword == "printcapformat") printcapformat_ = (value.lower() == "solaris" ? PRINTCAP_SOLARIS : PRINTCAP_BSD);
590  else if (keyword == "requestroot") requestdir_ = value;
591  else if (keyword == "remoteroot") remoteroot_ = value;
592  else if (keyword == "ripcache") ripcache_ = value;
593  else if (keyword == "serveradmin") serveradmin_ = value;
594  else if (keyword == "serverbin") serverbin_ = value;
595  else if (keyword == "servercertificate") encryptcert_ = value;
596  else if (keyword == "serverkey") encryptkey_ = value;
597  else if (keyword == "servername") servername_ = value;
598  else if (keyword == "serverroot") serverfiles_ = value;
599  else if (keyword == "ssllisten") listenaddresses_.append("SSLListen "+value);
600  else if (keyword == "sslport") listenaddresses_.append("SSLListen *:"+value);
601  else if (keyword == "systemgroup") systemgroup_ = value;
602  else if (keyword == "tempdir") tmpfiles_ = value;
603  else if (keyword == "timeout") clienttimeout_ = value.toInt();
604  else if (keyword == "user") user_ = value;
605  else
606  {
607  // unrecognized option
608  unknown_ << TQPair<TQString,TQString>(keyword, value);
609  }
610  return true;
611 }
612 
613 bool CupsdConf::loadAvailableResources()
614 {
615  TDEConfig conf("tdeprintrc");
616  conf.setGroup("CUPS");
617  TQString host = conf.readEntry("Host",cupsServer());
618  int port = conf.readNumEntry("Port",ippPort());
619  http_t *http_ = httpConnect(host.local8Bit(),port);
620 
621  resources_.clear();
622  // standard resources
623  resources_.append(new CupsResource("/"));
624  resources_.append(new CupsResource("/admin"));
625  resources_.append(new CupsResource("/printers"));
626  resources_.append(new CupsResource("/classes"));
627  resources_.append(new CupsResource("/jobs"));
628 
629  if (!http_)
630  return false;
631 
632  // printer resources
633  ipp_t *request_ = ippNew();
634  cups_lang_t* lang = cupsLangDefault();
635  ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, cupsLangEncoding(lang));
636  ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, lang->language);
637 #ifdef HAVE_CUPS_1_6
638  ippSetOperation(request_, CUPS_GET_PRINTERS);
639 #else // HAVE_CUPS_1_6
640  request_->request.op.operation_id = CUPS_GET_PRINTERS;
641 #endif // HAVE_CUPS_1_6
642  request_ = cupsDoRequest(http_, request_, "/printers/");
643  if (request_)
644  {
645  TQString name;
646  int type(0);
647 #ifdef HAVE_CUPS_1_6
648  ipp_attribute_t *attr = ippFirstAttribute(request_);
649 #else // HAVE_CUPS_1_6
650  ipp_attribute_t *attr = request_->attrs;
651 #endif // HAVE_CUPS_1_6
652  while (attr)
653  {
654  // check new printer (keep only local non-implicit printers)
655 #ifdef HAVE_CUPS_1_6
656  if (!ippGetName(attr))
657 #else // HAVE_CUPS_1_6
658  if (!attr->name)
659 #endif // HAVE_CUPS_1_6
660  {
661  if (!(type & CUPS_PRINTER_REMOTE) && !(type & CUPS_PRINTER_IMPLICIT) && !name.isEmpty())
662  resources_.append(new CupsResource("/printers/"+name));
663  name = "";
664  type = 0;
665  }
666 #ifdef HAVE_CUPS_1_6
667  else if (strcmp(ippGetName(attr), "printer-name") == 0) name = ippGetString(attr, 0, NULL);
668  else if (strcmp(ippGetName(attr), "printer-type") == 0) type = ippGetInteger(attr, 0);
669  attr = ippNextAttribute(request_);
670 #else // HAVE_CUPS_1_6
671  else if (strcmp(attr->name, "printer-name") == 0) name = attr->values[0].string.text;
672  else if (strcmp(attr->name, "printer-type") == 0) type = attr->values[0].integer;
673  attr = attr->next;
674 #endif // HAVE_CUPS_1_6
675  }
676  if (!(type & CUPS_PRINTER_REMOTE) && !(type & CUPS_PRINTER_IMPLICIT) && !name.isEmpty())
677  resources_.append(new CupsResource("/printers/"+name));
678  ippDelete(request_);
679  }
680  // class resources
681  request_ = ippNew();
682  ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, cupsLangEncoding(lang));
683  ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, lang->language);
684 #ifdef HAVE_CUPS_1_6
685  ippSetOperation(request_, CUPS_GET_CLASSES);
686 #else // HAVE_CUPS_1_6
687  request_->request.op.operation_id = CUPS_GET_CLASSES;
688 #endif // HAVE_CUPS_1_6
689  request_ = cupsDoRequest(http_, request_, "/classes/");
690  if (request_)
691  {
692  TQString name;
693  int type(0);
694 #ifdef HAVE_CUPS_1_6
695  ipp_attribute_t *attr = ippFirstAttribute(request_);
696 #else // HAVE_CUPS_1_6
697  ipp_attribute_t *attr = request_->attrs;
698 #endif // HAVE_CUPS_1_6
699  while (attr)
700  {
701  // check new class (keep only local classes)
702 #ifdef HAVE_CUPS_1_6
703  if (!ippGetName(attr))
704 #else // HAVE_CUPS_1_6
705  if (!attr->name)
706 #endif // HAVE_CUPS_1_6
707  {
708  if (!(type & CUPS_PRINTER_REMOTE) && !name.isEmpty())
709  resources_.append(new CupsResource("/classes/"+name));
710  name = "";
711  type = 0;
712  }
713 #ifdef HAVE_CUPS_1_6
714  else if (strcmp(ippGetName(attr), "printer-name") == 0) name = ippGetString(attr, 0, NULL);
715  else if (strcmp(ippGetName(attr), "printer-type") == 0) type = ippGetInteger(attr, 0);
716  attr = ippNextAttribute(request_);
717 #else // HAVE_CUPS_1_6
718  else if (strcmp(attr->name, "printer-name") == 0) name = attr->values[0].string.text;
719  else if (strcmp(attr->name, "printer-type") == 0) type = attr->values[0].integer;
720  attr = attr->next;
721 #endif // HAVE_CUPS_1_6
722  }
723  if (!(type & CUPS_PRINTER_REMOTE) && !name.isEmpty())
724  resources_.append(new CupsResource("/classes/"+name));
725  ippDelete(request_);
726  }
727  httpClose(http_);
728  return true;
729 }
730 
731 //------------------------------------------------------------------------------------------------
732 
733 CupsLocation::CupsLocation()
734 {
735  resource_ = 0;
736  resourcename_ = "";
737  authtype_ = AUTHTYPE_NONE;
738  authclass_ = AUTHCLASS_ANONYMOUS;
739  authname_ = TQString::null;
740  encryption_ = ENCRYPT_IFREQUESTED;
741  satisfy_ = SATISFY_ALL;
742  order_ = ORDER_ALLOW_DENY;
743  // addresses_
744 }
745 
746 CupsLocation::CupsLocation(const CupsLocation& loc)
747 : resource_(loc.resource_),
748  resourcename_(loc.resourcename_),
749  authtype_(loc.authtype_),
750  authclass_(loc.authclass_),
751  authname_(loc.authname_),
752  encryption_(loc.encryption_),
753  satisfy_(loc.satisfy_),
754  order_(loc.order_),
755  addresses_(loc.addresses_)
756 {
757 }
758 
759 bool CupsLocation::parseResource(const TQString& line)
760 {
761  TQString str = line.simplifyWhiteSpace();
762  int p1 = line.find(' '), p2 = line.find('>');
763  if (p1 != -1 && p2 != -1)
764  {
765  resourcename_ = str.mid(p1+1,p2-p1-1);
766  return true;
767  }
768  else return false;
769 }
770 
771 bool CupsLocation::parseOption(const TQString& line)
772 {
773  int p(-1);
774  TQString keyword, value, l(line.simplifyWhiteSpace());
775 
776  if ((p=l.find(' ')) != -1)
777  {
778  keyword = l.left(p).lower();
779  value = l.mid(p+1);
780  }
781  else
782  {
783  keyword = l.lower();
784  }
785 
786  if (keyword == "authtype")
787  {
788  TQString a = value.lower();
789  if (a == "basic") authtype_ = AUTHTYPE_BASIC;
790  else if (a == "digest") authtype_ = AUTHTYPE_DIGEST;
791  else authtype_ = AUTHTYPE_NONE;
792  }
793  else if (keyword == "authclass")
794  {
795  TQString a = value.lower();
796  if (a == "user") authclass_ = AUTHCLASS_USER;
797  else if (a == "system") authclass_ = AUTHCLASS_SYSTEM;
798  else if (a == "group") authclass_ = AUTHCLASS_GROUP;
799  else authclass_ = AUTHCLASS_ANONYMOUS;
800  }
801  else if (keyword == "authgroupname") authname_ = value;
802  else if (keyword == "require")
803  {
804  int p = value.find(' ');
805  if (p != -1)
806  {
807  authname_ = value.mid(p+1);
808  TQString cl = value.left(p).lower();
809  if (cl == "user")
810  authclass_ = AUTHCLASS_USER;
811  else if (cl == "group")
812  authclass_ = AUTHCLASS_GROUP;
813  }
814  }
815  else if (keyword == "allow") addresses_.append("Allow "+value);
816  else if (keyword == "deny") addresses_.append("Deny "+value);
817  else if (keyword == "order") order_ = (value.lower() == "deny,allow" ? ORDER_DENY_ALLOW : ORDER_ALLOW_DENY);
818  else if (keyword == "encryption")
819  {
820  TQString e = value.lower();
821  if (e == "always") encryption_ = ENCRYPT_ALWAYS;
822  else if (e == "never") encryption_ = ENCRYPT_NEVER;
823  else if (e == "required") encryption_ = ENCRYPT_REQUIRED;
824  else encryption_ = ENCRYPT_IFREQUESTED;
825  }
826  else if (keyword == "satisfy") satisfy_ = (value.lower() == "any" ? SATISFY_ANY : SATISFY_ALL);
827  else return false;
828  return true;
829 }
830 
831 //------------------------------------------------------------------------------------------------
832 
833 CupsResource::CupsResource()
834 {
835  type_ = RESOURCE_GLOBAL;
836 }
837 
838 CupsResource::CupsResource(const TQString& path)
839 {
840  setPath(path);
841 }
842 
843 void CupsResource::setPath(const TQString& path)
844 {
845  path_ = path;
846  type_ = typeFromPath(path_);
847  text_ = pathToText(path_);
848 }
849 
850 int CupsResource::typeFromText(const TQString& text)
851 {
852  if (text == i18n("Base", "Root") || text == i18n("All printers") || text == i18n("All classes") || text == i18n("Print jobs")) return RESOURCE_GLOBAL;
853  else if (text == i18n("Administration")) return RESOURCE_ADMIN;
854  else if (text.find(i18n("Class")) == 0) return RESOURCE_CLASS;
855  else if (text.find(i18n("Printer")) == 0) return RESOURCE_PRINTER;
856  else return RESOURCE_PRINTER;
857 }
858 
859 int CupsResource::typeFromPath(const TQString& path)
860 {
861  if (path == "/admin") return RESOURCE_ADMIN;
862  else if (path == "/printers" || path == "/classes" || path == "/" || path == "/jobs") return RESOURCE_GLOBAL;
863  else if (path.left(9) == "/printers") return RESOURCE_PRINTER;
864  else if (path.left(8) == "/classes") return RESOURCE_CLASS;
865  else return RESOURCE_GLOBAL;
866 }
867 
868 TQString CupsResource::textToPath(const TQString& text)
869 {
870  TQString path("/");
871  if (text == i18n("Administration")) path = "/admin";
872  else if (text == i18n("All printers")) path = "/printers";
873  else if (text == i18n("All classes")) path = "/classes";
874  else if (text == i18n("Print jobs")) path = "/jobs";
875  else if (text == i18n("Base", "Root")) path = "/";
876  else if (text.find(i18n("Printer")) == 0)
877  {
878  path = "/printers/";
879  path.append(text.right(text.length()-i18n("Printer").length()-1));
880  }
881  else if (text.find(i18n("Class")) == 0)
882  {
883  path = "/classes/";
884  path.append(text.right(text.length()-i18n("Class").length()-1));
885  }
886  return path;
887 }
888 
889 TQString CupsResource::pathToText(const TQString& path)
890 {
891  TQString text(i18n("Base", "Root"));
892  if (path == "/admin") text = i18n("Administration");
893  else if (path == "/printers") text = i18n("All printers");
894  else if (path == "/classes") text = i18n("All classes");
895  else if (path == "/") text = i18n("Root");
896  else if (path == "/jobs") text = i18n("Print jobs");
897  else if (path.find("/printers/") == 0)
898  {
899  text = i18n("Printer");
900  text.append(" ");
901  text.append(path.right(path.length()-10));
902  }
903  else if (path.find("/classes/") == 0)
904  {
905  text = i18n("Class");
906  text.append(" ");
907  text.append(path.right(path.length()-9));
908  }
909  return text;
910 }
911 
912 TQString CupsResource::typeToIconName(int type)
913 {
914  switch (type)
915  {
916  case RESOURCE_ADMIN:
917  case RESOURCE_GLOBAL:
918  return TQString("folder");
919  case RESOURCE_PRINTER:
920  return TQString("tdeprint_printer");
921  case RESOURCE_CLASS:
922  return TQString("tdeprint_printer_class");
923  }
924  return TQString("folder");
925 }

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
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.1
This website is maintained by Timothy Pearson.