summaryrefslogtreecommitdiff
path: root/libopie2/opieui/fileselector/ofileselector.cpp
Unidiff
Diffstat (limited to 'libopie2/opieui/fileselector/ofileselector.cpp') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opieui/fileselector/ofileselector.cpp1094
1 files changed, 1094 insertions, 0 deletions
diff --git a/libopie2/opieui/fileselector/ofileselector.cpp b/libopie2/opieui/fileselector/ofileselector.cpp
new file mode 100644
index 0000000..71d765c
--- a/dev/null
+++ b/libopie2/opieui/fileselector/ofileselector.cpp
@@ -0,0 +1,1094 @@
1/*
2               =. This file is part of the OPIE Project
3             .=l. Copyright (C) 2002,2003 Holger Freyther <zecke@handhelds.org>
4           .>+-=
5 _;:,     .>    :=|. This library is free software; you can
6.> <`_,   >  .   <= redistribute it and/or modify it under
7:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
8.="- .-=="i,     .._ License as published by the Free Software
9 - .   .-<_>     .<> Foundation; either version 2 of the License,
10     ._= =}       : or (at your option) any later version.
11    .%`+i>       _;_.
12    .i_,=:_.      -<s. This library is distributed in the hope that
13     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
14    : ..    .:,     . . . without even the implied warranty of
15    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
16  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
17..}^=.=       =       ; Library General Public License for more
18++=   -.     .`     .: details.
19 :     =  ...= . :.=-
20 -.   .:....=;==+<; You should have received a copy of the GNU
21  -_. . .   )=.  = Library General Public License along with
22    --        :-=` this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
26
27*/
28
29/* hacky but we need to get FileSelector::filter */
30#define private public
31#include <qpe/fileselector.h>
32#undef private
33
34#include "ofileselector_p.h"
35
36/* OPIE */
37#include <opie2/ofileselector.h>
38
39#include <qpe/qpeapplication.h>
40#include <qpe/mimetype.h>
41#include <qpe/resource.h>
42#include <qpe/storage.h>
43
44/* QT */
45#include <qcombobox.h>
46#include <qdir.h>
47#include <qhbox.h>
48#include <qheader.h>
49#include <qlabel.h>
50#include <qlayout.h>
51#include <qlineedit.h>
52#include <qlistview.h>
53#include <qpopupmenu.h>
54#include <qwidgetstack.h>
55#include <qregexp.h>
56#include <qobjectlist.h>
57
58using namespace Opie;
59
60OFileViewInterface::OFileViewInterface( OFileSelector* selector )
61 : m_selector( selector )
62{}
63
64OFileViewInterface::~OFileViewInterface()
65{}
66
67QString OFileViewInterface::name()const
68{
69 return m_name;
70}
71
72void OFileViewInterface::setName( const QString& name )
73{
74 m_name = name;
75}
76
77OFileSelector* OFileViewInterface::selector()const
78{
79 return m_selector;
80}
81
82DocLnk OFileViewInterface::selectedDocument()const
83{
84 return DocLnk( selectedName() );
85}
86
87bool OFileViewInterface::showNew()const
88{
89 return selector()->showNew();
90}
91
92bool OFileViewInterface::showClose()const
93{
94 return selector()->showClose();
95}
96
97MimeTypes OFileViewInterface::mimeTypes()const
98{
99 return selector()->mimeTypes();
100}
101
102QStringList OFileViewInterface::currentMimeType()const
103{
104 return selector()->currentMimeType();
105}
106
107void OFileViewInterface::activate( const QString& )
108{
109 // not implemented here
110}
111
112void OFileViewInterface::ok()
113{
114 emit selector()->ok();
115}
116
117void OFileViewInterface::cancel()
118{
119 emit selector()->cancel();
120}
121
122void OFileViewInterface::closeMe()
123{
124 emit selector()->closeMe();
125}
126
127void OFileViewInterface::fileSelected( const QString& str)
128{
129 emit selector()->fileSelected( str);
130}
131
132void OFileViewInterface::fileSelected( const DocLnk& lnk)
133{
134 emit selector()->fileSelected( lnk );
135}
136
137void OFileViewInterface::setCurrentFileName( const QString& str )
138{
139 selector()->m_lneEdit->setText( str );
140}
141
142QString OFileViewInterface::currentFileName()const
143{
144 return selector()->m_lneEdit->text();
145}
146
147QString OFileViewInterface::startDirectory()const
148{
149 return selector()->m_startDir;
150}
151
152ODocumentFileView::ODocumentFileView( OFileSelector* selector )
153 :OFileViewInterface( selector )
154{
155 m_selector = 0;
156 setName( QObject::tr("Documents") );
157}
158
159ODocumentFileView::~ODocumentFileView()
160{
161}
162
163QString ODocumentFileView::selectedName()const
164{
165 if (!m_selector)
166 return QString::null;
167
168 return m_selector->selectedDocument().file();
169}
170
171QString ODocumentFileView::selectedPath()const
172{
173 return QPEApplication::documentDir();
174}
175
176QString ODocumentFileView::directory()const
177{
178 return selectedPath();
179}
180
181void ODocumentFileView::reread()
182{
183 if (!m_selector)
184 return;
185
186 m_selector->setNewVisible( showNew() );
187 m_selector->setCloseVisible( showClose() );
188 m_selector->filter = currentMimeType().join(";");
189 m_selector->reread();
190}
191
192int ODocumentFileView::fileCount()const
193{
194 if (!m_selector)
195 return -1;
196
197 return m_selector->fileCount();
198}
199
200DocLnk ODocumentFileView::selectedDocument()const
201{
202 if (!m_selector)
203 return DocLnk();
204
205 return m_selector->selectedDocument();
206}
207
208QWidget* ODocumentFileView::widget( QWidget* parent )
209{
210 if (!m_selector )
211 {
212 m_selector = new FileSelector(currentMimeType().join(";"), parent, "fileselector", showNew(), showClose() );
213 QObject::connect(m_selector, SIGNAL(fileSelected( const DocLnk& ) ),
214 selector(), SLOT(slotDocLnkBridge(const DocLnk&) ) );
215 QObject::connect(m_selector, SIGNAL(closeMe() ),
216 selector(), SIGNAL(closeMe() ) );
217 QObject::connect(m_selector, SIGNAL(newSelected(const DocLnk& ) ),
218 selector(), SIGNAL(newSelected(const DocLnk& ) ) );
219 }
220
221 return m_selector;
222}
223
224/*
225 * This is the file system view used
226 * we use a QListView + QListViewItems for it
227 */
228
229OFileSelectorItem::OFileSelectorItem( QListView* view, const QPixmap& pixmap,
230 const QString& path, const QString& date,
231 const QString& size, const QString& dir,
232 bool isLocked, bool isDir )
233 : QListViewItem( view )
234{
235 setPixmap(0, pixmap );
236 setText(1, path );
237 setText(2, size );
238 setText(3, date );
239 m_isDir = isDir;
240 m_dir = dir;
241 m_locked = isLocked;
242}
243
244OFileSelectorItem::~OFileSelectorItem()
245{
246}
247
248bool OFileSelectorItem::isLocked()const
249{
250 return m_locked;
251}
252
253QString OFileSelectorItem::directory()const
254{
255 return m_dir;
256}
257
258bool OFileSelectorItem::isDir()const
259{
260 return m_isDir;
261}
262
263QString OFileSelectorItem::path()const
264{
265 return text( 1 );
266}
267
268QString OFileSelectorItem::key( int id, bool )const
269{
270 QString ke;
271 if( id == 0 || id == 1 )
272 { // name
273 if( m_isDir )
274 {
275 ke.append("0" );
276 ke.append( text(1) );
277 }
278 else
279 {
280 ke.append("1" );
281 ke.append( text(1) );
282 }
283 return ke;
284 }
285 else
286 return text( id );
287
288}
289
290OFileViewFileListView::OFileViewFileListView( QWidget* parent, const QString& startDir, OFileSelector* sel)
291 :QWidget( parent ), m_sel( sel )
292{
293 m_all = false;
294 QVBoxLayout* lay = new QVBoxLayout( this );
295 m_currentDir = startDir;
296
297 /*
298 * now we add a special bar
299 * One Button For Up
300 * Home
301 * Doc
302 * And a dropdown menu with FileSystems
303 * FUTURE: one to change dir with lineedit
304 * Bookmarks
305 * Create Dir
306 */
307 QHBox* box = new QHBox(this );
308 box->setBackgroundMode( PaletteButton );
309 box->setSpacing( 0 );
310
311 QToolButton *btn = new QToolButton( box );
312 btn->setIconSet( Resource::loadIconSet("up") );
313 connect(btn, SIGNAL(clicked() ),
314 this, SLOT( cdUP() ) );
315
316 btn = new QToolButton( box );
317 btn->setIconSet( Resource::loadIconSet("home") );
318 connect(btn, SIGNAL(clicked() ),
319 this, SLOT( cdHome() ) );
320
321 btn = new QToolButton( box );
322 btn->setIconSet( Resource::loadIconSet("DocsIcon") );
323 connect(btn, SIGNAL(clicked() ),
324 this, SLOT(cdDoc() ) );
325
326 m_btnNew = new QToolButton( box );
327 m_btnNew->setIconSet( Resource::loadIconSet("new") );
328 connect(m_btnNew, SIGNAL(clicked() ),
329 this, SLOT(slotNew() ) );
330
331
332 m_btnClose = new QToolButton( box );
333 m_btnClose->setIconSet( Resource::loadIconSet("close") );
334 connect(m_btnClose, SIGNAL(clicked() ),
335 selector(), SIGNAL(closeMe() ) );
336
337 btn = new QToolButton( box );
338 btn->setIconSet( Resource::loadIconSet("cardmon/pcmcia") );
339
340 /* let's fill device parts */
341 QPopupMenu* pop = new QPopupMenu(this);
342 connect(pop, SIGNAL( activated(int) ),
343 this, SLOT(slotFSActivated(int) ) );
344
345 StorageInfo storage;
346 const QList<FileSystem> &fs = storage.fileSystems();
347 QListIterator<FileSystem> it(fs);
348 for ( ; it.current(); ++it )
349 {
350 const QString disk = (*it)->name();
351 const QString path = (*it)->path();
352 m_dev.insert( disk, path );
353 pop->insertItem( disk );
354 }
355 m_fsPop = pop;
356
357
358 btn->setPopup( pop );
359
360 lay->addWidget( box );
361
362 m_view = new QListView( this );
363
364 m_view->installEventFilter(this);
365
366 QPEApplication::setStylusOperation( m_view->viewport(),
367 QPEApplication::RightOnHold);
368 m_view->addColumn(" " );
369 m_view->addColumn(tr("Name"), 135 );
370 m_view->addColumn(tr("Size"), -1 );
371 m_view->addColumn(tr("Date"), 60 );
372 m_view->addColumn(tr("Mime Type"), -1 );
373
374
375 m_view->setSorting( 1 );
376 m_view->setAllColumnsShowFocus( TRUE );
377
378 lay->addWidget( m_view, 1000 );
379 connectSlots();
380}
381
382OFileViewFileListView::~OFileViewFileListView()
383{
384}
385
386void OFileViewFileListView::slotNew()
387{
388 DocLnk lnk;
389 emit selector()->newSelected( lnk );
390}
391
392OFileSelectorItem* OFileViewFileListView::currentItem()const
393{
394 QListViewItem* item = m_view->currentItem();
395 if (!item )
396 return 0l;
397
398 return static_cast<OFileSelectorItem*>(item);
399}
400
401void OFileViewFileListView::reread( bool all )
402{
403 m_view->clear();
404
405 if (selector()->showClose() )
406 m_btnClose->show();
407 else
408 m_btnClose->hide();
409
410 if (selector()->showNew() )
411 m_btnNew->show();
412 else
413 m_btnNew->hide();
414
415 m_mimes = selector()->currentMimeType();
416 m_all = all;
417
418 QDir dir( m_currentDir );
419 if (!dir.exists() )
420 return;
421
422 dir.setSorting( QDir::Name | QDir::DirsFirst | QDir::Reversed );
423 int filter;
424 if (m_all )
425 filter = QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All;
426 else
427 filter = QDir::Files | QDir::Dirs | QDir::All;
428 dir.setFilter( filter );
429
430 // now go through all files
431 const QFileInfoList *list = dir.entryInfoList();
432 if (!list)
433 {
434 cdUP();
435 return;
436 }
437
438 QFileInfoListIterator it( *list );
439 QFileInfo *fi;
440 while( (fi=it.current() ) )
441 {
442 if( fi->fileName() == QString::fromLatin1("..") || fi->fileName() == QString::fromLatin1(".") )
443 {
444 ++it;
445 continue;
446 }
447
448 /*
449 * It is a symlink we try to resolve it now but don't let us attack by DOS
450 *
451 */
452 if( fi->isSymLink() )
453 {
454 QString file = fi->dirPath( true ) + "/" + fi->readLink();
455 for( int i = 0; i<=4; i++)
456 { // 5 tries to prevent dos
457 QFileInfo info( file );
458 if( !info.exists() )
459 {
460 addSymlink( fi, TRUE );
461 break;
462 }
463 else if( info.isDir() )
464 {
465 addDir( fi, TRUE );
466 break;
467 }
468 else if( info.isFile() )
469 {
470 addFile( fi, TRUE );
471 break;
472 }
473 else if( info.isSymLink() )
474 {
475 file = info.dirPath(true ) + "/" + info.readLink() ;
476 break;
477 }
478 else if( i == 4)
479 { // couldn't resolve symlink add it as symlink
480 addSymlink( fi );
481 }
482 } // off for loop for symlink resolving
483 }
484 else if( fi->isDir() )
485 addDir( fi );
486 else if( fi->isFile() )
487 addFile( fi );
488
489 ++it;
490 } // of while loop
491 m_view->sort();
492
493}
494int OFileViewFileListView::fileCount()const
495{
496 return m_view->childCount();
497}
498
499QString OFileViewFileListView::currentDir()const
500{
501 return m_currentDir;
502}
503
504OFileSelector* OFileViewFileListView::selector()
505{
506 return m_sel;
507}
508
509bool OFileViewFileListView::eventFilter (QObject *o, QEvent *e)
510{
511 if ( e->type() == QEvent::KeyPress )
512 {
513 QKeyEvent *k = (QKeyEvent *)e;
514 if ( (k->key()==Key_Enter) || (k->key()==Key_Return))
515 {
516 slotClicked( Qt::LeftButton,m_view->currentItem(),QPoint(0,0),0);
517 return true;
518 }
519 }
520 return false;
521}
522
523void OFileViewFileListView::connectSlots()
524{
525 connect(m_view, SIGNAL(clicked(QListViewItem*) ),
526 this, SLOT(slotCurrentChanged(QListViewItem*) ) );
527 connect(m_view, SIGNAL(mouseButtonClicked(int, QListViewItem*, const QPoint&, int ) ),
528 this, SLOT(slotClicked(int, QListViewItem*, const QPoint&, int ) ) );
529}
530
531void OFileViewFileListView::slotCurrentChanged( QListViewItem* item)
532{
533 if (!item)
534 return;
535#if 0
536
537 OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item);
538
539 if (!sel->isDir() )
540 {
541 selector()->m_lneEdit->setText( sel->text(1) );
542 // if in fileselector mode we will emit selected
543 if ( selector()->mode() == OFileSelector::FileSelector )
544 {
545 qWarning("slot Current Changed");
546 QStringList str = QStringList::split("->", sel->text(1) );
547 QString path = sel->directory() + "/" + str[0].stripWhiteSpace();
548 emit selector()->fileSelected( path );
549 DocLnk lnk( path );
550 emit selector()->fileSelected( lnk );
551 }
552 }
553#endif
554}
555
556void OFileViewFileListView::slotClicked(int button , QListViewItem* item, const QPoint&, int )
557{
558 if (!item || ( button != Qt::LeftButton) )
559 return;
560
561 OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item);
562 if (!sel->isLocked() )
563 {
564 QStringList str = QStringList::split("->", sel->text(1) );
565 if (sel->isDir() )
566 {
567 m_currentDir = sel->directory() + "/" + str[0].stripWhiteSpace();
568 emit selector()->dirSelected( m_currentDir );
569 reread( m_all );
570 }
571 else
572 { // file
573 qWarning("slot Clicked");
574 selector()->m_lneEdit->setText( str[0].stripWhiteSpace() );
575 QString path = sel->directory() + "/" + str[0].stripWhiteSpace();
576 emit selector()->fileSelected( path );
577 DocLnk lnk( path );
578 emit selector()->fileSelected( lnk );
579 }
580 } // not locked
581}
582
583void OFileViewFileListView::addFile( QFileInfo* info, bool symlink )
584{
585 MimeType type( info->absFilePath() );
586 if (!compliesMime( type.id() ) )
587 return;
588
589 QPixmap pix = type.pixmap();
590 QString dir, name; bool locked;
591 if ( pix.isNull() )
592 {
593 QWMatrix matrix;
594 QPixmap pixer(Resource::loadPixmap("UnknownDocument") );
595 matrix.scale( .4, .4 );
596 pix = pixer.xForm( matrix );
597 }
598 dir = info->dirPath( true );
599 locked = false;
600 if ( symlink )
601 name = info->fileName() + " -> " + info->dirPath() + "/" + info->readLink();
602 else
603 {
604 name = info->fileName();
605 if ( ( (selector()->mode() == OFileSelector::Open)&& !info->isReadable() ) ||
606 ( (selector()->mode() == OFileSelector::Save)&& !info->isWritable() ) )
607 {
608 locked = true; pix = Resource::loadPixmap("locked");
609 }
610 }
611 (void)new OFileSelectorItem( m_view, pix, name,
612 info->lastModified().toString(), QString::number( info->size() ),
613 dir, locked );
614}
615
616void OFileViewFileListView::addDir( QFileInfo* info, bool symlink )
617{
618 bool locked = false; QString name; QPixmap pix;
619
620 if ( ( ( selector()->mode() == OFileSelector::Open ) && !info->isReadable() ) ||
621 ( ( selector()->mode() == OFileSelector::Save ) && !info->isWritable() ) )
622 {
623 locked = true;
624 if ( symlink )
625 pix = Resource::loadPixmap( "opie/symlink" );
626 else
627 pix = Resource::loadPixmap( "lockedfolder" );
628 }
629 else
630 pix = symlink ? Resource::loadPixmap( "opie/symlink") : Resource::loadPixmap("folder");
631
632 name = symlink ? info->fileName() + " -> " + info->dirPath(true) + "/" + info->readLink() :
633 info->fileName();
634
635 (void)new OFileSelectorItem( m_view, pix, name,
636 info->lastModified().toString(),
637 QString::number( info->size() ),
638 info->dirPath( true ), locked, true );
639
640
641}
642
643void OFileViewFileListView::addSymlink( QFileInfo* , bool )
644{
645}
646
647void OFileViewFileListView::cdUP()
648{
649 QDir dir( m_currentDir );
650 dir.cdUp();
651
652 if (!dir.exists() )
653 m_currentDir = "/";
654 else
655 m_currentDir = dir.absPath();
656
657 emit selector()->dirSelected( m_currentDir );
658 reread( m_all );
659}
660
661void OFileViewFileListView::cdHome()
662{
663 m_currentDir = QDir::homeDirPath();
664 emit selector()->dirSelected( m_currentDir );
665 reread( m_all );
666}
667
668void OFileViewFileListView::cdDoc()
669{
670 m_currentDir = QPEApplication::documentDir();
671 emit selector()->dirSelected( m_currentDir );
672 reread( m_all );
673}
674
675void OFileViewFileListView::changeDir( const QString& dir )
676{
677 m_currentDir = dir;
678 emit selector()->dirSelected( m_currentDir );
679 reread( m_all );
680}
681
682void OFileViewFileListView::slotFSActivated( int id )
683{
684 changeDir ( m_dev[m_fsPop->text(id)] );
685}
686
687/* check if the mimetype in mime
688 * complies with the one which is current
689 */
690/*
691 * We've the mimetype of the file
692 * We need to get the stringlist of the current mimetype
693 *
694 * mime = image@slashjpeg
695 * QStringList = 'image@slash*'
696 * or QStringList = image/jpeg;image/png;application/x-ogg
697 * or QStringList = application/x-ogg;image@slash*;
698 * with all these mime filters it should get acceptes
699 * to do so we need to look if mime is contained inside
700 * the stringlist
701 * if it's contained return true
702 * if not ( I'm no RegExp expert at all ) we'll look if a '@slash*'
703 * is contained in the mimefilter and then we will
704 * look if both are equal until the '/'
705 */
706
707bool OFileViewFileListView::compliesMime( const QString& str)
708{
709 if (str.isEmpty() || m_mimes.isEmpty() || str.stripWhiteSpace().isEmpty() )
710 return true;
711
712 for (QStringList::Iterator it = m_mimes.begin(); it != m_mimes.end(); ++it )
713 {
714 QRegExp reg( (*it) );
715 reg.setWildcard( true );
716 if ( str.find( reg ) != -1 )
717 return true;
718
719 }
720 return false;
721}
722/*
723 * The listView giving access to the file system!
724 */
725
726class OFileViewFileSystem : public OFileViewInterface
727{
728public:
729 OFileViewFileSystem( OFileSelector* );
730 ~OFileViewFileSystem();
731
732 QString selectedName() const;
733 QString selectedPath() const;
734
735 QString directory()const;
736 void reread();
737 int fileCount()const;
738
739 QWidget* widget( QWidget* parent );
740 void activate( const QString& );
741private:
742 OFileViewFileListView* m_view;
743 bool m_all : 1;
744};
745
746OFileViewFileSystem::OFileViewFileSystem( OFileSelector* sel)
747 : OFileViewInterface( sel )
748{
749 m_view = 0;
750 m_all = false;
751}
752
753OFileViewFileSystem::~OFileViewFileSystem()
754{
755}
756
757QString OFileViewFileSystem::selectedName()const
758{
759 if (!m_view )
760 return QString::null;
761
762 QString cFN=currentFileName();
763 if (cFN.startsWith("/")) return cFN;
764 return m_view->currentDir() + "/" + cFN;
765}
766
767QString OFileViewFileSystem::selectedPath()const
768{
769 return QString::null;
770}
771
772QString OFileViewFileSystem::directory()const
773{
774 if (!m_view)
775 return QString::null;
776
777 OFileSelectorItem* item = m_view->currentItem();
778 if (!item )
779 return QString::null;
780
781 return QDir(item->directory() ).absPath();
782}
783
784void OFileViewFileSystem::reread()
785{
786 if (!m_view)
787 return;
788
789 m_view->reread( m_all );
790}
791
792int OFileViewFileSystem::fileCount()const
793{
794 if (!m_view )
795 return -1;
796 return m_view->fileCount();
797}
798
799QWidget* OFileViewFileSystem::widget( QWidget* parent )
800{
801 if (!m_view )
802 {
803 m_view = new OFileViewFileListView( parent, startDirectory(), selector() );
804 }
805 return m_view;
806}
807
808void OFileViewFileSystem::activate( const QString& str)
809{
810 m_all = (str != QObject::tr("Files") );
811}
812
813/* Selector */
814OFileSelector::OFileSelector( QWidget* parent, int mode, int sel,
815 const QString& dirName, const QString& fileName,
816 const MimeTypes& mimetypes,
817 bool showNew, bool showClose)
818 :QWidget( parent, "OFileSelector" )
819{
820 m_current = 0;
821 m_shNew = showNew;
822 m_shClose = showClose;
823 m_mimeType = mimetypes;
824 m_startDir = dirName;
825
826 m_mode = mode;
827 m_selector = sel;
828
829 initUI();
830 m_lneEdit->setText( fileName );
831 initMime();
832 initViews();
833
834 QString str;
835 switch ( m_selector )
836 {
837 default:
838 case Normal:
839 str = QObject::tr("Documents");
840 m_cmbView->setCurrentItem( 0 );
841 break;
842 case Extended:
843 str = QObject::tr("Files");
844 m_cmbView->setCurrentItem( 1 );
845 break;
846 case ExtendedAll:
847 str = QObject::tr("All Files");
848 m_cmbView->setCurrentItem( 2 );
849 break;
850 }
851 slotViewChange( str );
852
853}
854
855OFileSelector::OFileSelector( const QString& mimeFilter, QWidget* parent, const char* name,
856 bool showNew, bool showClose )
857 : QWidget( parent, name )
858{
859 m_current = 0;
860 m_shNew = showNew;
861 m_shClose = showClose;
862 m_startDir = QPEApplication::documentDir();
863
864 if (!mimeFilter.isEmpty() )
865 m_mimeType.insert(mimeFilter, QStringList::split(";", mimeFilter ) );
866
867 m_mode = OFileSelector::FileSelector;
868 m_selector = OFileSelector::Normal;
869
870 initUI();
871 initMime();
872 initViews();
873 m_cmbView->setCurrentItem( 0 );
874 slotViewChange( QObject::tr("Documents") );
875}
876
877/*
878 * INIT UI will set up the basic GUI
879 * Layout: Simple VBoxLayout
880 * On top a WidgetStack containing the Views...
881 * - List View
882 * - Document View
883 * Below we will have a Label + LineEdit
884 * Below we will have two ComoBoxes one for choosing the view one for
885 * choosing the mimetype
886 */
887void OFileSelector::initUI()
888{
889 QVBoxLayout* lay = new QVBoxLayout( this );
890
891 m_stack = new QWidgetStack( this );
892 lay->addWidget( m_stack, 1000 );
893
894 m_nameBox = new QHBox( this );
895 (void)new QLabel( tr("Name:"), m_nameBox );
896 m_lneEdit = new QLineEdit( m_nameBox );
897 m_lneEdit ->installEventFilter(this);
898 lay->addWidget( m_nameBox );
899
900 m_cmbBox = new QHBox( this );
901 m_cmbView = new QComboBox( m_cmbBox );
902 m_cmbMime = new QComboBox( m_cmbBox );
903 lay->addWidget( m_cmbBox );
904}
905
906/*
907 * This will make sure that the return key in the name edit causes dialogs to close
908 */
909
910bool OFileSelector::eventFilter (QObject *o, QEvent *e)
911{
912 if ( e->type() == QEvent::KeyPress )
913 {
914 QKeyEvent *k = (QKeyEvent *)e;
915 if ( (k->key()==Key_Enter) || (k->key()==Key_Return))
916 {
917 emit ok();
918 return true;
919 }
920 }
921 return false;
922}
923
924/*
925 * This will insert the MimeTypes into the Combo Box
926 * And also connect the changed signal
927 *
928 * AutoMimeTyping is disabled for now. It used to reparse a dir and then set available mimetypes
929 */
930void OFileSelector::initMime()
931{
932 MimeTypes::Iterator it;
933 for ( it = m_mimeType.begin(); it != m_mimeType.end(); ++it )
934 {
935 m_cmbMime->insertItem( it.key() );
936 }
937 m_cmbMime->setCurrentItem( 0 );
938
939 connect( m_cmbMime, SIGNAL(activated(int) ),
940 this, SLOT(slotMimeTypeChanged() ) );
941
942}
943
944void OFileSelector::initViews()
945{
946 m_cmbView->insertItem( QObject::tr("Documents") );
947 m_cmbView->insertItem( QObject::tr("Files") );
948 m_cmbView->insertItem( QObject::tr("All Files") );
949 connect(m_cmbView, SIGNAL(activated( const QString& ) ),
950 this, SLOT(slotViewChange( const QString& ) ) );
951
952
953 m_views.insert( QObject::tr("Documents"), new ODocumentFileView(this) );
954
955 /* see above why add both */
956 OFileViewInterface* in = new OFileViewFileSystem( this );
957 m_views.insert( QObject::tr("Files"), in );
958 m_views.insert( QObject::tr("All Files"), in );
959}
960
961OFileSelector::~OFileSelector()
962{
963}
964
965const DocLnk* OFileSelector::selected()
966{
967 DocLnk* lnk = new DocLnk( currentView()->selectedDocument() );
968 return lnk;
969}
970
971QString OFileSelector::selectedName()const
972{
973 return currentView()->selectedName();
974}
975
976QString OFileSelector::selectedPath()const
977{
978 return currentView()->selectedPath();
979}
980
981QString OFileSelector::directory()const
982{
983 return currentView()->directory();
984}
985
986DocLnk OFileSelector::selectedDocument()const
987{
988 return currentView()->selectedDocument();
989}
990
991int OFileSelector::fileCount()const
992{
993 return currentView()->fileCount();
994}
995
996void OFileSelector::reread()
997{
998 return currentView()->reread();
999}
1000
1001OFileViewInterface* OFileSelector::currentView()const
1002{
1003 return m_current;
1004}
1005
1006bool OFileSelector::showNew()const
1007{
1008 return m_shNew;
1009}
1010
1011bool OFileSelector::showClose()const
1012{
1013 return m_shClose;
1014}
1015
1016MimeTypes OFileSelector::mimeTypes()const
1017{
1018 return m_mimeType;
1019}
1020
1021int OFileSelector::mode()const
1022{
1023 return m_mode;
1024}
1025
1026int OFileSelector::selector()const
1027{
1028 return m_selector;
1029}
1030
1031QStringList OFileSelector::currentMimeType()const
1032{
1033 return m_mimeType[m_cmbMime->currentText()];
1034}
1035
1036void OFileSelector::slotMimeTypeChanged()
1037{
1038 reread();
1039}
1040
1041void OFileSelector::slotDocLnkBridge( const DocLnk& lnk)
1042{
1043 m_lneEdit->setText( lnk.name() );
1044 emit fileSelected( lnk );
1045 emit fileSelected( lnk.name() );
1046}
1047
1048void OFileSelector::slotFileBridge( const QString& str)
1049{
1050 DocLnk lnk( str );
1051 emit fileSelected( lnk );
1052}
1053
1054void OFileSelector::slotViewChange( const QString& view )
1055{
1056 OFileViewInterface* interface = m_views[view];
1057 if (!interface)
1058 return;
1059
1060 interface->activate( view );
1061 if (m_current)
1062 m_stack->removeWidget( m_current->widget( m_stack ) );
1063
1064 static int id = 1;
1065
1066 m_stack->addWidget( interface->widget(m_stack), id );
1067 m_stack->raiseWidget( id );
1068
1069 interface->reread();
1070 m_current = interface;
1071
1072 id++;
1073}
1074
1075void OFileSelector::setNewVisible( bool b )
1076{
1077 m_shNew = b;
1078 currentView()->reread();
1079}
1080
1081void OFileSelector::setCloseVisible( bool b )
1082{
1083 m_shClose = b;
1084 currentView()->reread();
1085}
1086
1087void OFileSelector::setNameVisible( bool b )
1088{
1089 if ( b )
1090 m_nameBox->show();
1091 else
1092 m_nameBox->hide();
1093}
1094