lib
KoDirectoryStore.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoDirectoryStore.h"
00021 #include <qfile.h>
00022 #include <qdir.h>
00023 #include <kdebug.h>
00024
00025
00026
00027 KoDirectoryStore::KoDirectoryStore( const QString& path, Mode _mode )
00028 : m_basePath( path )
00029 {
00030 const int pos = path.findRev( '/' );
00031
00032 if ( pos != -1 && pos != (int)m_basePath.length()-1 )
00033 m_basePath = m_basePath.left( pos );
00034 if ( !m_basePath.endsWith("/") )
00035 m_basePath += '/';
00036 m_currentPath = m_basePath;
00037 kdDebug(s_area) << "KoDirectoryStore::KoDirectoryStore base path:" << m_basePath << endl;
00038 m_bGood = init( _mode );
00039 }
00040
00041 KoDirectoryStore::~KoDirectoryStore()
00042 {
00043 }
00044
00045 bool KoDirectoryStore::init( Mode _mode )
00046 {
00047 KoStore::init( _mode );
00048 QDir dir( m_basePath );
00049 if ( dir.exists() )
00050 return true;
00051 dir = QDir::current();
00052
00053 if ( _mode == Write && dir.mkdir( m_basePath ) ) {
00054 kdDebug(s_area) << "KoDirectoryStore::init Directory created: " << m_basePath << endl;
00055 return true;
00056 }
00057 return false;
00058 }
00059
00060 bool KoDirectoryStore::openReadOrWrite( const QString& name, int iomode )
00061 {
00062
00063 int pos = name.findRev('/');
00064 if ( pos != -1 )
00065 {
00066 pushDirectory();
00067 enterAbsoluteDirectory( QString::null );
00068
00069 bool ret = enterDirectory( name.left( pos ) );
00070 popDirectory();
00071 if ( !ret )
00072 return false;
00073 }
00074 m_stream = new QFile( m_basePath + name );
00075 if ( !m_stream->open( iomode ) )
00076 {
00077 delete m_stream;
00078 m_stream = 0L;
00079 return false;
00080 }
00081 if ( iomode == IO_ReadOnly )
00082 m_iSize = m_stream->size();
00083 return true;
00084 }
00085
00086 bool KoDirectoryStore::enterRelativeDirectory( const QString& dirName )
00087 {
00088 QDir origDir( m_currentPath );
00089 m_currentPath += dirName;
00090 if ( !m_currentPath.endsWith("/") )
00091 m_currentPath += '/';
00092
00093 QDir newDir( m_currentPath );
00094 if ( newDir.exists() )
00095 return true;
00096
00097 if ( mode() == Write && origDir.mkdir( dirName ) ) {
00098 kdDebug(s_area) << "Created " << dirName << " under " << origDir.absPath() << endl;
00099 return true;
00100 }
00101 return false;
00102 }
00103
00104 bool KoDirectoryStore::enterAbsoluteDirectory( const QString& path )
00105 {
00106 m_currentPath = m_basePath + path;
00107
00108 QDir newDir( m_currentPath );
00109 Q_ASSERT( newDir.exists() );
00110 return newDir.exists();
00111 }
00112
00113 bool KoDirectoryStore::fileExists( const QString& absPath ) const
00114 {
00115 kdDebug(s_area) << "KoDirectoryStore::fileExists " << m_basePath+absPath << endl;
00116 return QFile::exists( m_basePath + absPath );
00117 }
|