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