Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ofileselector.cpp

Go to the documentation of this file.
00001 #include <qcombobox.h>
00002 #include <qdir.h>
00003 #include <qlabel.h>
00004 #include <qlayout.h>
00005 #include <qlineedit.h>
00006 #include <qpopupmenu.h>
00007 #include <qwidgetstack.h>
00008 
00009 /* hacky but we need to get FileSelector::filter */
00010 #define private public
00011 #include <qpe/fileselector.h>
00012 #undef private
00013 
00014 #include <qpe/qpeapplication.h>
00015 #include <qpe/mimetype.h>
00016 #include <qpe/resource.h>
00017 #include <qpe/storage.h>
00018 
00019 #include "ofileselector_p.h"
00020 #include "ofileselector.h"
00021 
00022 
00023 
00024 OFileViewInterface::OFileViewInterface( OFileSelector* selector )
00025     : m_selector( selector ) {
00026 }
00027 OFileViewInterface::~OFileViewInterface() {
00028 }
00029 QString OFileViewInterface::name()const{
00030     return m_name;
00031 }
00032 void OFileViewInterface::setName( const QString& name ) {
00033     m_name = name;
00034 }
00035 OFileSelector* OFileViewInterface::selector()const {
00036     return m_selector;
00037 }
00038 DocLnk OFileViewInterface::selectedDocument()const {
00039     return DocLnk( selectedName() );
00040 }
00041 bool OFileViewInterface::showNew()const {
00042     return selector()->showNew();
00043 }
00044 bool OFileViewInterface::showClose()const {
00045     return selector()->showClose();
00046 }
00047 MimeTypes OFileViewInterface::mimeTypes()const {
00048     return selector()->mimeTypes();
00049 }
00050 QStringList OFileViewInterface::currentMimeType()const {
00051     return selector()->currentMimeType();
00052 }
00053 void OFileViewInterface::activate( const QString& ) {
00054     // not implemented here
00055 }
00056 void OFileViewInterface::ok() {
00057     emit selector()->ok();
00058 }
00059 void OFileViewInterface::cancel() {
00060     emit selector()->cancel();
00061 }
00062 void OFileViewInterface::closeMe() {
00063     emit selector()->closeMe();
00064 }
00065 void OFileViewInterface::fileSelected( const QString& str) {
00066     emit selector()->fileSelected( str);
00067 }
00068 void OFileViewInterface::fileSelected( const DocLnk& lnk) {
00069     emit selector()->fileSelected( lnk );
00070 }
00071 void OFileViewInterface::setCurrentFileName( const QString& str ) {
00072     selector()->m_lneEdit->setText( str );
00073 }
00074 QString OFileViewInterface::currentFileName()const{
00075     return selector()->m_lneEdit->text();
00076 }
00077 QString OFileViewInterface::startDirectory()const{
00078     return selector()->m_startDir;
00079 }
00080 
00081 
00082 ODocumentFileView::ODocumentFileView( OFileSelector* selector )
00083     : OFileViewInterface( selector ) {
00084     m_selector = 0;
00085     setName( QObject::tr("Documents") );
00086 }
00087 ODocumentFileView::~ODocumentFileView() {
00088 
00089 }
00090 QString ODocumentFileView::selectedName()const {
00091     if (!m_selector)
00092         return QString::null;
00093 
00094     return m_selector->selectedDocument().file();
00095 }
00096 QString ODocumentFileView::selectedPath()const {
00097     return QPEApplication::documentDir();
00098 }
00099 QString ODocumentFileView::directory()const {
00100     return selectedPath();
00101 }
00102 void ODocumentFileView::reread() {
00103     if (!m_selector)
00104         return;
00105 
00106     m_selector->setNewVisible( showNew() );
00107     m_selector->setCloseVisible( showClose() );
00108     m_selector->filter = currentMimeType().join(";");
00109     m_selector->reread();
00110 }
00111 int ODocumentFileView::fileCount()const {
00112     if (!m_selector)
00113         return -1;
00114 
00115     return m_selector->fileCount();
00116 }
00117 DocLnk ODocumentFileView::selectedDocument()const {
00118     if (!m_selector)
00119         return DocLnk();
00120 
00121     return m_selector->selectedDocument();
00122 }
00123 QWidget* ODocumentFileView::widget( QWidget* parent ) {
00124     if (!m_selector ) {
00125         m_selector = new FileSelector(currentMimeType().join(";"), parent, "fileselector", showNew(), showClose() );
00126         QObject::connect(m_selector, SIGNAL(fileSelected(const DocLnk&) ),
00127                          selector(), SLOT(slotDocLnkBridge(const DocLnk&) ) );
00128         QObject::connect(m_selector, SIGNAL(closeMe() ),
00129                          selector(), SIGNAL(closeMe() ) );
00130         QObject::connect(m_selector, SIGNAL(newSelected(const DocLnk&) ),
00131                          selector(), SIGNAL(newSelected(const DocLnk&) ) );
00132     }
00133 
00134     return m_selector;
00135 }
00136 
00137 /*
00138  * This is the file system view used
00139  * we use a QListView + QListViewItems for it
00140  */
00141 
00142 OFileSelectorItem::OFileSelectorItem( QListView* view, const QPixmap& pixmap,
00143                                       const QString& path, const QString& date,
00144                                       const QString& size, const QString& dir,
00145                                       bool isLocked, bool isDir )
00146     : QListViewItem( view )
00147 {
00148     setPixmap(0, pixmap );
00149     setText(1, path );
00150     setText(2, size );
00151     setText(3, date );
00152     m_isDir = isDir;
00153     m_dir = dir;
00154     m_locked = isLocked;
00155 }
00156 OFileSelectorItem::~OFileSelectorItem() {
00157 
00158 }
00159 bool OFileSelectorItem::isLocked()const {
00160     return m_locked;
00161 }
00162 QString OFileSelectorItem::directory()const {
00163     return m_dir;
00164 }
00165 bool OFileSelectorItem::isDir()const {
00166     return m_isDir;
00167 }
00168 QString OFileSelectorItem::path()const {
00169     return text( 1 );
00170 }
00171 QString OFileSelectorItem::key( int id, bool )const {
00172     QString ke;
00173     if( id == 0 || id == 1 ){ // name
00174         if( m_isDir ){
00175             ke.append("0" );
00176             ke.append( text(1) );
00177         }else{
00178             ke.append("1" );
00179             ke.append( text(1) );
00180         }
00181         return ke;
00182     }else
00183         return text( id );
00184 
00185 }
00186 
00187 OFileViewFileListView::OFileViewFileListView( QWidget* parent, const QString& startDir,
00188                                               OFileSelector* sel)
00189     : QWidget( parent ), m_sel( sel ) {
00190     m_all = false;
00191     QVBoxLayout* lay = new QVBoxLayout( this );
00192     m_currentDir = startDir;
00193 
00194     /*
00195      * now we add a special bar
00196      * One Button For Up
00197      * Home
00198      * Doc
00199      * And a dropdown menu with FileSystems
00200      * FUTURE: one to change dir with lineedit
00201      * Bookmarks
00202      * Create Dir
00203      */
00204     QHBox* box = new QHBox(this );
00205     box->setBackgroundMode( PaletteButton );
00206     box->setSpacing( 0 );
00207 
00208     QToolButton *btn = new QToolButton( box );
00209     btn->setIconSet( Resource::loadIconSet("up") );
00210     connect(btn, SIGNAL(clicked() ),
00211             this, SLOT( cdUP() ) );
00212 
00213     btn = new QToolButton( box );
00214     btn->setIconSet( Resource::loadIconSet("home") );
00215     connect(btn, SIGNAL(clicked() ),
00216             this, SLOT( cdHome() ) );
00217 
00218     btn = new QToolButton( box );
00219     btn->setIconSet( Resource::loadIconSet("DocsIcon") );
00220     connect(btn, SIGNAL(clicked() ),
00221             this, SLOT(cdDoc() ) );
00222 
00223     m_btnNew = new QToolButton( box );
00224     m_btnNew->setIconSet( Resource::loadIconSet("new") );
00225     connect(m_btnNew, SIGNAL(clicked() ),
00226             this, SLOT(slotNew() ) );
00227 
00228 
00229     m_btnClose = new QToolButton( box );
00230     m_btnClose->setIconSet( Resource::loadIconSet("close") );
00231     connect(m_btnClose, SIGNAL(clicked() ),
00232             selector(), SIGNAL(closeMe() ) );
00233 
00234     btn = new QToolButton( box );
00235     btn->setIconSet( Resource::loadIconSet("pcmcia") );
00236 
00237     /* let's fill device parts */
00238     QPopupMenu* pop = new QPopupMenu(this);
00239     connect(pop, SIGNAL( activated(int) ),
00240             this, SLOT(slotFSActivated(int) ) );
00241 
00242     StorageInfo storage;
00243     const QList<FileSystem> &fs = storage.fileSystems();
00244     QListIterator<FileSystem> it(fs);
00245     for ( ; it.current(); ++it ) {
00246         const QString disk = (*it)->name();
00247         const QString path = (*it)->path();
00248         m_dev.insert( disk, path );
00249         pop->insertItem( disk );
00250     }
00251     m_fsPop = pop;
00252 
00253 
00254     btn->setPopup( pop );
00255 
00256     lay->addWidget( box );
00257 
00258     m_view = new QListView( this );
00259 
00260     m_view->installEventFilter(this);
00261 
00262     QPEApplication::setStylusOperation( m_view->viewport(),
00263                                         QPEApplication::RightOnHold);
00264     m_view->addColumn(" " );
00265     m_view->addColumn(tr("Name"), 135 );
00266     m_view->addColumn(tr("Size"), -1 );
00267     m_view->addColumn(tr("Date"), 60 );
00268     m_view->addColumn(tr("Mime Type"), -1 );
00269 
00270 
00271     m_view->setSorting( 1 );
00272     m_view->setAllColumnsShowFocus( TRUE );
00273 
00274     lay->addWidget( m_view, 1000 );
00275     connectSlots();
00276 }
00277 OFileViewFileListView::~OFileViewFileListView() {
00278 }
00279 void OFileViewFileListView::slotNew() {
00280     DocLnk lnk;
00281     emit selector()->newSelected( lnk );
00282 }
00283 OFileSelectorItem* OFileViewFileListView::currentItem()const{
00284     QListViewItem* item = m_view->currentItem();
00285     if (!item )
00286         return 0l;
00287 
00288     return static_cast<OFileSelectorItem*>(item);
00289 }
00290 void OFileViewFileListView::reread( bool all ) {
00291     m_view->clear();
00292 
00293     if (selector()->showClose() )
00294         m_btnClose->show();
00295     else
00296         m_btnClose->hide();
00297 
00298     if (selector()->showNew() )
00299         m_btnNew->show();
00300     else
00301         m_btnNew->hide();
00302 
00303     m_mimes = selector()->currentMimeType();
00304     m_all = all;
00305 
00306     QDir dir( m_currentDir );
00307     if (!dir.exists() )
00308         return;
00309 
00310     dir.setSorting( QDir::Name | QDir::DirsFirst | QDir::Reversed );
00311     int filter;
00312     if (m_all )
00313         filter = QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All;
00314     else
00315         filter = QDir::Files | QDir::Dirs | QDir::All;
00316     dir.setFilter( filter );
00317 
00318     // now go through all files
00319     const QFileInfoList *list = dir.entryInfoList();
00320     if (!list) {
00321         cdUP();
00322         return;
00323     }
00324     QFileInfoListIterator it( *list );
00325     QFileInfo *fi;
00326     while( (fi=it.current() ) ){
00327       if( fi->fileName() == QString::fromLatin1("..") || fi->fileName() == QString::fromLatin1(".") ){
00328           ++it;
00329           continue;
00330       }
00331 
00332       /*
00333        * It is a symlink we try to resolve it now but don't let us attack by DOS
00334        *
00335        */
00336       if( fi->isSymLink() ){
00337           QString file = fi->dirPath( true ) + "/" + fi->readLink();
00338           for( int i = 0; i<=4; i++) { // 5 tries to prevent dos
00339               QFileInfo info( file );
00340               if( !info.exists() ){
00341                   addSymlink( fi, TRUE );
00342                   break;
00343               }else if( info.isDir() ){
00344                   addDir( fi, TRUE );
00345                   break;
00346               }else if( info.isFile() ){
00347                   addFile( fi, TRUE );
00348                   break;
00349               }else if( info.isSymLink() ){
00350                   file = info.dirPath(true ) + "/" + info.readLink() ;
00351                   break;
00352               }else if( i == 4){ // couldn't resolve symlink add it as symlink
00353                   addSymlink( fi );
00354               }
00355         } // off for loop for symlink resolving
00356       }else if( fi->isDir() )
00357           addDir(  fi );
00358       else if( fi->isFile() )
00359         addFile( fi );
00360 
00361       ++it;
00362     } // of while loop
00363   m_view->sort();
00364 
00365 }
00366 int OFileViewFileListView::fileCount()const{
00367     return m_view->childCount();
00368 }
00369 QString OFileViewFileListView::currentDir()const{
00370     return m_currentDir;
00371 }
00372 OFileSelector* OFileViewFileListView::selector() {
00373     return m_sel;
00374 }
00375 
00376 bool OFileViewFileListView::eventFilter (QObject *o, QEvent *e) {
00377     if ( e->type() == QEvent::KeyPress ) {
00378         QKeyEvent *k = (QKeyEvent *)e;
00379         if ( (k->key()==Key_Enter) || (k->key()==Key_Return)) {
00380             slotClicked( Qt::LeftButton,m_view->currentItem(),QPoint(0,0),0);
00381             return true;
00382         }
00383     }
00384     return false;
00385 }
00386 
00387 
00388 void OFileViewFileListView::connectSlots() {
00389     connect(m_view, SIGNAL(clicked(QListViewItem*) ),
00390             this, SLOT(slotCurrentChanged(QListViewItem*) ) );
00391     connect(m_view, SIGNAL(mouseButtonClicked(int,QListViewItem*,const QPoint&,int) ),
00392             this, SLOT(slotClicked(int,QListViewItem*,const QPoint&,int) ) );
00393 }
00394 void OFileViewFileListView::slotCurrentChanged( QListViewItem* item) {
00395     if (!item)
00396         return;
00397 #if 0
00398 
00399     OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item);
00400 
00401     if (!sel->isDir() ) {
00402         selector()->m_lneEdit->setText( sel->text(1) );
00403         // if in fileselector mode we will emit selected
00404         if ( selector()->mode() == OFileSelector::FileSelector ) {
00405             qWarning("slot Current Changed");
00406             QStringList str = QStringList::split("->", sel->text(1) );
00407             QString path = sel->directory() + "/" + str[0].stripWhiteSpace();
00408             emit selector()->fileSelected( path );
00409             DocLnk lnk( path );
00410             emit selector()->fileSelected( lnk );
00411         }
00412     }
00413 #endif
00414 }
00415 void OFileViewFileListView::slotClicked(int button , QListViewItem* item, const QPoint&, int ) {
00416     if (!item || ( button != Qt::LeftButton) )
00417         return;
00418 
00419     OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item);
00420     if (!sel->isLocked() ) {
00421         QStringList str = QStringList::split("->", sel->text(1) );
00422         if (sel->isDir() ) {
00423             m_currentDir = sel->directory() + "/" + str[0].stripWhiteSpace();
00424             emit selector()->dirSelected( m_currentDir );
00425             reread( m_all );
00426         }else { // file
00427             qWarning("slot Clicked");
00428             selector()->m_lneEdit->setText( str[0].stripWhiteSpace() );
00429             QString path = sel->directory() + "/" + str[0].stripWhiteSpace();
00430             emit selector()->fileSelected( path );
00431             DocLnk lnk( path );
00432             emit selector()->fileSelected( lnk );
00433         }
00434     } // not locked
00435 }
00436 void OFileViewFileListView::addFile( QFileInfo* info, bool symlink ) {
00437     MimeType type( info->absFilePath() );
00438     if (!compliesMime( type.id() ) )
00439         return;
00440 
00441     QPixmap pix = type.pixmap();
00442     QString dir, name; bool locked;
00443     if ( pix.isNull() ) {
00444         QWMatrix matrix;
00445         QPixmap pixer(Resource::loadPixmap("UnknownDocument") );
00446         matrix.scale( .4, .4 );
00447         pix = pixer.xForm( matrix );
00448     }
00449     dir = info->dirPath( true );
00450     locked = false;
00451     if ( symlink )
00452         name = info->fileName() + " -> " + info->dirPath() + "/" + info->readLink();
00453     else{
00454         name = info->fileName();
00455         if ( ( (selector()->mode() == OFileSelector::Open)&& !info->isReadable() ) ||
00456              ( (selector()->mode() == OFileSelector::Save)&& !info->isWritable() ) ) {
00457             locked = true; pix = Resource::loadPixmap("locked");
00458         }
00459     }
00460     (void)new OFileSelectorItem( m_view, pix, name,
00461                                  info->lastModified().toString(), QString::number( info->size() ),
00462                                  dir, locked );
00463 }
00464 void OFileViewFileListView::addDir( QFileInfo* info, bool symlink ) {
00465     bool locked = false; QString name; QPixmap pix;
00466 
00467     if ( ( ( selector()->mode() == OFileSelector::Open )  && !info->isReadable() ) ||
00468          ( ( selector()->mode() == OFileSelector::Save )  && !info->isWritable() ) ) {
00469         locked = true;
00470         if ( symlink )
00471             pix = Resource::loadPixmap( "opie/symlink" );
00472         else
00473             pix = Resource::loadPixmap( "lockedfolder" );
00474     }else
00475         pix = symlink ? Resource::loadPixmap( "opie/symlink") : Resource::loadPixmap("folder");
00476 
00477     name = symlink ? info->fileName() + " -> " + info->dirPath(true) + "/" + info->readLink() :
00478            info->fileName();
00479 
00480     (void)new OFileSelectorItem( m_view, pix, name,
00481                                  info->lastModified().toString(),
00482                                  QString::number( info->size() ),
00483                                  info->dirPath( true ), locked, true );
00484 
00485 
00486 }
00487 void OFileViewFileListView::addSymlink( QFileInfo* , bool ) {
00488 
00489 }
00490 void OFileViewFileListView::cdUP() {
00491     QDir dir( m_currentDir );
00492     dir.cdUp();
00493 
00494     if (!dir.exists() )
00495         m_currentDir = "/";
00496     else
00497         m_currentDir = dir.absPath();
00498 
00499     emit selector()->dirSelected( m_currentDir );
00500     reread( m_all );
00501 }
00502 void OFileViewFileListView::cdHome() {
00503     m_currentDir = QDir::homeDirPath();
00504     emit selector()->dirSelected( m_currentDir );
00505     reread( m_all );
00506 }
00507 void OFileViewFileListView::cdDoc() {
00508     m_currentDir = QPEApplication::documentDir();
00509     emit selector()->dirSelected( m_currentDir );
00510     reread( m_all );
00511 }
00512 void OFileViewFileListView::changeDir( const QString& dir ) {
00513     m_currentDir = dir;
00514     emit selector()->dirSelected( m_currentDir );
00515     reread( m_all );
00516 }
00517 void OFileViewFileListView::slotFSActivated( int id ) {
00518     changeDir ( m_dev[m_fsPop->text(id)]  );
00519 }
00520 
00521 /*  check if the mimetype in mime
00522  *  complies with the  one which is current
00523  */
00524 /*
00525  * We've the mimetype of the file
00526  * We need to get the stringlist of the current mimetype
00527  *
00528  * mime = image@slashjpeg
00529  * QStringList = 'image@slash*'
00530  * or QStringList = image/jpeg;image/png;application/x-ogg
00531  * or QStringList = application/x-ogg;image@slash*;
00532  * with all these mime filters it should get acceptes
00533  * to do so we need to look if mime is contained inside
00534  * the stringlist
00535  * if it's contained return true
00536  * if not ( I'm no RegExp expert at all ) we'll look if a '@slash*'
00537  * is contained in the mimefilter and then we will
00538  * look if both are equal until the '/'
00539  */
00540 bool OFileViewFileListView::compliesMime( const QString& str) {
00541     if (str.isEmpty() || m_mimes.isEmpty() || str.stripWhiteSpace().isEmpty() )
00542         return true;
00543 
00544     for (QStringList::Iterator it = m_mimes.begin(); it != m_mimes.end(); ++it ) {
00545         QRegExp reg( (*it) );
00546         reg.setWildcard( true );
00547         if ( str.find( reg ) != -1 )
00548             return true;
00549 
00550     }
00551     return false;
00552 }
00553 /*
00554  * The listView giving access to the file system!
00555  */
00556 class OFileViewFileSystem : public OFileViewInterface {
00557 public:
00558     OFileViewFileSystem( OFileSelector* );
00559     ~OFileViewFileSystem();
00560 
00561     QString selectedName() const;
00562     QString selectedPath() const;
00563 
00564     QString directory()const;
00565     void reread();
00566     int fileCount()const;
00567 
00568     QWidget* widget( QWidget* parent );
00569     void activate( const QString& );
00570 private:
00571     OFileViewFileListView* m_view;
00572     bool m_all : 1;
00573 };
00574 OFileViewFileSystem::OFileViewFileSystem( OFileSelector* sel)
00575     : OFileViewInterface( sel ) {
00576     m_view = 0;
00577     m_all = false;
00578 }
00579 OFileViewFileSystem::~OFileViewFileSystem() {
00580 }
00581 QString OFileViewFileSystem::selectedName()const{
00582     if (!m_view )
00583         return QString::null;
00584 
00585     QString cFN=currentFileName();
00586     if (cFN.startsWith("/")) return cFN;
00587     return m_view->currentDir() + "/" + cFN;
00588 }
00589 QString OFileViewFileSystem::selectedPath()const{
00590     return QString::null;
00591 }
00592 QString OFileViewFileSystem::directory()const{
00593     if (!m_view)
00594         return QString::null;
00595 
00596     OFileSelectorItem* item = m_view->currentItem();
00597     if (!item )
00598         return QString::null;
00599 
00600     return QDir(item->directory() ).absPath();
00601 }
00602 void OFileViewFileSystem::reread() {
00603     if (!m_view)
00604         return;
00605 
00606     m_view->reread( m_all );
00607 }
00608 int OFileViewFileSystem::fileCount()const{
00609     if (!m_view )
00610         return -1;
00611     return m_view->fileCount();
00612 }
00613 QWidget* OFileViewFileSystem::widget( QWidget* parent ) {
00614     if (!m_view ) {
00615         m_view = new OFileViewFileListView( parent, startDirectory(), selector() );
00616     }
00617     return m_view;
00618 }
00619 void OFileViewFileSystem::activate( const QString& str) {
00620     m_all =  (str != QObject::tr("Files") );
00621 
00622 
00623 }
00624 
00625 /* Selector */
00655 OFileSelector::OFileSelector( QWidget* parent, int mode, int sel,
00656                               const QString& dirName, const QString& fileName,
00657                               const MimeTypes& mimetypes,
00658                               bool showNew,  bool showClose)
00659     : QWidget( parent, "OFileSelector" )
00660 {
00661     m_current = 0;
00662     m_shNew = showNew;
00663     m_shClose = showClose;
00664     m_mimeType = mimetypes;
00665     m_startDir = dirName;
00666 
00667     m_mode = mode;
00668     m_selector = sel;
00669 
00670     initUI();
00671     m_lneEdit->setText( fileName );
00672     initMime();
00673     initViews();
00674 
00675     QString str;
00676     switch ( m_selector ) {
00677     default:
00678     case Normal:
00679         str = QObject::tr("Documents");
00680         m_cmbView->setCurrentItem( 0 );
00681         break;
00682     case Extended:
00683         str = QObject::tr("Files");
00684         m_cmbView->setCurrentItem( 1 );
00685         break;
00686     case ExtendedAll:
00687         str = QObject::tr("All Files");
00688         m_cmbView->setCurrentItem( 2 );
00689         break;
00690     }
00691     slotViewChange( str );
00692 
00693 }
00694 
00698 OFileSelector::OFileSelector( const QString& mimeFilter, QWidget* parent, const char* name,
00699                               bool showNew, bool showClose )
00700     : QWidget( parent, name )
00701 {
00702     m_current = 0;
00703     m_shNew   = showNew;
00704     m_shClose = showClose;
00705     m_startDir = QPEApplication::documentDir();
00706 
00707     if (!mimeFilter.isEmpty() )
00708         m_mimeType.insert(mimeFilter, QStringList::split(";", mimeFilter ) );
00709 
00710     m_mode = OFileSelector::FileSelector;
00711     m_selector = OFileSelector::Normal;
00712 
00713     initUI();
00714     initMime();
00715     initViews();
00716     m_cmbView->setCurrentItem( 0 );
00717     slotViewChange( QObject::tr("Documents") );
00718 }
00719 /*
00720  * INIT UI will set up the basic GUI
00721  * Layout: Simple VBoxLayout
00722  * On top a WidgetStack containing the Views...
00723  *  - List View
00724  *  - Document View
00725  * Below we will have a Label + LineEdit
00726  * Below we will have two ComoBoxes one for choosing the view one for
00727  *       choosing the mimetype
00728  */
00729 void OFileSelector::initUI() {
00730     QVBoxLayout* lay = new QVBoxLayout( this );
00731 
00732     m_stack = new QWidgetStack( this );
00733     lay->addWidget( m_stack, 1000 );
00734 
00735     m_nameBox = new QHBox( this );
00736     (void)new QLabel( tr("Name:"), m_nameBox );
00737     m_lneEdit = new QLineEdit( m_nameBox );
00738     m_lneEdit ->installEventFilter(this);
00739     lay->addWidget( m_nameBox );
00740 
00741     m_cmbBox = new QHBox( this );
00742     m_cmbView = new QComboBox( m_cmbBox );
00743     m_cmbMime = new QComboBox( m_cmbBox );
00744     lay->addWidget( m_cmbBox );
00745 }
00746 
00747 /*
00748  * This will make sure that the return key in the name edit causes dialogs to close
00749  */
00750 
00751 bool OFileSelector::eventFilter (QObject *o, QEvent *e) {
00752     if ( e->type() == QEvent::KeyPress ) {
00753         QKeyEvent *k = (QKeyEvent *)e;
00754         if ( (k->key()==Key_Enter) || (k->key()==Key_Return)) {
00755             emit ok();
00756             return true;
00757         }
00758     }
00759     return false;
00760 }
00761 
00762 /*
00763  * This will insert the MimeTypes into the Combo Box
00764  * And also connect the changed signal
00765  *
00766  * AutoMimeTyping is disabled for now. It used to reparse a dir and then set available mimetypes
00767  */
00768 void OFileSelector::initMime() {
00769     MimeTypes::Iterator it;
00770     for ( it = m_mimeType.begin(); it != m_mimeType.end(); ++it ) {
00771         m_cmbMime->insertItem( it.key() );
00772     }
00773     m_cmbMime->setCurrentItem( 0 );
00774 
00775     connect( m_cmbMime, SIGNAL(activated(int) ),
00776              this, SLOT(slotMimeTypeChanged() ) );
00777 
00778 }
00779 void OFileSelector::initViews() {
00780     m_cmbView->insertItem( QObject::tr("Documents") );
00781     m_cmbView->insertItem( QObject::tr("Files") );
00782     m_cmbView->insertItem( QObject::tr("All Files") );
00783     connect(m_cmbView, SIGNAL(activated(const QString&) ),
00784             this, SLOT(slotViewChange(const QString&) ) );
00785 
00786 
00787     m_views.insert( QObject::tr("Documents"), new ODocumentFileView(this) );
00788 
00789     /* see above why add both */
00790     OFileViewInterface* in = new OFileViewFileSystem( this );
00791     m_views.insert( QObject::tr("Files"), in );
00792     m_views.insert( QObject::tr("All Files"), in );
00793 }
00794 
00798 OFileSelector::~OFileSelector() {
00799 
00800 }
00801 
00809 const DocLnk* OFileSelector::selected() {
00810     DocLnk* lnk = new DocLnk( currentView()->selectedDocument() );
00811     return lnk;
00812 }
00813 
00818 QString OFileSelector::selectedName()const{
00819        return currentView()->selectedName();
00820 }
00821 
00825 QString OFileSelector::selectedPath()const {
00826     return currentView()->selectedPath();
00827 }
00828 
00832 QString OFileSelector::directory()const {
00833     return currentView()->directory();
00834 }
00835 
00839 DocLnk OFileSelector::selectedDocument()const {
00840     return currentView()->selectedDocument();
00841 }
00842 
00846 int OFileSelector::fileCount()const {
00847     return currentView()->fileCount();
00848 }
00849 
00853 void OFileSelector::reread() {
00854     return currentView()->reread();
00855 }
00856 OFileViewInterface* OFileSelector::currentView()const{
00857     return m_current;
00858 }
00859 bool OFileSelector::showNew()const {
00860     return m_shNew;
00861 }
00862 bool OFileSelector::showClose()const {
00863     return m_shClose;
00864 }
00865 MimeTypes OFileSelector::mimeTypes()const {
00866     return m_mimeType;
00867 }
00868 
00872 int OFileSelector::mode()const{
00873     return m_mode;
00874 }
00875 
00879 int OFileSelector::selector()const{
00880     return m_selector;
00881 }
00882 QStringList OFileSelector::currentMimeType()const {
00883     return m_mimeType[m_cmbMime->currentText()];
00884 }
00885 void OFileSelector::slotMimeTypeChanged() {
00886     reread();
00887 }
00888 void OFileSelector::slotDocLnkBridge( const DocLnk& lnk) {
00889     m_lneEdit->setText( lnk.name() );
00890     emit fileSelected( lnk );
00891     emit fileSelected( lnk.name() );
00892 }
00893 void OFileSelector::slotFileBridge( const QString& str) {
00894     DocLnk lnk( str );
00895     emit fileSelected( lnk );
00896 }
00897 void OFileSelector::slotViewChange( const QString& view ) {
00898     OFileViewInterface* interface = m_views[view];
00899     if (!interface)
00900         return;
00901 
00902     interface->activate( view );
00903     if (m_current)
00904         m_stack->removeWidget( m_current->widget( m_stack ) );
00905 
00906     static int id = 1;
00907 
00908     m_stack->addWidget( interface->widget(m_stack), id );
00909     m_stack->raiseWidget( id );
00910 
00911     interface->reread();
00912     m_current = interface;
00913 
00914     id++;
00915 }
00916 void OFileSelector::setNewVisible( bool b ) {
00917     m_shNew = b;
00918     currentView()->reread();
00919 }
00920 void OFileSelector::setCloseVisible( bool b ) {
00921     m_shClose = b;
00922     currentView()->reread();
00923 }
00924 void OFileSelector::setNameVisible( bool b ) {
00925     if ( b )
00926         m_nameBox->show();
00927     else
00928         m_nameBox->hide();
00929 }

Generated on Sat Nov 5 16:16:25 2005 for OPIE by  doxygen 1.4.2