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 }
01198 
01199 void KFileDialog::enterURL( const QString& url )
01200 {
01201     setURL( KURL::fromPathOrURL( KURLCompletion::replacedPath( url, true, true )) );
01202 }
01203 
01204 void KFileDialog::toolbarCallback(int) // SLOT
01205 {
01206     /*
01207      * yes, nothing uses this anymore.
01208      * it used to be used to show the configure dialog
01209      */
01210 }
01211 
01212 
01213 void KFileDialog::setSelection(const QString& url)
01214 {
01215     kdDebug(kfile_area) << "setSelection " << url << endl;
01216 
01217     if (url.isEmpty()) {
01218         d->selection = QString::null;
01219         return;
01220     }
01221 
01222     KURL u = getCompleteURL(url);
01223     if (!u.isValid()) { // if it still is
01224         kdWarning() << url << " is not a correct argument for setSelection!" << endl;
01225         return;
01226     }
01227 
01228 //     #warning FIXME: http URLs, e.g. from KURLCombo
01229 
01230     /* we strip the first / from the path to avoid file://usr which means
01231      *  / on host usr
01232      */
01233     KFileItem i(KFileItem::Unknown, KFileItem::Unknown, u, true );
01234     //    KFileItem i(u.path());
01235     if ( i.isDir() && u.isLocalFile() && QFile::exists( u.path() ) ) {
01236         // trust isDir() only if the file is
01237         // local (we cannot stat non-local urls) and if it exists!
01238         // (as KFileItem does not check if the file exists or not
01239         // -> the statbuffer is undefined -> isDir() is unreliable) (Simon)
01240         setURL(u, true);
01241     }
01242     else {
01243         QString filename = u.url();
01244         int sep = filename.findRev('/');
01245         if (sep >= 0) { // there is a / in it
01246             if ( KProtocolInfo::supportsListing( u )) {
01247                 KURL dir(u);
01248                 dir.setQuery( QString::null );
01249                 dir.setFileName( QString::null );
01250                 setURL(dir, true );
01251             }
01252 
01253             // filename must be decoded, or "name with space" would become
01254             // "name%20with%20space", so we use KURL::fileName()
01255             filename = u.fileName();
01256             kdDebug(kfile_area) << "filename " << filename << endl;
01257             d->selection = filename;
01258             setLocationText( filename );
01259 
01260             // tell the line edit that it has been edited
01261             // otherwise we won't know this was set by the user
01262             // and it will be ignored if there has been an
01263             // auto completion. this caused bugs where automcompletion
01264             // would start, the user would pick something from the
01265             // history and then hit Ok only to get the autocompleted
01266             // selection. OOOPS.
01267             locationEdit->lineEdit()->setEdited( true );
01268         }
01269 
01270         d->url = ops->url();
01271         d->url.addPath(filename);
01272     }
01273 }
01274 
01275 void KFileDialog::slotLoadingFinished()
01276 {
01277     if ( !d->selection.isNull() )
01278         ops->setCurrentItem( d->selection );
01279 }
01280 
01281 // ### remove in KDE4
01282 void KFileDialog::pathComboChanged( const QString& )
01283 {
01284 }
01285 void KFileDialog::dirCompletion( const QString& ) // SLOT
01286 {
01287 }
01288 void KFileDialog::fileCompletion( const QString& match )
01289 {
01290     if ( match.isEmpty() && ops->view() )
01291         ops->view()->clearSelection();
01292     else
01293         ops->setCurrentItem( match );
01294 }
01295 
01296 void KFileDialog::slotLocationChanged( const QString& text )
01297 {
01298     if ( text.isEmpty() && ops->view() )
01299         ops->view()->clearSelection();
01300 }
01301 
01302 void KFileDialog::updateStatusLine(int /* dirs */, int /* files */)
01303 {
01304     kdWarning() << "KFileDialog::updateStatusLine is deprecated! The status line no longer exists. Do not try and use it!" << endl;
01305 }
01306 
01307 QString KFileDialog::getOpenFileName(const QString& startDir,
01308                                      const QString& filter,
01309                                      QWidget *parent, const QString& caption)
01310 {
01311     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01312     dlg.setOperationMode( Opening );
01313 
01314     dlg.setMode( KFile::File | KFile::LocalOnly );
01315     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01316 
01317     dlg.ops->clearHistory();
01318     dlg.exec();
01319 
01320     return dlg.selectedFile();
01321 }
01322 
01323 QStringList KFileDialog::getOpenFileNames(const QString& startDir,
01324                                           const QString& filter,
01325                                           QWidget *parent,
01326                                           const QString& caption)
01327 {
01328     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01329     dlg.setOperationMode( Opening );
01330 
01331     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01332     dlg.setMode(KFile::Files | KFile::LocalOnly);
01333     dlg.ops->clearHistory();
01334     dlg.exec();
01335 
01336     return dlg.selectedFiles();
01337 }
01338 
01339 KURL KFileDialog::getOpenURL(const QString& startDir, const QString& filter,
01340                                 QWidget *parent, const QString& caption)
01341 {
01342     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01343     dlg.setOperationMode( Opening );
01344 
01345     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01346     dlg.setMode( KFile::File );
01347     dlg.ops->clearHistory();
01348     dlg.exec();
01349 
01350     return dlg.selectedURL();
01351 }
01352 
01353 KURL::List KFileDialog::getOpenURLs(const QString& startDir,
01354                                           const QString& filter,
01355                                           QWidget *parent,
01356                                           const QString& caption)
01357 {
01358     KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01359     dlg.setOperationMode( Opening );
01360 
01361     dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01362     dlg.setMode(KFile::Files);
01363     dlg.ops->clearHistory();
01364     dlg.exec();
01365 
01366     return dlg.selectedURLs();
01367 }
01368 
01369 KURL KFileDialog::getExistingURL(const QString& startDir,
01370                                        QWidget *parent,
01371                                        const QString& caption)
01372 {
01373     return KDirSelectDialog::selectDirectory(startDir, false, parent, caption);
01374 }
01375 
01376 QString KFileDialog::getExistingDirectory(const QString& startDir,
01377                                           QWidget *parent,
01378                                           const QString& caption)
01379 {
01380     KURL url = KDirSelectDialog::selectDirectory(startDir, true, parent,
01381                                                  caption);
01382     if ( url.isValid() )
01383         return url.path();
01384 
01385     return QString::null;
01386 }
01387 
01388 KURL KFileDialog::getImageOpenURL( const QString& startDir, QWidget *parent,
01389                                    const QString& caption)
01390 {
01391     QStringList mimetypes = KImageIO::mimeTypes( KImageIO::Reading );
01392     KFileDialog dlg(startDir,
01393                     mimetypes.join(" "),
01394                     parent, "filedialog", true);
01395     dlg.setOperationMode( Opening );
01396     dlg.setCaption( caption.isNull() ? i18n("Open") : caption );
01397     dlg.setMode( KFile::File );
01398 
01399     KImageFilePreview *ip = new KImageFilePreview( &dlg );
01400     dlg.setPreviewWidget( ip );
01401     dlg.exec();
01402 
01403     return dlg.selectedURL();
01404 }
01405 
01406 KURL KFileDialog::selectedURL() const
01407 {
01408     if ( result() == QDialog::Accepted )
01409         return d->url;
01410     else
01411         return KURL();
01412 }
01413 
01414 KURL::List KFileDialog::selectedURLs() const
01415 {
01416     KURL::List list;
01417     if ( result() == QDialog::Accepted ) {
01418         if ( (ops->mode() & KFile::Files) == KFile::Files )
01419             list = parseSelectedURLs();
01420         else
01421             list.append( d->url );
01422     }
01423     return list;
01424 }
01425 
01426 
01427 KURL::List& KFileDialog::parseSelectedURLs() const
01428 {
01429     if ( d->filenames.isEmpty() ) {
01430         return d->urlList;
01431     }
01432 
01433     d->urlList.clear();
01434     if ( d->filenames.contains( '/' )) { // assume _one_ absolute filename
01435         static const QString &prot = KGlobal::staticQString(":/");
01436         KURL u;
01437         if ( d->filenames.find( prot ) != -1 )
01438             u = d->filenames;
01439         else
01440             u.setPath( d->filenames );
01441 
01442         if ( u.isValid() )
01443             d->urlList.append( u );
01444         else
01445             KMessageBox::error( d->mainWidget,
01446                                 i18n("The chosen filenames do not\n"
01447                                      "appear to be valid."),
01448                                 i18n("Invalid Filenames") );
01449     }
01450 
01451     else
01452         d->urlList = tokenize( d->filenames );
01453 
01454     d->filenames = QString::null; // indicate that we parsed that one
01455 
01456     return d->urlList;
01457 }
01458 
01459 
01460 // FIXME: current implementation drawback: a filename can't contain quotes
01461 KURL::List KFileDialog::tokenize( const QString& line ) const
01462 {
01463     KURL::List urls;
01464     KURL u( ops->url() );
01465     QString name;
01466 
01467     int count = line.contains( '"' );
01468     if ( count == 0 ) { // no " " -> assume one single file
01469         u.setFileName( line );
01470         if ( u.isValid() )
01471             urls.append( u );
01472 
01473         return urls;
01474     }
01475 
01476     if ( (count % 2) == 1 ) { // odd number of " -> error
01477         QWidget *that = const_cast<KFileDialog *>(this);
01478         KMessageBox::sorry(that, i18n("The requested filenames\n"
01479                                       "%1\n"
01480                                       "do not appear to be valid;\n"
01481                                       "make sure every filename is enclosed in double quotes.").arg(line),
01482                            i18n("Filename Error"));
01483         return urls;
01484     }
01485 
01486     int start = 0;
01487     int index1 = -1, index2 = -1;
01488     while ( true ) {
01489         index1 = line.find( '"', start );
01490         index2 = line.find( '"', index1 + 1 );
01491 
01492         if ( index1 < 0 )
01493             break;
01494 
01495         // get everything between the " "
01496         name = line.mid( index1 + 1, index2 - index1 - 1 );
01497         u.setFileName( name );
01498         if ( u.isValid() )
01499             urls.append( u );
01500 
01501         start = index2 + 1;
01502     }
01503     return urls;
01504 }
01505 
01506 
01507 QString KFileDialog::selectedFile() const
01508 {
01509     if ( result() == QDialog::Accepted )
01510     {
01511        if (d->url.isLocalFile())
01512            return d->url.path();
01513     }
01514     return QString::null;
01515 }
01516 
01517 QStringList KFileDialog::selectedFiles() const
01518 {
01519     QStringList list;
01520 
01521     if ( result() == QDialog::Accepted ) {
01522         if ( (ops->mode() & KFile::Files) == KFile::Files ) {
01523             KURL::List urls = parseSelectedURLs();
01524             QValueListConstIterator<KURL> it = urls.begin();
01525             while ( it != urls.end() ) {
01526                 if ( (*it).isLocalFile() )
01527                     list.append( (*it).path() );
01528                 ++it;
01529             }
01530         }
01531 
01532         else { // single-selection mode
01533             if ( d->url.isLocalFile() )
01534                 list.append( d->url.path() );
01535         }
01536     }
01537 
01538     return list;
01539 }
01540 
01541 KURL KFileDialog::baseURL() const
01542 {
01543     return ops->url();
01544 }
01545 
01546 QString KFileDialog::getSaveFileName(const QString& dir, const QString& filter,
01547                                      QWidget *parent,
01548                                      const QString& caption)
01549 {
01550     bool specialDir = dir.at(0) == ':';
01551     KFileDialog dlg( specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01552     if ( !specialDir )
01553         dlg.setSelection( dir ); // may also be a filename
01554 
01555     dlg.setOperationMode( Saving );
01556     dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01557 
01558     dlg.exec();
01559 
01560     QString filename = dlg.selectedFile();
01561     if (!filename.isEmpty())
01562         KRecentDocument::add(filename);
01563 
01564     return filename;
01565 }
01566 
01567 KURL KFileDialog::getSaveURL(const QString& dir, const QString& filter,
01568                              QWidget *parent, const QString& caption)
01569 {
01570     bool specialDir = dir.at(0) == ':';
01571     KFileDialog dlg(specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01572     if ( !specialDir )
01573     dlg.setSelection( dir ); // may also be a filename
01574 
01575     dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01576     dlg.setOperationMode( Saving );
01577 
01578     dlg.exec();
01579 
01580     KURL url = dlg.selectedURL();
01581     if (url.isValid())
01582         KRecentDocument::add( url );
01583 
01584     return url;
01585 }
01586 
01587 void KFileDialog::show()
01588 {
01589     if ( !d->hasView ) { // delayed view-creation
01590         ops->setView(KFile::Default);
01591         ops->clearHistory();
01592         d->hasView = true;
01593     }
01594 
01595     KDialogBase::show();
01596 }
01597 
01598 void KFileDialog::setMode( KFile::Mode m )
01599 {
01600     ops->setMode(m);
01601     if ( ops->dirOnlyMode() ) {
01602         filterWidget->setDefaultFilter( i18n("*|All Folders") );
01603     }
01604     else {
01605         filterWidget->setDefaultFilter( i18n("*|All Files") );
01606     }
01607 
01608     updateAutoSelectExtension ();
01609 }
01610 
01611 void KFileDialog::setMode( unsigned int m )
01612 {
01613     setMode(static_cast<KFile::Mode>( m ));
01614 }
01615 
01616 KFile::Mode KFileDialog::mode() const
01617 {
01618     return ops->mode();
01619 }
01620 
01621 
01622 void KFileDialog::readConfig( KConfig *kc, const QString& group )
01623 {
01624     if ( !kc )
01625         return;
01626 
01627     QString oldGroup = kc->group();
01628     if ( !group.isEmpty() )
01629         kc->setGroup( group );
01630 
01631     ops->readConfig( kc, group );
01632 
01633     KURLComboBox *combo = d->pathCombo;
01634     combo->setURLs( kc->readPathListEntry( RecentURLs ), KURLComboBox::RemoveTop );
01635     combo->setMaxItems( kc->readNumEntry( RecentURLsNumber,
01636                                           DefaultRecentURLsNumber ) );
01637     combo->setURL( ops->url() );
01638     autoDirectoryFollowing = kc->readBoolEntry( AutoDirectoryFollowing,
01639                                                 DefaultDirectoryFollowing );
01640 
01641     KGlobalSettings::Completion cm = (KGlobalSettings::Completion)
01642                                       kc->readNumEntry( PathComboCompletionMode,
01643                                       KGlobalSettings::completionMode() );
01644     if ( cm != KGlobalSettings::completionMode() )
01645         combo->setCompletionMode( cm );
01646 
01647     cm = (KGlobalSettings::Completion)
01648          kc->readNumEntry( LocationComboCompletionMode,
01649                            KGlobalSettings::completionMode() );
01650     if ( cm != KGlobalSettings::completionMode() )
01651         locationEdit->setCompletionMode( cm );
01652 
01653     // show or don't show the speedbar
01654     toggleSpeedbar( kc->readBoolEntry(ShowSpeedbar, true) );
01655 
01656     // does the user want Automatically Select Extension?
01657     d->autoSelectExtChecked = kc->readBoolEntry (AutoSelectExtChecked, DefaultAutoSelectExtChecked);
01658     updateAutoSelectExtension ();
01659 
01660     int w1 = minimumSize().width();
01661     int w2 = toolbar->sizeHint().width() + 10;
01662     if (w1 < w2)
01663         setMinimumWidth(w2);
01664 
01665     QSize size = configDialogSize( group );
01666     resize( size );
01667     kc->setGroup( oldGroup );
01668 }
01669 
01670 void KFileDialog::writeConfig( KConfig *kc, const QString& group )
01671 {
01672     if ( !kc )
01673         return;
01674 
01675     QString oldGroup = kc->group();
01676     if ( !group.isEmpty() )
01677         kc->setGroup( group );
01678 
01679     kc->writePathEntry( RecentURLs, d->pathCombo->urls() );
01680     saveDialogSize( group, true );
01681     kc->writeEntry( PathComboCompletionMode, static_cast<int>(d->pathCombo->completionMode()) );
01682     kc->writeEntry( LocationComboCompletionMode, static_cast<int>(locationEdit->completionMode()) );
01683     kc->writeEntry( ShowSpeedbar, d->urlBar && !d->urlBar->isHidden() );
01684     kc->writeEntry( AutoSelectExtChecked, d->autoSelectExtChecked );
01685 
01686     ops->writeConfig( kc, group );
01687     kc->setGroup( oldGroup );
01688 }
01689 
01690 
01691 void KFileDialog::readRecentFiles( KConfig *kc )
01692 {
01693     QString oldGroup = kc->group();
01694     kc->setGroup( ConfigGroup );
01695 
01696     locationEdit->setMaxItems( kc->readNumEntry( RecentFilesNumber,
01697                                                  DefaultRecentURLsNumber ) );
01698     locationEdit->setURLs( kc->readPathListEntry( RecentFiles ),
01699                            KURLComboBox::RemoveBottom );
01700     locationEdit->insertItem( QString::null, 0 ); // dummy item without pixmap
01701     locationEdit->setCurrentItem( 0 );
01702 
01703     kc->setGroup( oldGroup );
01704 }
01705 
01706 void KFileDialog::saveRecentFiles( KConfig *kc )
01707 {
01708     QString oldGroup = kc->group();
01709     kc->setGroup( ConfigGroup );
01710 
01711     kc->writePathEntry( RecentFiles, locationEdit->urls() );
01712 
01713     kc->setGroup( oldGroup );
01714 }
01715 
01716 KPushButton * KFileDialog::okButton() const
01717 {
01718     return d->okButton;
01719 }
01720 
01721 KPushButton * KFileDialog::cancelButton() const
01722 {
01723     return d->cancelButton;
01724 }
01725 
01726 KURLBar * KFileDialog::speedBar()
01727 {
01728     return d->urlBar;
01729 }
01730 
01731 void KFileDialog::slotCancel()
01732 {
01733     ops->close();
01734     KDialogBase::slotCancel();
01735 }
01736 
01737 void KFileDialog::setKeepLocation( bool keep )
01738 {
01739     d->keepLocation = keep;
01740 }
01741 
01742 bool KFileDialog::keepsLocation() const
01743 {
01744     return d->keepLocation;
01745 }
01746 
01747 void KFileDialog::setOperationMode( OperationMode mode )
01748 {
01749     d->operationMode = mode;
01750     d->keepLocation = (mode == Saving);
01751     filterWidget->setEditable( !d->hasDefaultFilter || mode != Saving );
01752     if ( mode == Opening )
01753        d->okButton->setGuiItem( KGuiItem( i18n("&Open"), "fileopen") );
01754     else if ( mode == Saving )
01755        d->okButton->setGuiItem( KStdGuiItem::save() );
01756     else
01757        d->okButton->setGuiItem( KStdGuiItem::KStdGuiItem::ok() );
01758     updateLocationWhatsThis ();
01759     updateAutoSelectExtension ();
01760 }
01761 
01762 KFileDialog::OperationMode KFileDialog::operationMode() const
01763 {
01764     return d->operationMode;
01765 }
01766 
01767 void KFileDialog::slotAutoSelectExtClicked()
01768 {
01769     kdDebug (kfile_area) << "slotAutoSelectExtClicked(): "
01770                          << d->autoSelectExtCheckBox->isChecked () << endl;
01771 
01772     // whether the _user_ wants it on/off
01773     d->autoSelectExtChecked = d->autoSelectExtCheckBox->isChecked ();
01774 
01775     // update the current filename's extension
01776     updateLocationEditExtension (d->extension /* extension hasn't changed */);
01777 }
01778 
01779 static QString getExtensionFromPatternList (const QStringList &patternList)
01780 {
01781     QString ret;
01782     kdDebug (kfile_area) << "\tgetExtension " << patternList << endl;
01783 
01784     QStringList::ConstIterator patternListEnd = patternList.end ();
01785     for (QStringList::ConstIterator it = patternList.begin ();
01786          it != patternListEnd;
01787          it++)
01788     {
01789         kdDebug (kfile_area) << "\t\ttry: \'" << (*it) << "\'" << endl;
01790 
01791         // is this pattern like "*.BMP" rather than useless things like:
01792         //
01793         // README
01794         // *.
01795         // *.*
01796         // *.JP*G
01797         // *.JP?
01798         if ((*it).startsWith ("*.") &&
01799             (*it).length () > 2 &&
01800             (*it).find ('*', 2) < 0 && (*it).find ('?', 2) < 0)
01801         {
01802             ret = (*it).mid (1);
01803             break;
01804         }
01805     }
01806 
01807     return ret;
01808 }
01809 
01810 static QString stripUndisplayable (const QString &string)
01811 {
01812     QString ret = string;
01813 
01814     ret.remove (':');
01815     ret.remove ('&');
01816 
01817     return ret;
01818 }
01819 
01820 
01821 QString KFileDialog::currentFilterExtension (void)
01822 {
01823     return d->extension;
01824 }
01825 
01826 void KFileDialog::updateAutoSelectExtension (void)
01827 {
01828     if (!d->autoSelectExtCheckBox) return;
01829 
01830     //
01831     // Figure out an extension for the Automatically Select Extension thing
01832     // (some Windows users apparently don't know what to do when confronted
01833     // with a text file called "COPYING" but do know what to do with
01834     // COPYING.txt ...)
01835     //
01836 
01837     kdDebug (kfile_area) << "Figure out an extension: " << endl;
01838     QString lastExtension = d->extension;
01839     d->extension = QString::null;
01840 
01841     // Automatically Select Extension is only valid if the user is _saving_ a _file_
01842     if ((operationMode () == Saving) && (mode () & KFile::File))
01843     {
01844         //
01845         // Get an extension from the filter
01846         //
01847 
01848         QString filter = currentFilter ();
01849         if (!filter.isEmpty ())
01850         {
01851             // e.g. "*.cpp"
01852             if (filter.find ('/') < 0)
01853             {
01854                 d->extension = getExtensionFromPatternList (QStringList::split (" ", filter)).lower ();
01855                 kdDebug (kfile_area) << "\tsetFilter-style: pattern ext=\'"
01856                                     << d->extension << "\'" << endl;
01857             }
01858             // e.g. "text/html"
01859             else
01860             {
01861                 KMimeType::Ptr mime = KMimeType::mimeType (filter);
01862 
01863                 // first try X-KDE-NativeExtension
01864                 QString nativeExtension = mime->property ("X-KDE-NativeExtension").toString ();
01865                 if (nativeExtension.at (0) == '.')
01866                 {
01867                     d->extension = nativeExtension.lower ();
01868                     kdDebug (kfile_area) << "\tsetMimeFilter-style: native ext=\'"
01869                                          << d->extension << "\'" << endl;
01870                 }
01871 
01872                 // no X-KDE-NativeExtension
01873                 if (d->extension.isEmpty ())
01874                 {
01875                     d->extension = getExtensionFromPatternList (mime->patterns ()).lower ();
01876                     kdDebug (kfile_area) << "\tsetMimeFilter-style: pattern ext=\'"
01877                                          << d->extension << "\'" << endl;
01878                 }
01879             }
01880         }
01881 
01882 
01883         //
01884         // GUI: checkbox
01885         //
01886 
01887         QString whatsThisExtension;
01888         if (!d->extension.isEmpty ())
01889         {
01890             // remember: sync any changes to the string with below
01891             d->autoSelectExtCheckBox->setText (i18n ("Automatically select filename e&xtension (%1)").arg (d->extension));
01892             whatsThisExtension = i18n ("the extension <b>%1</b>").arg (d->extension);
01893 
01894             d->autoSelectExtCheckBox->setEnabled (true);
01895             d->autoSelectExtCheckBox->setChecked (d->autoSelectExtChecked);
01896         }
01897         else
01898         {
01899             // remember: sync any changes to the string with above
01900             d->autoSelectExtCheckBox->setText (i18n ("Automatically select filename e&xtension"));
01901             whatsThisExtension = i18n ("a suitable extension");
01902 
01903             d->autoSelectExtCheckBox->setChecked (false);
01904             d->autoSelectExtCheckBox->setEnabled (false);
01905         }
01906 
01907         const QString locationLabelText = stripUndisplayable (d->locationLabel->text ());
01908         const QString filterLabelText = stripUndisplayable (d->filterLabel->text ());
01909         QWhatsThis::add (d->autoSelectExtCheckBox,
01910             "<qt>" +
01911                 i18n (
01912                   "This option enables some convenient features for "
01913                   "saving files with extensions:<br>"
01914                   "<ol>"
01915                     "<li>Any extension specified in the <b>%1</b> text "
01916                     "area will be updated if you change the file type "
01917                     "to save in.<br>"
01918                     "<br></li>"
01919                     "<li>If no extension is specified in the <b>%2</b> "
01920                     "text area when you click "
01921                     "<b>Save</b>, %3 will be added to the end of the "
01922                     "filename (if the filename does not already exist). "
01923                     "This extension is based on the file type that you "
01924                     "have chosen to save in.<br>"
01925                     "<br>"
01926                     "If you do not want KDE to supply an extension for the "
01927                     "filename, you can either turn this option off or you "
01928                     "can suppress it by adding a period (.) to the end of "
01929                     "the filename (the period will be automatically "
01930                     "removed)."
01931                     "</li>"
01932                   "</ol>"
01933                   "If unsure, keep this option enabled as it makes your "
01934                   "files more manageable."
01935                     )
01936                 .arg (locationLabelText)
01937                 .arg (locationLabelText)
01938                 .arg (whatsThisExtension)
01939             + "</qt>"
01940             );
01941 
01942         d->autoSelectExtCheckBox->show ();
01943 
01944 
01945         // update the current filename's extension
01946         updateLocationEditExtension (lastExtension);
01947     }
01948     // Automatically Select Extension not valid
01949     else
01950     {
01951         d->autoSelectExtCheckBox->setChecked (false);
01952         d->autoSelectExtCheckBox->hide ();
01953     }
01954 }
01955 
01956 // Updates the extension of the filename specified in locationEdit if the
01957 // Automatically Select Extension feature is enabled.
01958 // (this prevents you from accidently saving "file.kwd" as RTF, for example)
01959 void KFileDialog::updateLocationEditExtension (const QString &lastExtension)
01960 {
01961     if (!d->autoSelectExtCheckBox->isChecked () || d->extension.isEmpty ())
01962         return;
01963 
01964     QString urlStr = locationEdit->currentText ();
01965     if (urlStr.isEmpty ())
01966         return;
01967 
01968     KURL url = getCompleteURL (urlStr);
01969     kdDebug (kfile_area) << "updateLocationEditExtension (" << url << ")" << endl;
01970 
01971     const int fileNameOffset = urlStr.findRev ('/') + 1;
01972     QString fileName = urlStr.mid (fileNameOffset);
01973 
01974     const int dot = fileName.findRev ('.');
01975     const int len = fileName.length ();
01976     if (dot > 0 && // has an extension already and it's not a hidden file
01977                    // like ".hidden" (but we do accept ".hidden.ext")
01978         dot != len - 1 // and not deliberately suppressing extension
01979     )
01980     {
01981         // exists?
01982         KIO::UDSEntry t;
01983         if (KIO::NetAccess::stat (url, t, topLevelWidget()))
01984         {
01985             kdDebug (kfile_area) << "\tfile exists" << endl;
01986 
01987             if (isDirectory (t))
01988             {
01989                 kdDebug (kfile_area) << "\tisDir - won't alter extension" << endl;
01990                 return;
01991             }
01992 
01993             // --- fall through ---
01994         }
01995 
01996 
01997         //
01998         // try to get rid of the current extension
01999         //
02000 
02001         // catch "double extensions" like ".tar.gz"
02002         if (lastExtension.length () && fileName.endsWith (lastExtension))
02003             fileName.truncate (len - lastExtension.length ());
02004         // can only handle "single extensions"
02005         else
02006             fileName.truncate (dot);
02007 
02008         // add extension
02009         locationEdit->setCurrentText (urlStr.left (fileNameOffset) + fileName + d->extension);
02010         locationEdit->lineEdit()->setEdited (true);
02011     }
02012 }
02013 
02014 // applies only to a file that doesn't already exist
02015 void KFileDialog::appendExtension (KURL &url)
02016 {
02017     if (!d->autoSelectExtCheckBox->isChecked () || d->extension.isEmpty ())
02018         return;
02019 
02020     QString fileName = url.fileName ();
02021     if (fileName.isEmpty ())
02022         return;
02023 
02024     kdDebug (kfile_area) << "appendExtension(" << url << ")" << endl;
02025 
02026     const int len = fileName.length ();
02027     const int dot = fileName.findRev ('.');
02028 
02029     const bool suppressExtension = (dot == len - 1);
02030     const bool unspecifiedExtension = (dot <= 0);
02031 
02032     // don't KIO::NetAccess::Stat if unnecessary
02033     if (!(suppressExtension || unspecifiedExtension))
02034         return;
02035 
02036     // exists?
02037     KIO::UDSEntry t;
02038     if (KIO::NetAccess::stat (url, t, topLevelWidget()))
02039     {
02040         kdDebug (kfile_area) << "\tfile exists - won't append extension" << endl;
02041         return;
02042     }
02043 
02044     // suppress automatically append extension?
02045     if (suppressExtension)
02046     {
02047         //
02048         // Strip trailing dot
02049         // This allows lazy people to have autoSelectExtCheckBox->isChecked
02050         // but don't want a file extension to be appended
02051         // e.g. "README." will make a file called "README"
02052         //
02053         // If you really want a name like "README.", then type "README.."
02054         // and the trailing dot will be removed (or just stop being lazy and
02055         // turn off this feature so that you can type "README.")
02056         //
02057         kdDebug (kfile_area) << "\tstrip trailing dot" << endl;
02058         url.setFileName (fileName.left (len - 1));
02059     }
02060     // evilmatically append extension :) if the user hasn't specified one
02061     else if (unspecifiedExtension)
02062     {
02063         kdDebug (kfile_area) << "\tappending extension \'" << d->extension << "\'..." << endl;
02064         url.setFileName (fileName + d->extension);
02065         kdDebug (kfile_area) << "\tsaving as \'" << url << "\'" << endl;
02066     }
02067 }
02068 
02069 
02070 // adds the selected files/urls to 'recent documents'
02071 void KFileDialog::addToRecentDocuments()
02072 {
02073     int m = ops->mode();
02074 
02075     if ( m & KFile::LocalOnly ) {
02076         QStringList files = selectedFiles();
02077         QStringList::ConstIterator it = files.begin();
02078         for ( ; it != files.end(); ++it )
02079             KRecentDocument::add( *it );
02080     }
02081 
02082     else { // urls
02083         KURL::List urls = selectedURLs();
02084         KURL::List::ConstIterator it = urls.begin();
02085         for ( ; it != urls.end(); ++it ) {
02086             if ( (*it).isValid() )
02087                 KRecentDocument::add( *it );
02088         }
02089     }
02090 }
02091 
02092 KActionCollection * KFileDialog::actionCollection() const
02093 {
02094     return ops->actionCollection();
02095 }
02096 
02097 void KFileDialog::keyPressEvent( QKeyEvent *e )
02098 {
02099     if ( e->key() == Key_Escape )
02100     {
02101         e->accept();
02102         d->cancelButton->animateClick();
02103     }
02104     else
02105         KDialogBase::keyPressEvent( e );
02106 }
02107 
02108 void KFileDialog::toggleSpeedbar( bool show )
02109 {
02110     if ( show )
02111     {
02112         if ( !d->urlBar )
02113             initSpeedbar();
02114 
02115         d->urlBar->show();
02116 
02117         // check to see if they have a home item defined, if not show the home button
02118         KURLBarItem *urlItem = static_cast<KURLBarItem*>( d->urlBar->listBox()->firstItem() );
02119         KURL homeURL;
02120         homeURL.setPath( QDir::homeDirPath() );
02121         while ( urlItem )
02122         {
02123             if ( homeURL.equals( urlItem->url(), true ) )
02124             {
02125                 ops->actionCollection()->action( "home" )->unplug( toolbar );
02126                 break;
02127             }
02128 
02129             urlItem = static_cast<KURLBarItem*>( urlItem->next() );
02130         }
02131     }
02132     else
02133     {
02134         if (d->urlBar)
02135             d->urlBar->hide();
02136 
02137         if ( !ops->actionCollection()->action( "home" )->isPlugged( toolbar ) )
02138             ops->actionCollection()->action( "home" )->plug( toolbar, 3 );
02139     }
02140 
02141     static_cast<KToggleAction *>(actionCollection()->action("toggleSpeedbar"))->setChecked( show );
02142 }
02143 
02144 int KFileDialog::pathComboIndex()
02145 {
02146     return d->m_pathComboIndex;
02147 }
02148 
02149 // static
02150 void KFileDialog::initStatic()
02151 {
02152     if ( lastDirectory )
02153         return;
02154 
02155     lastDirectory = ldd.setObject(lastDirectory, new KURL());
02156 }
02157 
02158 // static
02159 KURL KFileDialog::getStartURL( const QString& startDir,
02160                                QString& recentDirClass )
02161 {
02162     initStatic();
02163 
02164     recentDirClass = QString::null;
02165     KURL ret;
02166 
02167     bool useDefaultStartDir = startDir.isEmpty();
02168     if ( !useDefaultStartDir )
02169     {
02170         if (startDir[0] == ':')
02171         {
02172             recentDirClass = startDir;
02173             ret = KURL::fromPathOrURL( KRecentDirs::dir(recentDirClass) );
02174         }
02175         else
02176         {
02177             ret = KCmdLineArgs::makeURL( QFile::encodeName(startDir) );
02178             // If we won't be able to list it (e.g. http), then use default
02179             if ( !KProtocolInfo::supportsListing( ret ) )
02180                 useDefaultStartDir = true;
02181         }
02182     }
02183 
02184     if ( useDefaultStartDir )
02185     {
02186         if (lastDirectory->isEmpty()) {
02187             lastDirectory->setPath(KGlobalSettings::documentPath());
02188             KURL home;
02189             home.setPath( QDir::homeDirPath() );
02190             // if there is no docpath set (== home dir), we prefer the current
02191             // directory over it. We also prefer the homedir when our CWD is
02192             // different from our homedirectory or when the document dir
02193             // does not exist
02194             if ( lastDirectory->path(+1) == home.path(+1) ||
02195                  QDir::currentDirPath() != QDir::homeDirPath() ||
02196                  !QDir(lastDirectory->path(+1)).exists() )
02197                 lastDirectory->setPath(QDir::currentDirPath());
02198         }
02199         ret = *lastDirectory;
02200     }
02201 
02202     return ret;
02203 }
02204 
02205 void KFileDialog::setStartDir( const KURL& directory )
02206 {
02207     initStatic();
02208     if ( directory.isValid() )
02209         *lastDirectory = directory;
02210 }
02211 
02212 void KFileDialog::virtual_hook( int id, void* data )
02213 { KDialogBase::virtual_hook( id, data ); }
02214 
02215 
02216 #include "kfiledialog.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:33:24 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003