00001 /* This file is part of the KDE project 00002 Copyright (C) 2002 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "KoDirectoryStore.h" 00021 #include <QFile> 00022 #include <QDir> 00023 #include <kdebug.h> 00024 00025 // HMMM... I used QFile and QDir.... but maybe this should be made network transparent? 00026 00027 KoDirectoryStore::KoDirectoryStore( const QString& path, Mode _mode ) 00028 : m_basePath( path ) 00029 { 00030 // The parameter must include "maindoc.xml" 00031 int pos = m_basePath.lastIndexOf( '/' ); 00032 if ( pos != -1 && pos != m_basePath.length()-1 ) 00033 m_basePath = m_basePath.left( pos ); 00034 if ( !m_basePath.endsWith("/") ) 00035 m_basePath += '/'; 00036 //if ( !m_basePath.startsWith("/") ) 00037 // m_basePath.prepend( QDir::currentPath() + '/' ); 00038 m_currentPath = m_basePath; 00039 kDebug(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 // Dir doesn't exist. If reading -> error. If writing -> create. 00055 if ( _mode == Write && dir.mkdir( m_basePath ) ) { 00056 kDebug(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, QIODevice::OpenModeFlag iomode ) 00063 { 00064 //kDebug(s_area) << "KoDirectoryStore::openReadOrWrite m_currentPath=" << m_currentPath << " name=" << name << endl; 00065 int pos = name.lastIndexOf('/'); 00066 if ( pos != -1 ) // there are subdirs in the name -> maybe need to create them, when writing 00067 { 00068 pushDirectory(); // remember where we were 00069 enterAbsoluteDirectory( QString::null ); 00070 //kDebug(s_area) << "KoDirectoryStore::openReadOrWrite entering " << name.left(pos) << endl; 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 == QIODevice::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 //kDebug(s_area) << "KoDirectoryStore::enterRelativeDirectory m_currentPath now " << m_currentPath << endl; 00095 QDir newDir( m_currentPath ); 00096 if ( newDir.exists() ) 00097 return true; 00098 // Dir doesn't exist. If reading -> error. If writing -> create. 00099 if ( mode() == Write && origDir.mkdir( dirName ) ) { 00100 kDebug(s_area) << "Created " << dirName << " under " << origDir.absolutePath() << 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 //kDebug(s_area) << "KoDirectoryStore::enterAbsoluteDirectory " << m_currentPath << endl; 00110 QDir newDir( m_currentPath ); 00111 Q_ASSERT( newDir.exists() ); // We've been there before, therefore it must exist. 00112 return newDir.exists(); 00113 } 00114 00115 bool KoDirectoryStore::fileExists( const QString& absPath ) const 00116 { 00117 kDebug(s_area) << "KoDirectoryStore::fileExists " << m_basePath+absPath << endl; 00118 return QFile::exists( m_basePath + absPath ); 00119 }