author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /microkde/ofileselector_p.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'microkde/ofileselector_p.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/ofileselector_p.cpp | 863 |
1 files changed, 863 insertions, 0 deletions
diff --git a/microkde/ofileselector_p.cpp b/microkde/ofileselector_p.cpp new file mode 100644 index 0000000..cf6074d --- a/dev/null +++ b/microkde/ofileselector_p.cpp | |||
@@ -0,0 +1,863 @@ | |||
1 | #include <qcombobox.h> | ||
2 | #include <qdir.h> | ||
3 | #include <qhbox.h> | ||
4 | #include <qheader.h> | ||
5 | #include <qlabel.h> | ||
6 | #include <qlayout.h> | ||
7 | #include <qlineedit.h> | ||
8 | #include <qlistview.h> | ||
9 | #include <qpopupmenu.h> | ||
10 | #include <qwidgetstack.h> | ||
11 | #include <qregexp.h> | ||
12 | #include <qobjectlist.h> | ||
13 | |||
14 | /* hacky but we need to get FileSelector::filter */ | ||
15 | #define private public | ||
16 | #include <qpe/fileselector.h> | ||
17 | #undef private | ||
18 | |||
19 | #include <qpe/qpeapplication.h> | ||
20 | #include <qpe/mimetype.h> | ||
21 | #include <qpe/resource.h> | ||
22 | #include <qpe/storage.h> | ||
23 | |||
24 | #include "ofileselector_p.h" | ||
25 | //US#include "ofileselector.h" | ||
26 | |||
27 | #include "klocale.h" | ||
28 | |||
29 | OFileViewInterface::OFileViewInterface( OFileSelector* selector ) | ||
30 | : m_selector( selector ) { | ||
31 | } | ||
32 | OFileViewInterface::~OFileViewInterface() { | ||
33 | } | ||
34 | QString OFileViewInterface::name()const{ | ||
35 | return m_name; | ||
36 | } | ||
37 | void OFileViewInterface::setName( const QString& name ) { | ||
38 | m_name = name; | ||
39 | } | ||
40 | OFileSelector* OFileViewInterface::selector()const { | ||
41 | return m_selector; | ||
42 | } | ||
43 | |||
44 | DocLnk OFileViewInterface::selectedDocument()const { | ||
45 | return DocLnk( selectedName() ); | ||
46 | } | ||
47 | |||
48 | bool OFileViewInterface::showNew()const { | ||
49 | return selector()->showNew(); | ||
50 | } | ||
51 | bool OFileViewInterface::showClose()const { | ||
52 | return selector()->showClose(); | ||
53 | } | ||
54 | MimeTypes OFileViewInterface::mimeTypes()const { | ||
55 | return selector()->mimeTypes(); | ||
56 | } | ||
57 | QStringList OFileViewInterface::currentMimeType()const { | ||
58 | return selector()->currentMimeType(); | ||
59 | } | ||
60 | void OFileViewInterface::activate( const QString& ) { | ||
61 | // not implemented here | ||
62 | } | ||
63 | void OFileViewInterface::ok() { | ||
64 | emit selector()->ok(); | ||
65 | } | ||
66 | void OFileViewInterface::cancel() { | ||
67 | emit selector()->cancel(); | ||
68 | } | ||
69 | void OFileViewInterface::closeMe() { | ||
70 | emit selector()->closeMe(); | ||
71 | } | ||
72 | void OFileViewInterface::fileSelected( const QString& str) { | ||
73 | emit selector()->fileSelected( str); | ||
74 | } | ||
75 | void OFileViewInterface::fileSelected( const DocLnk& lnk) { | ||
76 | emit selector()->fileSelected( lnk ); | ||
77 | } | ||
78 | void OFileViewInterface::setCurrentFileName( const QString& str ) { | ||
79 | selector()->m_lneEdit->setText( str ); | ||
80 | } | ||
81 | QString OFileViewInterface::currentFileName()const{ | ||
82 | return selector()->m_lneEdit->text(); | ||
83 | } | ||
84 | QString OFileViewInterface::startDirectory()const{ | ||
85 | return selector()->m_startDir; | ||
86 | } | ||
87 | |||
88 | |||
89 | ODocumentFileView::ODocumentFileView( OFileSelector* selector ) | ||
90 | : OFileViewInterface( selector ) { | ||
91 | m_selector = 0; | ||
92 | setName( i18n("Documents") ); | ||
93 | } | ||
94 | ODocumentFileView::~ODocumentFileView() { | ||
95 | |||
96 | } | ||
97 | QString ODocumentFileView::selectedName()const { | ||
98 | if (!m_selector) | ||
99 | return QString::null; | ||
100 | |||
101 | return m_selector->selected()->file(); | ||
102 | } | ||
103 | QString ODocumentFileView::selectedPath()const { | ||
104 | return QPEApplication::documentDir(); | ||
105 | } | ||
106 | QString ODocumentFileView::directory()const { | ||
107 | return selectedPath(); | ||
108 | } | ||
109 | void ODocumentFileView::reread() { | ||
110 | if (!m_selector) | ||
111 | return; | ||
112 | |||
113 | m_selector->setNewVisible( showNew() ); | ||
114 | m_selector->setCloseVisible( showClose() ); | ||
115 | m_selector->filter = currentMimeType().join(";"); | ||
116 | m_selector->reread(); | ||
117 | } | ||
118 | int ODocumentFileView::fileCount()const { | ||
119 | if (!m_selector) | ||
120 | return -1; | ||
121 | |||
122 | return m_selector->fileCount(); | ||
123 | } | ||
124 | |||
125 | DocLnk ODocumentFileView::selectedDocument()const { | ||
126 | if (!m_selector) | ||
127 | return DocLnk(); | ||
128 | DocLnk lnk = *m_selector->selected(); | ||
129 | return lnk; | ||
130 | } | ||
131 | |||
132 | QWidget* ODocumentFileView::widget( QWidget* parent ) { | ||
133 | if (!m_selector ) { | ||
134 | m_selector = new FileSelector(currentMimeType().join(";"), parent, "fileselector", showNew(), showClose() ); | ||
135 | QObject::connect(m_selector, SIGNAL(fileSelected( const DocLnk& ) ), | ||
136 | selector(), SLOT(slotDocLnkBridge(const DocLnk&) ) ); | ||
137 | QObject::connect(m_selector, SIGNAL(closeMe() ), | ||
138 | selector(), SIGNAL(closeMe() ) ); | ||
139 | QObject::connect(m_selector, SIGNAL(newSelected(const DocLnk& ) ), | ||
140 | selector(), SIGNAL(newSelected(const DocLnk& ) ) ); | ||
141 | } | ||
142 | |||
143 | return m_selector; | ||
144 | } | ||
145 | |||
146 | /* | ||
147 | * This is the file system view used | ||
148 | * we use a QListView + QListViewItems for it | ||
149 | */ | ||
150 | |||
151 | OFileSelectorItem::OFileSelectorItem( QListView* view, const QPixmap& pixmap, | ||
152 | const QString& path, const QString& date, | ||
153 | const QString& size, const QString& dir, | ||
154 | bool isLocked, bool isDir ) | ||
155 | : QListViewItem( view ) | ||
156 | { | ||
157 | setPixmap(0, pixmap ); | ||
158 | setText(1, path ); | ||
159 | setText(2, size ); | ||
160 | setText(3, date ); | ||
161 | m_isDir = isDir; | ||
162 | m_dir = dir; | ||
163 | m_locked = isLocked; | ||
164 | } | ||
165 | OFileSelectorItem::~OFileSelectorItem() { | ||
166 | |||
167 | } | ||
168 | bool OFileSelectorItem::isLocked()const { | ||
169 | return m_locked; | ||
170 | } | ||
171 | QString OFileSelectorItem::directory()const { | ||
172 | return m_dir; | ||
173 | } | ||
174 | bool OFileSelectorItem::isDir()const { | ||
175 | return m_isDir; | ||
176 | } | ||
177 | QString OFileSelectorItem::path()const { | ||
178 | return text( 1 ); | ||
179 | } | ||
180 | QString OFileSelectorItem::key( int id, bool )const { | ||
181 | QString ke; | ||
182 | if( id == 0 || id == 1 ){ // name | ||
183 | if( m_isDir ){ | ||
184 | ke.append("0" ); | ||
185 | ke.append( text(1) ); | ||
186 | }else{ | ||
187 | ke.append("1" ); | ||
188 | ke.append( text(1) ); | ||
189 | } | ||
190 | return ke; | ||
191 | }else | ||
192 | return text( id ); | ||
193 | |||
194 | } | ||
195 | |||
196 | OFileViewFileListView::OFileViewFileListView( QWidget* parent, const QString& startDir, | ||
197 | OFileSelector* sel) | ||
198 | : QWidget( parent ), m_sel( sel ) { | ||
199 | m_all = false; | ||
200 | QVBoxLayout* lay = new QVBoxLayout( this ); | ||
201 | m_currentDir = startDir; | ||
202 | |||
203 | /* | ||
204 | * now we add a special bar | ||
205 | * One Button For Up | ||
206 | * Home | ||
207 | * Doc | ||
208 | * And a dropdown menu with FileSystems | ||
209 | * FUTURE: one to change dir with lineedit | ||
210 | * Bookmarks | ||
211 | * Create Dir | ||
212 | */ | ||
213 | QHBox* box = new QHBox(this ); | ||
214 | box->setBackgroundMode( PaletteButton ); | ||
215 | box->setSpacing( 0 ); | ||
216 | |||
217 | QToolButton *btn = new QToolButton( box ); | ||
218 | btn->setIconSet( Resource::loadPixmap("up") ); | ||
219 | connect(btn, SIGNAL(clicked() ), | ||
220 | this, SLOT( cdUP() ) ); | ||
221 | |||
222 | btn = new QToolButton( box ); | ||
223 | btn->setIconSet( Resource::loadPixmap("home") ); | ||
224 | connect(btn, SIGNAL(clicked() ), | ||
225 | this, SLOT( cdHome() ) ); | ||
226 | |||
227 | btn = new QToolButton( box ); | ||
228 | btn->setIconSet( Resource::loadPixmap("DocsIcon") ); | ||
229 | connect(btn, SIGNAL(clicked() ), | ||
230 | this, SLOT(cdDoc() ) ); | ||
231 | |||
232 | m_btnNew = new QToolButton( box ); | ||
233 | m_btnNew->setIconSet( Resource::loadPixmap("new") ); | ||
234 | connect(m_btnNew, SIGNAL(clicked() ), | ||
235 | this, SLOT(slotNew() ) ); | ||
236 | |||
237 | |||
238 | m_btnClose = new QToolButton( box ); | ||
239 | m_btnClose->setIconSet( Resource::loadPixmap("close") ); | ||
240 | connect(m_btnClose, SIGNAL(clicked() ), | ||
241 | selector(), SIGNAL(closeMe() ) ); | ||
242 | |||
243 | btn = new QToolButton( box ); | ||
244 | btn->setIconSet( Resource::loadPixmap("cardmon/pcmcia") ); | ||
245 | |||
246 | /* let's fill device parts */ | ||
247 | QPopupMenu* pop = new QPopupMenu(this); | ||
248 | connect(pop, SIGNAL( activated(int) ), | ||
249 | this, SLOT(slotFSActivated(int) ) ); | ||
250 | |||
251 | StorageInfo storage; | ||
252 | const QList<FileSystem> &fs = storage.fileSystems(); | ||
253 | QListIterator<FileSystem> it(fs); | ||
254 | for ( ; it.current(); ++it ) { | ||
255 | const QString disk = (*it)->name(); | ||
256 | const QString path = (*it)->path(); | ||
257 | m_dev.insert( disk, path ); | ||
258 | pop->insertItem( disk ); | ||
259 | } | ||
260 | m_fsPop = pop; | ||
261 | |||
262 | |||
263 | btn->setPopup( pop ); | ||
264 | btn->setPopupDelay ( 0 ); | ||
265 | lay->addWidget( box ); | ||
266 | |||
267 | m_view = new QListView( this ); | ||
268 | |||
269 | m_view->installEventFilter(this); | ||
270 | |||
271 | QPEApplication::setStylusOperation( m_view->viewport(), | ||
272 | QPEApplication::RightOnHold); | ||
273 | m_view->addColumn(" " ); | ||
274 | m_view->addColumn(i18n("Name"), 135 ); | ||
275 | m_view->addColumn(i18n("Size"), -1 ); | ||
276 | m_view->addColumn(i18n("Date"), 60 ); | ||
277 | m_view->addColumn(i18n("Mime Type"), -1 ); | ||
278 | |||
279 | |||
280 | m_view->setSorting( 1 ); | ||
281 | m_view->setAllColumnsShowFocus( TRUE ); | ||
282 | |||
283 | lay->addWidget( m_view, 1000 ); | ||
284 | connectSlots(); | ||
285 | } | ||
286 | OFileViewFileListView::~OFileViewFileListView() { | ||
287 | } | ||
288 | void OFileViewFileListView::slotNew() { | ||
289 | DocLnk lnk; | ||
290 | emit selector()->newSelected( lnk ); | ||
291 | } | ||
292 | OFileSelectorItem* OFileViewFileListView::currentItem()const{ | ||
293 | QListViewItem* item = m_view->currentItem(); | ||
294 | if (!item ) | ||
295 | return 0l; | ||
296 | |||
297 | return static_cast<OFileSelectorItem*>(item); | ||
298 | } | ||
299 | void OFileViewFileListView::reread( bool all ) { | ||
300 | m_view->clear(); | ||
301 | |||
302 | if (selector()->showClose() ) | ||
303 | m_btnClose->show(); | ||
304 | else | ||
305 | m_btnClose->hide(); | ||
306 | |||
307 | if (selector()->showNew() ) | ||
308 | m_btnNew->show(); | ||
309 | else | ||
310 | m_btnNew->hide(); | ||
311 | |||
312 | m_mimes = selector()->currentMimeType(); | ||
313 | m_all = all; | ||
314 | |||
315 | QDir dir( m_currentDir ); | ||
316 | if (!dir.exists() ) | ||
317 | return; | ||
318 | topLevelWidget()->setCaption( dir.path() ); | ||
319 | dir.setSorting( QDir::Name | QDir::DirsFirst | QDir::Reversed ); | ||
320 | int filter; | ||
321 | if (m_all ) | ||
322 | filter = QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All; | ||
323 | else | ||
324 | filter = QDir::Files | QDir::Dirs | QDir::All; | ||
325 | dir.setFilter( filter ); | ||
326 | |||
327 | // now go through all files | ||
328 | const QFileInfoList *list = dir.entryInfoList(); | ||
329 | if (!list) { | ||
330 | cdUP(); | ||
331 | return; | ||
332 | } | ||
333 | QFileInfoListIterator it( *list ); | ||
334 | QFileInfo *fi; | ||
335 | while( (fi=it.current() ) ){ | ||
336 | if( fi->fileName() == QString::fromLatin1("..") || fi->fileName() == QString::fromLatin1(".") ){ | ||
337 | ++it; | ||
338 | continue; | ||
339 | } | ||
340 | |||
341 | /* | ||
342 | * It is a symlink we try to resolve it now but don't let us attack by DOS | ||
343 | * | ||
344 | */ | ||
345 | if( fi->isSymLink() ){ | ||
346 | QString file = fi->dirPath( true ) + "/" + fi->readLink(); | ||
347 | for( int i = 0; i<=4; i++) { // 5 tries to prevent dos | ||
348 | QFileInfo info( file ); | ||
349 | if( !info.exists() ){ | ||
350 | addSymlink( fi, TRUE ); | ||
351 | break; | ||
352 | }else if( info.isDir() ){ | ||
353 | addDir( fi, TRUE ); | ||
354 | break; | ||
355 | }else if( info.isFile() ){ | ||
356 | addFile( fi, TRUE ); | ||
357 | break; | ||
358 | }else if( info.isSymLink() ){ | ||
359 | file = info.dirPath(true ) + "/" + info.readLink() ; | ||
360 | break; | ||
361 | }else if( i == 4){ // couldn't resolve symlink add it as symlink | ||
362 | addSymlink( fi ); | ||
363 | } | ||
364 | } // off for loop for symlink resolving | ||
365 | }else if( fi->isDir() ) | ||
366 | addDir( fi ); | ||
367 | else if( fi->isFile() ) | ||
368 | addFile( fi ); | ||
369 | |||
370 | ++it; | ||
371 | } // of while loop | ||
372 | m_view->sort(); | ||
373 | |||
374 | } | ||
375 | int OFileViewFileListView::fileCount()const{ | ||
376 | return m_view->childCount(); | ||
377 | } | ||
378 | QString OFileViewFileListView::currentDir()const{ | ||
379 | return m_currentDir; | ||
380 | } | ||
381 | OFileSelector* OFileViewFileListView::selector() { | ||
382 | return m_sel; | ||
383 | } | ||
384 | |||
385 | bool OFileViewFileListView::eventFilter (QObject *o, QEvent *e) { | ||
386 | if ( e->type() == QEvent::KeyPress ) { | ||
387 | QKeyEvent *k = (QKeyEvent *)e; | ||
388 | if ( (k->key()==Key_Enter) || (k->key()==Key_Return)) { | ||
389 | slotClicked( Qt::LeftButton,m_view->currentItem(),QPoint(0,0),0); | ||
390 | return true; | ||
391 | } | ||
392 | } | ||
393 | return false; | ||
394 | } | ||
395 | |||
396 | |||
397 | void OFileViewFileListView::connectSlots() { | ||
398 | connect(m_view, SIGNAL(clicked(QListViewItem*) ), | ||
399 | this, SLOT(slotCurrentChanged(QListViewItem*) ) ); | ||
400 | connect(m_view, SIGNAL(mouseButtonClicked(int, QListViewItem*, const QPoint&, int ) ), | ||
401 | this, SLOT(slotClicked(int, QListViewItem*, const QPoint&, int ) ) ); | ||
402 | } | ||
403 | void OFileViewFileListView::slotCurrentChanged( QListViewItem* item) { | ||
404 | if (!item) | ||
405 | return; | ||
406 | #if 0 | ||
407 | |||
408 | OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item); | ||
409 | |||
410 | if (!sel->isDir() ) { | ||
411 | selector()->m_lneEdit->setText( sel->text(1) ); | ||
412 | // if in fileselector mode we will emit selected | ||
413 | if ( selector()->mode() == OFileSelector::FileSelector ) { | ||
414 | qWarning("slot Current Changed"); | ||
415 | QStringList str = QStringList::split("->", sel->text(1) ); | ||
416 | QString path = sel->directory() + "/" + str[0].stripWhiteSpace(); | ||
417 | emit selector()->fileSelected( path ); | ||
418 | DocLnk lnk( path ); | ||
419 | emit selector()->fileSelected( lnk ); | ||
420 | } | ||
421 | } | ||
422 | #endif | ||
423 | } | ||
424 | void OFileViewFileListView::slotClicked(int button , QListViewItem* item, const QPoint&, int ) { | ||
425 | if (!item || ( button != Qt::LeftButton) ) | ||
426 | return; | ||
427 | |||
428 | OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item); | ||
429 | if (!sel->isLocked() ) { | ||
430 | QStringList str = QStringList::split("->", sel->text(1) ); | ||
431 | if (sel->isDir() ) { | ||
432 | m_currentDir = sel->directory() + "/" + str[0].stripWhiteSpace(); | ||
433 | emit selector()->dirSelected( m_currentDir ); | ||
434 | reread( m_all ); | ||
435 | }else { // file | ||
436 | qWarning("slot Clicked"); | ||
437 | selector()->m_lneEdit->setText( str[0].stripWhiteSpace() ); | ||
438 | QString path = sel->directory() + "/" + str[0].stripWhiteSpace(); | ||
439 | emit selector()->fileSelected( path ); | ||
440 | DocLnk lnk( path ); | ||
441 | emit selector()->fileSelected( lnk ); | ||
442 | } | ||
443 | } // not locked | ||
444 | } | ||
445 | void OFileViewFileListView::addFile( QFileInfo* info, bool symlink ) { | ||
446 | MimeType type( info->absFilePath() ); | ||
447 | if (!compliesMime( type.id() ) ) | ||
448 | return; | ||
449 | |||
450 | QPixmap pix = type.pixmap(); | ||
451 | QString dir, name; bool locked; | ||
452 | if ( pix.isNull() ) { | ||
453 | QWMatrix matrix; | ||
454 | QPixmap pixer(Resource::loadPixmap("UnknownDocument") ); | ||
455 | matrix.scale( .4, .4 ); | ||
456 | pix = pixer.xForm( matrix ); | ||
457 | } | ||
458 | dir = info->dirPath( true ); | ||
459 | locked = false; | ||
460 | if ( symlink ) | ||
461 | name = info->fileName() + " -> " + info->dirPath() + "/" + info->readLink(); | ||
462 | else{ | ||
463 | name = info->fileName(); | ||
464 | if ( ( (selector()->mode() == OFileSelector::Open)&& !info->isReadable() ) || | ||
465 | ( (selector()->mode() == OFileSelector::Save)&& !info->isWritable() ) ) { | ||
466 | locked = true; pix = Resource::loadPixmap("locked"); | ||
467 | } | ||
468 | } | ||
469 | (void)new OFileSelectorItem( m_view, pix, name, | ||
470 | info->lastModified().toString(), QString::number( info->size() ), | ||
471 | dir, locked ); | ||
472 | } | ||
473 | void OFileViewFileListView::addDir( QFileInfo* info, bool symlink ) { | ||
474 | bool locked = false; QString name; QPixmap pix; | ||
475 | |||
476 | if ( ( ( selector()->mode() == OFileSelector::Open ) && !info->isReadable() ) || | ||
477 | ( ( selector()->mode() == OFileSelector::Save ) && !info->isWritable() ) ) { | ||
478 | locked = true; | ||
479 | if ( symlink ) | ||
480 | pix = Resource::loadPixmap( "symlink" ); | ||
481 | else | ||
482 | pix = Resource::loadPixmap( "lockedfolder" ); | ||
483 | }else | ||
484 | pix = symlink ? Resource::loadPixmap( "symlink") : Resource::loadPixmap("folder"); | ||
485 | |||
486 | name = symlink ? info->fileName() + " -> " + info->dirPath(true) + "/" + info->readLink() : | ||
487 | info->fileName(); | ||
488 | |||
489 | (void)new OFileSelectorItem( m_view, pix, name, | ||
490 | info->lastModified().toString(), | ||
491 | QString::number( info->size() ), | ||
492 | info->dirPath( true ), locked, true ); | ||
493 | |||
494 | |||
495 | } | ||
496 | void OFileViewFileListView::addSymlink( QFileInfo* , bool ) { | ||
497 | |||
498 | } | ||
499 | void OFileViewFileListView::cdUP() { | ||
500 | QDir dir( m_currentDir ); | ||
501 | dir.cdUp(); | ||
502 | |||
503 | if (!dir.exists() ) | ||
504 | m_currentDir = "/"; | ||
505 | else | ||
506 | m_currentDir = dir.absPath(); | ||
507 | |||
508 | emit selector()->dirSelected( m_currentDir ); | ||
509 | reread( m_all ); | ||
510 | } | ||
511 | void OFileViewFileListView::cdHome() { | ||
512 | m_currentDir = QDir::homeDirPath(); | ||
513 | emit selector()->dirSelected( m_currentDir ); | ||
514 | reread( m_all ); | ||
515 | } | ||
516 | void OFileViewFileListView::cdDoc() { | ||
517 | m_currentDir = QPEApplication::documentDir(); | ||
518 | emit selector()->dirSelected( m_currentDir ); | ||
519 | reread( m_all ); | ||
520 | } | ||
521 | void OFileViewFileListView::changeDir( const QString& dir ) { | ||
522 | m_currentDir = dir; | ||
523 | emit selector()->dirSelected( m_currentDir ); | ||
524 | reread( m_all ); | ||
525 | } | ||
526 | void OFileViewFileListView::slotFSActivated( int id ) { | ||
527 | changeDir ( m_dev[m_fsPop->text(id)] ); | ||
528 | } | ||
529 | |||
530 | /* check if the mimetype in mime | ||
531 | * complies with the one which is current | ||
532 | */ | ||
533 | /* | ||
534 | * We've the mimetype of the file | ||
535 | * We need to get the stringlist of the current mimetype | ||
536 | * | ||
537 | * mime = image@slashjpeg | ||
538 | * QStringList = 'image@slash*' | ||
539 | * or QStringList = image/jpeg;image/png;application/x-ogg | ||
540 | * or QStringList = application/x-ogg;image@slash*; | ||
541 | * with all these mime filters it should get acceptes | ||
542 | * to do so we need to look if mime is contained inside | ||
543 | * the stringlist | ||
544 | * if it's contained return true | ||
545 | * if not ( I'm no RegExp expert at all ) we'll look if a '@slash*' | ||
546 | * is contained in the mimefilter and then we will | ||
547 | * look if both are equal until the '/' | ||
548 | */ | ||
549 | bool OFileViewFileListView::compliesMime( const QString& str) { | ||
550 | if (str.isEmpty() || m_mimes.isEmpty() || str.stripWhiteSpace().isEmpty() ) | ||
551 | return true; | ||
552 | |||
553 | for (QStringList::Iterator it = m_mimes.begin(); it != m_mimes.end(); ++it ) { | ||
554 | QRegExp reg( (*it) ); | ||
555 | reg.setWildcard( true ); | ||
556 | if ( str.find( reg ) != -1 ) | ||
557 | return true; | ||
558 | |||
559 | } | ||
560 | return false; | ||
561 | } | ||
562 | /* | ||
563 | * The listView giving access to the file system! | ||
564 | */ | ||
565 | class OFileViewFileSystem : public OFileViewInterface { | ||
566 | public: | ||
567 | OFileViewFileSystem( OFileSelector* ); | ||
568 | ~OFileViewFileSystem(); | ||
569 | |||
570 | QString selectedName() const; | ||
571 | QString selectedPath() const; | ||
572 | |||
573 | QString directory()const; | ||
574 | void reread(); | ||
575 | int fileCount()const; | ||
576 | |||
577 | QWidget* widget( QWidget* parent ); | ||
578 | void activate( const QString& ); | ||
579 | private: | ||
580 | OFileViewFileListView* m_view; | ||
581 | bool m_all : 1; | ||
582 | }; | ||
583 | OFileViewFileSystem::OFileViewFileSystem( OFileSelector* sel) | ||
584 | : OFileViewInterface( sel ) { | ||
585 | m_view = 0; | ||
586 | m_all = false; | ||
587 | } | ||
588 | OFileViewFileSystem::~OFileViewFileSystem() { | ||
589 | } | ||
590 | QString OFileViewFileSystem::selectedName()const{ | ||
591 | if (!m_view ) | ||
592 | return QString::null; | ||
593 | |||
594 | QString cFN=currentFileName(); | ||
595 | if (cFN.startsWith("/")) return cFN; | ||
596 | return m_view->currentDir() + "/" + cFN; | ||
597 | } | ||
598 | QString OFileViewFileSystem::selectedPath()const{ | ||
599 | return QString::null; | ||
600 | } | ||
601 | QString OFileViewFileSystem::directory()const{ | ||
602 | if (!m_view) | ||
603 | return QString::null; | ||
604 | |||
605 | OFileSelectorItem* item = m_view->currentItem(); | ||
606 | if (!item ) | ||
607 | return QString::null; | ||
608 | |||
609 | return QDir(item->directory() ).absPath(); | ||
610 | } | ||
611 | void OFileViewFileSystem::reread() { | ||
612 | if (!m_view) | ||
613 | return; | ||
614 | |||
615 | m_view->reread( m_all ); | ||
616 | } | ||
617 | int OFileViewFileSystem::fileCount()const{ | ||
618 | if (!m_view ) | ||
619 | return -1; | ||
620 | return m_view->fileCount(); | ||
621 | } | ||
622 | QWidget* OFileViewFileSystem::widget( QWidget* parent ) { | ||
623 | if (!m_view ) { | ||
624 | m_view = new OFileViewFileListView( parent, startDirectory(), selector() ); | ||
625 | } | ||
626 | return m_view; | ||
627 | } | ||
628 | void OFileViewFileSystem::activate( const QString& str) { | ||
629 | m_all = (str !=i18n("Files") ); | ||
630 | |||
631 | |||
632 | } | ||
633 | |||
634 | /* Selector */ | ||
635 | OFileSelector::OFileSelector( QWidget* parent, int mode, int sel, | ||
636 | const QString& dirName, const QString& fileName, | ||
637 | const MimeTypes& mimetypes, | ||
638 | bool showNew, bool showClose) | ||
639 | : QWidget( parent, "OFileSelector" ) | ||
640 | { | ||
641 | m_current = 0; | ||
642 | m_shNew = showNew; | ||
643 | m_shClose = showClose; | ||
644 | m_mimeType = mimetypes; | ||
645 | m_startDir = dirName; | ||
646 | m_mode = mode; | ||
647 | m_selector = sel; | ||
648 | |||
649 | initUI(); | ||
650 | m_lneEdit->setText( fileName ); | ||
651 | initMime(); | ||
652 | initViews(); | ||
653 | |||
654 | QString str; | ||
655 | switch ( m_selector ) { | ||
656 | default: | ||
657 | case Normal: | ||
658 | str = i18n("Documents"); | ||
659 | m_cmbView->setCurrentItem( 0 ); | ||
660 | break; | ||
661 | case Extended: | ||
662 | str = i18n("Files"); | ||
663 | m_cmbView->setCurrentItem( 1 ); | ||
664 | break; | ||
665 | case ExtendedAll: | ||
666 | str = i18n("All Files"); | ||
667 | m_cmbView->setCurrentItem( 2 ); | ||
668 | break; | ||
669 | } | ||
670 | slotViewChange( str ); | ||
671 | |||
672 | } | ||
673 | OFileSelector::OFileSelector( const QString& mimeFilter, QWidget* parent, const char* name, | ||
674 | bool showNew, bool showClose ) | ||
675 | : QWidget( parent, name ) | ||
676 | { | ||
677 | m_current = 0; | ||
678 | m_shNew = showNew; | ||
679 | m_shClose = showClose; | ||
680 | m_startDir = QPEApplication::documentDir(); | ||
681 | |||
682 | if (!mimeFilter.isEmpty() ) | ||
683 | m_mimeType.insert(mimeFilter, QStringList::split(";", mimeFilter ) ); | ||
684 | |||
685 | m_mode = OFileSelector::FileSelector; | ||
686 | m_selector = OFileSelector::Normal; | ||
687 | |||
688 | initUI(); | ||
689 | initMime(); | ||
690 | initViews(); | ||
691 | m_cmbView->setCurrentItem( 0 ); | ||
692 | slotViewChange( i18n("Documents") ); | ||
693 | } | ||
694 | /* | ||
695 | * INIT UI will set up the basic GUI | ||
696 | * Layout: Simple VBoxLayout | ||
697 | * On top a WidgetStack containing the Views... | ||
698 | * - List View | ||
699 | * - Document View | ||
700 | * Below we will have a Label + LineEdit | ||
701 | * Below we will have two ComoBoxes one for choosing the view one for | ||
702 | * choosing the mimetype | ||
703 | */ | ||
704 | void OFileSelector::initUI() { | ||
705 | QVBoxLayout* lay = new QVBoxLayout( this ); | ||
706 | |||
707 | m_stack = new QWidgetStack( this ); | ||
708 | lay->addWidget( m_stack, 1000 ); | ||
709 | |||
710 | m_nameBox = new QHBox( this ); | ||
711 | (void)new QLabel( i18n("Name:"), m_nameBox ); | ||
712 | m_lneEdit = new QLineEdit( m_nameBox ); | ||
713 | m_lneEdit ->installEventFilter(this); | ||
714 | lay->addWidget( m_nameBox ); | ||
715 | |||
716 | m_cmbBox = new QHBox( this ); | ||
717 | m_cmbView = new QComboBox( m_cmbBox ); | ||
718 | m_cmbMime = new QComboBox( m_cmbBox ); | ||
719 | lay->addWidget( m_cmbBox ); | ||
720 | } | ||
721 | |||
722 | /* | ||
723 | * This will make sure that the return key in the name edit causes dialogs to close | ||
724 | */ | ||
725 | |||
726 | bool OFileSelector::eventFilter (QObject *o, QEvent *e) { | ||
727 | if ( e->type() == QEvent::KeyPress ) { | ||
728 | QKeyEvent *k = (QKeyEvent *)e; | ||
729 | if ( (k->key()==Key_Enter) || (k->key()==Key_Return)) { | ||
730 | emit ok(); | ||
731 | return true; | ||
732 | } | ||
733 | } | ||
734 | return false; | ||
735 | } | ||
736 | |||
737 | /* | ||
738 | * This will insert the MimeTypes into the Combo Box | ||
739 | * And also connect the changed signal | ||
740 | * | ||
741 | * AutoMimeTyping is disabled for now. It used to reparse a dir and then set available mimetypes | ||
742 | */ | ||
743 | void OFileSelector::initMime() { | ||
744 | MimeTypes::Iterator it; | ||
745 | for ( it = m_mimeType.begin(); it != m_mimeType.end(); ++it ) { | ||
746 | m_cmbMime->insertItem( it.key() ); | ||
747 | } | ||
748 | m_cmbMime->setCurrentItem( 0 ); | ||
749 | |||
750 | connect( m_cmbMime, SIGNAL(activated(int) ), | ||
751 | this, SLOT(slotMimeTypeChanged() ) ); | ||
752 | |||
753 | } | ||
754 | void OFileSelector::initViews() { | ||
755 | m_cmbView->insertItem( i18n("Documents") ); | ||
756 | m_cmbView->insertItem( i18n("Files") ); | ||
757 | m_cmbView->insertItem( i18n("All Files") ); | ||
758 | connect(m_cmbView, SIGNAL(activated( const QString& ) ), | ||
759 | this, SLOT(slotViewChange( const QString& ) ) ); | ||
760 | |||
761 | |||
762 | m_views.insert( i18n("Documents"), new ODocumentFileView(this) ); | ||
763 | |||
764 | /* see above why add both */ | ||
765 | OFileViewInterface* in = new OFileViewFileSystem( this ); | ||
766 | m_views.insert( i18n("Files"), in ); | ||
767 | m_views.insert( i18n("All Files"), in ); | ||
768 | } | ||
769 | OFileSelector::~OFileSelector() { | ||
770 | |||
771 | } | ||
772 | |||
773 | const DocLnk* OFileSelector::selected() { | ||
774 | DocLnk* lnk = ¤tView()->selectedDocument() ; | ||
775 | return lnk; | ||
776 | } | ||
777 | |||
778 | QString OFileSelector::selectedName()const{ | ||
779 | return currentView()->selectedName(); | ||
780 | } | ||
781 | QString OFileSelector::selectedPath()const { | ||
782 | return currentView()->selectedPath(); | ||
783 | } | ||
784 | QString OFileSelector::directory()const { | ||
785 | return currentView()->directory(); | ||
786 | } | ||
787 | |||
788 | DocLnk OFileSelector::selectedDocument()const { | ||
789 | return currentView()->selectedDocument(); | ||
790 | } | ||
791 | |||
792 | int OFileSelector::fileCount()const { | ||
793 | return currentView()->fileCount(); | ||
794 | } | ||
795 | void OFileSelector::reread() { | ||
796 | return currentView()->reread(); | ||
797 | } | ||
798 | OFileViewInterface* OFileSelector::currentView()const{ | ||
799 | return m_current; | ||
800 | } | ||
801 | bool OFileSelector::showNew()const { | ||
802 | return m_shNew; | ||
803 | } | ||
804 | bool OFileSelector::showClose()const { | ||
805 | return m_shClose; | ||
806 | } | ||
807 | MimeTypes OFileSelector::mimeTypes()const { | ||
808 | return m_mimeType; | ||
809 | } | ||
810 | int OFileSelector::mode()const{ | ||
811 | return m_mode; | ||
812 | } | ||
813 | int OFileSelector::selector()const{ | ||
814 | return m_selector; | ||
815 | } | ||
816 | QStringList OFileSelector::currentMimeType()const { | ||
817 | return m_mimeType[m_cmbMime->currentText()]; | ||
818 | } | ||
819 | void OFileSelector::slotMimeTypeChanged() { | ||
820 | reread(); | ||
821 | } | ||
822 | void OFileSelector::slotDocLnkBridge( const DocLnk& lnk) { | ||
823 | m_lneEdit->setText( lnk.name() ); | ||
824 | emit fileSelected( lnk ); | ||
825 | emit fileSelected( lnk.name() ); | ||
826 | } | ||
827 | void OFileSelector::slotFileBridge( const QString& str) { | ||
828 | DocLnk lnk( str ); | ||
829 | emit fileSelected( lnk ); | ||
830 | } | ||
831 | void OFileSelector::slotViewChange( const QString& view ) { | ||
832 | OFileViewInterface* interface = m_views[view]; | ||
833 | if (!interface) | ||
834 | return; | ||
835 | |||
836 | interface->activate( view ); | ||
837 | if (m_current) | ||
838 | m_stack->removeWidget( m_current->widget( m_stack ) ); | ||
839 | |||
840 | static int id = 1; | ||
841 | |||
842 | m_stack->addWidget( interface->widget(m_stack), id ); | ||
843 | m_stack->raiseWidget( id ); | ||
844 | |||
845 | interface->reread(); | ||
846 | m_current = interface; | ||
847 | |||
848 | id++; | ||
849 | } | ||
850 | void OFileSelector::setNewVisible( bool b ) { | ||
851 | m_shNew = b; | ||
852 | currentView()->reread(); | ||
853 | } | ||
854 | void OFileSelector::setCloseVisible( bool b ) { | ||
855 | m_shClose = b; | ||
856 | currentView()->reread(); | ||
857 | } | ||
858 | void OFileSelector::setNameVisible( bool b ) { | ||
859 | if ( b ) | ||
860 | m_nameBox->show(); | ||
861 | else | ||
862 | m_nameBox->hide(); | ||
863 | } | ||