lib

KoDirectoryStore.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, 2006 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.h>
00022 #include <qdir.h>
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     const int pos = path.findRev( '/' );
00031     // The parameter must include "maindoc.xml" or "content.xml"
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     // Dir doesn't exist. If reading -> error. If writing -> create.
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     //kdDebug(s_area) << "KoDirectoryStore::openReadOrWrite m_currentPath=" << m_currentPath << " name=" << name << endl;
00063     int pos = name.findRev('/');
00064     if ( pos != -1 ) // there are subdirs in the name -> maybe need to create them, when writing
00065     {
00066         pushDirectory(); // remember where we were
00067         enterAbsoluteDirectory( QString::null );
00068         //kdDebug(s_area) << "KoDirectoryStore::openReadOrWrite entering " << name.left(pos) << endl;
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     //kdDebug(s_area) << "KoDirectoryStore::enterRelativeDirectory m_currentPath now " << m_currentPath << endl;
00093     QDir newDir( m_currentPath );
00094     if ( newDir.exists() )
00095         return true;
00096     // Dir doesn't exist. If reading -> error. If writing -> create.
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     //kdDebug(s_area) << "KoDirectoryStore::enterAbsoluteDirectory " << m_currentPath << endl;
00108     QDir newDir( m_currentPath );
00109     Q_ASSERT( newDir.exists() ); // We've been there before, therefore it must exist.
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys