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

tdeprint

  • tdeprint
kmfactory.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 "kmfactory.h"
21#include "kmmanager.h"
22#include "kmjobmanager.h"
23#include "kmuimanager.h"
24#include "kprinterimpl.h"
25#include "kprinter.h"
26#include "kpreloadobject.h"
27#include "tdeprintcheck.h"
28#include "kxmlcommand.h"
29
30#include <tqdir.h>
31#include <tqfile.h>
32#include <tqsettings.h>
33
34#include <klibloader.h>
35#include <tdeconfig.h>
36#include <tdestandarddirs.h>
37#include <kiconloader.h>
38#include <kdebug.h>
39#include <tdemessagebox.h>
40#include <tdelocale.h>
41#include <ksimpleconfig.h>
42#include <kstaticdeleter.h>
43#include <tdeapplication.h>
44#include <dcopclient.h>
45#include <dcopref.h>
46#include <tdeio/authinfo.h>
47
48#include <unistd.h>
49
50#define UNLOAD_OBJECT(x) if (x != 0) { delete x; x = 0; }
51
52#ifdef TQ_WS_X11
53extern void tqt_generate_epsf( bool b );
54#endif
55
56KMFactory* KMFactory::m_self = 0;
57static KStaticDeleter<KMFactory> s_kmfactorysd;
58
59KMFactory* KMFactory::self()
60{
61 if (!m_self)
62 m_self = s_kmfactorysd.setObject(m_self, new KMFactory());
63 return m_self;
64}
65
66bool KMFactory::exists()
67{
68 return m_self != 0L;
69}
70
71void KMFactory::release()
72{
73 if (m_self)
74 {
75 KMFactory* p = m_self;
76 m_self = 0; // so that exists() says false
77 delete p;
78 }
79}
80
81KMFactory::KMFactory()
82 : TQObject(NULL, "Factory")
83{
84 m_settings = new Settings;
85 m_settings->application = KPrinter::Dialog;
86 m_settings->pageSelection = KPrinter::SystemSide;
87 m_settings->standardDialogPages = KPrinter::CopiesPage;
88 m_settings->pageSize = -1;
89 m_settings->orientation = -1;
90
91 m_objects.setAutoDelete(false);
92
93 m_manager = 0;
94 m_jobmanager = 0;
95 m_uimanager = 0;
96 m_implementation = 0;
97 m_factory = 0;
98 m_printconfig = 0;
99#if TQT_VERSION >= 230
100 // Qt's default behavior, to generate EPS in some cases and not in others, sucks.
101 // This is fixed in Qt 3.0, but for Qt 2.x we need to disable it explicitly.
102 // If this is a problem for anyone, we can add a public method to set this flag.
103 // (David Faure, doing as advised by Lars Knoll)
104#ifdef TQ_WS_X11
105 tqt_generate_epsf( false );
106#endif
107#endif
108
109 // By default, embed PS fonts
110 bool ok = false;
111 TQSettings settings;
112 settings.readBoolEntry( "/qt/embedFonts", true, &ok );
113 if ( !ok )
114 settings.writeEntry( "/qt/embedFonts", true );
115
116 TDEGlobal::iconLoader()->addAppDir("tdeprint");
117 TDEGlobal::locale()->insertCatalogue("tdeprint");
118
119 // create DCOP signal connection
120 connectDCOPSignal(0, 0, "pluginChanged(pid_t)", "slot_pluginChanged(pid_t)", false);
121 connectDCOPSignal(0, 0, "configChanged()", "slot_configChanged()", false);
122}
123
124KMFactory::~KMFactory()
125{
126 delete m_settings;
127 // The only object to be destroyed is m_printconfig. All other objects have been
128 // created with "this" as parent, so we don't need to care about their destruction
129 UNLOAD_OBJECT(m_printconfig);
130 m_self = 0;
131}
132
133KMManager* KMFactory::manager()
134{
135 if (!m_manager)
136 createManager();
137 TQ_CHECK_PTR(m_manager);
138 return m_manager;
139}
140
141KMJobManager* KMFactory::jobManager()
142{
143 if (!m_jobmanager)
144 createJobManager();
145 TQ_CHECK_PTR(m_jobmanager);
146 return m_jobmanager;
147}
148
149KMUiManager* KMFactory::uiManager()
150{
151 if (!m_uimanager)
152 createUiManager();
153 TQ_CHECK_PTR(m_uimanager);
154 return m_uimanager;
155}
156
157KPrinterImpl* KMFactory::printerImplementation()
158{
159 if (!m_implementation)
160 createPrinterImpl();
161 TQ_CHECK_PTR(m_implementation);
162 return m_implementation;
163}
164
165KMVirtualManager* KMFactory::virtualManager()
166{
167 return manager()->m_virtualmgr;
168}
169
170KMSpecialManager* KMFactory::specialManager()
171{
172 return manager()->m_specialmgr;
173}
174
175KXmlCommandManager* KMFactory::commandManager()
176{
177 return KXmlCommandManager::self();
178}
179
180void KMFactory::createManager()
181{
182 loadFactory();
183 if (m_factory) m_manager = (KMManager*)m_factory->create(this,"Manager","KMManager");
184 if (!m_manager) m_manager = new KMManager(this,"Manager");
185}
186
187void KMFactory::createJobManager()
188{
189 loadFactory();
190 if (m_factory) m_jobmanager = (KMJobManager*)m_factory->create(this,"JobManager","KMJobManager");
191 if (!m_jobmanager) m_jobmanager = new KMJobManager(this,"JobManager");
192}
193
194void KMFactory::createUiManager()
195{
196 loadFactory();
197 if (m_factory) m_uimanager = (KMUiManager*)m_factory->create(this,"UiManager","KMUiManager");
198 if (!m_uimanager) m_uimanager = new KMUiManager(this,"UiManager");
199}
200
201void KMFactory::createPrinterImpl()
202{
203 loadFactory();
204 if (m_factory) m_implementation = (KPrinterImpl*)m_factory->create(this,"PrinterImpl","KPrinterImpl");
205 if (!m_implementation) m_implementation = new KPrinterImpl(this,"PrinterImpl");
206}
207
208void KMFactory::loadFactory(const TQString& syst)
209{
210 if (!m_factory)
211 {
212 TQString sys(syst);
213 if (sys.isEmpty())
214 // load default configured print plugin
215 sys = printSystem();
216 TQString libname = TQString::fromLatin1("tdeprint_%1").arg(sys);
217 m_factory = KLibLoader::self()->factory(TQFile::encodeName(libname));
218 if (!m_factory)
219 {
220 KMessageBox::error(0,
221 i18n("<qt>There was an error loading %1. The diagnostic is:<p>%2</p></qt>")
222 .arg(libname).arg(KLibLoader::self()->lastErrorMessage()));
223 }
224 }
225}
226
227TDEConfig* KMFactory::printConfig(const TQString& group)
228{
229 if (!m_printconfig)
230 {
231 m_printconfig = new TDEConfig("tdeprintrc");
232 TQ_CHECK_PTR(m_printconfig);
233 }
234 if (!group.isEmpty())
235 m_printconfig->setGroup(group);
236 return m_printconfig;
237}
238
239TQString KMFactory::printSystem()
240{
241 TDEConfig *conf = printConfig();
242 conf->setGroup("General");
243 TQString sys = conf->readEntry("PrintSystem");
244 if (sys.isEmpty())
245 {
246 // perform auto-detection (will at least return "lpdunix")
247 sys = autoDetect();
248 // save the result
249 conf->writeEntry("PrintSystem", sys);
250 conf->sync();
251 }
252 else if ( sys.length()==1 && sys[0].isDigit() ) // discard old-style settings
253 sys = "lpdunix";
254 return sys;
255}
256
257void KMFactory::unload()
258{
259 UNLOAD_OBJECT(m_manager);
260 UNLOAD_OBJECT(m_jobmanager);
261 UNLOAD_OBJECT(m_uimanager);
262 UNLOAD_OBJECT(m_implementation);
263 // factory will be automatically unloaded by KLibLoader as all object have been deleted.
264 // But to have loadFactory() to work, we need to set m_factory to NULL.
265 m_factory = 0;
266}
267
268void KMFactory::reload(const TQString& syst, bool saveSyst)
269{
270 // notify all registered objects about the coming reload
271 TQPtrListIterator<KPReloadObject> it(m_objects);
272 for (;it.current();++it)
273 it.current()->aboutToReload();
274
275 // unload all objects from the plugin
276 unload();
277 if (saveSyst)
278 {
279 TDEConfig *conf = printConfig();
280 conf->setGroup("General");
281 conf->writeEntry("PrintSystem", syst);
282 conf->sync();
283
284 // notify all other apps using DCOP signal
285 emit pluginChanged(getpid());
286 }
287
288 // reload the factory
289 loadFactory(syst);
290
291 // notify all registered objects
292 for (it.toFirst();it.current();++it)
293 it.current()->reload();
294}
295
296TQValueList<KMFactory::PluginInfo> KMFactory::pluginList()
297{
298 TQDir d(locate("data", "tdeprint/plugins/"), "*.print", TQDir::Name, TQDir::Files);
299 TQValueList<PluginInfo> list;
300 for (uint i=0; i<d.count(); i++)
301 {
302 PluginInfo info(pluginInfo(d.absFilePath(d[i])));
303 if (info.name.isEmpty())
304 continue;
305 list.append(info);
306 }
307 return list;
308}
309
310KMFactory::PluginInfo KMFactory::pluginInfo(const TQString& name)
311{
312 TQString path(name);
313 if (path[0] != '/')
314 path = locate("data", TQString::fromLatin1("tdeprint/plugins/%1.print").arg(name));
315 KSimpleConfig conf(path);
316 PluginInfo info;
317
318 conf.setGroup("TDE Print Entry");
319 info.name = conf.readEntry("PrintSystem");
320 info.comment = conf.readEntry("Comment");
321 if (info.comment.isEmpty())
322 info.comment = info.name;
323 info.detectUris = conf.readListEntry("DetectUris");
324 info.detectPrecedence = conf.readNumEntry("DetectPrecedence", 0);
325 info.mimeTypes = conf.readListEntry("MimeTypes");
326 if (info.mimeTypes.isEmpty())
327 info.mimeTypes << "application/postscript";
328 info.primaryMimeType = conf.readEntry("PrimaryMimeType", info.mimeTypes[0]);
329
330 return info;
331}
332
333void KMFactory::registerObject(KPReloadObject *obj, bool priority)
334{
335 // check if object already registered, then add it
336 if (m_objects.findRef(obj) == -1)
337 {
338 if (priority)
339 m_objects.prepend(obj);
340 else
341 m_objects.append(obj);
342 kdDebug(500) << "tdeprint: registering " << (void*)obj << ", number of objects = " << m_objects.count() << endl;
343 }
344}
345
346void KMFactory::unregisterObject(KPReloadObject *obj)
347{
348 // remove object from list (not deleted as autoDelete is false)
349 m_objects.removeRef(obj);
350 kdDebug(500) << "tdeprint: unregistering " << (void*)obj << ", number of objects = " << m_objects.count() << endl;
351}
352
353TQString KMFactory::autoDetect()
354{
355 TQValueList<PluginInfo> plugins = pluginList();
356 int pluginIndex(-1), currentPrecedence(0);
357 for (uint i=0;i<plugins.count();i++)
358 {
359 if (plugins[i].detectUris.count() > 0 && KdeprintChecker::check(plugins[i].detectUris)
360 && (pluginIndex == -1 || plugins[i].detectPrecedence >= currentPrecedence))
361 {
362 pluginIndex = i;
363 currentPrecedence = plugins[i].detectPrecedence;
364 }
365 }
366 return (pluginIndex == -1 ? TQString::fromLatin1("lpdunix") : plugins[pluginIndex].name);
367}
368
369void KMFactory::slot_pluginChanged(pid_t pid)
370{
371 // only do something if the notification comes from another process
372 if (pid != getpid())
373 {
374 // Unload config object (avoid saving it)
375 printConfig()->rollback();
376 UNLOAD_OBJECT(m_printconfig);
377 // Then reload everything and notified registered objects.
378 // Do NOT re-save the new print system.
379 TQString syst = printSystem();
380 reload(syst, false);
381 }
382}
383
384void KMFactory::slot_configChanged()
385{
386 kdDebug(500) << "KMFactory (" << getpid() << ") receiving DCOP signal configChanged()" << endl;
387 // unload/reload config object (make it non dirty to
388 // avoid saving it and overwriting the newly saved options
389 // in the other application)
390 printConfig()->rollback();
391 UNLOAD_OBJECT(m_printconfig);
392 printConfig();
393
394 // notify all registered objects about the coming reload
395 TQPtrListIterator<KPReloadObject> it(m_objects);
396 /*for (;it.current();++it)
397 it.current()->aboutToReload();*/
398
399 // notify all object about the change
400 for (it.toFirst(); it.current();++it)
401 it.current()->configChanged();
402}
403
404void KMFactory::saveConfig()
405{
406 TDEConfig *conf = printConfig();
407 conf->sync();
408 kdDebug(500) << "KMFactory (" << getpid() << ") emitting DCOP signal configChanged()" << endl;
409 emit configChanged();
410 // normally, the self application should also receive the signal,
411 // anyway the config object has been updated "locally", so ne real
412 // need to reload the config file.
413}
414
415TQPair<TQString,TQString> KMFactory::requestPassword( int& seqNbr, const TQString& user, const TQString& host, int port )
416{
417 DCOPRef tdeprintd( "kded", "tdeprintd" );
424 DCOPReply reply = tdeprintd.call( "requestPassword", user, host, port, seqNbr );
425 if ( reply.isValid() )
426 {
427 TQString replyString = reply;
428 if ( replyString != "::" )
429 {
430 TQStringList l = TQStringList::split( ':', replyString, true );
431 if ( l.count() == 3 )
432 {
433 seqNbr = l[ 2 ].toInt();
434 return TQPair<TQString,TQString>( l[ 0 ], l[ 1 ] );
435 }
436 }
437 }
438 return TQPair<TQString,TQString>( TQString::null, TQString::null );
439}
440
441void KMFactory::initPassword( const TQString& user, const TQString& password, const TQString& host, int port )
442{
443 DCOPRef tdeprintd( "kded", "tdeprintd" );
450 tdeprintd.call( "initPassword", user, password, host, port );
451}
452
453#include "kmfactory.moc"

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.4
This website is maintained by Timothy Pearson.