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 /*
00002                =.            This file is part of the OPIE Project
00003              .=l.            Copyright (C) Holger Freyther <freyther@handhelds.org>
00004            .>+-=
00005  _;:,     .>    :=|.         This library is free software; you can
00006 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00007     :`=1 )Y*s>-.--   :       the terms of the GNU Library General Public
00008 .="- .-=="i,     .._         License as published by the Free Software
00009  - .   .-<_>     .<>         Foundation; version 2 of the License.
00010      ._= =}       :
00011     .%`+i>       _;_.
00012     .i_,=:_.      -<s.       This library is distributed in the hope that
00013      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00014     : ..    .:,     . . .    without even the implied warranty of
00015     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00016   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00017 ..}^=.=       =       ;      Library General Public License for more
00018 ++=   -.     .`     .:       details.
00019     :     =  ...= . :.=-
00020  -.   .:....=;==+<;          You should have received a copy of the GNU
00021   -_. . .   )=.  =           Library General Public License along with
00022     --        :-=`           this library; see the file COPYING.LIB.
00023                              If not, write to the Free Software Foundation,
00024                              Inc., 59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 /* hacky but we need to get FileSelector::filter */
00030 #define private public
00031 #include <qpe/fileselector.h>
00032 #undef private
00033 
00034 #include "ofileselector_p.h"
00035 
00036 /* OPIE */
00037 #include <opie2/odebug.h>
00038 #include <opie2/ofileselector.h>
00039 #include <opie2/oresource.h>
00040 
00041 #include <qpe/qpeapplication.h>
00042 #include <qpe/mimetype.h>
00043 #include <qpe/storage.h>
00044 
00045 /* QT */
00046 #include <qcombobox.h>
00047 #include <qdir.h>
00048 #include <qhbox.h>
00049 #include <qheader.h>
00050 #include <qlabel.h>
00051 #include <qlayout.h>
00052 #include <qlineedit.h>
00053 #include <qlistview.h>
00054 #include <qpopupmenu.h>
00055 #include <qwidgetstack.h>
00056 #include <qregexp.h>
00057 #include <qobjectlist.h>
00058 
00059 using namespace Opie::Ui::Internal;
00060 
00061 namespace Opie {
00062 namespace Ui   {
00063 namespace Internal {
00064 /*
00065  * Create a path by adding a '/'/QDir::seperator in between
00066  * base and ending, but only if base is not empty
00067  */
00068 static inline QString createNewPath(const QString& base, const QString &ending) {
00069     return  base == QString::fromLatin1("/") ?
00070         base + ending : base + "/" + ending;
00071 }
00072 
00073 
00074 OFileViewInterface::OFileViewInterface( OFileSelector* _selector )
00075         : m_selector( _selector )
00076 {
00077     selector()->registerView( this );
00078 }
00079 
00080 OFileViewInterface::~OFileViewInterface()
00081 {}
00082 
00083 QString OFileViewInterface::name()const
00084 {
00085     return m_name;
00086 }
00087 
00088 void OFileViewInterface::setName( const QString& name )
00089 {
00090     m_name = name;
00091 }
00092 
00093 OFileSelector* OFileViewInterface::selector()const
00094 {
00095     return m_selector;
00096 }
00097 
00098 DocLnk OFileViewInterface::selectedDocument()const
00099 {
00100     return DocLnk( selectedName() );
00101 }
00102 
00103 bool OFileViewInterface::showNew()const
00104 {
00105     return selector()->showNew();
00106 }
00107 
00108 bool OFileViewInterface::showClose()const
00109 {
00110     return selector()->showClose();
00111 }
00112 
00113 MimeTypes OFileViewInterface::mimeTypes()const
00114 {
00115     return selector()->mimeTypes();
00116 }
00117 
00118 QStringList OFileViewInterface::currentMimeType()const
00119 {
00120     return selector()->currentMimeType();
00121 }
00122 
00123 void OFileViewInterface::activate( const QString& )
00124 {
00125     // not implemented here
00126 }
00127 
00128 void OFileViewInterface::ok()
00129 {
00130     emit selector()->ok();
00131 }
00132 
00133 void OFileViewInterface::cancel()
00134 {
00135     emit selector()->cancel();
00136 }
00137 
00138 void OFileViewInterface::closeMe()
00139 {
00140     emit selector()->closeMe();
00141 }
00142 
00143 void OFileViewInterface::fileSelected( const QString& str)
00144 {
00145     emit selector()->fileSelected( str);
00146 }
00147 
00148 void OFileViewInterface::fileSelected( const DocLnk& lnk)
00149 {
00150     emit selector()->fileSelected( lnk );
00151 }
00152 
00153 void OFileViewInterface::setCurrentFileName( const QString& str )
00154 {
00155     selector()->m_lneEdit->setText( str );
00156 }
00157 
00158 QString OFileViewInterface::currentFileName()const
00159 {
00160     return selector()->m_lneEdit->text();
00161 }
00162 
00163 QString OFileViewInterface::startDirectory()const
00164 {
00165     return selector()->m_startDir;
00166 }
00167 
00168 bool OFileViewInterface::allItem( const QString& item )const
00169 {
00170     return selector()->m_allList.contains( item );
00171 }
00172 
00173 
00174 ODocumentFileView::ODocumentFileView( OFileSelector* selector )
00175                   :OFileViewInterface( selector )
00176 {
00177     m_selector = 0;
00178     setName( QObject::tr("Documents") );
00179 }
00180 
00181 ODocumentFileView::~ODocumentFileView()
00182 {
00183 }
00184 
00185 QString ODocumentFileView::selectedName()const
00186 {
00187     if (!m_selector)
00188         return QString::null;
00189 
00190     return m_selector->selectedDocument().file();
00191 }
00192 
00193 QString ODocumentFileView::selectedPath()const
00194 {
00195     return QPEApplication::documentDir();
00196 }
00197 
00198 QString ODocumentFileView::directory()const
00199 {
00200     return selectedPath();
00201 }
00202 
00203 void ODocumentFileView::reread()
00204 {
00205     if (!m_selector)
00206         return;
00207 
00208     m_selector->setNewVisible( showNew() );
00209     m_selector->setCloseVisible( showClose() );
00210     m_selector->filter = currentMimeType().join(";");
00211     m_selector->reread();
00212 }
00213 
00214 int ODocumentFileView::fileCount()const
00215 {
00216     if (!m_selector)
00217         return -1;
00218 
00219     return m_selector->fileCount();
00220 }
00221 
00222 DocLnk ODocumentFileView::selectedDocument()const
00223 {
00224     if (!m_selector)
00225         return DocLnk();
00226 
00227     return m_selector->selectedDocument();
00228 }
00229 
00230 QWidget* ODocumentFileView::widget( QWidget* parent )
00231 {
00232     if (!m_selector )
00233     {
00234         m_selector = new FileSelector(currentMimeType().join(";"), parent, "fileselector", showNew(), showClose() );
00235         QObject::connect(m_selector, SIGNAL(fileSelected(const DocLnk&) ),
00236                          selector(), SLOT(slotDocLnkBridge(const DocLnk&) ) );
00237         QObject::connect(m_selector, SIGNAL(closeMe() ),
00238                          selector(), SIGNAL(closeMe() ) );
00239         QObject::connect(m_selector, SIGNAL(newSelected(const DocLnk&) ),
00240                          selector(), SIGNAL(newSelected(const DocLnk&) ) );
00241     }
00242 
00243     return m_selector;
00244 }
00245 
00246 /*
00247  * This is the file system view used
00248  * we use a QListView + QListViewItems for it
00249  */
00250 
00251 OFileSelectorItem::OFileSelectorItem( QListView* view, const QPixmap& pixmap,
00252                                       const QString& path, const QString& date,
00253                                       const QString& size, const QString& dir,
00254                                       bool isLocked, bool isDir )
00255         : QListViewItem( view ), m_dir(dir), m_isDir(isDir), m_locked(isLocked)
00256 {
00257     setPixmap(0, pixmap );
00258     setText(1, path );
00259     setText(2, size );
00260     setText(3, date );
00261 }
00262 
00263 OFileSelectorItem::~OFileSelectorItem()
00264 {
00265 }
00266 
00267 bool OFileSelectorItem::isLocked()const
00268 {
00269     return m_locked;
00270 }
00271 
00272 QString OFileSelectorItem::directory()const
00273 {
00274     return m_dir;
00275 }
00276 
00277 bool OFileSelectorItem::isDir()const
00278 {
00279     return m_isDir;
00280 }
00281 
00282 QString OFileSelectorItem::path()const
00283 {
00284     return text( 1 );
00285 }
00286 
00287 QString OFileSelectorItem::key( int id, bool )const
00288 {
00289     QString ke;
00290 
00291     /*
00292      * id = 0 ||id == 1 : Sort By Name but Directories at Top
00293      * id = 2           : Sort By Size: Prepend '0' to the key
00294      */
00295     if( id == 0 || id == 1 )
00296     { // name
00297         if( m_isDir )
00298         {
00299             ke.append("0" );
00300             ke.append( text(1) );
00301         }
00302         else
00303         {
00304             ke.append("1" );
00305             ke.append( text(1) );
00306         }
00307         return ke;
00308     }else if(id == 2) {
00309     return text(2).rightJustify(20, '0');
00310     }else
00311         return text( id );
00312 
00313 }
00314 
00315 OFileViewFileListView::OFileViewFileListView( QWidget* parent, const QString& startDir, OFileSelector* sel)
00316                       :QWidget( parent ), m_sel( sel )
00317 {
00318     m_all = false;
00319     QVBoxLayout* lay = new QVBoxLayout( this );
00320     m_currentDir = startDir;
00321     bool bigicons = qApp->desktop()->size().width()>330;
00322 
00323     /*
00324      * now we add a special bar
00325      * One Button For Up
00326      * Home
00327      * Doc
00328      * And a dropdown menu with FileSystems
00329      * FUTURE: one to change dir with lineedit
00330      * Bookmarks
00331      * Create Dir
00332      */
00333     QHBox* box = new QHBox(this );
00334     box->setBackgroundMode( PaletteButton );
00335     box->setSpacing( 0 );
00336 
00337     QToolButton *btn = new QToolButton( box );
00338     btn->setUsesBigPixmap(bigicons);
00339     btn->setPixmap( Opie::Core::OResource::loadPixmap( "up", Opie::Core::OResource::SmallIcon ) );
00340     connect(btn, SIGNAL(clicked() ),
00341             this, SLOT( cdUP() ) );
00342 
00343     btn = new QToolButton( box );
00344     btn->setUsesBigPixmap(bigicons);
00345     btn->setPixmap( Opie::Core::OResource::loadPixmap( "home", Opie::Core::OResource::SmallIcon ) );
00346     connect(btn, SIGNAL(clicked() ),
00347             this, SLOT( cdHome() ) );
00348 
00349     btn = new QToolButton( box );
00350     btn->setUsesBigPixmap(bigicons);
00351     btn->setPixmap( Opie::Core::OResource::loadPixmap( "DocsIcon", Opie::Core::OResource::SmallIcon ) );
00352     connect(btn, SIGNAL(clicked() ),
00353             this, SLOT(cdDoc() ) );
00354 
00355     m_btnNew = new QToolButton( box );
00356     m_btnNew->setUsesBigPixmap(bigicons);
00357     m_btnNew->setPixmap( Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ) );
00358     connect(m_btnNew, SIGNAL(clicked() ),
00359             this, SLOT(slotNew() ) );
00360 
00361 
00362     m_btnClose = new QToolButton( box );
00363     m_btnClose->setUsesBigPixmap(bigicons);
00364     m_btnClose->setPixmap( Opie::Core::OResource::loadPixmap( "close", Opie::Core::OResource::SmallIcon ) );
00365     connect(m_btnClose, SIGNAL(clicked() ),
00366             selector(), SIGNAL(closeMe() ) );
00367 
00368     btn = new QToolButton( box );
00369     btn->setUsesBigPixmap(bigicons);
00370     btn->setPixmap( Opie::Core::OResource::loadPixmap( "pcmcia", Opie::Core::OResource::SmallIcon ) );
00371 
00372     m_fsButton = btn;
00373     /* let's fill device parts */
00374     QPopupMenu* pop = new QPopupMenu(this);
00375     connect(pop, SIGNAL( activated(int) ),
00376             this, SLOT(slotFSActivated(int) ) );
00377 
00378     StorageInfo storage;
00379     const QList<FileSystem> &fs = storage.fileSystems();
00380     QListIterator<FileSystem> it(fs);
00381     for ( ; it.current(); ++it )
00382     {
00383         const QString disk = (*it)->name();
00384         const QString path = (*it)->path();
00385         m_dev.insert( disk, path );
00386         pop->insertItem( disk );
00387     }
00388     m_fsPop = pop;
00389 
00390     connect(btn,SIGNAL(pressed()),this,SLOT(slotFSpressed()));
00391 
00392     lay->addWidget( box );
00393 
00394     m_view = new QListView( this );
00395 
00396     m_view->installEventFilter(this);
00397 
00398     QPEApplication::setStylusOperation( m_view->viewport(),
00399                                         QPEApplication::RightOnHold);
00400     m_view->addColumn(" " );
00401     m_view->addColumn(tr("Name"), 135 );
00402     m_view->addColumn(tr("Size"), -1 );
00403     m_view->addColumn(tr("Date"), 60 );
00404     m_view->addColumn(tr("Mime Type"), -1 );
00405 
00406 
00407     m_view->setSorting( 1 );
00408     m_view->setAllColumnsShowFocus( TRUE );
00409 
00410     lay->addWidget( m_view, 1000 );
00411     connectSlots();
00412 }
00413 
00414 void OFileViewFileListView::slotFSpressed()
00415 {
00416     m_fsPop->exec(QPoint( QCursor::pos().x(), QCursor::pos().y()));
00417     m_fsButton->setDown(false);
00418 }
00419 
00420 OFileViewFileListView::~OFileViewFileListView()
00421 {
00422 }
00423 
00424 void OFileViewFileListView::slotNew()
00425 {
00426     DocLnk lnk;
00427     emit selector()->newSelected( lnk );
00428 }
00429 
00430 OFileSelectorItem* OFileViewFileListView::currentItem()const
00431 {
00432     QListViewItem* item = m_view->currentItem();
00433     if (!item )
00434         return 0l;
00435 
00436     return static_cast<OFileSelectorItem*>(item);
00437 }
00438 
00439 void OFileViewFileListView::reread( bool all )
00440 {
00441     m_view->clear();
00442 
00443     if (selector()->showClose() )
00444         m_btnClose->show();
00445     else
00446         m_btnClose->hide();
00447 
00448     if (selector()->showNew() )
00449         m_btnNew->show();
00450     else
00451         m_btnNew->hide();
00452 
00453     m_mimes = selector()->currentMimeType();
00454     m_all = all;
00455 
00456     QDir dir( m_currentDir );
00457     if (!dir.exists() )
00458         return;
00459 
00460     dir.setSorting( QDir::Name | QDir::DirsFirst | QDir::Reversed );
00461     int filter;
00462     filter = QDir::Dirs;
00463     if ( selector()->mode() != OFileSelector::DIRECTORYSELECTOR )
00464         filter = filter | QDir::Files | QDir::All;
00465 
00466     if ( m_all )
00467         filter = filter | QDir::Hidden;
00468 
00469     dir.setFilter( filter );
00470 
00471     // now go through all files
00472     const QFileInfoList *list = dir.entryInfoList();
00473     if (!list)
00474     {
00475         cdUP();
00476         return;
00477     }
00478 
00479     QFileInfoListIterator it( *list );
00480     QFileInfo *fi;
00481     while( (fi=it.current() ) )
00482     {
00483         if( fi->fileName() == QString::fromLatin1("..") || fi->fileName() == QString::fromLatin1(".") )
00484         {
00485             ++it;
00486             continue;
00487         }
00488 
00489         /*
00490          * It is a symlink we try to resolve it now but don't let us attack by DOS
00491          *
00492          */
00493         if( fi->isSymLink() )
00494         {
00495             QString file = createNewPath(fi->dirPath( true ),fi->readLink());
00496             for( int i = 0; i<=4; i++)
00497             { // 5 tries to prevent dos
00498                 QFileInfo info( file );
00499                 if( !info.exists() )
00500                 {
00501                     addSymlink( fi, TRUE );
00502                     break;
00503                 }
00504                 else if( info.isDir() )
00505                 {
00506                     addDir( fi, TRUE );
00507                     break;
00508                 }
00509                 else if( info.isFile() )
00510                 {
00511                     addFile( fi, TRUE );
00512                     break;
00513                 }
00514                 else if( info.isSymLink() )
00515                 {
00516                     file = createNewPath(info.dirPath(true ),info.readLink());
00517                     break;
00518                 }
00519                 else if( i == 4)
00520                 { // couldn't resolve symlink add it as symlink
00521                     addSymlink( fi );
00522                 }
00523             } // off for loop for symlink resolving
00524         }
00525         else if( fi->isDir() )
00526             addDir(  fi );
00527         else if( fi->isFile() )
00528             addFile( fi );
00529 
00530         ++it;
00531     } // of while loop
00532     m_view->sort();
00533 
00534 }
00535 int OFileViewFileListView::fileCount()const
00536 {
00537     return m_view->childCount();
00538 }
00539 
00540 QString OFileViewFileListView::currentDir()const
00541 {
00542     return m_currentDir;
00543 }
00544 
00545 OFileSelector* OFileViewFileListView::selector()
00546 {
00547     return m_sel;
00548 }
00549 
00550 bool OFileViewFileListView::eventFilter (QObject *, QEvent *e)
00551 {
00552     if ( e->type() == QEvent::KeyPress )
00553     {
00554         QKeyEvent *k = (QKeyEvent *)e;
00555         if ( (k->key()==Key_Enter) || (k->key()==Key_Return))
00556         {
00557             slotClicked( Qt::LeftButton,m_view->currentItem(),QPoint(0,0),0);
00558             return true;
00559         }
00560     }
00561     return false;
00562 }
00563 
00564 void OFileViewFileListView::connectSlots()
00565 {
00566     connect(m_view, SIGNAL(clicked(QListViewItem*) ),
00567             this, SLOT(slotCurrentChanged(QListViewItem*) ) );
00568     connect(m_view, SIGNAL(mouseButtonClicked(int,QListViewItem*,const QPoint&,int) ),
00569             this, SLOT(slotClicked(int,QListViewItem*,const QPoint&,int) ) );
00570 }
00571 
00572 void OFileViewFileListView::slotCurrentChanged( QListViewItem* item)
00573 {
00574     if (!item)
00575         return;
00576 #if 0
00577 
00578     OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item);
00579 
00580     if (!sel->isDir() )
00581     {
00582         selector()->m_lneEdit->setText( sel->text(1) );
00583         // if in fileselector mode we will emit selected
00584         if ( selector()->mode() == OFileSelector::FileSelector )
00585         {
00586             odebug << "slot Current Changed" << oendl;
00587             QStringList str = QStringList::split("->", sel->text(1) );
00588             QString path = createNewPath(sel->directory(),str[0].stripWhiteSpace());
00589             emit selector()->fileSelected( path );
00590             DocLnk lnk( path );
00591             emit selector()->fileSelected( lnk );
00592         }
00593     }
00594 #endif
00595 }
00596 
00597 void OFileViewFileListView::slotClicked(int button , QListViewItem* item, const QPoint&, int )
00598 {
00599     if (!item || ( button != Qt::LeftButton) )
00600         return;
00601 
00602     OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item);
00603     if (!sel->isLocked() )
00604     {
00605         QStringList str = QStringList::split("->", sel->text(1) );
00606         if (sel->isDir() )
00607         {
00608             m_currentDir = createNewPath(sel->directory(),str[0].stripWhiteSpace());
00609             emit selector()->dirSelected( m_currentDir );
00610             reread( m_all );
00611         }
00612         else
00613         { // file
00614             odebug << "slot Clicked" << oendl;
00615             selector()->m_lneEdit->setText( str[0].stripWhiteSpace() );
00616             QString path = createNewPath(sel->directory(),str[0].stripWhiteSpace());
00617             emit selector()->fileSelected( path );
00618             DocLnk lnk( path );
00619             emit selector()->fileSelected( lnk );
00620         }
00621     } // not locked
00622 }
00623 
00624 void OFileViewFileListView::addFile( QFileInfo* info, bool symlink )
00625 {
00626     MimeType type( info->absFilePath() );
00627     if (!compliesMime( type.id() ) )
00628         return;
00629 
00630     QPixmap pix = type.pixmap();
00631     QString dir, name; bool locked;
00632     if ( pix.isNull() ) pix = Opie::Core::OResource::loadPixmap( "UnknownDocument", Opie::Core::OResource::SmallIcon );
00633     dir = info->dirPath( true );
00634     locked = false;
00635     if ( symlink )
00636         name = info->fileName() + " -> " + createNewPath(info->dirPath(),info->readLink());
00637     else
00638     {
00639         name = info->fileName();
00640         if ( ( (selector()->mode() == OFileSelector::Open)&& !info->isReadable() ) ||
00641                 ( (selector()->mode() == OFileSelector::Save)&& !info->isWritable() ) )
00642         {
00643             locked = true;
00644             pix = Opie::Core::OResource::loadPixmap( "locked", Opie::Core::OResource::SmallIcon );
00645         }
00646     }
00647     (void)new OFileSelectorItem( m_view, pix, name,
00648                                  info->lastModified().toString(), QString::number( info->size() ),
00649                                  dir, locked );
00650 }
00651 
00652 void OFileViewFileListView::addDir( QFileInfo* info, bool symlink )
00653 {
00654     bool locked = false; QString name; QPixmap pix;
00655 
00656     if ( ( ( selector()->mode() == OFileSelector::Open )  && !info->isReadable() ) ||
00657             ( ( selector()->mode() == OFileSelector::Save )  && !info->isWritable() ) )
00658     {
00659         locked = true;
00660         if ( symlink )
00661             pix = Opie::Core::OResource::loadPixmap( "opie/symlink" );
00662         else
00663             pix = Opie::Core::OResource::loadPixmap( "lockedfolder" );
00664     }
00665     else
00666         pix = symlink ? Opie::Core::OResource::loadPixmap( "opie/symlink" ) : Opie::Core::OResource::loadPixmap( "folder" );
00667 
00668     name = symlink ? info->fileName() + " -> " + createNewPath(info->dirPath(true),info->readLink()) :
00669            info->fileName();
00670 
00671     (void)new OFileSelectorItem( m_view, pix, name,
00672                                  info->lastModified().toString(),
00673                                  QString::number( info->size() ),
00674                                  info->dirPath( true ), locked, true );
00675 
00676 
00677 }
00678 
00679 void OFileViewFileListView::addSymlink( QFileInfo* , bool )
00680 {
00681 }
00682 
00683 void OFileViewFileListView::cdUP()
00684 {
00685     QDir dir( m_currentDir );
00686     dir.cdUp();
00687 
00688     if (!dir.exists() )
00689         m_currentDir = "/";
00690     else
00691         m_currentDir = dir.absPath();
00692 
00693     emit selector()->dirSelected( m_currentDir );
00694     reread( m_all );
00695 }
00696 
00697 void OFileViewFileListView::cdHome()
00698 {
00699     m_currentDir = QDir::homeDirPath();
00700     emit selector()->dirSelected( m_currentDir );
00701     reread( m_all );
00702 }
00703 
00704 void OFileViewFileListView::cdDoc()
00705 {
00706     m_currentDir = QPEApplication::documentDir();
00707     emit selector()->dirSelected( m_currentDir );
00708     reread( m_all );
00709 }
00710 
00711 void OFileViewFileListView::changeDir( const QString& dir )
00712 {
00713     m_currentDir = dir;
00714     emit selector()->dirSelected( m_currentDir );
00715     reread( m_all );
00716 }
00717 
00718 void OFileViewFileListView::slotFSActivated( int id )
00719 {
00720     changeDir ( m_dev[m_fsPop->text(id)]  );
00721 }
00722 
00723 /*  check if the mimetype in mime
00724  *  complies with the  one which is current
00725  */
00726 /*
00727  * We've the mimetype of the file
00728  * We need to get the stringlist of the current mimetype
00729  *
00730  * mime = image@slashjpeg
00731  * QStringList = 'image@slash*'
00732  * or QStringList = image/jpeg;image/png;application/x-ogg
00733  * or QStringList = application/x-ogg;image@slash*;
00734  * with all these mime filters it should get acceptes
00735  * to do so we need to look if mime is contained inside
00736  * the stringlist
00737  * if it's contained return true
00738  * if not ( I'm no RegExp expert at all ) we'll look if a '@slash*'
00739  * is contained in the mimefilter and then we will
00740  * look if both are equal until the '/'
00741  */
00742 
00743 bool OFileViewFileListView::compliesMime( const QString& str)
00744 {
00745     if (str.isEmpty() || m_mimes.isEmpty() || str.stripWhiteSpace().isEmpty() )
00746         return true;
00747 
00748     for (QStringList::Iterator it = m_mimes.begin(); it != m_mimes.end(); ++it )
00749     {
00750         QRegExp reg( (*it) );
00751         reg.setWildcard( true );
00752         if ( str.find( reg ) != -1 )
00753             return true;
00754 
00755     }
00756     return false;
00757 }
00758 /*
00759  * The listView giving access to the file system!
00760  */
00761 
00762 class OFileViewFileSystem : public OFileViewInterface
00763 {
00764 public:
00765     OFileViewFileSystem( OFileSelector* );
00766     ~OFileViewFileSystem();
00767 
00768     QString selectedName() const;
00769     QString selectedPath() const;
00770 
00771     QString directory()const;
00772     void reread();
00773     int fileCount()const;
00774 
00775     QWidget* widget( QWidget* parent );
00776     void activate( const QString& );
00777 private:
00778     OFileViewFileListView* m_view;
00779     bool m_all : 1;
00780 };
00781 
00782 OFileViewFileSystem::OFileViewFileSystem( OFileSelector* sel)
00783         : OFileViewInterface( sel )
00784 {
00785     m_view = 0;
00786     m_all = false;
00787 }
00788 
00789 OFileViewFileSystem::~OFileViewFileSystem()
00790 {
00791 }
00792 
00793 QString OFileViewFileSystem::selectedName()const
00794 {
00795     if (!m_view )
00796         return QString::null;
00797 
00798     QString cFN=currentFileName();
00799     if (cFN.startsWith("/")) return cFN;
00800     return createNewPath(m_view->currentDir(),cFN);
00801 }
00802 
00803 QString OFileViewFileSystem::selectedPath()const
00804 {
00805     return QString::null;
00806 }
00807 
00808 QString OFileViewFileSystem::directory()const
00809 {
00810     if (!m_view)
00811         return QString::null;
00812 
00813     OFileSelectorItem* item = m_view->currentItem();
00814     if (!item )
00815         return QString::null;
00816 
00817     return QDir(item->directory() ).absPath();
00818 }
00819 
00820 void OFileViewFileSystem::reread()
00821 {
00822     if (!m_view)
00823         return;
00824 
00825     m_view->reread( m_all );
00826 }
00827 
00828 int OFileViewFileSystem::fileCount()const
00829 {
00830     if (!m_view )
00831         return -1;
00832     return m_view->fileCount();
00833 }
00834 
00835 QWidget* OFileViewFileSystem::widget( QWidget* parent )
00836 {
00837     if (!m_view )
00838     {
00839         m_view = new OFileViewFileListView( parent, startDirectory(), selector() );
00840     }
00841     return m_view;
00842 }
00843 
00844 void OFileViewFileSystem::activate( const QString& str )
00845 {
00846     m_all = allItem( str );
00847 }
00848 
00849 
00850 }
00851 /* Selector */
00881 OFileSelector::OFileSelector( QWidget* parent, int mode, int sel,
00882                               const QString& dirName, const QString& fileName,
00883                               const MimeTypes& mimetypes,
00884                               bool showNew,  bool showClose)
00885               :QWidget( parent, "OFileSelector" )
00886 {
00887     m_current = 0;
00888     m_shNew = showNew;
00889     m_shClose = showClose;
00890     m_mimeType = mimetypes;
00891     m_startDir = dirName;
00892 
00893     m_mode = mode;
00894     m_selector = sel;
00895 
00896     m_allList = QStringList();
00897 
00898     initUI();
00899     m_lneEdit->setText( fileName );
00900     initMime();
00901     initViews();
00902 
00903     QString str;
00904     switch ( m_selector )
00905     {
00906     default:
00907     case Normal:
00908         if ( m_mode == DIRECTORYSELECTOR )
00909             str = QObject::tr("Directories");
00910         else
00911             str = QObject::tr("Documents");
00912         m_cmbView->setCurrentItem( 0 );
00913         break;
00914     case Extended:
00915         if ( m_mode == DIRECTORYSELECTOR )
00916         {
00917             str = QObject::tr("Directories");
00918             m_cmbView->setCurrentItem( 0 );
00919         } else {
00920             str = QObject::tr("Files");
00921             m_cmbView->setCurrentItem( 1 );
00922         }
00923         break;
00924     case ExtendedAll:
00925         if ( m_mode == DIRECTORYSELECTOR )
00926         {
00927             str = QObject::tr("All Directories");
00928             m_cmbView->setCurrentItem( 1 );
00929         } else {
00930             str = QObject::tr("All Files");
00931             m_cmbView->setCurrentItem( 2 );
00932         }
00933         break;
00934     }
00935     slotViewChange( str );
00936 
00937 }
00938 
00939 
00943 OFileSelector::OFileSelector( const QString& mimeFilter, QWidget* parent, const char* name,
00944                               bool showNew, bool showClose )
00945         : QWidget( parent, name )
00946 {
00947     m_current = 0;
00948     m_shNew   = showNew;
00949     m_shClose = showClose;
00950     m_startDir = QPEApplication::documentDir();
00951 
00952     if (!mimeFilter.isEmpty() )
00953         m_mimeType.insert(mimeFilter, QStringList::split(";", mimeFilter ) );
00954 
00955     m_mode = OFileSelector::FileSelector;
00956     m_selector = OFileSelector::Normal;
00957 
00958     initUI();
00959     initMime();
00960     initViews();
00961     m_cmbView->setCurrentItem( 0 );
00962     slotViewChange( QObject::tr("Documents") );
00963 }
00964 
00965 /*
00966  * INIT UI will set up the basic GUI
00967  * Layout: Simple VBoxLayout
00968  * On top a WidgetStack containing the Views...
00969  *  - List View
00970  *  - Document View
00971  * Below we will have a Label + LineEdit
00972  * Below we will have two ComoBoxes one for choosing the view one for
00973  *       choosing the mimetype
00974  */
00975 void OFileSelector::initUI()
00976 {
00977     QVBoxLayout* lay = new QVBoxLayout( this );
00978 
00979     m_stack = new QWidgetStack( this );
00980     lay->addWidget( m_stack, 1000 );
00981 
00982     m_nameBox = new QHBox( this );
00983     (void)new QLabel( tr("Name:"), m_nameBox );
00984     m_lneEdit = new QLineEdit( m_nameBox );
00985     m_lneEdit ->installEventFilter(this);
00986     lay->addWidget( m_nameBox );
00987 
00988     m_cmbBox = new QHBox( this );
00989     m_cmbView = new QComboBox( m_cmbBox );
00990     m_cmbMime = new QComboBox( m_cmbBox );
00991     lay->addWidget( m_cmbBox );
00992 }
00993 
00994 /*
00995  * This will make sure that the return key in the name edit causes dialogs to close
00996  */
00997 
00998 bool OFileSelector::eventFilter (QObject *, QEvent *e)
00999 {
01000     if ( e->type() == QEvent::KeyPress )
01001     {
01002         QKeyEvent *k = (QKeyEvent *)e;
01003         if ( (k->key()==Key_Enter) || (k->key()==Key_Return))
01004         {
01005             emit ok();
01006             return true;
01007         }
01008     }
01009     return false;
01010 }
01011 
01012 /*
01013  * This will insert the MimeTypes into the Combo Box
01014  * And also connect the changed signal
01015  *
01016  * AutoMimeTyping is disabled for now. It used to reparse a dir and then set available mimetypes
01017  */
01018 void OFileSelector::initMime()
01019 {
01020     MimeTypes::Iterator it;
01021     for ( it = m_mimeType.begin(); it != m_mimeType.end(); ++it )
01022     {
01023         m_cmbMime->insertItem( it.key() );
01024     }
01025     m_cmbMime->setCurrentItem( 0 );
01026 
01027     connect( m_cmbMime, SIGNAL(activated(int) ),
01028              this, SLOT(slotMimeTypeChanged() ) );
01029 
01030 }
01031 
01032 void OFileSelector::initViews()
01033 {
01034     if ( m_mode == OFileSelector::DIRECTORYSELECTOR )
01035     {
01036         m_cmbView->insertItem( QObject::tr("Directories") );
01037         m_cmbView->insertItem( QObject::tr("All Directories") );
01038     } else {
01039         m_cmbView->insertItem( QObject::tr("Documents") );
01040         m_cmbView->insertItem( QObject::tr("Files") );
01041         m_cmbView->insertItem( QObject::tr("All Files") );
01042     }
01043 
01044     connect(m_cmbView, SIGNAL(activated(const QString&) ),
01045             this, SLOT(slotViewChange(const QString&) ) );
01046 
01047     /* see above why add both */
01048     OFileViewInterface* in = new OFileViewFileSystem( this );
01049 
01050     if ( m_mode == OFileSelector::DIRECTORYSELECTOR )
01051     {
01052         m_views.insert( QObject::tr("Directories"), in );
01053         m_views.insert( QObject::tr("All Directories"), in );
01054         m_allList.append( QObject::tr("All Directories") );
01055     } else {
01056         m_views.insert( QObject::tr("Documents"), new ODocumentFileView(this) );
01057         m_views.insert( QObject::tr("Files"), in );
01058         m_views.insert( QObject::tr("All Files"), in );
01059         m_allList.append( QObject::tr("All Files") );
01060     }
01061 }
01062 
01063 void OFileSelector::registerView( const Internal::OFileViewInterface* iface ) {
01064     m_viewsPtr.append( iface );
01065 }
01066 
01067 
01071 OFileSelector::~OFileSelector()
01072 {
01073     m_viewsPtr.setAutoDelete( true );
01074     m_viewsPtr.clear();
01075 }
01076 
01077 
01078 
01086 const DocLnk* OFileSelector::selected()
01087 {
01088     DocLnk* lnk = new DocLnk( currentView()->selectedDocument() );
01089     return lnk;
01090 }
01091 
01096 QString OFileSelector::selectedName()const
01097 {
01098     return currentView()->selectedName();
01099 }
01100 
01101 
01105 QString OFileSelector::selectedPath()const
01106 {
01107     return currentView()->selectedPath();
01108 }
01109 
01113 QString OFileSelector::directory()const
01114 {
01115     return currentView()->directory();
01116 }
01117 
01121 DocLnk OFileSelector::selectedDocument()const
01122 {
01123     return currentView()->selectedDocument();
01124 }
01125 
01129 int OFileSelector::fileCount()const
01130 {
01131     return currentView()->fileCount();
01132 }
01133 
01137 void OFileSelector::reread()
01138 {
01139     return currentView()->reread();
01140 }
01141 
01142 OFileViewInterface* OFileSelector::currentView()const
01143 {
01144     return m_current;
01145 }
01146 
01147 bool OFileSelector::showNew()const
01148 {
01149     return m_shNew;
01150 }
01151 
01152 bool OFileSelector::showClose()const
01153 {
01154     return m_shClose;
01155 }
01156 
01157 MimeTypes OFileSelector::mimeTypes()const
01158 {
01159     return m_mimeType;
01160 }
01161 
01165 int OFileSelector::mode()const
01166 {
01167     return m_mode;
01168 }
01169 
01170 
01174 int OFileSelector::selector()const
01175 {
01176     return m_selector;
01177 }
01178 
01179 QStringList OFileSelector::currentMimeType()const
01180 {
01181     return m_mimeType[m_cmbMime->currentText()];
01182 }
01183 
01184 void OFileSelector::slotMimeTypeChanged()
01185 {
01186     reread();
01187 }
01188 
01189 void OFileSelector::slotDocLnkBridge( const DocLnk& lnk)
01190 {
01191     m_lneEdit->setText( lnk.name() );
01192     emit fileSelected( lnk );
01193     emit fileSelected( lnk.name() );
01194 }
01195 
01196 void OFileSelector::slotFileBridge( const QString& str)
01197 {
01198     DocLnk lnk( str );
01199     emit fileSelected( lnk );
01200 }
01201 
01202 void OFileSelector::slotViewChange( const QString& view )
01203 {
01204     OFileViewInterface* interface = m_views[view];
01205     if (!interface)
01206         return;
01207 
01208     if (m_current)
01209         m_stack->removeWidget( m_current->widget( m_stack ) );
01210 
01211     static int id = 1;
01212 
01213     m_stack->addWidget( interface->widget(m_stack), id );
01214     m_stack->raiseWidget( id );
01215 
01216     interface->activate( view );
01217     interface->reread();
01218     m_current = interface;
01219 
01220     id++;
01221 }
01222 
01223 void OFileSelector::setNewVisible( bool b )
01224 {
01225     m_shNew = b;
01226     currentView()->reread();
01227 }
01228 
01229 void OFileSelector::setCloseVisible( bool b )
01230 {
01231     m_shClose = b;
01232     currentView()->reread();
01233 }
01234 
01235 void OFileSelector::setNameVisible( bool b )
01236 {
01237     if ( b )
01238         m_nameBox->show();
01239     else
01240         m_nameBox->hide();
01241 }
01242 
01243 }
01244 }

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