22 #include <tqpopupmenu.h>
23 #include <tqstringlist.h>
24 #include <tqvaluestack.h>
26 #include <tdeactionclasses.h>
27 #include <tdeapplication.h>
28 #include <kcombobox.h>
29 #include <tdeconfig.h>
30 #include <tdefiledialog.h>
31 #include <tdefilespeedbar.h>
32 #include <tdeglobalsettings.h>
33 #include <kiconloader.h>
34 #include <tdelocale.h>
35 #include <kprotocolinfo.h>
36 #include <tderecentdirs.h>
39 #include <kurlcompletion.h>
40 #include <kurlpixmapprovider.h>
41 #include <kinputdialog.h>
42 #include <tdeio/netaccess.h>
43 #include <tdeio/renamedlg.h>
44 #include <tdemessagebox.h>
46 #include "tdefiletreeview.h"
47 #include "kdirselectdialog.h"
51 class KDirSelectDialog::KDirSelectDialogPrivate
54 KDirSelectDialogPrivate()
61 KFileSpeedBar *speedBar;
62 KHistoryCombo *urlCombo;
64 TQString recentDirClass;
66 TQValueStack<KURL> dirsToList;
71 static KURL rootUrl(
const KURL &url)
76 if (!kapp->authorizeURLAction(
"list", KURL(), root))
78 root = KURL::fromPathOrURL( TQDir::homeDirPath() );
79 if (!kapp->authorizeURLAction(
"list", KURL(), root))
88 TQWidget *parent,
const char *name,
90 : KDialogBase( parent, name, modal, i18n(
"Select Folder"),
91 Ok|Cancel|User1, Ok, false,
92 KGuiItem( i18n(
"New Folder..."),
"folder-new" ) ),
93 m_localOnly( localOnly )
95 d =
new KDirSelectDialogPrivate;
98 TQFrame *page = makeMainWidget();
99 TQHBoxLayout *hlay =
new TQHBoxLayout( page, 0, spacingHint() );
100 m_mainLayout =
new TQVBoxLayout();
101 d->speedBar =
new KFileSpeedBar( page,
"speedbar" );
102 connect( d->speedBar, TQT_SIGNAL( activated(
const KURL& )),
103 TQT_SLOT( setCurrentURL(
const KURL& )) );
104 hlay->addWidget( d->speedBar, 0 );
105 hlay->addLayout( m_mainLayout, 1 );
109 m_treeView->addColumn( i18n(
"Folders") );
110 m_treeView->setColumnWidthMode( 0, TQListView::Maximum );
111 m_treeView->setResizeMode( TQListView::AllColumns );
113 d->urlCombo =
new KHistoryCombo( page,
"url combo" );
114 d->urlCombo->setTrapReturnKey(
true );
115 d->urlCombo->setPixmapProvider(
new KURLPixmapProvider() );
116 KURLCompletion *comp =
new KURLCompletion();
117 comp->setMode( KURLCompletion::DirCompletion );
118 d->urlCombo->setCompletionObject( comp,
true );
119 d->urlCombo->setAutoDeleteCompletionObject(
true );
120 d->urlCombo->setDuplicatesEnabled(
false );
121 connect( d->urlCombo, TQT_SIGNAL( textChanged(
const TQString& ) ),
122 TQT_SLOT( slotComboTextChanged(
const TQString& ) ));
124 m_contextMenu =
new TQPopupMenu(
this );
125 TDEAction* newFolder =
new TDEAction( i18n(
"New Folder..."),
"folder-new", 0, TQT_TQOBJECT(
this), TQT_SLOT( slotMkdir() ), TQT_TQOBJECT(
this));
126 newFolder->plug(m_contextMenu);
127 m_contextMenu->insertSeparator();
128 m_showHiddenFolders =
new TDEToggleAction ( i18n(
"Show Hidden Folders" ), 0, TQT_TQOBJECT(
this),
129 TQT_SLOT( slotShowHiddenFoldersToggled() ), TQT_TQOBJECT(
this));
130 m_showHiddenFolders->plug(m_contextMenu);
133 if ( localOnly && !d->startURL.isLocalFile() )
135 d->startURL = KURL();
136 TQString docPath = TDEGlobalSettings::documentPath();
137 if (TQDir(docPath).exists())
138 d->startURL.setPath( docPath );
140 d->startURL.setPath( TQDir::homeDirPath() );
143 KURL root = rootUrl(d->startURL);
145 m_startDir = d->startURL.url();
147 d->branch = createBranch( root );
149 readConfig( TDEGlobal::config(),
"DirSelect Dialog" );
151 m_mainLayout->addWidget( m_treeView, 1 );
152 m_mainLayout->addWidget( d->urlCombo, 0 );
154 connect( m_treeView, TQT_SIGNAL( currentChanged( TQListViewItem * )),
155 TQT_SLOT( slotCurrentChanged() ));
156 connect( m_treeView, TQT_SIGNAL( contextMenu( TDEListView *, TQListViewItem *,
const TQPoint & )),
157 TQT_SLOT( slotContextMenu( TDEListView *, TQListViewItem *,
const TQPoint & )));
159 connect( d->urlCombo, TQT_SIGNAL( activated(
const TQString& )),
160 TQT_SLOT( slotURLActivated(
const TQString& )));
161 connect( d->urlCombo, TQT_SIGNAL( returnPressed(
const TQString& )),
162 TQT_SLOT( slotURLActivated(
const TQString& )));
164 setCurrentURL( d->startURL );
168 KDirSelectDialog::~KDirSelectDialog()
173 void KDirSelectDialog::setCurrentURL(
const KURL& url )
175 if ( !url.isValid() )
178 KURL root = rootUrl(url);
182 url.protocol() != d->branch->url().protocol() ||
183 url.host() != d->branch->url().host() )
189 d->comboLocked =
true;
191 d->comboLocked =
false;
194 d->branch = createBranch( root );
202 KURL dirToList = root;
203 d->dirsToList.clear();
204 TQString path = url.path(+1);
205 int pos = path.length();
207 if ( path.isEmpty() )
208 d->dirsToList.push( root );
214 pos = path.findRev(
'/', pos -1 );
217 dirToList.setPath( path.left( pos +1 ) );
218 d->dirsToList.push( dirToList );
224 if ( !d->dirsToList.isEmpty() )
225 openNextDir( d->branch->root() );
233 KURL url = d->dirsToList.pop();
238 if ( !item->isOpen() )
239 item->setOpen(
true );
241 slotNextDirToList( item );
250 view()->ensureItemVisible( item );
251 TQRect r = view()->itemRect( item );
255 view()->viewportToContents( view()->contentsX(), r.y(), x, y );
256 view()->setContentsPos( x, y );
259 if ( !d->dirsToList.isEmpty() )
265 view()->setCurrentItem( item );
266 item->setSelected(
true );
270 void KDirSelectDialog::readConfig( TDEConfig *config,
const TQString& group )
272 d->urlCombo->clear();
274 TDEConfigGroup conf( config, group );
275 d->urlCombo->setHistoryItems( conf.readPathListEntry(
"History Items" ));
277 TQSize defaultSize( 400, 450 );
278 resize( conf.readSizeEntry(
"DirSelectDialog Size", &defaultSize ));
281 void KDirSelectDialog::saveConfig( TDEConfig *config,
const TQString& group )
283 TDEConfigGroup conf( config, group );
284 conf.writePathEntry(
"History Items", d->urlCombo->historyItems(),
',',
286 conf.writeEntry(
"DirSelectDialog Size", size(),
true,
true );
288 d->speedBar->save( config );
293 void KDirSelectDialog::slotUser1()
298 void KDirSelectDialog::accept()
304 if ( !d->recentDirClass.isEmpty() )
306 KURL dir = item->
url();
307 if ( !item->
isDir() )
313 d->urlCombo->addToHistory( item->
url().prettyURL() );
314 KFileDialog::setStartDir(
url() );
316 KDialogBase::accept();
317 saveConfig( TDEGlobal::config(),
"DirSelect Dialog" );
326 void KDirSelectDialog::slotCurrentChanged()
328 if ( d->comboLocked )
332 KURL u = current ? current->
url() : (d->branch ? d->branch->rootUrl() : KURL());
336 if ( u.isLocalFile() )
337 d->urlCombo->setEditText( u.path() );
340 d->urlCombo->setEditText( u.prettyURL() );
343 d->urlCombo->setEditText( TQString::null );
346 void KDirSelectDialog::slotURLActivated(
const TQString& text )
348 if ( text.isEmpty() )
351 KURL url = KURL::fromPathOrURL( text );
352 d->urlCombo->addToHistory( url.prettyURL() );
354 if ( localOnly() && !url.isLocalFile() )
358 if ( oldURL.isEmpty() )
359 oldURL = KURL::fromPathOrURL( m_startDir );
361 setCurrentURL( url );
366 TQString title = url.isLocalFile() ? url.path() : url.prettyURL();
374 void KDirSelectDialog::slotComboTextChanged(
const TQString& text )
378 KURL url = KURL::fromPathOrURL( KShell::tildeExpand( text ) );
382 view()->setCurrentItem( item );
383 view()->setSelected( item,
true );
384 view()->ensureItemVisible( item );
389 TQListViewItem *item = view()->currentItem();
392 item->setSelected(
false );
398 void KDirSelectDialog::slotContextMenu( TDEListView *, TQListViewItem *,
const TQPoint& pos )
400 m_contextMenu->popup( pos );
403 void KDirSelectDialog::slotMkdir()
406 TQString where =
url().pathOrURL();
407 TQString name = i18n(
"New Folder" );
408 if (
url().isLocalFile() && TQFileInfo(
url().path(+1) + name ).exists() )
409 name = TDEIO::RenameDlg::suggestName(
url(), name );
411 TQString directory = TDEIO::encodeFileName( KInputDialog::getText( i18n(
"New Folder" ),
412 i18n(
"Create new folder in:\n%1" ).arg( where ),
418 bool writeOk =
false;
420 KURL folderurl(
url() );
422 TQStringList dirs = TQStringList::split( TQDir::separator(), directory );
423 TQStringList::ConstIterator it = dirs.begin();
425 for ( ; it != dirs.end(); ++it )
427 folderurl.addPath( *it );
428 exists = TDEIO::NetAccess::exists( folderurl,
false, 0 );
429 writeOk = !exists && TDEIO::NetAccess::mkdir( folderurl, topLevelWidget() );
434 TQString which = folderurl.isLocalFile() ? folderurl.path() : folderurl.prettyURL();
435 KMessageBox::sorry(
this, i18n(
"A file or folder named %1 already exists.").arg(which));
436 selectDirectory =
false;
438 else if ( !writeOk ) {
439 KMessageBox::sorry(
this, i18n(
"You do not have permission to create that folder." ));
441 else if ( selectDirectory ) {
442 setCurrentURL( folderurl );
446 void KDirSelectDialog::slotShowHiddenFoldersToggled()
448 KURL currentURL =
url();
450 d->comboLocked =
true;
452 d->comboLocked =
false;
454 KURL root = rootUrl(d->startURL);
455 d->branch = createBranch( root );
457 setCurrentURL( currentURL );
464 const TQString& caption)
467 "kdirselect dialog",
true );
469 if ( !caption.isNull() )
470 myDialog.setCaption( caption );
472 if ( myDialog.exec() == TQDialog::Accepted )
473 return TDEIO::NetAccess::mostLocalURL(myDialog.
url(),parent);
478 void KDirSelectDialog::virtual_hook(
int id,
void* data )
479 { KDialogBase::virtual_hook(
id, data ); }
481 #include "kdirselectdialog.moc"
KDirSelectDialog(const TQString &startDir=TQString::null, bool localOnly=false, TQWidget *parent=0L, const char *name=0, bool modal=false)
The constructor.
KFileTreeViewItem * currentKFileTreeViewItem() const
virtual void setDirOnlyMode(KFileTreeBranch *branch, bool)
set the directory mode for branches.
static KURL selectDirectory(const TQString &startDir=TQString::null, bool localOnly=false, TQWidget *parent=0L, const TQString &caption=TQString::null)
Creates a KDirSelectDialog, and returns the result.
This is the branch class of the KFileTreeView, which represents one branch in the treeview...
The filetreeview offers a treeview on the file system which behaves like a QTreeView showing files an...
An item for a KFileTreeView that knows about its own KFileItem.
KURL url() const
Returns the currently-selected URL, or a blank URL if none is selected.
virtual bool removeBranch(KFileTreeBranch *branch)
removes the branch from the treeview.
static void add(const TQString &fileClass, const TQString &directory)
Associates directory with fileClass.
static KURL getStartURL(const TQString &startDir, TQString &recentDirClass)
This method implements the logic to determine the user's default directory to be listed.
KFileTreeBranch * addBranch(const KURL &path, const TQString &name, bool showHidden=false)
Adds a branch to the treeview item.
A pretty dialog for a KDirSelect control for selecting directories.
void setChildRecurse(bool t=true)
sets if children recursion is wanted or not.
KFileTreeViewItem * findItem(KFileTreeBranch *brnch, const TQString &relUrl)
searches a branch for a KFileTreeViewItem identified by the relative url given as second parameter...