00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "KWDocument.h"
00023 #include "KWMailMergeDataBase.h"
00024 #include "KWMailMergeDataBase.moc"
00025 #include <KoCustomVariablesDia.h>
00026 #include "defs.h"
00027
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030 #include <kstdaction.h>
00031 #include <kaction.h>
00032 #include <kmessagebox.h>
00033 #include <kseparator.h>
00034 #include <kservice.h>
00035 #include <kmainwindow.h>
00036
00037 #include <qfile.h>
00038 #include <qvbox.h>
00039 #include <qspinbox.h>
00040 #include <qlayout.h>
00041 #include <qlabel.h>
00042 #include <qpushbutton.h>
00043
00044
00045
00046
00047
00048
00049
00050 KWMailMergeDataBase::KWMailMergeDataBase( KWDocument *doc_ )
00051 : QObject(doc_,doc_->dcopObject()->objId()+".MailMergeDataBase"),
00052 KWordMailMergeDatabaseIface(QCString(doc_->dcopObject()->objId()+".MailMergeDataBase")),
00053 m_version(0),
00054 doc( doc_ )
00055 {
00056 plugin=0;
00057 rejectdcopcall=false;
00058 }
00059
00060 QStringList KWMailMergeDataBase::availablePlugins()
00061 {
00062 QStringList tmp;
00063 KTrader::OfferList pluginOffers=KTrader::self()->query(QString::fromLatin1("KWord/MailMergePlugin"),QString::null);
00064 for (KTrader::OfferList::Iterator it=pluginOffers.begin();*it;++it)
00065 {
00066 tmp.append((*it)->property("X-KDE-InternalName").toString());
00067 kdDebug()<<"Found mail merge plugin: "<< (*it)->name()<<endl;
00068 }
00069 return tmp;
00070 }
00071
00072 bool KWMailMergeDataBase::isConfigDialogShown()
00073 {
00074 return rejectdcopcall;
00075 }
00076
00077 bool KWMailMergeDataBase::loadPlugin(const QString &name,const QString &command)
00078 {
00079 if (rejectdcopcall)return false;
00080 QString constrain=QString("[X-KDE-InternalName] =='"+name+"'");
00081 kdDebug()<<constrain<<endl;
00082 KTrader::OfferList pluginOffers=KTrader::self()->query(QString::fromLatin1("KWord/MailMergePlugin"),constrain);
00083 KService::Ptr it=pluginOffers.first();
00084
00085 QVariant verProp=it->property("X-KDE-PluginVersion");
00086 int version=verProp.toInt();
00087
00088 if (it)
00089 {
00090 KWMailMergeDataSource *tmp=loadPlugin(it->library());
00091 if (!tmp)
00092 {
00093 kdDebug()<<"Couldn't load plugin"<<endl;
00094 return false;
00095 }
00096
00097 if (command=="silent") {
00098 return askUserForConfirmationAndConfig(tmp,false,0,version);
00099 }
00100 else
00101 {
00102 if (command=="open") action=KWSLOpen;
00103 else if (command=="create") action=KWSLCreate;
00104 else action=KWSLUnspecified;
00105 return askUserForConfirmationAndConfig(tmp,true,0,version);
00106 }
00107 }
00108 else
00109 {
00110 kdDebug()<<"No plugin found"<<endl;
00111 return false;
00112 }
00113 }
00114
00115 KWMailMergeDataSource *KWMailMergeDataBase::openPluginFor(int type,int &version)
00116 {
00117 version=0;
00118 KWMailMergeDataSource *ret=0;
00119 QString constrain=QString("'%1' in [X-KDE-Capabilities]").arg(((type==KWSLCreate)?KWSLCreate_text:KWSLOpen_text));
00120 kdDebug()<<constrain<<endl;
00121 KTrader::OfferList pluginOffers=KTrader::self()->query(QString::fromLatin1("KWord/MailMergePlugin"),constrain);
00122
00123
00124 for (KTrader::OfferList::Iterator it=pluginOffers.begin();*it;++it)
00125 {
00126 kdDebug()<<"Found mail merge plugin: "<< (*it)->name()<<endl;
00127 }
00128
00129 if (!pluginOffers.count())
00130 {
00131
00132 kdDebug()<<"No plugins found"<<endl;
00133 KMessageBox::sorry(0,i18n("No plugins supporting the requested action were found."));
00134 }
00135 else
00136 {
00137
00138 KWMailMergeChoosePluginDialog *dia=new KWMailMergeChoosePluginDialog(pluginOffers);
00139 if (dia->exec()==QDialog::Accepted)
00140 {
00141 QVariant verProp=(*(pluginOffers.at(dia->currentPlugin())))->property("X-KDE-PluginVersion");
00142 version=verProp.toInt();
00143
00144 ret=loadPlugin((*(pluginOffers.at(dia->currentPlugin())))->library());
00145 }
00146
00147 }
00148 return ret;
00149 }
00150
00151 KWMailMergeDataSource *KWMailMergeDataBase::loadPlugin(const QString& name)
00152 {
00153 if (!name.isEmpty())
00154 {
00155
00156
00157 KLibLoader *loader = KLibLoader::self();
00158
00159
00160 QString libname=name;
00161
00162 KLibrary *lib = loader->library(QFile::encodeName(libname));
00163 if (lib) {
00164
00165 QString factory=QString("create_%1").arg(name);
00166 void *create = lib->symbol(QFile::encodeName(factory));
00167
00168 if (create)
00169 {
00170
00171 KWMailMergeDataSource * (*func)(KInstance*,QObject*);
00172 func = (KWMailMergeDataSource* (*)(KInstance*,QObject*)) create;
00173 KWMailMergeDataSource *tmpsource =func(KWFactory::instance(),this);
00174 if (tmpsource)
00175 {
00176 QDataStream tmpstream(tmpsource->info,IO_WriteOnly);
00177 tmpstream<<name;
00178 }
00179 return tmpsource;
00180 }
00181 }
00182 kdWarning() << "Couldn't load plugin " << name << endl;
00183 }
00184 else
00185 kdWarning()<< "No plugin name specified" <<endl;
00186 return 0;
00187 }
00188
00189 bool KWMailMergeDataBase::isSampleRecord() {
00190 return (0>doc->mailMergeRecord());
00191 }
00192
00193 QString KWMailMergeDataBase::getValue( const QString &name, int record ) const
00194 {
00195 if (plugin)
00196 {
00197 if (record==-1) record=doc->mailMergeRecord();
00198 return plugin->getValue(name,record);
00199 }
00200 else
00201 return QString("");
00202 }
00203
00204
00205 void KWMailMergeDataBase::refresh(bool force)
00206 {
00207 if (plugin) plugin->refresh(force);
00208 }
00209
00210 const QMap< QString, QString > &KWMailMergeDataBase::getRecordEntries() const
00211 {
00212 if (plugin)
00213 return plugin->getRecordEntries();
00214 else
00215 return emptyMap;
00216 }
00217
00218 int KWMailMergeDataBase::getNumRecords() const
00219 {
00220 if (plugin)
00221 return plugin->getNumRecords();
00222 else
00223 return 0;
00224
00225 }
00226
00227
00228 void KWMailMergeDataBase::showConfigDialog(QWidget *par)
00229 {
00230 rejectdcopcall=true;
00231 KWMailMergeConfigDialog *dia=new KWMailMergeConfigDialog(par,this);
00232 dia->exec();
00233 delete dia;
00234 rejectdcopcall=false;
00235 }
00236
00237
00238 bool KWMailMergeDataBase::askUserForConfirmationAndConfig(KWMailMergeDataSource *tmpPlugin,bool config,QWidget *par,int version)
00239 {
00240 if (tmpPlugin)
00241 {
00242 bool replaceit=false;
00243 if (!config) replaceit=true;
00244 else
00245 replaceit=tmpPlugin->showConfigDialog(par,action);
00246 if (replaceit)
00247 {
00248 if (plugin)
00249 {
00250 if (KMessageBox::warningContinueCancel(par,
00251 i18n("Do you really want to replace the current datasource?"),
00252 QString::null,QString::null,QString::null,true)== KMessageBox::Cancel)
00253 {
00254 delete tmpPlugin;
00255 tmpPlugin=0;
00256 return false;
00257 }
00258 delete plugin;
00259 }
00260 m_version=version;
00261 plugin=tmpPlugin;
00262 }
00263 else
00264 {
00265 delete tmpPlugin;
00266 tmpPlugin=0;
00267 return false;
00268 }
00269 }
00270 tmpPlugin->setObjId(QCString(objId()+".MailMergePlugin"));
00271 return true;
00272 }
00273
00274
00275
00276 QDomElement KWMailMergeDataBase::save(QDomDocument &doc) const
00277 {
00278 kdDebug()<<"KWMailMergeDataBase::save()"<<endl;
00279 QDomElement parentElem=doc.createElement("MAILMERGE");
00280 if (plugin)
00281 {
00282 kdDebug()<<"KWMailMergeDataBase::save() There is really something to save"<<endl;
00283 QDomElement el=doc.createElement(QString::fromLatin1("PLUGIN"));
00284
00285 QDataStream ds(plugin->info,IO_ReadOnly);
00286 QString libname;
00287 ds>>libname;
00288 el.setAttribute("library",libname);
00289 parentElem.appendChild(el);
00290 kdDebug()<<"KWMailMergeDataBase::save() Calling datasource save()"<<endl;
00291 QDomElement el2=doc.createElement(QString::fromLatin1("DATASOURCE"));
00292 plugin->save(doc,el2);
00293 parentElem.appendChild(el2);
00294
00295 }
00296 kdDebug()<<"KWMailMergeDataBase::save() leaving now"<<endl;
00297 return parentElem;
00298
00299 }
00300
00301 void KWMailMergeDataBase::load( const QDomElement& parentElem )
00302 {
00303 QDomNode dn=parentElem.namedItem("PLUGIN");
00304 if (dn.isNull()) return;
00305 QDomElement el=dn.toElement();
00306 plugin=loadPlugin(el.attribute("library"));
00307
00308 dn=parentElem.namedItem("DATASOURCE");
00309 if (dn.isNull()) return;
00310 el=dn.toElement();
00311 if (plugin) plugin->load(el);
00312 }
00313
00314
00315 int KWMailMergeDataBase::version() {
00316 kdDebug()<<"KWMailMergeDataBase::version:"<<m_version<<endl;
00317 return m_version;
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327 KWMailMergeChoosePluginDialog::KWMailMergeChoosePluginDialog( KTrader::OfferList offers )
00328 : KDialogBase( Plain, i18n( "Mail Merge Setup" ), Ok | Cancel, Ok,
00329 0, "", true ), pluginOffers( offers )
00330 {
00331 QWidget *back = plainPage();
00332 QVBoxLayout *layout = new QVBoxLayout( back, 0, spacingHint() );
00333
00334 QLabel *label = new QLabel( i18n( "&Available sources:" ), back );
00335 chooser = new QComboBox( false, back );
00336 label->setBuddy( chooser );
00337 descriptionLabel = new QLabel( back );
00338 descriptionLabel->hide();
00339 descriptionLabel->setAlignment( WordBreak );
00340 descriptionLabel->setFrameShape( QFrame::Box );
00341 descriptionLabel->setFrameShadow( QFrame::Sunken );
00342
00343 QSize old_sizeHint;
00344 for ( KTrader::OfferList::Iterator it = pluginOffers.begin(); *it; ++it )
00345 {
00346 chooser->insertItem( (*it)->name() );
00347 old_sizeHint = descriptionLabel->sizeHint();
00348 descriptionLabel->setText( (*it)->comment() );
00349 if (descriptionLabel->sizeHint().width()*descriptionLabel->sizeHint().height() > old_sizeHint.width()*old_sizeHint.height())
00350 descriptionLabel->setMinimumSize(descriptionLabel->sizeHint() );
00351 }
00352 descriptionLabel->show();
00353
00354 connect( chooser, SIGNAL( activated( int ) ),
00355 this, SLOT( pluginChanged( int ) ) );
00356
00357 layout->addWidget( label );
00358 layout->addWidget( chooser );
00359 layout->addWidget( descriptionLabel );
00360 layout->addStretch( 1 );
00361
00362 pluginChanged( 0 );
00363 }
00364
00365 KWMailMergeChoosePluginDialog::~KWMailMergeChoosePluginDialog()
00366 {
00367 }
00368
00369 int KWMailMergeChoosePluginDialog::currentPlugin() const
00370 {
00371 return chooser->currentItem();
00372 }
00373
00374 void KWMailMergeChoosePluginDialog::pluginChanged( int pos )
00375 {
00376 descriptionLabel->setText( (*pluginOffers.at( pos ))->comment() );
00377 }
00378
00379
00380
00381
00382
00383
00384
00385 KWMailMergeConfigDialog::KWMailMergeConfigDialog(QWidget *parent,KWMailMergeDataBase *db)
00386 : KDialogBase(Plain, i18n( "Mail Merge Setup" ), Close, Close, parent, "", true )
00387 {
00388 db_=db;
00389 QWidget *back = plainPage();
00390 QVBoxLayout *layout=new QVBoxLayout(back);
00391
00392 layout->setSpacing( KDialog::spacingHint() );
00393
00394
00395
00396
00397 QLabel *l = new QLabel( i18n( "Datasource:" ),back );
00398
00399 layout->addWidget(l);
00400
00401 QHBox *row1=new QHBox(back);
00402 layout->addWidget(row1);
00403 row1->setSpacing( KDialog::spacingHint() );
00404 edit=new QPushButton(i18n("Edit Current..."),row1);
00405 create=new QPushButton(i18n("Create New..."),row1);
00406 open=new QPushButton(i18n("Open Existing..."),row1);
00407
00408 KSeparator *separator1 = new KSeparator(back);
00409 layout->addWidget(separator1);
00410
00411 l = new QLabel( i18n( "Merging:" ),back );
00412 layout->addWidget(l);
00413
00414 QHBox *row2=new QHBox(back);
00415 layout->addWidget(row2);
00416 row2->setSpacing( KDialog::spacingHint() );
00417 preview=new QPushButton(i18n("Print Preview..."),row2);
00418 document=new QPushButton(i18n("Create New Document"),row2);
00419 document->hide();
00420 (void) new QWidget(row2);
00421 layout->addStretch();
00422
00423 KSeparator *separator2 = new KSeparator(back);
00424 layout->addWidget(separator2);
00425
00426 enableDisableEdit();
00427
00428 connect(edit,SIGNAL(clicked()), this, SLOT(slotEditClicked()));
00429 connect(create,SIGNAL(clicked()),this,SLOT(slotCreateClicked()));
00430 connect(open,SIGNAL(clicked()),this,SLOT(slotOpenClicked()));
00431 connect(preview,SIGNAL(clicked()),this,SLOT(slotPreviewClicked()));
00432 connect(document,SIGNAL(clicked()),this,SLOT(slotDocumentClicked()));
00433 }
00434
00435 void KWMailMergeConfigDialog::enableDisableEdit()
00436 {
00437 if (!db_->plugin)
00438 {
00439 preview->setEnabled(false);
00440 document->setEnabled(false);
00441 edit->setEnabled(false);
00442 }
00443 else
00444 {
00445 preview->setEnabled(true);
00446 document->setEnabled(true);
00447 edit->setEnabled(true);
00448 }
00449 }
00450
00451 void KWMailMergeConfigDialog::slotEditClicked()
00452 {db_->action=KWSLEdit;
00453 if (db_->plugin) db_->plugin->showConfigDialog((QWidget*)parent(),KWSLEdit);
00454 }
00455
00456 void KWMailMergeConfigDialog::slotCreateClicked()
00457 {
00458 db_->action=KWSLCreate;
00459 doNewActions();
00460
00461 }
00462
00463 void KWMailMergeConfigDialog::doNewActions()
00464 {
00465 int tmpVersion;
00466 KWMailMergeDataSource *tmpPlugin=db_->openPluginFor(db_->action,tmpVersion);
00467 if (tmpPlugin)
00468 {
00469 if (db_->askUserForConfirmationAndConfig(tmpPlugin,true,this,tmpVersion))
00470
00471 enableDisableEdit();
00472 }
00473 }
00474
00475
00476 void KWMailMergeConfigDialog::slotOpenClicked()
00477 {
00478 db_->action=KWSLOpen;
00479 doNewActions();
00480 }
00481
00482 void KWMailMergeConfigDialog::slotPreviewClicked()
00483 {
00484 db_->action=KWSLMergePreview;
00485 KMainWindow *mw=dynamic_cast<KMainWindow*>(((QWidget *)parent())->topLevelWidget());
00486 if (mw)
00487 {
00488 KAction *ac=mw->actionCollection()->action(KStdAction::stdName(KStdAction::PrintPreview));
00489 if (ac) ac->activate();
00490 else kdWarning()<<"Toplevel doesn't provide a print preview action"<<endl;
00491 }
00492 else
00493 kdWarning()<<"Toplevel is no KMainWindow->no preview"<<endl;
00494 }
00495
00496 void KWMailMergeConfigDialog::slotDocumentClicked()
00497 {
00498 db_->action=KWSLMergeDocument;
00499 done(QDialog::Accepted);
00500 }
00501
00502 KWMailMergeConfigDialog::~KWMailMergeConfigDialog()
00503 {
00504 }
00505
00506
00507
00508
00509
00510
00511
00512 KWMailMergeVariableInsertDia::KWMailMergeVariableInsertDia( QWidget *parent, KWMailMergeDataBase *db )
00513 : KDialogBase( Plain, i18n( "Mail Merge - Variable Name" ),
00514 Ok | Cancel, Ok, parent, "", true )
00515 {
00516 m_db=db;
00517 QWidget *page = plainPage();
00518
00519 QVBoxLayout *layout = new QVBoxLayout( page, marginHint(), spacingHint() );
00520 layout->setAutoAdd( true );
00521
00522 QLabel *l = new QLabel( i18n( "Name:" ), page );
00523 l->setMaximumHeight( l->sizeHint().height() );
00524 names = new QListBox( page );
00525
00526 QMap< QString, QString >::ConstIterator it = db->getRecordEntries().begin();
00527 for ( ; it != db->getRecordEntries().end(); ++it )
00528 names->insertItem( m_db->version() ?it.data():it.key(), -1 );
00529
00530 setInitialSize( QSize( 350, 400 ) );
00531 connect( names, SIGNAL( selectionChanged() ),
00532 this, SLOT( slotSelectionChanged() ) );
00533 connect( names, SIGNAL( doubleClicked( QListBoxItem* ) ),
00534 this, SLOT( slotOk() ) );
00535
00536 setFocus();
00537 enableButtonOK( names->currentItem() != -1 );
00538 }
00539
00540 void KWMailMergeVariableInsertDia::slotSelectionChanged()
00541 {
00542 enableButtonOK( names->currentItem() != -1 );
00543 }
00544
00545 QString KWMailMergeVariableInsertDia::getName() const
00546 {
00547 if (m_db->version()>=1) {
00548 QString description=names->text(names->currentItem());
00549 QMap< QString, QString >::ConstIterator it = m_db->getRecordEntries().begin();
00550 for ( ; it != m_db->getRecordEntries().end(); ++it )
00551 if (description==it.data()) {
00552 return it.key();
00553 }
00554 Q_ASSERT(0);
00555 return "";
00556 }
00557 else
00558 return names->text( names->currentItem() );
00559 }