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                 selectedURL.cleanPath (); // fix "dir/../"
00399             }
00400         } else // complete URL
00401             selectedURL = text;
00402     }
00403 
00404     if ( selectedURL.isMalformed() ) {
00405        KMessageBox::sorry( d->mainWidget, i18n("%1\ndoes not appear to be a valid URL.\n").arg(d->url.url()), i18n("Invalid URL") );
00406        return;
00407     }
00408 
00409     if ( (mode() & KFile::LocalOnly) == KFile::LocalOnly &&
00410          !selectedURL.isLocalFile() ) {
00411         KMessageBox::sorry( d->mainWidget,
00412                             i18n("You can only select local files."),
00413                             i18n("Remote Files not Accepted") );
00414         return;
00415     }
00416 
00417     d->url = selectedURL;
00418 
00419     // d->url is a correct URL now
00420 
00421     if ( (mode() & KFile::Directory) == KFile::Directory ) {
00422         kdDebug(kfile_area) << "Directory" << endl;
00423         bool done = true;
00424         if ( d->url.isLocalFile() ) {
00425             if ( locationEdit->currentText().stripWhiteSpace().isEmpty() ) {
00426                 QFileInfo info( d->url.path() );
00427                 if ( info.isDir() ) {
00428                     d->filenames = QString::null;
00429                     d->urlList.clear();
00430                     d->urlList.append( d->url );
00431                     accept();
00432                 }
00433                 else if (!info.exists() && (mode() & KFile::File) != KFile::File) {
00434                     // directory doesn't exist, create and enter it
00435                     if ( ops->mkdir( d->url.url(), true ))
00436                         return;
00437                     else
00438                         accept();
00439                 }
00440                 else { // d->url is not a directory,
00441                     // maybe we are in File(s) | Directory mode
00442                     if ( mode() & KFile::File == KFile::File ||
00443                         mode() & KFile::Files == KFile::Files )
00444                         done = false;
00445                 }
00446             }
00447             else  // Directory mode, with file[s]/dir[s] selected
00448             {
00449                 if ( mode() & KFile::ExistingOnly )
00450                 {
00451                     if ( ops->dirOnlyMode() )
00452                     {
00453                         KURL fullURL(d->url, locationEdit->currentText());
00454                         if ( QFile::exists( fullURL.path() ) )
00455                         {
00456                             d->url = fullURL;
00457                             d->filenames = QString::null;
00458                             d->urlList.clear();
00459                             accept();
00460                             return;
00461                         }
00462                         else // doesn't exist -> reject
00463                             return;
00464                     }
00465                 }
00466 
00467                 d->filenames = locationEdit->currentText();
00468                 accept(); // what can we do?
00469             }
00470 
00471         }
00472         else { // FIXME: remote directory, should we allow that?
00473 //             qDebug( "**** Selected remote directory: %s", d->url.url().latin1());
00474             d->filenames = QString::null;
00475             d->urlList.clear();
00476             d->urlList.append( d->url );
00477 
00478             if ( mode() & KFile::ExistingOnly )
00479                 done = false;
00480             else
00481                 accept();
00482         }
00483 
00484         if ( done )
00485             return;
00486     }
00487 
00488     if (!kapp->authorizeURLAction("open", KURL(), d->url))
00489     {
00490         QString msg = KIO::buildErrorString(KIO::ERR_ACCESS_DENIED, d->url.prettyURL());
00491         KMessageBox::error( d->mainWidget, msg);
00492         return;
00493     }
00494 
00495     KIO::StatJob *job = 0L;
00496     d->statJobs.clear();
00497     d->filenames = locationEdit->currentText();
00498 
00499     if ( (mode() & KFile::Files) == KFile::Files &&
00500          !locationEdit->currentText().contains( '/' )) {
00501         kdDebug(kfile_area) << "Files\n";
00502         KURL::List list = parseSelectedURLs();
00503         for ( KURL::List::ConstIterator it = list.begin();
00504               it != list.end(); ++it ) 
00505         {
00506             if (!kapp->authorizeURLAction("open", KURL(), *it))
00507             {
00508                 QString msg = KIO::buildErrorString(KIO::ERR_ACCESS_DENIED, (*it).prettyURL());
00509                 KMessageBox::error( d->mainWidget, msg);
00510                 return;
00511             }
00512         }
00513         for ( KURL::List::ConstIterator it = list.begin();
00514               it != list.end(); ++it ) 
00515         {
00516             job = KIO::stat( *it, !(*it).isLocalFile() );
00517             KIO::Scheduler::scheduleJob( job );
00518             d->statJobs.append( job );
00519             connect( job, SIGNAL( result(KIO::Job *) ),
00520                      SLOT( slotStatResult( KIO::Job *) ));
00521         }
00522         return;
00523     }
00524 
00525     job = KIO::stat(d->url,!d->url.isLocalFile());
00526     d->statJobs.append( job );
00527     connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotStatResult(KIO::Job*)));
00528 }
00529 
00530 
00531 // FIXME : count all errors and show messagebox when d->statJobs.count() == 0
00532 // in case of an error, we cancel the whole operation (clear d->statJobs and
00533 // don't call accept)
00534 void KFileDialog::slotStatResult(KIO::Job* job)
00535 {
00536     kdDebug(kfile_area) << "slotStatResult" << endl;
00537     KIO::StatJob *sJob = static_cast<KIO::StatJob *>( job );
00538 
00539     if ( !d->statJobs.removeRef( sJob ) ) {
00540         return;
00541     }
00542 
00543     int count = d->statJobs.count();
00544 
00545     // errors mean in general, the location is no directory ;/
00546     // Can we be sure that it is exististant at all? (pfeiffer)
00547     if (sJob->error() && count == 0 && !ops->dirOnlyMode())
00548        accept();
00549 
00550     KIO::UDSEntry t = sJob->statResult();
00551     bool isDir = false;
00552     for (KIO::UDSEntry::ConstIterator it = t.begin();
00553         it != t.end(); ++it) {
00554        if ((*it).m_uds == KIO::UDS_FILE_TYPE ) {
00555            isDir = S_ISDIR( (mode_t)((*it).m_long));
00556            break;
00557        }
00558     }
00559 
00560     if (isDir)
00561     {
00562         if ( ops->dirOnlyMode() )
00563         {
00564             d->filenames = QString::null;
00565             d->urlList.clear();
00566             accept();
00567         }
00568         else // in File[s] mode, directory means error -> cd into it
00569         {
00570             if ( count == 0 ) {
00571                 locationEdit->clearEdit();
00572                 locationEdit->lineEdit()->setEdited( false );
00573                 setURL( sJob->url() );
00574             }
00575         }
00576         d->statJobs.clear();
00577         return;
00578     }
00579     else if ( ops->dirOnlyMode() && !isDir )
00580     {
00581         return; // ### error message?
00582     }
00583 
00584     kdDebug(kfile_area) << "filename " << sJob->url().url() << endl;
00585 
00586     if ( count == 0 )
00587         accept();
00588 }
00589 
00590 
00591 void KFileDialog::accept()
00592 {
00593     setResult( QDialog::Accepted ); // parseSelectedURLs() checks that
00594 
00595     *lastDirectory = ops->url();
00596     if (!d->fileClass.isEmpty())
00597        KRecentDirs::add(d->fileClass, ops->url().url());
00598 
00599     // clear the topmost item, we insert it as full path later on as item 1
00600     locationEdit->changeItem( QString::null, 0 );
00601 
00602     KURL::List list = selectedURLs();
00603     QValueListConstIterator<KURL> it = list.begin();
00604     for ( ; it != list.end(); ++it ) {
00605         const KURL& url = *it;
00606         // we strip the last slash (-1) because KURLComboBox does that as well
00607         // when operating in file-mode. If we wouldn't , dupe-finding wouldn't
00608         // work.
00609         QString file = url.isLocalFile() ? url.path(-1) : url.prettyURL(-1);
00610 
00611         // remove dupes
00612         for ( int i = 1; i < locationEdit->count(); i++ ) {
00613             if ( locationEdit->text( i ) == file ) {
00614                 locationEdit->removeItem( i-- );
00615                 break;
00616             }
00617         }
00618         locationEdit->insertItem( file, 1 );
00619     }
00620 
00621     KConfig *config = KGlobal::config();
00622     config->setForceGlobal( true );
00623     writeConfig( config, ConfigGroup );
00624     config->setForceGlobal( false );
00625 
00626     saveRecentFiles( config );
00627     config->sync();
00628 
00629     KDialogBase::accept();
00630 
00631     addToRecentDocuments();
00632 
00633     if ( (mode() & KFile::Files) != KFile::Files ) // single selection
00634         emit fileSelected(d->url.url());
00635 
00636     ops->close();
00637     emit okClicked();
00638 }
00639 
00640 
00641 void KFileDialog::fileHighlighted(const KFileItem *i)
00642 {
00643     if (i && i->isDir())
00644         return;
00645 
00646 
00647     if ( (ops->mode() & KFile::Files) != KFile::Files ) {
00648         if ( !i )
00649             return;
00650 
00651         d->url = i->url();
00652 
00653         if ( !d->completionLock ) {
00654             locationEdit->setCurrentItem( 0 );
00655             locationEdit->setEditText( i->name() );
00656             locationEdit->lineEdit()->setEdited( false );
00657         }
00658         emit fileHighlighted(d->url.url());
00659     }
00660 
00661     else {
00662         multiSelectionChanged();
00663         emit selectionChanged();
00664     }
00665 }
00666 
00667 void KFileDialog::fileSelected(const KFileItem *i)
00668 {
00669     if (i && i->isDir())
00670         return;
00671 
00672     if ( (ops->mode() & KFile::Files) != KFile::Files ) {
00673         if ( !i )
00674             return;
00675 
00676         d->url = i->url();
00677         locationEdit->setCurrentItem( 0 );
00678         locationEdit->setEditText( i->name() );
00679         locationEdit->lineEdit()->setEdited( false );
00680     }
00681     else {
00682         multiSelectionChanged();
00683         emit selectionChanged();
00684     }
00685     slotOk();
00686 }
00687 
00688 
00689 // I know it's slow to always iterate thru the whole filelist
00690 // (ops->selectedItems()), but what can we do?
00691 void KFileDialog::multiSelectionChanged()
00692 {
00693     if ( d->completionLock ) // FIXME: completion with multiselection?
00694         return;
00695 
00696     locationEdit->lineEdit()->setEdited( false );
00697     KFileItem *item;
00698     const KFileItemList *list = ops->selectedItems();
00699     if ( !list ) {
00700         locationEdit->clearEdit();
00701         return;
00702     }
00703 
00704     static const QString &begin = KGlobal::staticQString(" \"");
00705     KFileItemListIterator it ( *list );
00706     QString text;
00707     while ( (item = it.current()) ) {
00708         text.append( begin ).append( item->name() ).append( '\"' );
00709         ++it;
00710     }
00711     locationEdit->setCurrentItem( 0 );
00712     locationEdit->setEditText( text.stripWhiteSpace() );
00713 }
00714 
00715 void KFileDialog::init(const QString& startDir, const QString& filter, QWidget* widget)
00716 {
00717     initStatic();
00718     d = new KFileDialogPrivate();
00719     
00720     d->boxLayout = 0;
00721     d->keepLocation = false;
00722     d->operationMode = Opening;
00723     d->hasDefaultFilter = false;
00724     d->hasView = false;
00725     d->mainWidget = new QWidget( this, "KFileDialog::mainWidget");
00726     setMainWidget( d->mainWidget );
00727     d->okButton = new KPushButton( KStdGuiItem::ok(), d->mainWidget );
00728     d->okButton->setDefault( true );
00729     d->cancelButton = new KPushButton(KStdGuiItem::cancel(), d->mainWidget);
00730     connect( d->okButton, SIGNAL( clicked() ), SLOT( slotOk() ));
00731     connect( d->cancelButton, SIGNAL( clicked() ), SLOT( slotCancel() ));
00732     d->customWidget = widget;
00733     d->urlBar = 0; // delayed loading
00734     KConfig *config = KGlobal::config();
00735     KConfigGroupSaver cs( config, ConfigGroup );
00736     d->initializeSpeedbar = config->readBoolEntry( "Set speedbar defaults",
00737                                                    true );
00738     d->completionLock = false;
00739 
00740     QtMsgHandler oldHandler = qInstallMsgHandler( silenceQToolBar );
00741     toolbar = new KToolBar( d->mainWidget, "KFileDialog::toolbar", true);
00742     toolbar->setFlat(true);
00743     qInstallMsgHandler( oldHandler );
00744 
00745     QString autocompletionWhatsThisText = i18n("<p>While typing in the text area, you may be presented "
00746                                                "with possible matches. "
00747                                                "This feature can be controlled by clicking with the right mouse button "
00748                                                "and selecting a preferred mode from the <b>Text Completion</b> menu.") + "</qt>";
00749     d->pathCombo = new KURLComboBox( KURLComboBox::Directories, true,
00750                                      toolbar, "path combo" );
00751     QToolTip::add( d->pathCombo, i18n("Often used directories") );
00752     QWhatsThis::add( d->pathCombo, i18n("<qt>Commonly used locations are listed here. "
00753                                         "This includes standard locations, such as your home directory, as well as "
00754                                         "locations that have been visited recently.") + autocompletionWhatsThisText);
00755 
00756     KURL u;
00757     u.setPath( QDir::rootDirPath() );
00758     QString text = i18n("Root Directory: %1").arg( u.path() );
00759     d->pathCombo->addDefaultURL( u,
00760                                  KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00761                                  text );
00762 
00763     u.setPath( QDir::homeDirPath() );
00764     text = i18n("Home Directory: %1").arg( u.path( +1 ) );
00765     d->pathCombo->addDefaultURL( u, KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00766                                  text );
00767 
00768     KURL docPath;
00769     docPath.setPath( KGlobalSettings::documentPath() );
00770     if ( u.path(+1) != docPath.path(+1) ) {
00771         text = i18n("Documents: %1").arg( docPath.path( +1 ) );
00772         d->pathCombo->addDefaultURL( docPath,
00773                                      KMimeType::pixmapForURL( docPath, 0, KIcon::Small ),
00774                                      text );
00775     }
00776 
00777     u.setPath( KGlobalSettings::desktopPath() );
00778     text = i18n("Desktop: %1").arg( u.path( +1 ) );
00779     d->pathCombo->addDefaultURL( u,
00780                                  KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00781                                  text );
00782 
00783     u.setPath( "/tmp" );
00784 
00785     d->url = getStartURL( startDir, d->fileClass );
00786     d->selection = d->url.url();
00787 
00788     // If local, check it exists. If not, go up until it exists.
00789     if ( d->url.isLocalFile() )
00790     {
00791         if ( !QFile::exists( d->url.path() ) )
00792         {
00793             d->url = d->url.upURL();
00794             QDir dir( d->url.path() );
00795             while ( !dir.exists() )
00796             {
00797                 d->url = d->url.upURL();
00798                 dir.setPath( d->url.path() );
00799             }
00800         }
00801     }
00802 
00803     ops = new KDirOperator(d->url, d->mainWidget, "KFileDialog::ops");
00804     ops->setOnlyDoubleClickSelectsFiles( true );
00805     connect(ops, SIGNAL(urlEntered(const KURL&)),
00806             SLOT(urlEntered(const KURL&)));
00807     connect(ops, SIGNAL(fileHighlighted(const KFileItem *)),
00808             SLOT(fileHighlighted(const KFileItem *)));
00809     connect(ops, SIGNAL(fileSelected(const KFileItem *)),
00810             SLOT(fileSelected(const KFileItem *)));
00811     connect(ops, SIGNAL(finishedLoading()),
00812             SLOT(slotLoadingFinished()));
00813 
00814     ops->setupMenu(KDirOperator::SortActions |
00815                    KDirOperator::FileActions |
00816                    KDirOperator::ViewActions);
00817     KActionCollection *coll = ops->actionCollection();
00818 
00819     // plug nav items into the toolbar
00820     coll->action( "up" )->plug( toolbar );
00821     coll->action( "up" )->setWhatsThis(i18n("<qt>Click this button to enter the parent directory.<p>"
00822                                             "For instance, if the current location is file:/home/%1 clicking this "
00823                                             "button will take you to file:/home.</qt>").arg(getlogin()));
00824     coll->action( "back" )->plug( toolbar );
00825     coll->action( "back" )->setWhatsThis(i18n("Click this button to move backwards one step in the browsing history."));
00826     coll->action( "forward" )->plug( toolbar );
00827     coll->action( "forward" )->setWhatsThis(i18n("Click this button to move forward one step in the browsing history."));
00828     coll->action( "reload" )->plug( toolbar );
00829     coll->action( "reload" )->setWhatsThis(i18n("Click this button to reload the contents of the current location."));
00830     coll->action( "mkdir" )->setShortcut(Key_F10);
00831     coll->action( "mkdir" )->plug( toolbar );
00832     coll->action( "mkdir" )->setWhatsThis(i18n("Click this button to create a new directory."));
00833 
00834     d->bookmarkHandler = new KFileBookmarkHandler( this );
00835     toolbar->insertButton(QString::fromLatin1("bookmark"),
00836                           (int)HOTLIST_BUTTON, true,
00837                           i18n("Bookmarks"));
00838     toolbar->getButton(HOTLIST_BUTTON)->setPopup( d->bookmarkHandler->menu(),
00839                                                   true);
00840     QWhatsThis::add(toolbar->getButton(HOTLIST_BUTTON),
00841                     i18n("<qt>This button allows you to bookmark specific locations. "
00842                          "Click on this button to open the bookmark menu where you may add, "
00843                          "edit or select a bookmark.<p>"
00844                          "These bookmarks are specific to the file dialog, but otherwise operate "
00845                          "like bookmarks elsewhere in KDE.</qt>"));
00846     connect( d->bookmarkHandler, SIGNAL( openURL( const QString& )),
00847              SLOT( enterURL( const QString& )));
00848 
00849     KToggleAction *showSidebarAction =
00850         new KToggleAction(i18n("Show Quick Access Navigation Panel"), Key_F9, coll,"toggleSpeedbar");
00851     connect( showSidebarAction, SIGNAL( toggled( bool ) ),
00852              SLOT( toggleSpeedbar( bool )) );
00853 
00854     KActionMenu *menu = new KActionMenu( i18n("Configure"), "configure", this, "extra menu" );
00855     menu->setWhatsThis(i18n("<qt>This is the configuration menu for the file dialog. "
00856                             "Various options can be accessed from this menu including: <ul>"
00857                             "<li>how files are sorted in the list</li>"
00858                             "<li>types of view, including icon and list</li>"
00859                             "<li>showing of hidden files</li>"
00860                             "<li>the Quick Access navigation panel</li>"
00861                             "<li>file previews</li>"
00862                             "<li>separating directories from files</li></ul></qt>"));
00863     menu->insert( coll->action( "sorting menu" ));
00864     menu->insert( coll->action( "separator" ));
00865     coll->action( "short view" )->setShortcut(Key_F6);
00866     menu->insert( coll->action( "short view" ));
00867     coll->action( "detailed view" )->setShortcut(Key_F7);
00868     menu->insert( coll->action( "detailed view" ));
00869     menu->insert( coll->action( "separator" ));
00870     coll->action( "show hidden" )->setShortcut(Key_F8);
00871     menu->insert( coll->action( "show hidden" ));
00872     menu->insert( showSidebarAction );
00873     coll->action( "preview" )->setShortcut(Key_F11);
00874     menu->insert( coll->action( "preview" ));
00875     coll->action( "separate dirs" )->setShortcut(Key_F12);
00876     menu->insert( coll->action( "separate dirs" ));
00877 
00878     menu->setDelayed( false );
00879     connect( menu->popupMenu(), SIGNAL( aboutToShow() ),
00880              ops, SLOT( updateSelectionDependentActions() ));
00881     menu->plug( toolbar );
00882 
00883     /*
00884      * ugly little hack to have a 5 pixel space between the buttons
00885      * and the combo box
00886      */
00887     QWidget *spacerWidget = new QWidget(toolbar);
00888     spacerWidget->setMinimumWidth(spacingHint());
00889     spacerWidget->setMaximumWidth(spacingHint());
00890     d->m_pathComboIndex = toolbar->insertWidget(-1, -1, spacerWidget);
00891     toolbar->insertWidget(PATH_COMBO, 0, d->pathCombo);
00892 
00893 
00894     toolbar->setItemAutoSized (PATH_COMBO);
00895     toolbar->setIconText(KToolBar::IconOnly);
00896     toolbar->setBarPos(KToolBar::Top);
00897     toolbar->setMovingEnabled(false);
00898     toolbar->adjustSize();
00899 
00900     d->pathCombo->setCompletionObject( ops->dirCompletionObject(), false );
00901 
00902     connect( d->pathCombo, SIGNAL( urlActivated( const KURL&  )),
00903              this,  SLOT( enterURL( const KURL& ) ));
00904     connect( d->pathCombo, SIGNAL( returnPressed( const QString&  )),
00905              this,  SLOT( enterURL( const QString& ) ));
00906     connect( d->pathCombo, SIGNAL(textChanged( const QString& )),
00907              SLOT( pathComboChanged( const QString& ) ));
00908     connect( d->pathCombo, SIGNAL( completion( const QString& )),
00909              SLOT( dirCompletion( const QString& )));
00910     connect( d->pathCombo, SIGNAL( textRotation(KCompletionBase::KeyBindingType) ),
00911              d->pathCombo, SLOT( rotateText(KCompletionBase::KeyBindingType) ));
00912 
00913     QString whatsThisText;
00914     if (d->operationMode == KFileDialog::Saving)
00915     {
00916         whatsThisText = i18n("<qt>This is the name to save the file as.") +
00917                         autocompletionWhatsThisText;
00918     }
00919     else if (ops->mode() & KFile::Files)
00920     {
00921         whatsThisText = i18n("<qt>This is the list of files to open. More than "
00922                              "one file can be specified by listing several "
00923                              "files, separated by spaces.") +
00924                         autocompletionWhatsThisText;
00925     }
00926     else
00927     {
00928         whatsThisText = i18n("<qt>This is the name of the file to open.") +
00929                         autocompletionWhatsThisText;
00930     }
00931 
00932     // the Location label/edit
00933     d->locationLabel = new QLabel(i18n("&Location:"), d->mainWidget);
00934     QWhatsThis::add(d->locationLabel, whatsThisText);
00935     locationEdit = new KURLComboBox(KURLComboBox::Files, true,
00936                                     d->mainWidget, "LocationEdit");
00937     d->locationLabel->setBuddy(locationEdit);
00938     QWhatsThis::add(locationEdit, whatsThisText);
00939     // to get the completionbox-signals connected:
00940     locationEdit->setHandleSignals( true );
00941     (void) locationEdit->completionBox();
00942 
00943     locationEdit->setFocus();
00944 //     locationEdit->setCompletionObject( new KURLCompletion() );
00945 //     locationEdit->setAutoDeleteCompletionObject( true );
00946     locationEdit->setCompletionObject( ops->completionObject(), false );
00947 
00948     connect( locationEdit, SIGNAL( returnPressed() ),
00949              this, SLOT( slotOk()));
00950     connect(locationEdit, SIGNAL( activated( const QString&  )),
00951             this,  SLOT( locationActivated( const QString& ) ));
00952     connect( locationEdit, SIGNAL( completion( const QString& )),
00953              SLOT( fileCompletion( const QString& )));
00954     connect( locationEdit, SIGNAL( textRotation(KCompletionBase::KeyBindingType) ),
00955              locationEdit, SLOT( rotateText(KCompletionBase::KeyBindingType) ));
00956 
00957     // the Filter label/edit
00958     whatsThisText = i18n("<qt>This is the filter to apply to the file list. "
00959                          "File names that do not match the filter will not be shown.<p>"
00960                          "You may select from one of the preset filters in the "
00961                          "drop down menu, or you may enter a custom filter "
00962                          "directly into the text area.<p>"
00963                          "Wildcards such as * and ? are allowed.</qt>");
00964     d->filterLabel = new QLabel(i18n("&Filter:"), d->mainWidget);
00965     QWhatsThis::add(d->filterLabel, whatsThisText);
00966     filterWidget = new KFileFilterCombo(d->mainWidget,
00967                                         "KFileDialog::filterwidget");
00968     QWhatsThis::add(filterWidget, whatsThisText);
00969     setFilter(filter);
00970     d->filterLabel->setBuddy(filterWidget);
00971     connect(filterWidget, SIGNAL(filterChanged()), SLOT(slotFilterChanged()));
00972 
00973     initGUI(); // activate GM
00974 
00975     readRecentFiles( config );
00976 
00977     adjustSize();
00978 
00979     // we set the completionLock to avoid entering pathComboChanged() when
00980     // inserting the list of URLs into the combo.
00981     d->completionLock = true;
00982     ops->setViewConfig( config, ConfigGroup );
00983     readConfig( config, ConfigGroup );
00984     setSelection(d->selection);
00985     d->completionLock = false;
00986 }
00987 
00988 void KFileDialog::initSpeedbar()
00989 {
00990     d->urlBar = new KFileSpeedBar( d->mainWidget, "url bar" );
00991     connect( d->urlBar, SIGNAL( activated( const KURL& )),
00992              SLOT( enterURL( const KURL& )) );
00993 
00994     // need to set the current url of the urlbar manually (not via urlEntered()
00995     // here, because the initial url of KDirOperator might be the same as the
00996     // one that will be set later (and then urlEntered() won't be emitted).
00997     // ### REMOVE THIS when KDirOperator's initial URL (in the c'tor) is gone.
00998         d->urlBar->setCurrentItem( d->url );
00999 
01000     d->urlBarLayout->insertWidget( 0, d->urlBar );
01001 }
01002 
01003 void KFileDialog::initGUI()
01004 {
01005     delete d->boxLayout; // deletes all sub layouts
01006 
01007     d->boxLayout = new QVBoxLayout( d->mainWidget, 0, KDialog::spacingHint());
01008     d->boxLayout->addWidget(toolbar, AlignTop);
01009 
01010     d->urlBarLayout = new QHBoxLayout( d->boxLayout ); // needed for the urlBar that may appear
01011     QVBoxLayout *vbox = new QVBoxLayout( d->urlBarLayout );
01012 
01013     vbox->addWidget(ops, 4);
01014     vbox->addSpacing(3);
01015 
01016     QGridLayout* lafBox= new QGridLayout(2, 3, KDialog::spacingHint());
01017     vbox->addLayout(lafBox, 0);
01018     lafBox->addWidget(d->locationLabel, 0, 0, AlignVCenter);
01019     lafBox->addWidget(locationEdit, 0, 1, AlignVCenter);
01020     lafBox->addWidget(d->okButton, 0, 2, AlignVCenter);
01021 
01022     lafBox->addWidget(d->filterLabel, 1, 0, AlignVCenter);
01023     lafBox->addWidget(filterWidget, 1, 1, AlignVCenter);
01024     lafBox->addWidget(d->cancelButton, 1, 2, AlignVCenter);
01025 
01026     lafBox->setColStretch(1, 4);
01027 
01028     vbox->addSpacing(3);
01029 
01030     setTabOrder(ops,  locationEdit);
01031     setTabOrder(locationEdit, filterWidget);
01032     setTabOrder(filterWidget, d->okButton);
01033     setTabOrder(d->okButton, d->cancelButton);
01034     setTabOrder(d->cancelButton, d->pathCombo);
01035     setTabOrder(d->pathCombo, ops);
01036 
01037     // If a custom widget was specified...
01038     if ( d->customWidget != 0 )
01039     {
01040         // ...add it to the dialog, below the filter list box.
01041 
01042         // Change the parent so that this widget is a child of the main widget
01043         d->customWidget->reparent( d->mainWidget, QPoint() );
01044 
01045         vbox->addWidget( d->customWidget );
01046 
01047         // FIXME: This should adjust the tab orders so that the custom widget
01048         // comes after the Cancel button. The code appears to do this, but the result
01049         // somehow screws up the tab order of the file path combo box. Not a major
01050         // problem, but ideally the tab order with a custom widget should be
01051         // the same as the order without one.
01052         setTabOrder(d->cancelButton, d->customWidget);
01053         setTabOrder(d->customWidget, d->pathCombo);
01054     }
01055     else
01056     {
01057         setTabOrder(d->cancelButton, d->pathCombo);
01058     }
01059 
01060     setTabOrder(d->pathCombo, ops);
01061 }
01062 
01063 void KFileDialog::slotFilterChanged()
01064 {
01065     QString filter = filterWidget->currentFilter();
01066     ops->clearFilter();
01067 
01068     if ( filter.find( '/' ) > -1 ) {
01069         QStringList types = QStringList::split( " ", filter );
01070         types.prepend( "inode/directory" );
01071         ops->setMimeFilter( types );
01072     }
01073     else
01074         ops->setNameFilter( filter );
01075 
01076     ops->updateDir();
01077     emit filterChanged( filter );
01078 }
01079 
01080 
01081 void KFileDialog::pathComboChanged( const QString& txt )
01082 {
01083     if ( d->completionLock )
01084         return;
01085 
01086     static const QString& localRoot = KGlobal::staticQString("file:/");
01087     KURLComboBox *combo = d->pathCombo;
01088     QString text = txt;
01089     QString newText = text.left(combo->cursorPosition() -1);
01090     KURL url;
01091     if ( text.at( 0 ) == '/' )
01092         url.setPath( text );
01093     else
01094         url = text;
01095 
01096 
01097     // don't mess with malformed urls or remote urls without directory or host
01098     if ( url.isMalformed() ||
01099          !KProtocolInfo::supportsListing( url.protocol() ) ||
01100          ( !url.url().startsWith( localRoot ) &&
01101            ( url.directory().isNull() || url.host().isNull()) )) {
01102         d->completionHack = newText;
01103         return;
01104     }
01105 
01106     // when editing somewhere in the middle of the text, don't complete or
01107     // follow directories
01108     if ( combo->cursorPosition() != (int) combo->currentText().length() ) {
01109         d->completionHack = newText;
01110         return;
01111     }
01112 
01113     // the user is backspacing -> don't annoy him with completions
01114     if ( autoDirectoryFollowing && d->completionHack.startsWith( newText ) ) {
01115         // but we can follow the directories, if configured so
01116 
01117         // find out the current directory according to combobox and cd into
01118         int l = text.length() - 1;
01119         while (!text.isEmpty() && text[l] != '/')
01120             l--;
01121 
01122         KURL newLocation(text.left(l+1));
01123 
01124         if ( !newLocation.isMalformed() && newLocation != ops->url() ) {
01125             setURL(newLocation, true);
01126             combo->setEditText(text);
01127         }
01128     }
01129 
01130     // typing forward, ending with a / -> cd into the directory
01131     else if ( autoDirectoryFollowing &&
01132               text.at(text.length()-1) == '/' && ops->url() != text ) {
01133         d->selection = QString::null;
01134         setURL( text, false );
01135     }
01136 
01137     d->completionHack = newText;
01138 }
01139 
01140 
01141 void KFileDialog::setURL(const KURL& url, bool clearforward)
01142 {
01143     d->selection = QString::null;
01144     ops->setURL( url, clearforward);
01145 }
01146 
01147 // Protected
01148 void KFileDialog::urlEntered(const KURL& url)
01149 {
01150     QString filename = locationEdit->currentText();
01151     d->selection = QString::null;
01152 
01153     if ( d->pathCombo->count() != 0 ) { // little hack
01154         d->pathCombo->setURL( url );
01155     }
01156 
01157     locationEdit->blockSignals( true );
01158     locationEdit->setCurrentItem( 0 );
01159     if ( d->keepLocation )
01160         locationEdit->setEditText( filename );
01161 
01162     locationEdit->blockSignals( false );
01163     d->completionHack = d->pathCombo->currentText();
01164 
01165     if ( d->urlBar )
01166         d->urlBar->setCurrentItem( url );
01167 }
01168 
01169 void KFileDialog::locationActivated( const QString& url )
01170 {
01171     // This guard prevents any URL _typed_ by the user from being interpreted
01172     // twice (by returnPressed/slotOk and here, activated/locationActivated)
01173     // after the user presses Enter.  Without this, _both_ setSelection and
01174     // slotOk would "u.addPath( url )" ...so instead we leave it up to just
01175     // slotOk....
01176     if (!locationEdit->lineEdit()->edited())
01177     setSelection( url );
01178 }
01179 
01180 void KFileDialog::enterURL( const KURL& url)
01181 {
01182     setURL( url );
01183 }
01184 
01185 void KFileDialog::enterURL( const QString& url )
01186 {
01187     setURL( url );
01188 }
01189 
01190 void KFileDialog::toolbarCallback(int) // SLOT
01191 {
01192     /*
01193      * yes, nothing uses this anymore.
01194      * it used to be used to show the configure dialog
01195      */
01196 }
01197 
01198 
01199 void KFileDialog::setSelection(const QString& url)
01200 {
01201     kdDebug(kfile_area) << "setSelection " << url << endl;
01202 
01203     if (url.isEmpty()) {
01204         d->selection = QString::null;
01205         return;
01206     }
01207 
01208     // This code is the same as the one in slotOK
01209     KURL u;
01210     if ( KURL::isRelativeURL(url) )
01211     {
01212         if (!url.isEmpty() && url[0] == '/' ) // absolute path
01213             u.setPath( url );
01214         else
01215         {
01216             u = ops->url();
01217             u.addPath( url ); // works for filenames and relative paths
01218         }
01219     } else // complete URL
01220         u = url;
01221 
01222     if (u.isMalformed()) { // if it still is
01223         kdWarning() << url << " is not a correct argument for setSelection!" << endl;
01224         return;
01225     }
01226 
01227     /* we strip the first / from the path to avoid file://usr which means
01228      *  / on host usr
01229      */
01230     KFileItem i(KFileItem::Unknown, KFileItem::Unknown, u, true );
01231     //    KFileItem i(u.path());
01232     if ( i.isDir() && u.isLocalFile() && QFile::exists( u.path() ) ) {
01233         // trust isDir() only if the file is
01234         // local (we cannot stat non-local urls) and if it exists!
01235         // (as KFileItem does not check if the file exists or not
01236         // -> the statbuffer is undefined -> isDir() is unreliable) (Simon)
01237         setURL(u, true);
01238     }
01239     else {
01240         QString filename = u.url();
01241         int sep = filename.findRev('/');
01242         if (sep >= 0) { // there is a / in it
01243             if ( KProtocolInfo::supportsListing( u.protocol() ))
01244                 setURL(filename.left(sep), true);
01245 
01246             // filename must be decoded, or "name with space" would become
01247             // "name%20with%20space", so we use KURL::fileName()
01248             filename = u.fileName();
01249             kdDebug(kfile_area) << "filename " << filename << endl;
01250             d->selection = filename;
01251             locationEdit->setCurrentItem( 0 );
01252             locationEdit->setEditText( filename );
01253 
01254             // tell the line edit that it has been edited
01255             // otherwise we won't know this was set by the user
01256             // and it will be ignored if there has been an
01257             // auto completion. this caused bugs where automcompletion
01258             // would start, the user would pick something from the
01259             // history and then hit Ok only to get the autocompleted
01260             // selection. OOOPS.
01261             locationEdit->lineEdit()->setEdited( true );
01262         }
01263 
01264         d->url = ops->url();
01265         d->url.addPath(filename);
01266     }
01267 }
01268 
01269 void KFileDialog::slotLoadingFinished()
01270 {
01271     if ( !d->selection.isNull() )
01272         ops->setCurrentItem( d->selection );
01273 }
01274 
01275 
01276 void KFileDialog::dirCompletion( const QString& dir ) // SLOT
01277 {
01278     // we don't support popup completion here, sorry
01279     if ( ops->dirCompletionObject()->completionMode() ==
01280          KGlobalSettings::CompletionPopup )
01281         return;
01282 
01283     QString base = ops->url().url();
01284 
01285     // if someone uses completion, he doesn't like the current selection
01286     d->selection = QString::null;
01287 
01288     KURL url;
01289     if ( dir.at( 0 ) == '/' )
01290         url.setPath( dir );
01291     else
01292         url = dir;
01293 
01294     if ( url.isMalformed() )
01295         return; // invalid entry in path combo
01296 
01297     d->completionLock = true;
01298 
01299     if (url.url().startsWith( base )) {
01300         QString complete = ops->makeDirCompletion( url.fileName(false) );
01301 
01302         if (!complete.isNull()) {
01303             if(!base.endsWith("/"))
01304                 base.append('/');
01305             QString newText = base + complete;
01306             QString fileProt = QString::fromLatin1( "file:" );
01307 
01308             if ( dir.startsWith( fileProt ) != newText.startsWith( fileProt ))
01309                 newText = newText.mid( 5 ); // remove file:
01310 
01311             d->pathCombo->setCompletedText( newText );
01312             d->url = newText;
01313         }
01314     }
01315     d->completionLock = false;
01316 }
01317 
01318 
01319 void KFileDialog::fileCompletion( const QString& file )
01320 {
01321     d->completionLock = true;
01322     QString text = ops->makeCompletion( file );
01323     if ( !text.isEmpty() ) {
01324         KCompletion *comp = ops->completionObject();
01325         if ( comp->completionMode() == KGlobalSettings::CompletionPopup ||
01326             comp->completionMode() == KGlobalSettings::CompletionPopupAuto )
01327             locationEdit->setCompletedItems( comp->allMatches() );
01328         else
01329             locationEdit->setCompletedText( text );
01330     }
01331     else
01332         if (locationEdit->completionMode() == KGlobalSettings::CompletionPopup ||
01333             locationEdit->completionMode() == KGlobalSettings::CompletionPopupAuto )
01334             locationEdit->completionBox()->hide();
01335 
01336     d->completionLock = false;
01337 }
01338 
01339 void KFileDialog::updateStatusLine(int /* dirs */, int /* files */)
01340 {
01341     kdWarning() << "KFileDialog::updateStatusLine is deprecated! The status line no longer exists. Do not try and use it!" << endl;
01342 }
01343 
01344 QString KFileDialog::getOpenFileName(const QString& startDir,
01345                                      const QString& filter,
01346                                      QWidget *parent, const QString& caption)
01347 {
01348     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01349     dlg.setOperationMode( Opening );
01350 
01351     dlg.setMode( KFile::File | KFile::LocalOnly );
01352     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01353 
01354     dlg.ops->clearHistory();
01355     dlg.exec();
01356 
01357     return dlg.selectedFile();
01358 }
01359 
01360 QStringList KFileDialog::getOpenFileNames(const QString& startDir,
01361                                           const QString& filter,
01362                                           QWidget *parent,
01363                                           const QString& caption)
01364 {
01365     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01366     dlg.setOperationMode( Opening );
01367 
01368     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01369     dlg.setMode(KFile::Files | KFile::LocalOnly);
01370     dlg.ops->clearHistory();
01371     dlg.exec();
01372 
01373     return dlg.selectedFiles();
01374 }
01375 
01376 KURL KFileDialog::getOpenURL(const QString& startDir, const QString& filter,
01377                                 QWidget *parent, const QString& caption)
01378 {
01379     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01380     dlg.setOperationMode( Opening );
01381 
01382     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01383     dlg.setMode( KFile::File );
01384     dlg.ops->clearHistory();
01385     dlg.exec();
01386 
01387     return dlg.selectedURL();
01388 }
01389 
01390 KURL::List KFileDialog::getOpenURLs(const QString& startDir,
01391                                           const QString& filter,
01392                                           QWidget *parent,
01393                                           const QString& caption)
01394 {
01395     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01396     dlg.setOperationMode( Opening );
01397 
01398     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01399     dlg.setMode(KFile::Files);
01400     dlg.ops->clearHistory();
01401     dlg.exec();
01402 
01403     return dlg.selectedURLs();
01404 }
01405 
01406 KURL KFileDialog::getExistingURL(const QString& startDir,
01407                                        QWidget *parent,
01408                                        const QString& caption)
01409 {
01410     KFileDialog dlg(startDir, QString::null, parent, "filedialog", true);
01411     dlg.setMode(KFile::Directory | KFile::ExistingOnly);
01412     // to get "All Directories" instead of "All Files" in the combo
01413     dlg.setFilter( QString::null );
01414     dlg.ops->clearHistory();
01415     dlg.setCaption(caption.isNull() ? i18n("Select Directory") : caption);
01416     dlg.exec();
01417 
01418     return dlg.selectedURL();
01419 }
01420 
01421 QString KFileDialog::getExistingDirectory(const QString& startDir,
01422                                           QWidget *parent,
01423                                           const QString& caption)
01424 {
01425     KFileDialog dlg(startDir, QString::null, parent, "filedialog", true);
01426     dlg.setMode(KFile::Directory | KFile::LocalOnly | KFile::ExistingOnly);
01427     // to get "All Directories" instead of "All Files" in the combo
01428     dlg.setFilter( QString::null );
01429     dlg.ops->clearHistory();
01430     dlg.setCaption(caption.isNull() ? i18n("Select Directory") : caption);
01431     dlg.exec();
01432 
01433     return dlg.selectedFile();
01434 }
01435 
01436 KURL KFileDialog::getImageOpenURL( const QString& startDir, QWidget *parent,
01437                                    const QString& caption)
01438 {
01439     QStringList mimetypes = KImageIO::mimeTypes( KImageIO::Reading );
01440     KFileDialog dlg(startDir,
01441                     mimetypes.join(" "),
01442                     parent, "filedialog", true);
01443     dlg.setOperationMode( Opening );
01444     dlg.setCaption( caption.isNull() ? i18n("Open") : caption );
01445     dlg.setMode( KFile::File );
01446 
01447     KImageFilePreview *ip = new KImageFilePreview( &dlg );
01448     dlg.setPreviewWidget( ip );
01449     dlg.exec();
01450 
01451     return dlg.selectedURL();
01452 }
01453 
01454 KURL KFileDialog::selectedURL() const
01455 {
01456     if ( result() == QDialog::Accepted )
01457         return d->url;
01458     else
01459         return KURL();
01460 }
01461 
01462 KURL::List KFileDialog::selectedURLs() const
01463 {
01464     KURL::List list;
01465     if ( result() == QDialog::Accepted ) {
01466         if ( (ops->mode() & KFile::Files) == KFile::Files )
01467             list = parseSelectedURLs();
01468         else
01469             list.append( d->url );
01470     }
01471     return list;
01472 }
01473 
01474 
01475 KURL::List& KFileDialog::parseSelectedURLs() const
01476 {
01477     if ( d->filenames.isEmpty() ) {
01478         return d->urlList;
01479     }
01480 
01481     d->urlList.clear();
01482     if ( d->filenames.contains( '/' )) { // assume _one_ absolute filename
01483         static const QString &prot = KGlobal::staticQString(":/");
01484         KURL u;
01485         if ( d->filenames.find( prot ) != -1 )
01486             u = d->filenames;
01487         else
01488             u.setPath( d->filenames );
01489 
01490         if ( !u.isMalformed() )
01491             d->urlList.append( u );
01492         else
01493             KMessageBox::error( d->mainWidget,
01494                                 i18n("The chosen filename(s) don't\nappear to be valid."), i18n("Invalid Filename(s)") );
01495     }
01496 
01497     else
01498         d->urlList = tokenize( d->filenames );
01499 
01500     d->filenames = QString::null; // indicate that we parsed that one
01501 
01502     return d->urlList;
01503 }
01504 
01505 
01506 // FIXME: current implementation drawback: a filename can't contain quotes
01507 KURL::List KFileDialog::tokenize( const QString& line ) const
01508 {
01509     KURL::List urls;
01510     KURL u( ops->url() );
01511     QString name;
01512 
01513     int count = line.contains( '"' );
01514     if ( count == 0 ) { // no " " -> assume one single file
01515         u.setFileName( line );
01516         if ( !u.isMalformed() )
01517             urls.append( u );
01518 
01519         return urls;
01520     }
01521 
01522     if ( (count % 2) == 1 ) { // odd number of " -> error
01523         QWidget *that = const_cast<KFileDialog *>(this);
01524         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"));
01525         return urls;
01526     }
01527 
01528     int start = 0;
01529     int index1 = -1, index2 = -1;
01530     while ( true ) {
01531         index1 = line.find( '"', start );
01532         index2 = line.find( '"', index1 + 1 );
01533 
01534         if ( index1 < 0 )
01535             break;
01536 
01537         // get everything between the " "
01538         name = line.mid( index1 + 1, index2 - index1 - 1 );
01539         u.setFileName( name );
01540         if ( !u.isMalformed() )
01541             urls.append( u );
01542 
01543         start = index2 + 1;
01544     }
01545     return urls;
01546 }
01547 
01548 
01549 QString KFileDialog::selectedFile() const
01550 {
01551     if ( result() == QDialog::Accepted )
01552     {
01553        if (d->url.isLocalFile())
01554            return d->url.path();
01555     }
01556     return QString::null;
01557 }
01558 
01559 QStringList KFileDialog::selectedFiles() const
01560 {
01561     QStringList list;
01562 
01563     if ( result() == QDialog::Accepted ) {
01564         if ( (ops->mode() & KFile::Files) == KFile::Files ) {
01565             KURL::List urls = parseSelectedURLs();
01566             QValueListConstIterator<KURL> it = urls.begin();
01567             while ( it != urls.end() ) {
01568                 if ( (*it).isLocalFile() )
01569                     list.append( (*it).path() );
01570                 ++it;
01571             }
01572         }
01573 
01574         else { // single-selection mode
01575             if ( d->url.isLocalFile() )
01576                 list.append( d->url.path() );
01577         }
01578     }
01579 
01580     return list;
01581 }
01582 
01583 KURL KFileDialog::baseURL() const
01584 {
01585     return ops->url();
01586 }
01587 
01588 QString KFileDialog::getSaveFileName(const QString& dir, const QString& filter,
01589                                      QWidget *parent,
01590                                      const QString& caption)
01591 {
01592     bool specialDir = dir.at(0) == ':';
01593     KFileDialog dlg( specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01594     if ( !specialDir )
01595         dlg.setSelection( dir ); // may also be a filename
01596 
01597     dlg.setOperationMode( Saving );
01598     dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01599 
01600     dlg.exec();
01601 
01602     QString filename = dlg.selectedFile();
01603     if (!filename.isEmpty())
01604         KRecentDocument::add(filename);
01605 
01606     return filename;
01607 }
01608 
01609 KURL KFileDialog::getSaveURL(const QString& dir, const QString& filter,
01610                              QWidget *parent, const QString& caption)
01611 {
01612     bool specialDir = dir.at(0) == ':';
01613     KFileDialog dlg(specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01614     if ( !specialDir )
01615     dlg.setSelection( dir ); // may also be a filename
01616 
01617     dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01618     dlg.setOperationMode( Saving );
01619 
01620     dlg.exec();
01621 
01622     KURL url = dlg.selectedURL();
01623     if (!url.isMalformed())
01624         KRecentDocument::add( url );
01625 
01626     return url;
01627 }
01628 
01629 void KFileDialog::show()
01630 {
01631     if ( !d->hasView ) { // delayed view-creation
01632         ops->setView(KFile::Default);
01633         ops->clearHistory();
01634         d->hasView = true;
01635     }
01636 
01637     KDialogBase::show();
01638 }
01639 
01640 void KFileDialog::setMode( KFile::Mode m )
01641 {
01642     ops->setMode(m);
01643     if ( ops->dirOnlyMode() ) {
01644         filterWidget->setDefaultFilter( i18n("*|All Directories") );
01645     }
01646     else {
01647         filterWidget->setDefaultFilter( i18n("*|All Files") );
01648     }
01649 }
01650 
01651 void KFileDialog::setMode( unsigned int m )
01652 {
01653     setMode(static_cast<KFile::Mode>( m ));
01654 }
01655 
01656 KFile::Mode KFileDialog::mode() const
01657 {
01658     return ops->mode();
01659 }
01660 
01661 
01662 void KFileDialog::readConfig( KConfig *kc, const QString& group )
01663 {
01664     if ( !kc )
01665         return;
01666 
01667     QString oldGroup = kc->group();
01668     if ( !group.isEmpty() )
01669         kc->setGroup( group );
01670 
01671     ops->readConfig( kc, group );
01672 
01673     KURLComboBox *combo = d->pathCombo;
01674     combo->setURLs( kc->readPathListEntry( RecentURLs ), KURLComboBox::RemoveTop );
01675     combo->setMaxItems( kc->readNumEntry( RecentURLsNumber,
01676                                           DefaultRecentURLsNumber ) );
01677     combo->setURL( ops->url() );
01678     autoDirectoryFollowing = kc->readBoolEntry( AutoDirectoryFollowing,
01679                                                 DefaultDirectoryFollowing );
01680 
01681     KGlobalSettings::Completion cm = (KGlobalSettings::Completion)
01682                                       kc->readNumEntry( PathComboCompletionMode,
01683                                       KGlobalSettings::CompletionAuto );
01684     if ( cm != KGlobalSettings::completionMode() )
01685         combo->setCompletionMode( cm );
01686 
01687     cm = (KGlobalSettings::Completion)
01688          kc->readNumEntry( LocationComboCompletionMode,
01689                            KGlobalSettings::CompletionAuto );
01690     if ( cm != KGlobalSettings::completionMode() )
01691         locationEdit->setCompletionMode( cm );
01692 
01693     // show or don't show the speedbar
01694     toggleSpeedbar( kc->readBoolEntry(ShowSpeedbar, true) );
01695 
01696     int w1 = minimumSize().width();
01697     int w2 = toolbar->sizeHint().width() + 10;
01698     if (w1 < w2)
01699         setMinimumWidth(w2);
01700 
01701     QSize size = configDialogSize( group );
01702     resize( size );
01703     kc->setGroup( oldGroup );
01704 }
01705 
01706 void KFileDialog::writeConfig( KConfig *kc, const QString& group )
01707 {
01708     if ( !kc )
01709         return;
01710 
01711     QString oldGroup = kc->group();
01712     if ( !group.isEmpty() )
01713         kc->setGroup( group );
01714 
01715     kc->writeEntry( RecentURLs, d->pathCombo->urls() );
01716     saveDialogSize( group, true );
01717     kc->writeEntry( PathComboCompletionMode, static_cast<int>(d->pathCombo->completionMode()) );
01718     kc->writeEntry(LocationComboCompletionMode, static_cast<int>(locationEdit->completionMode()) );
01719     kc->writeEntry( ShowSpeedbar, d->urlBar && !d->urlBar->isHidden() );
01720 
01721     ops->writeConfig( kc, group );
01722     kc->setGroup( oldGroup );
01723 }
01724 
01725 
01726 void KFileDialog::readRecentFiles( KConfig *kc )
01727 {
01728     QString oldGroup = kc->group();
01729     kc->setGroup( ConfigGroup );
01730 
01731     locationEdit->setMaxItems( kc->readNumEntry( RecentFilesNumber,
01732                                                  DefaultRecentURLsNumber ) );
01733     locationEdit->setURLs( kc->readPathListEntry( RecentFiles ),
01734                            KURLComboBox::RemoveBottom );
01735     locationEdit->insertItem( QString::null, 0 ); // dummy item without pixmap
01736     locationEdit->setCurrentItem( 0 );
01737 
01738     kc->setGroup( oldGroup );
01739 }
01740 
01741 void KFileDialog::saveRecentFiles( KConfig *kc )
01742 {
01743     QString oldGroup = kc->group();
01744     kc->setGroup( ConfigGroup );
01745 
01746     kc->writeEntry( RecentFiles, locationEdit->urls() );
01747 
01748     kc->setGroup( oldGroup );
01749 }
01750 
01751 KPushButton * KFileDialog::okButton() const
01752 {
01753     return d->okButton;
01754 }
01755 
01756 KPushButton * KFileDialog::cancelButton() const
01757 {
01758     return d->cancelButton;
01759 }
01760 
01761 void KFileDialog::slotCancel()
01762 {
01763     ops->close();
01764     KDialogBase::slotCancel();
01765 }
01766 
01767 void KFileDialog::setKeepLocation( bool keep )
01768 {
01769     d->keepLocation = keep;
01770 }
01771 
01772 bool KFileDialog::keepsLocation() const
01773 {
01774     return d->keepLocation;
01775 }
01776 
01777 void KFileDialog::setOperationMode( OperationMode mode )
01778 {
01779     d->operationMode = mode;
01780     d->keepLocation = (mode == Saving);
01781     filterWidget->setEditable( !d->hasDefaultFilter || mode != Saving );
01782     d->okButton->setGuiItem( (mode == Saving) ? KStdGuiItem::save() : KStdGuiItem::ok() );
01783 }
01784 
01785 KFileDialog::OperationMode KFileDialog::operationMode() const
01786 {
01787     return d->operationMode;
01788 }
01789 
01790 // adds the selected files/urls to 'recent documents'
01791 void KFileDialog::addToRecentDocuments()
01792 {
01793     int m = ops->mode();
01794 
01795     if ( m & KFile::LocalOnly ) {
01796         QStringList files = selectedFiles();
01797         QStringList::ConstIterator it = files.begin();
01798         for ( ; it != files.end(); ++it )
01799             KRecentDocument::add( *it );
01800     }
01801 
01802     else { // urls
01803         KURL::List urls = selectedURLs();
01804         KURL::List::ConstIterator it = urls.begin();
01805         for ( ; it != urls.end(); ++it ) {
01806             if ( (*it).isValid() )
01807                 KRecentDocument::add( *it );
01808         }
01809     }
01810 }
01811 
01812 KActionCollection * KFileDialog::actionCollection() const
01813 {
01814     return ops->actionCollection();
01815 }
01816 
01817 void KFileDialog::toggleSpeedbar( bool show )
01818 {
01819     if ( show )
01820     {
01821         if ( !d->urlBar )
01822             initSpeedbar();
01823 
01824         d->urlBar->show();
01825 
01826         // check to see if they have a home item defined, if not show the home button
01827         KURLBarItem *urlItem = static_cast<KURLBarItem*>( d->urlBar->listBox()->firstItem() );
01828         KURL homeURL;
01829         homeURL.setPath( QDir::homeDirPath() );
01830         while ( urlItem )
01831         {
01832             if ( homeURL.equals( urlItem->url(), true ) )
01833             {
01834                 ops->actionCollection()->action( "home" )->unplug( toolbar );
01835                 break;
01836             }
01837 
01838             urlItem = static_cast<KURLBarItem*>( urlItem->next() );
01839         }
01840     }
01841     else
01842     {
01843         if (d->urlBar)
01844             d->urlBar->hide();
01845 
01846         if ( !ops->actionCollection()->action( "home" )->isPlugged( toolbar ) )
01847             ops->actionCollection()->action( "home" )->plug( toolbar, 3 );
01848     }
01849 
01850     static_cast<KToggleAction *>(actionCollection()->action("toggleSpeedbar"))->setChecked( show );
01851 }
01852 
01853 int KFileDialog::pathComboIndex()
01854 {
01855     return d->m_pathComboIndex;
01856 }
01857 
01858 // static
01859 void KFileDialog::initStatic()
01860 {
01861     if ( lastDirectory )
01862         return;
01863 
01864     lastDirectory = ldd.setObject(new KURL());
01865 }
01866 
01867 // static
01868 KURL KFileDialog::getStartURL( const QString& startDir,
01869                                QString& recentDirClass )
01870 {
01871     initStatic();
01872     
01873     recentDirClass = QString::null;
01874     KURL ret;
01875 
01876     bool useDefaultStartDir = startDir.isEmpty();
01877     if ( !useDefaultStartDir )
01878     {
01879         if (startDir[0] == ':')
01880         {
01881             recentDirClass = startDir;
01882             ret = KURL::fromPathOrURL( KRecentDirs::dir(recentDirClass) );
01883         }
01884         else
01885         {
01886             ret = KCmdLineArgs::makeURL( QFile::encodeName(startDir) );
01887             // If we won't be able to list it (e.g. http), then use default
01888             if ( !KProtocolInfo::supportsListing( ret.protocol() ) )
01889                 useDefaultStartDir = true;
01890         }
01891     }
01892 
01893     if ( useDefaultStartDir )
01894     {
01895         if (lastDirectory->isEmpty()) {
01896             *lastDirectory = KGlobalSettings::documentPath();
01897             KURL home;
01898             home.setPath( QDir::homeDirPath() );
01899             // if there is no docpath set (== home dir), we prefer the current
01900             // directory over it. We also prefer the homedir when our CWD is
01901             // different from our homedirectory
01902             if ( lastDirectory->path(+1) == home.path(+1) ||
01903                  QDir::currentDirPath() != QDir::homeDirPath() )
01904                 *lastDirectory = QDir::currentDirPath();
01905         }
01906         ret = *lastDirectory;
01907     }
01908 
01909     return ret;
01910 }
01911 
01912 void KFileDialog::virtual_hook( int id, void* data )
01913 { KDialogBase::virtual_hook( id, data ); }
01914 
01915 #include "kfiledialog.moc"
01916 #include "kpreviewwidgetbase.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:27 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001