-rw-r--r-- | noncore/unsupported/filebrowser/filebrowser.cpp | 153 | ||||
-rw-r--r-- | noncore/unsupported/filebrowser/filebrowser.h | 11 |
2 files changed, 158 insertions, 6 deletions
diff --git a/noncore/unsupported/filebrowser/filebrowser.cpp b/noncore/unsupported/filebrowser/filebrowser.cpp index 8c01655..10e50f0 100644 --- a/noncore/unsupported/filebrowser/filebrowser.cpp +++ b/noncore/unsupported/filebrowser/filebrowser.cpp @@ -1,113 +1,119 @@ /********************************************************************** ** Copyright (C) 2000 Trolltech AS. All rights reserved. ** ** This file is part of Qtopia Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** See http://www.trolltech.com/gpl/ for GPL licensing information. ** ** Contact info@trolltech.com if any conditions of this licensing are ** not clear to you. ** **********************************************************************/ + #include "inlineedit.h" #include "filebrowser.h" #include "filePermissions.h" #include <qpe/resource.h> #include <qpe/global.h> #include <qpe/mimetype.h> #include <qpe/applnk.h> #include <qpe/config.h> #include <qcopchannel_qws.h> #include <qpe/qcopenvelope_qws.h> #include <qmessagebox.h> #include <qdir.h> #include <qregexp.h> #include <qheader.h> #include <qpe/qpetoolbar.h> #include <qpopupmenu.h> #include <qpe/qpemenubar.h> #include <qaction.h> #include <qstringlist.h> #include <qcursor.h> #include <qmultilineedit.h> #include <qfont.h> #include <qpainter.h> +#include <qprogressbar.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> +#include <qpe/qpeapplication.h> // // FileItem // FileItem::FileItem( QListView * parent, const QFileInfo & fi ) : QListViewItem( parent ), fileInfo( fi ) { QDate d = fi.lastModified().date(); setText( 0, fi.fileName() ); setText( 1, sizeString( fi.size() ) + " " ); setText( 2, QString().sprintf("%4d-%02d-%02d",d.year(), d.month(), d.day() ) ); MimeType mt(fi.filePath()); if ( fi.isSymLink() ) setText( 3, "symlink" ); else if( fi.isDir() ) setText( 3, "directory" ); else if( isLib() ) setText( 3, "library" ); else setText( 3, mt.description() ); QPixmap pm; if( fi.isDir() ){ if( !QDir( fi.filePath() ).isReadable() ) pm = Resource::loadPixmap( "lockedfolder" ); else pm = Resource::loadPixmap( "folder" ); } else if( !fi.isReadable() ) pm = Resource::loadPixmap( "locked" ); else if( isLib() ) pm = Resource::loadPixmap( "library" ); + else if( ((FileView* )parent)->getShowThumbnails() && mt.id().contains(QRegExp("^image/", FALSE, FALSE)) ) + pm = drawThumbnail(fi); else pm = mt.pixmap(); if ( pm.isNull() ) pm = Resource::loadPixmap("UnknownDocument-14"); + if( fi.isSymLink() ){ // overlay link image QPixmap lnk = Resource::loadPixmap( "filebrowser/symlink" ); QPainter painter( &pm ); painter.drawPixmap( pm.width()-lnk.width(), pm.height()-lnk.height(), lnk ); pm.setMask( pm.createHeuristicMask( FALSE ) ); } setPixmap(0,pm); } QString FileItem::sizeString( unsigned int s ) { double size = s; if ( size > 1024 * 1024 * 1024 ) return QString().sprintf( "%.1f", size / ( 1024 * 1024 * 1024 ) ) + "G"; else if ( size > 1024 * 1024 ) return QString().sprintf( "%.1f", size / ( 1024 * 1024 ) ) + "M"; else if ( size > 1024 ) return QString().sprintf( "%.1f", size / ( 1024 ) ) + "K"; else return QString::number( size ) + "B"; } @@ -151,60 +157,112 @@ int FileItem::launch() doc.execute(); listView()->clearSelection(); return 1; } bool FileItem::rename( const QString & name ) { QString oldpath, newpath; if ( name.isEmpty() ) return FALSE; if ( name.contains( QRegExp("[/\\$\"\'\\*\\?]") ) ) return FALSE; oldpath = fileInfo.filePath(); newpath = fileInfo.dirPath() + "/" + name; if ( ::rename( (const char *) oldpath, (const char *) newpath ) != 0 ) return FALSE; else return TRUE; } +QPixmap FileItem::drawThumbnail(const QFileInfo &file) { + + /* + * this thing is sloooooow, and it also doesn't load + * dynamicly (like a web browser). if anyone knows how to + * do that, please do! + */ + QString cacheDir = "/tmp/filebrowserThumbnailCache"; + QFileInfo cachedFile (cacheDir + file.filePath()); + + if (cachedFile.exists() && cachedFile.lastModified() == file.lastModified()) { + + QPixmap cachedImage (cachedFile.filePath()); + return cachedImage; + } + else { + + QImage image (file.filePath()); + + // if inside of cache dir, don't render thumbnails! recursive error! + if (image.isNull() || file.filePath().contains(QRegExp("^" + cacheDir))) { + DocLnk doc (file.filePath()); + return doc.pixmap(); + } + Config cfg("Filebrowser"); + cfg.setGroup("View"); + int size; + size =cfg.readNumEntry("ThumbSize", 72); + QPixmap thumb (size, size); + + double scale = (double)image.height() / (double)image.width(); + int newHeight = int(size * scale); + thumb.convertFromImage (image.smoothScale(size, newHeight)); + + if (!cachedFile.dir().exists()) { + QString cmd = "/bin/mkdir -p \"" + cachedFile.dirPath() +"\""; + system( (const char *) cmd ); + } + + if (thumb.save(cachedFile.filePath(), QPixmap::imageFormat(file.filePath()), 70)) { + // make thumbnail modify time the same as the image + QString cmd = "/bin/touch -r \"" + file.filePath() +"\" " + + "\"" + cachedFile.filePath() + "\""; + system( (const char *) cmd ); + + } + + return thumb; + } +} + // // FileView // FileView::FileView( const QString & dir, QWidget * parent, const char * name, - bool hidden, bool symlinks ) + bool hidden, bool symlinks, bool thumbnails ) : QListView( parent, name ), menuTimer( this ), le( NULL ), itemToRename( NULL ), showHidden( hidden ), showSymlinks( symlinks), + showThumbnails( thumbnails ), menuKeepsOpen( FALSE ) { addColumn( "Name" ); addColumn( "Size" ); addColumn( "Date" ); addColumn( "Type" ); setMultiSelection( TRUE ); //header()->hide(); setColumnWidthMode( 0, Manual ); setColumnWidthMode( 3, Manual ); // right align yize column setColumnAlignment( 1, AlignRight ); generateDir( dir ); connect( this, SIGNAL( clicked( QListViewItem * )), SLOT( itemClicked( QListViewItem * )) ); connect( this, SIGNAL( doubleClicked( QListViewItem * )), SLOT( itemDblClicked( QListViewItem * )) ); connect( this, SIGNAL( selectionChanged() ), SLOT( cancelMenuTimer() ) ); connect( &menuTimer, SIGNAL( timeout() ), SLOT( showFileMenu() ) ); @@ -232,67 +290,87 @@ void FileView::setDir( const QString & dir ) return; } dirHistory += currentDir; generateDir( dir ); } void FileView::generateDir( const QString & dir ) { if(menuKeepsOpen){ cancelMenuTimer(); } QDir d( dir ); if( d.exists() && !d.isReadable() ) return; currentDir = d.canonicalPath(); if( !showHidden) d.setFilter( QDir::Dirs | QDir::Files ); else d.setFilter( QDir::Dirs | QDir::Files |QDir::Hidden | QDir::All); d.setSorting( QDir::Name | QDir::DirsFirst | QDir::IgnoreCase | QDir::Reversed ); - const QFileInfoList * list = d.entryInfoList(); QFileInfoListIterator it( *list ); QFileInfo *fi; + QProgressBar *thumbProgress = 0; + if (showThumbnails) { + + thumbProgress = new QProgressBar(it.count(), this); + thumbProgress->show(); + } + clear(); + + int fileCount = 1; // used in the thumbnail progress meter while( (fi = it.current()) ){ if( (fi->fileName() == ".") || (fi->fileName() == "..") ){ ++it; continue; } if(!showSymlinks && fi->isSymLink()){ ++it; continue; } + // thumbnail progress + if (showThumbnails) { + + thumbProgress->setProgress(fileCount); + } (void) new FileItem( (QListView *) this, *fi ); + ++it; + ++fileCount; + } + + if (showThumbnails) { + thumbProgress->close(); } emit dirChanged(); + } void FileView::rename() { itemToRename = (FileItem *) currentItem(); const QPixmap * pm; int pmw; if( itemToRename == NULL ) return; if( ( pm = itemToRename->pixmap( 0 ) ) == NULL ) pmw = 0; else pmw = pm->width(); ensureItemVisible( itemToRename ); horizontalScrollBar()->setValue( 0 ); horizontalScrollBar()->setEnabled( FALSE ); verticalScrollBar()->setEnabled( FALSE ); selected = isSelected( itemToRename ); setSelected( itemToRename, FALSE ); if( le == NULL ){ @@ -442,48 +520,67 @@ bool FileView::copyFile( const QString & dest, const QString & src ) return success; } void FileView::cut() { int err; // ##### a better inmplementation might be to rename the CUT file // ##### to ".QPE-FILEBROWSER-MOVING" rather than copying it. QString cmd, dest, basename, cd = "/tmp/qpemoving"; QStringList newflist; newflist.clear(); cmd = "rm -rf " + cd; system ( (const char *) cmd ); cmd = "mkdir " + cd; system( (const char *) cmd ); // get the names of the files to cut FileItem * item; if((item = (FileItem *) firstChild()) == 0) return; flist.clear(); while( item ){ + if( ite + // ##### a better inmplementation might be to rename the CUT file
+ // ##### to ".QPE-FILEBROWSER-MOVING" rather than copying it.
+ QString cmd, dest, basename, cd = "/tmp/qpemoving";
+ QStringList newflist;
+ newflist.clear();
+
+ cmd = "rm -rf " + cd;
+ system ( (const char *) cmd );
+ cmd = "mkdir " + cd;
+ system( (const char *) cmd );
+
+// get the names of the files to cut
+ FileItem * item;
+
+ if((item = (FileItem *) firstChild()) == 0) return;
+
+ flist.clear();
+ while( item ){
if( item->isSelected() /*&& !item->isDir()*/ ){ flist += item->getFilePath(); } item = (FileItem *) item->nextSibling(); } // move these files into a tmp dir for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) { basename = (*it).mid((*it).findRev("/") + 1, (*it).length()); dest = cd + "/" + basename; newflist += dest; cmd = "/bin/mv -f \"" + (*it) +"\" " + "\"" + dest + "\""; err = system( (const char *) cmd ); if ( err != 0 ) { QMessageBox::warning( this, tr("Cut file"), tr("Cut failed!"), tr("Ok") ); break; } else { updateDir(); QListViewItem * im = firstChild(); @@ -692,113 +789,123 @@ void FileView::showFileMenu() m->insertItem( Resource::loadPixmap("paste"), tr( "Paste" ), this, SLOT( paste() ) ); m->insertItem( tr( "Change Permissions" ), this, SLOT( chPerm() ) ); m->insertItem(Resource::loadPixmap( "close" ), tr( "Delete" ), this, SLOT( del() ) ); m->insertSeparator(); m->insertItem( tr( "Select all" ), this, SLOT( selectAll() ) ); m->insertItem( tr( "Deselect all" ), this, SLOT( deselectAll() ) ); m->popup( QCursor::pos() ); } // // FileBrowser // void FileView::setShowHidden(bool hidden) { showHidden=hidden; } void FileView::setShowSymlinks(bool symlinks) { showSymlinks=symlinks; } +void FileView::setShowThumbnails(bool thumbnails) +{ + showThumbnails=thumbnails; +} + void FileView::setMenuKeepsOpen(bool keepOpen) { menuKeepsOpen=keepOpen; } FileBrowser::FileBrowser( QWidget * parent, const char * name, WFlags f ) : QMainWindow( parent, name, f ) { init( QDir::current().canonicalPath() ); } FileBrowser::FileBrowser( const QString & dir, QWidget * parent, const char * name, WFlags f ) : QMainWindow( parent, name, f ) { init( dir ); } void FileBrowser::init(const QString & dir) { setCaption( tr("File Manager") ); setIcon( Resource::loadPixmap( "filebrowser_icon" ) ); + connect( qApp,SIGNAL( aboutToQuit()),SLOT( cleanUp()) ); + Config cfg("Filebrowser"); cfg.setGroup("View"); bool showHidden=(cfg.readEntry("Hidden","FALSE") == "TRUE"); bool showSymlinks=(cfg.readEntry("Symlinks","FALSE") == "TRUE"); + bool showThumbnails=(cfg.readEntry("Thumbnails","FALSE") == "TRUE"); cfg.setGroup("Menu"); bool menuKeepsOpen=(cfg.readEntry("KeepOpen", "FALSE") == "TRUE"); - fileView = new FileView( dir, this, 0, showHidden, showSymlinks ); + fileView = new FileView( dir, this, 0, showHidden, showSymlinks, showThumbnails ); fileView->setAllColumnsShowFocus( TRUE ); fileView->setMenuKeepsOpen(menuKeepsOpen); setCentralWidget( fileView ); setToolBarsMovable( FALSE ); QPEToolBar* toolBar = new QPEToolBar( this ); toolBar->setHorizontalStretchable( TRUE ); QPEMenuBar* menuBar = new QPEMenuBar( toolBar ); dirMenu = new QPopupMenu( this ); menuBar->insertItem( tr( "Dir" ), dirMenu ); sortMenu = new QPopupMenu( this ); menuBar->insertItem( tr( "Sort" ), sortMenu ); sortMenu->insertItem( tr( "by Name "), this, SLOT( sortName() ) ); sortMenu->insertItem( tr( "by Size "), this, SLOT( sortSize() ) ); sortMenu->insertItem( tr( "by Date "), this, SLOT( sortDate() ) ); sortMenu->insertItem( tr( "by Type "), this, SLOT( sortType() ) ); sortMenu->insertSeparator(); sortMenu->insertItem( tr( "Ascending" ), this, SLOT( updateSorting() ) ); sortMenu->setItemChecked( sortMenu->idAt( 5 ), TRUE ); sortMenu->setItemChecked( sortMenu->idAt( 0 ), TRUE ); viewMenu = new QPopupMenu( this); viewMenu->insertItem( tr( "Hidden"), this, SLOT( updateShowHidden() ) ); viewMenu->insertItem( tr( "Symlinks"), this, SLOT( updateShowSymlinks() ) ); + viewMenu->insertItem( tr( "Thumbnails"), this, SLOT( updateShowThumbnails() ) ); viewMenu->setItemChecked( viewMenu->idAt( 0 ), showHidden ); viewMenu->setItemChecked( viewMenu->idAt( 1 ), showSymlinks ); + viewMenu->setItemChecked( viewMenu->idAt( 2 ), showThumbnails ); menuBar->insertItem( tr("View"), viewMenu ); toolBar = new QPEToolBar( this ); lastAction = new QAction( tr("Previous dir"), Resource::loadIconSet( "back" ), QString::null, 0, this, 0 ); connect( lastAction, SIGNAL( activated() ), fileView, SLOT( lastDir() ) ); lastAction->addTo( toolBar ); lastAction->setEnabled( FALSE ); upAction = new QAction( tr("Parent dir"), Resource::loadIconSet( "up" ), QString::null, 0, this, 0 ); connect( upAction, SIGNAL( activated() ), fileView, SLOT( parentDir() ) ); upAction->addTo( toolBar ); QAction *a = new QAction( tr("New folder"), Resource::loadPixmap( "newfolder" ), QString::null, 0, this, 0 ); connect( a, SIGNAL( activated() ), fileView, SLOT( newFolder() ) ); a->addTo( toolBar ); a = new QAction( tr("Cut"), Resource::loadPixmap( "cut" ), QString::null, 0, this, 0 ); connect( a, SIGNAL( activated() ), fileView, SLOT( cut() ) ); @@ -909,49 +1016,48 @@ void FileBrowser::sortType() sortMenu->setItemChecked( sortMenu->idAt( 0 ), FALSE ); sortMenu->setItemChecked( sortMenu->idAt( 1 ), FALSE ); sortMenu->setItemChecked( sortMenu->idAt( 2 ), FALSE ); sortMenu->setItemChecked( sortMenu->idAt( 3 ), TRUE ); } void FileBrowser::updateSorting() { sortMenu->setItemChecked( sortMenu->idAt( 5 ), !sortMenu->isItemChecked( sortMenu->idAt( 5 ) ) ); if ( sortMenu->isItemChecked( sortMenu->idAt( 0 ) ) ) sortName(); else if ( sortMenu->isItemChecked( sortMenu->idAt( 1 ) ) ) sortSize(); else if ( sortMenu->isItemChecked( sortMenu->idAt( 2 ) ) ) sortDate(); else sortType(); } void FileView::chPerm() { FileItem * i; QStringList fl; QString cmd; - int err; if((i = (FileItem *) firstChild()) == 0) return; while( i ){ if( i->isSelected() ){ fl += i->getFilePath(); } i = (FileItem *) i->nextSibling(); } if( fl.count() < 1 ) return; if( QMessageBox::warning( this, tr("Change permissions"), tr("Are you sure?"), tr("Yes"), tr("No") ) == 0) { for ( QStringList::Iterator it = fl.begin(); it != fl.end(); ++it ) { filePermissions *filePerm; filePerm = new filePermissions(this, "Permissions",true,0,(const QString &)(*it)); filePerm->exec(); if( filePerm) delete filePerm; break; } updateDir(); } } @@ -961,24 +1067,63 @@ void FileBrowser::updateShowHidden() valShowHidden=!valShowHidden; viewMenu->setItemChecked( viewMenu->idAt( 0 ), valShowHidden ); fileView->setShowHidden(valShowHidden); Config cfg("Filebrowser"); cfg.setGroup("View"); cfg.writeEntry("Hidden",valShowHidden?"TRUE":"FALSE"); fileView->updateDir(); } void FileBrowser::updateShowSymlinks() { bool valShowSymlinks=viewMenu->isItemChecked( viewMenu->idAt( 1 ) ); valShowSymlinks=!valShowSymlinks; viewMenu->setItemChecked( viewMenu->idAt( 1 ), valShowSymlinks ); fileView->setShowSymlinks(valShowSymlinks); Config cfg("Filebrowser"); cfg.setGroup("View"); cfg.writeEntry("Symlinks",valShowSymlinks?"TRUE":"FALSE"); fileView->updateDir(); } + +void FileBrowser::updateShowThumbnails() +{ + bool valShowThumbnails=viewMenu->isItemChecked( viewMenu->idAt( 2 ) ); + valShowThumbnails=!valShowThumbnails; + viewMenu->setItemChecked( viewMenu->idAt( 2 ), valShowThumbnails ); + fileView->setShowThumbnails(valShowThumbnails); + + Config cfg("Filebrowser"); + cfg.setGroup("View"); + cfg.writeEntry("Thumbnails",valShowThumbnails?"TRUE":"FALSE"); + + fileView->updateDir(); +} + +void FileBrowser::cleanUp() { + QString cmdr = "rm -rf /tmp/filebrowserThumbnailCache"; +// qDebug("exit"); + system(cmdr.latin1()); +} +
+{
+ bool valShowThumbnails=viewMenu->isItemChecked( viewMenu->idAt( 2 ) );
+ valShowThumbnails=!valShowThumbnails;
+ viewMenu->setItemChecked( viewMenu->idAt( 2 ), valShowThumbnails );
+ fileView->setShowThumbnails(valShowThumbnails);
+
+ Config cfg("Filebrowser");
+ cfg.setGroup("View");
+ cfg.writeEntry("Thumbnails",valShowThumbnails?"TRUE":"FALSE");
+
+ fileView->updateDir();
+}
+
+void FileBrowser::cleanUp() {
+ QString cmdr = "rm -rf /tmp/filebrowserThumbnailCache";
+// qDebug("exit");
+ system(cmdr.latin1());
+}
diff --git a/noncore/unsupported/filebrowser/filebrowser.h b/noncore/unsupported/filebrowser/filebrowser.h index 983e58e..549d463 100644 --- a/noncore/unsupported/filebrowser/filebrowser.h +++ b/noncore/unsupported/filebrowser/filebrowser.h @@ -24,132 +24,139 @@ #include <qmainwindow.h> #include <qfileinfo.h> #include <qaction.h> #include <qtimer.h> #include <qstringlist.h> class QLabel; class InlineEdit; class FileItem : public QListViewItem { public: FileItem( QListView * parent, const QFileInfo & fi ); QString key( int column, bool ascending = TRUE ) const; QString getFilePath(){ return fileInfo.filePath(); } QString getFileName(){ return fileInfo.fileName(); } bool isDir(){ return fileInfo.isDir(); } bool isExecutable(){ return fileInfo.isExecutable(); } bool isLib(); int launch(); bool rename( const QString & name ); private: QString sizeString( unsigned int size ); QFileInfo fileInfo; + QPixmap FileItem::drawThumbnail(const QFileInfo &file); }; class FileView : public QListView { Q_OBJECT public: FileView( const QString & dir, QWidget * parent = 0, const char * name = 0, bool hidden = FALSE, - bool symlinks = FALSE ); + bool symlinks = FALSE, + bool thumbnails = FALSE ); void setDir( const QString & dir ); QString cd(){ return currentDir; } QStringList history() const { return dirHistory; } bool showingHidden; void setShowHidden(bool hidden); void setShowSymlinks(bool symlinks); + void setShowThumbnails(bool thumbnails); + bool getShowThumbnails () const { return showThumbnails; } void setMenuKeepsOpen(bool keepOpen); public slots: void updateDir(); void parentDir(); void lastDir(); void rename(); void copy(); void paste(); void del(); void cut(); void newFolder(); void viewAsText(); void chPerm(); protected: void generateDir( const QString & dir ); void resizeEvent( QResizeEvent* ); void contentsMousePressEvent( QMouseEvent * e ); void contentsMouseReleaseEvent( QMouseEvent * e ); protected slots: void itemClicked( QListViewItem * i ); void itemDblClicked( QListViewItem * i ); void showFileMenu(); void cancelMenuTimer(); void selectAll(){ QListView::selectAll( TRUE ); } void deselectAll(){ QListView::selectAll( FALSE ); } void addToDocuments(); void run(); void endRenaming(); - private: QString currentDir; QStringList dirHistory, flist; QTimer menuTimer; InlineEdit * le; FileItem * itemToRename; bool selected; bool showHidden; bool showSymlinks; + bool showThumbnails; bool menuKeepsOpen; bool copyFile( const QString & dest, const QString & src ); signals: void dirChanged(); void textViewActivated( QWidget * w ); void textViewDeactivated(); }; class FileBrowser : public QMainWindow { Q_OBJECT public: FileBrowser( QWidget * parent = 0, const char * name = 0, WFlags f = 0 ); FileBrowser( const QString & dir, QWidget * parent = 0, const char * name = 0, WFlags f = 0 ); public slots: void changeCaption(const QString &); private: void init(const QString & dir); QString fileToCopy; QPopupMenu * dirMenu, * sortMenu, *viewMenu; FileView * fileView; QAction * pasteAction; QAction *lastAction; QAction *upAction; bool copyFile( const QString & dest, const QString & src ); private slots: void pcmciaMessage( const QCString &msg, const QByteArray &); void sortName(); void sortDate(); void sortSize(); void sortType(); void updateSorting(); void updateShowHidden(); void updateShowSymlinks(); + void updateShowThumbnails(); void updateDirMenu(); void dirSelected( int id ); + void cleanUp(); + }; #endif |