1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/*
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.
*/
#include <tqdir.h>
#include <tqlayout.h>
#include <tqpopupmenu.h>
#include <tqstringlist.h>
#include <tqvaluestack.h>
#include <kactionclasses.h>
#include <kapplication.h>
#include <kcombobox.h>
#include <kconfig.h>
#include <kfiledialog.h>
#include <kfilespeedbar.h>
#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>
#include <kio/netaccess.h>
#include <kio/renamedlg.h>
#include <kmessagebox.h>
#include "kfiletreeview.h"
#include "kdirselectdialog.h"
// ### add mutator for treeview!
class KDirSelectDialog::KDirSelectDialogPrivate
{
public:
KDirSelectDialogPrivate()
{
urlCombo = 0L;
branch = 0L;
comboLocked = false;
}
KFileSpeedBar *speedBar;
KHistoryCombo *urlCombo;
KFileTreeBranch *branch;
TQString recentDirClass;
KURL startURL;
TQValueStack<KURL> dirsToList;
bool comboLocked : 1;
};
static KURL rootUrl(const KURL &url)
{
KURL root = url;
root.setPath( "/" );
if (!kapp->authorizeURLAction("list", KURL(), root))
{
root = KURL::fromPathOrURL( TQDir::homeDirPath() );
if (!kapp->authorizeURLAction("list", KURL(), root))
{
root = url;
}
}
return root;
}
KDirSelectDialog::KDirSelectDialog(const TQString &startDir, bool localOnly,
TQWidget *parent, const char *name,
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;
TQFrame *page = makeMainWidget();
TQHBoxLayout *hlay = new TQHBoxLayout( page, 0, spacingHint() );
m_mainLayout = new TQVBoxLayout();
d->speedBar = new KFileSpeedBar( page, "speedbar" );
connect( d->speedBar, TQT_SIGNAL( activated( const KURL& )),
TQT_SLOT( setCurrentURL( const KURL& )) );
hlay->addWidget( d->speedBar, 0 );
hlay->addLayout( m_mainLayout, 1 );
// Create dir list
m_treeView = new KFileTreeView( page );
m_treeView->addColumn( i18n("Folders") );
m_treeView->setColumnWidthMode( 0, TQListView::Maximum );
m_treeView->setResizeMode( TQListView::AllColumns );
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 );
connect( d->urlCombo, TQT_SIGNAL( textChanged( const TQString& ) ),
TQT_SLOT( slotComboTextChanged( const TQString& ) ));
m_contextMenu = new TQPopupMenu( this );
KAction* newFolder = new KAction( i18n("New Folder..."), "folder_new", 0, TQT_TQOBJECT(this), TQT_SLOT( slotMkdir() ), TQT_TQOBJECT(this));
newFolder->plug(m_contextMenu);
m_contextMenu->insertSeparator();
m_showHiddenFolders = new KToggleAction ( i18n( "Show Hidden Folders" ), 0, TQT_TQOBJECT(this),
TQT_SLOT( slotShowHiddenFoldersToggled() ), TQT_TQOBJECT(this));
m_showHiddenFolders->plug(m_contextMenu);
d->startURL = KFileDialog::getStartURL( startDir, d->recentDirClass );
if ( localOnly && !d->startURL.isLocalFile() )
{
d->startURL = KURL();
TQString docPath = KGlobalSettings::documentPath();
if (TQDir(docPath).exists())
d->startURL.setPath( docPath );
else
d->startURL.setPath( TQDir::homeDirPath() );
}
KURL root = rootUrl(d->startURL);
m_startDir = d->startURL.url();
d->branch = createBranch( root );
readConfig( KGlobal::config(), "DirSelect Dialog" );
m_mainLayout->addWidget( m_treeView, 1 );
m_mainLayout->addWidget( d->urlCombo, 0 );
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 & )));
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& )));
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 );
}
d->branch->disconnect( TQT_SIGNAL( populateFinished( KFileTreeViewItem * )),
this, TQT_SLOT( slotNextDirToList( KFileTreeViewItem *)));
connect( d->branch, TQT_SIGNAL( populateFinished( KFileTreeViewItem * )),
TQT_SLOT( slotNextDirToList( KFileTreeViewItem * ) ));
KURL dirToList = root;
d->dirsToList.clear();
TQString path = url.path(+1);
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 )
{
pos = path.findRev( '/', pos -1 );
if ( pos >= 0 )
{
dirToList.setPath( path.left( pos +1 ) );
d->dirsToList.push( dirToList );
// qDebug( "List: %s", dirToList.url().latin1());
}
}
}
if ( !d->dirsToList.isEmpty() )
openNextDir( d->branch->root() );
}
void KDirSelectDialog::openNextDir( KFileTreeViewItem * /*parent*/ )
{
if ( !d->branch )
return;
KURL url = d->dirsToList.pop();
KFileTreeViewItem *item = view()->findItem( d->branch, url.path().mid(1));
if ( item )
{
if ( !item->isOpen() )
item->setOpen( true );
else // already open -> go to next one
slotNextDirToList( item );
}
// else
// qDebug("###### openNextDir: item not found!");
}
void KDirSelectDialog::slotNextDirToList( KFileTreeViewItem *item )
{
// scroll to make item the topmost item
view()->ensureItemVisible( item );
TQRect r = view()->itemRect( item );
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
{
d->branch->disconnect( TQT_SIGNAL( populateFinished( KFileTreeViewItem * )),
this, TQT_SLOT( slotNextDirToList( KFileTreeViewItem *)));
view()->setCurrentItem( item );
item->setSelected( true );
}
}
void KDirSelectDialog::readConfig( KConfig *config, const TQString& group )
{
d->urlCombo->clear();
KConfigGroup conf( config, group );
d->urlCombo->setHistoryItems( conf.readPathListEntry( "History Items" ));
TQSize defaultSize( 400, 450 );
resize( conf.readSizeEntry( "DirSelectDialog Size", &defaultSize ));
}
void KDirSelectDialog::saveConfig( KConfig *config, const TQString& group )
{
KConfigGroup conf( config, group );
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();
saveConfig( KGlobal::config(), "DirSelect Dialog" );
}
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
d->urlCombo->setEditText( TQString::null );
}
void KDirSelectDialog::slotURLActivated( const TQString& text )
{
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 )
{
TQString title = url.isLocalFile() ? url.path() : url.prettyURL();
KFileTreeBranch *branch = view()->addBranch( url, title, m_showHiddenFolders->isChecked() );
branch->setChildRecurse( false );
view()->setDirOnlyMode( branch, true );
return branch;
}
void KDirSelectDialog::slotComboTextChanged( const TQString& text )
{
if ( d->branch )
{
KURL url = KURL::fromPathOrURL( KShell::tildeExpand( text ) );
KFileTreeViewItem *item = d->branch->findTVIByURL( url );
if ( item )
{
view()->setCurrentItem( item );
view()->setSelected( item, true );
view()->ensureItemVisible( item );
return;
}
}
TQListViewItem *item = view()->currentItem();
if ( item )
{
item->setSelected( false );
// 2002/12/27, deselected item is not repainted, so force it
item->tqrepaint();
}
}
void KDirSelectDialog::slotContextMenu( KListView *, TQListViewItem *, const TQPoint& pos )
{
m_contextMenu->popup( pos );
}
void KDirSelectDialog::slotMkdir()
{
bool ok;
TQString where = url().pathOrURL();
TQString name = i18n( "New Folder" );
if ( url().isLocalFile() && TQFileInfo( url().path(+1) + name ).exists() )
name = KIO::RenameDlg::suggestName( url(), name );
TQString directory = KIO::encodeFileName( KInputDialog::getText( i18n( "New Folder" ),
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() );
TQStringList dirs = TQStringList::split( TQDir::separator(), directory );
TQStringList::ConstIterator it = dirs.begin();
for ( ; it != dirs.end(); ++it )
{
folderurl.addPath( *it );
exists = KIO::NetAccess::exists( folderurl, false, 0 );
writeOk = !exists && KIO::NetAccess::mkdir( folderurl, topLevelWidget() );
}
if ( exists ) // url was already existant
{
TQString which = folderurl.isLocalFile() ? folderurl.path() : folderurl.prettyURL();
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
KURL KDirSelectDialog::selectDirectory( const TQString& startDir,
bool localOnly,
TQWidget *parent,
const TQString& caption)
{
KDirSelectDialog myDialog( startDir, localOnly, parent,
"kdirselect dialog", true );
if ( !caption.isNull() )
myDialog.setCaption( caption );
if ( myDialog.exec() == TQDialog::Accepted )
return KIO::NetAccess::mostLocalURL(myDialog.url(),parent);
else
return KURL();
}
void KDirSelectDialog::virtual_hook( int id, void* data )
{ KDialogBase::virtual_hook( id, data ); }
#include "kdirselectdialog.moc"
|