lib Library API Documentation

koDocumentInfoDlg.cc

00001 /* This file is part of the KDE project
00002    Copyright (c) 2000 Simon Hausmann <hausmann@kde.org>
00003 
00004    $Id: koDocumentInfoDlg.cc,v 1.36 2003/08/29 13:13:33 faure Exp $
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <koDocumentInfoDlg.h>
00023 #include <koDocumentInfo.h>
00024 #include <koGlobal.h>
00025 #include <koStore.h>
00026 
00027 #include <sys/stat.h>
00028 #include <unistd.h>
00029 #include <assert.h>
00030 
00031 #include <qlabel.h>
00032 #include <qlineedit.h>
00033 #include <qmultilineedit.h>
00034 #include <qbuffer.h>
00035 #include <qdom.h>
00036 #include <qdir.h>
00037 #include <qpushbutton.h>
00038 
00039 #include <kabc/addressee.h>
00040 #include <kabc/stdaddressbook.h>
00041 #include <kdeversion.h>
00042 #include <klocale.h>
00043 #include <ktar.h>
00044 #include <kdebug.h>
00045 #include <ktempfile.h>
00046 #include <kmimetype.h>
00047 #include <qlayout.h>
00048 #include <qgrid.h>
00049 #include <kfilterdev.h>
00050 
00051 class KoDocumentInfoDlg::KoDocumentInfoDlgPrivate
00052 {
00053 public:
00054   KoDocumentInfoDlgPrivate()
00055   {
00056   }
00057   ~KoDocumentInfoDlgPrivate()
00058   {
00059   }
00060 
00061   KoDocumentInfo *m_info;
00062 
00063   QLineEdit *m_leFullName;
00064   QLineEdit *m_leInitial;
00065   QLineEdit *m_leAuthorTitle;
00066   QLineEdit *m_leCompany;
00067   QLineEdit *m_leEmail;
00068   QLineEdit *m_leTelephone;
00069   QLineEdit *m_leFax;
00070   QLineEdit *m_leCountry;
00071   QLineEdit *m_lePostalCode;
00072   QLineEdit *m_leCity;
00073   QLineEdit *m_leStreet;
00074   QPushButton *m_pbLoadKABC;
00075 
00076   QLineEdit *m_leDocTitle;
00077   QMultiLineEdit *m_meAbstract;
00078 
00079   KConfig *m_emailCfg;
00080 
00081   bool m_bDeleteDialog;
00082   KDialogBase *m_dialog;
00083 };
00084 
00085 KoDocumentInfoDlg::KoDocumentInfoDlg( KoDocumentInfo *docInfo, QWidget *parent, const char *name,
00086                                       KDialogBase *dialog )
00087 : QObject( parent, "docinfodlg" )
00088 {
00089   d = new KoDocumentInfoDlgPrivate;
00090   d->m_info = docInfo;
00091   d->m_emailCfg = new KConfig( "emaildefaults", true );
00092 
00093   d->m_emailCfg->setGroup( "Defaults" );
00094 
00095   QString group = d->m_emailCfg->readEntry("Profile","Default");
00096 
00097   d->m_emailCfg->setGroup(QString("PROFILE_%1").arg(group));
00098 
00099   d->m_dialog = dialog;
00100   d->m_bDeleteDialog = false;
00101 
00102   if ( !dialog )
00103   {
00104     d->m_dialog = new KDialogBase( KDialogBase::Tabbed,
00105                                    i18n( "Document Information" ),
00106                                    KDialogBase::Ok | KDialogBase::Cancel,
00107                                    KDialogBase::Ok, parent, name, true, true );
00108     d->m_bDeleteDialog = true;
00109   }
00110 
00111   QStringList pages = docInfo->pages();
00112   QStringList::ConstIterator it = pages.begin();
00113   QStringList::ConstIterator end = pages.end();
00114   for (; it != end; ++it )
00115   {
00116     KoDocumentInfoPage *pg = docInfo->page( *it );
00117     if ( pg->inherits( "KoDocumentInfoAuthor" ) )
00118       addAuthorPage( static_cast<KoDocumentInfoAuthor *>( pg ) );
00119     else if ( pg->inherits( "KoDocumentInfoAbout" ) )
00120       addAboutPage( static_cast<KoDocumentInfoAbout *>( pg ) );
00121   }
00122 }
00123 
00124 KoDocumentInfoDlg::~KoDocumentInfoDlg()
00125 {
00126   delete d->m_emailCfg;
00127 
00128   if ( d->m_bDeleteDialog )
00129     delete d->m_dialog;
00130 
00131   delete d;
00132 }
00133 
00134 int KoDocumentInfoDlg::exec()
00135 {
00136   return d->m_dialog->exec();
00137 }
00138 
00139 KDialogBase *KoDocumentInfoDlg::dialog() const
00140 {
00141   return d->m_dialog;
00142 }
00143 
00144 void KoDocumentInfoDlg::loadFromKABC()
00145 {
00146 #if KDE_IS_VERSION( 3, 1, 90 )
00147   KABC::StdAddressBook *ab = static_cast<KABC::StdAddressBook*>
00148                              ( KABC::StdAddressBook::self() );
00149 
00150   if ( !ab )
00151     return;
00152 
00153   KABC::Addressee addr = ab->whoAmI();
00154   if ( addr.isEmpty() )
00155     return;
00156 
00157   d->m_leFullName->setText( addr.formattedName() );
00158   d->m_leInitial->setText( addr.givenName()[ 0 ] + ". " +
00159                            addr.familyName()[ 0 ] + "." );
00160   d->m_leAuthorTitle->setText( addr.title() );
00161   d->m_leCompany->setText( addr.organization() );
00162   d->m_leEmail->setText( addr.preferredEmail() );
00163 
00164   KABC::PhoneNumber phone = addr.phoneNumber( KABC::PhoneNumber::Home );
00165   d->m_leTelephone->setText( phone.number() );
00166   phone = addr.phoneNumber( KABC::PhoneNumber::Fax );
00167   d->m_leFax->setText( phone.number() );
00168 
00169   KABC::Address a = addr.address( KABC::Address::Home );
00170   d->m_leCountry->setText( a.country() );
00171   d->m_lePostalCode->setText( a.postalCode() );
00172   d->m_leCity->setText( a.locality() );
00173   d->m_leStreet->setText( a.street() );
00174 
00175   emit changed();
00176 #endif
00177 }
00178 
00179 void KoDocumentInfoDlg::addAuthorPage( KoDocumentInfoAuthor *authorInfo )
00180 {
00181   QFrame *page = d->m_dialog->addPage( i18n( "Author" ) );
00182   QGridLayout *layout = new QGridLayout( page, 11, 2, KDialog::marginHint(),
00183                                          KDialog::spacingHint() );
00184 
00185   layout->addWidget( new QLabel( i18n( "Name:" ), page ), 0, 0 );
00186   d->m_leFullName = new QLineEdit( authorInfo->fullName(), page );
00187   layout->addWidget( d->m_leFullName, 0, 1 );
00188 
00189   if ( authorInfo->fullName().isNull() ) // only if null. Empty means the user made it explicitly empty.
00190   {
00191     QString name = d->m_emailCfg->readEntry( "FullName" );
00192     if ( !name.isEmpty() )
00193       d->m_leFullName->setText( name );
00194   }
00195 
00196   layout->addWidget( new QLabel( i18n( "Initials:" ), page ), 1, 0 );
00197   d->m_leInitial = new QLineEdit( authorInfo->initial(), page );
00198   layout->addWidget( d->m_leInitial, 1, 1 );
00199 
00200   layout->addWidget( new QLabel( i18n( "Title:" ), page ), 2, 0 );
00201   d->m_leAuthorTitle = new QLineEdit( authorInfo->title(), page );
00202   layout->addWidget( d->m_leAuthorTitle, 2, 1 );
00203 
00204   layout->addWidget( new QLabel( i18n( "Company:" ), page ), 3, 0 );
00205   d->m_leCompany = new QLineEdit( authorInfo->company(), page );
00206   layout->addWidget( d->m_leCompany, 3, 1 );
00207 
00208   if ( authorInfo->company().isNull() )
00209   {
00210     QString name = d->m_emailCfg->readEntry( "Organization" );
00211     if ( !name.isEmpty() )
00212       d->m_leCompany->setText( name );
00213   }
00214 
00215   layout->addWidget( new QLabel( i18n( "Email:" ), page ), 4, 0 );
00216   d->m_leEmail = new QLineEdit( authorInfo->email(), page );
00217   layout->addWidget( d->m_leEmail, 4, 1 );
00218 
00219   if ( authorInfo->email().isNull() )
00220   {
00221     QString email = d->m_emailCfg->readEntry( "EmailAddress" );
00222     if ( !email.isEmpty() )
00223       d->m_leEmail->setText( email );
00224   }
00225 
00226   layout->addWidget( new QLabel( i18n( "Telephone:" ), page ), 5, 0 );
00227   d->m_leTelephone = new QLineEdit( authorInfo->telephone(), page );
00228   layout->addWidget( d->m_leTelephone, 5, 1 );
00229 
00230   layout->addWidget( new QLabel( i18n( "Fax:" ), page ), 6, 0 );
00231   d->m_leFax = new QLineEdit( authorInfo->fax(), page );
00232   layout->addWidget( d->m_leFax, 6, 1 );
00233 
00234   layout->addWidget( new QLabel( i18n( "Street:" ), page ), 7, 0 );
00235   d->m_leStreet = new QLineEdit( authorInfo->street(), page );
00236   layout->addWidget( d->m_leStreet, 7, 1 );
00237 
00238   layout->addWidget( new QLabel( i18n( "Postal code:" ), page ), 8, 0 );
00239   d->m_lePostalCode = new QLineEdit( authorInfo->postalCode(), page );
00240   layout->addWidget( d->m_lePostalCode, 8, 1 );
00241 
00242   layout->addWidget( new QLabel( i18n( "City:" ), page ), 9, 0 );
00243   d->m_leCity = new QLineEdit( authorInfo->city(), page );
00244   layout->addWidget( d->m_leCity, 9, 1 );
00245 
00246   layout->addWidget( new QLabel( i18n( "Country:" ), page ), 10, 0 );
00247   d->m_leCountry = new QLineEdit( authorInfo->country(), page );
00248   layout->addWidget( d->m_leCountry, 10, 1 );
00249 
00250   d->m_pbLoadKABC = new QPushButton( i18n( "Load From Address Book" ), page );
00251   layout->addMultiCellWidget( d->m_pbLoadKABC, 11, 11, 0, 1 );
00252 
00253   connect( d->m_leFullName, SIGNAL( textChanged( const QString & ) ),
00254            this, SIGNAL( changed() ) );
00255   connect( d->m_leInitial, SIGNAL( textChanged( const QString & ) ),
00256            this, SIGNAL( changed() ) );
00257 
00258   connect( d->m_leAuthorTitle, SIGNAL( textChanged( const QString & ) ),
00259            this, SIGNAL( changed() ) );
00260   connect( d->m_leCompany, SIGNAL( textChanged( const QString & ) ),
00261            this, SIGNAL( changed() ) );
00262   connect( d->m_leEmail, SIGNAL( textChanged( const QString & ) ),
00263            this, SIGNAL( changed() ) );
00264   connect( d->m_leTelephone, SIGNAL( textChanged( const QString & ) ),
00265            this, SIGNAL( changed() ) );
00266   connect( d->m_leFax, SIGNAL( textChanged( const QString & ) ),
00267            this, SIGNAL( changed() ) );
00268   connect( d->m_leCountry, SIGNAL( textChanged( const QString & ) ),
00269            this, SIGNAL( changed() ) );
00270   connect( d->m_lePostalCode, SIGNAL( textChanged( const QString & ) ),
00271            this, SIGNAL( changed() ) );
00272   connect( d->m_leCity, SIGNAL( textChanged( const QString & ) ),
00273            this, SIGNAL( changed() ) );
00274   connect( d->m_leStreet, SIGNAL( textChanged( const QString & ) ),
00275            this, SIGNAL( changed() ) );
00276   connect( d->m_pbLoadKABC, SIGNAL( clicked() ),
00277            this, SLOT( loadFromKABC() ) );
00278 }
00279 
00280 void KoDocumentInfoDlg::addAboutPage( KoDocumentInfoAbout *aboutInfo )
00281 {
00282   QFrame *page = d->m_dialog->addPage( i18n("about the document", "About") );
00283   QGridLayout *grid = new QGridLayout( page, 3, 2, KDialog::marginHint(), KDialog::spacingHint() );
00284 
00285   grid->addWidget( new QLabel( i18n( "Title:" ), page ), 0, 0);
00286   d->m_leDocTitle = new QLineEdit( aboutInfo->title(), page );
00287   grid->addWidget(d->m_leDocTitle, 0, 1);
00288 
00289   grid->addWidget(new QLabel( i18n( "Abstract:" ), page ), 1, 0, Qt::AlignTop );
00290 
00291   d->m_meAbstract = new QMultiLineEdit( page );
00292   d->m_meAbstract->setText( aboutInfo->abstract() );
00293   grid->addMultiCellWidget(d->m_meAbstract, 1, 2, 1, 1);
00294 
00295   connect( d->m_leDocTitle, SIGNAL( textChanged( const QString & ) ),
00296            this, SIGNAL( changed() ) );
00297   connect( d->m_meAbstract, SIGNAL( textChanged() ),
00298            this, SIGNAL( changed() ) );
00299 }
00300 
00301 void KoDocumentInfoDlg::save()
00302 {
00303   QStringList pages = d->m_info->pages();
00304   QStringList::ConstIterator it = pages.begin();
00305   QStringList::ConstIterator end = pages.end();
00306   bool saveInfo=false;
00307   for (; it != end; ++it )
00308   {
00309     KoDocumentInfoPage *pg = d->m_info->page( *it );
00310     if ( pg->inherits( "KoDocumentInfoAuthor" ) )
00311     {
00312         saveInfo=true;
00313         save( static_cast<KoDocumentInfoAuthor *>( pg ) );
00314     }
00315     else if ( pg->inherits( "KoDocumentInfoAbout" ) )
00316     {
00317         saveInfo=true;
00318         save( static_cast<KoDocumentInfoAbout *>( pg ) );
00319     }
00320   }
00321   if(saveInfo)
00322       d->m_info->documentInfochanged();
00323 }
00324 
00325 void KoDocumentInfoDlg::save( KoDocumentInfoAuthor *authorInfo )
00326 {
00327   authorInfo->setFullName( d->m_leFullName->text() );
00328   authorInfo->setInitial( d->m_leInitial->text() );
00329   authorInfo->setTitle( d->m_leAuthorTitle->text() );
00330   authorInfo->setCompany( d->m_leCompany->text() );
00331   authorInfo->setEmail( d->m_leEmail->text() );
00332   authorInfo->setTelephone( d->m_leTelephone->text() );
00333   authorInfo->setFax( d->m_leFax->text() );
00334   authorInfo->setCountry( d->m_leCountry->text() );
00335   authorInfo->setPostalCode( d->m_lePostalCode->text() );
00336   authorInfo->setCity( d->m_leCity->text() );
00337   authorInfo->setStreet( d->m_leStreet->text() );
00338 
00339   KConfig* config = KoGlobal::kofficeConfig();
00340   KConfigGroupSaver cgs( config, "Author" );
00341   config->writeEntry("telephone", d->m_leTelephone->text());
00342   config->writeEntry("fax", d->m_leFax->text());
00343   config->writeEntry("country",d->m_leCountry->text());
00344   config->writeEntry("postal-code",d->m_lePostalCode->text());
00345   config->writeEntry("city",  d->m_leCity->text());
00346   config->writeEntry("street", d->m_leStreet->text());
00347   config->sync();
00348 }
00349 
00350 void KoDocumentInfoDlg::save( KoDocumentInfoAbout *aboutInfo )
00351 {
00352   aboutInfo->setTitle( d->m_leDocTitle->text() );
00353   aboutInfo->setAbstract( d->m_meAbstract->text() );
00354 }
00355 
00356 class KoDocumentInfoPropsPage::KoDocumentInfoPropsPagePrivate
00357 {
00358 public:
00359   KoDocumentInfo *m_info;
00360   KoDocumentInfoDlg *m_dlg;
00361   KURL m_url;
00362   KTarGz *m_src;
00363   KTarGz *m_dst;
00364 
00365   const KTarFile *m_docInfoFile;
00366 };
00367 
00368 KoDocumentInfoPropsPage::KoDocumentInfoPropsPage( KPropertiesDialog *props,
00369                                                   const char *,
00370                                                   const QStringList & )
00371 : KPropsDlgPlugin( props )
00372 {
00373   d = new KoDocumentInfoPropsPagePrivate;
00374   d->m_info = new KoDocumentInfo( this, "docinfo" );
00375   d->m_url = props->item()->url();
00376   d->m_dlg = 0;
00377 
00378   if ( !d->m_url.isLocalFile() )
00379     return;
00380 
00381   d->m_dst = 0;
00382 
00383 #ifdef __GNUC__
00384 #warning TODO port this to KoStore !!!
00385 #endif
00386   d->m_src = new KTarGz( d->m_url.path(), "application/x-gzip" );
00387 
00388   if ( !d->m_src->open( IO_ReadOnly ) )
00389     return;
00390 
00391   const KTarDirectory *root = d->m_src->directory();
00392   if ( !root )
00393     return;
00394 
00395   const KTarEntry *entry = root->entry( "documentinfo.xml" );
00396 
00397   if ( entry && entry->isFile() )
00398   {
00399     d->m_docInfoFile = static_cast<const KTarFile *>( entry );
00400 
00401     QBuffer buffer( d->m_docInfoFile->data() );
00402     buffer.open( IO_ReadOnly );
00403 
00404     QDomDocument doc;
00405     doc.setContent( &buffer );
00406 
00407     d->m_info->load( doc );
00408   }
00409 
00410   d->m_dlg = new KoDocumentInfoDlg( d->m_info, 0, 0, props );
00411   connect( d->m_dlg, SIGNAL( changed() ),
00412            this, SIGNAL( changed() ) );
00413 }
00414 
00415 KoDocumentInfoPropsPage::~KoDocumentInfoPropsPage()
00416 {
00417   delete d->m_info;
00418   delete d->m_src;
00419   delete d->m_dst;
00420   delete d->m_dlg;
00421   delete d;
00422 }
00423 
00424 void KoDocumentInfoPropsPage::applyChanges()
00425 {
00426   const KTarDirectory *root = d->m_src->directory();
00427   if ( !root )
00428     return;
00429 
00430   struct stat statBuff;
00431 
00432   if ( stat( QFile::encodeName( d->m_url.path() ), &statBuff ) != 0 )
00433     return;
00434 
00435   KTempFile tempFile( d->m_url.path(), QString::null, statBuff.st_mode );
00436 
00437   tempFile.setAutoDelete( true );
00438 
00439   if ( tempFile.status() != 0 )
00440     return;
00441 
00442   if ( !tempFile.close() )
00443     return;
00444 
00445   d->m_dst = new KTarGz( tempFile.name(), "application/x-gzip" );
00446 
00447   if ( !d->m_dst->open( IO_WriteOnly ) )
00448     return;
00449 
00450   KMimeType::Ptr mimeType = KMimeType::findByURL( d->m_url, 0, true );
00451   if ( mimeType && dynamic_cast<KFilterDev *>( d->m_dst->device() ) != 0 )
00452   {
00453       QCString appIdentification( "KOffice " ); // We are limited in the number of chars.
00454       appIdentification += mimeType->name().latin1();
00455       appIdentification += '\004'; // Two magic bytes to make the identification
00456       appIdentification += '\006'; // more reliable (DF)
00457       d->m_dst->setOrigFileName( appIdentification );
00458   }
00459 
00460   bool docInfoSaved = false;
00461 
00462   QStringList entries = root->entries();
00463   QStringList::ConstIterator it = entries.begin();
00464   QStringList::ConstIterator end = entries.end();
00465   for (; it != end; ++it )
00466   {
00467     const KTarEntry *entry = root->entry( *it );
00468 
00469     assert( entry );
00470 
00471     if ( entry->name() == "documentinfo.xml" ||
00472          ( !docInfoSaved && !entries.contains( "documentinfo.xml" ) ) )
00473     {
00474       d->m_dlg->save();
00475 
00476       QBuffer buffer;
00477       buffer.open( IO_WriteOnly );
00478       QTextStream str( &buffer );
00479       str << d->m_info->save();
00480       buffer.close();
00481 
00482       kdDebug( 30003 ) << "writing documentinfo.xml" << endl;
00483       d->m_dst->writeFile( "documentinfo.xml", entry->user(), entry->group(), buffer.buffer().size(),
00484                            buffer.buffer().data() );
00485 
00486       docInfoSaved = true;
00487     }
00488     else
00489       copy( QString::null, entry );
00490   }
00491 
00492   d->m_dst->close();
00493 
00494   QDir dir;
00495   dir.rename( tempFile.name(), d->m_url.path() );
00496 
00497   delete d->m_dst;
00498   d->m_dst = 0;
00499 }
00500 
00501 void KoDocumentInfoPropsPage::copy( const QString &path, const KTarEntry *entry )
00502 {
00503   kdDebug( 30003 ) << "copy " << entry->name() << endl;
00504   if ( entry->isFile() )
00505   {
00506     const KTarFile *file = static_cast<const KTarFile *>( entry );
00507     kdDebug( 30003 ) << "file :" << entry->name() << endl;
00508     kdDebug( 30003 ) << "full path is: " << path << entry->name() << endl;
00509     d->m_dst->writeFile( path + entry->name(), entry->user(), entry->group(),
00510                          file->size(),
00511                          file->data().data() );
00512   }
00513   else
00514   {
00515     const KTarDirectory *dir = static_cast<const KTarDirectory *>( entry );
00516     kdDebug( 30003 ) << "dir : " << entry->name() << endl;
00517     kdDebug( 30003 ) << "full path is: " << path << entry->name() << endl;
00518 
00519     QString p = path + entry->name();
00520     if ( p != "/" )
00521     {
00522       d->m_dst->writeDir( p, entry->user(), entry->group() );
00523       p.append( "/" );
00524     }
00525 
00526     QStringList entries = dir->entries();
00527     QStringList::ConstIterator it = entries.begin();
00528     QStringList::ConstIterator end = entries.end();
00529     for (; it != end; ++it )
00530       copy( p, dir->entry( *it ) );
00531   }
00532 }
00533 
00534 /* vim: sw=2 et
00535  */
00536 
00537 #include <koDocumentInfoDlg.moc>
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Mar 11 11:47:41 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003