summaryrefslogtreecommitdiff
path: root/libopie2
authorar <ar>2004-08-14 19:17:39 (UTC)
committer ar <ar>2004-08-14 19:17:39 (UTC)
commitb7e50e77e0b22b3dd0feee43b15d0152d9ca8abb (patch) (side-by-side diff)
tree8b1812fe843ecde7b7b12f8697ca4cf8f7444c0b /libopie2
parent74363a9e1d5688d65286e7fea156227b68a28002 (diff)
downloadopie-b7e50e77e0b22b3dd0feee43b15d0152d9ca8abb.zip
opie-b7e50e77e0b22b3dd0feee43b15d0152d9ca8abb.tar.gz
opie-b7e50e77e0b22b3dd0feee43b15d0152d9ca8abb.tar.bz2
- add OFileDialog::getDirectory()
this function open a file dialog to select a directory. it can show Directories and All Directories. the first one is without and the second is with hidden directories.
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opieui/fileselector/ofiledialog.cpp35
-rw-r--r--libopie2/opieui/fileselector/ofiledialog.h5
-rw-r--r--libopie2/opieui/fileselector/ofileselector.cpp66
-rw-r--r--libopie2/opieui/fileselector/ofileselector.h3
4 files changed, 89 insertions, 20 deletions
diff --git a/libopie2/opieui/fileselector/ofiledialog.cpp b/libopie2/opieui/fileselector/ofiledialog.cpp
index beb4d6c..ebce0ef 100644
--- a/libopie2/opieui/fileselector/ofiledialog.cpp
+++ b/libopie2/opieui/fileselector/ofiledialog.cpp
@@ -25,97 +25,100 @@
Boston, MA 02111-1307, USA.
*/
/* OPIE */
#include <opie2/ofiledialog.h>
#include <qpe/applnk.h>
#include <qpe/config.h>
#include <qpe/qpeapplication.h>
/* QT */
#include <qfileinfo.h>
#include <qstring.h>
#include <qapplication.h>
#include <qlayout.h>
using namespace Opie::Ui;
namespace
{
/*
* helper functions to load the start dir
* and to save it
* helper to extract the dir out of a file name
*/
/**
* This method will use Config( argv[0] );
* @param key The group key used
*/
QString lastUsedDir( const QString& key )
{
if ( qApp->argc() < 1 )
return QString::null;
Config cfg( QFileInfo(qApp->argv()[0]).fileName() ); // appname
cfg.setGroup( key );
return cfg.readEntry("LastDir", QPEApplication::documentDir() );
}
void saveLastDir( const QString& key, const QString& file )
{
if ( qApp->argc() < 1 )
return;
Config cfg( QFileInfo(qApp->argv()[0]).fileName() );
cfg.setGroup( key );
QFileInfo inf( file );
- cfg.writeEntry("LastDir", inf.dirPath( true ) );
+ if ( inf.isFile() )
+ cfg.writeEntry("LastDir", inf.dirPath( true ) );
+ else
+ cfg.writeEntry("LastDir", file );
}
};
/**
* This constructs a modal dialog
*
* @param caption The caption of the dialog
* @param wid The parent widget
* @param mode The mode of the OFileSelector @see OFileSelector
* @param selector The selector of the OFileSelector
* @param dirName the dir or resource to start from
* @param fileName a proposed or existing filename
* @param mimetypes The mimeTypes
*/
OFileDialog::OFileDialog(const QString &caption,
QWidget *wid, int mode, int selector,
const QString &dirName,
const QString &fileName,
const QMap<QString,QStringList>& mimetypes )
: QDialog( wid, "OFileDialog", true )
{
// QVBoxLayout *lay = new QVBoxLayout(this);
//showMaximized();
QVBoxLayout *lay = new QVBoxLayout(this );
file = new OFileSelector(this , mode, selector,
dirName, fileName,
mimetypes );
lay->addWidget( file );
//lay->addWidget( file );
//showFullScreen();
setCaption( caption.isEmpty() ? tr("FileDialog") : caption );
connect(file, SIGNAL(fileSelected(const QString&) ),
this, SLOT(slotFileSelected(const QString&) ) );
connect(file, SIGNAL(ok() ),
this, SLOT(slotSelectorOk()) ) ;
connect(file, SIGNAL(dirSelected(const QString&) ), this, SLOT(slotDirSelected(const QString&) ) );
#if 0
connect(file, SIGNAL(dirSelected(const QString&) ),
this, SLOT(slotDirSelected(const QString&) ) );
#endif
}
/**
* @returns the mimetype of the selected
* currently it return QString::null
*/
@@ -159,64 +162,94 @@ QString OFileDialog::getOpenFileName(int selector,
const QString &caption )
{
QString ret;
QString startDir = _startDir;
if (startDir.isEmpty() )
startDir = lastUsedDir( "FileDialog-OPEN" );
OFileDialog dlg( caption.isEmpty() ? tr("Open") : caption,
wid, OFileSelector::Open, selector, startDir, file, mimes);
dlg.showMaximized();
if( dlg.exec() )
{
ret = dlg.fileName();
saveLastDir( "FileDialog-OPEN", ret );
}
return ret;
}
/**
* This opens up a file dialog in save mode
* @see getOpenFileName
*/
QString OFileDialog::getSaveFileName(int selector,
const QString &_startDir,
const QString &file,
const MimeTypes &mimes,
QWidget *wid,
const QString &caption )
{
QString ret;
QString startDir = _startDir;
if (startDir.isEmpty() )
startDir = lastUsedDir( "FileDialog-SAVE" );
OFileDialog dlg( caption.isEmpty() ? tr("Save") : caption,
wid, OFileSelector::Save, selector, startDir, file, mimes);
dlg.showMaximized();
if( dlg.exec() )
{
ret = dlg.fileName();
saveLastDir( "FileDialog-SAVE", ret );
}
return ret;
}
+/**
+ * This opens up a filedialog in select directory mode
+ *
+ * @param selector the Selector Mode
+ * @param startDir Where to start from
+ * @param wid the parent
+ * @param caption of the dialog if QString::null tr("Open") will be used
+ * @return the directoryName or QString::null
+ */
+QString OFileDialog::getDirectory(int selector,
+ const QString &_startDir,
+ QWidget *wid,
+ const QString &caption )
+{
+ QString ret;
+ QString startDir = _startDir;
+ if ( startDir.isEmpty() )
+ startDir = lastUsedDir( "FileDialog-SELECTDIR" );
+
+ OFileDialog dlg( caption.isEmpty() ? tr( "Select Directory" ) : caption,
+ wid, OFileSelector::DirectorySelector, selector, startDir );
+ dlg.showMaximized();
+ if ( dlg.exec() )
+ {
+ ret = dlg.fileName();
+ saveLastDir( "FileDialog-SELECTDIR", ret );
+ }
+ return ret;
+}
+
void OFileDialog::slotFileSelected(const QString & )
{
accept();
}
void OFileDialog::slotSelectorOk( )
{
accept();
}
void OFileDialog::slotDirSelected(const QString &dir )
{
setCaption( dir );
// if mode
//accept();
}
diff --git a/libopie2/opieui/fileselector/ofiledialog.h b/libopie2/opieui/fileselector/ofiledialog.h
index dfecf3d..569f45c 100644
--- a/libopie2/opieui/fileselector/ofiledialog.h
+++ b/libopie2/opieui/fileselector/ofiledialog.h
@@ -44,67 +44,72 @@ namespace Ui {
* a file for either opening or saving.
* Most of the time the c'tor will not be used instead using
* the static member functions is prefered.
*
* <pre>
* QMap<QString, QStringList> mimeTypes;
* QStringList types;
* types << "text[slash]* ";
* mimeTypes.insert( tr("Text"), types );
* mimeTypes.insert( tr("All"), " * / * " ); // remove the spaces in the 2nd comment
* QString fileName= OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
* "foo","bar", mimeTypes);
* </pre>
*
* @short A small QDialog swalloing a FileSelector
* @see QDialog
* @see OFileSelector
* @version 0.1-unfinished
* @author Holger Freyther ( zecke@handhelds.org )
*/
class OFileDialog : public QDialog
{
Q_OBJECT
public:
OFileDialog(const QString &caption,
QWidget *, int mode, int selector,
const QString &dirName,
const QString &fileName = QString::null,
const MimeTypes &mimetypes = MimeTypes() );
QString mimetype() const;
QString fileName() const;
DocLnk selectedDocument()const;
// static methods
static QString getOpenFileName(int selector,
const QString& startDir = QString::null,
const QString &fileName = QString::null,
const MimeTypes& mime = MimeTypes(),
QWidget *wid = 0,
const QString &caption = QString::null );
static QString getSaveFileName(int selector,
const QString& startDir = QString::null,
const QString& fileName = QString::null,
const MimeTypes& mimefilter = MimeTypes(),
QWidget *wid = 0,
const QString &caption = QString::null );
+ static QString getDirectory(int selector,
+ const QString &startDir = QString::null,
+ QWidget *wid = 0,
+ const QString &caption = QString::null );
+
//let's OFileSelector catch up first
//static QString getExistingDirectory(const QString& startDir = QString::null,
//QWidget *parent = 0, const QString& caption = QString::null );
private:
class OFileDialogPrivate;
OFileDialogPrivate *d;
OFileSelector *file;
private slots:
void slotFileSelected( const QString & );
void slotDirSelected(const QString & );
void slotSelectorOk();
};
}
}
#endif
diff --git a/libopie2/opieui/fileselector/ofileselector.cpp b/libopie2/opieui/fileselector/ofileselector.cpp
index b06defd..346eeae 100644
--- a/libopie2/opieui/fileselector/ofileselector.cpp
+++ b/libopie2/opieui/fileselector/ofileselector.cpp
@@ -383,100 +383,103 @@ OFileViewFileListView::OFileViewFileListView( QWidget* parent, const QString& st
m_view->setAllColumnsShowFocus( TRUE );
lay->addWidget( m_view, 1000 );
connectSlots();
}
OFileViewFileListView::~OFileViewFileListView()
{
}
void OFileViewFileListView::slotNew()
{
DocLnk lnk;
emit selector()->newSelected( lnk );
}
OFileSelectorItem* OFileViewFileListView::currentItem()const
{
QListViewItem* item = m_view->currentItem();
if (!item )
return 0l;
return static_cast<OFileSelectorItem*>(item);
}
void OFileViewFileListView::reread( bool all )
{
m_view->clear();
if (selector()->showClose() )
m_btnClose->show();
else
m_btnClose->hide();
if (selector()->showNew() )
m_btnNew->show();
else
m_btnNew->hide();
m_mimes = selector()->currentMimeType();
m_all = all;
QDir dir( m_currentDir );
if (!dir.exists() )
return;
dir.setSorting( QDir::Name | QDir::DirsFirst | QDir::Reversed );
int filter;
- if (m_all )
- filter = QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All;
- else
- filter = QDir::Files | QDir::Dirs | QDir::All;
+ filter = QDir::Dirs;
+ if ( selector()->mode() != OFileSelector::DIRECTORYSELECTOR )
+ filter = filter | QDir::Files | QDir::All;
+
+ if ( m_all )
+ filter = filter | QDir::Hidden;
+
dir.setFilter( filter );
// now go through all files
const QFileInfoList *list = dir.entryInfoList();
if (!list)
{
cdUP();
return;
}
QFileInfoListIterator it( *list );
QFileInfo *fi;
while( (fi=it.current() ) )
{
if( fi->fileName() == QString::fromLatin1("..") || fi->fileName() == QString::fromLatin1(".") )
{
++it;
continue;
}
/*
* It is a symlink we try to resolve it now but don't let us attack by DOS
*
*/
if( fi->isSymLink() )
{
QString file = fi->dirPath( true ) + "/" + fi->readLink();
for( int i = 0; i<=4; i++)
{ // 5 tries to prevent dos
QFileInfo info( file );
if( !info.exists() )
{
addSymlink( fi, TRUE );
break;
}
else if( info.isDir() )
{
addDir( fi, TRUE );
break;
}
else if( info.isFile() )
{
addFile( fi, TRUE );
break;
}
else if( info.isSymLink() )
{
file = info.dirPath(true ) + "/" + info.readLink() ;
@@ -769,166 +772,181 @@ QString OFileViewFileSystem::selectedName()const
QString cFN=currentFileName();
if (cFN.startsWith("/")) return cFN;
return m_view->currentDir() + "/" + cFN;
}
QString OFileViewFileSystem::selectedPath()const
{
return QString::null;
}
QString OFileViewFileSystem::directory()const
{
if (!m_view)
return QString::null;
OFileSelectorItem* item = m_view->currentItem();
if (!item )
return QString::null;
return QDir(item->directory() ).absPath();
}
void OFileViewFileSystem::reread()
{
if (!m_view)
return;
m_view->reread( m_all );
}
int OFileViewFileSystem::fileCount()const
{
if (!m_view )
return -1;
return m_view->fileCount();
}
QWidget* OFileViewFileSystem::widget( QWidget* parent )
{
if (!m_view )
{
m_view = new OFileViewFileListView( parent, startDirectory(), selector() );
}
return m_view;
}
void OFileViewFileSystem::activate( const QString& str)
{
- m_all = (str != QObject::tr("Files") );
+ m_all = ( str.find( "All" ) != -1 );
}
}
/* Selector */
/**
* @short new and complete c'tor
*
* Create a OFileSelector to let the user select a file. It can
* either be used to open a file, select a save name in a dir or
* as a dropin for the FileSelector.
*
* <pre>
* QMap<QString, QStringList> mimeTypes;
* QStringList types;
* types << "text@slash* ";
* types << "audio@slash*";
* mimeTypes.insert( tr("Audio and Text"), types );
* mimeTypes.insert( tr("All"), "*@slash*);
*
* now you could create your fileselector
* </pre>
*
*
* @param parent the parent of this widget
* @param mode The mode from the enum Mode (Open,Save,FILESELECTOR)
* @param sel The selector to be used
* @param dirName The name of the dir to start int
* @param fileName The fileName placed in the fileselector lineedit
* @param mimetypes The MimeType map of used mimetypes
* @param showNew Show a New Button. Most likely to be used in the FileSelector view.
* @param showClose Show a Close Button. Most likely to be used in FileSelector view.
*
*/
OFileSelector::OFileSelector( QWidget* parent, int mode, int sel,
const QString& dirName, const QString& fileName,
const MimeTypes& mimetypes,
bool showNew, bool showClose)
:QWidget( parent, "OFileSelector" )
{
m_current = 0;
m_shNew = showNew;
m_shClose = showClose;
m_mimeType = mimetypes;
m_startDir = dirName;
m_mode = mode;
m_selector = sel;
initUI();
m_lneEdit->setText( fileName );
initMime();
initViews();
QString str;
switch ( m_selector )
{
default:
case Normal:
- str = QObject::tr("Documents");
+ if ( m_mode == DIRECTORYSELECTOR )
+ str = QObject::tr("Directories");
+ else
+ str = QObject::tr("Documents");
m_cmbView->setCurrentItem( 0 );
break;
case Extended:
- str = QObject::tr("Files");
- m_cmbView->setCurrentItem( 1 );
+ if ( m_mode == DIRECTORYSELECTOR )
+ {
+ str = QObject::tr("Directories");
+ m_cmbView->setCurrentItem( 0 );
+ } else {
+ str = QObject::tr("Files");
+ m_cmbView->setCurrentItem( 1 );
+ }
break;
case ExtendedAll:
- str = QObject::tr("All Files");
- m_cmbView->setCurrentItem( 2 );
+ if ( m_mode == DIRECTORYSELECTOR )
+ {
+ str = QObject::tr("All Directories");
+ m_cmbView->setCurrentItem( 1 );
+ } else {
+ str = QObject::tr("All Files");
+ m_cmbView->setCurrentItem( 2 );
+ }
break;
}
slotViewChange( str );
}
/**
* This a convience c'tor to just substitute the use of FileSelector
*/
OFileSelector::OFileSelector( const QString& mimeFilter, QWidget* parent, const char* name,
bool showNew, bool showClose )
: QWidget( parent, name )
{
m_current = 0;
m_shNew = showNew;
m_shClose = showClose;
m_startDir = QPEApplication::documentDir();
if (!mimeFilter.isEmpty() )
m_mimeType.insert(mimeFilter, QStringList::split(";", mimeFilter ) );
m_mode = OFileSelector::FileSelector;
m_selector = OFileSelector::Normal;
initUI();
initMime();
initViews();
m_cmbView->setCurrentItem( 0 );
slotViewChange( QObject::tr("Documents") );
}
/*
* INIT UI will set up the basic GUI
* Layout: Simple VBoxLayout
* On top a WidgetStack containing the Views...
* - List View
* - Document View
* Below we will have a Label + LineEdit
* Below we will have two ComoBoxes one for choosing the view one for
* choosing the mimetype
*/
void OFileSelector::initUI()
{
QVBoxLayout* lay = new QVBoxLayout( this );
m_stack = new QWidgetStack( this );
lay->addWidget( m_stack, 1000 );
@@ -940,109 +958,121 @@ void OFileSelector::initUI()
lay->addWidget( m_nameBox );
m_cmbBox = new QHBox( this );
m_cmbView = new QComboBox( m_cmbBox );
m_cmbMime = new QComboBox( m_cmbBox );
lay->addWidget( m_cmbBox );
}
/*
* This will make sure that the return key in the name edit causes dialogs to close
*/
bool OFileSelector::eventFilter (QObject *, QEvent *e)
{
if ( e->type() == QEvent::KeyPress )
{
QKeyEvent *k = (QKeyEvent *)e;
if ( (k->key()==Key_Enter) || (k->key()==Key_Return))
{
emit ok();
return true;
}
}
return false;
}
/*
* This will insert the MimeTypes into the Combo Box
* And also connect the changed signal
*
* AutoMimeTyping is disabled for now. It used to reparse a dir and then set available mimetypes
*/
void OFileSelector::initMime()
{
MimeTypes::Iterator it;
for ( it = m_mimeType.begin(); it != m_mimeType.end(); ++it )
{
m_cmbMime->insertItem( it.key() );
}
m_cmbMime->setCurrentItem( 0 );
connect( m_cmbMime, SIGNAL(activated(int) ),
this, SLOT(slotMimeTypeChanged() ) );
}
void OFileSelector::initViews()
{
- m_cmbView->insertItem( QObject::tr("Documents") );
- m_cmbView->insertItem( QObject::tr("Files") );
- m_cmbView->insertItem( QObject::tr("All Files") );
+ if ( m_mode == OFileSelector::DIRECTORYSELECTOR )
+ {
+ m_cmbView->insertItem( QObject::tr("Directories") );
+ m_cmbView->insertItem( QObject::tr("All Directories") );
+ } else {
+ m_cmbView->insertItem( QObject::tr("Documents") );
+ m_cmbView->insertItem( QObject::tr("Files") );
+ m_cmbView->insertItem( QObject::tr("All Files") );
+ }
+
connect(m_cmbView, SIGNAL(activated(const QString&) ),
this, SLOT(slotViewChange(const QString&) ) );
-
- m_views.insert( QObject::tr("Documents"), new ODocumentFileView(this) );
-
/* see above why add both */
OFileViewInterface* in = new OFileViewFileSystem( this );
- m_views.insert( QObject::tr("Files"), in );
- m_views.insert( QObject::tr("All Files"), in );
+
+ if ( m_mode == OFileSelector::DIRECTORYSELECTOR )
+ {
+ m_views.insert( QObject::tr("Directories"), in );
+ m_views.insert( QObject::tr("All Directories"), in );
+ } else {
+ m_views.insert( QObject::tr("Documents"), new ODocumentFileView(this) );
+ m_views.insert( QObject::tr("Files"), in );
+ m_views.insert( QObject::tr("All Files"), in );
+ }
}
void OFileSelector::registerView( const Internal::OFileViewInterface* iface ) {
m_viewsPtr.append( iface );
}
/**
* d'tor
*/
OFileSelector::~OFileSelector()
{
m_viewsPtr.setAutoDelete( true );
m_viewsPtr.clear();
}
/**
* Convience function for the fileselector
* make sure to delete the DocLnk
*
* @see DocLnk
* @todo remove in ODP
*/
const DocLnk* OFileSelector::selected()
{
DocLnk* lnk = new DocLnk( currentView()->selectedDocument() );
return lnk;
}
/**
*
* @return the name of the selected file
*/
QString OFileSelector::selectedName()const
{
return currentView()->selectedName();
}
/**
* @return the selected path
*/
QString OFileSelector::selectedPath()const
{
return currentView()->selectedPath();
}
diff --git a/libopie2/opieui/fileselector/ofileselector.h b/libopie2/opieui/fileselector/ofileselector.h
index de2b98a..b1cd405 100644
--- a/libopie2/opieui/fileselector/ofileselector.h
+++ b/libopie2/opieui/fileselector/ofileselector.h
@@ -40,98 +40,99 @@
/* QT */
#include <qlist.h>
#include <qwidget.h>
#include <qmap.h>
#include <qvaluelist.h>
#include <qstringlist.h>
#include <qlist.h>
class QLineEdit;
class QComboBox;
class QWidgetStack;
class QHBox;
typedef QMap<QString, QStringList> MimeTypes;
namespace Opie {
namespace Ui {
namespace Internal {
class OFileViewInterface;
class OFileViewFileListView;
}
/**
* @short a dropin replacement for the FileSelector
*
* This class is first used insert the OFileDialog.
* It supports multiple view and mimetype filtering for now.
*
* @see OFileDialog
* @see FileSelector
* @author zecke
* @version 0.1
*/
class OFileSelector : public QWidget
{
Q_OBJECT
friend class Internal::OFileViewInterface;
friend class Internal::OFileViewFileListView;
public:
/**
* The Mode of the Fileselector
* Open = Open A File
* Save = Save a File
* FILESELECTOR = As A GUI in a screen to select a file
+ * SelectDir = Select a Directory
*/
- enum Mode { Open=1, Save=2, FileSelector=4, OPEN=1, SAVE=2, FILESELECTOR=4 };
+ enum Mode { Open = 1, Save = 2, DirectorySelector = 3, FileSelector = 4, OPEN = 1, SAVE = 2, DIRECTORYSELECTOR = 3, FILESELECTOR = 4 };
// enum OldMode { OPEN=1, SAVE=2, FILESELECTOR = 4 };
/**
* Normal = The old FileSelector
* Extended = Dir View
* ExtendedAll = Dir View with all hidden files
* Default = What the vendor considers best
*/
enum Selector { Normal = 0, Extended=1, ExtendedAll =2, Default=3, NORMAL=0,EXTENDED=1, EXTENDED_ALL =2, DEFAULT=3 };
// enum OldSelector { NORMAL = 0, EXTENDED =1, EXTENDED_ALL = 2};
OFileSelector(QWidget* parent, int mode, int selector,
const QString& dirName,
const QString& fileName,
const MimeTypes& mimetypes = MimeTypes(),
bool newVisible = FALSE, bool closeVisible = FALSE );
OFileSelector(const QString& mimeFilter, QWidget* parent,
const char* name = 0, bool newVisible = TRUE, bool closeVisible = FALSE );
~OFileSelector();
const DocLnk* selected();
QString selectedName()const;
QString selectedPath()const;
QString directory()const;
DocLnk selectedDocument()const;
int fileCount()const;
void reread();
int mode()const;
int selector()const;
/**
* Set the Icon visible
* @param b Show or Hide the New Button
*/
void setNewVisible( bool b );
/**
* Set the Icon visible
*/
void setCloseVisible( bool b );
/**
* Set the Name Line visible
*/