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