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