From 3413da9f84485deaf6416fe6cf9a9a48adb4e0c1 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Mon, 28 Dec 2020 18:47:21 +0900 Subject: Renaming of files in preparation for code style tools. Signed-off-by: Michele Calgaro --- src/projects/kostore/CMakeLists.txt | 2 +- src/projects/kostore/Makefile.am | 2 +- src/projects/kostore/koStore.cc | 628 ----------------------------------- src/projects/kostore/koStore.cpp | 628 +++++++++++++++++++++++++++++++++++ src/projects/kostore/koStoreBase.cc | 29 -- src/projects/kostore/koStoreBase.cpp | 29 ++ src/projects/kostore/koZipStore.cc | 237 ------------- src/projects/kostore/koZipStore.cpp | 237 +++++++++++++ 8 files changed, 896 insertions(+), 896 deletions(-) delete mode 100644 src/projects/kostore/koStore.cc create mode 100644 src/projects/kostore/koStore.cpp delete mode 100644 src/projects/kostore/koStoreBase.cc create mode 100644 src/projects/kostore/koStoreBase.cpp delete mode 100644 src/projects/kostore/koZipStore.cc create mode 100644 src/projects/kostore/koZipStore.cpp diff --git a/src/projects/kostore/CMakeLists.txt b/src/projects/kostore/CMakeLists.txt index 9ac7377..bc35875 100644 --- a/src/projects/kostore/CMakeLists.txt +++ b/src/projects/kostore/CMakeLists.txt @@ -16,5 +16,5 @@ link_directories( ##### kostore (static) ########################## tde_add_library( kostore STATIC_PIC AUTOMOC - SOURCES koStore.cc koZipStore.cc koStoreBase.cc + SOURCES koStore.cpp koZipStore.cpp koStoreBase.cpp ) diff --git a/src/projects/kostore/Makefile.am b/src/projects/kostore/Makefile.am index 65de5e7..e87beeb 100644 --- a/src/projects/kostore/Makefile.am +++ b/src/projects/kostore/Makefile.am @@ -6,6 +6,6 @@ INCLUDES = $(all_includes) ####### Files libkostore_la_LIBADD = $(LIB_TDEIO) -libkostore_la_SOURCES = koStore.cc koZipStore.cc koStoreBase.cc +libkostore_la_SOURCES = koStore.cpp koZipStore.cpp koStoreBase.cpp #libkostore_la_LDFLAGS = $(all_libraries) -version-info 3:0:0 $(KDE_LDFLAGS) -no-undefined #include_HEADERS = koStore.h diff --git a/src/projects/kostore/koStore.cc b/src/projects/kostore/koStore.cc deleted file mode 100644 index 24fc737..0000000 --- a/src/projects/kostore/koStore.cc +++ /dev/null @@ -1,628 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 1998, 1999 Torben Weis - Copyright (C) 2000-2002 David Faure , Werner Trobin - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include -#include -#include - -#include "koStore.h" -//#include "koTarStore.h" -#include "koZipStore.h" -//#include "koDirectoryStore.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -//#define DefaultFormat KoStore::Tar -#define DefaultFormat KoStore::Zip - -const int KoStore::s_area = 30002; - -KoStore::Backend KoStore::determineBackend( TQIODevice* dev ) -{ - unsigned char buf[5]; - if ( dev->readBlock( (char *)buf, 4 ) < 4 ) - return DefaultFormat; // will create a "bad" store (bad()==true) - if ( buf[0] == 0037 && buf[1] == 0213 ) // gzip -> tar.gz - return Tar; - if ( buf[0] == 'P' && buf[1] == 'K' && buf[2] == 3 && buf[3] == 4 ) - return Zip; - return DefaultFormat; // fallback -} - -KoStore* KoStore::createStore( const TQString& fileName, Mode mode, const TQCString & appIdentification, Backend backend ) -{ - if ( backend == Auto ) { - if ( mode == KoStore::Write ) - backend = DefaultFormat; - else - { - TQFileInfo inf( fileName ); - if ( inf.isDir() ) - backend = Directory; - else - { - TQFile file( fileName ); - if ( file.open( IO_ReadOnly ) ) - backend = determineBackend( TQT_TQIODEVICE(&file) ); - else - backend = DefaultFormat; // will create a "bad" store (bad()==true) - } - } - } - switch ( backend ) - { -// case Tar: -// return new KoTarStore( fileName, mode, appIdentification ); - case Zip: - return new KoZipStore( fileName, mode, appIdentification ); -// case Directory: -// return new KoDirectoryStore( fileName /* should be a dir name.... */, mode ); - default: - kdWarning(s_area) << "Unsupported backend requested for KoStore : " << backend << endl; - return 0L; - } -} - -KoStore* KoStore::createStore( TQIODevice *device, Mode mode, const TQCString & appIdentification, Backend backend ) -{ - if ( backend == Auto ) - { - if ( mode == KoStore::Write ) - backend = DefaultFormat; - else { - if ( device->open( IO_ReadOnly ) ) { - backend = determineBackend( device ); - device->close(); - } - } - } - switch ( backend ) - { -// case Tar: -// return new KoTarStore( device, mode, appIdentification ); -// case Directory: -// kdError(s_area) << "Can't create a Directory store for a memory buffer!" << endl; - // fallback - case Zip: - return new KoZipStore( device, mode, appIdentification ); - default: - kdWarning(s_area) << "Unsupported backend requested for KoStore : " << backend << endl; - return 0L; - } -} - -KoStore* KoStore::createStore( TQWidget* window, const KURL& url, Mode mode, const TQCString & appIdentification, Backend backend ) -{ - if ( url.isLocalFile() ) - return createStore(url.path(), mode, appIdentification, backend ); - - TQString tmpFile; - if ( mode == KoStore::Write ) - { - if ( backend == Auto ) - backend = DefaultFormat; - } - else - { - const bool downloaded = - TDEIO::NetAccess::download( url, tmpFile, window ); - - if (!downloaded) - { - kdError(s_area) << "Could not download file!" << endl; - backend = DefaultFormat; // will create a "bad" store (bad()==true) - } - else if ( backend == Auto ) - { - TQFile file( tmpFile ); - if ( file.open( IO_ReadOnly ) ) - { - backend = determineBackend( TQT_TQIODEVICE(&file) ); - file.close(); - } - } - } - switch ( backend ) - { -// case Tar: -// return new KoTarStore( window, url, tmpFile, mode, appIdentification ); - case Zip: - return new KoZipStore( window, url, tmpFile, mode, appIdentification ); - default: - kdWarning(s_area) << "Unsupported backend requested for KoStore (KURL) : " << backend << endl; - KMessageBox::sorry( window, - i18n("The directory mode is not supported for remote locations."), - i18n("KOffice Storage")); - return 0L; - } -} - -namespace { - const char* const ROOTPART = "root"; - const char* const MAINNAME = "maindoc.xml"; -} - -bool KoStore::init( Mode _mode ) -{ - d = 0; - m_bIsOpen = false; - m_mode = _mode; - m_stream = 0; - - // Assume new style names. - m_namingVersion = NAMING_VERSION_2_2; - return true; -} - -KoStore::~KoStore() -{ - delete m_stream; -} - -bool KoStore::open( const TQString & _name ) -{ - // This also converts from relative to absolute, i.e. merges the currentPath() - m_sName = toExternalNaming( _name ); - - if ( m_bIsOpen ) - { - kdWarning(s_area) << "KoStore: File is already opened" << endl; - //return TDEIO::ERR_INTERNAL; - return false; - } - - if ( m_sName.length() > 512 ) - { - kdError(s_area) << "KoStore: Filename " << m_sName << " is too long" << endl; - //return TDEIO::ERR_MALFORMED_URL; - return false; - } - - if ( m_mode == Write ) - { - kdDebug(s_area) << "KoStore: opening for writing '" << m_sName << "'" << endl; - if ( m_strFiles.findIndex( m_sName ) != -1 ) // just check if it's there - { - kdWarning(s_area) << "KoStore: Duplicate filename " << m_sName << endl; - //return TDEIO::ERR_FILE_ALREADY_EXIST; - return false; - } - - m_strFiles.append( m_sName ); - - m_iSize = 0; - if ( !openWrite( m_sName ) ) - return false; - } - else if ( m_mode == Read ) - { - kdDebug(s_area) << "Opening for reading '" << m_sName << "'" << endl; - if ( !openRead( m_sName ) ) - return false; - } - else - //return TDEIO::ERR_UNSUPPORTED_ACTION; - return false; - - m_bIsOpen = true; - return true; -} - -bool KoStore::isOpen() const -{ - return m_bIsOpen; -} - -bool KoStore::close() -{ - kdDebug(s_area) << "KoStore: Closing" << endl; - - if ( !m_bIsOpen ) - { - kdWarning(s_area) << "KoStore: You must open before closing" << endl; - //return TDEIO::ERR_INTERNAL; - return false; - } - - bool ret = m_mode == Write ? closeWrite() : closeRead(); - - delete m_stream; - m_stream = 0L; - m_bIsOpen = false; - return ret; -} - -TQIODevice* KoStore::device() const -{ - if ( !m_bIsOpen ) - kdWarning(s_area) << "KoStore: You must open before asking for a device" << endl; - if ( m_mode != Read ) - kdWarning(s_area) << "KoStore: Can not get device from store that is opened for writing" << endl; - return m_stream; -} - -TQByteArray KoStore::read( unsigned long int max ) -{ - TQByteArray data; // Data is a TQArray - - if ( !m_bIsOpen ) - { - kdWarning(s_area) << "KoStore: You must open before reading" << endl; - data.resize( 0 ); - return data; - } - if ( m_mode != Read ) - { - kdError(s_area) << "KoStore: Can not read from store that is opened for writing" << endl; - data.resize( 0 ); - return data; - } - - if ( m_stream->atEnd() ) - { - data.resize( 0 ); - return data; - } - - if ( max > m_iSize - m_stream->at() ) - max = m_iSize - m_stream->at(); - if ( max == 0 ) - { - data.resize( 0 ); - return data; - } - - char *p = new char[ max ]; - m_stream->readBlock( p, max ); - - data.setRawData( p, max ); - return data; -} - -TQ_LONG KoStore::write( const TQByteArray& data ) -{ - return write( data.data(), data.size() ); // see below -} - -TQ_LONG KoStore::read( char *_buffer, TQ_ULONG _len ) -{ - if ( !m_bIsOpen ) - { - kdError(s_area) << "KoStore: You must open before reading" << endl; - return -1; - } - if ( m_mode != Read ) - { - kdError(s_area) << "KoStore: Can not read from store that is opened for writing" << endl; - return -1; - } - - if ( m_stream->atEnd() ) - return 0; - - if ( _len > m_iSize - m_stream->at() ) - _len = m_iSize - m_stream->at(); - if ( _len == 0 ) - return 0; - - return m_stream->readBlock( _buffer, _len ); -} - -TQ_LONG KoStore::write( const char* _data, TQ_ULONG _len ) -{ - if ( _len == 0L ) return 0; - - if ( !m_bIsOpen ) - { - kdError(s_area) << "KoStore: You must open before writing" << endl; - return 0L; - } - if ( m_mode != Write ) - { - kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl; - return 0L; - } - - int nwritten = m_stream->writeBlock( _data, _len ); - Q_ASSERT( nwritten == (int)_len ); - m_iSize += nwritten; - - return nwritten; -} - -TQIODevice::Offset KoStore::size() const -{ - if ( !m_bIsOpen ) - { - kdWarning(s_area) << "KoStore: You must open before asking for a size" << endl; - return static_cast(-1); - } - if ( m_mode != Read ) - { - kdWarning(s_area) << "KoStore: Can not get size from store that is opened for writing" << endl; - return static_cast(-1); - } - return m_iSize; -} - -bool KoStore::enterDirectory( const TQString& directory ) -{ - //kdDebug(s_area) << "KoStore::enterDirectory " << directory << endl; - int pos; - bool success = true; - TQString tmp( directory ); - - while ( ( pos = tmp.find( '/' ) ) != -1 && - ( success = enterDirectoryInternal( tmp.left( pos ) ) ) ) - tmp = tmp.mid( pos + 1 ); - - if ( success && !tmp.isEmpty() ) - return enterDirectoryInternal( tmp ); - return success; -} - -bool KoStore::leaveDirectory() -{ - if ( m_currentPath.isEmpty() ) - return false; - - m_currentPath.pop_back(); - - return enterAbsoluteDirectory( expandEncodedDirectory( currentPath() ) ); -} - -TQString KoStore::currentDirectory() const -{ - return expandEncodedDirectory( currentPath() ); -} - -TQString KoStore::currentPath() const -{ - TQString path; - TQStringList::ConstIterator it = m_currentPath.begin(); - TQStringList::ConstIterator end = m_currentPath.end(); - for ( ; it != end; ++it ) { - path += *it; - path += '/'; - } - return path; -} - -void KoStore::pushDirectory() -{ - m_directoryStack.push( currentPath() ); -} - -void KoStore::popDirectory() -{ - m_currentPath.clear(); - enterAbsoluteDirectory( TQString() ); - enterDirectory( m_directoryStack.pop() ); -} - -bool KoStore::addLocalFile( const TQString &fileName, const TQString &destName ) -{ - TQFileInfo fi( fileName ); - uint size = fi.size(); - TQFile file( fileName ); - if ( !file.open( IO_ReadOnly )) - { - return false; - } - - if ( !open ( destName ) ) - { - return false; - } - - TQByteArray data ( 8 * 1024 ); - - uint total = 0; - for ( int block = 0; ( block = file.readBlock ( data.data(), data.size() ) ) > 0; total += block ) - { - data.resize(block); - if ( write( data ) != block ) - return false; - data.resize(8*1024); - } - Q_ASSERT( total == size ); - - close(); - file.close(); - - return true; -} - -bool KoStore::extractFile ( const TQString &srcName, const TQString &fileName ) -{ - if ( !open ( srcName ) ) - return false; - - TQFile file( fileName ); - - if( !file.open ( IO_WriteOnly ) ) - { - close(); - return false; - } - - TQByteArray data ( 8 * 1024 ); - uint total = 0; - for( int block = 0; ( block = read ( data.data(), data.size() ) ) > 0; total += block ) - { - file.writeBlock ( data.data(), block ); - } - - if( size() != static_cast(-1) ) - Q_ASSERT( total == size() ); - - file.close(); - close(); - - return true; -} - -TQStringList KoStore::addLocalDirectory( const TQString &dirPath, const TQString &destName ) -{ - TQString dot = "."; - TQString dotdot = ".."; - TQStringList content; - - TQDir dir(dirPath); - if ( !dir.exists() ) - return 0; - - TQStringList files = dir.entryList(); - for ( TQStringList::Iterator it = files.begin(); it != files.end(); ++it ) - { - if ( *it != dot && *it != dotdot ) - { - TQString currentFile = dirPath + "/" + *it; - TQString dest = destName.isEmpty() ? *it : (destName + "/" + *it); - - TQFileInfo fi ( currentFile ); - if ( fi.isFile() ) - { - addLocalFile ( currentFile, dest ); - content.append(dest); - } - else if ( fi.isDir() ) - { - content += addLocalDirectory ( currentFile, dest ); - } - } - } - - return content; -} - - -bool KoStore::at( TQIODevice::Offset pos ) -{ - return m_stream->at( pos ); -} - -TQIODevice::Offset KoStore::at() const -{ - return m_stream->at(); -} - -bool KoStore::atEnd() const -{ - return m_stream->atEnd(); -} - -// See the specification for details of what this function does. -TQString KoStore::toExternalNaming( const TQString & _internalNaming ) const -{ - if ( _internalNaming == ROOTPART ) - return expandEncodedDirectory( currentPath() ) + MAINNAME; - - TQString intern; - if ( _internalNaming.startsWith( "tar:/" ) ) // absolute reference - intern = _internalNaming.mid( 5 ); // remove protocol - else - intern = currentPath() + _internalNaming; - - return expandEncodedPath( intern ); -} - -TQString KoStore::expandEncodedPath( TQString intern ) const -{ - if ( m_namingVersion == NAMING_VERSION_RAW ) - return intern; - - TQString result; - int pos; - - if ( ( pos = intern.findRev( '/', -1 ) ) != -1 ) { - result = expandEncodedDirectory( intern.left( pos ) ) + '/'; - intern = intern.mid( pos + 1 ); - } - - // Now process the filename. If the first character is numeric, we have - // a main document. - if ( TQChar(intern.at(0)).isDigit() ) - { - // If this is the first part name, check if we have a store with - // old-style names. - if ( ( m_namingVersion == NAMING_VERSION_2_2 ) && - ( m_mode == Read ) && - ( fileExists( result + "part" + intern + ".xml" ) ) ) - m_namingVersion = NAMING_VERSION_2_1; - - if ( m_namingVersion == NAMING_VERSION_2_1 ) - result = result + "part" + intern + ".xml"; - else - result = result + "part" + intern + "/" + MAINNAME; - } - else - result += intern; - return result; -} - -TQString KoStore::expandEncodedDirectory( TQString intern ) const -{ - if ( m_namingVersion == NAMING_VERSION_RAW ) - return intern; - - TQString result; - int pos; - while ( ( pos = intern.find( '/' ) ) != -1 ) { - if ( TQChar(intern.at(0)).isDigit() ) - result += "part"; - result += intern.left( pos + 1 ); // copy numbers (or "pictures") + "/" - intern = intern.mid( pos + 1 ); // remove the dir we just processed - } - - if ( TQChar(intern.at(0)).isDigit() ) - result += "part"; - result += intern; - return result; -} - -bool KoStore::enterDirectoryInternal( const TQString& directory ) -{ - if ( enterRelativeDirectory( expandEncodedDirectory( directory ) ) ) - { - m_currentPath.append( directory ); - return true; - } - return false; -} - -void KoStore::disallowNameExpansion( void ) -{ - m_namingVersion = NAMING_VERSION_RAW; -} - -bool KoStore::hasFile( const TQString& fileName ) const -{ - return fileExists( toExternalNaming( currentPath() + fileName ) ); -} diff --git a/src/projects/kostore/koStore.cpp b/src/projects/kostore/koStore.cpp new file mode 100644 index 0000000..24fc737 --- /dev/null +++ b/src/projects/kostore/koStore.cpp @@ -0,0 +1,628 @@ +/* This file is part of the KDE project + Copyright (C) 1998, 1999 Torben Weis + Copyright (C) 2000-2002 David Faure , Werner Trobin + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include + +#include "koStore.h" +//#include "koTarStore.h" +#include "koZipStore.h" +//#include "koDirectoryStore.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +//#define DefaultFormat KoStore::Tar +#define DefaultFormat KoStore::Zip + +const int KoStore::s_area = 30002; + +KoStore::Backend KoStore::determineBackend( TQIODevice* dev ) +{ + unsigned char buf[5]; + if ( dev->readBlock( (char *)buf, 4 ) < 4 ) + return DefaultFormat; // will create a "bad" store (bad()==true) + if ( buf[0] == 0037 && buf[1] == 0213 ) // gzip -> tar.gz + return Tar; + if ( buf[0] == 'P' && buf[1] == 'K' && buf[2] == 3 && buf[3] == 4 ) + return Zip; + return DefaultFormat; // fallback +} + +KoStore* KoStore::createStore( const TQString& fileName, Mode mode, const TQCString & appIdentification, Backend backend ) +{ + if ( backend == Auto ) { + if ( mode == KoStore::Write ) + backend = DefaultFormat; + else + { + TQFileInfo inf( fileName ); + if ( inf.isDir() ) + backend = Directory; + else + { + TQFile file( fileName ); + if ( file.open( IO_ReadOnly ) ) + backend = determineBackend( TQT_TQIODEVICE(&file) ); + else + backend = DefaultFormat; // will create a "bad" store (bad()==true) + } + } + } + switch ( backend ) + { +// case Tar: +// return new KoTarStore( fileName, mode, appIdentification ); + case Zip: + return new KoZipStore( fileName, mode, appIdentification ); +// case Directory: +// return new KoDirectoryStore( fileName /* should be a dir name.... */, mode ); + default: + kdWarning(s_area) << "Unsupported backend requested for KoStore : " << backend << endl; + return 0L; + } +} + +KoStore* KoStore::createStore( TQIODevice *device, Mode mode, const TQCString & appIdentification, Backend backend ) +{ + if ( backend == Auto ) + { + if ( mode == KoStore::Write ) + backend = DefaultFormat; + else { + if ( device->open( IO_ReadOnly ) ) { + backend = determineBackend( device ); + device->close(); + } + } + } + switch ( backend ) + { +// case Tar: +// return new KoTarStore( device, mode, appIdentification ); +// case Directory: +// kdError(s_area) << "Can't create a Directory store for a memory buffer!" << endl; + // fallback + case Zip: + return new KoZipStore( device, mode, appIdentification ); + default: + kdWarning(s_area) << "Unsupported backend requested for KoStore : " << backend << endl; + return 0L; + } +} + +KoStore* KoStore::createStore( TQWidget* window, const KURL& url, Mode mode, const TQCString & appIdentification, Backend backend ) +{ + if ( url.isLocalFile() ) + return createStore(url.path(), mode, appIdentification, backend ); + + TQString tmpFile; + if ( mode == KoStore::Write ) + { + if ( backend == Auto ) + backend = DefaultFormat; + } + else + { + const bool downloaded = + TDEIO::NetAccess::download( url, tmpFile, window ); + + if (!downloaded) + { + kdError(s_area) << "Could not download file!" << endl; + backend = DefaultFormat; // will create a "bad" store (bad()==true) + } + else if ( backend == Auto ) + { + TQFile file( tmpFile ); + if ( file.open( IO_ReadOnly ) ) + { + backend = determineBackend( TQT_TQIODEVICE(&file) ); + file.close(); + } + } + } + switch ( backend ) + { +// case Tar: +// return new KoTarStore( window, url, tmpFile, mode, appIdentification ); + case Zip: + return new KoZipStore( window, url, tmpFile, mode, appIdentification ); + default: + kdWarning(s_area) << "Unsupported backend requested for KoStore (KURL) : " << backend << endl; + KMessageBox::sorry( window, + i18n("The directory mode is not supported for remote locations."), + i18n("KOffice Storage")); + return 0L; + } +} + +namespace { + const char* const ROOTPART = "root"; + const char* const MAINNAME = "maindoc.xml"; +} + +bool KoStore::init( Mode _mode ) +{ + d = 0; + m_bIsOpen = false; + m_mode = _mode; + m_stream = 0; + + // Assume new style names. + m_namingVersion = NAMING_VERSION_2_2; + return true; +} + +KoStore::~KoStore() +{ + delete m_stream; +} + +bool KoStore::open( const TQString & _name ) +{ + // This also converts from relative to absolute, i.e. merges the currentPath() + m_sName = toExternalNaming( _name ); + + if ( m_bIsOpen ) + { + kdWarning(s_area) << "KoStore: File is already opened" << endl; + //return TDEIO::ERR_INTERNAL; + return false; + } + + if ( m_sName.length() > 512 ) + { + kdError(s_area) << "KoStore: Filename " << m_sName << " is too long" << endl; + //return TDEIO::ERR_MALFORMED_URL; + return false; + } + + if ( m_mode == Write ) + { + kdDebug(s_area) << "KoStore: opening for writing '" << m_sName << "'" << endl; + if ( m_strFiles.findIndex( m_sName ) != -1 ) // just check if it's there + { + kdWarning(s_area) << "KoStore: Duplicate filename " << m_sName << endl; + //return TDEIO::ERR_FILE_ALREADY_EXIST; + return false; + } + + m_strFiles.append( m_sName ); + + m_iSize = 0; + if ( !openWrite( m_sName ) ) + return false; + } + else if ( m_mode == Read ) + { + kdDebug(s_area) << "Opening for reading '" << m_sName << "'" << endl; + if ( !openRead( m_sName ) ) + return false; + } + else + //return TDEIO::ERR_UNSUPPORTED_ACTION; + return false; + + m_bIsOpen = true; + return true; +} + +bool KoStore::isOpen() const +{ + return m_bIsOpen; +} + +bool KoStore::close() +{ + kdDebug(s_area) << "KoStore: Closing" << endl; + + if ( !m_bIsOpen ) + { + kdWarning(s_area) << "KoStore: You must open before closing" << endl; + //return TDEIO::ERR_INTERNAL; + return false; + } + + bool ret = m_mode == Write ? closeWrite() : closeRead(); + + delete m_stream; + m_stream = 0L; + m_bIsOpen = false; + return ret; +} + +TQIODevice* KoStore::device() const +{ + if ( !m_bIsOpen ) + kdWarning(s_area) << "KoStore: You must open before asking for a device" << endl; + if ( m_mode != Read ) + kdWarning(s_area) << "KoStore: Can not get device from store that is opened for writing" << endl; + return m_stream; +} + +TQByteArray KoStore::read( unsigned long int max ) +{ + TQByteArray data; // Data is a TQArray + + if ( !m_bIsOpen ) + { + kdWarning(s_area) << "KoStore: You must open before reading" << endl; + data.resize( 0 ); + return data; + } + if ( m_mode != Read ) + { + kdError(s_area) << "KoStore: Can not read from store that is opened for writing" << endl; + data.resize( 0 ); + return data; + } + + if ( m_stream->atEnd() ) + { + data.resize( 0 ); + return data; + } + + if ( max > m_iSize - m_stream->at() ) + max = m_iSize - m_stream->at(); + if ( max == 0 ) + { + data.resize( 0 ); + return data; + } + + char *p = new char[ max ]; + m_stream->readBlock( p, max ); + + data.setRawData( p, max ); + return data; +} + +TQ_LONG KoStore::write( const TQByteArray& data ) +{ + return write( data.data(), data.size() ); // see below +} + +TQ_LONG KoStore::read( char *_buffer, TQ_ULONG _len ) +{ + if ( !m_bIsOpen ) + { + kdError(s_area) << "KoStore: You must open before reading" << endl; + return -1; + } + if ( m_mode != Read ) + { + kdError(s_area) << "KoStore: Can not read from store that is opened for writing" << endl; + return -1; + } + + if ( m_stream->atEnd() ) + return 0; + + if ( _len > m_iSize - m_stream->at() ) + _len = m_iSize - m_stream->at(); + if ( _len == 0 ) + return 0; + + return m_stream->readBlock( _buffer, _len ); +} + +TQ_LONG KoStore::write( const char* _data, TQ_ULONG _len ) +{ + if ( _len == 0L ) return 0; + + if ( !m_bIsOpen ) + { + kdError(s_area) << "KoStore: You must open before writing" << endl; + return 0L; + } + if ( m_mode != Write ) + { + kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl; + return 0L; + } + + int nwritten = m_stream->writeBlock( _data, _len ); + Q_ASSERT( nwritten == (int)_len ); + m_iSize += nwritten; + + return nwritten; +} + +TQIODevice::Offset KoStore::size() const +{ + if ( !m_bIsOpen ) + { + kdWarning(s_area) << "KoStore: You must open before asking for a size" << endl; + return static_cast(-1); + } + if ( m_mode != Read ) + { + kdWarning(s_area) << "KoStore: Can not get size from store that is opened for writing" << endl; + return static_cast(-1); + } + return m_iSize; +} + +bool KoStore::enterDirectory( const TQString& directory ) +{ + //kdDebug(s_area) << "KoStore::enterDirectory " << directory << endl; + int pos; + bool success = true; + TQString tmp( directory ); + + while ( ( pos = tmp.find( '/' ) ) != -1 && + ( success = enterDirectoryInternal( tmp.left( pos ) ) ) ) + tmp = tmp.mid( pos + 1 ); + + if ( success && !tmp.isEmpty() ) + return enterDirectoryInternal( tmp ); + return success; +} + +bool KoStore::leaveDirectory() +{ + if ( m_currentPath.isEmpty() ) + return false; + + m_currentPath.pop_back(); + + return enterAbsoluteDirectory( expandEncodedDirectory( currentPath() ) ); +} + +TQString KoStore::currentDirectory() const +{ + return expandEncodedDirectory( currentPath() ); +} + +TQString KoStore::currentPath() const +{ + TQString path; + TQStringList::ConstIterator it = m_currentPath.begin(); + TQStringList::ConstIterator end = m_currentPath.end(); + for ( ; it != end; ++it ) { + path += *it; + path += '/'; + } + return path; +} + +void KoStore::pushDirectory() +{ + m_directoryStack.push( currentPath() ); +} + +void KoStore::popDirectory() +{ + m_currentPath.clear(); + enterAbsoluteDirectory( TQString() ); + enterDirectory( m_directoryStack.pop() ); +} + +bool KoStore::addLocalFile( const TQString &fileName, const TQString &destName ) +{ + TQFileInfo fi( fileName ); + uint size = fi.size(); + TQFile file( fileName ); + if ( !file.open( IO_ReadOnly )) + { + return false; + } + + if ( !open ( destName ) ) + { + return false; + } + + TQByteArray data ( 8 * 1024 ); + + uint total = 0; + for ( int block = 0; ( block = file.readBlock ( data.data(), data.size() ) ) > 0; total += block ) + { + data.resize(block); + if ( write( data ) != block ) + return false; + data.resize(8*1024); + } + Q_ASSERT( total == size ); + + close(); + file.close(); + + return true; +} + +bool KoStore::extractFile ( const TQString &srcName, const TQString &fileName ) +{ + if ( !open ( srcName ) ) + return false; + + TQFile file( fileName ); + + if( !file.open ( IO_WriteOnly ) ) + { + close(); + return false; + } + + TQByteArray data ( 8 * 1024 ); + uint total = 0; + for( int block = 0; ( block = read ( data.data(), data.size() ) ) > 0; total += block ) + { + file.writeBlock ( data.data(), block ); + } + + if( size() != static_cast(-1) ) + Q_ASSERT( total == size() ); + + file.close(); + close(); + + return true; +} + +TQStringList KoStore::addLocalDirectory( const TQString &dirPath, const TQString &destName ) +{ + TQString dot = "."; + TQString dotdot = ".."; + TQStringList content; + + TQDir dir(dirPath); + if ( !dir.exists() ) + return 0; + + TQStringList files = dir.entryList(); + for ( TQStringList::Iterator it = files.begin(); it != files.end(); ++it ) + { + if ( *it != dot && *it != dotdot ) + { + TQString currentFile = dirPath + "/" + *it; + TQString dest = destName.isEmpty() ? *it : (destName + "/" + *it); + + TQFileInfo fi ( currentFile ); + if ( fi.isFile() ) + { + addLocalFile ( currentFile, dest ); + content.append(dest); + } + else if ( fi.isDir() ) + { + content += addLocalDirectory ( currentFile, dest ); + } + } + } + + return content; +} + + +bool KoStore::at( TQIODevice::Offset pos ) +{ + return m_stream->at( pos ); +} + +TQIODevice::Offset KoStore::at() const +{ + return m_stream->at(); +} + +bool KoStore::atEnd() const +{ + return m_stream->atEnd(); +} + +// See the specification for details of what this function does. +TQString KoStore::toExternalNaming( const TQString & _internalNaming ) const +{ + if ( _internalNaming == ROOTPART ) + return expandEncodedDirectory( currentPath() ) + MAINNAME; + + TQString intern; + if ( _internalNaming.startsWith( "tar:/" ) ) // absolute reference + intern = _internalNaming.mid( 5 ); // remove protocol + else + intern = currentPath() + _internalNaming; + + return expandEncodedPath( intern ); +} + +TQString KoStore::expandEncodedPath( TQString intern ) const +{ + if ( m_namingVersion == NAMING_VERSION_RAW ) + return intern; + + TQString result; + int pos; + + if ( ( pos = intern.findRev( '/', -1 ) ) != -1 ) { + result = expandEncodedDirectory( intern.left( pos ) ) + '/'; + intern = intern.mid( pos + 1 ); + } + + // Now process the filename. If the first character is numeric, we have + // a main document. + if ( TQChar(intern.at(0)).isDigit() ) + { + // If this is the first part name, check if we have a store with + // old-style names. + if ( ( m_namingVersion == NAMING_VERSION_2_2 ) && + ( m_mode == Read ) && + ( fileExists( result + "part" + intern + ".xml" ) ) ) + m_namingVersion = NAMING_VERSION_2_1; + + if ( m_namingVersion == NAMING_VERSION_2_1 ) + result = result + "part" + intern + ".xml"; + else + result = result + "part" + intern + "/" + MAINNAME; + } + else + result += intern; + return result; +} + +TQString KoStore::expandEncodedDirectory( TQString intern ) const +{ + if ( m_namingVersion == NAMING_VERSION_RAW ) + return intern; + + TQString result; + int pos; + while ( ( pos = intern.find( '/' ) ) != -1 ) { + if ( TQChar(intern.at(0)).isDigit() ) + result += "part"; + result += intern.left( pos + 1 ); // copy numbers (or "pictures") + "/" + intern = intern.mid( pos + 1 ); // remove the dir we just processed + } + + if ( TQChar(intern.at(0)).isDigit() ) + result += "part"; + result += intern; + return result; +} + +bool KoStore::enterDirectoryInternal( const TQString& directory ) +{ + if ( enterRelativeDirectory( expandEncodedDirectory( directory ) ) ) + { + m_currentPath.append( directory ); + return true; + } + return false; +} + +void KoStore::disallowNameExpansion( void ) +{ + m_namingVersion = NAMING_VERSION_RAW; +} + +bool KoStore::hasFile( const TQString& fileName ) const +{ + return fileExists( toExternalNaming( currentPath() + fileName ) ); +} diff --git a/src/projects/kostore/koStoreBase.cc b/src/projects/kostore/koStoreBase.cc deleted file mode 100644 index a9cc63e..0000000 --- a/src/projects/kostore/koStoreBase.cc +++ /dev/null @@ -1,29 +0,0 @@ -// -/* This file is part of the KDE project - Copyright 2004 Nicolas GOUTTE - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "koStoreBase.h" - -KoStoreBase::KoStoreBase(void) : m_fileMode(Local), m_window(0) -{ -} - -KoStoreBase::~KoStoreBase(void) -{ -} diff --git a/src/projects/kostore/koStoreBase.cpp b/src/projects/kostore/koStoreBase.cpp new file mode 100644 index 0000000..a9cc63e --- /dev/null +++ b/src/projects/kostore/koStoreBase.cpp @@ -0,0 +1,29 @@ +// +/* This file is part of the KDE project + Copyright 2004 Nicolas GOUTTE + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include "koStoreBase.h" + +KoStoreBase::KoStoreBase(void) : m_fileMode(Local), m_window(0) +{ +} + +KoStoreBase::~KoStoreBase(void) +{ +} diff --git a/src/projects/kostore/koZipStore.cc b/src/projects/kostore/koZipStore.cc deleted file mode 100644 index 0bbecdc..0000000 --- a/src/projects/kostore/koZipStore.cc +++ /dev/null @@ -1,237 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2000-2002 David Faure - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "koZipStore.h" - -#include - -#include -#include -#include -#include -#include -#if ! KDE_IS_VERSION( 3, 4, 1 ) -#include -#include -#endif - -KoZipStore::KoZipStore( const TQString & _filename, Mode _mode, const TQCString & appIdentification ) -{ - kdDebug(s_area) << "KoZipStore Constructor filename = " << _filename - << " mode = " << int(_mode) - << " mimetype = " << appIdentification << endl; - - m_pZip = new KZip( _filename ); - -#if ! KDE_IS_VERSION( 3, 4, 1 ) - // Workaround for KZip KSaveFile double deletion in tdelibs-3.4, - // when trying to write to a non-writable directory. - TQDir dir( TQFileInfo( _filename ).dir() ); - if (_mode == Write && !TQFileInfo( dir.path() ).isWritable() ) - { - kdWarning(s_area) << dir.path() << " isn't writable" << endl; - m_bGood = false; - m_currentDir = 0; - KoStore::init( _mode ); - } - else -#endif - { - m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars - } -} - -KoZipStore::KoZipStore( TQIODevice *dev, Mode mode, const TQCString & appIdentification ) -{ - m_pZip = new KZip( dev ); - m_bGood = init( mode, appIdentification ); -} - -KoZipStore::KoZipStore( TQWidget* window, const KURL & _url, const TQString & _filename, Mode _mode, const TQCString & appIdentification ) -{ - kdDebug(s_area) << "KoZipStore Constructor url" << _url.prettyURL() - << " filename = " << _filename - << " mode = " << int(_mode) - << " mimetype = " << appIdentification << endl; - - m_url = _url; - m_window = window; - - if ( _mode == KoStore::Read ) - { - m_fileMode = KoStoreBase::RemoteRead; - m_localFileName = _filename; - - } - else - { - m_fileMode = KoStoreBase::RemoteWrite; - m_localFileName = "/tmp/kozip"; // ### FIXME with KTempFile - } - - m_pZip = new KZip( m_localFileName ); - m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars -} - -KoZipStore::~KoZipStore() -{ - kdDebug(s_area) << "KoZipStore::~KoZipStore" << endl; - m_pZip->close(); - delete m_pZip; - - // Now we have still some job to do for remote files. - if ( m_fileMode == KoStoreBase::RemoteRead ) - { - TDEIO::NetAccess::removeTempFile( m_localFileName ); - } - else if ( m_fileMode == KoStoreBase::RemoteWrite ) - { - TDEIO::NetAccess::upload( m_localFileName, m_url, m_window ); - // ### FIXME: delete temp file - } -} - -bool KoZipStore::init( Mode _mode, const TQCString& appIdentification ) -{ - KoStore::init( _mode ); - m_currentDir = 0; - bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly ); - - if ( good && _mode == Read ) - good = m_pZip->directory() != 0; - else if ( good && _mode == Write ) - { - //kdDebug(s_area) << "KoZipStore::init writing mimetype " << appIdentification << endl; - - m_pZip->setCompression( KZip::NoCompression ); - m_pZip->setExtraField( KZip::NoExtraField ); - // Write identification - (void)m_pZip->writeFile( "mimetype", "", "", appIdentification.length(), appIdentification.data() ); - m_pZip->setCompression( KZip::DeflateCompression ); - // We don't need the extra field in KOffice - so we leave it as "no extra field". - } - return good; -} - -bool KoZipStore::openWrite( const TQString& name ) -{ -#if 0 - // Prepare memory buffer for writing - m_byteArray.resize( 0 ); - m_stream = new TQBuffer( m_byteArray ); - m_stream->open( IO_WriteOnly ); - return true; -#endif - m_stream = 0L; // Don't use! - return m_pZip->prepareWriting( name, "", "" /*m_pZip->rootDir()->user(), m_pZip->rootDir()->group()*/, 0 ); -} - -bool KoZipStore::openRead( const TQString& name ) -{ - const KArchiveEntry * entry = m_pZip->directory()->entry( name ); - if ( entry == 0L ) - { - //kdWarning(s_area) << "Unknown filename " << name << endl; - //return TDEIO::ERR_DOES_NOT_EXIST; - return false; - } - if ( entry->isDirectory() ) - { - kdWarning(s_area) << name << " is a directory !" << endl; - //return TDEIO::ERR_IS_DIRECTORY; - return false; - } - // Must cast to KZipFileEntry, not only KArchiveFile, because device() isn't virtual! - const KZipFileEntry * f = static_cast(entry); - delete m_stream; - m_stream = f->device(); - m_iSize = f->size(); - return true; -} - -TQ_LONG KoZipStore::write( const char* _data, TQ_ULONG _len ) -{ - if ( _len == 0L ) return 0; - //kdDebug(s_area) << "KoZipStore::write " << _len << endl; - - if ( !m_bIsOpen ) - { - kdError(s_area) << "KoStore: You must open before writing" << endl; - return 0L; - } - if ( m_mode != Write ) - { - kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl; - return 0L; - } - - m_iSize += _len; - if ( m_pZip->writeData( _data, _len ) ) // writeData returns a bool! - return _len; - return 0L; -} - -bool KoZipStore::closeWrite() -{ - kdDebug(s_area) << "Wrote file " << m_sName << " into ZIP archive. size " - << m_iSize << endl; - return m_pZip->doneWriting( m_iSize ); -#if 0 - if ( !m_pZip->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) ) - kdWarning( s_area ) << "Failed to write " << m_sName << endl; - m_byteArray.resize( 0 ); // save memory - return true; -#endif -} - -bool KoZipStore::enterRelativeDirectory( const TQString& dirName ) -{ - if ( m_mode == Read ) { - if ( !m_currentDir ) { - m_currentDir = m_pZip->directory(); // initialize - Q_ASSERT( m_currentPath.isEmpty() ); - } - const KArchiveEntry *entry = m_currentDir->entry( dirName ); - if ( entry && entry->isDirectory() ) { - m_currentDir = dynamic_cast( entry ); - return m_currentDir != 0; - } - return false; - } - else // Write, no checking here - return true; -} - -bool KoZipStore::enterAbsoluteDirectory( const TQString& path ) -{ - if ( path.isEmpty() ) - { - m_currentDir = 0; - return true; - } - m_currentDir = dynamic_cast( m_pZip->directory()->entry( path ) ); - Q_ASSERT( m_currentDir ); - return m_currentDir != 0; -} - -bool KoZipStore::fileExists( const TQString& absPath ) const -{ - const KArchiveEntry *entry = m_pZip->directory()->entry( absPath ); - return entry && entry->isFile(); -} diff --git a/src/projects/kostore/koZipStore.cpp b/src/projects/kostore/koZipStore.cpp new file mode 100644 index 0000000..0bbecdc --- /dev/null +++ b/src/projects/kostore/koZipStore.cpp @@ -0,0 +1,237 @@ +/* This file is part of the KDE project + Copyright (C) 2000-2002 David Faure + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include "koZipStore.h" + +#include + +#include +#include +#include +#include +#include +#if ! KDE_IS_VERSION( 3, 4, 1 ) +#include +#include +#endif + +KoZipStore::KoZipStore( const TQString & _filename, Mode _mode, const TQCString & appIdentification ) +{ + kdDebug(s_area) << "KoZipStore Constructor filename = " << _filename + << " mode = " << int(_mode) + << " mimetype = " << appIdentification << endl; + + m_pZip = new KZip( _filename ); + +#if ! KDE_IS_VERSION( 3, 4, 1 ) + // Workaround for KZip KSaveFile double deletion in tdelibs-3.4, + // when trying to write to a non-writable directory. + TQDir dir( TQFileInfo( _filename ).dir() ); + if (_mode == Write && !TQFileInfo( dir.path() ).isWritable() ) + { + kdWarning(s_area) << dir.path() << " isn't writable" << endl; + m_bGood = false; + m_currentDir = 0; + KoStore::init( _mode ); + } + else +#endif + { + m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars + } +} + +KoZipStore::KoZipStore( TQIODevice *dev, Mode mode, const TQCString & appIdentification ) +{ + m_pZip = new KZip( dev ); + m_bGood = init( mode, appIdentification ); +} + +KoZipStore::KoZipStore( TQWidget* window, const KURL & _url, const TQString & _filename, Mode _mode, const TQCString & appIdentification ) +{ + kdDebug(s_area) << "KoZipStore Constructor url" << _url.prettyURL() + << " filename = " << _filename + << " mode = " << int(_mode) + << " mimetype = " << appIdentification << endl; + + m_url = _url; + m_window = window; + + if ( _mode == KoStore::Read ) + { + m_fileMode = KoStoreBase::RemoteRead; + m_localFileName = _filename; + + } + else + { + m_fileMode = KoStoreBase::RemoteWrite; + m_localFileName = "/tmp/kozip"; // ### FIXME with KTempFile + } + + m_pZip = new KZip( m_localFileName ); + m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars +} + +KoZipStore::~KoZipStore() +{ + kdDebug(s_area) << "KoZipStore::~KoZipStore" << endl; + m_pZip->close(); + delete m_pZip; + + // Now we have still some job to do for remote files. + if ( m_fileMode == KoStoreBase::RemoteRead ) + { + TDEIO::NetAccess::removeTempFile( m_localFileName ); + } + else if ( m_fileMode == KoStoreBase::RemoteWrite ) + { + TDEIO::NetAccess::upload( m_localFileName, m_url, m_window ); + // ### FIXME: delete temp file + } +} + +bool KoZipStore::init( Mode _mode, const TQCString& appIdentification ) +{ + KoStore::init( _mode ); + m_currentDir = 0; + bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly ); + + if ( good && _mode == Read ) + good = m_pZip->directory() != 0; + else if ( good && _mode == Write ) + { + //kdDebug(s_area) << "KoZipStore::init writing mimetype " << appIdentification << endl; + + m_pZip->setCompression( KZip::NoCompression ); + m_pZip->setExtraField( KZip::NoExtraField ); + // Write identification + (void)m_pZip->writeFile( "mimetype", "", "", appIdentification.length(), appIdentification.data() ); + m_pZip->setCompression( KZip::DeflateCompression ); + // We don't need the extra field in KOffice - so we leave it as "no extra field". + } + return good; +} + +bool KoZipStore::openWrite( const TQString& name ) +{ +#if 0 + // Prepare memory buffer for writing + m_byteArray.resize( 0 ); + m_stream = new TQBuffer( m_byteArray ); + m_stream->open( IO_WriteOnly ); + return true; +#endif + m_stream = 0L; // Don't use! + return m_pZip->prepareWriting( name, "", "" /*m_pZip->rootDir()->user(), m_pZip->rootDir()->group()*/, 0 ); +} + +bool KoZipStore::openRead( const TQString& name ) +{ + const KArchiveEntry * entry = m_pZip->directory()->entry( name ); + if ( entry == 0L ) + { + //kdWarning(s_area) << "Unknown filename " << name << endl; + //return TDEIO::ERR_DOES_NOT_EXIST; + return false; + } + if ( entry->isDirectory() ) + { + kdWarning(s_area) << name << " is a directory !" << endl; + //return TDEIO::ERR_IS_DIRECTORY; + return false; + } + // Must cast to KZipFileEntry, not only KArchiveFile, because device() isn't virtual! + const KZipFileEntry * f = static_cast(entry); + delete m_stream; + m_stream = f->device(); + m_iSize = f->size(); + return true; +} + +TQ_LONG KoZipStore::write( const char* _data, TQ_ULONG _len ) +{ + if ( _len == 0L ) return 0; + //kdDebug(s_area) << "KoZipStore::write " << _len << endl; + + if ( !m_bIsOpen ) + { + kdError(s_area) << "KoStore: You must open before writing" << endl; + return 0L; + } + if ( m_mode != Write ) + { + kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl; + return 0L; + } + + m_iSize += _len; + if ( m_pZip->writeData( _data, _len ) ) // writeData returns a bool! + return _len; + return 0L; +} + +bool KoZipStore::closeWrite() +{ + kdDebug(s_area) << "Wrote file " << m_sName << " into ZIP archive. size " + << m_iSize << endl; + return m_pZip->doneWriting( m_iSize ); +#if 0 + if ( !m_pZip->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) ) + kdWarning( s_area ) << "Failed to write " << m_sName << endl; + m_byteArray.resize( 0 ); // save memory + return true; +#endif +} + +bool KoZipStore::enterRelativeDirectory( const TQString& dirName ) +{ + if ( m_mode == Read ) { + if ( !m_currentDir ) { + m_currentDir = m_pZip->directory(); // initialize + Q_ASSERT( m_currentPath.isEmpty() ); + } + const KArchiveEntry *entry = m_currentDir->entry( dirName ); + if ( entry && entry->isDirectory() ) { + m_currentDir = dynamic_cast( entry ); + return m_currentDir != 0; + } + return false; + } + else // Write, no checking here + return true; +} + +bool KoZipStore::enterAbsoluteDirectory( const TQString& path ) +{ + if ( path.isEmpty() ) + { + m_currentDir = 0; + return true; + } + m_currentDir = dynamic_cast( m_pZip->directory()->entry( path ) ); + Q_ASSERT( m_currentDir ); + return m_currentDir != 0; +} + +bool KoZipStore::fileExists( const TQString& absPath ) const +{ + const KArchiveEntry *entry = m_pZip->directory()->entry( absPath ); + return entry && entry->isFile(); +} -- cgit v1.2.1