00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <qlayout.h>
00010 #include <qcombobox.h>
00011 #include <qpushbutton.h>
00012 #include <qvbox.h>
00013 #include <qhbox.h>
00014 #include <qlistview.h>
00015 #include <qfileinfo.h>
00016 #include <qpixmap.h>
00017 #include <qlabel.h>
00018 #include <qlineedit.h>
00019
00020 #include <qpe/mimetype.h>
00021 #include <qpe/resource.h>
00022
00023 #include "ZFileDialog.h"
00024
00025 class ZFileItem : public QListViewItem {
00026
00027 public:
00028
00029 ZFileItem( QListView *parent, const QFileInfo finfo ) :
00030 QListViewItem( parent ), finfo( finfo ) {
00031 }
00032
00033 QString text( int column ) const {
00034 if( column == 1 ) {
00035 if( finfo.isDir() )
00036 return "<dir>";
00037 else {
00038 uint fsize = finfo.size();
00039 if( fsize < 1000 )
00040 return QString::number(finfo.size());
00041 else if( fsize < 1000000 ) {
00042 return QString::number(finfo.size()/1000) + " Kb";
00043 } else
00044 return QString::number(finfo.size()/1000000) + " Mb";
00045 }
00046 }
00047 if( column == 2 ) {
00048 QDate fdate = finfo.lastModified().date();
00049 QString dateStr;
00050 dateStr.sprintf( "%04d/%02d/%02d", fdate.year(), fdate.month(), fdate.day() );
00051 return dateStr;
00052 }
00053 return finfo.fileName();
00054 }
00055
00056 virtual const QPixmap* pixmap( int column ) const {
00057 if( column != 0 )
00058 return NULL;
00059
00060 if( file_pixmap.isNull() ) {
00061 if( finfo.isDir() )
00062 file_pixmap = Resource::loadPixmap( "folder" );
00063 else {
00064 const MimeType mime( finfo.absFilePath() );
00065 if( mime.id().length() == 0 )
00066 return NULL;
00067 file_pixmap = mime.pixmap();
00068 }
00069 }
00070 return &file_pixmap;
00071 }
00072
00073 public:
00074
00075 const QFileInfo finfo;
00076
00077 protected:
00078
00079
00080
00081
00082 mutable QPixmap file_pixmap;
00083
00084 };
00085
00086 ZFileDialog::ZFileDialog( const QString title, const QString &path, Mode mode, QWidget *parent ) :
00087 QDialog( parent, "zfiledialog", true ), mode( mode ), customFilter( NULL ) {
00088 setCaption( title );
00089
00090
00091
00092
00093
00094 QBoxLayout *l = new QVBoxLayout( this );
00095 l->setMargin( 2 );
00096
00097 QVBox* vbox = new QVBox( this );
00098 vbox->setSpacing(2);
00099 vbox->setMargin(2);
00100 l->addWidget( vbox );
00101
00102 QHBox* hbox;
00103
00104 if( mode == AnyFile ) {
00105 QHBox* hbox = new QHBox( vbox );
00106 hbox->setSpacing(2);
00107 new QLabel( tr( "File" ), hbox );
00108 selection = new QLineEdit( hbox );
00109 }
00110
00111 hbox = new QHBox( vbox );
00112 fstabCB = new QComboBox( hbox );
00113 QPixmap pixmap = Resource::loadPixmap( "folderup" );
00114 QPushButton *parentBT = new QPushButton( pixmap, QString::null, hbox );
00115 parentBT->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) );
00116 parentBT->setFixedWidth( pixmap.width() );
00117
00118
00119
00120
00121
00122 fileLV = new QListView( vbox );
00123 fileLV->addColumn( tr( "Name" ) );
00124 fileLV->addColumn( tr( "Size" ) );
00125 fileLV->addColumn( tr( "Date" ) );
00126 if( mode == ExistingFiles )
00127 fileLV->setMultiSelection( true );
00128
00129 showMaximized();
00130 currentDir.setSorting( QDir::Name | QDir::DirsFirst | QDir::IgnoreCase );
00131
00132 setDir( path );
00133
00134
00135
00136 connect( fstabCB, SIGNAL( activated( int ) ), this, SLOT( dirSelected( int ) ) );
00137 connect( fileLV, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( fileClicked( QListViewItem* ) ) );
00138 connect( parentBT, SIGNAL( clicked() ), this, SLOT( parentDirClicked() ) );
00139
00140 if( mode == AnyFile ) {
00141 connect( fileLV, SIGNAL( selectionChanged() ), this, SLOT( itemSelected() ) );
00142 }
00143 }
00144
00145 void ZFileDialog::insertDirEntry( const QString &label, const QString &path, QPixmap *pixmap ) {
00146 if( pixmap == NULL )
00147 fstabCB->insertItem( label );
00148 else
00149 fstabCB->insertItem( *pixmap, label );
00150
00151 dir_list.append( path );
00152 }
00153
00154 int ZFileDialog::insertDirTree( const QString &label, QString basePath, const QString &path, bool subCheck, QPixmap *pixmap ) {
00155 insertDirEntry( label, basePath, pixmap );
00156 if( basePath == path )
00157 fstabCB->setCurrentItem( fstabCB->count()-1 );
00158
00159 if( !subCheck )
00160 return false;
00161
00162 int level = 1;
00163 bool finish = false;
00164 while( !finish && path.left( basePath.length() ) == basePath ) {
00165 int start_ind = basePath.length();
00166 while( path[start_ind] == '/' )
00167 start_ind++;
00168 int nextDir = path.find( '/', start_ind );
00169 if( nextDir == -1 ) {
00170 finish = true;
00171 nextDir = path.length();
00172 }
00173 if( nextDir <= start_ind )
00174 break;
00175
00176 basePath = path.left( nextDir );
00177
00178 QString tab;
00179 for( int l = 0; l < level; l++ )
00180 tab += " ";
00181
00182 insertDirEntry( tab + path.mid( start_ind, nextDir-start_ind ), basePath );
00183 level++;
00184 }
00185 if( finish ) {
00186 fstabCB->setCurrentItem( fstabCB->count()-1 );
00187 return true;
00188 }
00189
00190 return false;
00191 }
00192
00193 void ZFileDialog::setDir( QString path ) {
00194 currentDir.setPath( path );
00195 if( !currentDir.exists() ) {
00196 path = "/";
00197 currentDir.setPath( path );
00198 }
00199
00200 if( isVisible() )
00201 refreshFileList();
00202 }
00203
00204 void ZFileDialog::refreshFileList() {
00205 fileLV->clear();
00206
00207 QString path = currentDir.path();
00208
00209 QStringList flist = currentDir.entryList();
00210 if( customFilter != NULL ) {
00211 customFilter->filter( flist );
00212 }
00213
00214 dir_list.clear();
00215 fstabCB->clear();
00216
00217 QPixmap homePixmap = Resource::loadPixmap( "home" );
00218 QPixmap sdPixmap = Resource::loadPixmap( "SDDeviceS" );
00219 QPixmap cfPixmap = Resource::loadPixmap( "CFDeviceS" );
00220
00221 bool selected = false;
00222 selected |= insertDirTree( "Home", "/home/zaurus", path, !selected, &homePixmap );
00223 selected |= insertDirTree( "SD Card", "/mnt/card", path, !selected, &sdPixmap );
00224 selected |= insertDirTree( "CF Card", "/mnt/cf", path, !selected, &cfPixmap );
00225 selected |= insertDirTree( "Root", "/", path, !selected );
00226
00227
00228 for( QStringList::Iterator itf = flist.begin(); itf != flist.end(); itf++ ) {
00229
00230 const QFileInfo finfo( currentDir, *itf );
00231 if( finfo.fileName().left( 1 ) == "." ) continue;
00232 if( finfo.isFile() && !filters.isEmpty() ) {
00233 bool matched = false;
00234 for( QStringList::Iterator it = filters.begin(); it != filters.end(); it++ ) {
00235 if( finfo.fileName().right( (*it).length() ) == *it ) {
00236 matched = true;
00237 break;
00238 }
00239 }
00240 if( !matched )
00241 continue;
00242 }
00243 fileLV->insertItem( new ZFileItem( fileLV, finfo ) );
00244 }
00245 }
00246
00247 void ZFileDialog::show() {
00248 QDialog::show();
00249 refreshFileList();
00250 }
00251
00252 void ZFileDialog::fileClicked( QListViewItem *it ) {
00253 if( it == NULL )
00254 return;
00255 ZFileItem *zitem = (ZFileItem*)it;
00256 if( zitem->finfo.isDir() ) {
00257 setDir( zitem->finfo.absFilePath( ) );
00258 }
00259 }
00260
00261 void ZFileDialog::dirSelected( int idx ) {
00262 setDir( dir_list[idx] );
00263 }
00264
00265 void ZFileDialog::parentDirClicked() {
00266 if( currentDir.isRoot() )
00267 return;
00268 QString path = currentDir.path();
00269 int lastDir = path.length()-1;
00270 if( lastDir <= 0 )
00271 return;
00272 while( path[lastDir] == '/' )
00273 lastDir--;
00274 lastDir = path.findRev( '/', lastDir );
00275 if( lastDir < 0 )
00276 return;
00277 setDir( path.left( lastDir ) );
00278 }
00279
00280 void ZFileDialog::setFilters( const QStringList &filters ) {
00281 this->filters = filters;
00282 }
00283
00284 QStringList ZFileDialog::selectedFiles() const {
00285 QStringList selected;
00286
00287 for( QListViewItem *item = fileLV->firstChild(); item != NULL; item = item->nextSibling() ) {
00288 ZFileItem *fitem = (ZFileItem*)item;
00289 if( fileLV->isSelected(item) ) {
00290 selected.append( fitem->finfo.filePath() );
00291 }
00292 }
00293 return selected;
00294 }
00295
00296 QString ZFileDialog::selectedFile() const {
00297 if( mode == AnyFile ) {
00298 return currentDir.path() + "/" + selection->text();
00299 }
00300 else {
00301 ZFileItem *fitem = (ZFileItem*)fileLV->selectedItem();
00302 if( fitem == NULL ) return QString::null;
00303 return fitem->finfo.filePath();
00304 }
00305 }
00306
00307 void ZFileDialog::setSelection( const QString &name ) {
00308 selection->setText( name );
00309 }
00310
00311 void ZFileDialog::itemSelected( ) {
00312 QListViewItem *it = fileLV->selectedItem();
00313 if( it == NULL )
00314 return;
00315 ZFileItem *zitem = (ZFileItem*)it;
00316 if( zitem->isSelected() && zitem->finfo.isFile() ) {
00317 selection->setText( zitem->finfo.fileName() );
00318 }
00319 }