kio Library API Documentation

kfiledialog.cpp

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