lib

KoDirectoryStore.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, 2006 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KoDirectoryStore.h"
21#include <tqfile.h>
22#include <tqdir.h>
23#include <kdebug.h>
24
25// HMMM... I used TQFile and TQDir.... but maybe this should be made network transparent?
26
27KoDirectoryStore::KoDirectoryStore( const TQString& path, Mode _mode )
28 : m_basePath( path )
29{
30 const int pos = path.findRev( '/' );
31 // The parameter must include "maindoc.xml" or "content.xml"
32 if ( pos != -1 && pos != (int)m_basePath.length()-1 )
33 m_basePath = m_basePath.left( pos );
34 if ( !m_basePath.endsWith("/") )
35 m_basePath += '/';
36 m_currentPath = m_basePath;
37 kdDebug(s_area) << "KoDirectoryStore::KoDirectoryStore base path:" << m_basePath << endl;
38 m_bGood = init( _mode );
39}
40
41KoDirectoryStore::~KoDirectoryStore()
42{
43}
44
45bool KoDirectoryStore::init( Mode _mode )
46{
47 KoStore::init( _mode );
48 TQDir dir( m_basePath );
49 if ( dir.exists() )
50 return true;
51 dir = TQDir::current();
52 // Dir doesn't exist. If reading -> error. If writing -> create.
53 if ( _mode == Write && dir.mkdir( m_basePath ) ) {
54 kdDebug(s_area) << "KoDirectoryStore::init Directory created: " << m_basePath << endl;
55 return true;
56 }
57 return false;
58}
59
60bool KoDirectoryStore::openReadOrWrite( const TQString& name, int iomode )
61{
62 //kdDebug(s_area) << "KoDirectoryStore::openReadOrWrite m_currentPath=" << m_currentPath << " name=" << name << endl;
63 int pos = name.findRev('/');
64 if ( pos != -1 ) // there are subdirs in the name -> maybe need to create them, when writing
65 {
66 pushDirectory(); // remember where we were
67 enterAbsoluteDirectory( TQString() );
68 //kdDebug(s_area) << "KoDirectoryStore::openReadOrWrite entering " << name.left(pos) << endl;
69 bool ret = enterDirectory( name.left( pos ) );
70 popDirectory();
71 if ( !ret )
72 return false;
73 }
74 m_stream = new TQFile( m_basePath + name );
75 if ( !m_stream->open( iomode ) )
76 {
77 delete m_stream;
78 m_stream = 0L;
79 return false;
80 }
81 if ( iomode == IO_ReadOnly )
82 m_iSize = m_stream->size();
83 return true;
84}
85
86bool KoDirectoryStore::enterRelativeDirectory( const TQString& dirName )
87{
88 TQDir origDir( m_currentPath );
89 m_currentPath += dirName;
90 if ( !m_currentPath.endsWith("/") )
91 m_currentPath += '/';
92 //kdDebug(s_area) << "KoDirectoryStore::enterRelativeDirectory m_currentPath now " << m_currentPath << endl;
93 TQDir newDir( m_currentPath );
94 if ( newDir.exists() )
95 return true;
96 // Dir doesn't exist. If reading -> error. If writing -> create.
97 if ( mode() == Write && origDir.mkdir( dirName ) ) {
98 kdDebug(s_area) << "Created " << dirName << " under " << origDir.absPath() << endl;
99 return true;
100 }
101 return false;
102}
103
104bool KoDirectoryStore::enterAbsoluteDirectory( const TQString& path )
105{
106 m_currentPath = m_basePath + path;
107 //kdDebug(s_area) << "KoDirectoryStore::enterAbsoluteDirectory " << m_currentPath << endl;
108 TQDir newDir( m_currentPath );
109 Q_ASSERT( newDir.exists() ); // We've been there before, therefore it must exist.
110 return newDir.exists();
111}
112
113bool KoDirectoryStore::fileExists( const TQString& absPath ) const
114{
115 kdDebug(s_area) << "KoDirectoryStore::fileExists " << m_basePath+absPath << endl;
116 return TQFile::exists( m_basePath + absPath );
117}
virtual bool init(Mode mode)
Init store - called by constructor.
Definition KoStore.cpp:171
const char * name(StdAction id)