• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdefile
 

tdeio/tdefile

  • tdeio
  • tdefile
kdirselectdialog.cpp
1/*
2 Copyright (C) 2001,2002 Carsten Pfeiffer <pfeiffer@kde.org>
3 Copyright (C) 2001 Michael Jarrett <michaelj@corel.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include <tqdir.h>
21#include <tqlayout.h>
22#include <tqpopupmenu.h>
23#include <tqstringlist.h>
24#include <tqvaluestack.h>
25
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>
37#include <kshell.h>
38#include <kurl.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>
45
46#include "tdefiletreeview.h"
47#include "kdirselectdialog.h"
48
49// ### add mutator for treeview!
50
51class KDirSelectDialog::KDirSelectDialogPrivate
52{
53public:
54 KDirSelectDialogPrivate()
55 {
56 urlCombo = 0L;
57 branch = 0L;
58 comboLocked = false;
59 }
60
61 KFileSpeedBar *speedBar;
62 KHistoryCombo *urlCombo;
63 KFileTreeBranch *branch;
64 TQString recentDirClass;
65 KURL startURL;
66 TQValueStack<KURL> dirsToList;
67
68 bool comboLocked : 1;
69};
70
71static KURL rootUrl(const KURL &url)
72{
73 KURL root = url;
74 root.setPath( "/" );
75
76 if (!tdeApp->authorizeURLAction("list", KURL(), root))
77 {
78 root = KURL::fromPathOrURL( TQDir::homeDirPath() );
79 if (!tdeApp->authorizeURLAction("list", KURL(), root))
80 {
81 root = url;
82 }
83 }
84 return root;
85}
86
87KDirSelectDialog::KDirSelectDialog(const TQString &startDir, bool localOnly,
88 TQWidget *parent, const char *name,
89 bool modal)
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 )
94{
95 d = new KDirSelectDialogPrivate;
96 d->branch = 0L;
97
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, TQ_SIGNAL( activated( const KURL& )),
103 TQ_SLOT( setCurrentURL( const KURL& )) );
104 hlay->addWidget( d->speedBar, 0 );
105 hlay->addLayout( m_mainLayout, 1 );
106
107 // Create dir list
108 m_treeView = new KFileTreeView( page );
109 m_treeView->addColumn( i18n("Folders") );
110 m_treeView->setColumnWidthMode( 0, TQListView::Maximum );
111 m_treeView->setResizeMode( TQListView::AllColumns );
112
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, TQ_SIGNAL( textChanged( const TQString& ) ),
122 TQ_SLOT( slotComboTextChanged( const TQString& ) ));
123
124 m_contextMenu = new TQPopupMenu( this );
125 TDEAction* newFolder = new TDEAction( i18n("New Folder..."), "folder-new", 0, this, TQ_SLOT( slotMkdir() ), this);
126 newFolder->plug(m_contextMenu);
127 m_contextMenu->insertSeparator();
128 m_showHiddenFolders = new TDEToggleAction ( i18n( "Show Hidden Folders" ), 0, this,
129 TQ_SLOT( slotShowHiddenFoldersToggled() ), this);
130 m_showHiddenFolders->plug(m_contextMenu);
131
132 d->startURL = KFileDialog::getStartURL( startDir, d->recentDirClass );
133 if ( localOnly && !d->startURL.isLocalFile() )
134 {
135 d->startURL = KURL();
136 TQString docPath = TDEGlobalSettings::documentPath();
137 if (TQDir(docPath).exists())
138 d->startURL.setPath( docPath );
139 else
140 d->startURL.setPath( TQDir::homeDirPath() );
141 }
142
143 KURL root = rootUrl(d->startURL);
144
145 m_startDir = d->startURL.url();
146
147 d->branch = createBranch( root );
148
149 readConfig( TDEGlobal::config(), "DirSelect Dialog" );
150
151 m_mainLayout->addWidget( m_treeView, 1 );
152 m_mainLayout->addWidget( d->urlCombo, 0 );
153
154 connect( m_treeView, TQ_SIGNAL( currentChanged( TQListViewItem * )),
155 TQ_SLOT( slotCurrentChanged() ));
156 connect( m_treeView, TQ_SIGNAL( contextMenu( TDEListView *, TQListViewItem *, const TQPoint & )),
157 TQ_SLOT( slotContextMenu( TDEListView *, TQListViewItem *, const TQPoint & )));
158
159 connect( d->urlCombo, TQ_SIGNAL( activated( const TQString& )),
160 TQ_SLOT( slotURLActivated( const TQString& )));
161 connect( d->urlCombo, TQ_SIGNAL( returnPressed( const TQString& )),
162 TQ_SLOT( slotURLActivated( const TQString& )));
163
164 setCurrentURL( d->startURL );
165}
166
167
168KDirSelectDialog::~KDirSelectDialog()
169{
170 delete d;
171}
172
173void KDirSelectDialog::setCurrentURL( const KURL& url )
174{
175 if ( !url.isValid() )
176 return;
177
178 KURL root = rootUrl(url);
179
180 d->startURL = url;
181 if ( !d->branch ||
182 url.protocol() != d->branch->url().protocol() ||
183 url.host() != d->branch->url().host() )
184 {
185 if ( d->branch )
186 {
187 // removing the root-item causes the currentChanged() signal to be
188 // emitted, but we don't want to update the location-combo yet.
189 d->comboLocked = true;
190 view()->removeBranch( d->branch );
191 d->comboLocked = false;
192 }
193
194 d->branch = createBranch( root );
195 }
196
197 d->branch->disconnect( TQ_SIGNAL( populateFinished( KFileTreeViewItem * )),
198 this, TQ_SLOT( slotNextDirToList( KFileTreeViewItem *)));
199 connect( d->branch, TQ_SIGNAL( populateFinished( KFileTreeViewItem * )),
200 TQ_SLOT( slotNextDirToList( KFileTreeViewItem * ) ));
201
202 KURL dirToList = root;
203 d->dirsToList.clear();
204 TQString path = url.path(+1);
205 int pos = path.length();
206
207 if ( path.isEmpty() ) // e.g. ftp://host.com/ -> just list the root dir
208 d->dirsToList.push( root );
209
210 else
211 {
212 while ( pos > 0 )
213 {
214 pos = path.findRev( '/', pos -1 );
215 if ( pos >= 0 )
216 {
217 dirToList.setPath( path.left( pos +1 ) );
218 d->dirsToList.push( dirToList );
219// tqDebug( "List: %s", dirToList.url().latin1());
220 }
221 }
222 }
223
224 if ( !d->dirsToList.isEmpty() )
225 openNextDir( d->branch->root() );
226}
227
228void KDirSelectDialog::openNextDir( KFileTreeViewItem * /*parent*/ )
229{
230 if ( !d->branch )
231 return;
232
233 KURL url = d->dirsToList.pop();
234
235 KFileTreeViewItem *item = view()->findItem( d->branch, url.path().mid(1));
236 if ( item )
237 {
238 if ( !item->isOpen() )
239 item->setOpen( true );
240 else // already open -> go to next one
241 slotNextDirToList( item );
242 }
243// else
244// tqDebug("###### openNextDir: item not found!");
245}
246
247void KDirSelectDialog::slotNextDirToList( KFileTreeViewItem *item )
248{
249 // scroll to make item the topmost item
250 view()->ensureItemVisible( item );
251 TQRect r = view()->itemRect( item );
252 if ( r.isValid() )
253 {
254 int x, y;
255 view()->viewportToContents( view()->contentsX(), r.y(), x, y );
256 view()->setContentsPos( x, y );
257 }
258
259 if ( !d->dirsToList.isEmpty() )
260 openNextDir( item );
261 else
262 {
263 d->branch->disconnect( TQ_SIGNAL( populateFinished( KFileTreeViewItem * )),
264 this, TQ_SLOT( slotNextDirToList( KFileTreeViewItem *)));
265 view()->setCurrentItem( item );
266 item->setSelected( true );
267 }
268}
269
270void KDirSelectDialog::readConfig( TDEConfig *config, const TQString& group )
271{
272 d->urlCombo->clear();
273
274 TDEConfigGroup conf( config, group );
275 d->urlCombo->setHistoryItems( conf.readPathListEntry( "History Items" ));
276
277 TQSize defaultSize( 400, 450 );
278 resize( conf.readSizeEntry( "DirSelectDialog Size", &defaultSize ));
279}
280
281void KDirSelectDialog::saveConfig( TDEConfig *config, const TQString& group )
282{
283 TDEConfigGroup conf( config, group );
284 conf.writePathEntry( "History Items", d->urlCombo->historyItems(), ',',
285 true, true);
286 conf.writeEntry( "DirSelectDialog Size", size(), true, true );
287
288 d->speedBar->save( config );
289
290 config->sync();
291}
292
293void KDirSelectDialog::slotUser1()
294{
295 slotMkdir();
296}
297
298void KDirSelectDialog::accept()
299{
300 KFileTreeViewItem *item = m_treeView->currentKFileTreeViewItem();
301 if ( !item )
302 return;
303
304 if ( !d->recentDirClass.isEmpty() )
305 {
306 KURL dir = item->url();
307 if ( !item->isDir() )
308 dir = dir.upURL();
309
310 TDERecentDirs::add(d->recentDirClass, dir.url());
311 }
312
313 d->urlCombo->addToHistory( item->url().prettyURL() );
314 KFileDialog::setStartDir( url() );
315
316 KDialogBase::accept();
317 saveConfig( TDEGlobal::config(), "DirSelect Dialog" );
318}
319
320
321KURL KDirSelectDialog::url() const
322{
323 return m_treeView->currentURL();
324}
325
326void KDirSelectDialog::slotCurrentChanged()
327{
328 if ( d->comboLocked )
329 return;
330
331 KFileTreeViewItem *current = view()->currentKFileTreeViewItem();
332 KURL u = current ? current->url() : (d->branch ? d->branch->rootUrl() : KURL());
333
334 if ( u.isValid() )
335 {
336 if ( u.isLocalFile() )
337 d->urlCombo->setEditText( u.path() );
338
339 else // remote url
340 d->urlCombo->setEditText( u.prettyURL() );
341 }
342 else
343 d->urlCombo->setEditText( TQString::null );
344}
345
346void KDirSelectDialog::slotURLActivated( const TQString& text )
347{
348 if ( text.isEmpty() )
349 return;
350
351 KURL url = KURL::fromPathOrURL( text );
352 d->urlCombo->addToHistory( url.prettyURL() );
353
354 if ( localOnly() && !url.isLocalFile() )
355 return; // ### messagebox
356
357 KURL oldURL = m_treeView->currentURL();
358 if ( oldURL.isEmpty() )
359 oldURL = KURL::fromPathOrURL( m_startDir );
360
361 setCurrentURL( url );
362}
363
364KFileTreeBranch * KDirSelectDialog::createBranch( const KURL& url )
365{
366 TQString title = url.isLocalFile() ? url.path() : url.prettyURL();
367 KFileTreeBranch *branch = view()->addBranch( url, title, m_showHiddenFolders->isChecked() );
368 branch->setChildRecurse( false );
369 view()->setDirOnlyMode( branch, true );
370
371 return branch;
372}
373
374void KDirSelectDialog::slotComboTextChanged( const TQString& text )
375{
376 if ( d->branch )
377 {
378 KURL url = KURL::fromPathOrURL( KShell::tildeExpand( text ) );
379 KFileTreeViewItem *item = d->branch->findTVIByURL( url );
380 if ( item )
381 {
382 view()->setCurrentItem( item );
383 view()->setSelected( item, true );
384 view()->ensureItemVisible( item );
385 return;
386 }
387 }
388
389 TQListViewItem *item = view()->currentItem();
390 if ( item )
391 {
392 item->setSelected( false );
393 // 2002/12/27, deselected item is not repainted, so force it
394 item->repaint();
395 }
396}
397
398void KDirSelectDialog::slotContextMenu( TDEListView *, TQListViewItem *, const TQPoint& pos )
399{
400 m_contextMenu->popup( pos );
401}
402
403void KDirSelectDialog::slotMkdir()
404{
405 bool ok;
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 );
410
411 TQString directory = TDEIO::encodeFileName( KInputDialog::getText( i18n( "New Folder" ),
412 i18n( "Create new folder in:\n%1" ).arg( where ),
413 name, &ok, this));
414 if (!ok)
415 return;
416
417 bool selectDirectory = true;
418 bool writeOk = false;
419 bool exists = false;
420 KURL folderurl( url() );
421
422 TQStringList dirs = TQStringList::split( TQDir::separator(), directory );
423 TQStringList::ConstIterator it = dirs.begin();
424
425 for ( ; it != dirs.end(); ++it )
426 {
427 folderurl.addPath( *it );
428 exists = TDEIO::NetAccess::exists( folderurl, false, 0 );
429 writeOk = !exists && TDEIO::NetAccess::mkdir( folderurl, topLevelWidget() );
430 }
431
432 if ( exists ) // url was already existant
433 {
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;
437 }
438 else if ( !writeOk ) {
439 KMessageBox::sorry(this, i18n("You do not have permission to create that folder." ));
440 }
441 else if ( selectDirectory ) {
442 setCurrentURL( folderurl );
443 }
444}
445
446void KDirSelectDialog::slotShowHiddenFoldersToggled()
447{
448 KURL currentURL = url();
449
450 d->comboLocked = true;
451 view()->removeBranch( d->branch );
452 d->comboLocked = false;
453
454 KURL root = rootUrl(d->startURL);
455 d->branch = createBranch( root );
456
457 setCurrentURL( currentURL );
458}
459
460// static
461KURL KDirSelectDialog::selectDirectory( const TQString& startDir,
462 bool localOnly,
463 TQWidget *parent,
464 const TQString& caption)
465{
466 KDirSelectDialog myDialog( startDir, localOnly, parent,
467 "kdirselect dialog", true );
468
469 if ( !caption.isNull() )
470 myDialog.setCaption( caption );
471
472 if ( myDialog.exec() == TQDialog::Accepted )
473 return TDEIO::NetAccess::mostLocalURL(myDialog.url(),parent);
474 else
475 return KURL();
476}
477
478void KDirSelectDialog::virtual_hook( int id, void* data )
479{ KDialogBase::virtual_hook( id, data ); }
480
481#include "kdirselectdialog.moc"
KDirSelectDialog
A pretty dialog for a KDirSelect control for selecting directories.
Definition: kdirselectdialog.h:40
KDirSelectDialog::KDirSelectDialog
KDirSelectDialog(const TQString &startDir=TQString::null, bool localOnly=false, TQWidget *parent=0L, const char *name=0, bool modal=false)
The constructor.
Definition: kdirselectdialog.cpp:87
KDirSelectDialog::url
KURL url() const
Returns the currently-selected URL, or a blank URL if none is selected.
Definition: kdirselectdialog.cpp:321
KDirSelectDialog::selectDirectory
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.
Definition: kdirselectdialog.cpp:461
KDirSelectDialog::startDir
TQString startDir() const
Definition: kdirselectdialog.h:89
KFileDialog::getStartURL
static KURL getStartURL(const TQString &startDir, TQString &recentDirClass)
This method implements the logic to determine the user's default directory to be listed.
Definition: tdefiledialog.cpp:2306
KFileTreeBranch
This is the branch class of the KFileTreeView, which represents one branch in the treeview.
Definition: tdefiletreebranch.h:49
KFileTreeBranch::setChildRecurse
void setChildRecurse(bool t=true)
sets if children recursion is wanted or not.
Definition: tdefiletreebranch.cpp:256
KFileTreeViewItem
An item for a KFileTreeView that knows about its own KFileItem.
Definition: tdefiletreeviewitem.h:41
KFileTreeViewItem::isDir
bool isDir() const
Definition: tdefiletreeviewitem.cpp:80
KFileTreeViewItem::url
KURL url() const
Definition: tdefiletreeviewitem.cpp:70
KFileTreeView
The filetreeview offers a treeview on the file system which behaves like a QTreeView showing files an...
Definition: tdefiletreeview.h:67
KFileTreeView::findItem
KFileTreeViewItem * findItem(KFileTreeBranch *brnch, const TQString &relUrl)
searches a branch for a KFileTreeViewItem identified by the relative url given as second parameter.
Definition: tdefiletreeview.cpp:625
KFileTreeView::setDirOnlyMode
virtual void setDirOnlyMode(KFileTreeBranch *branch, bool)
set the directory mode for branches.
Definition: tdefiletreeview.cpp:428
KFileTreeView::removeBranch
virtual bool removeBranch(KFileTreeBranch *branch)
removes the branch from the treeview.
Definition: tdefiletreeview.cpp:414
KFileTreeView::addBranch
KFileTreeBranch * addBranch(const KURL &path, const TQString &name, bool showHidden=false)
Adds a branch to the treeview item.
Definition: tdefiletreeview.cpp:357
KFileTreeView::currentKFileTreeViewItem
KFileTreeViewItem * currentKFileTreeViewItem() const
Definition: tdefiletreeview.cpp:586
KFileTreeView::currentURL
KURL currentURL() const
Definition: tdefiletreeview.cpp:591
TDERecentDirs::add
static void add(const TQString &fileClass, const TQString &directory)
Associates directory with fileClass.
Definition: tderecentdirs.cpp:86

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdefile by doxygen 1.9.4
This website is maintained by Timothy Pearson.