kword

KWMailMergeDataBase.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3
4 Modified by Joseph wenninger, 2001
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "KWDocument.h"
23#include "KWMailMergeDataBase.h"
24#include "KWMailMergeDataBase.moc"
25#include <KoCustomVariablesDia.h>
26#include "defs.h"
27
28#include <tdelocale.h>
29#include <kdebug.h>
30#include <kstdaction.h>
31#include <tdeaction.h>
32#include <tdemessagebox.h>
33#include <kseparator.h>
34#include <kservice.h>
35#include <tdemainwindow.h>
36
37#include <tqfile.h>
38#include <tqvbox.h>
39#include <tqspinbox.h>
40#include <tqlayout.h>
41#include <tqlabel.h>
42#include <tqpushbutton.h>
43
44/******************************************************************
45 *
46 * Class: KWMailMergeDataBase
47 *
48 ******************************************************************/
49
50KWMailMergeDataBase::KWMailMergeDataBase( KWDocument *doc_ )
51 : TQObject(doc_,doc_->dcopObject()->objId()+".MailMergeDataBase"),
52 KWordMailMergeDatabaseIface(TQCString(doc_->dcopObject()->objId()+".MailMergeDataBase")),
53 m_version(0),
54 doc( doc_ )
55{
56 plugin=0; //loadPlugin("classic");
57 rejectdcopcall=false;
58}
59
60TQStringList KWMailMergeDataBase::availablePlugins()
61{
62 TQStringList tmp;
63 TDETrader::OfferList pluginOffers=TDETrader::self()->query(TQString::fromLatin1("KWord/MailMergePlugin"),TQString());
64 for (TDETrader::OfferList::Iterator it=pluginOffers.begin();*it;++it)
65 {
66 tmp.append((*it)->property("X-TDE-InternalName").toString());
67 kdDebug()<<"Found mail merge plugin: "<< (*it)->name()<<endl;
68 }
69 return tmp;
70}
71
72bool KWMailMergeDataBase::isConfigDialogShown()
73{
74 return rejectdcopcall;
75}
76
77bool KWMailMergeDataBase::loadPlugin(const TQString &name,const TQString &command)
78{
79 if (rejectdcopcall)return false;
80 TQString constrain=TQString("[X-TDE-InternalName] =='"+name+"'");
81 kdDebug()<<constrain<<endl;
82 TDETrader::OfferList pluginOffers=TDETrader::self()->query(TQString::fromLatin1("KWord/MailMergePlugin"),constrain);
83 KService::Ptr it=pluginOffers.first();
84
85 TQVariant verProp=it->property("X-TDE-PluginVersion");
86 int version=verProp.toInt();
87
88 if (it)
89 {
90 KWMailMergeDataSource *tmp=loadPlugin(it->library());
91 if (!tmp)
92 {
93 kdDebug()<<"Couldn't load plugin"<<endl;
94 return false; //Plugin couldn't be loaded
95 }
96 //Plugin found and loaded
97 if (command=="silent") {
98 return askUserForConfirmationAndConfig(tmp,false,0,version);
99 }
100 else
101 {
102 if (command=="open") action=KWSLOpen;
103 else if (command=="create") action=KWSLCreate;
104 else action=KWSLUnspecified;
105 return askUserForConfirmationAndConfig(tmp,true,0,version);
106 }
107 }
108 else
109 {
110 kdDebug()<<"No plugin found"<<endl;
111 return false; //No plugin with specified name found
112 }
113}
114
115KWMailMergeDataSource *KWMailMergeDataBase::openPluginFor(int type,int &version)
116{
117 version=0;
118 KWMailMergeDataSource *ret=0;
119 TQString constrain=TQString("'%1' in [X-TDE-Capabilities]").arg(((type==KWSLCreate)?KWSLCreate_text:KWSLOpen_text));
120 kdDebug()<<constrain<<endl;
121 TDETrader::OfferList pluginOffers=TDETrader::self()->query(TQString::fromLatin1("KWord/MailMergePlugin"),constrain);
122
123 //Only for debugging
124 for (TDETrader::OfferList::Iterator it=pluginOffers.begin();*it;++it)
125 {
126 kdDebug()<<"Found mail merge plugin: "<< (*it)->name()<<endl;
127 }
128
129 if (!pluginOffers.count())
130 {
131 //Sorry no suitable plugins found
132 kdDebug()<<"No plugins found"<<endl;
133 KMessageBox::sorry(0,i18n("No plugins supporting the requested action were found."));
134 }
135 else
136 {
137
138 KWMailMergeChoosePluginDialog *dia=new KWMailMergeChoosePluginDialog(pluginOffers);
139 if (dia->exec()==TQDialog::Accepted)
140 {
141 TQVariant verProp=(*(pluginOffers.at(dia->currentPlugin())))->property("X-TDE-PluginVersion");
142 version=verProp.toInt();
143
144 ret=loadPlugin((*(pluginOffers.at(dia->currentPlugin())))->library());
145 }
146
147 }
148 return ret;
149}
150
151KWMailMergeDataSource *KWMailMergeDataBase::loadPlugin(const TQString& name)
152{
153 if (!name.isEmpty())
154 {
155 // get the library loader instance
156
157 KLibLoader *loader = KLibLoader::self();
158
159 // try to load the library
160 TQString libname=name;
161// TQString libname("lib%1");
162 KLibrary *lib = loader->library(TQFile::encodeName(libname));
163 if (lib) {
164 // get the create_ function
165 TQString factory=TQString("create_%1").arg(name);
166 void *create = lib->symbol(TQFile::encodeName(factory));
167
168 if (create)
169 {
170 // create the module
171 KWMailMergeDataSource * (*func)(TDEInstance*,TQObject*);
172 func = (KWMailMergeDataSource* (*)(TDEInstance*,TQObject*)) create;
173 KWMailMergeDataSource *tmpsource =func(KWFactory::instance(),this);
174 if (tmpsource)
175 {
176 TQDataStream tmpstream(tmpsource->info,IO_WriteOnly);
177 tmpstream<<name;
178 }
179 return tmpsource;
180 }
181 }
182 kdWarning() << "Couldn't load plugin " << name << endl;
183 }
184 else
185 kdWarning()<< "No plugin name specified" <<endl;
186 return 0;
187}
188
189bool KWMailMergeDataBase::isSampleRecord() {
190 return (0>doc->mailMergeRecord());
191}
192
193TQString KWMailMergeDataBase::getValue( const TQString &name, int record ) const
194{
195 if (plugin)
196 {
197 if (record==-1) record=doc->mailMergeRecord();
198 return plugin->getValue(name,record);
199 }
200 else
201 return TQString("");
202}
203
204
205void KWMailMergeDataBase::refresh(bool force)
206{
207 if (plugin) plugin->refresh(force);
208}
209
210const TQMap< TQString, TQString > &KWMailMergeDataBase::getRecordEntries() const
211{
212 if (plugin)
213 return plugin->getRecordEntries();
214 else
215 return emptyMap;
216}
217
218int KWMailMergeDataBase::getNumRecords() const
219{
220 if (plugin)
221 return plugin->getNumRecords();
222 else
223 return 0;
224
225}
226
227
228void KWMailMergeDataBase::showConfigDialog(TQWidget *par)
229{
230 rejectdcopcall=true;
231 KWMailMergeConfigDialog *dia=new KWMailMergeConfigDialog(par,this);
232 dia->exec();
233 delete dia;
234 rejectdcopcall=false;
235}
236
237
238bool KWMailMergeDataBase::askUserForConfirmationAndConfig(KWMailMergeDataSource *tmpPlugin,bool config,TQWidget *par,int version)
239{
240 if (tmpPlugin)
241 {
242 bool replaceit=false;
243 if (!config) replaceit=true;
244 else
245 replaceit=tmpPlugin->showConfigDialog(par,action);
246 if (replaceit)
247 {
248 if (plugin)
249 {
250 if (KMessageBox::warningContinueCancel(par,
251 i18n("Do you really want to replace the current datasource?"),
252 TQString(),TQString(),TQString(),true)== KMessageBox::Cancel)
253 {
254 delete tmpPlugin;
255 tmpPlugin=0;
256 return false;
257 }
258 delete plugin;
259 }
260 m_version=version;
261 plugin=tmpPlugin;
262 }
263 else
264 {
265 delete tmpPlugin;
266 tmpPlugin=0;
267 return false;
268 }
269 }
270 tmpPlugin->setObjId(TQCString(objId()+".MailMergePlugin"));
271 return true;
272}
273
274
275
276TQDomElement KWMailMergeDataBase::save(TQDomDocument &doc) const
277{
278 kdDebug()<<"KWMailMergeDataBase::save()"<<endl;
279 TQDomElement parentElem=doc.createElement("MAILMERGE");
280 if (plugin)
281 {
282 kdDebug()<<"KWMailMergeDataBase::save() There is really something to save"<<endl;
283 TQDomElement el=doc.createElement(TQString::fromLatin1("PLUGIN"));
284
285 TQDataStream ds(plugin->info,IO_ReadOnly);
286 TQString libname;
287 ds>>libname;
288 el.setAttribute("library",libname);
289 parentElem.appendChild(el);
290 kdDebug()<<"KWMailMergeDataBase::save() Calling datasource save()"<<endl;
291 TQDomElement el2=doc.createElement(TQString::fromLatin1("DATASOURCE"));
292 plugin->save(doc,el2);
293 parentElem.appendChild(el2);
294
295 }
296 kdDebug()<<"KWMailMergeDataBase::save() leaving now"<<endl;
297 return parentElem;
298 // if (plugin) plugin->save(parentElem); // Not completely sure, perhaps the database itself has to save something too (JoWenn)
299}
300
301void KWMailMergeDataBase::load( const TQDomElement& parentElem )
302{
303 TQDomNode dn=parentElem.namedItem("PLUGIN");
304 if (dn.isNull()) return;
305 TQDomElement el=dn.toElement();
306 plugin=loadPlugin(el.attribute("library"));
307
308 dn=parentElem.namedItem("DATASOURCE");
309 if (dn.isNull()) return;
310 el=dn.toElement();
311 if (plugin) plugin->load(el);
312}
313
314
315int KWMailMergeDataBase::version() {
316 kdDebug()<<"KWMailMergeDataBase::version:"<<m_version<<endl;
317 return m_version;
318}
319
320
321/******************************************************************
322 *
323 * Class: KWMailMerge ChoosePluginDialog
324 *
325 ******************************************************************/
326
327KWMailMergeChoosePluginDialog::KWMailMergeChoosePluginDialog( TDETrader::OfferList offers )
328 : KDialogBase( Plain, i18n( "Mail Merge Setup" ), Ok | Cancel, Ok,
329 /*parent*/ 0, "", true ), pluginOffers( offers )
330{
331 TQWidget *back = plainPage();
332 TQVBoxLayout *layout = new TQVBoxLayout( back, 0, spacingHint() );
333
334 TQLabel *label = new TQLabel( i18n( "&Available sources:" ), back );
335 chooser = new TQComboBox( false, back );
336 label->setBuddy( chooser );
337 descriptionLabel = new TQLabel( back );
338 descriptionLabel->hide();
339 descriptionLabel->setAlignment( WordBreak );
340 descriptionLabel->setFrameShape( TQFrame::Box );
341 descriptionLabel->setFrameShadow( TQFrame::Sunken );
342
343 TQSize old_sizeHint;
344 for ( TDETrader::OfferList::Iterator it = pluginOffers.begin(); *it; ++it )
345 {
346 chooser->insertItem( (*it)->name() );
347 old_sizeHint = descriptionLabel->sizeHint();
348 descriptionLabel->setText( (*it)->comment() );
349 if (descriptionLabel->sizeHint().width()*descriptionLabel->sizeHint().height() > old_sizeHint.width()*old_sizeHint.height())
350 descriptionLabel->setMinimumSize(descriptionLabel->sizeHint() );
351 }
352 descriptionLabel->show();
353
354 connect( chooser, TQ_SIGNAL( activated( int ) ),
355 this, TQ_SLOT( pluginChanged( int ) ) );
356
357 layout->addWidget( label );
358 layout->addWidget( chooser );
359 layout->addWidget( descriptionLabel );
360 layout->addStretch( 1 );
361
362 pluginChanged( 0 );
363}
364
365KWMailMergeChoosePluginDialog::~KWMailMergeChoosePluginDialog()
366{
367}
368
369int KWMailMergeChoosePluginDialog::currentPlugin() const
370{
371 return chooser->currentItem();
372}
373
374void KWMailMergeChoosePluginDialog::pluginChanged( int pos )
375{
376 descriptionLabel->setText( (*pluginOffers.at( pos ))->comment() );
377}
378
379/******************************************************************
380 *
381 * Class: KWMailMergeConfigDialog
382 *
383 ******************************************************************/
384
385KWMailMergeConfigDialog::KWMailMergeConfigDialog(TQWidget *parent,KWMailMergeDataBase *db)
386 : KDialogBase(Plain, i18n( "Mail Merge Setup" ), Close, Close, parent, "", true )
387{
388 db_=db;
389 TQWidget *back = plainPage();
390 TQVBoxLayout *layout=new TQVBoxLayout(back);
391// TQVBox *back = new TQVBox( page );
392 layout->setSpacing( KDialog::spacingHint() );
393
394// TQVBox *row1 = new TQVBox( back );
395// row1->setSpacing( KDialog::spacingHint() );
396
397 TQLabel *l = new TQLabel( i18n( "Datasource:" ),back );
398// l->setMaximumHeight( l->sizeHint().height() );
399 layout->addWidget(l);
400
401 TQHBox *row1=new TQHBox(back);
402 layout->addWidget(row1);
403 row1->setSpacing( KDialog::spacingHint() );
404 edit=new TQPushButton(i18n("Edit Current..."),row1);
405 create=new TQPushButton(i18n("Create New..."),row1);
406 open=new TQPushButton(i18n("Open Existing..."),row1);
407
408 KSeparator *separator1 = new KSeparator(back);
409 layout->addWidget(separator1);
410
411 l = new TQLabel( i18n( "Merging:" ),back );
412 layout->addWidget(l);
413// l->setMaximumHeight( l->sizeHint().height() );
414 TQHBox *row2=new TQHBox(back);
415 layout->addWidget(row2);
416 row2->setSpacing( KDialog::spacingHint() );
417 preview=new TQPushButton(i18n("Print Preview..."),row2);
418 document=new TQPushButton(i18n("Create New Document"),row2);
419 document->hide();
420 (void) new TQWidget(row2);
421 layout->addStretch();
422
423 KSeparator *separator2 = new KSeparator(back);
424 layout->addWidget(separator2);
425
426 enableDisableEdit();
427
428 connect(edit,TQ_SIGNAL(clicked()), this, TQ_SLOT(slotEditClicked()));
429 connect(create,TQ_SIGNAL(clicked()),this,TQ_SLOT(slotCreateClicked()));
430 connect(open,TQ_SIGNAL(clicked()),this,TQ_SLOT(slotOpenClicked()));
431 connect(preview,TQ_SIGNAL(clicked()),this,TQ_SLOT(slotPreviewClicked()));
432 connect(document,TQ_SIGNAL(clicked()),this,TQ_SLOT(slotDocumentClicked()));
433}
434
435void KWMailMergeConfigDialog::enableDisableEdit()
436{
437 if (!db_->plugin)
438 {
439 preview->setEnabled(false);
440 document->setEnabled(false);
441 edit->setEnabled(false);
442 }
443 else
444 {
445 preview->setEnabled(true);
446 document->setEnabled(true);
447 edit->setEnabled(true);
448 }
449}
450
451void KWMailMergeConfigDialog::slotEditClicked()
452{db_->action=KWSLEdit;
453 if (db_->plugin) db_->plugin->showConfigDialog((TQWidget*)parent(),KWSLEdit);
454}
455
456void KWMailMergeConfigDialog::slotCreateClicked()
457{
458 db_->action=KWSLCreate;
459 doNewActions();
460//done(TQDialog::Accepted);
461}
462
463void KWMailMergeConfigDialog::doNewActions()
464{
465 int tmpVersion;
466 KWMailMergeDataSource *tmpPlugin=db_->openPluginFor(db_->action,tmpVersion);
467 if (tmpPlugin)
468 {
469 if (db_->askUserForConfirmationAndConfig(tmpPlugin,true,this,tmpVersion))
470
471 enableDisableEdit();
472 }
473}
474
475
476void KWMailMergeConfigDialog::slotOpenClicked()
477{
478 db_->action=KWSLOpen;
479 doNewActions();
480}
481
482void KWMailMergeConfigDialog::slotPreviewClicked()
483{
484 db_->action=KWSLMergePreview;
485 TDEMainWindow *mw=dynamic_cast<TDEMainWindow*>(((TQWidget *)parent())->topLevelWidget());
486 if (mw)
487 {
488 TDEAction *ac=mw->actionCollection()->action(KStdAction::stdName(KStdAction::PrintPreview));
489 if (ac) ac->activate();
490 else kdWarning()<<"Toplevel doesn't provide a print preview action"<<endl;
491 }
492 else
493 kdWarning()<<"Toplevel is no TDEMainWindow->no preview"<<endl;
494}
495
496void KWMailMergeConfigDialog::slotDocumentClicked()
497{
498 db_->action=KWSLMergeDocument;
499 done(TQDialog::Accepted);
500}
501
502KWMailMergeConfigDialog::~KWMailMergeConfigDialog()
503{
504}
505
506/******************************************************************
507 *
508 * Class: KWMailMergeVariableInsertDia
509 *
510 ******************************************************************/
511
512KWMailMergeVariableInsertDia::KWMailMergeVariableInsertDia( TQWidget *parent, KWMailMergeDataBase *db )
513 : KDialogBase( Plain, i18n( "Mail Merge - Variable Name" ),
514 Ok | Cancel, Ok, parent, "", true )
515{
516 m_db=db;
517 TQWidget *page = plainPage();
518
519 TQVBoxLayout *layout = new TQVBoxLayout( page, marginHint(), spacingHint() );
520 layout->setAutoAdd( true );
521
522 TQLabel *l = new TQLabel( i18n( "Name:" ), page );
523 l->setMaximumHeight( l->sizeHint().height() );
524 names = new TQListBox( page );
525
526 TQMap< TQString, TQString >::ConstIterator it = db->getRecordEntries().begin();
527 for ( ; it != db->getRecordEntries().end(); ++it )
528 names->insertItem( m_db->version() ?it.data():it.key(), -1 );
529
530 setInitialSize( TQSize( 350, 400 ) );
531 connect( names, TQ_SIGNAL( selectionChanged() ),
532 this, TQ_SLOT( slotSelectionChanged() ) );
533 connect( names, TQ_SIGNAL( doubleClicked( TQListBoxItem* ) ),
534 this, TQ_SLOT( slotOk() ) );
535
536 setFocus();
537 enableButtonOK( names->currentItem() != -1 );
538}
539
540void KWMailMergeVariableInsertDia::slotSelectionChanged()
541{
542 enableButtonOK( names->currentItem() != -1 );
543}
544
545TQString KWMailMergeVariableInsertDia::getName() const
546{
547 if (m_db->version()>=1) {
548 TQString description=names->text(names->currentItem());
549 TQMap< TQString, TQString >::ConstIterator it = m_db->getRecordEntries().begin();
550 for ( ; it != m_db->getRecordEntries().end(); ++it )
551 if (description==it.data()) {
552 return it.key();
553 }
554 Q_ASSERT(0);
555 return ""; // can't happen
556 }
557 else
558 return names->text( names->currentItem() );
559}
DCOP interface for the MailMergeDataBase class.