00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kdirselectdialog.h"
00021
00022 #include <QtCore/QDir>
00023 #include <QtCore/QStringList>
00024 #include <QtGui/QLayout>
00025 #include <QtGui/QMenu>
00026
00027 #include <kactioncollection.h>
00028 #include <kapplication.h>
00029 #include <kauthorized.h>
00030 #include <kconfig.h>
00031 #include <kconfiggroup.h>
00032 #include <khistorycombobox.h>
00033 #include <kfiledialog.h>
00034 #include <kfiletreeview.h>
00035 #include <kfileitemdelegate.h>
00036 #include <kglobalsettings.h>
00037 #include <kicon.h>
00038 #include <kinputdialog.h>
00039 #include <kio/job.h>
00040 #include <kio/netaccess.h>
00041 #include <kio/renamedialog.h>
00042 #include <klocale.h>
00043 #include <kmessagebox.h>
00044 #include <krecentdirs.h>
00045 #include <ktoggleaction.h>
00046 #include <kurlcompletion.h>
00047 #include <kurlpixmapprovider.h>
00048
00049 #include <kdebug.h>
00050
00051 #include "kfileplacesview.h"
00052 #include "kfileplacesmodel.h"
00053
00054
00055 class KDirSelectDialog::Private
00056 {
00057 public:
00058 Private( bool localOnly, KDirSelectDialog *parent )
00059 : m_parent( parent ),
00060 m_localOnly( localOnly ),
00061 m_comboLocked( false ),
00062 m_urlCombo(0)
00063 {
00064 }
00065
00066 void readConfig(const KSharedConfigPtr &config, const QString& group);
00067 void saveConfig(KSharedConfigPtr config, const QString& group);
00068 void slotMkdir();
00069
00070 void _k_slotCurrentChanged();
00071 void _k_slotExpand(const QModelIndex&);
00072 void _k_slotUrlActivated(const QString&);
00073 void _k_slotComboTextChanged(const QString&);
00074 void _k_slotContextMenu(const QPoint&);
00075 void _k_slotUser1();
00076
00077 KDirSelectDialog *m_parent;
00078 bool m_localOnly : 1;
00079 bool m_comboLocked : 1;
00080 KUrl m_rootUrl;
00081 KUrl m_startDir;
00082 KFileTreeView *m_treeView;
00083 QMenu *m_contextMenu;
00084 KActionCollection *m_actions;
00085 KFilePlacesView *m_placesView;
00086 KHistoryComboBox *m_urlCombo;
00087 QString m_recentDirClass;
00088 KUrl m_startURL;
00089
00090 };
00091
00092 void KDirSelectDialog::Private::readConfig(const KSharedConfig::Ptr &config, const QString& group)
00093 {
00094 m_urlCombo->clear();
00095
00096 KConfigGroup conf( config, group );
00097 m_urlCombo->setHistoryItems( conf.readPathEntry( "History Items", QStringList() ));
00098
00099 m_parent->resize( conf.readEntry( "DirSelectDialog Size", QSize( 400, 450 ) ) );
00100 }
00101
00102 void KDirSelectDialog::Private::saveConfig(KSharedConfig::Ptr config, const QString& group)
00103 {
00104 KConfigGroup conf( config, group );
00105 KConfigGroup::WriteConfigFlags flags(KConfigGroup::Persistent|KConfigGroup::Global);
00106 conf.writePathEntry( "History Items", m_urlCombo->historyItems(), flags );
00107 conf.writeEntry( "DirSelectDialog Size", m_parent->size(), flags );
00108
00109 config->sync();
00110 }
00111
00112 void KDirSelectDialog::Private::slotMkdir()
00113 {
00114 bool ok;
00115 QString where = m_parent->url().pathOrUrl();
00116 QString name = i18nc("folder name", "New Folder" );
00117 if ( m_parent->url().isLocalFile() && QFileInfo( m_parent->url().path(KUrl::AddTrailingSlash) + name ).exists() )
00118 name = KIO::RenameDialog::suggestName( m_parent->url(), name );
00119
00120 QString directory = KIO::encodeFileName( KInputDialog::getText( i18nc("@title:window", "New Folder" ),
00121 i18nc("@label:textbox", "Create new folder in:\n%1" , where ),
00122 name, &ok, m_parent));
00123 if (!ok)
00124 return;
00125
00126 bool selectDirectory = true;
00127 bool writeOk = false;
00128 bool exists = false;
00129 KUrl folderurl( m_parent->url() );
00130
00131 const QStringList dirs = directory.split( QDir::separator(), QString::SkipEmptyParts );
00132 QStringList::ConstIterator it = dirs.begin();
00133
00134 for ( ; it != dirs.end(); ++it )
00135 {
00136 folderurl.addPath( *it );
00137 exists = KIO::NetAccess::exists( folderurl, KIO::NetAccess::DestinationSide, 0 );
00138 writeOk = !exists && KIO::NetAccess::mkdir( folderurl, m_parent->topLevelWidget() );
00139 }
00140
00141 if ( exists )
00142 {
00143 QString which = folderurl.isLocalFile() ? folderurl.path() : folderurl.prettyUrl();
00144 KMessageBox::sorry(m_parent, i18n("A file or folder named %1 already exists.", which));
00145 selectDirectory = false;
00146 }
00147 else if ( !writeOk ) {
00148 KMessageBox::sorry(m_parent, i18n("You do not have permission to create that folder." ));
00149 }
00150 else if ( selectDirectory ) {
00151 m_parent->setCurrentUrl( folderurl );
00152 }
00153 }
00154
00155 void KDirSelectDialog::Private::_k_slotCurrentChanged()
00156 {
00157 if ( m_comboLocked )
00158 return;
00159
00160 const KUrl u = m_treeView->currentUrl();
00161
00162 if ( u.isValid() )
00163 {
00164 if ( u.isLocalFile() )
00165 m_urlCombo->setEditText( u.path() );
00166
00167 else
00168 m_urlCombo->setEditText( u.prettyUrl() );
00169 }
00170 else
00171 m_urlCombo->setEditText( QString() );
00172 }
00173
00174 void KDirSelectDialog::Private::_k_slotUrlActivated( const QString& text )
00175 {
00176 if ( text.isEmpty() )
00177 return;
00178
00179 KUrl url( text );
00180 m_urlCombo->addToHistory( url.prettyUrl() );
00181
00182 if ( m_parent->localOnly() && !url.isLocalFile() )
00183 return;
00184
00185 KUrl oldUrl = m_treeView->currentUrl();
00186 if ( oldUrl.isEmpty() )
00187 oldUrl = m_startDir;
00188
00189 m_parent->setCurrentUrl( oldUrl );
00190 }
00191
00192 void KDirSelectDialog::Private::_k_slotComboTextChanged( const QString& text )
00193 {
00194 m_treeView->blockSignals(true);
00195 m_treeView->setCurrentUrl( KUrl( text ) );
00196 m_treeView->blockSignals(false);
00197 }
00198
00199 void KDirSelectDialog::Private::_k_slotContextMenu( const QPoint& pos )
00200 {
00201 m_contextMenu->popup( pos );
00202 }
00203
00204 void KDirSelectDialog::Private::_k_slotExpand(const QModelIndex &index)
00205 {
00206 m_treeView->setExpanded(index, !m_treeView->isExpanded(index));
00207 }
00208
00209 void KDirSelectDialog::Private::_k_slotUser1()
00210 {
00211 slotMkdir();
00212 }
00213
00214
00215
00216
00217 KDirSelectDialog::KDirSelectDialog(const KUrl &startDir, bool localOnly,
00218 QWidget *parent)
00219 #ifdef Q_WS_WIN
00220 : KDialog( parent , Qt::WindowMinMaxButtonsHint),
00221 #else
00222 : KDialog( parent ),
00223 #endif
00224 d( new Private( localOnly, this ) )
00225 {
00226 setCaption( i18nc("@title:window","Select Folder") );
00227 setButtons( Ok | Cancel | User1 );
00228 setButtonGuiItem( User1, KGuiItem( i18nc("@action:button","New Folder..."), "folder-new" ) );
00229 showButtonSeparator(false);
00230 setDefaultButton(Ok);
00231
00232 QFrame *page = new QFrame(this);
00233 setMainWidget(page);
00234 QHBoxLayout *hlay = new QHBoxLayout( page);
00235 hlay->setMargin(0);
00236 hlay->setSpacing(spacingHint());
00237 QVBoxLayout *mainLayout = new QVBoxLayout();
00238 d->m_actions=new KActionCollection(this);
00239 d->m_placesView = new KFilePlacesView( page );
00240 d->m_placesView->setModel(new KFilePlacesModel(d->m_placesView));
00241 d->m_placesView->setObjectName( QLatin1String( "speedbar" ) );
00242 d->m_placesView->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
00243 d->m_placesView->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
00244 connect( d->m_placesView, SIGNAL( urlChanged( const KUrl& )),
00245 SLOT( setCurrentUrl( const KUrl& )) );
00246 hlay->addWidget( d->m_placesView );
00247 hlay->addLayout( mainLayout );
00248
00249 d->m_treeView = new KFileTreeView(page);
00250 d->m_treeView->setDirOnlyMode(true);
00251
00252 for (int i = 1; i < d->m_treeView->model()->columnCount(); ++i)
00253 d->m_treeView->hideColumn(i);
00254
00255 d->m_urlCombo = new KHistoryComboBox( page);
00256 d->m_urlCombo->setLayoutDirection( Qt::LeftToRight );
00257 d->m_urlCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
00258 d->m_urlCombo->setTrapReturnKey( true );
00259 d->m_urlCombo->setPixmapProvider( new KUrlPixmapProvider() );
00260 KUrlCompletion *comp = new KUrlCompletion();
00261 comp->setMode( KUrlCompletion::DirCompletion );
00262 comp->setIgnoreCase(true);
00263 d->m_urlCombo->setCompletionObject( comp, true );
00264 d->m_urlCombo->setAutoDeleteCompletionObject( true );
00265 d->m_urlCombo->setDuplicatesEnabled( false );
00266
00267 d->m_contextMenu = new QMenu( this );
00268 KAction* newFolder = new KAction( i18nc("@action:inmenu","New Folder..."), this);
00269 d->m_actions->addAction(newFolder->objectName(), newFolder);
00270 newFolder->setIcon( KIcon( "folder-new" ) );
00271 connect( newFolder, SIGNAL( triggered( bool ) ), this, SLOT( _k_slotUser1() ) );
00272 d->m_contextMenu->addAction( newFolder );
00273 d->m_contextMenu->addSeparator();
00274
00275 KToggleAction *action = new KToggleAction( i18nc("@option:check", "Show Hidden Folders" ), this );
00276 d->m_actions->addAction( action->objectName(), action );
00277 connect( action, SIGNAL( triggered( bool ) ), d->m_treeView, SLOT( setShowHiddenFiles( bool ) ) );
00278 d->m_contextMenu->addAction( action );
00279
00280 d->m_startURL = KFileDialog::getStartUrl( startDir, d->m_recentDirClass );
00281 if ( localOnly && !d->m_startURL.isLocalFile() )
00282 {
00283 d->m_startURL = KUrl();
00284 QString docPath = KGlobalSettings::documentPath();
00285 if (QDir(docPath).exists())
00286 d->m_startURL.setPath( docPath );
00287 else
00288 d->m_startURL.setPath( QDir::homePath() );
00289 }
00290
00291 d->m_startDir = d->m_startURL;
00292 d->m_rootUrl = d->m_treeView->rootUrl();
00293
00294 d->readConfig( KGlobal::config(), "DirSelect Dialog" );
00295
00296 mainLayout->addWidget( d->m_treeView, 1 );
00297 mainLayout->addWidget( d->m_urlCombo, 0 );
00298
00299 connect( d->m_treeView, SIGNAL( currentChanged(const KUrl&)),
00300 SLOT( _k_slotCurrentChanged() ));
00301 connect( d->m_treeView, SIGNAL( activated(const QModelIndex&)),
00302 SLOT( _k_slotExpand(const QModelIndex&) ));
00303 connect( d->m_treeView, SIGNAL( customContextMenuRequested( const QPoint & )),
00304 SLOT( _k_slotContextMenu( const QPoint & )));
00305
00306 connect( d->m_urlCombo, SIGNAL( editTextChanged( const QString& ) ),
00307 SLOT( _k_slotComboTextChanged( const QString& ) ));
00308 connect( d->m_urlCombo, SIGNAL( activated( const QString& )),
00309 SLOT( _k_slotUrlActivated( const QString& )));
00310 connect( d->m_urlCombo, SIGNAL( returnPressed( const QString& )),
00311 SLOT( _k_slotUrlActivated( const QString& )));
00312
00313 connect(this, SIGNAL(user1Clicked()), this, SLOT(_k_slotUser1()));
00314
00315 setCurrentUrl(d->m_startURL);
00316 }
00317
00318
00319 KDirSelectDialog::~KDirSelectDialog()
00320 {
00321 delete d;
00322 }
00323
00324 KUrl KDirSelectDialog::url() const
00325 {
00326 KUrl comboUrl(d->m_urlCombo->currentText());
00327
00328 if ( comboUrl.isValid() ) {
00329 KIO::StatJob *statJob = KIO::stat(comboUrl, KIO::HideProgressInfo);
00330 const bool ok = KIO::NetAccess::synchronousRun(statJob, 0);
00331 if (ok && statJob->statResult().isDir()) {
00332 return comboUrl;
00333 }
00334 }
00335
00336 kDebug() << comboUrl.path() << " is not an accessible directory";
00337 return d->m_treeView->currentUrl();
00338 }
00339
00340 QAbstractItemView* KDirSelectDialog::view() const
00341 {
00342 return d->m_treeView;
00343 }
00344
00345 bool KDirSelectDialog::localOnly() const
00346 {
00347 return d->m_localOnly;
00348 }
00349
00350 KUrl KDirSelectDialog::startDir() const
00351 {
00352 return d->m_startDir;
00353 }
00354
00355 void KDirSelectDialog::setCurrentUrl( const KUrl& url )
00356 {
00357 if ( !url.isValid() )
00358 return;
00359
00360 if (url.protocol() != d->m_rootUrl.protocol()) {
00361 KUrl u( url );
00362 u.cd("/");
00363 d->m_treeView->setRootUrl( u );
00364 d->m_rootUrl = u;
00365 }
00366
00367 d->m_treeView->setCurrentUrl( url );
00368 }
00369
00370 void KDirSelectDialog::accept()
00371 {
00372 KUrl selectedUrl = url();
00373 if (!selectedUrl.isValid()) {
00374 return;
00375 }
00376
00377 if (!d->m_recentDirClass.isEmpty()) {
00378 KRecentDirs::add(d->m_recentDirClass, selectedUrl.url());
00379 }
00380
00381 d->m_urlCombo->addToHistory( selectedUrl.prettyUrl() );
00382 KFileDialog::setStartDir( url() );
00383
00384 KDialog::accept();
00385 }
00386
00387 void KDirSelectDialog::hideEvent( QHideEvent *event )
00388 {
00389 d->saveConfig( KGlobal::config(), "DirSelect Dialog" );
00390
00391 KDialog::hideEvent(event);
00392 }
00393
00394
00395 KUrl KDirSelectDialog::selectDirectory( const KUrl& startDir,
00396 bool localOnly,
00397 QWidget *parent,
00398 const QString& caption)
00399 {
00400 KDirSelectDialog myDialog( startDir, localOnly, parent);
00401
00402 if ( !caption.isNull() )
00403 myDialog.setCaption( caption );
00404
00405 if ( myDialog.exec() == QDialog::Accepted )
00406 return KIO::NetAccess::mostLocalUrl(myDialog.url(),parent);
00407 else
00408 return KUrl();
00409 }
00410
00411 #include "kdirselectdialog.moc"