kio Library API Documentation

kfiledialog.cpp

00001 // -*- c++ -*-
00002 /* This file is part of the KDE libraries
00003     Copyright (C) 1997, 1998 Richard Moore <rich@kde.org>
00004                   1998 Stephan Kulow <coolo@kde.org>
00005                   1998 Daniel Grana <grana@ie.iwi.unibe.ch>
00006                   1999,2000,2001,2002 Carsten Pfeiffer <pfeiffer@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021     Boston, MA 02111-1307, USA.
00022 */
00023 
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 
00028 #include <qptrcollection.h>
00029 #include <qcombobox.h>
00030 #include <qlabel.h>
00031 #include <qlayout.h>
00032 #include <qlineedit.h>
00033 #include <qptrlist.h>
00034 #include <qpixmap.h>
00035 #include <qtooltip.h>
00036 #include <qtimer.h>
00037 #include <qwhatsthis.h>
00038 
00039 #include <kaccel.h>
00040 #include <kaction.h>
00041 #include <kapplication.h>
00042 #include <kcmdlineargs.h>
00043 #include <kcompletionbox.h>
00044 #include <kconfig.h>
00045 #include <kdebug.h>
00046 #include <kglobal.h>
00047 #include <kglobalsettings.h>
00048 #include <kiconloader.h>
00049 #include <kimageio.h>
00050 #include <kio/job.h>
00051 #include <kio/previewjob.h>
00052 #include <kio/scheduler.h>
00053 #include <klocale.h>
00054 #include <kmessagebox.h>
00055 #include <kmimetype.h>
00056 #include <kpopupmenu.h>
00057 #include <kprotocolinfo.h>
00058 #include <kpushbutton.h>
00059 #include <krecentdirs.h>
00060 #include <kstandarddirs.h>
00061 #include <kstdguiitem.h>
00062 #include <kstaticdeleter.h>
00063 #include <ktoolbar.h>
00064 #include <ktoolbarbutton.h>
00065 #include <kurl.h>
00066 #include <kurlcombobox.h>
00067 #include <kurlcompletion.h>
00068 
00069 #include "config-kfile.h"
00070 #include "kpreviewwidgetbase.h"
00071 
00072 #include <kfileview.h>
00073 #include <krecentdocument.h>
00074 #include <kfiledialog.h>
00075 #include <kfilefiltercombo.h>
00076 #include <kdiroperator.h>
00077 #include <kimagefilepreview.h>
00078 
00079 #include <kfilespeedbar.h>
00080 #include <kfilebookmarkhandler.h>
00081 
00082 enum Buttons { HOTLIST_BUTTON,
00083                PATH_COMBO, CONFIGURE_BUTTON };
00084 
00085 template class QPtrList<KIO::StatJob>;
00086 
00087 namespace {
00088 static void silenceQToolBar(QtMsgType, const char *)
00089 {
00090 }
00091 }
00092 
00093 struct KFileDialogPrivate
00094 {
00095     // the last selected url
00096     KURL url;
00097 
00098     // the selected filenames in multiselection mode -- FIXME
00099     QString filenames;
00100 
00101     // the name of the filename set by setSelection
00102     QString selection;
00103 
00104     // we need this to determine what has changed in the location bar
00105     QString completionHack;
00106 
00107     // now following all kind of widgets, that I need to rebuild
00108     // the geometry managment
00109     QBoxLayout *boxLayout;
00110     QWidget *mainWidget;
00111 
00112     QLabel *locationLabel;
00113 
00114     // @deprecated remove in KDE4
00115     QLabel *filterLabel;
00116     KURLComboBox *pathCombo;
00117     KPushButton *okButton, *cancelButton;
00118     KFileSpeedBar *urlBar;
00119     QHBoxLayout *urlBarLayout;
00120     QWidget *customWidget;
00121 
00122     QPtrList<KIO::StatJob> statJobs;
00123 
00124     KURL::List urlList; //the list of selected urls
00125 
00126     QStringList mimetypes; //the list of possible mimetypes to save as
00127 
00128     // indicates if the location edit should be kept or cleared when changing
00129     // directories
00130     bool keepLocation :1;
00131 
00132     // the KDirOperators view is set in KFileDialog::show(), so to avoid
00133     // setting it again and again, we have this nice little boolean :)
00134     bool hasView :1;
00135 
00136     // do we show the speedbar for the first time?
00137     bool initializeSpeedbar :1;
00138 
00139     // an indicator that we're currently in a completion operation
00140     // we need to lock some slots for this
00141     bool completionLock :1;
00142 
00143     bool hasDefaultFilter :1; // necessary for the operationMode
00144     KFileDialog::OperationMode operationMode;
00145 
00146     // The file class used for KRecentDirs
00147     QString fileClass;
00148 
00149     KFileBookmarkHandler *bookmarkHandler;
00150 
00151     // the ID of the path drop down so subclasses can place their custom widgets properly
00152     int m_pathComboIndex;
00153 };
00154 
00155 KURL *KFileDialog::lastDirectory; // to set the start path
00156 
00157 static KStaticDeleter<KURL> ldd;
00158 
00159 KFileDialog::KFileDialog(const QString& startDir, const QString& filter,
00160                          QWidget *parent, const char* name, bool modal)
00161     : KDialogBase( parent, name, modal, QString::null, 0 )
00162 {
00163     init( startDir, filter, 0 );
00164 }
00165 
00166 KFileDialog::KFileDialog(const QString& startDir, const QString& filter,
00167                          QWidget *parent, const char* name, bool modal, QWidget* widget)
00168     : KDialogBase( parent, name, modal, QString::null, 0 )
00169 {
00170     init( startDir, filter, widget );
00171 }
00172 
00173 KFileDialog::~KFileDialog()
00174 {
00175     hide();
00176 
00177     KConfig *config = KGlobal::config();
00178 
00179     if (d->urlBar)
00180         d->urlBar->save( config );
00181 
00182     config->sync();
00183 
00184     delete ops;
00185     delete d;
00186 }
00187 
00188 void KFileDialog::setLocationLabel(const QString& text)
00189 {
00190     d->locationLabel->setText(text);
00191 }
00192 
00193 void KFileDialog::setFilter(const QString& filter)
00194 {
00195     int pos = filter.find('/');
00196 
00197     // Check for an un-escaped '/', if found
00198     // interpret as a MIME filter.
00199 
00200     if (pos > 0 && filter[pos - 1] != '\\') {
00201         QStringList filters = QStringList::split( " ", filter );
00202         setMimeFilter( filters );
00203         return;
00204     }
00205 
00206     // Strip the escape characters from
00207     // escaped '/' characters.
00208 
00209     QString copy (filter);
00210     for (pos = 0; (pos = copy.find("\\/", pos)) != -1; ++pos)
00211         copy.remove(pos, 1);
00212 
00213     ops->clearFilter();
00214     filterWidget->setFilter(copy);
00215     ops->setNameFilter(filterWidget->currentFilter());
00216     d->hasDefaultFilter = false;
00217     filterWidget->setEditable( true );
00218 }
00219 
00220 QString KFileDialog::currentFilter() const
00221 {
00222     return filterWidget->currentFilter();
00223 }
00224 
00225 // deprecated
00226 void KFileDialog::setFilterMimeType(const QString &label,
00227                                     const KMimeType::List &types,
00228                                     const KMimeType::Ptr &defaultType)
00229 {
00230     d->mimetypes.clear();
00231     d->filterLabel->setText(label);
00232 
00233     KMimeType::List::ConstIterator it;
00234     for( it = types.begin(); it != types.end(); ++it)
00235         d->mimetypes.append( (*it)->name() );
00236 
00237     setMimeFilter( d->mimetypes, defaultType->name() );
00238 }
00239 
00240 void KFileDialog::setMimeFilter( const QStringList& mimeTypes,
00241                                  const QString& defaultType )
00242 {
00243     d->mimetypes = mimeTypes;
00244     filterWidget->setMimeFilter( mimeTypes, defaultType );
00245 
00246     QStringList types = QStringList::split(" ", filterWidget->currentFilter());
00247     types.append( QString::fromLatin1( "inode/directory" ));
00248     ops->clearFilter();
00249     ops->setMimeFilter( types );
00250     d->hasDefaultFilter = !defaultType.isEmpty();
00251     filterWidget->setEditable( !d->hasDefaultFilter ||
00252                                d->operationMode != Saving );
00253 }
00254 
00255 void KFileDialog::clearFilter()
00256 {
00257     d->mimetypes.clear();
00258     filterWidget->setFilter( QString::null );
00259     ops->clearFilter();
00260     d->hasDefaultFilter = false;
00261     filterWidget->setEditable( true );
00262 }
00263 
00264 QString KFileDialog::currentMimeFilter() const
00265 {
00266     int i = filterWidget->currentItem();
00267     if (filterWidget->showsAllTypes())
00268         i--;
00269 
00270     if ((i >= 0) && (i < (int) d->mimetypes.count()))
00271         return d->mimetypes[i];
00272     return QString::null; // The "all types" item has no mimetype
00273 }
00274 
00275 KMimeType::Ptr KFileDialog::currentFilterMimeType()
00276 {
00277     return KMimeType::mimeType( currentMimeFilter() );
00278 }
00279 
00280 void KFileDialog::setPreviewWidget(const QWidget *w) {
00281     ops->setPreviewWidget(w);
00282     ops->clearHistory();
00283     d->hasView = true;
00284 }
00285 
00286 void KFileDialog::setPreviewWidget(const KPreviewWidgetBase *w) {
00287     ops->setPreviewWidget(w);
00288     ops->clearHistory();
00289     d->hasView = true;
00290 }
00291 
00292 // FIXME: check for "existing" flag here?
00293 void KFileDialog::slotOk()
00294 {
00295     kdDebug(kfile_area) << "slotOK\n";
00296 
00297     // a list of all selected files/directories (if any)
00298     // can only be used if the user didn't type any filenames/urls himself
00299     const KFileItemList *items = ops->selectedItems();
00300 
00301     if ( (mode() & KFile::Directory) != KFile::Directory ) {
00302         if ( locationEdit->currentText().stripWhiteSpace().isEmpty() ) {
00303         if ( !items || items->isEmpty() )
00304             return;
00305 
00306         // weird case: the location edit is empty, but there are
00307         // highlighted files
00308         else {
00309 
00310         bool multi = (mode() & KFile::Files) != 0;
00311             KFileItemListIterator it( *items );
00312         QString endQuote = QString::fromLatin1("\" ");
00313         QString name, files;
00314         while ( it.current() ) {
00315             name = (*it)->name();
00316             if ( multi ) {
00317                 name.prepend( '"' );
00318             name.append( endQuote );
00319             }
00320 
00321             files.append( name );
00322             ++it;
00323         }
00324         locationEdit->setEditText( files );
00325         locationEdit->lineEdit()->setEdited( false );
00326         return;
00327         }
00328     }
00329     }
00330 
00331     bool dirOnly = ops->dirOnlyMode();
00332 
00333     // we can use our kfileitems, no need to parse anything
00334     if ( items && !locationEdit->lineEdit()->edited() &&
00335      !(items->isEmpty() && !dirOnly) ) {
00336 
00337     d->urlList.clear();
00338     d->filenames = QString::null;
00339 
00340     if ( dirOnly ) {
00341         d->url = ops->url();
00342     }
00343     else {
00344         if ( !(mode() & KFile::Files) ) {// single selection
00345         d->url = items->getFirst()->url();
00346         }
00347 
00348         else { // multi (dirs and/or files)
00349         d->url = ops->url();
00350         KFileItemListIterator it( *items );
00351         while ( it.current() ) {
00352             d->urlList.append( (*it)->url() );
00353             ++it;
00354         }
00355         }
00356     }
00357 
00358         if ( (mode() & KFile::LocalOnly) == KFile::LocalOnly &&
00359              !d->url.isLocalFile() ) {
00360 // ### after message freeze, add message for directories!
00361             KMessageBox::sorry( d->mainWidget,
00362                                 i18n("You can only select local files."),
00363                                 i18n("Remote Files not Accepted") );
00364             return;
00365         }
00366 
00367     accept();
00368     return;
00369     }
00370 
00371 
00372     KURL selectedURL;
00373 
00374     if ( (mode() & KFile::Files) == KFile::Files ) {// multiselection mode
00375     if ( locationEdit->currentText().contains( '/' )) {
00376 
00377         // relative path? -> prepend the current directory
00378         KURL u( ops->url(), locationEdit->currentText() );
00379         if ( !u.isMalformed() )
00380         selectedURL = u;
00381         else
00382         selectedURL = ops->url();
00383     }
00384     else // simple filename -> just use the current URL
00385         selectedURL = ops->url();
00386     }
00387 
00388     else {
00389         QString text = locationEdit->currentText();
00390         if ( KURL::isRelativeURL(text) ) // only a full URL isn't relative. Even /path is.
00391         {
00392             if ( !text.isEmpty() && text[0] == '/' ) // absolute path
00393                 selectedURL.setPath( text );
00394             else
00395             {
00396                 selectedURL = ops->url();
00397                 selectedURL.addPath( text ); // works for filenames and relative paths
00398             }
00399         } else // complete URL
00400             selectedURL = text;
00401     }
00402 
00403     if ( selectedURL.isMalformed() ) {
00404        KMessageBox::sorry( d->mainWidget, i18n("%1\ndoes not appear to be a valid URL.\n").arg(d->url.url()), i18n("Invalid URL") );
00405        return;
00406     }
00407 
00408     if ( (mode() & KFile::LocalOnly) == KFile::LocalOnly &&
00409          !selectedURL.isLocalFile() ) {
00410         KMessageBox::sorry( d->mainWidget,
00411                             i18n("You can only select local files."),
00412                             i18n("Remote Files not Accepted") );
00413         return;
00414     }
00415 
00416     d->url = selectedURL;
00417 
00418     // d->url is a correct URL now
00419 
00420     if ( (mode() & KFile::Directory) == KFile::Directory ) {
00421         kdDebug(kfile_area) << "Directory" << endl;
00422         bool done = true;
00423         if ( d->url.isLocalFile() ) {
00424             if ( locationEdit->currentText().stripWhiteSpace().isEmpty() ) {
00425                 QFileInfo info( d->url.path() );
00426                 if ( info.isDir() ) {
00427                     d->filenames = QString::null;
00428                     d->urlList.clear();
00429                     d->urlList.append( d->url );
00430                     accept();
00431                 }
00432                 else if (!info.exists() && (mode() & KFile::File) != KFile::File) {
00433                     // directory doesn't exist, create and enter it
00434                     if ( ops->mkdir( d->url.url(), true ))
00435                         return;
00436                     else
00437                         accept();
00438                 }
00439                 else { // d->url is not a directory,
00440                     // maybe we are in File(s) | Directory mode
00441                     if ( mode() & KFile::File == KFile::File ||
00442                         mode() & KFile::Files == KFile::Files )
00443                         done = false;
00444                 }
00445             }
00446             else  // Directory mode, with file[s]/dir[s] selected
00447             {
00448                 if ( mode() & KFile::ExistingOnly )
00449                 {
00450                     if ( ops->dirOnlyMode() )
00451                     {
00452                         KURL fullURL(d->url, locationEdit->currentText());
00453                         if ( QFile::exists( fullURL.path() ) )
00454                         {
00455                             d->url = fullURL;
00456                             d->filenames = QString::null;
00457                             d->urlList.clear();
00458                             accept();
00459                             return;
00460                         }
00461                         else // doesn't exist -> reject
00462                             return;
00463                     }
00464                 }
00465 
00466                 d->filenames = locationEdit->currentText();
00467                 accept(); // what can we do?
00468             }
00469 
00470         }
00471         else { // FIXME: remote directory, should we allow that?
00472 //             qDebug( "**** Selected remote directory: %s", d->url.url().latin1());
00473             d->filenames = QString::null;
00474             d->urlList.clear();
00475             d->urlList.append( d->url );
00476 
00477             if ( mode() & KFile::ExistingOnly )
00478                 done = false;
00479             else
00480                 accept();
00481         }
00482 
00483         if ( done )
00484             return;
00485     }
00486 
00487     KIO::StatJob *job = 0L;
00488     d->statJobs.clear();
00489     d->filenames = locationEdit->currentText();
00490 
00491     if ( (mode() & KFile::Files) == KFile::Files &&
00492          !locationEdit->currentText().contains( '/' )) {
00493         kdDebug(kfile_area) << "Files\n";
00494         KURL::List list = parseSelectedURLs();
00495         KURL::List::ConstIterator it = list.begin();
00496         for ( ; it != list.end(); ++it ) {
00497             job = KIO::stat( *it, !(*it).isLocalFile() );
00498             KIO::Scheduler::scheduleJob( job );
00499             d->statJobs.append( job );
00500             connect( job, SIGNAL( result(KIO::Job *) ),
00501                      SLOT( slotStatResult( KIO::Job *) ));
00502         }
00503         return;
00504     }
00505 
00506     job = KIO::stat(d->url,!d->url.isLocalFile());
00507     d->statJobs.append( job );
00508     connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotStatResult(KIO::Job*)));
00509 }
00510 
00511 
00512 // FIXME : count all errors and show messagebox when d->statJobs.count() == 0
00513 // in case of an error, we cancel the whole operation (clear d->statJobs and
00514 // don't call accept)
00515 void KFileDialog::slotStatResult(KIO::Job* job)
00516 {
00517     kdDebug(kfile_area) << "slotStatResult" << endl;
00518     KIO::StatJob *sJob = static_cast<KIO::StatJob *>( job );
00519 
00520     if ( !d->statJobs.removeRef( sJob ) ) {
00521         return;
00522     }
00523 
00524     int count = d->statJobs.count();
00525 
00526     // errors mean in general, the location is no directory ;/
00527     // Can we be sure that it is exististant at all? (pfeiffer)
00528     if (sJob->error() && count == 0 && !ops->dirOnlyMode())
00529        accept();
00530 
00531     KIO::UDSEntry t = sJob->statResult();
00532     bool isDir = false;
00533     for (KIO::UDSEntry::ConstIterator it = t.begin();
00534         it != t.end(); ++it) {
00535        if ((*it).m_uds == KIO::UDS_FILE_TYPE ) {
00536            isDir = S_ISDIR( (mode_t)((*it).m_long));
00537            break;
00538        }
00539     }
00540 
00541     if (isDir)
00542     {
00543         if ( ops->dirOnlyMode() )
00544         {
00545             d->filenames = QString::null;
00546             d->urlList.clear();
00547             accept();
00548         }
00549         else // in File[s] mode, directory means error -> cd into it
00550         {
00551             if ( count == 0 ) {
00552                 locationEdit->clearEdit();
00553                 locationEdit->lineEdit()->setEdited( false );
00554                 setURL( sJob->url() );
00555             }
00556         }
00557         d->statJobs.clear();
00558         return;
00559     }
00560     else if ( ops->dirOnlyMode() && !isDir )
00561     {
00562         return; // ### error message?
00563     }
00564 
00565     kdDebug(kfile_area) << "filename " << sJob->url().url() << endl;
00566 
00567     if ( count == 0 )
00568         accept();
00569 }
00570 
00571 
00572 void KFileDialog::accept()
00573 {
00574     setResult( QDialog::Accepted ); // parseSelectedURLs() checks that
00575 
00576     *lastDirectory = ops->url();
00577     if (!d->fileClass.isEmpty())
00578        KRecentDirs::add(d->fileClass, ops->url().url());
00579 
00580     // clear the topmost item, we insert it as full path later on as item 1
00581     locationEdit->changeItem( QString::null, 0 );
00582 
00583     KURL::List list = selectedURLs();
00584     QValueListConstIterator<KURL> it = list.begin();
00585     for ( ; it != list.end(); ++it ) {
00586         const KURL& url = *it;
00587         // we strip the last slash (-1) because KURLComboBox does that as well
00588         // when operating in file-mode. If we wouldn't , dupe-finding wouldn't
00589         // work.
00590         QString file = url.isLocalFile() ? url.path(-1) : url.prettyURL(-1);
00591 
00592         // remove dupes
00593         for ( int i = 1; i < locationEdit->count(); i++ ) {
00594             if ( locationEdit->text( i ) == file ) {
00595                 locationEdit->removeItem( i-- );
00596                 break;
00597             }
00598         }
00599         locationEdit->insertItem( file, 1 );
00600     }
00601 
00602     KConfig *config = KGlobal::config();
00603     config->setForceGlobal( true );
00604     writeConfig( config, ConfigGroup );
00605     config->setForceGlobal( false );
00606 
00607     saveRecentFiles( config );
00608     config->sync();
00609 
00610     KDialogBase::accept();
00611 
00612     addToRecentDocuments();
00613 
00614     if ( (mode() & KFile::Files) != KFile::Files ) // single selection
00615         emit fileSelected(d->url.url());
00616 
00617     ops->close();
00618     emit okClicked();
00619 }
00620 
00621 
00622 void KFileDialog::fileHighlighted(const KFileItem *i)
00623 {
00624     if (i && i->isDir())
00625         return;
00626 
00627 
00628     if ( (ops->mode() & KFile::Files) != KFile::Files ) {
00629         if ( !i )
00630             return;
00631 
00632         d->url = i->url();
00633 
00634         if ( !d->completionLock ) {
00635             locationEdit->setCurrentItem( 0 );
00636             locationEdit->setEditText( i->name() );
00637             locationEdit->lineEdit()->setEdited( false );
00638         }
00639         emit fileHighlighted(d->url.url());
00640     }
00641 
00642     else {
00643         multiSelectionChanged();
00644         emit selectionChanged();
00645     }
00646 }
00647 
00648 void KFileDialog::fileSelected(const KFileItem *i)
00649 {
00650     if (i && i->isDir())
00651         return;
00652 
00653     if ( (ops->mode() & KFile::Files) != KFile::Files ) {
00654         if ( !i )
00655             return;
00656 
00657         d->url = i->url();
00658         locationEdit->setCurrentItem( 0 );
00659         locationEdit->setEditText( i->name() );
00660         locationEdit->lineEdit()->setEdited( false );
00661     }
00662     else {
00663         multiSelectionChanged();
00664         emit selectionChanged();
00665     }
00666     slotOk();
00667 }
00668 
00669 
00670 // I know it's slow to always iterate thru the whole filelist
00671 // (ops->selectedItems()), but what can we do?
00672 void KFileDialog::multiSelectionChanged()
00673 {
00674     if ( d->completionLock ) // FIXME: completion with multiselection?
00675         return;
00676 
00677     locationEdit->lineEdit()->setEdited( false );
00678     KFileItem *item;
00679     const KFileItemList *list = ops->selectedItems();
00680     if ( !list ) {
00681         locationEdit->clearEdit();
00682         return;
00683     }
00684 
00685     static const QString &begin = KGlobal::staticQString(" \"");
00686     KFileItemListIterator it ( *list );
00687     QString text;
00688     while ( (item = it.current()) ) {
00689         text.append( begin ).append( item->name() ).append( '\"' );
00690         ++it;
00691     }
00692     locationEdit->setCurrentItem( 0 );
00693     locationEdit->setEditText( text.stripWhiteSpace() );
00694 }
00695 
00696 void KFileDialog::init(const QString& startDir, const QString& filter, QWidget* widget)
00697 {
00698     initStatic();
00699     d = new KFileDialogPrivate();
00700     
00701     d->boxLayout = 0;
00702     d->keepLocation = false;
00703     d->operationMode = Opening;
00704     d->hasDefaultFilter = false;
00705     d->hasView = false;
00706     d->mainWidget = new QWidget( this, "KFileDialog::mainWidget");
00707     setMainWidget( d->mainWidget );
00708     d->okButton = new KPushButton( KStdGuiItem::ok(), d->mainWidget );
00709     d->okButton->setDefault( true );
00710     d->cancelButton = new KPushButton(KStdGuiItem::cancel(), d->mainWidget);
00711     connect( d->okButton, SIGNAL( clicked() ), SLOT( slotOk() ));
00712     connect( d->cancelButton, SIGNAL( clicked() ), SLOT( slotCancel() ));
00713     d->customWidget = widget;
00714     d->urlBar = 0; // delayed loading
00715     KConfig *config = KGlobal::config();
00716     KConfigGroupSaver cs( config, ConfigGroup );
00717     d->initializeSpeedbar = config->readBoolEntry( "Set speedbar defaults",
00718                                                    true );
00719     d->completionLock = false;
00720 
00721     QtMsgHandler oldHandler = qInstallMsgHandler( silenceQToolBar );
00722     toolbar = new KToolBar( d->mainWidget, "KFileDialog::toolbar", true);
00723     toolbar->setFlat(true);
00724     qInstallMsgHandler( oldHandler );
00725 
00726     QString autocompletionWhatsThisText = i18n("<p>While typing in the text area, you may be presented "
00727                                                "with possible matches. "
00728                                                "This feature can be controlled by clicking with the right mouse button "
00729                                                "and selecting a preferred mode from the <b>Text Completion</b> menu.") + "</qt>";
00730     d->pathCombo = new KURLComboBox( KURLComboBox::Directories, true,
00731                                      toolbar, "path combo" );
00732     QToolTip::add( d->pathCombo, i18n("Often used directories") );
00733     QWhatsThis::add( d->pathCombo, i18n("<qt>Commonly used locations are listed here. "
00734                                         "This includes standard locations, such as your home directory, as well as "
00735                                         "locations that have been visited recently.") + autocompletionWhatsThisText);
00736 
00737     KURL u;
00738     u.setPath( QDir::rootDirPath() );
00739     QString text = i18n("Root Directory: %1").arg( u.path() );
00740     d->pathCombo->addDefaultURL( u,
00741                                  KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00742                                  text );
00743 
00744     u.setPath( QDir::homeDirPath() );
00745     text = i18n("Home Directory: %1").arg( u.path( +1 ) );
00746     d->pathCombo->addDefaultURL( u, KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00747                                  text );
00748 
00749     KURL docPath;
00750     docPath.setPath( KGlobalSettings::documentPath() );
00751     if ( u.path(+1) != docPath.path(+1) ) {
00752         text = i18n("Documents: %1").arg( docPath.path( +1 ) );
00753         d->pathCombo->addDefaultURL( u,
00754                                      KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00755                                      text );
00756     }
00757 
00758     u.setPath( KGlobalSettings::desktopPath() );
00759     text = i18n("Desktop: %1").arg( u.path( +1 ) );
00760     d->pathCombo->addDefaultURL( u,
00761                                  KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00762                                  text );
00763 
00764     u.setPath( "/tmp" );
00765 
00766     d->url = getStartURL( startDir, d->fileClass );
00767     d->selection = d->url.url();
00768 
00769     // If local, check it exists. If not, go up until it exists.
00770     if ( d->url.isLocalFile() )
00771     {
00772         if ( !QFile::exists( d->url.path() ) )
00773         {
00774             d->url = d->url.upURL();
00775             QDir dir( d->url.path() );
00776             while ( !dir.exists() )
00777             {
00778                 d->url = d->url.upURL();
00779                 dir.setPath( d->url.path() );
00780             }
00781         }
00782     }
00783 
00784     ops = new KDirOperator(d->url, d->mainWidget, "KFileDialog::ops");
00785     ops->setOnlyDoubleClickSelectsFiles( true );
00786     connect(ops, SIGNAL(urlEntered(const KURL&)),
00787             SLOT(urlEntered(const KURL&)));
00788     connect(ops, SIGNAL(fileHighlighted(const KFileItem *)),
00789             SLOT(fileHighlighted(const KFileItem *)));
00790     connect(ops, SIGNAL(fileSelected(const KFileItem *)),
00791             SLOT(fileSelected(const KFileItem *)));
00792     connect(ops, SIGNAL(finishedLoading()),
00793             SLOT(slotLoadingFinished()));
00794 
00795     ops->setupMenu(KDirOperator::SortActions |
00796                    KDirOperator::FileActions |
00797                    KDirOperator::ViewActions);
00798     KActionCollection *coll = ops->actionCollection();
00799 
00800     // plug nav items into the toolbar
00801     coll->action( "up" )->plug( toolbar );
00802     coll->action( "up" )->setWhatsThis(i18n("<qt>Click this button to enter the parent directory.<p>"
00803                                             "For instance, if the current location is file:/home/%1 clicking this "
00804                                             "button will take you to file:/home.</qt>").arg(getlogin()));
00805     coll->action( "back" )->plug( toolbar );
00806     coll->action( "back" )->setWhatsThis(i18n("Click this button to move backwards one step in the browsing history."));
00807     coll->action( "forward" )->plug( toolbar );
00808     coll->action( "forward" )->setWhatsThis(i18n("Click this button to move forward one step in the browsing history."));
00809     coll->action( "reload" )->plug( toolbar );
00810     coll->action( "reload" )->setWhatsThis(i18n("Click this button to reload the contents of the current location."));
00811     coll->action( "mkdir" )->setShortcut(Key_F10);
00812     coll->action( "mkdir" )->plug( toolbar );
00813     coll->action( "mkdir" )->setWhatsThis(i18n("Click this button to create a new directory."));
00814 
00815     d->bookmarkHandler = new KFileBookmarkHandler( this );
00816     toolbar->insertButton(QString::fromLatin1("bookmark"),
00817                           (int)HOTLIST_BUTTON, true,
00818                           i18n("Bookmarks"));
00819     toolbar->getButton(HOTLIST_BUTTON)->setPopup( d->bookmarkHandler->menu(),
00820                                                   true);
00821     QWhatsThis::add(toolbar->getButton(HOTLIST_BUTTON),
00822                     i18n("<qt>This button allows you to bookmark specific locations. "
00823                          "Click on this button to open the bookmark menu where you may add, "
00824                          "edit or select a bookmark.<p>"
00825                          "These bookmarks are specific to the file dialog, but otherwise operate "
00826                          "like bookmarks elsewhere in KDE.</qt>"));
00827     connect( d->bookmarkHandler, SIGNAL( openURL( const QString& )),
00828              SLOT( enterURL( const QString& )));
00829 
00830     KToggleAction *showSidebarAction =
00831         new KToggleAction(i18n("Show Quick Access Navigation Panel"), Key_F9, coll,"toggleSpeedbar");
00832     connect( showSidebarAction, SIGNAL( toggled( bool ) ),
00833              SLOT( toggleSpeedbar( bool )) );
00834 
00835     KActionMenu *menu = new KActionMenu( i18n("Configure"), "configure", this, "extra menu" );
00836     menu->setWhatsThis(i18n("<qt>This is the configuration menu for the file dialog. "
00837                             "Various options can be accessed from this menu including: <ul>"
00838                             "<li>how files are sorted in the list</li>"
00839                             "<li>types of view, including icon and list</li>"
00840                             "<li>showing of hidden files</li>"
00841                             "<li>the Quick Access navigation panel</li>"
00842                             "<li>file previews</li>"
00843                             "<li>separating directories from files</li></ul></qt>"));
00844     menu->insert( coll->action( "sorting menu" ));
00845     menu->insert( coll->action( "separator" ));
00846     coll->action( "short view" )->setShortcut(Key_F6);
00847     menu->insert( coll->action( "short view" ));
00848     coll->action( "detailed view" )->setShortcut(Key_F7);
00849     menu->insert( coll->action( "detailed view" ));
00850     menu->insert( coll->action( "separator" ));
00851     coll->action( "show hidden" )->setShortcut(Key_F8);
00852     menu->insert( coll->action( "show hidden" ));
00853     menu->insert( showSidebarAction );
00854     coll->action( "preview" )->setShortcut(Key_F11);
00855     menu->insert( coll->action( "preview" ));
00856     coll->action( "separate dirs" )->setShortcut(Key_F12);
00857     menu->insert( coll->action( "separate dirs" ));
00858 
00859     menu->setDelayed( false );
00860     connect( menu->popupMenu(), SIGNAL( aboutToShow() ),
00861              ops, SLOT( updateSelectionDependentActions() ));
00862     menu->plug( toolbar );
00863 
00864     /*
00865      * ugly little hack to have a 5 pixel space between the buttons
00866      * and the combo box
00867      */
00868     QWidget *spacerWidget = new QWidget(toolbar);
00869     spacerWidget->setMinimumWidth(spacingHint());
00870     spacerWidget->setMaximumWidth(spacingHint());
00871     d->m_pathComboIndex = toolbar->insertWidget(-1, -1, spacerWidget);
00872     toolbar->insertWidget(PATH_COMBO, 0, d->pathCombo);
00873 
00874 
00875     toolbar->setItemAutoSized (PATH_COMBO);
00876     toolbar->setIconText(KToolBar::IconOnly);
00877     toolbar->setBarPos(KToolBar::Top);
00878     toolbar->setMovingEnabled(false);
00879     toolbar->adjustSize();
00880 
00881     d->pathCombo->setCompletionObject( ops->dirCompletionObject(), false );
00882 
00883     connect( d->pathCombo, SIGNAL( urlActivated( const KURL&  )),
00884              this,  SLOT( enterURL( const KURL& ) ));
00885     connect( d->pathCombo, SIGNAL( returnPressed( const QString&  )),
00886              this,  SLOT( enterURL( const QString& ) ));
00887     connect( d->pathCombo, SIGNAL(textChanged( const QString& )),
00888              SLOT( pathComboChanged( const QString& ) ));
00889     connect( d->pathCombo, SIGNAL( completion( const QString& )),
00890              SLOT( dirCompletion( const QString& )));
00891     connect( d->pathCombo, SIGNAL( textRotation(KCompletionBase::KeyBindingType) ),
00892              d->pathCombo, SLOT( rotateText(KCompletionBase::KeyBindingType) ));
00893 
00894     QString whatsThisText;
00895     if (d->operationMode == KFileDialog::Saving)
00896     {
00897         whatsThisText = i18n("<qt>This is the name to save the file as.") +
00898                         autocompletionWhatsThisText;
00899     }
00900     else if (ops->mode() & KFile::Files)
00901     {
00902         whatsThisText = i18n("<qt>This is the list of files to open. More than "
00903                              "one file can be specified by listing several "
00904                              "files, separated by spaces.") +
00905                         autocompletionWhatsThisText;
00906     }
00907     else
00908     {
00909         whatsThisText = i18n("<qt>This is the name of the file to open.") +
00910                         autocompletionWhatsThisText;
00911     }
00912 
00913     // the Location label/edit
00914     d->locationLabel = new QLabel(i18n("&Location:"), d->mainWidget);
00915     QWhatsThis::add(d->locationLabel, whatsThisText);
00916     locationEdit = new KURLComboBox(KURLComboBox::Files, true,
00917                                     d->mainWidget, "LocationEdit");
00918     d->locationLabel->setBuddy(locationEdit);
00919     QWhatsThis::add(locationEdit, whatsThisText);
00920     // to get the completionbox-signals connected:
00921     locationEdit->setHandleSignals( true );
00922     (void) locationEdit->completionBox();
00923 
00924     locationEdit->setFocus();
00925 //     locationEdit->setCompletionObject( new KURLCompletion() );
00926 //     locationEdit->setAutoDeleteCompletionObject( true );
00927     locationEdit->setCompletionObject( ops->completionObject(), false );
00928 
00929     connect( locationEdit, SIGNAL( returnPressed() ),
00930              this, SLOT( slotOk()));
00931     connect(locationEdit, SIGNAL( activated( const QString&  )),
00932             this,  SLOT( locationActivated( const QString& ) ));
00933     connect( locationEdit, SIGNAL( completion( const QString& )),
00934              SLOT( fileCompletion( const QString& )));
00935     connect( locationEdit, SIGNAL( textRotation(KCompletionBase::KeyBindingType) ),
00936              locationEdit, SLOT( rotateText(KCompletionBase::KeyBindingType) ));
00937 
00938     // the Filter label/edit
00939     whatsThisText = i18n("<qt>This is the filter to apply to the file list. "
00940                          "File names that do not match the filter will not be shown.<p>"
00941                          "You may select from one of the preset filters in the "
00942                          "drop down menu, or you may enter a custom filter "
00943                          "directly into the text area.<p>"
00944                          "Wildcards such as * and ? are allowed.</qt>");
00945     d->filterLabel = new QLabel(i18n("&Filter:"), d->mainWidget);
00946     QWhatsThis::add(d->filterLabel, whatsThisText);
00947     filterWidget = new KFileFilterCombo(d->mainWidget,
00948                                         "KFileDialog::filterwidget");
00949     QWhatsThis::add(filterWidget, whatsThisText);
00950     setFilter(filter);
00951     d->filterLabel->setBuddy(filterWidget);
00952     connect(filterWidget, SIGNAL(filterChanged()), SLOT(slotFilterChanged()));
00953 
00954     initGUI(); // activate GM
00955 
00956     readRecentFiles( config );
00957 
00958     adjustSize();
00959 
00960     // we set the completionLock to avoid entering pathComboChanged() when
00961     // inserting the list of URLs into the combo.
00962     d->completionLock = true;
00963     ops->setViewConfig( config, ConfigGroup );
00964     readConfig( config, ConfigGroup );
00965     setSelection(d->selection);
00966     d->completionLock = false;
00967 }
00968 
00969 void KFileDialog::initSpeedbar()
00970 {
00971     d->urlBar = new KFileSpeedBar( d->mainWidget, "url bar" );
00972     connect( d->urlBar, SIGNAL( activated( const KURL& )),
00973              SLOT( enterURL( const KURL& )) );
00974 
00975     // need to set the current url of the urlbar manually (not via urlEntered()
00976     // here, because the initial url of KDirOperator might be the same as the
00977     // one that will be set later (and then urlEntered() won't be emitted).
00978     // ### REMOVE THIS when KDirOperator's initial URL (in the c'tor) is gone.
00979         d->urlBar->setCurrentItem( d->url );
00980 
00981     d->urlBarLayout->insertWidget( 0, d->urlBar );
00982 }
00983 
00984 void KFileDialog::initGUI()
00985 {
00986     delete d->boxLayout; // deletes all sub layouts
00987 
00988     d->boxLayout = new QVBoxLayout( d->mainWidget, 0, KDialog::spacingHint());
00989     d->boxLayout->addWidget(toolbar, AlignTop);
00990 
00991     d->urlBarLayout = new QHBoxLayout( d->boxLayout ); // needed for the urlBar that may appear
00992     QVBoxLayout *vbox = new QVBoxLayout( d->urlBarLayout );
00993 
00994     vbox->addWidget(ops, 4);
00995     vbox->addSpacing(3);
00996 
00997     QGridLayout* lafBox= new QGridLayout(2, 3, KDialog::spacingHint());
00998     vbox->addLayout(lafBox, 0);
00999     lafBox->addWidget(d->locationLabel, 0, 0, AlignVCenter);
01000     lafBox->addWidget(locationEdit, 0, 1, AlignVCenter);
01001     lafBox->addWidget(d->okButton, 0, 2, AlignVCenter);
01002 
01003     lafBox->addWidget(d->filterLabel, 1, 0, AlignVCenter);
01004     lafBox->addWidget(filterWidget, 1, 1, AlignVCenter);
01005     lafBox->addWidget(d->cancelButton, 1, 2, AlignVCenter);
01006 
01007     lafBox->setColStretch(1, 4);
01008 
01009     vbox->addSpacing(3);
01010 
01011     setTabOrder(ops,  locationEdit);
01012     setTabOrder(locationEdit, filterWidget);
01013     setTabOrder(filterWidget, d->okButton);
01014     setTabOrder(d->okButton, d->cancelButton);
01015     setTabOrder(d->cancelButton, d->pathCombo);
01016     setTabOrder(d->pathCombo, ops);
01017 
01018     // If a custom widget was specified...
01019     if ( d->customWidget != 0 )
01020     {
01021         // ...add it to the dialog, below the filter list box.
01022 
01023         // Change the parent so that this widget is a child of the main widget
01024         d->customWidget->reparent( d->mainWidget, QPoint() );
01025 
01026         vbox->addWidget( d->customWidget );
01027 
01028         // FIXME: This should adjust the tab orders so that the custom widget
01029         // comes after the Cancel button. The code appears to do this, but the result
01030         // somehow screws up the tab order of the file path combo box. Not a major
01031         // problem, but ideally the tab order with a custom widget should be
01032         // the same as the order without one.
01033         setTabOrder(d->cancelButton, d->customWidget);
01034         setTabOrder(d->customWidget, d->pathCombo);
01035     }
01036     else
01037     {
01038         setTabOrder(d->cancelButton, d->pathCombo);
01039     }
01040 
01041     setTabOrder(d->pathCombo, ops);
01042 }
01043 
01044 void KFileDialog::slotFilterChanged()
01045 {
01046     QString filter = filterWidget->currentFilter();
01047     ops->clearFilter();
01048 
01049     if ( filter.find( '/' ) > -1 ) {
01050         QStringList types = QStringList::split( " ", filter );
01051         types.prepend( "inode/directory" );
01052         ops->setMimeFilter( types );
01053     }
01054     else
01055         ops->setNameFilter( filter );
01056 
01057     ops->updateDir();
01058     emit filterChanged( filter );
01059 }
01060 
01061 
01062 void KFileDialog::pathComboChanged( const QString& txt )
01063 {
01064     if ( d->completionLock )
01065         return;
01066 
01067     static const QString& localRoot = KGlobal::staticQString("file:/");
01068     KURLComboBox *combo = d->pathCombo;
01069     QString text = txt;
01070     QString newText = text.left(combo->cursorPosition() -1);
01071     KURL url;
01072     if ( text.at( 0 ) == '/' )
01073         url.setPath( text );
01074     else
01075         url = text;
01076 
01077 
01078     // don't mess with malformed urls or remote urls without directory or host
01079     if ( url.isMalformed() ||
01080          !KProtocolInfo::supportsListing( url.protocol() ) ||
01081          ( !url.url().startsWith( localRoot ) &&
01082            ( url.directory().isNull() || url.host().isNull()) )) {
01083         d->completionHack = newText;
01084         return;
01085     }
01086 
01087     // when editing somewhere in the middle of the text, don't complete or
01088     // follow directories
01089     if ( combo->cursorPosition() != (int) combo->currentText().length() ) {
01090         d->completionHack = newText;
01091         return;
01092     }
01093 
01094     // the user is backspacing -> don't annoy him with completions
01095     if ( autoDirectoryFollowing && d->completionHack.startsWith( newText ) ) {
01096         // but we can follow the directories, if configured so
01097 
01098         // find out the current directory according to combobox and cd into
01099         int l = text.length() - 1;
01100         while (!text.isEmpty() && text[l] != '/')
01101             l--;
01102 
01103         KURL newLocation(text.left(l+1));
01104 
01105         if ( !newLocation.isMalformed() && newLocation != ops->url() ) {
01106             setURL(newLocation, true);
01107             combo->setEditText(text);
01108         }
01109     }
01110 
01111     // typing forward, ending with a / -> cd into the directory
01112     else if ( autoDirectoryFollowing &&
01113               text.at(text.length()-1) == '/' && ops->url() != text ) {
01114         d->selection = QString::null;
01115         setURL( text, false );
01116     }
01117 
01118     d->completionHack = newText;
01119 }
01120 
01121 
01122 void KFileDialog::setURL(const KURL& url, bool clearforward)
01123 {
01124     d->selection = QString::null;
01125     ops->setURL( url, clearforward);
01126 }
01127 
01128 // Protected
01129 void KFileDialog::urlEntered(const KURL& url)
01130 {
01131     QString filename = locationEdit->currentText();
01132     d->selection = QString::null;
01133 
01134     if ( d->pathCombo->count() != 0 ) { // little hack
01135         d->pathCombo->setURL( url );
01136     }
01137 
01138     locationEdit->blockSignals( true );
01139     locationEdit->setCurrentItem( 0 );
01140     if ( d->keepLocation )
01141         locationEdit->setEditText( filename );
01142 
01143     locationEdit->blockSignals( false );
01144     d->completionHack = d->pathCombo->currentText();
01145 
01146     if ( d->urlBar )
01147         d->urlBar->setCurrentItem( url );
01148 }
01149 
01150 void KFileDialog::locationActivated( const QString& url )
01151 {
01152     setSelection( url );
01153 }
01154 
01155 void KFileDialog::enterURL( const KURL& url)
01156 {
01157     setURL( url );
01158 }
01159 
01160 void KFileDialog::enterURL( const QString& url )
01161 {
01162     setURL( url );
01163 }
01164 
01165 void KFileDialog::toolbarCallback(int) // SLOT
01166 {
01167     /*
01168      * yes, nothing uses this anymore.
01169      * it used to be used to show the configure dialog
01170      */
01171 }
01172 
01173 
01174 void KFileDialog::setSelection(const QString& url)
01175 {
01176     kdDebug(kfile_area) << "setSelection " << url << endl;
01177 
01178     if (url.isEmpty()) {
01179         d->selection = QString::null;
01180         return;
01181     }
01182 
01183     // This code is the same as the one in slotOK
01184     KURL u;
01185     if ( KURL::isRelativeURL(url) )
01186     {
01187         if (!url.isEmpty() && url[0] == '/' ) // absolute path
01188             u.setPath( url );
01189         else
01190         {
01191             u = ops->url();
01192             u.addPath( url ); // works for filenames and relative paths
01193         }
01194     } else // complete URL
01195         u = url;
01196 
01197     if (u.isMalformed()) { // if it still is
01198         kdWarning() << url << " is not a correct argument for setSelection!" << endl;
01199         return;
01200     }
01201 
01202     /* we strip the first / from the path to avoid file://usr which means
01203      *  / on host usr
01204      */
01205     KFileItem i(KFileItem::Unknown, KFileItem::Unknown, u, true );
01206     //    KFileItem i(u.path());
01207     if ( i.isDir() && u.isLocalFile() && QFile::exists( u.path() ) ) {
01208         // trust isDir() only if the file is
01209         // local (we cannot stat non-local urls) and if it exists!
01210         // (as KFileItem does not check if the file exists or not
01211         // -> the statbuffer is undefined -> isDir() is unreliable) (Simon)
01212         setURL(u, true);
01213     }
01214     else {
01215         QString filename = u.url();
01216         int sep = filename.findRev('/');
01217         if (sep >= 0) { // there is a / in it
01218             if ( KProtocolInfo::supportsListing( u.protocol() ))
01219                 setURL(filename.left(sep), true);
01220 
01221             // filename must be decoded, or "name with space" would become
01222             // "name%20with%20space", so we use KURL::fileName()
01223             filename = u.fileName();
01224             kdDebug(kfile_area) << "filename " << filename << endl;
01225             d->selection = filename;
01226             locationEdit->setCurrentItem( 0 );
01227             locationEdit->setEditText( filename );
01228 
01229             // tell the line edit that it has been edited
01230             // otherwise we won't know this was set by the user
01231             // and it will be ignored if there has been an
01232             // auto completion. this caused bugs where automcompletion
01233             // would start, the user would pick something from the
01234             // history and then hit Ok only to get the autocompleted
01235             // selection. OOOPS.
01236             locationEdit->lineEdit()->setEdited( true );
01237         }
01238 
01239         d->url = ops->url();
01240         d->url.addPath(filename);
01241     }
01242 }
01243 
01244 void KFileDialog::slotLoadingFinished()
01245 {
01246     if ( !d->selection.isNull() )
01247         ops->setCurrentItem( d->selection );
01248 }
01249 
01250 
01251 void KFileDialog::dirCompletion( const QString& dir ) // SLOT
01252 {
01253     // we don't support popup completion here, sorry
01254     if ( ops->dirCompletionObject()->completionMode() ==
01255          KGlobalSettings::CompletionPopup )
01256         return;
01257 
01258     QString base = ops->url().url();
01259 
01260     // if someone uses completion, he doesn't like the current selection
01261     d->selection = QString::null;
01262 
01263     KURL url;
01264     if ( dir.at( 0 ) == '/' )
01265         url.setPath( dir );
01266     else
01267         url = dir;
01268 
01269     if ( url.isMalformed() )
01270         return; // invalid entry in path combo
01271 
01272     d->completionLock = true;
01273 
01274     if (url.url().startsWith( base )) {
01275         QString complete = ops->makeDirCompletion( url.fileName(false) );
01276 
01277         if (!complete.isNull()) {
01278         if(!base.endsWith("/"))
01279             base.append('/');
01280             QString newText = base + complete;
01281             QString fileProt = QString::fromLatin1( "file:" );
01282 
01283             if ( dir.startsWith( fileProt ) != newText.startsWith( fileProt ))
01284                 newText = newText.mid( 5 ); // remove file:
01285 
01286             d->pathCombo->setCompletedText( newText );
01287             d->url = newText;
01288         }
01289     }
01290     d->completionLock = false;
01291 }
01292 
01293 
01294 void KFileDialog::fileCompletion( const QString& file )
01295 {
01296     d->completionLock = true;
01297     QString text = ops->makeCompletion( file );
01298     if ( !text.isEmpty() ) {
01299         KCompletion *comp = ops->completionObject();
01300         if ( comp->completionMode() == KGlobalSettings::CompletionPopup ||
01301             comp->completionMode() == KGlobalSettings::CompletionPopupAuto )
01302             locationEdit->setCompletedItems( comp->allMatches() );
01303         else
01304             locationEdit->setCompletedText( text );
01305     }
01306     else
01307         if (locationEdit->completionMode() == KGlobalSettings::CompletionPopup ||
01308             locationEdit->completionMode() == KGlobalSettings::CompletionPopupAuto )
01309             locationEdit->completionBox()->hide();
01310 
01311     d->completionLock = false;
01312 }
01313 
01314 void KFileDialog::updateStatusLine(int /* dirs */, int /* files */)
01315 {
01316     kdWarning() << "KFileDialog::updateStatusLine is deprecated! The status line no longer exists. Do not try and use it!" << endl;
01317 }
01318 
01319 QString KFileDialog::getOpenFileName(const QString& startDir,
01320                                      const QString& filter,
01321                                      QWidget *parent, const QString& caption)
01322 {
01323     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01324     dlg.setOperationMode( Opening );
01325 
01326     dlg.setMode( KFile::File | KFile::LocalOnly );
01327     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01328 
01329     dlg.ops->clearHistory();
01330     dlg.exec();
01331 
01332     return dlg.selectedFile();
01333 }
01334 
01335 QStringList KFileDialog::getOpenFileNames(const QString& startDir,
01336                                           const QString& filter,
01337                                           QWidget *parent,
01338                                           const QString& caption)
01339 {
01340     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01341     dlg.setOperationMode( Opening );
01342 
01343     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01344     dlg.setMode(KFile::Files | KFile::LocalOnly);
01345     dlg.ops->clearHistory();
01346     dlg.exec();
01347 
01348     return dlg.selectedFiles();
01349 }
01350 
01351 KURL KFileDialog::getOpenURL(const QString& startDir, const QString& filter,
01352                                 QWidget *parent, const QString& caption)
01353 {
01354     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01355     dlg.setOperationMode( Opening );
01356 
01357     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01358     dlg.setMode( KFile::File );
01359     dlg.ops->clearHistory();
01360     dlg.exec();
01361 
01362     return dlg.selectedURL();
01363 }
01364 
01365 KURL::List KFileDialog::getOpenURLs(const QString& startDir,
01366                                           const QString& filter,
01367                                           QWidget *parent,
01368                                           const QString& caption)
01369 {
01370     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01371     dlg.setOperationMode( Opening );
01372 
01373     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01374     dlg.setMode(KFile::Files);
01375     dlg.ops->clearHistory();
01376     dlg.exec();
01377 
01378     return dlg.selectedURLs();
01379 }
01380 
01381 KURL KFileDialog::getExistingURL(const QString& startDir,
01382                                        QWidget *parent,
01383                                        const QString& caption)
01384 {
01385     KFileDialog dlg(startDir, QString::null, parent, "filedialog", true);
01386     dlg.setMode(KFile::Directory | KFile::ExistingOnly);
01387     // to get "All Directories" instead of "All Files" in the combo
01388     dlg.setFilter( QString::null );
01389     dlg.ops->clearHistory();
01390     dlg.setCaption(caption.isNull() ? i18n("Select Directory") : caption);
01391     dlg.exec();
01392 
01393     return dlg.selectedURL();
01394 }
01395 
01396 QString KFileDialog::getExistingDirectory(const QString& startDir,
01397                                           QWidget *parent,
01398                                           const QString& caption)
01399 {
01400     KFileDialog dlg(startDir, QString::null, parent, "filedialog", true);
01401     dlg.setMode(KFile::Directory | KFile::LocalOnly | KFile::ExistingOnly);
01402     // to get "All Directories" instead of "All Files" in the combo
01403     dlg.setFilter( QString::null );
01404     dlg.ops->clearHistory();
01405     dlg.setCaption(caption.isNull() ? i18n("Select Directory") : caption);
01406     dlg.exec();
01407 
01408     return dlg.selectedFile();
01409 }
01410 
01411 KURL KFileDialog::getImageOpenURL( const QString& startDir, QWidget *parent,
01412                                    const QString& caption)
01413 {
01414     QStringList mimetypes = KImageIO::mimeTypes( KImageIO::Reading );
01415     KFileDialog dlg(startDir,
01416                     mimetypes.join(" "),
01417                     parent, "filedialog", true);
01418     dlg.setOperationMode( Opening );
01419     dlg.setCaption( caption.isNull() ? i18n("Open") : caption );
01420     dlg.setMode( KFile::File );
01421 
01422     KImageFilePreview *ip = new KImageFilePreview( &dlg );
01423     dlg.setPreviewWidget( ip );
01424     dlg.exec();
01425 
01426     return dlg.selectedURL();
01427 }
01428 
01429 KURL KFileDialog::selectedURL() const
01430 {
01431     if ( result() == QDialog::Accepted )
01432         return d->url;
01433     else
01434         return KURL();
01435 }
01436 
01437 KURL::List KFileDialog::selectedURLs() const
01438 {
01439     KURL::List list;
01440     if ( result() == QDialog::Accepted ) {
01441         if ( (ops->mode() & KFile::Files) == KFile::Files )
01442             list = parseSelectedURLs();
01443         else
01444             list.append( d->url );
01445     }
01446     return list;
01447 }
01448 
01449 
01450 KURL::List& KFileDialog::parseSelectedURLs() const
01451 {
01452     if ( d->filenames.isEmpty() ) {
01453         return d->urlList;
01454     }
01455 
01456     d->urlList.clear();
01457     if ( d->filenames.contains( '/' )) { // assume _one_ absolute filename
01458         static const QString &prot = KGlobal::staticQString(":/");
01459         KURL u;
01460         if ( d->filenames.find( prot ) != -1 )
01461             u = d->filenames;
01462         else
01463             u.setPath( d->filenames );
01464 
01465         if ( !u.isMalformed() )
01466             d->urlList.append( u );
01467         else
01468             KMessageBox::error( d->mainWidget,
01469                                 i18n("The chosen filename(s) don't\nappear to be valid."), i18n("Invalid Filename(s)") );
01470     }
01471 
01472     else
01473         d->urlList = tokenize( d->filenames );
01474 
01475     d->filenames = QString::null; // indicate that we parsed that one
01476 
01477     return d->urlList;
01478 }
01479 
01480 
01481 // FIXME: current implementation drawback: a filename can't contain quotes
01482 KURL::List KFileDialog::tokenize( const QString& line ) const
01483 {
01484     KURL::List urls;
01485     KURL u( ops->url() );
01486     QString name;
01487 
01488     int count = line.contains( '"' );
01489     if ( count == 0 ) { // no " " -> assume one single file
01490         u.setFileName( line );
01491         if ( !u.isMalformed() )
01492             urls.append( u );
01493 
01494         return urls;
01495     }
01496 
01497     if ( (count % 2) == 1 ) { // odd number of " -> error
01498         QWidget *that = const_cast<KFileDialog *>(this);
01499         KMessageBox::sorry(that, i18n("The requested filenames\n%1\ndon't look valid to me.\nMake sure every filename is enclosed in double quotes.").arg(line), i18n("Filename Error"));
01500         return urls;
01501     }
01502 
01503     int start = 0;
01504     int index1 = -1, index2 = -1;
01505     while ( true ) {
01506         index1 = line.find( '"', start );
01507         index2 = line.find( '"', index1 + 1 );
01508 
01509         if ( index1 < 0 )
01510             break;
01511 
01512         // get everything between the " "
01513         name = line.mid( index1 + 1, index2 - index1 - 1 );
01514         u.setFileName( name );
01515         if ( !u.isMalformed() )
01516             urls.append( u );
01517 
01518         start = index2 + 1;
01519     }
01520     return urls;
01521 }
01522 
01523 
01524 QString KFileDialog::selectedFile() const
01525 {
01526     if ( result() == QDialog::Accepted )
01527     {
01528        if (d->url.isLocalFile())
01529            return d->url.path();
01530     }
01531     return QString::null;
01532 }
01533 
01534 QStringList KFileDialog::selectedFiles() const
01535 {
01536     QStringList list;
01537 
01538     if ( result() == QDialog::Accepted ) {
01539         if ( (ops->mode() & KFile::Files) == KFile::Files ) {
01540             KURL::List urls = parseSelectedURLs();
01541         QValueListConstIterator<KURL> it = urls.begin();
01542         while ( it != urls.end() ) {
01543         if ( (*it).isLocalFile() )
01544             list.append( (*it).path() );
01545         ++it;
01546         }
01547     }
01548 
01549         else { // single-selection mode
01550         if ( d->url.isLocalFile() )
01551         list.append( d->url.path() );
01552         }
01553     }
01554 
01555     return list;
01556 }
01557 
01558 KURL KFileDialog::baseURL() const
01559 {
01560     return ops->url();
01561 }
01562 
01563 QString KFileDialog::getSaveFileName(const QString& dir, const QString& filter,
01564                                      QWidget *parent,
01565                                      const QString& caption)
01566 {
01567     bool specialDir = dir.at(0) == ':';
01568     KFileDialog dlg( specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01569     if ( !specialDir )
01570         dlg.setSelection( dir ); // may also be a filename
01571 
01572     dlg.setOperationMode( Saving );
01573     dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01574 
01575     dlg.exec();
01576 
01577     QString filename = dlg.selectedFile();
01578     if (!filename.isEmpty())
01579         KRecentDocument::add(filename);
01580 
01581     return filename;
01582 }
01583 
01584 KURL KFileDialog::getSaveURL(const QString& dir, const QString& filter,
01585                              QWidget *parent, const QString& caption)
01586 {
01587     bool specialDir = dir.at(0) == ':';
01588     KFileDialog dlg(specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01589     if ( !specialDir )
01590     dlg.setSelection( dir ); // may also be a filename
01591 
01592     dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01593     dlg.setOperationMode( Saving );
01594 
01595     dlg.exec();
01596 
01597     KURL url = dlg.selectedURL();
01598     if (!url.isMalformed())
01599         KRecentDocument::add( url );
01600 
01601     return url;
01602 }
01603 
01604 void KFileDialog::show()
01605 {
01606     if ( !d->hasView ) { // delayed view-creation
01607         ops->setView(KFile::Default);
01608         ops->clearHistory();
01609         d->hasView = true;
01610     }
01611 
01612     KDialogBase::show();
01613 }
01614 
01615 void KFileDialog::setMode( KFile::Mode m )
01616 {
01617     ops->setMode(m);
01618     if ( ops->dirOnlyMode() ) {
01619         filterWidget->setDefaultFilter( i18n("*|All Directories") );
01620     }
01621     else {
01622         filterWidget->setDefaultFilter( i18n("*|All Files") );
01623     }
01624 }
01625 
01626 void KFileDialog::setMode( unsigned int m )
01627 {
01628     setMode(static_cast<KFile::Mode>( m ));
01629 }
01630 
01631 KFile::Mode KFileDialog::mode() const
01632 {
01633     return ops->mode();
01634 }
01635 
01636 
01637 void KFileDialog::readConfig( KConfig *kc, const QString& group )
01638 {
01639     if ( !kc )
01640         return;
01641 
01642     QString oldGroup = kc->group();
01643     if ( !group.isEmpty() )
01644         kc->setGroup( group );
01645 
01646     ops->readConfig( kc, group );
01647 
01648     KURLComboBox *combo = d->pathCombo;
01649     combo->setURLs( kc->readListEntry( RecentURLs ), KURLComboBox::RemoveTop );
01650     combo->setMaxItems( kc->readNumEntry( RecentURLsNumber,
01651                                           DefaultRecentURLsNumber ) );
01652     combo->setURL( ops->url() );
01653     autoDirectoryFollowing = kc->readBoolEntry( AutoDirectoryFollowing,
01654                                                 DefaultDirectoryFollowing );
01655 
01656     KGlobalSettings::Completion cm = (KGlobalSettings::Completion)
01657                                       kc->readNumEntry( PathComboCompletionMode,
01658                                       KGlobalSettings::CompletionAuto );
01659     if ( cm != KGlobalSettings::completionMode() )
01660         combo->setCompletionMode( cm );
01661 
01662     cm = (KGlobalSettings::Completion)
01663          kc->readNumEntry( LocationComboCompletionMode,
01664                            KGlobalSettings::CompletionAuto );
01665     if ( cm != KGlobalSettings::completionMode() )
01666         locationEdit->setCompletionMode( cm );
01667 
01668     // show or don't show the speedbar
01669     toggleSpeedbar( kc->readBoolEntry(ShowSpeedbar, true) );
01670 
01671     int w1 = minimumSize().width();
01672     int w2 = toolbar->sizeHint().width() + 10;
01673     if (w1 < w2)
01674         setMinimumWidth(w2);
01675 
01676     QSize size = configDialogSize( group );
01677     resize( size );
01678     kc->setGroup( oldGroup );
01679 }
01680 
01681 void KFileDialog::writeConfig( KConfig *kc, const QString& group )
01682 {
01683     if ( !kc )
01684         return;
01685 
01686     QString oldGroup = kc->group();
01687     if ( !group.isEmpty() )
01688         kc->setGroup( group );
01689 
01690     kc->writeEntry( RecentURLs, d->pathCombo->urls() );
01691     saveDialogSize( group, true );
01692     kc->writeEntry( PathComboCompletionMode, static_cast<int>(d->pathCombo->completionMode()) );
01693     kc->writeEntry(LocationComboCompletionMode, static_cast<int>(locationEdit->completionMode()) );
01694     kc->writeEntry( ShowSpeedbar, d->urlBar && !d->urlBar->isHidden() );
01695 
01696     ops->writeConfig( kc, group );
01697     kc->setGroup( oldGroup );
01698 }
01699 
01700 
01701 void KFileDialog::readRecentFiles( KConfig *kc )
01702 {
01703     QString oldGroup = kc->group();
01704     kc->setGroup( ConfigGroup );
01705 
01706     locationEdit->setMaxItems( kc->readNumEntry( RecentFilesNumber,
01707                                                  DefaultRecentURLsNumber ) );
01708     locationEdit->setURLs( kc->readListEntry( RecentFiles ),
01709                            KURLComboBox::RemoveBottom );
01710     locationEdit->insertItem( QString::null, 0 ); // dummy item without pixmap
01711     locationEdit->setCurrentItem( 0 );
01712 
01713     kc->setGroup( oldGroup );
01714 }
01715 
01716 void KFileDialog::saveRecentFiles( KConfig *kc )
01717 {
01718     QString oldGroup = kc->group();
01719     kc->setGroup( ConfigGroup );
01720 
01721     kc->writeEntry( RecentFiles, locationEdit->urls() );
01722 
01723     kc->setGroup( oldGroup );
01724 }
01725 
01726 KPushButton * KFileDialog::okButton() const
01727 {
01728     return d->okButton;
01729 }
01730 
01731 KPushButton * KFileDialog::cancelButton() const
01732 {
01733     return d->cancelButton;
01734 }
01735 
01736 void KFileDialog::slotCancel()
01737 {
01738     ops->close();
01739     KDialogBase::slotCancel();
01740 }
01741 
01742 void KFileDialog::setKeepLocation( bool keep )
01743 {
01744     d->keepLocation = keep;
01745 }
01746 
01747 bool KFileDialog::keepsLocation() const
01748 {
01749     return d->keepLocation;
01750 }
01751 
01752 void KFileDialog::setOperationMode( OperationMode mode )
01753 {
01754     d->operationMode = mode;
01755     d->keepLocation = (mode == Saving);
01756     filterWidget->setEditable( !d->hasDefaultFilter || mode != Saving );
01757     d->okButton->setGuiItem( (mode == Saving) ? KStdGuiItem::save() : KStdGuiItem::ok() );
01758 }
01759 
01760 KFileDialog::OperationMode KFileDialog::operationMode() const
01761 {
01762     return d->operationMode;
01763 }
01764 
01765 // adds the selected files/urls to 'recent documents'
01766 void KFileDialog::addToRecentDocuments()
01767 {
01768     int m = ops->mode();
01769 
01770     if ( m & KFile::LocalOnly ) {
01771         QStringList files = selectedFiles();
01772         QStringList::ConstIterator it = files.begin();
01773         for ( ; it != files.end(); ++it )
01774             KRecentDocument::add( *it );
01775     }
01776 
01777     else { // urls
01778         KURL::List urls = selectedURLs();
01779         KURL::List::ConstIterator it = urls.begin();
01780         for ( ; it != urls.end(); ++it ) {
01781             if ( (*it).isValid() )
01782                 KRecentDocument::add( *it );
01783         }
01784     }
01785 }
01786 
01787 KActionCollection * KFileDialog::actionCollection() const
01788 {
01789     return ops->actionCollection();
01790 }
01791 
01792 void KFileDialog::toggleSpeedbar( bool show )
01793 {
01794     if ( show )
01795     {
01796         if ( !d->urlBar )
01797             initSpeedbar();
01798 
01799         d->urlBar->show();
01800 
01801         // check to see if they have a home item defined, if not show the home button
01802         KURLBarItem *urlItem = static_cast<KURLBarItem*>( d->urlBar->listBox()->firstItem() );
01803         KURL homeURL;
01804         homeURL.setPath( QDir::homeDirPath() );
01805         while ( urlItem )
01806         {
01807             if ( homeURL.equals( urlItem->url(), true ) )
01808             {
01809                 ops->actionCollection()->action( "home" )->unplug( toolbar );
01810                 break;
01811             }
01812 
01813             urlItem = static_cast<KURLBarItem*>( urlItem->next() );
01814         }
01815     }
01816     else
01817     {
01818         if (d->urlBar)
01819             d->urlBar->hide();
01820 
01821         if ( !ops->actionCollection()->action( "home" )->isPlugged( toolbar ) )
01822             ops->actionCollection()->action( "home" )->plug( toolbar, 3 );
01823     }
01824 
01825     static_cast<KToggleAction *>(actionCollection()->action("toggleSpeedbar"))->setChecked( show );
01826 }
01827 
01828 int KFileDialog::pathComboIndex()
01829 {
01830     return d->m_pathComboIndex;
01831 }
01832 
01833 // static
01834 void KFileDialog::initStatic()
01835 {
01836     if ( lastDirectory )
01837         return;
01838 
01839     lastDirectory = ldd.setObject(new KURL());
01840 }
01841 
01842 // static
01843 KURL KFileDialog::getStartURL( const QString& startDir,
01844                                QString& recentDirClass )
01845 {
01846     initStatic();
01847     
01848     recentDirClass = QString::null;
01849     KURL ret;
01850 
01851     bool useDefaultStartDir = startDir.isEmpty();
01852     if ( !useDefaultStartDir )
01853     {
01854         if (startDir[0] == ':')
01855         {
01856             recentDirClass = startDir;
01857             ret = KURL::fromPathOrURL( KRecentDirs::dir(recentDirClass) );
01858         }
01859         else
01860         {
01861             ret = KCmdLineArgs::makeURL( QFile::encodeName(startDir) );
01862             // If we won't be able to list it (e.g. http), then use default
01863             if ( !KProtocolInfo::supportsListing( ret.protocol() ) )
01864                 useDefaultStartDir = true;
01865         }
01866     }
01867 
01868     if ( useDefaultStartDir )
01869     {
01870         if (lastDirectory->isEmpty()) {
01871             *lastDirectory = KGlobalSettings::documentPath();
01872             KURL home;
01873             home.setPath( QDir::homeDirPath() );
01874             // if there is no docpath set (== home dir), we prefer the current
01875             // directory over it. We also prefer the homedir when our CWD is
01876             // different from our homedirectory
01877             if ( lastDirectory->path(+1) == home.path(+1) ||
01878                  QDir::currentDirPath() != QDir::homeDirPath() )
01879                 *lastDirectory = QDir::currentDirPath();
01880         }
01881         ret = *lastDirectory;
01882     }
01883 
01884     return ret;
01885 }
01886 
01887 void KFileDialog::virtual_hook( int id, void* data )
01888 { KDialogBase::virtual_hook( id, data ); }
01889 
01890 #include "kfiledialog.moc"
01891 #include "kpreviewwidgetbase.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:29 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001