You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
482 lines
14 KiB
482 lines
14 KiB
15 years ago
|
/*
|
||
|
Copyright (C) 2001,2002 Carsten Pfeiffer <pfeiffer@kde.org>
|
||
|
Copyright (C) 2001 Michael Jarrett <michaelj@corel.com>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2 as published by the Free Software Foundation.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqdir.h>
|
||
13 years ago
|
#include <tqlayout.h>
|
||
15 years ago
|
#include <tqpopupmenu.h>
|
||
|
#include <tqstringlist.h>
|
||
|
#include <tqvaluestack.h>
|
||
15 years ago
|
|
||
|
#include <kactionclasses.h>
|
||
|
#include <kapplication.h>
|
||
|
#include <kcombobox.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
|
#include <tdefiledialog.h>
|
||
|
#include <tdefilespeedbar.h>
|
||
15 years ago
|
#include <kglobalsettings.h>
|
||
|
#include <kiconloader.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kprotocolinfo.h>
|
||
|
#include <krecentdirs.h>
|
||
|
#include <kshell.h>
|
||
|
#include <kurl.h>
|
||
|
#include <kurlcompletion.h>
|
||
|
#include <kurlpixmapprovider.h>
|
||
|
#include <kinputdialog.h>
|
||
12 years ago
|
#include <tdeio/netaccess.h>
|
||
|
#include <tdeio/renamedlg.h>
|
||
15 years ago
|
#include <kmessagebox.h>
|
||
|
|
||
12 years ago
|
#include "tdefiletreeview.h"
|
||
15 years ago
|
#include "kdirselectdialog.h"
|
||
|
|
||
|
// ### add mutator for treeview!
|
||
|
|
||
|
class KDirSelectDialog::KDirSelectDialogPrivate
|
||
|
{
|
||
|
public:
|
||
|
KDirSelectDialogPrivate()
|
||
|
{
|
||
|
urlCombo = 0L;
|
||
|
branch = 0L;
|
||
|
comboLocked = false;
|
||
|
}
|
||
|
|
||
|
KFileSpeedBar *speedBar;
|
||
|
KHistoryCombo *urlCombo;
|
||
|
KFileTreeBranch *branch;
|
||
15 years ago
|
TQString recentDirClass;
|
||
15 years ago
|
KURL startURL;
|
||
15 years ago
|
TQValueStack<KURL> dirsToList;
|
||
15 years ago
|
|
||
|
bool comboLocked : 1;
|
||
|
};
|
||
|
|
||
|
static KURL rootUrl(const KURL &url)
|
||
|
{
|
||
|
KURL root = url;
|
||
|
root.setPath( "/" );
|
||
|
|
||
|
if (!kapp->authorizeURLAction("list", KURL(), root))
|
||
|
{
|
||
15 years ago
|
root = KURL::fromPathOrURL( TQDir::homeDirPath() );
|
||
15 years ago
|
if (!kapp->authorizeURLAction("list", KURL(), root))
|
||
|
{
|
||
|
root = url;
|
||
|
}
|
||
|
}
|
||
|
return root;
|
||
|
}
|
||
|
|
||
15 years ago
|
KDirSelectDialog::KDirSelectDialog(const TQString &startDir, bool localOnly,
|
||
|
TQWidget *parent, const char *name,
|
||
15 years ago
|
bool modal)
|
||
|
: KDialogBase( parent, name, modal, i18n("Select Folder"),
|
||
|
Ok|Cancel|User1, Ok, false,
|
||
|
KGuiItem( i18n("New Folder..."), "folder_new" ) ),
|
||
|
m_localOnly( localOnly )
|
||
|
{
|
||
|
d = new KDirSelectDialogPrivate;
|
||
|
d->branch = 0L;
|
||
|
|
||
15 years ago
|
TQFrame *page = makeMainWidget();
|
||
|
TQHBoxLayout *hlay = new TQHBoxLayout( page, 0, spacingHint() );
|
||
|
m_mainLayout = new TQVBoxLayout();
|
||
15 years ago
|
d->speedBar = new KFileSpeedBar( page, "speedbar" );
|
||
15 years ago
|
connect( d->speedBar, TQT_SIGNAL( activated( const KURL& )),
|
||
|
TQT_SLOT( setCurrentURL( const KURL& )) );
|
||
15 years ago
|
hlay->addWidget( d->speedBar, 0 );
|
||
|
hlay->addLayout( m_mainLayout, 1 );
|
||
|
|
||
|
// Create dir list
|
||
|
m_treeView = new KFileTreeView( page );
|
||
|
m_treeView->addColumn( i18n("Folders") );
|
||
15 years ago
|
m_treeView->setColumnWidthMode( 0, TQListView::Maximum );
|
||
|
m_treeView->setResizeMode( TQListView::AllColumns );
|
||
15 years ago
|
|
||
|
d->urlCombo = new KHistoryCombo( page, "url combo" );
|
||
|
d->urlCombo->setTrapReturnKey( true );
|
||
|
d->urlCombo->setPixmapProvider( new KURLPixmapProvider() );
|
||
|
KURLCompletion *comp = new KURLCompletion();
|
||
|
comp->setMode( KURLCompletion::DirCompletion );
|
||
|
d->urlCombo->setCompletionObject( comp, true );
|
||
|
d->urlCombo->setAutoDeleteCompletionObject( true );
|
||
|
d->urlCombo->setDuplicatesEnabled( false );
|
||
15 years ago
|
connect( d->urlCombo, TQT_SIGNAL( textChanged( const TQString& ) ),
|
||
|
TQT_SLOT( slotComboTextChanged( const TQString& ) ));
|
||
15 years ago
|
|
||
15 years ago
|
m_contextMenu = new TQPopupMenu( this );
|
||
14 years ago
|
KAction* newFolder = new KAction( i18n("New Folder..."), "folder_new", 0, TQT_TQOBJECT(this), TQT_SLOT( slotMkdir() ), TQT_TQOBJECT(this));
|
||
15 years ago
|
newFolder->plug(m_contextMenu);
|
||
|
m_contextMenu->insertSeparator();
|
||
14 years ago
|
m_showHiddenFolders = new KToggleAction ( i18n( "Show Hidden Folders" ), 0, TQT_TQOBJECT(this),
|
||
|
TQT_SLOT( slotShowHiddenFoldersToggled() ), TQT_TQOBJECT(this));
|
||
15 years ago
|
m_showHiddenFolders->plug(m_contextMenu);
|
||
|
|
||
|
d->startURL = KFileDialog::getStartURL( startDir, d->recentDirClass );
|
||
|
if ( localOnly && !d->startURL.isLocalFile() )
|
||
|
{
|
||
|
d->startURL = KURL();
|
||
12 years ago
|
TQString docPath = TDEGlobalSettings::documentPath();
|
||
15 years ago
|
if (TQDir(docPath).exists())
|
||
15 years ago
|
d->startURL.setPath( docPath );
|
||
|
else
|
||
15 years ago
|
d->startURL.setPath( TQDir::homeDirPath() );
|
||
15 years ago
|
}
|
||
|
|
||
|
KURL root = rootUrl(d->startURL);
|
||
|
|
||
|
m_startDir = d->startURL.url();
|
||
|
|
||
|
d->branch = createBranch( root );
|
||
|
|
||
12 years ago
|
readConfig( TDEGlobal::config(), "DirSelect Dialog" );
|
||
15 years ago
|
|
||
|
m_mainLayout->addWidget( m_treeView, 1 );
|
||
|
m_mainLayout->addWidget( d->urlCombo, 0 );
|
||
|
|
||
15 years ago
|
connect( m_treeView, TQT_SIGNAL( currentChanged( TQListViewItem * )),
|
||
|
TQT_SLOT( slotCurrentChanged() ));
|
||
|
connect( m_treeView, TQT_SIGNAL( contextMenu( KListView *, TQListViewItem *, const TQPoint & )),
|
||
|
TQT_SLOT( slotContextMenu( KListView *, TQListViewItem *, const TQPoint & )));
|
||
15 years ago
|
|
||
15 years ago
|
connect( d->urlCombo, TQT_SIGNAL( activated( const TQString& )),
|
||
|
TQT_SLOT( slotURLActivated( const TQString& )));
|
||
|
connect( d->urlCombo, TQT_SIGNAL( returnPressed( const TQString& )),
|
||
|
TQT_SLOT( slotURLActivated( const TQString& )));
|
||
15 years ago
|
|
||
|
setCurrentURL( d->startURL );
|
||
|
}
|
||
|
|
||
|
|
||
|
KDirSelectDialog::~KDirSelectDialog()
|
||
|
{
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::setCurrentURL( const KURL& url )
|
||
|
{
|
||
|
if ( !url.isValid() )
|
||
|
return;
|
||
|
|
||
|
KURL root = rootUrl(url);
|
||
|
|
||
|
d->startURL = url;
|
||
|
if ( !d->branch ||
|
||
|
url.protocol() != d->branch->url().protocol() ||
|
||
|
url.host() != d->branch->url().host() )
|
||
|
{
|
||
|
if ( d->branch )
|
||
|
{
|
||
|
// removing the root-item causes the currentChanged() signal to be
|
||
|
// emitted, but we don't want to update the location-combo yet.
|
||
|
d->comboLocked = true;
|
||
|
view()->removeBranch( d->branch );
|
||
|
d->comboLocked = false;
|
||
|
}
|
||
|
|
||
|
d->branch = createBranch( root );
|
||
|
}
|
||
|
|
||
15 years ago
|
d->branch->disconnect( TQT_SIGNAL( populateFinished( KFileTreeViewItem * )),
|
||
|
this, TQT_SLOT( slotNextDirToList( KFileTreeViewItem *)));
|
||
|
connect( d->branch, TQT_SIGNAL( populateFinished( KFileTreeViewItem * )),
|
||
|
TQT_SLOT( slotNextDirToList( KFileTreeViewItem * ) ));
|
||
15 years ago
|
|
||
|
KURL dirToList = root;
|
||
|
d->dirsToList.clear();
|
||
15 years ago
|
TQString path = url.path(+1);
|
||
15 years ago
|
int pos = path.length();
|
||
|
|
||
|
if ( path.isEmpty() ) // e.g. ftp://host.com/ -> just list the root dir
|
||
|
d->dirsToList.push( root );
|
||
|
|
||
|
else
|
||
|
{
|
||
|
while ( pos > 0 )
|
||
|
{
|
||
13 years ago
|
pos = path.findRev( '/', pos -1 );
|
||
15 years ago
|
if ( pos >= 0 )
|
||
|
{
|
||
|
dirToList.setPath( path.left( pos +1 ) );
|
||
|
d->dirsToList.push( dirToList );
|
||
13 years ago
|
// tqDebug( "List: %s", dirToList.url().latin1());
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( !d->dirsToList.isEmpty() )
|
||
|
openNextDir( d->branch->root() );
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::openNextDir( KFileTreeViewItem * /*parent*/ )
|
||
|
{
|
||
|
if ( !d->branch )
|
||
|
return;
|
||
|
|
||
|
KURL url = d->dirsToList.pop();
|
||
|
|
||
14 years ago
|
KFileTreeViewItem *item = view()->findItem( d->branch, url.path().mid(1));
|
||
15 years ago
|
if ( item )
|
||
|
{
|
||
|
if ( !item->isOpen() )
|
||
|
item->setOpen( true );
|
||
|
else // already open -> go to next one
|
||
|
slotNextDirToList( item );
|
||
|
}
|
||
|
// else
|
||
13 years ago
|
// tqDebug("###### openNextDir: item not found!");
|
||
15 years ago
|
}
|
||
|
|
||
|
void KDirSelectDialog::slotNextDirToList( KFileTreeViewItem *item )
|
||
|
{
|
||
|
// scroll to make item the topmost item
|
||
|
view()->ensureItemVisible( item );
|
||
13 years ago
|
TQRect r = view()->itemRect( item );
|
||
15 years ago
|
if ( r.isValid() )
|
||
|
{
|
||
|
int x, y;
|
||
|
view()->viewportToContents( view()->contentsX(), r.y(), x, y );
|
||
|
view()->setContentsPos( x, y );
|
||
|
}
|
||
|
|
||
|
if ( !d->dirsToList.isEmpty() )
|
||
|
openNextDir( item );
|
||
|
else
|
||
|
{
|
||
15 years ago
|
d->branch->disconnect( TQT_SIGNAL( populateFinished( KFileTreeViewItem * )),
|
||
|
this, TQT_SLOT( slotNextDirToList( KFileTreeViewItem *)));
|
||
15 years ago
|
view()->setCurrentItem( item );
|
||
|
item->setSelected( true );
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void KDirSelectDialog::readConfig( TDEConfig *config, const TQString& group )
|
||
15 years ago
|
{
|
||
|
d->urlCombo->clear();
|
||
|
|
||
12 years ago
|
TDEConfigGroup conf( config, group );
|
||
15 years ago
|
d->urlCombo->setHistoryItems( conf.readPathListEntry( "History Items" ));
|
||
|
|
||
15 years ago
|
TQSize defaultSize( 400, 450 );
|
||
15 years ago
|
resize( conf.readSizeEntry( "DirSelectDialog Size", &defaultSize ));
|
||
|
}
|
||
|
|
||
12 years ago
|
void KDirSelectDialog::saveConfig( TDEConfig *config, const TQString& group )
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEConfigGroup conf( config, group );
|
||
15 years ago
|
conf.writePathEntry( "History Items", d->urlCombo->historyItems(), ',',
|
||
|
true, true);
|
||
|
conf.writeEntry( "DirSelectDialog Size", size(), true, true );
|
||
|
|
||
|
d->speedBar->save( config );
|
||
|
|
||
|
config->sync();
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::slotUser1()
|
||
|
{
|
||
|
slotMkdir();
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::accept()
|
||
|
{
|
||
|
KFileTreeViewItem *item = m_treeView->currentKFileTreeViewItem();
|
||
|
if ( !item )
|
||
|
return;
|
||
|
|
||
|
if ( !d->recentDirClass.isEmpty() )
|
||
|
{
|
||
|
KURL dir = item->url();
|
||
|
if ( !item->isDir() )
|
||
|
dir = dir.upURL();
|
||
|
|
||
|
KRecentDirs::add(d->recentDirClass, dir.url());
|
||
|
}
|
||
|
|
||
|
d->urlCombo->addToHistory( item->url().prettyURL() );
|
||
|
KFileDialog::setStartDir( url() );
|
||
|
|
||
|
KDialogBase::accept();
|
||
12 years ago
|
saveConfig( TDEGlobal::config(), "DirSelect Dialog" );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
KURL KDirSelectDialog::url() const
|
||
|
{
|
||
|
return m_treeView->currentURL();
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::slotCurrentChanged()
|
||
|
{
|
||
|
if ( d->comboLocked )
|
||
|
return;
|
||
|
|
||
|
KFileTreeViewItem *current = view()->currentKFileTreeViewItem();
|
||
|
KURL u = current ? current->url() : (d->branch ? d->branch->rootUrl() : KURL());
|
||
|
|
||
|
if ( u.isValid() )
|
||
|
{
|
||
|
if ( u.isLocalFile() )
|
||
|
d->urlCombo->setEditText( u.path() );
|
||
|
|
||
|
else // remote url
|
||
|
d->urlCombo->setEditText( u.prettyURL() );
|
||
|
}
|
||
|
else
|
||
15 years ago
|
d->urlCombo->setEditText( TQString::null );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void KDirSelectDialog::slotURLActivated( const TQString& text )
|
||
15 years ago
|
{
|
||
|
if ( text.isEmpty() )
|
||
|
return;
|
||
|
|
||
|
KURL url = KURL::fromPathOrURL( text );
|
||
|
d->urlCombo->addToHistory( url.prettyURL() );
|
||
|
|
||
|
if ( localOnly() && !url.isLocalFile() )
|
||
|
return; // ### messagebox
|
||
|
|
||
|
KURL oldURL = m_treeView->currentURL();
|
||
|
if ( oldURL.isEmpty() )
|
||
|
oldURL = KURL::fromPathOrURL( m_startDir );
|
||
|
|
||
|
setCurrentURL( url );
|
||
|
}
|
||
|
|
||
|
KFileTreeBranch * KDirSelectDialog::createBranch( const KURL& url )
|
||
|
{
|
||
15 years ago
|
TQString title = url.isLocalFile() ? url.path() : url.prettyURL();
|
||
15 years ago
|
KFileTreeBranch *branch = view()->addBranch( url, title, m_showHiddenFolders->isChecked() );
|
||
|
branch->setChildRecurse( false );
|
||
|
view()->setDirOnlyMode( branch, true );
|
||
|
|
||
|
return branch;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KDirSelectDialog::slotComboTextChanged( const TQString& text )
|
||
15 years ago
|
{
|
||
|
if ( d->branch )
|
||
|
{
|
||
|
KURL url = KURL::fromPathOrURL( KShell::tildeExpand( text ) );
|
||
14 years ago
|
KFileTreeViewItem *item = d->branch->findTVIByURL( url );
|
||
15 years ago
|
if ( item )
|
||
|
{
|
||
|
view()->setCurrentItem( item );
|
||
|
view()->setSelected( item, true );
|
||
|
view()->ensureItemVisible( item );
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
TQListViewItem *item = view()->currentItem();
|
||
15 years ago
|
if ( item )
|
||
|
{
|
||
|
item->setSelected( false );
|
||
14 years ago
|
// 2002/12/27, deselected item is not repainted, so force it
|
||
13 years ago
|
item->repaint();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
void KDirSelectDialog::slotContextMenu( KListView *, TQListViewItem *, const TQPoint& pos )
|
||
15 years ago
|
{
|
||
|
m_contextMenu->popup( pos );
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::slotMkdir()
|
||
|
{
|
||
|
bool ok;
|
||
15 years ago
|
TQString where = url().pathOrURL();
|
||
|
TQString name = i18n( "New Folder" );
|
||
|
if ( url().isLocalFile() && TQFileInfo( url().path(+1) + name ).exists() )
|
||
12 years ago
|
name = TDEIO::RenameDlg::suggestName( url(), name );
|
||
15 years ago
|
|
||
12 years ago
|
TQString directory = TDEIO::encodeFileName( KInputDialog::getText( i18n( "New Folder" ),
|
||
15 years ago
|
i18n( "Create new folder in:\n%1" ).arg( where ),
|
||
|
name, &ok, this));
|
||
|
if (!ok)
|
||
|
return;
|
||
|
|
||
|
bool selectDirectory = true;
|
||
|
bool writeOk = false;
|
||
|
bool exists = false;
|
||
|
KURL folderurl( url() );
|
||
|
|
||
15 years ago
|
TQStringList dirs = TQStringList::split( TQDir::separator(), directory );
|
||
|
TQStringList::ConstIterator it = dirs.begin();
|
||
15 years ago
|
|
||
|
for ( ; it != dirs.end(); ++it )
|
||
|
{
|
||
|
folderurl.addPath( *it );
|
||
12 years ago
|
exists = TDEIO::NetAccess::exists( folderurl, false, 0 );
|
||
|
writeOk = !exists && TDEIO::NetAccess::mkdir( folderurl, topLevelWidget() );
|
||
15 years ago
|
}
|
||
|
|
||
|
if ( exists ) // url was already existant
|
||
|
{
|
||
15 years ago
|
TQString which = folderurl.isLocalFile() ? folderurl.path() : folderurl.prettyURL();
|
||
15 years ago
|
KMessageBox::sorry(this, i18n("A file or folder named %1 already exists.").arg(which));
|
||
|
selectDirectory = false;
|
||
|
}
|
||
|
else if ( !writeOk ) {
|
||
|
KMessageBox::sorry(this, i18n("You do not have permission to create that folder." ));
|
||
|
}
|
||
|
else if ( selectDirectory ) {
|
||
|
setCurrentURL( folderurl );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::slotShowHiddenFoldersToggled()
|
||
|
{
|
||
|
KURL currentURL = url();
|
||
|
|
||
|
d->comboLocked = true;
|
||
|
view()->removeBranch( d->branch );
|
||
|
d->comboLocked = false;
|
||
|
|
||
|
KURL root = rootUrl(d->startURL);
|
||
|
d->branch = createBranch( root );
|
||
|
|
||
|
setCurrentURL( currentURL );
|
||
|
}
|
||
|
|
||
|
// static
|
||
15 years ago
|
KURL KDirSelectDialog::selectDirectory( const TQString& startDir,
|
||
15 years ago
|
bool localOnly,
|
||
15 years ago
|
TQWidget *parent,
|
||
|
const TQString& caption)
|
||
15 years ago
|
{
|
||
|
KDirSelectDialog myDialog( startDir, localOnly, parent,
|
||
|
"kdirselect dialog", true );
|
||
|
|
||
|
if ( !caption.isNull() )
|
||
|
myDialog.setCaption( caption );
|
||
|
|
||
15 years ago
|
if ( myDialog.exec() == TQDialog::Accepted )
|
||
12 years ago
|
return TDEIO::NetAccess::mostLocalURL(myDialog.url(),parent);
|
||
15 years ago
|
else
|
||
|
return KURL();
|
||
|
}
|
||
|
|
||
|
void KDirSelectDialog::virtual_hook( int id, void* data )
|
||
|
{ KDialogBase::virtual_hook( id, data ); }
|
||
|
|
||
|
#include "kdirselectdialog.moc"
|