00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027
00028 #include <qptrcollection.h>
00029 #include <qcombobox.h>
00030 #include <qlabel.h>
00031 #include <qlayout.h>
00032 #include <qlineedit.h>
00033 #include <qptrlist.h>
00034 #include <qpixmap.h>
00035 #include <qtooltip.h>
00036 #include <qtimer.h>
00037 #include <qwhatsthis.h>
00038
00039 #include <kaccel.h>
00040 #include <kaction.h>
00041 #include <kapplication.h>
00042 #include <kcmdlineargs.h>
00043 #include <kcompletionbox.h>
00044 #include <kconfig.h>
00045 #include <kdebug.h>
00046 #include <kglobal.h>
00047 #include <kglobalsettings.h>
00048 #include <kiconloader.h>
00049 #include <kimageio.h>
00050 #include <kio/job.h>
00051 #include <kio/previewjob.h>
00052 #include <kio/scheduler.h>
00053 #include <klocale.h>
00054 #include <kmessagebox.h>
00055 #include <kmimetype.h>
00056 #include <kpopupmenu.h>
00057 #include <kprotocolinfo.h>
00058 #include <kpushbutton.h>
00059 #include <krecentdirs.h>
00060 #include <kstandarddirs.h>
00061 #include <kstdguiitem.h>
00062 #include <kstaticdeleter.h>
00063 #include <ktoolbar.h>
00064 #include <ktoolbarbutton.h>
00065 #include <kurl.h>
00066 #include <kurlcombobox.h>
00067 #include <kurlcompletion.h>
00068
00069 #include "config-kfile.h"
00070 #include "kpreviewwidgetbase.h"
00071
00072 #include <kfileview.h>
00073 #include <krecentdocument.h>
00074 #include <kfiledialog.h>
00075 #include <kfilefiltercombo.h>
00076 #include <kdiroperator.h>
00077 #include <kimagefilepreview.h>
00078
00079 #include <kfilespeedbar.h>
00080 #include <kfilebookmarkhandler.h>
00081
00082 enum Buttons { HOTLIST_BUTTON,
00083 PATH_COMBO, CONFIGURE_BUTTON };
00084
00085 template class QPtrList<KIO::StatJob>;
00086
00087 namespace {
00088 static void silenceQToolBar(QtMsgType, const char *)
00089 {
00090 }
00091 }
00092
00093 struct KFileDialogPrivate
00094 {
00095
00096 KURL url;
00097
00098
00099 QString filenames;
00100
00101
00102 QString selection;
00103
00104
00105 QString completionHack;
00106
00107
00108
00109 QBoxLayout *boxLayout;
00110 QWidget *mainWidget;
00111
00112 QLabel *locationLabel;
00113
00114
00115 QLabel *filterLabel;
00116 KURLComboBox *pathCombo;
00117 KPushButton *okButton, *cancelButton;
00118 KFileSpeedBar *urlBar;
00119 QHBoxLayout *urlBarLayout;
00120 QWidget *customWidget;
00121
00122 QPtrList<KIO::StatJob> statJobs;
00123
00124 KURL::List urlList;
00125
00126 QStringList mimetypes;
00127
00128
00129
00130 bool keepLocation :1;
00131
00132
00133
00134 bool hasView :1;
00135
00136
00137 bool initializeSpeedbar :1;
00138
00139
00140
00141 bool completionLock :1;
00142
00143 bool hasDefaultFilter :1;
00144 KFileDialog::OperationMode operationMode;
00145
00146
00147 QString fileClass;
00148
00149 KFileBookmarkHandler *bookmarkHandler;
00150
00151
00152 int m_pathComboIndex;
00153 };
00154
00155 KURL *KFileDialog::lastDirectory;
00156
00157 static KStaticDeleter<KURL> ldd;
00158
00159 KFileDialog::KFileDialog(const QString& startDir, const QString& filter,
00160 QWidget *parent, const char* name, bool modal)
00161 : KDialogBase( parent, name, modal, QString::null, 0 )
00162 {
00163 init( startDir, filter, 0 );
00164 }
00165
00166 KFileDialog::KFileDialog(const QString& startDir, const QString& filter,
00167 QWidget *parent, const char* name, bool modal, QWidget* widget)
00168 : KDialogBase( parent, name, modal, QString::null, 0 )
00169 {
00170 init( startDir, filter, widget );
00171 }
00172
00173 KFileDialog::~KFileDialog()
00174 {
00175 hide();
00176
00177 KConfig *config = KGlobal::config();
00178
00179 if (d->urlBar)
00180 d->urlBar->save( config );
00181
00182 config->sync();
00183
00184 delete ops;
00185 delete d;
00186 }
00187
00188 void KFileDialog::setLocationLabel(const QString& text)
00189 {
00190 d->locationLabel->setText(text);
00191 }
00192
00193 void KFileDialog::setFilter(const QString& filter)
00194 {
00195 int pos = filter.find('/');
00196
00197
00198
00199
00200 if (pos > 0 && filter[pos - 1] != '\\') {
00201 QStringList filters = QStringList::split( " ", filter );
00202 setMimeFilter( filters );
00203 return;
00204 }
00205
00206
00207
00208
00209 QString copy (filter);
00210 for (pos = 0; (pos = copy.find("\\/", pos)) != -1; ++pos)
00211 copy.remove(pos, 1);
00212
00213 ops->clearFilter();
00214 filterWidget->setFilter(copy);
00215 ops->setNameFilter(filterWidget->currentFilter());
00216 d->hasDefaultFilter = false;
00217 filterWidget->setEditable( true );
00218 }
00219
00220 QString KFileDialog::currentFilter() const
00221 {
00222 return filterWidget->currentFilter();
00223 }
00224
00225
00226 void KFileDialog::setFilterMimeType(const QString &label,
00227 const KMimeType::List &types,
00228 const KMimeType::Ptr &defaultType)
00229 {
00230 d->mimetypes.clear();
00231 d->filterLabel->setText(label);
00232
00233 KMimeType::List::ConstIterator it;
00234 for( it = types.begin(); it != types.end(); ++it)
00235 d->mimetypes.append( (*it)->name() );
00236
00237 setMimeFilter( d->mimetypes, defaultType->name() );
00238 }
00239
00240 void KFileDialog::setMimeFilter( const QStringList& mimeTypes,
00241 const QString& defaultType )
00242 {
00243 d->mimetypes = mimeTypes;
00244 filterWidget->setMimeFilter( mimeTypes, defaultType );
00245
00246 QStringList types = QStringList::split(" ", filterWidget->currentFilter());
00247 types.append( QString::fromLatin1( "inode/directory" ));
00248 ops->clearFilter();
00249 ops->setMimeFilter( types );
00250 d->hasDefaultFilter = !defaultType.isEmpty();
00251 filterWidget->setEditable( !d->hasDefaultFilter ||
00252 d->operationMode != Saving );
00253 }
00254
00255 void KFileDialog::clearFilter()
00256 {
00257 d->mimetypes.clear();
00258 filterWidget->setFilter( QString::null );
00259 ops->clearFilter();
00260 d->hasDefaultFilter = false;
00261 filterWidget->setEditable( true );
00262 }
00263
00264 QString KFileDialog::currentMimeFilter() const
00265 {
00266 int i = filterWidget->currentItem();
00267 if (filterWidget->showsAllTypes())
00268 i--;
00269
00270 if ((i >= 0) && (i < (int) d->mimetypes.count()))
00271 return d->mimetypes[i];
00272 return QString::null;
00273 }
00274
00275 KMimeType::Ptr KFileDialog::currentFilterMimeType()
00276 {
00277 return KMimeType::mimeType( currentMimeFilter() );
00278 }
00279
00280 void KFileDialog::setPreviewWidget(const QWidget *w) {
00281 ops->setPreviewWidget(w);
00282 ops->clearHistory();
00283 d->hasView = true;
00284 }
00285
00286 void KFileDialog::setPreviewWidget(const KPreviewWidgetBase *w) {
00287 ops->setPreviewWidget(w);
00288 ops->clearHistory();
00289 d->hasView = true;
00290 }
00291
00292
00293 void KFileDialog::slotOk()
00294 {
00295 kdDebug(kfile_area) << "slotOK\n";
00296
00297
00298
00299 const KFileItemList *items = ops->selectedItems();
00300
00301 if ( (mode() & KFile::Directory) != KFile::Directory ) {
00302 if ( locationEdit->currentText().stripWhiteSpace().isEmpty() ) {
00303 if ( !items || items->isEmpty() )
00304 return;
00305
00306
00307
00308 else {
00309
00310 bool multi = (mode() & KFile::Files) != 0;
00311 KFileItemListIterator it( *items );
00312 QString endQuote = QString::fromLatin1("\" ");
00313 QString name, files;
00314 while ( it.current() ) {
00315 name = (*it)->name();
00316 if ( multi ) {
00317 name.prepend( '"' );
00318 name.append( endQuote );
00319 }
00320
00321 files.append( name );
00322 ++it;
00323 }
00324 locationEdit->setEditText( files );
00325 locationEdit->lineEdit()->setEdited( false );
00326 return;
00327 }
00328 }
00329 }
00330
00331 bool dirOnly = ops->dirOnlyMode();
00332
00333
00334 if ( items && !locationEdit->lineEdit()->edited() &&
00335 !(items->isEmpty() && !dirOnly) ) {
00336
00337 d->urlList.clear();
00338 d->filenames = QString::null;
00339
00340 if ( dirOnly ) {
00341 d->url = ops->url();
00342 }
00343 else {
00344 if ( !(mode() & KFile::Files) ) {
00345 d->url = items->getFirst()->url();
00346 }
00347
00348 else {
00349 d->url = ops->url();
00350 KFileItemListIterator it( *items );
00351 while ( it.current() ) {
00352 d->urlList.append( (*it)->url() );
00353 ++it;
00354 }
00355 }
00356 }
00357
00358 if ( (mode() & KFile::LocalOnly) == KFile::LocalOnly &&
00359 !d->url.isLocalFile() ) {
00360
00361 KMessageBox::sorry( d->mainWidget,
00362 i18n("You can only select local files."),
00363 i18n("Remote Files not Accepted") );
00364 return;
00365 }
00366
00367 accept();
00368 return;
00369 }
00370
00371
00372 KURL selectedURL;
00373
00374 if ( (mode() & KFile::Files) == KFile::Files ) {
00375 if ( locationEdit->currentText().contains( '/' )) {
00376
00377
00378 KURL u( ops->url(), locationEdit->currentText() );
00379 if ( !u.isMalformed() )
00380 selectedURL = u;
00381 else
00382 selectedURL = ops->url();
00383 }
00384 else
00385 selectedURL = ops->url();
00386 }
00387
00388 else {
00389 QString text = locationEdit->currentText();
00390 if ( KURL::isRelativeURL(text) )
00391 {
00392 if ( !text.isEmpty() && text[0] == '/' )
00393 selectedURL.setPath( text );
00394 else
00395 {
00396 selectedURL = ops->url();
00397 selectedURL.addPath( text );
00398 selectedURL.cleanPath ();
00399 }
00400 } else
00401 selectedURL = text;
00402 }
00403
00404 if ( selectedURL.isMalformed() ) {
00405 KMessageBox::sorry( d->mainWidget, i18n("%1\ndoes not appear to be a valid URL.\n").arg(d->url.url()), i18n("Invalid URL") );
00406 return;
00407 }
00408
00409 if ( (mode() & KFile::LocalOnly) == KFile::LocalOnly &&
00410 !selectedURL.isLocalFile() ) {
00411 KMessageBox::sorry( d->mainWidget,
00412 i18n("You can only select local files."),
00413 i18n("Remote Files not Accepted") );
00414 return;
00415 }
00416
00417 d->url = selectedURL;
00418
00419
00420
00421 if ( (mode() & KFile::Directory) == KFile::Directory ) {
00422 kdDebug(kfile_area) << "Directory" << endl;
00423 bool done = true;
00424 if ( d->url.isLocalFile() ) {
00425 if ( locationEdit->currentText().stripWhiteSpace().isEmpty() ) {
00426 QFileInfo info( d->url.path() );
00427 if ( info.isDir() ) {
00428 d->filenames = QString::null;
00429 d->urlList.clear();
00430 d->urlList.append( d->url );
00431 accept();
00432 }
00433 else if (!info.exists() && (mode() & KFile::File) != KFile::File) {
00434
00435 if ( ops->mkdir( d->url.url(), true ))
00436 return;
00437 else
00438 accept();
00439 }
00440 else {
00441
00442 if ( mode() & KFile::File == KFile::File ||
00443 mode() & KFile::Files == KFile::Files )
00444 done = false;
00445 }
00446 }
00447 else
00448 {
00449 if ( mode() & KFile::ExistingOnly )
00450 {
00451 if ( ops->dirOnlyMode() )
00452 {
00453 KURL fullURL(d->url, locationEdit->currentText());
00454 if ( QFile::exists( fullURL.path() ) )
00455 {
00456 d->url = fullURL;
00457 d->filenames = QString::null;
00458 d->urlList.clear();
00459 accept();
00460 return;
00461 }
00462 else
00463 return;
00464 }
00465 }
00466
00467 d->filenames = locationEdit->currentText();
00468 accept();
00469 }
00470
00471 }
00472 else {
00473
00474 d->filenames = QString::null;
00475 d->urlList.clear();
00476 d->urlList.append( d->url );
00477
00478 if ( mode() & KFile::ExistingOnly )
00479 done = false;
00480 else
00481 accept();
00482 }
00483
00484 if ( done )
00485 return;
00486 }
00487
00488 if (!kapp->authorizeURLAction("open", KURL(), d->url))
00489 {
00490 QString msg = KIO::buildErrorString(KIO::ERR_ACCESS_DENIED, d->url.prettyURL());
00491 KMessageBox::error( d->mainWidget, msg);
00492 return;
00493 }
00494
00495 KIO::StatJob *job = 0L;
00496 d->statJobs.clear();
00497 d->filenames = locationEdit->currentText();
00498
00499 if ( (mode() & KFile::Files) == KFile::Files &&
00500 !locationEdit->currentText().contains( '/' )) {
00501 kdDebug(kfile_area) << "Files\n";
00502 KURL::List list = parseSelectedURLs();
00503 for ( KURL::List::ConstIterator it = list.begin();
00504 it != list.end(); ++it )
00505 {
00506 if (!kapp->authorizeURLAction("open", KURL(), *it))
00507 {
00508 QString msg = KIO::buildErrorString(KIO::ERR_ACCESS_DENIED, (*it).prettyURL());
00509 KMessageBox::error( d->mainWidget, msg);
00510 return;
00511 }
00512 }
00513 for ( KURL::List::ConstIterator it = list.begin();
00514 it != list.end(); ++it )
00515 {
00516 job = KIO::stat( *it, !(*it).isLocalFile() );
00517 KIO::Scheduler::scheduleJob( job );
00518 d->statJobs.append( job );
00519 connect( job, SIGNAL( result(KIO::Job *) ),
00520 SLOT( slotStatResult( KIO::Job *) ));
00521 }
00522 return;
00523 }
00524
00525 job = KIO::stat(d->url,!d->url.isLocalFile());
00526 d->statJobs.append( job );
00527 connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotStatResult(KIO::Job*)));
00528 }
00529
00530
00531
00532
00533
00534 void KFileDialog::slotStatResult(KIO::Job* job)
00535 {
00536 kdDebug(kfile_area) << "slotStatResult" << endl;
00537 KIO::StatJob *sJob = static_cast<KIO::StatJob *>( job );
00538
00539 if ( !d->statJobs.removeRef( sJob ) ) {
00540 return;
00541 }
00542
00543 int count = d->statJobs.count();
00544
00545
00546
00547 if (sJob->error() && count == 0 && !ops->dirOnlyMode())
00548 accept();
00549
00550 KIO::UDSEntry t = sJob->statResult();
00551 bool isDir = false;
00552 for (KIO::UDSEntry::ConstIterator it = t.begin();
00553 it != t.end(); ++it) {
00554 if ((*it).m_uds == KIO::UDS_FILE_TYPE ) {
00555 isDir = S_ISDIR( (mode_t)((*it).m_long));
00556 break;
00557 }
00558 }
00559
00560 if (isDir)
00561 {
00562 if ( ops->dirOnlyMode() )
00563 {
00564 d->filenames = QString::null;
00565 d->urlList.clear();
00566 accept();
00567 }
00568 else
00569 {
00570 if ( count == 0 ) {
00571 locationEdit->clearEdit();
00572 locationEdit->lineEdit()->setEdited( false );
00573 setURL( sJob->url() );
00574 }
00575 }
00576 d->statJobs.clear();
00577 return;
00578 }
00579 else if ( ops->dirOnlyMode() && !isDir )
00580 {
00581 return;
00582 }
00583
00584 kdDebug(kfile_area) << "filename " << sJob->url().url() << endl;
00585
00586 if ( count == 0 )
00587 accept();
00588 }
00589
00590
00591 void KFileDialog::accept()
00592 {
00593 setResult( QDialog::Accepted );
00594
00595 *lastDirectory = ops->url();
00596 if (!d->fileClass.isEmpty())
00597 KRecentDirs::add(d->fileClass, ops->url().url());
00598
00599
00600 locationEdit->changeItem( QString::null, 0 );
00601
00602 KURL::List list = selectedURLs();
00603 QValueListConstIterator<KURL> it = list.begin();
00604 for ( ; it != list.end(); ++it ) {
00605 const KURL& url = *it;
00606
00607
00608
00609 QString file = url.isLocalFile() ? url.path(-1) : url.prettyURL(-1);
00610
00611
00612 for ( int i = 1; i < locationEdit->count(); i++ ) {
00613 if ( locationEdit->text( i ) == file ) {
00614 locationEdit->removeItem( i-- );
00615 break;
00616 }
00617 }
00618 locationEdit->insertItem( file, 1 );
00619 }
00620
00621 KConfig *config = KGlobal::config();
00622 config->setForceGlobal( true );
00623 writeConfig( config, ConfigGroup );
00624 config->setForceGlobal( false );
00625
00626 saveRecentFiles( config );
00627 config->sync();
00628
00629 KDialogBase::accept();
00630
00631 addToRecentDocuments();
00632
00633 if ( (mode() & KFile::Files) != KFile::Files )
00634 emit fileSelected(d->url.url());
00635
00636 ops->close();
00637 emit okClicked();
00638 }
00639
00640
00641 void KFileDialog::fileHighlighted(const KFileItem *i)
00642 {
00643 if (i && i->isDir())
00644 return;
00645
00646
00647 if ( (ops->mode() & KFile::Files) != KFile::Files ) {
00648 if ( !i )
00649 return;
00650
00651 d->url = i->url();
00652
00653 if ( !d->completionLock ) {
00654 locationEdit->setCurrentItem( 0 );
00655 locationEdit->setEditText( i->name() );
00656 locationEdit->lineEdit()->setEdited( false );
00657 }
00658 emit fileHighlighted(d->url.url());
00659 }
00660
00661 else {
00662 multiSelectionChanged();
00663 emit selectionChanged();
00664 }
00665 }
00666
00667 void KFileDialog::fileSelected(const KFileItem *i)
00668 {
00669 if (i && i->isDir())
00670 return;
00671
00672 if ( (ops->mode() & KFile::Files) != KFile::Files ) {
00673 if ( !i )
00674 return;
00675
00676 d->url = i->url();
00677 locationEdit->setCurrentItem( 0 );
00678 locationEdit->setEditText( i->name() );
00679 locationEdit->lineEdit()->setEdited( false );
00680 }
00681 else {
00682 multiSelectionChanged();
00683 emit selectionChanged();
00684 }
00685 slotOk();
00686 }
00687
00688
00689
00690
00691 void KFileDialog::multiSelectionChanged()
00692 {
00693 if ( d->completionLock )
00694 return;
00695
00696 locationEdit->lineEdit()->setEdited( false );
00697 KFileItem *item;
00698 const KFileItemList *list = ops->selectedItems();
00699 if ( !list ) {
00700 locationEdit->clearEdit();
00701 return;
00702 }
00703
00704 static const QString &begin = KGlobal::staticQString(" \"");
00705 KFileItemListIterator it ( *list );
00706 QString text;
00707 while ( (item = it.current()) ) {
00708 text.append( begin ).append( item->name() ).append( '\"' );
00709 ++it;
00710 }
00711 locationEdit->setCurrentItem( 0 );
00712 locationEdit->setEditText( text.stripWhiteSpace() );
00713 }
00714
00715 void KFileDialog::init(const QString& startDir, const QString& filter, QWidget* widget)
00716 {
00717 initStatic();
00718 d = new KFileDialogPrivate();
00719
00720 d->boxLayout = 0;
00721 d->keepLocation = false;
00722 d->operationMode = Opening;
00723 d->hasDefaultFilter = false;
00724 d->hasView = false;
00725 d->mainWidget = new QWidget( this, "KFileDialog::mainWidget");
00726 setMainWidget( d->mainWidget );
00727 d->okButton = new KPushButton( KStdGuiItem::ok(), d->mainWidget );
00728 d->okButton->setDefault( true );
00729 d->cancelButton = new KPushButton(KStdGuiItem::cancel(), d->mainWidget);
00730 connect( d->okButton, SIGNAL( clicked() ), SLOT( slotOk() ));
00731 connect( d->cancelButton, SIGNAL( clicked() ), SLOT( slotCancel() ));
00732 d->customWidget = widget;
00733 d->urlBar = 0;
00734 KConfig *config = KGlobal::config();
00735 KConfigGroupSaver cs( config, ConfigGroup );
00736 d->initializeSpeedbar = config->readBoolEntry( "Set speedbar defaults",
00737 true );
00738 d->completionLock = false;
00739
00740 QtMsgHandler oldHandler = qInstallMsgHandler( silenceQToolBar );
00741 toolbar = new KToolBar( d->mainWidget, "KFileDialog::toolbar", true);
00742 toolbar->setFlat(true);
00743 qInstallMsgHandler( oldHandler );
00744
00745 QString autocompletionWhatsThisText = i18n("<p>While typing in the text area, you may be presented "
00746 "with possible matches. "
00747 "This feature can be controlled by clicking with the right mouse button "
00748 "and selecting a preferred mode from the <b>Text Completion</b> menu.") + "</qt>";
00749 d->pathCombo = new KURLComboBox( KURLComboBox::Directories, true,
00750 toolbar, "path combo" );
00751 QToolTip::add( d->pathCombo, i18n("Often used directories") );
00752 QWhatsThis::add( d->pathCombo, i18n("<qt>Commonly used locations are listed here. "
00753 "This includes standard locations, such as your home directory, as well as "
00754 "locations that have been visited recently.") + autocompletionWhatsThisText);
00755
00756 KURL u;
00757 u.setPath( QDir::rootDirPath() );
00758 QString text = i18n("Root Directory: %1").arg( u.path() );
00759 d->pathCombo->addDefaultURL( u,
00760 KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00761 text );
00762
00763 u.setPath( QDir::homeDirPath() );
00764 text = i18n("Home Directory: %1").arg( u.path( +1 ) );
00765 d->pathCombo->addDefaultURL( u, KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00766 text );
00767
00768 KURL docPath;
00769 docPath.setPath( KGlobalSettings::documentPath() );
00770 if ( u.path(+1) != docPath.path(+1) ) {
00771 text = i18n("Documents: %1").arg( docPath.path( +1 ) );
00772 d->pathCombo->addDefaultURL( docPath,
00773 KMimeType::pixmapForURL( docPath, 0, KIcon::Small ),
00774 text );
00775 }
00776
00777 u.setPath( KGlobalSettings::desktopPath() );
00778 text = i18n("Desktop: %1").arg( u.path( +1 ) );
00779 d->pathCombo->addDefaultURL( u,
00780 KMimeType::pixmapForURL( u, 0, KIcon::Small ),
00781 text );
00782
00783 u.setPath( "/tmp" );
00784
00785 d->url = getStartURL( startDir, d->fileClass );
00786 d->selection = d->url.url();
00787
00788
00789 if ( d->url.isLocalFile() )
00790 {
00791 if ( !QFile::exists( d->url.path() ) )
00792 {
00793 d->url = d->url.upURL();
00794 QDir dir( d->url.path() );
00795 while ( !dir.exists() )
00796 {
00797 d->url = d->url.upURL();
00798 dir.setPath( d->url.path() );
00799 }
00800 }
00801 }
00802
00803 ops = new KDirOperator(d->url, d->mainWidget, "KFileDialog::ops");
00804 ops->setOnlyDoubleClickSelectsFiles( true );
00805 connect(ops, SIGNAL(urlEntered(const KURL&)),
00806 SLOT(urlEntered(const KURL&)));
00807 connect(ops, SIGNAL(fileHighlighted(const KFileItem *)),
00808 SLOT(fileHighlighted(const KFileItem *)));
00809 connect(ops, SIGNAL(fileSelected(const KFileItem *)),
00810 SLOT(fileSelected(const KFileItem *)));
00811 connect(ops, SIGNAL(finishedLoading()),
00812 SLOT(slotLoadingFinished()));
00813
00814 ops->setupMenu(KDirOperator::SortActions |
00815 KDirOperator::FileActions |
00816 KDirOperator::ViewActions);
00817 KActionCollection *coll = ops->actionCollection();
00818
00819
00820 coll->action( "up" )->plug( toolbar );
00821 coll->action( "up" )->setWhatsThis(i18n("<qt>Click this button to enter the parent directory.<p>"
00822 "For instance, if the current location is file:/home/%1 clicking this "
00823 "button will take you to file:/home.</qt>").arg(getlogin()));
00824 coll->action( "back" )->plug( toolbar );
00825 coll->action( "back" )->setWhatsThis(i18n("Click this button to move backwards one step in the browsing history."));
00826 coll->action( "forward" )->plug( toolbar );
00827 coll->action( "forward" )->setWhatsThis(i18n("Click this button to move forward one step in the browsing history."));
00828 coll->action( "reload" )->plug( toolbar );
00829 coll->action( "reload" )->setWhatsThis(i18n("Click this button to reload the contents of the current location."));
00830 coll->action( "mkdir" )->setShortcut(Key_F10);
00831 coll->action( "mkdir" )->plug( toolbar );
00832 coll->action( "mkdir" )->setWhatsThis(i18n("Click this button to create a new directory."));
00833
00834 d->bookmarkHandler = new KFileBookmarkHandler( this );
00835 toolbar->insertButton(QString::fromLatin1("bookmark"),
00836 (int)HOTLIST_BUTTON, true,
00837 i18n("Bookmarks"));
00838 toolbar->getButton(HOTLIST_BUTTON)->setPopup( d->bookmarkHandler->menu(),
00839 true);
00840 QWhatsThis::add(toolbar->getButton(HOTLIST_BUTTON),
00841 i18n("<qt>This button allows you to bookmark specific locations. "
00842 "Click on this button to open the bookmark menu where you may add, "
00843 "edit or select a bookmark.<p>"
00844 "These bookmarks are specific to the file dialog, but otherwise operate "
00845 "like bookmarks elsewhere in KDE.</qt>"));
00846 connect( d->bookmarkHandler, SIGNAL( openURL( const QString& )),
00847 SLOT( enterURL( const QString& )));
00848
00849 KToggleAction *showSidebarAction =
00850 new KToggleAction(i18n("Show Quick Access Navigation Panel"), Key_F9, coll,"toggleSpeedbar");
00851 connect( showSidebarAction, SIGNAL( toggled( bool ) ),
00852 SLOT( toggleSpeedbar( bool )) );
00853
00854 KActionMenu *menu = new KActionMenu( i18n("Configure"), "configure", this, "extra menu" );
00855 menu->setWhatsThis(i18n("<qt>This is the configuration menu for the file dialog. "
00856 "Various options can be accessed from this menu including: <ul>"
00857 "<li>how files are sorted in the list</li>"
00858 "<li>types of view, including icon and list</li>"
00859 "<li>showing of hidden files</li>"
00860 "<li>the Quick Access navigation panel</li>"
00861 "<li>file previews</li>"
00862 "<li>separating directories from files</li></ul></qt>"));
00863 menu->insert( coll->action( "sorting menu" ));
00864 menu->insert( coll->action( "separator" ));
00865 coll->action( "short view" )->setShortcut(Key_F6);
00866 menu->insert( coll->action( "short view" ));
00867 coll->action( "detailed view" )->setShortcut(Key_F7);
00868 menu->insert( coll->action( "detailed view" ));
00869 menu->insert( coll->action( "separator" ));
00870 coll->action( "show hidden" )->setShortcut(Key_F8);
00871 menu->insert( coll->action( "show hidden" ));
00872 menu->insert( showSidebarAction );
00873 coll->action( "preview" )->setShortcut(Key_F11);
00874 menu->insert( coll->action( "preview" ));
00875 coll->action( "separate dirs" )->setShortcut(Key_F12);
00876 menu->insert( coll->action( "separate dirs" ));
00877
00878 menu->setDelayed( false );
00879 connect( menu->popupMenu(), SIGNAL( aboutToShow() ),
00880 ops, SLOT( updateSelectionDependentActions() ));
00881 menu->plug( toolbar );
00882
00883
00884
00885
00886
00887 QWidget *spacerWidget = new QWidget(toolbar);
00888 spacerWidget->setMinimumWidth(spacingHint());
00889 spacerWidget->setMaximumWidth(spacingHint());
00890 d->m_pathComboIndex = toolbar->insertWidget(-1, -1, spacerWidget);
00891 toolbar->insertWidget(PATH_COMBO, 0, d->pathCombo);
00892
00893
00894 toolbar->setItemAutoSized (PATH_COMBO);
00895 toolbar->setIconText(KToolBar::IconOnly);
00896 toolbar->setBarPos(KToolBar::Top);
00897 toolbar->setMovingEnabled(false);
00898 toolbar->adjustSize();
00899
00900 d->pathCombo->setCompletionObject( ops->dirCompletionObject(), false );
00901
00902 connect( d->pathCombo, SIGNAL( urlActivated( const KURL& )),
00903 this, SLOT( enterURL( const KURL& ) ));
00904 connect( d->pathCombo, SIGNAL( returnPressed( const QString& )),
00905 this, SLOT( enterURL( const QString& ) ));
00906 connect( d->pathCombo, SIGNAL(textChanged( const QString& )),
00907 SLOT( pathComboChanged( const QString& ) ));
00908 connect( d->pathCombo, SIGNAL( completion( const QString& )),
00909 SLOT( dirCompletion( const QString& )));
00910 connect( d->pathCombo, SIGNAL( textRotation(KCompletionBase::KeyBindingType) ),
00911 d->pathCombo, SLOT( rotateText(KCompletionBase::KeyBindingType) ));
00912
00913 QString whatsThisText;
00914 if (d->operationMode == KFileDialog::Saving)
00915 {
00916 whatsThisText = i18n("<qt>This is the name to save the file as.") +
00917 autocompletionWhatsThisText;
00918 }
00919 else if (ops->mode() & KFile::Files)
00920 {
00921 whatsThisText = i18n("<qt>This is the list of files to open. More than "
00922 "one file can be specified by listing several "
00923 "files, separated by spaces.") +
00924 autocompletionWhatsThisText;
00925 }
00926 else
00927 {
00928 whatsThisText = i18n("<qt>This is the name of the file to open.") +
00929 autocompletionWhatsThisText;
00930 }
00931
00932
00933 d->locationLabel = new QLabel(i18n("&Location:"), d->mainWidget);
00934 QWhatsThis::add(d->locationLabel, whatsThisText);
00935 locationEdit = new KURLComboBox(KURLComboBox::Files, true,
00936 d->mainWidget, "LocationEdit");
00937 d->locationLabel->setBuddy(locationEdit);
00938 QWhatsThis::add(locationEdit, whatsThisText);
00939
00940 locationEdit->setHandleSignals( true );
00941 (void) locationEdit->completionBox();
00942
00943 locationEdit->setFocus();
00944
00945
00946 locationEdit->setCompletionObject( ops->completionObject(), false );
00947
00948 connect( locationEdit, SIGNAL( returnPressed() ),
00949 this, SLOT( slotOk()));
00950 connect(locationEdit, SIGNAL( activated( const QString& )),
00951 this, SLOT( locationActivated( const QString& ) ));
00952 connect( locationEdit, SIGNAL( completion( const QString& )),
00953 SLOT( fileCompletion( const QString& )));
00954 connect( locationEdit, SIGNAL( textRotation(KCompletionBase::KeyBindingType) ),
00955 locationEdit, SLOT( rotateText(KCompletionBase::KeyBindingType) ));
00956
00957
00958 whatsThisText = i18n("<qt>This is the filter to apply to the file list. "
00959 "File names that do not match the filter will not be shown.<p>"
00960 "You may select from one of the preset filters in the "
00961 "drop down menu, or you may enter a custom filter "
00962 "directly into the text area.<p>"
00963 "Wildcards such as * and ? are allowed.</qt>");
00964 d->filterLabel = new QLabel(i18n("&Filter:"), d->mainWidget);
00965 QWhatsThis::add(d->filterLabel, whatsThisText);
00966 filterWidget = new KFileFilterCombo(d->mainWidget,
00967 "KFileDialog::filterwidget");
00968 QWhatsThis::add(filterWidget, whatsThisText);
00969 setFilter(filter);
00970 d->filterLabel->setBuddy(filterWidget);
00971 connect(filterWidget, SIGNAL(filterChanged()), SLOT(slotFilterChanged()));
00972
00973 initGUI();
00974
00975 readRecentFiles( config );
00976
00977 adjustSize();
00978
00979
00980
00981 d->completionLock = true;
00982 ops->setViewConfig( config, ConfigGroup );
00983 readConfig( config, ConfigGroup );
00984 setSelection(d->selection);
00985 d->completionLock = false;
00986 }
00987
00988 void KFileDialog::initSpeedbar()
00989 {
00990 d->urlBar = new KFileSpeedBar( d->mainWidget, "url bar" );
00991 connect( d->urlBar, SIGNAL( activated( const KURL& )),
00992 SLOT( enterURL( const KURL& )) );
00993
00994
00995
00996
00997
00998 d->urlBar->setCurrentItem( d->url );
00999
01000 d->urlBarLayout->insertWidget( 0, d->urlBar );
01001 }
01002
01003 void KFileDialog::initGUI()
01004 {
01005 delete d->boxLayout;
01006
01007 d->boxLayout = new QVBoxLayout( d->mainWidget, 0, KDialog::spacingHint());
01008 d->boxLayout->addWidget(toolbar, AlignTop);
01009
01010 d->urlBarLayout = new QHBoxLayout( d->boxLayout );
01011 QVBoxLayout *vbox = new QVBoxLayout( d->urlBarLayout );
01012
01013 vbox->addWidget(ops, 4);
01014 vbox->addSpacing(3);
01015
01016 QGridLayout* lafBox= new QGridLayout(2, 3, KDialog::spacingHint());
01017 vbox->addLayout(lafBox, 0);
01018 lafBox->addWidget(d->locationLabel, 0, 0, AlignVCenter);
01019 lafBox->addWidget(locationEdit, 0, 1, AlignVCenter);
01020 lafBox->addWidget(d->okButton, 0, 2, AlignVCenter);
01021
01022 lafBox->addWidget(d->filterLabel, 1, 0, AlignVCenter);
01023 lafBox->addWidget(filterWidget, 1, 1, AlignVCenter);
01024 lafBox->addWidget(d->cancelButton, 1, 2, AlignVCenter);
01025
01026 lafBox->setColStretch(1, 4);
01027
01028 vbox->addSpacing(3);
01029
01030 setTabOrder(ops, locationEdit);
01031 setTabOrder(locationEdit, filterWidget);
01032 setTabOrder(filterWidget, d->okButton);
01033 setTabOrder(d->okButton, d->cancelButton);
01034 setTabOrder(d->cancelButton, d->pathCombo);
01035 setTabOrder(d->pathCombo, ops);
01036
01037
01038 if ( d->customWidget != 0 )
01039 {
01040
01041
01042
01043 d->customWidget->reparent( d->mainWidget, QPoint() );
01044
01045 vbox->addWidget( d->customWidget );
01046
01047
01048
01049
01050
01051
01052 setTabOrder(d->cancelButton, d->customWidget);
01053 setTabOrder(d->customWidget, d->pathCombo);
01054 }
01055 else
01056 {
01057 setTabOrder(d->cancelButton, d->pathCombo);
01058 }
01059
01060 setTabOrder(d->pathCombo, ops);
01061 }
01062
01063 void KFileDialog::slotFilterChanged()
01064 {
01065 QString filter = filterWidget->currentFilter();
01066 ops->clearFilter();
01067
01068 if ( filter.find( '/' ) > -1 ) {
01069 QStringList types = QStringList::split( " ", filter );
01070 types.prepend( "inode/directory" );
01071 ops->setMimeFilter( types );
01072 }
01073 else
01074 ops->setNameFilter( filter );
01075
01076 ops->updateDir();
01077 emit filterChanged( filter );
01078 }
01079
01080
01081 void KFileDialog::pathComboChanged( const QString& txt )
01082 {
01083 if ( d->completionLock )
01084 return;
01085
01086 static const QString& localRoot = KGlobal::staticQString("file:/");
01087 KURLComboBox *combo = d->pathCombo;
01088 QString text = txt;
01089 QString newText = text.left(combo->cursorPosition() -1);
01090 KURL url;
01091 if ( text.at( 0 ) == '/' )
01092 url.setPath( text );
01093 else
01094 url = text;
01095
01096
01097
01098 if ( url.isMalformed() ||
01099 !KProtocolInfo::supportsListing( url.protocol() ) ||
01100 ( !url.url().startsWith( localRoot ) &&
01101 ( url.directory().isNull() || url.host().isNull()) )) {
01102 d->completionHack = newText;
01103 return;
01104 }
01105
01106
01107
01108 if ( combo->cursorPosition() != (int) combo->currentText().length() ) {
01109 d->completionHack = newText;
01110 return;
01111 }
01112
01113
01114 if ( autoDirectoryFollowing && d->completionHack.startsWith( newText ) ) {
01115
01116
01117
01118 int l = text.length() - 1;
01119 while (!text.isEmpty() && text[l] != '/')
01120 l--;
01121
01122 KURL newLocation(text.left(l+1));
01123
01124 if ( !newLocation.isMalformed() && newLocation != ops->url() ) {
01125 setURL(newLocation, true);
01126 combo->setEditText(text);
01127 }
01128 }
01129
01130
01131 else if ( autoDirectoryFollowing &&
01132 text.at(text.length()-1) == '/' && ops->url() != text ) {
01133 d->selection = QString::null;
01134 setURL( text, false );
01135 }
01136
01137 d->completionHack = newText;
01138 }
01139
01140
01141 void KFileDialog::setURL(const KURL& url, bool clearforward)
01142 {
01143 d->selection = QString::null;
01144 ops->setURL( url, clearforward);
01145 }
01146
01147
01148 void KFileDialog::urlEntered(const KURL& url)
01149 {
01150 QString filename = locationEdit->currentText();
01151 d->selection = QString::null;
01152
01153 if ( d->pathCombo->count() != 0 ) {
01154 d->pathCombo->setURL( url );
01155 }
01156
01157 locationEdit->blockSignals( true );
01158 locationEdit->setCurrentItem( 0 );
01159 if ( d->keepLocation )
01160 locationEdit->setEditText( filename );
01161
01162 locationEdit->blockSignals( false );
01163 d->completionHack = d->pathCombo->currentText();
01164
01165 if ( d->urlBar )
01166 d->urlBar->setCurrentItem( url );
01167 }
01168
01169 void KFileDialog::locationActivated( const QString& url )
01170 {
01171
01172
01173
01174
01175
01176 if (!locationEdit->lineEdit()->edited())
01177 setSelection( url );
01178 }
01179
01180 void KFileDialog::enterURL( const KURL& url)
01181 {
01182 setURL( url );
01183 locationEdit->setFocus();
01184 }
01185
01186 void KFileDialog::enterURL( const QString& url )
01187 {
01188 setURL( url );
01189 locationEdit->setFocus();
01190 }
01191
01192 void KFileDialog::toolbarCallback(int)
01193 {
01194
01195
01196
01197
01198 }
01199
01200
01201 void KFileDialog::setSelection(const QString& url)
01202 {
01203 kdDebug(kfile_area) << "setSelection " << url << endl;
01204
01205 if (url.isEmpty()) {
01206 d->selection = QString::null;
01207 return;
01208 }
01209
01210
01211 KURL u;
01212 if ( KURL::isRelativeURL(url) )
01213 {
01214 if (!url.isEmpty() && url[0] == '/' )
01215 u.setPath( url );
01216 else
01217 {
01218 u = ops->url();
01219 u.addPath( url );
01220 }
01221 } else
01222 u = url;
01223
01224 if (u.isMalformed()) {
01225 kdWarning() << url << " is not a correct argument for setSelection!" << endl;
01226 return;
01227 }
01228
01229
01230
01231
01232 KFileItem i(KFileItem::Unknown, KFileItem::Unknown, u, true );
01233
01234 if ( i.isDir() && u.isLocalFile() && QFile::exists( u.path() ) ) {
01235
01236
01237
01238
01239 setURL(u, true);
01240 }
01241 else {
01242 QString filename = u.url();
01243 int sep = filename.findRev('/');
01244 if (sep >= 0) {
01245 if ( KProtocolInfo::supportsListing( u.protocol() ))
01246 setURL(filename.left(sep), true);
01247
01248
01249
01250 filename = u.fileName();
01251 kdDebug(kfile_area) << "filename " << filename << endl;
01252 d->selection = filename;
01253 locationEdit->setCurrentItem( 0 );
01254 locationEdit->setEditText( filename );
01255
01256
01257
01258
01259
01260
01261
01262
01263 locationEdit->lineEdit()->setEdited( true );
01264 }
01265
01266 d->url = ops->url();
01267 d->url.addPath(filename);
01268 }
01269 }
01270
01271 void KFileDialog::slotLoadingFinished()
01272 {
01273 if ( !d->selection.isNull() )
01274 ops->setCurrentItem( d->selection );
01275 }
01276
01277
01278 void KFileDialog::dirCompletion( const QString& dir )
01279 {
01280
01281 if ( ops->dirCompletionObject()->completionMode() ==
01282 KGlobalSettings::CompletionPopup )
01283 return;
01284
01285 QString base = ops->url().url();
01286
01287
01288 d->selection = QString::null;
01289
01290 KURL url;
01291 if ( dir.at( 0 ) == '/' )
01292 url.setPath( dir );
01293 else
01294 url = dir;
01295
01296 if ( url.isMalformed() )
01297 return;
01298
01299 d->completionLock = true;
01300
01301 if (url.url().startsWith( base )) {
01302 QString complete = ops->makeDirCompletion( url.fileName(false) );
01303
01304 if (!complete.isNull()) {
01305 if(!base.endsWith("/"))
01306 base.append('/');
01307 QString newText = base + complete;
01308 QString fileProt = QString::fromLatin1( "file:" );
01309
01310 if ( dir.startsWith( fileProt ) != newText.startsWith( fileProt ))
01311 newText = newText.mid( 5 );
01312
01313 d->pathCombo->setCompletedText( newText );
01314 d->url = newText;
01315 }
01316 }
01317 d->completionLock = false;
01318 }
01319
01320
01321 void KFileDialog::fileCompletion( const QString& file )
01322 {
01323 d->completionLock = true;
01324 QString text = ops->makeCompletion( file );
01325 if ( !text.isEmpty() ) {
01326 KCompletion *comp = ops->completionObject();
01327 if ( comp->completionMode() == KGlobalSettings::CompletionPopup ||
01328 comp->completionMode() == KGlobalSettings::CompletionPopupAuto )
01329 locationEdit->setCompletedItems( comp->allMatches() );
01330 else
01331 locationEdit->setCompletedText( text );
01332 }
01333 else
01334 if (locationEdit->completionMode() == KGlobalSettings::CompletionPopup ||
01335 locationEdit->completionMode() == KGlobalSettings::CompletionPopupAuto )
01336 locationEdit->completionBox()->hide();
01337
01338 d->completionLock = false;
01339 }
01340
01341 void KFileDialog::updateStatusLine(int , int )
01342 {
01343 kdWarning() << "KFileDialog::updateStatusLine is deprecated! The status line no longer exists. Do not try and use it!" << endl;
01344 }
01345
01346 QString KFileDialog::getOpenFileName(const QString& startDir,
01347 const QString& filter,
01348 QWidget *parent, const QString& caption)
01349 {
01350 KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01351 dlg.setOperationMode( Opening );
01352
01353 dlg.setMode( KFile::File | KFile::LocalOnly );
01354 dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01355
01356 dlg.ops->clearHistory();
01357 dlg.exec();
01358
01359 return dlg.selectedFile();
01360 }
01361
01362 QStringList KFileDialog::getOpenFileNames(const QString& startDir,
01363 const QString& filter,
01364 QWidget *parent,
01365 const QString& caption)
01366 {
01367 KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01368 dlg.setOperationMode( Opening );
01369
01370 dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01371 dlg.setMode(KFile::Files | KFile::LocalOnly);
01372 dlg.ops->clearHistory();
01373 dlg.exec();
01374
01375 return dlg.selectedFiles();
01376 }
01377
01378 KURL KFileDialog::getOpenURL(const QString& startDir, const QString& filter,
01379 QWidget *parent, const QString& caption)
01380 {
01381 KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01382 dlg.setOperationMode( Opening );
01383
01384 dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01385 dlg.setMode( KFile::File );
01386 dlg.ops->clearHistory();
01387 dlg.exec();
01388
01389 return dlg.selectedURL();
01390 }
01391
01392 KURL::List KFileDialog::getOpenURLs(const QString& startDir,
01393 const QString& filter,
01394 QWidget *parent,
01395 const QString& caption)
01396 {
01397 KFileDialog dlg(startDir, filter, parent, "filedialog", true);
01398 dlg.setOperationMode( Opening );
01399
01400 dlg.setCaption(caption.isNull() ? i18n("Open") : caption);
01401 dlg.setMode(KFile::Files);
01402 dlg.ops->clearHistory();
01403 dlg.exec();
01404
01405 return dlg.selectedURLs();
01406 }
01407
01408 KURL KFileDialog::getExistingURL(const QString& startDir,
01409 QWidget *parent,
01410 const QString& caption)
01411 {
01412 KFileDialog dlg(startDir, QString::null, parent, "filedialog", true);
01413 dlg.setMode(KFile::Directory | KFile::ExistingOnly);
01414
01415 dlg.setFilter( QString::null );
01416 dlg.ops->clearHistory();
01417 dlg.setCaption(caption.isNull() ? i18n("Select Directory") : caption);
01418 dlg.exec();
01419
01420 return dlg.selectedURL();
01421 }
01422
01423 QString KFileDialog::getExistingDirectory(const QString& startDir,
01424 QWidget *parent,
01425 const QString& caption)
01426 {
01427 KFileDialog dlg(startDir, QString::null, parent, "filedialog", true);
01428 dlg.setMode(KFile::Directory | KFile::LocalOnly | KFile::ExistingOnly);
01429
01430 dlg.setFilter( QString::null );
01431 dlg.ops->clearHistory();
01432 dlg.setCaption(caption.isNull() ? i18n("Select Directory") : caption);
01433 dlg.exec();
01434
01435 return dlg.selectedFile();
01436 }
01437
01438 KURL KFileDialog::getImageOpenURL( const QString& startDir, QWidget *parent,
01439 const QString& caption)
01440 {
01441 QStringList mimetypes = KImageIO::mimeTypes( KImageIO::Reading );
01442 KFileDialog dlg(startDir,
01443 mimetypes.join(" "),
01444 parent, "filedialog", true);
01445 dlg.setOperationMode( Opening );
01446 dlg.setCaption( caption.isNull() ? i18n("Open") : caption );
01447 dlg.setMode( KFile::File );
01448
01449 KImageFilePreview *ip = new KImageFilePreview( &dlg );
01450 dlg.setPreviewWidget( ip );
01451 dlg.exec();
01452
01453 return dlg.selectedURL();
01454 }
01455
01456 KURL KFileDialog::selectedURL() const
01457 {
01458 if ( result() == QDialog::Accepted )
01459 return d->url;
01460 else
01461 return KURL();
01462 }
01463
01464 KURL::List KFileDialog::selectedURLs() const
01465 {
01466 KURL::List list;
01467 if ( result() == QDialog::Accepted ) {
01468 if ( (ops->mode() & KFile::Files) == KFile::Files )
01469 list = parseSelectedURLs();
01470 else
01471 list.append( d->url );
01472 }
01473 return list;
01474 }
01475
01476
01477 KURL::List& KFileDialog::parseSelectedURLs() const
01478 {
01479 if ( d->filenames.isEmpty() ) {
01480 return d->urlList;
01481 }
01482
01483 d->urlList.clear();
01484 if ( d->filenames.contains( '/' )) {
01485 static const QString &prot = KGlobal::staticQString(":/");
01486 KURL u;
01487 if ( d->filenames.find( prot ) != -1 )
01488 u = d->filenames;
01489 else
01490 u.setPath( d->filenames );
01491
01492 if ( !u.isMalformed() )
01493 d->urlList.append( u );
01494 else
01495 KMessageBox::error( d->mainWidget,
01496 i18n("The chosen filename(s) don't\nappear to be valid."), i18n("Invalid Filename(s)") );
01497 }
01498
01499 else
01500 d->urlList = tokenize( d->filenames );
01501
01502 d->filenames = QString::null;
01503
01504 return d->urlList;
01505 }
01506
01507
01508
01509 KURL::List KFileDialog::tokenize( const QString& line ) const
01510 {
01511 KURL::List urls;
01512 KURL u( ops->url() );
01513 QString name;
01514
01515 int count = line.contains( '"' );
01516 if ( count == 0 ) {
01517 u.setFileName( line );
01518 if ( !u.isMalformed() )
01519 urls.append( u );
01520
01521 return urls;
01522 }
01523
01524 if ( (count % 2) == 1 ) {
01525 QWidget *that = const_cast<KFileDialog *>(this);
01526 KMessageBox::sorry(that, i18n("The requested filenames\n%1\ndon't look valid to me.\nMake sure every filename is enclosed in double quotes.").arg(line), i18n("Filename Error"));
01527 return urls;
01528 }
01529
01530 int start = 0;
01531 int index1 = -1, index2 = -1;
01532 while ( true ) {
01533 index1 = line.find( '"', start );
01534 index2 = line.find( '"', index1 + 1 );
01535
01536 if ( index1 < 0 )
01537 break;
01538
01539
01540 name = line.mid( index1 + 1, index2 - index1 - 1 );
01541 u.setFileName( name );
01542 if ( !u.isMalformed() )
01543 urls.append( u );
01544
01545 start = index2 + 1;
01546 }
01547 return urls;
01548 }
01549
01550
01551 QString KFileDialog::selectedFile() const
01552 {
01553 if ( result() == QDialog::Accepted )
01554 {
01555 if (d->url.isLocalFile())
01556 return d->url.path();
01557 }
01558 return QString::null;
01559 }
01560
01561 QStringList KFileDialog::selectedFiles() const
01562 {
01563 QStringList list;
01564
01565 if ( result() == QDialog::Accepted ) {
01566 if ( (ops->mode() & KFile::Files) == KFile::Files ) {
01567 KURL::List urls = parseSelectedURLs();
01568 QValueListConstIterator<KURL> it = urls.begin();
01569 while ( it != urls.end() ) {
01570 if ( (*it).isLocalFile() )
01571 list.append( (*it).path() );
01572 ++it;
01573 }
01574 }
01575
01576 else {
01577 if ( d->url.isLocalFile() )
01578 list.append( d->url.path() );
01579 }
01580 }
01581
01582 return list;
01583 }
01584
01585 KURL KFileDialog::baseURL() const
01586 {
01587 return ops->url();
01588 }
01589
01590 QString KFileDialog::getSaveFileName(const QString& dir, const QString& filter,
01591 QWidget *parent,
01592 const QString& caption)
01593 {
01594 bool specialDir = dir.at(0) == ':';
01595 KFileDialog dlg( specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01596 if ( !specialDir )
01597 dlg.setSelection( dir );
01598
01599 dlg.setOperationMode( Saving );
01600 dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01601
01602 dlg.exec();
01603
01604 QString filename = dlg.selectedFile();
01605 if (!filename.isEmpty())
01606 KRecentDocument::add(filename);
01607
01608 return filename;
01609 }
01610
01611 KURL KFileDialog::getSaveURL(const QString& dir, const QString& filter,
01612 QWidget *parent, const QString& caption)
01613 {
01614 bool specialDir = dir.at(0) == ':';
01615 KFileDialog dlg(specialDir ? dir : QString::null, filter, parent, "filedialog", true);
01616 if ( !specialDir )
01617 dlg.setSelection( dir );
01618
01619 dlg.setCaption(caption.isNull() ? i18n("Save As") : caption);
01620 dlg.setOperationMode( Saving );
01621
01622 dlg.exec();
01623
01624 KURL url = dlg.selectedURL();
01625 if (!url.isMalformed())
01626 KRecentDocument::add( url );
01627
01628 return url;
01629 }
01630
01631 void KFileDialog::show()
01632 {
01633 if ( !d->hasView ) {
01634 ops->setView(KFile::Default);
01635 ops->clearHistory();
01636 d->hasView = true;
01637 }
01638
01639 KDialogBase::show();
01640 }
01641
01642 void KFileDialog::setMode( KFile::Mode m )
01643 {
01644 ops->setMode(m);
01645 if ( ops->dirOnlyMode() ) {
01646 filterWidget->setDefaultFilter( i18n("*|All Directories") );
01647 }
01648 else {
01649 filterWidget->setDefaultFilter( i18n("*|All Files") );
01650 }
01651 }
01652
01653 void KFileDialog::setMode( unsigned int m )
01654 {
01655 setMode(static_cast<KFile::Mode>( m ));
01656 }
01657
01658 KFile::Mode KFileDialog::mode() const
01659 {
01660 return ops->mode();
01661 }
01662
01663
01664 void KFileDialog::readConfig( KConfig *kc, const QString& group )
01665 {
01666 if ( !kc )
01667 return;
01668
01669 QString oldGroup = kc->group();
01670 if ( !group.isEmpty() )
01671 kc->setGroup( group );
01672
01673 ops->readConfig( kc, group );
01674
01675 KURLComboBox *combo = d->pathCombo;
01676 combo->setURLs( kc->readPathListEntry( RecentURLs ), KURLComboBox::RemoveTop );
01677 combo->setMaxItems( kc->readNumEntry( RecentURLsNumber,
01678 DefaultRecentURLsNumber ) );
01679 combo->setURL( ops->url() );
01680 autoDirectoryFollowing = kc->readBoolEntry( AutoDirectoryFollowing,
01681 DefaultDirectoryFollowing );
01682
01683 KGlobalSettings::Completion cm = (KGlobalSettings::Completion)
01684 kc->readNumEntry( PathComboCompletionMode,
01685 KGlobalSettings::CompletionAuto );
01686 if ( cm != KGlobalSettings::completionMode() )
01687 combo->setCompletionMode( cm );
01688
01689 cm = (KGlobalSettings::Completion)
01690 kc->readNumEntry( LocationComboCompletionMode,
01691 KGlobalSettings::CompletionAuto );
01692 if ( cm != KGlobalSettings::completionMode() )
01693 locationEdit->setCompletionMode( cm );
01694
01695
01696 toggleSpeedbar( kc->readBoolEntry(ShowSpeedbar, true) );
01697
01698 int w1 = minimumSize().width();
01699 int w2 = toolbar->sizeHint().width() + 10;
01700 if (w1 < w2)
01701 setMinimumWidth(w2);
01702
01703 QSize size = configDialogSize( group );
01704 resize( size );
01705 kc->setGroup( oldGroup );
01706 }
01707
01708 void KFileDialog::writeConfig( KConfig *kc, const QString& group )
01709 {
01710 if ( !kc )
01711 return;
01712
01713 QString oldGroup = kc->group();
01714 if ( !group.isEmpty() )
01715 kc->setGroup( group );
01716
01717 kc->writeEntry( RecentURLs, d->pathCombo->urls() );
01718 saveDialogSize( group, true );
01719 kc->writeEntry( PathComboCompletionMode, static_cast<int>(d->pathCombo->completionMode()) );
01720 kc->writeEntry(LocationComboCompletionMode, static_cast<int>(locationEdit->completionMode()) );
01721 kc->writeEntry( ShowSpeedbar, d->urlBar && !d->urlBar->isHidden() );
01722
01723 ops->writeConfig( kc, group );
01724 kc->setGroup( oldGroup );
01725 }
01726
01727
01728 void KFileDialog::readRecentFiles( KConfig *kc )
01729 {
01730 QString oldGroup = kc->group();
01731 kc->setGroup( ConfigGroup );
01732
01733 locationEdit->setMaxItems( kc->readNumEntry( RecentFilesNumber,
01734 DefaultRecentURLsNumber ) );
01735 locationEdit->setURLs( kc->readPathListEntry( RecentFiles ),
01736 KURLComboBox::RemoveBottom );
01737 locationEdit->insertItem( QString::null, 0 );
01738 locationEdit->setCurrentItem( 0 );
01739
01740 kc->setGroup( oldGroup );
01741 }
01742
01743 void KFileDialog::saveRecentFiles( KConfig *kc )
01744 {
01745 QString oldGroup = kc->group();
01746 kc->setGroup( ConfigGroup );
01747
01748 kc->writeEntry( RecentFiles, locationEdit->urls() );
01749
01750 kc->setGroup( oldGroup );
01751 }
01752
01753 KPushButton * KFileDialog::okButton() const
01754 {
01755 return d->okButton;
01756 }
01757
01758 KPushButton * KFileDialog::cancelButton() const
01759 {
01760 return d->cancelButton;
01761 }
01762
01763 void KFileDialog::slotCancel()
01764 {
01765 ops->close();
01766 KDialogBase::slotCancel();
01767 }
01768
01769 void KFileDialog::setKeepLocation( bool keep )
01770 {
01771 d->keepLocation = keep;
01772 }
01773
01774 bool KFileDialog::keepsLocation() const
01775 {
01776 return d->keepLocation;
01777 }
01778
01779 void KFileDialog::setOperationMode( OperationMode mode )
01780 {
01781 d->operationMode = mode;
01782 d->keepLocation = (mode == Saving);
01783 filterWidget->setEditable( !d->hasDefaultFilter || mode != Saving );
01784 d->okButton->setGuiItem( (mode == Saving) ? KStdGuiItem::save() : KStdGuiItem::ok() );
01785 }
01786
01787 KFileDialog::OperationMode KFileDialog::operationMode() const
01788 {
01789 return d->operationMode;
01790 }
01791
01792
01793 void KFileDialog::addToRecentDocuments()
01794 {
01795 int m = ops->mode();
01796
01797 if ( m & KFile::LocalOnly ) {
01798 QStringList files = selectedFiles();
01799 QStringList::ConstIterator it = files.begin();
01800 for ( ; it != files.end(); ++it )
01801 KRecentDocument::add( *it );
01802 }
01803
01804 else {
01805 KURL::List urls = selectedURLs();
01806 KURL::List::ConstIterator it = urls.begin();
01807 for ( ; it != urls.end(); ++it ) {
01808 if ( (*it).isValid() )
01809 KRecentDocument::add( *it );
01810 }
01811 }
01812 }
01813
01814 KActionCollection * KFileDialog::actionCollection() const
01815 {
01816 return ops->actionCollection();
01817 }
01818
01819 void KFileDialog::toggleSpeedbar( bool show )
01820 {
01821 if ( show )
01822 {
01823 if ( !d->urlBar )
01824 initSpeedbar();
01825
01826 d->urlBar->show();
01827
01828
01829 KURLBarItem *urlItem = static_cast<KURLBarItem*>( d->urlBar->listBox()->firstItem() );
01830 KURL homeURL;
01831 homeURL.setPath( QDir::homeDirPath() );
01832 while ( urlItem )
01833 {
01834 if ( homeURL.equals( urlItem->url(), true ) )
01835 {
01836 ops->actionCollection()->action( "home" )->unplug( toolbar );
01837 break;
01838 }
01839
01840 urlItem = static_cast<KURLBarItem*>( urlItem->next() );
01841 }
01842 }
01843 else
01844 {
01845 if (d->urlBar)
01846 d->urlBar->hide();
01847
01848 if ( !ops->actionCollection()->action( "home" )->isPlugged( toolbar ) )
01849 ops->actionCollection()->action( "home" )->plug( toolbar, 3 );
01850 }
01851
01852 static_cast<KToggleAction *>(actionCollection()->action("toggleSpeedbar"))->setChecked( show );
01853 }
01854
01855 int KFileDialog::pathComboIndex()
01856 {
01857 return d->m_pathComboIndex;
01858 }
01859
01860
01861 void KFileDialog::initStatic()
01862 {
01863 if ( lastDirectory )
01864 return;
01865
01866 lastDirectory = ldd.setObject(new KURL());
01867 }
01868
01869
01870 KURL KFileDialog::getStartURL( const QString& startDir,
01871 QString& recentDirClass )
01872 {
01873 initStatic();
01874
01875 recentDirClass = QString::null;
01876 KURL ret;
01877
01878 bool useDefaultStartDir = startDir.isEmpty();
01879 if ( !useDefaultStartDir )
01880 {
01881 if (startDir[0] == ':')
01882 {
01883 recentDirClass = startDir;
01884 ret = KURL::fromPathOrURL( KRecentDirs::dir(recentDirClass) );
01885 }
01886 else
01887 {
01888 ret = KCmdLineArgs::makeURL( QFile::encodeName(startDir) );
01889
01890 if ( !KProtocolInfo::supportsListing( ret.protocol() ) )
01891 useDefaultStartDir = true;
01892 }
01893 }
01894
01895 if ( useDefaultStartDir )
01896 {
01897 if (lastDirectory->isEmpty()) {
01898 *lastDirectory = KGlobalSettings::documentPath();
01899 KURL home;
01900 home.setPath( QDir::homeDirPath() );
01901
01902
01903
01904 if ( lastDirectory->path(+1) == home.path(+1) ||
01905 QDir::currentDirPath() != QDir::homeDirPath() )
01906 *lastDirectory = QDir::currentDirPath();
01907 }
01908 ret = *lastDirectory;
01909 }
01910
01911 return ret;
01912 }
01913
01914 void KFileDialog::virtual_hook( int id, void* data )
01915 { KDialogBase::virtual_hook( id, data ); }
01916
01917 #include "kfiledialog.moc"
01918 #include "kpreviewwidgetbase.moc"