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