00001
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
00031
int pos = m_basePath.findRev(
'/' );
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
00037
00038 m_currentPath = m_basePath;
00039 kdDebug(s_area) <<
"KoDirectoryStore::KoDirectoryStore base path:" << m_basePath << endl;
00040 m_bGood = init( _mode );
00041 }
00042
00043 KoDirectoryStore::~KoDirectoryStore()
00044 {
00045 }
00046
00047
bool KoDirectoryStore::init( Mode _mode )
00048 {
00049
KoStore::init( _mode );
00050
QDir dir( m_basePath );
00051
if ( dir.exists() )
00052
return true;
00053 dir = QDir::current();
00054
00055
if ( _mode == Write && dir.mkdir( m_basePath ) ) {
00056 kdDebug(s_area) <<
"KoDirectoryStore::init Directory created: " << m_basePath << endl;
00057
return true;
00058 }
00059
return false;
00060 }
00061
00062
bool KoDirectoryStore::openReadOrWrite(
const QString& name,
int iomode )
00063 {
00064
00065
int pos = name.findRev(
'/');
00066
if ( pos != -1 )
00067 {
00068 pushDirectory();
00069 enterAbsoluteDirectory( QString::null );
00070
00071
bool ret = enterDirectory( name.left( pos ) );
00072 popDirectory();
00073
if ( !ret )
00074
return false;
00075 }
00076 m_stream =
new QFile( m_basePath + name );
00077
if ( !m_stream->open( iomode ) )
00078 {
00079
delete m_stream;
00080 m_stream = 0L;
00081
return false;
00082 }
00083
if ( iomode == IO_ReadOnly )
00084 m_iSize = m_stream->size();
00085
return true;
00086 }
00087
00088
bool KoDirectoryStore::enterRelativeDirectory(
const QString& dirName )
00089 {
00090
QDir origDir( m_currentPath );
00091 m_currentPath += dirName;
00092
if ( !m_currentPath.endsWith(
"/") )
00093 m_currentPath +=
'/';
00094
00095
QDir newDir( m_currentPath );
00096
if ( newDir.exists() )
00097
return true;
00098
00099
if ( mode() == Write && origDir.mkdir( dirName ) ) {
00100 kdDebug(s_area) <<
"Created " << dirName <<
" under " << origDir.absPath() << endl;
00101
return true;
00102 }
00103
return false;
00104 }
00105
00106
bool KoDirectoryStore::enterAbsoluteDirectory(
const QString& path )
00107 {
00108 m_currentPath = m_basePath + path;
00109
00110
QDir newDir( m_currentPath );
00111 Q_ASSERT( newDir.exists() );
00112
return newDir.exists();
00113 }
00114
00115
bool KoDirectoryStore::fileExists(
const QString& absPath )
00116 {
00117 kdDebug(s_area) <<
"KoDirectoryStore::fileExists " << m_basePath+absPath << endl;
00118
return QFile::exists( m_basePath + absPath );
00119 }