22#include "KWDocument.h"
23#include "KWMailMergeDataBase.h"
24#include "KWMailMergeDataBase.moc"
25#include <KoCustomVariablesDia.h>
30#include <kstdaction.h>
32#include <tdemessagebox.h>
33#include <kseparator.h>
35#include <tdemainwindow.h>
42#include <tqpushbutton.h>
50KWMailMergeDataBase::KWMailMergeDataBase( KWDocument *doc_ )
51 : TQObject(doc_,doc_->dcopObject()->objId()+
".MailMergeDataBase"),
60TQStringList KWMailMergeDataBase::availablePlugins()
63 TDETrader::OfferList pluginOffers=TDETrader::self()->query(TQString::fromLatin1(
"KWord/MailMergePlugin"),TQString());
64 for (TDETrader::OfferList::Iterator it=pluginOffers.begin();*it;++it)
66 tmp.append((*it)->property(
"X-TDE-InternalName").toString());
67 kdDebug()<<
"Found mail merge plugin: "<< (*it)->name()<<endl;
72bool KWMailMergeDataBase::isConfigDialogShown()
74 return rejectdcopcall;
77bool KWMailMergeDataBase::loadPlugin(
const TQString &name,
const TQString &command)
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();
85 TQVariant verProp=it->property(
"X-TDE-PluginVersion");
86 int version=verProp.toInt();
90 KWMailMergeDataSource *tmp=loadPlugin(it->library());
93 kdDebug()<<
"Couldn't load plugin"<<endl;
97 if (command==
"silent") {
98 return askUserForConfirmationAndConfig(tmp,
false,0,version);
102 if (command==
"open") action=KWSLOpen;
103 else if (command==
"create") action=KWSLCreate;
104 else action=KWSLUnspecified;
105 return askUserForConfirmationAndConfig(tmp,
true,0,version);
110 kdDebug()<<
"No plugin found"<<endl;
115KWMailMergeDataSource *KWMailMergeDataBase::openPluginFor(
int type,
int &version)
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);
124 for (TDETrader::OfferList::Iterator it=pluginOffers.begin();*it;++it)
126 kdDebug()<<
"Found mail merge plugin: "<< (*it)->name()<<endl;
129 if (!pluginOffers.count())
132 kdDebug()<<
"No plugins found"<<endl;
133 KMessageBox::sorry(0,i18n(
"No plugins supporting the requested action were found."));
138 KWMailMergeChoosePluginDialog *dia=
new KWMailMergeChoosePluginDialog(pluginOffers);
139 if (dia->exec()==TQDialog::Accepted)
141 TQVariant verProp=(*(pluginOffers.at(dia->currentPlugin())))->property(
"X-TDE-PluginVersion");
142 version=verProp.toInt();
144 ret=loadPlugin((*(pluginOffers.at(dia->currentPlugin())))->library());
151KWMailMergeDataSource *KWMailMergeDataBase::loadPlugin(
const TQString& name)
157 KLibLoader *loader = KLibLoader::self();
160 TQString libname=name;
162 KLibrary *lib = loader->library(TQFile::encodeName(libname));
165 TQString factory=TQString(
"create_%1").arg(name);
166 void *create = lib->symbol(TQFile::encodeName(factory));
171 KWMailMergeDataSource * (*func)(TDEInstance*,TQObject*);
172 func = (KWMailMergeDataSource* (*)(TDEInstance*,TQObject*)) create;
173 KWMailMergeDataSource *tmpsource =func(KWFactory::instance(),
this);
176 TQDataStream tmpstream(tmpsource->info,IO_WriteOnly);
182 kdWarning() <<
"Couldn't load plugin " << name << endl;
185 kdWarning()<<
"No plugin name specified" <<endl;
189bool KWMailMergeDataBase::isSampleRecord() {
190 return (0>doc->mailMergeRecord());
193TQString KWMailMergeDataBase::getValue(
const TQString &name,
int record )
const
197 if (record==-1) record=doc->mailMergeRecord();
198 return plugin->getValue(name,record);
205void KWMailMergeDataBase::refresh(
bool force)
207 if (plugin) plugin->refresh(force);
210const TQMap< TQString, TQString > &KWMailMergeDataBase::getRecordEntries()
const
213 return plugin->getRecordEntries();
218int KWMailMergeDataBase::getNumRecords()
const
221 return plugin->getNumRecords();
228void KWMailMergeDataBase::showConfigDialog(TQWidget *par)
231 KWMailMergeConfigDialog *dia=
new KWMailMergeConfigDialog(par,
this);
234 rejectdcopcall=
false;
238bool KWMailMergeDataBase::askUserForConfirmationAndConfig(KWMailMergeDataSource *tmpPlugin,
bool config,TQWidget *par,
int version)
242 bool replaceit=
false;
243 if (!config) replaceit=
true;
245 replaceit=tmpPlugin->showConfigDialog(par,action);
250 if (KMessageBox::warningContinueCancel(par,
251 i18n(
"Do you really want to replace the current datasource?"),
252 TQString(),TQString(),TQString(),
true)== KMessageBox::Cancel)
270 tmpPlugin->setObjId(TQCString(objId()+
".MailMergePlugin"));
276TQDomElement KWMailMergeDataBase::save(TQDomDocument &doc)
const
278 kdDebug()<<
"KWMailMergeDataBase::save()"<<endl;
279 TQDomElement parentElem=doc.createElement(
"MAILMERGE");
282 kdDebug()<<
"KWMailMergeDataBase::save() There is really something to save"<<endl;
283 TQDomElement el=doc.createElement(TQString::fromLatin1(
"PLUGIN"));
285 TQDataStream ds(plugin->info,IO_ReadOnly);
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);
296 kdDebug()<<
"KWMailMergeDataBase::save() leaving now"<<endl;
301void KWMailMergeDataBase::load(
const TQDomElement& parentElem )
303 TQDomNode dn=parentElem.namedItem(
"PLUGIN");
304 if (dn.isNull())
return;
305 TQDomElement el=dn.toElement();
306 plugin=loadPlugin(el.attribute(
"library"));
308 dn=parentElem.namedItem(
"DATASOURCE");
309 if (dn.isNull())
return;
311 if (plugin) plugin->load(el);
315int KWMailMergeDataBase::version() {
316 kdDebug()<<
"KWMailMergeDataBase::version:"<<m_version<<endl;
327KWMailMergeChoosePluginDialog::KWMailMergeChoosePluginDialog( TDETrader::OfferList offers )
328 : KDialogBase( Plain, i18n(
"Mail Merge Setup" ), Ok | Cancel, Ok,
329 0,
"", true ), pluginOffers( offers )
331 TQWidget *back = plainPage();
332 TQVBoxLayout *layout =
new TQVBoxLayout( back, 0, spacingHint() );
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 );
344 for ( TDETrader::OfferList::Iterator it = pluginOffers.begin(); *it; ++it )
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() );
352 descriptionLabel->show();
354 connect( chooser, TQ_SIGNAL( activated(
int ) ),
355 this, TQ_SLOT( pluginChanged(
int ) ) );
357 layout->addWidget( label );
358 layout->addWidget( chooser );
359 layout->addWidget( descriptionLabel );
360 layout->addStretch( 1 );
365KWMailMergeChoosePluginDialog::~KWMailMergeChoosePluginDialog()
369int KWMailMergeChoosePluginDialog::currentPlugin()
const
371 return chooser->currentItem();
374void KWMailMergeChoosePluginDialog::pluginChanged(
int pos )
376 descriptionLabel->setText( (*pluginOffers.at( pos ))->comment() );
385KWMailMergeConfigDialog::KWMailMergeConfigDialog(TQWidget *parent,KWMailMergeDataBase *db)
386 : KDialogBase(Plain, i18n(
"Mail Merge Setup" ), Close, Close, parent,
"", true )
389 TQWidget *back = plainPage();
390 TQVBoxLayout *layout=
new TQVBoxLayout(back);
392 layout->setSpacing( KDialog::spacingHint() );
397 TQLabel *l =
new TQLabel( i18n(
"Datasource:" ),back );
399 layout->addWidget(l);
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);
408 KSeparator *separator1 =
new KSeparator(back);
409 layout->addWidget(separator1);
411 l =
new TQLabel( i18n(
"Merging:" ),back );
412 layout->addWidget(l);
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);
420 (void)
new TQWidget(row2);
421 layout->addStretch();
423 KSeparator *separator2 =
new KSeparator(back);
424 layout->addWidget(separator2);
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()));
435void KWMailMergeConfigDialog::enableDisableEdit()
439 preview->setEnabled(
false);
440 document->setEnabled(
false);
441 edit->setEnabled(
false);
445 preview->setEnabled(
true);
446 document->setEnabled(
true);
447 edit->setEnabled(
true);
451void KWMailMergeConfigDialog::slotEditClicked()
452{db_->action=KWSLEdit;
453 if (db_->plugin) db_->plugin->showConfigDialog((TQWidget*)parent(),KWSLEdit);
456void KWMailMergeConfigDialog::slotCreateClicked()
458 db_->action=KWSLCreate;
463void KWMailMergeConfigDialog::doNewActions()
466 KWMailMergeDataSource *tmpPlugin=db_->openPluginFor(db_->action,tmpVersion);
469 if (db_->askUserForConfirmationAndConfig(tmpPlugin,
true,
this,tmpVersion))
476void KWMailMergeConfigDialog::slotOpenClicked()
478 db_->action=KWSLOpen;
482void KWMailMergeConfigDialog::slotPreviewClicked()
484 db_->action=KWSLMergePreview;
485 TDEMainWindow *mw=
dynamic_cast<TDEMainWindow*
>(((TQWidget *)parent())->topLevelWidget());
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;
493 kdWarning()<<
"Toplevel is no TDEMainWindow->no preview"<<endl;
496void KWMailMergeConfigDialog::slotDocumentClicked()
498 db_->action=KWSLMergeDocument;
499 done(TQDialog::Accepted);
502KWMailMergeConfigDialog::~KWMailMergeConfigDialog()
512KWMailMergeVariableInsertDia::KWMailMergeVariableInsertDia( TQWidget *parent, KWMailMergeDataBase *db )
513 : KDialogBase( Plain, i18n(
"Mail Merge - Variable Name" ),
514 Ok | Cancel, Ok, parent,
"", true )
517 TQWidget *page = plainPage();
519 TQVBoxLayout *layout =
new TQVBoxLayout( page, marginHint(), spacingHint() );
520 layout->setAutoAdd(
true );
522 TQLabel *l =
new TQLabel( i18n(
"Name:" ), page );
523 l->setMaximumHeight( l->sizeHint().height() );
524 names =
new TQListBox( page );
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 );
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() ) );
537 enableButtonOK( names->currentItem() != -1 );
540void KWMailMergeVariableInsertDia::slotSelectionChanged()
542 enableButtonOK( names->currentItem() != -1 );
545TQString KWMailMergeVariableInsertDia::getName()
const
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()) {
558 return names->text( names->currentItem() );
DCOP interface for the MailMergeDataBase class.