kio Library: kfiledialog.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

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

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:57 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003