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