lib

KoInsertLink.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C)  2001 Montel Laurent <lmontel@mandrakesoft.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kapplication.h>
00021 #include <klocale.h>
00022 
00023 #include <qlayout.h>
00024 #include <qvbox.h>
00025 #include <kdebug.h>
00026 #include <qlabel.h>
00027 #include <qcombobox.h>
00028 
00029 #include <klineedit.h>
00030 #include <kurlrequester.h>
00031 #include <kseparator.h>
00032 #include <kiconloader.h>
00033 #include "KoInsertLink.h"
00034 #include <kdesktopfile.h>
00035 #include <krecentdocument.h>
00036 
00037 using namespace KOfficePrivate;
00038 
00039 KoInsertLinkDia::KoInsertLinkDia( QWidget *parent, const char *name, bool displayBookmarkLink )
00040     : KDialogBase( KDialogBase::IconList, i18n("Insert Link"),
00041            KDialogBase::Ok | KDialogBase::Cancel,
00042            KDialogBase::Ok, parent, name )
00043 {
00044   bookmarkLink = 0L;
00045   QVBox *page=addVBoxPage(i18n("Internet"), QString::null,BarIcon("html",KIcon::SizeMedium));
00046   internetLink = new  internetLinkPage(page );
00047   connect(internetLink,SIGNAL(textChanged()),this,SLOT(slotTextChanged (  )));
00048 
00049   page=addVBoxPage(i18n("Mail & News"), QString::null,BarIcon("mail_generic",KIcon::SizeMedium));
00050   mailLink = new  mailLinkPage(page );
00051   connect(mailLink,SIGNAL(textChanged()),this,SLOT(slotTextChanged ()));
00052 
00053   page=addVBoxPage(i18n("File"), QString::null,BarIcon("filenew",KIcon::SizeMedium));
00054   fileLink = new  fileLinkPage(page );
00055   connect(fileLink,SIGNAL(textChanged()),this,SLOT(slotTextChanged ()));
00056 
00057   if ( displayBookmarkLink)
00058   {
00059       page=addVBoxPage(i18n("Bookmark"), QString::null,BarIcon("bookmark",KIcon::SizeMedium));
00060       bookmarkLink = new  bookmarkLinkPage(page );
00061       connect(bookmarkLink,SIGNAL(textChanged()),this,SLOT(slotTextChanged ()));
00062   }
00063 
00064   connect( this, SIGNAL( aboutToShowPage(QWidget *) ), this, SLOT( tabChanged(QWidget *) ) );
00065 
00066   slotTextChanged ( );
00067   resize(400,300);
00068 }
00069 
00070 void KoInsertLinkDia::tabChanged(QWidget *)
00071 {
00072     switch( activePageIndex() )
00073     {
00074     case 0:
00075       internetLink->setLinkName( currentText );
00076       break;
00077     case 1:
00078       mailLink->setLinkName( currentText );
00079       break;
00080     case 2:
00081       fileLink->setLinkName( currentText );
00082       break;
00083     case 3:
00084     {
00085         if ( bookmarkLink)
00086             bookmarkLink->setLinkName( currentText );
00087     }
00088     break;
00089     default:
00090       kdDebug()<<"Error in linkName\n";
00091     }
00092     enableButtonOK( !(linkName().isEmpty()  || hrefName().isEmpty()) );
00093 }
00094 
00095 void KoInsertLinkDia::slotTextChanged ( )
00096 {
00097     enableButtonOK( !(linkName().isEmpty()  || hrefName().isEmpty()));
00098     currentText = linkName();
00099 }
00100 
00101 bool KoInsertLinkDia::createLinkDia(QString & _linkName, QString & _hrefName, const QStringList& bkmlist, bool displayBookmarkLink, QWidget* parent, const char* name)
00102 {
00103     bool res = false;
00104 
00105     KoInsertLinkDia *dlg = new KoInsertLinkDia( parent, name, displayBookmarkLink );
00106     dlg->setHrefLinkName(_hrefName,_linkName, bkmlist);
00107     if ( dlg->exec() == Accepted )
00108     {
00109         _linkName = dlg->linkName();
00110         _hrefName = dlg->hrefName();
00111         res = true;
00112     }
00113     delete dlg;
00114 
00115     return res;
00116 }
00117 
00118 void KoInsertLinkDia::setHrefLinkName(const QString &_href, const QString &_link, const QStringList & bkmlist)
00119 {
00120     if ( bookmarkLink)
00121         bookmarkLink->setBookmarkList(bkmlist);
00122     if ( _href.isEmpty())
00123     {
00124         if ( !_link.isEmpty() )
00125         {
00126             internetLink->setLinkName(_link);
00127             showPage(0);
00128             slotTextChanged ( );
00129         }
00130         return;
00131     }
00132     if(_href.find("http://")!=-1 || _href.find("https://")!=-1 ||_href.find("ftp://")!=-1 )
00133     {
00134         internetLink->setHrefName(_href);
00135         internetLink->setLinkName(_link);
00136         showPage(0);
00137     }
00138     else if(_href.find("file:/")!=-1)
00139     {
00140         fileLink->setHrefName(_href);
00141         fileLink->setLinkName(_link);
00142         showPage(2);
00143     }
00144     else if(_href.find("mailto:")!=-1 || _href.find("news:")!=-1)
00145     {
00146         mailLink->setHrefName(_href);
00147         mailLink->setLinkName(_link);
00148         showPage(1);
00149     }
00150     else if(_href.find("bkm://")!=-1)
00151     {
00152         if ( bookmarkLink )
00153         {
00154             bookmarkLink->setHrefName(_href.mid(6));
00155             bookmarkLink->setLinkName(_link);
00156             showPage(3);
00157         }
00158     }
00159     slotTextChanged ( );
00160 }
00161 
00162 QString KoInsertLinkDia::linkName() const
00163 {
00164     QString result;
00165     switch(activePageIndex())
00166     {
00167     case 0:
00168       result=internetLink->linkName();
00169       break;
00170     case 1:
00171       result=mailLink->linkName();
00172       break;
00173     case 2:
00174       result=fileLink->linkName();
00175       break;
00176     case 3:
00177     {
00178         if ( bookmarkLink)
00179             result=bookmarkLink->linkName();
00180     }
00181     break;
00182     default:
00183       kdDebug()<<"Error in linkName\n";
00184     }
00185   return result;
00186 }
00187 
00188 QString KoInsertLinkDia::hrefName() const
00189 {
00190     QString result;
00191     switch(activePageIndex())
00192     {
00193     case 0:
00194       result=internetLink->hrefName();
00195       break;
00196     case 1:
00197       result=mailLink->hrefName();
00198       break;
00199     case 2:
00200       result=fileLink->hrefName();
00201       break;
00202     case 3:
00203     {
00204         if ( bookmarkLink )
00205             result=bookmarkLink->hrefName();
00206     }
00207     break;
00208     default:
00209       kdDebug()<<"Error in hrefName\n";
00210     }
00211   return result;
00212 }
00213 
00214 void KoInsertLinkDia::slotOk()
00215 {
00216     KDialogBase::slotOk();
00217 }
00218 
00219 
00220 internetLinkPage::internetLinkPage( QWidget *parent , char *name  )
00221   : QWidget(parent,name)
00222 {
00223   QVBoxLayout *lay1 = new QVBoxLayout( this );
00224   lay1->setSpacing( KDialog::spacingHint() );
00225   QVBoxLayout *lay2 = new QVBoxLayout( lay1);
00226   lay2->setSpacing( KDialog::spacingHint() );
00227 
00228   QLabel* tmpQLabel = new QLabel( this);
00229 
00230   lay2->addWidget(tmpQLabel);
00231   tmpQLabel->setText(i18n("Text to display:"));
00232 
00233   m_linkName = new QLineEdit( this );
00234   lay2->addWidget(m_linkName);
00235 
00236   tmpQLabel = new QLabel( this);
00237   lay2->addWidget(tmpQLabel);
00238 
00239   tmpQLabel->setText(i18n("Internet address:"));
00240   m_hrefName = new QLineEdit( this );
00241 
00242   lay2->addWidget(m_hrefName);
00243 
00244   lay2->addStretch( 1 );
00245   
00246   m_linkName->setFocus();
00247 
00248   connect(m_linkName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00249   connect(m_hrefName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00250   KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
00251   bar1->setFixedHeight( 10 );
00252   lay2->addWidget( bar1 );
00253 }
00254 
00255 QString internetLinkPage::createInternetLink()
00256 {
00257     QString result=m_hrefName->text();
00258 
00259     if(result.isEmpty())
00260         return result;
00261 
00262     if(result.find("http://")==-1 && result.find("https://")==-1 && result.find("ftp://")==-1)
00263         result = "http://"+result;
00264     return result;
00265 }
00266 
00267 
00268 void internetLinkPage::setLinkName(const QString & _name)
00269 {
00270     m_linkName->setText(_name);
00271 }
00272 
00273 void internetLinkPage::setHrefName(const QString &_name)
00274 {
00275     m_hrefName->setText(_name);
00276 }
00277 
00278 QString internetLinkPage::linkName()const
00279 {
00280   return m_linkName->text();
00281 }
00282 
00283 QString internetLinkPage::hrefName()
00284 {
00285   return createInternetLink();
00286 }
00287 
00288 void internetLinkPage::textChanged ( const QString & )
00289 {
00290     emit textChanged();
00291 }
00292 
00293 bookmarkLinkPage::bookmarkLinkPage( QWidget *parent , char *name  )
00294   : QWidget(parent,name)
00295 {
00296   QVBoxLayout *lay1 = new QVBoxLayout( this );
00297   lay1->setSpacing( KDialog::spacingHint() );
00298   QVBoxLayout *lay2 = new QVBoxLayout( lay1);
00299   lay2->setSpacing( KDialog::spacingHint() );
00300 
00301   QLabel* tmpQLabel = new QLabel( this);
00302 
00303   lay2->addWidget(tmpQLabel);
00304   tmpQLabel->setText(i18n("Text to display:"));
00305 
00306   m_linkName = new QLineEdit( this );
00307   lay2->addWidget(m_linkName);
00308 
00309   tmpQLabel = new QLabel( this);
00310   lay2->addWidget(tmpQLabel);
00311 
00312   tmpQLabel->setText(i18n("Bookmark name:"));
00313   m_hrefName = new QComboBox( this );
00314 
00315   lay2->addWidget(m_hrefName);
00316 
00317   lay2->addStretch( 1 );
00318   
00319   m_linkName->setFocus();
00320 
00321   connect(m_linkName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00322   connect(m_hrefName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00323   KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
00324   bar1->setFixedHeight( 10 );
00325   lay2->addWidget( bar1 );
00326 }
00327 
00328 QString bookmarkLinkPage::createBookmarkLink()
00329 {
00330     QString result=m_hrefName->currentText();
00331 
00332     if(result.isEmpty())
00333         return result;
00334 
00335     if(result.find("bkm://")==-1)
00336         result = "bkm://"+result;
00337     return result;
00338 }
00339 
00340 
00341 void bookmarkLinkPage::setLinkName(const QString & _name)
00342 {
00343     m_linkName->setText(_name);
00344 }
00345 
00346 void bookmarkLinkPage::setHrefName(const QString &_name)
00347 {
00348     m_hrefName->setCurrentText(_name);
00349 }
00350 
00351 void bookmarkLinkPage::setBookmarkList(const QStringList & bkmlist)
00352 {
00353     m_hrefName->clear();
00354     m_hrefName->insertStringList(bkmlist, 0);
00355     if ( bkmlist.isEmpty())
00356         m_linkName->setEnabled( false);
00357     //m_hrefName->setEditable(true);
00358 }
00359 
00360 QString bookmarkLinkPage::linkName()const
00361 {
00362   return m_linkName->text();
00363 }
00364 
00365 QString bookmarkLinkPage::hrefName()
00366 {
00367   return createBookmarkLink();
00368 }
00369 
00370 void bookmarkLinkPage::textChanged ( const QString & )
00371 {
00372     emit textChanged();
00373 }
00374 
00375 mailLinkPage::mailLinkPage( QWidget *parent , char *name  )
00376   : QWidget(parent,name)
00377 {
00378   QVBoxLayout *lay1 = new QVBoxLayout( this );
00379   lay1->setSpacing( KDialog::spacingHint() );
00380   QVBoxLayout *lay2 = new QVBoxLayout( lay1);
00381   lay2->setSpacing( KDialog::spacingHint() );
00382 
00383   QLabel* tmpQLabel = new QLabel( this);
00384 
00385   lay2->addWidget(tmpQLabel);
00386   tmpQLabel->setText(i18n("Text to display:"));
00387 
00388   m_linkName = new QLineEdit( this );
00389   lay2->addWidget(m_linkName);
00390 
00391   tmpQLabel = new QLabel( this);
00392   lay2->addWidget(tmpQLabel);
00393 
00394   tmpQLabel->setText(i18n("Target:"));
00395   m_hrefName = new QLineEdit( this );
00396 
00397   lay2->addWidget(m_hrefName);
00398   lay2->addStretch( 1 );
00399   
00400   connect(m_linkName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00401   connect(m_hrefName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00402   KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
00403   bar1->setFixedHeight( 10 );
00404   lay2->addWidget( bar1 );
00405 }
00406 
00407 QString mailLinkPage::createMailLink()
00408 {
00409     QString result=m_hrefName->text();
00410 
00411     if(result.isEmpty())
00412         return result;
00413 
00414     if(result.find("mailto:")==-1 && result.find("news:")==-1)
00415         result = "mailto:"+result;
00416     return result;
00417 }
00418 
00419 
00420 void mailLinkPage::setLinkName(const QString & _name)
00421 {
00422     m_linkName->setText(_name);
00423 }
00424 
00425 void mailLinkPage::setHrefName(const QString &_name)
00426 {
00427     m_hrefName->setText(_name);
00428 }
00429 
00430 QString mailLinkPage::linkName()const
00431 {
00432   return m_linkName->text();
00433 }
00434 
00435 QString mailLinkPage::hrefName()
00436 {
00437   return createMailLink();
00438 }
00439 
00440 void mailLinkPage::textChanged ( const QString & )
00441 {
00442     emit textChanged();
00443 }
00444 
00445 fileLinkPage::fileLinkPage( QWidget *parent , char *name  )
00446   : QWidget(parent,name)
00447 {
00448   QVBoxLayout *lay1 = new QVBoxLayout( this );
00449   lay1->setSpacing( KDialog::spacingHint() );
00450   QVBoxLayout *lay2 = new QVBoxLayout( lay1);
00451   lay2->setSpacing( KDialog::spacingHint() );
00452 
00453   QLabel* tmpQLabel = new QLabel( this);
00454 
00455   lay2->addWidget(tmpQLabel);
00456   tmpQLabel->setText(i18n("Text to display:"));
00457 
00458   m_linkName = new QLineEdit( this );
00459   lay2->addWidget(m_linkName);
00460 
00461   tmpQLabel = new QLabel( this);
00462   lay2->addWidget(tmpQLabel);
00463   tmpQLabel->setText(i18n("Recent file:"));
00464 
00465   QComboBox * recentFile = new QComboBox( this );
00466   recentFile->setMaximumWidth( kapp->desktop()->width()*3/4 );
00467   lay2->addWidget(recentFile);
00468 
00469   QStringList fileList = KRecentDocument::recentDocuments();
00470   QStringList lst;
00471   lst <<"";
00472   for (QStringList::ConstIterator it = fileList.begin();it != fileList.end(); ++it)
00473   {
00474       KDesktopFile f(*it, true /* read only */);
00475       if ( !f.readURL().isEmpty())
00476           lst.append( f.readURL());
00477   }
00478   if ( lst.count()<= 1 )
00479   {
00480       recentFile->clear();
00481       recentFile->insertItem( i18n("No Entries") );
00482       recentFile->setEnabled( false );
00483   }
00484   else
00485       recentFile->insertStringList( lst);
00486   
00487   recentFile->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00488   
00489   connect( recentFile , SIGNAL(highlighted ( const QString &)), this,  SLOT( slotSelectRecentFile( const QString & )));
00490 
00491   tmpQLabel = new QLabel( this);
00492   lay2->addWidget(tmpQLabel);
00493 
00494   tmpQLabel->setText(i18n("File location:"));
00495   m_hrefName = new KURLRequester( this );
00496 
00497   lay2->addWidget(m_hrefName);
00498   lay2->addStretch( 1 );
00499 
00500   connect(m_linkName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00501   connect(m_hrefName,SIGNAL(textChanged ( const QString & )),this,SLOT(textChanged ( const QString & )));
00502 
00503   KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
00504   bar1->setFixedHeight( 10 );
00505   lay2->addWidget( bar1 );
00506 }
00507 
00508 void fileLinkPage::slotSelectRecentFile( const QString &_file )
00509 {
00510     m_hrefName->lineEdit()->setText(_file );
00511 }
00512 
00513 QString fileLinkPage::createFileLink()
00514 {
00515     QString result=m_hrefName->lineEdit()->text();
00516     if(result.isEmpty())
00517         return result;
00518 
00519     if(result.find("file:/")==-1)
00520         result = "file://"+result;
00521     return result;
00522 }
00523 
00524 void fileLinkPage::setLinkName(const QString & _name)
00525 {
00526     m_linkName->setText(_name);
00527 }
00528 
00529 void fileLinkPage::setHrefName(const QString &_name)
00530 {
00531     m_hrefName->lineEdit()->setText(_name);
00532 }
00533 
00534 QString fileLinkPage::linkName()const
00535 {
00536   return m_linkName->text();
00537 }
00538 
00539 QString fileLinkPage::hrefName()
00540 {
00541   return createFileLink();
00542 }
00543 
00544 void fileLinkPage::textChanged ( const QString & )
00545 {
00546     emit textChanged();
00547 }
00548 
00549 #include "KoInsertLink.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys