00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KoDocumentInfoPropsPage.h"
00022 #include "KoDocumentInfo.h"
00023 #include "KoDocumentInfoDlg.h"
00024 #include <KoXmlReader.h>
00025 #include <ktar.h>
00026 #include <ktemporaryfile.h>
00027 #include <kdeversion.h>
00028 #include <kfilterdev.h>
00029
00030 #include <QBuffer>
00031 #include <QFile>
00032 #include <QDir>
00033 #include <QTextStream>
00034
00035 #include <sys/stat.h>
00036 #include <unistd.h>
00037 #include <assert.h>
00038
00039 class KoDocumentInfoPropsPage::KoDocumentInfoPropsPagePrivate
00040 {
00041 public:
00042 KoDocumentInfo *m_info;
00043 KoDocumentInfoDlg *m_dlg;
00044 KUrl m_url;
00045 KTar *m_src;
00046 KTar *m_dst;
00047
00048 const KArchiveFile *m_docInfoFile;
00049 };
00050
00051 KoDocumentInfoPropsPage::KoDocumentInfoPropsPage( KPropertiesDialog *props,
00052 const QStringList & )
00053 : KPropsDlgPlugin( props )
00054 {
00055 d = new KoDocumentInfoPropsPagePrivate;
00056 d->m_info = new KoDocumentInfo( this );
00057 d->m_url = props->item()->url();
00058 d->m_dlg = 0;
00059
00060 if ( !d->m_url.isLocalFile() )
00061 return;
00062
00063 d->m_dst = 0;
00064
00065 #ifdef __GNUC__
00066 #warning TODO port this to KoStore !!!
00067 #endif
00068 d->m_src = new KTar( d->m_url.path(), "application/x-gzip" );
00069
00070 if ( !d->m_src->open( QIODevice::ReadOnly ) )
00071 return;
00072
00073 const KArchiveDirectory *root = d->m_src->directory();
00074 if ( !root )
00075 return;
00076
00077 const KArchiveEntry *entry = root->entry( "documentinfo.xml" );
00078
00079 if ( entry && entry->isFile() )
00080 {
00081 d->m_docInfoFile = static_cast<const KArchiveFile *>( entry );
00082
00083 QByteArray data = d->m_docInfoFile->data();
00084 QBuffer buffer( &data );
00085 buffer.open( QIODevice::ReadOnly );
00086
00087 KoXmlDocument doc;
00088 doc.setContent( &buffer );
00089
00090 d->m_info->load( doc );
00091 }
00092
00093 d->m_dlg = new KoDocumentInfoDlg( props, d->m_info );
00094 connect( d->m_dlg, SIGNAL( changed() ),
00095 this, SIGNAL( changed() ) );
00096 }
00097
00098 KoDocumentInfoPropsPage::~KoDocumentInfoPropsPage()
00099 {
00100 delete d->m_info;
00101 delete d->m_src;
00102 delete d->m_dst;
00103 delete d->m_dlg;
00104 delete d;
00105 }
00106
00107 void KoDocumentInfoPropsPage::applyChanges()
00108 {
00109 const KArchiveDirectory *root = d->m_src->directory();
00110 if ( !root )
00111 return;
00112
00113 QFileInfo fileInfo(d->m_url.path());
00114
00115 KTemporaryFile tempFile;
00116 tempFile.setPrefix(d->m_url.path());
00117
00118 if ( !tempFile.open() )
00119 return;
00120 tempFile.setPermissions(fileInfo.permissions());
00121
00122 d->m_dst = new KTar( tempFile.fileName(), "application/x-gzip" );
00123
00124 if ( !d->m_dst->open( QIODevice::WriteOnly ) )
00125 return;
00126
00127 KMimeType::Ptr mimeType = KMimeType::findByUrl( d->m_url, 0, true );
00128 if ( mimeType && dynamic_cast<KFilterDev *>( d->m_dst->device() ) != 0 )
00129 {
00130 QByteArray appIdentification( "KOffice " );
00131 appIdentification += mimeType->name().toLatin1();
00132 appIdentification += '\004';
00133 appIdentification += '\006';
00134 d->m_dst->setOrigFileName( appIdentification );
00135 }
00136
00137 bool docInfoSaved = false;
00138
00139 QStringList entries = root->entries();
00140 QStringList::ConstIterator it = entries.begin();
00141 QStringList::ConstIterator end = entries.end();
00142 for (; it != end; ++it )
00143 {
00144 const KArchiveEntry *entry = root->entry( *it );
00145
00146 assert( entry );
00147
00148 if ( entry->name() == "documentinfo.xml" ||
00149 ( !docInfoSaved && !entries.contains( "documentinfo.xml" ) ) )
00150 {
00151 d->m_dlg->slotApply();
00152
00153 QBuffer buffer;
00154 buffer.open( QIODevice::WriteOnly );
00155 QTextStream str( &buffer );
00156 str << d->m_info->save();
00157 buffer.close();
00158
00159 kDebug( 30003 ) << "writing documentinfo.xml" << endl;
00160 d->m_dst->writeFile( "documentinfo.xml", entry->user(), entry->group(),
00161 buffer.buffer().data(), buffer.buffer().size() );
00162
00163 docInfoSaved = true;
00164 }
00165 else
00166 copy( QString::null, entry );
00167 }
00168
00169 d->m_dst->close();
00170
00171 QDir dir;
00172 dir.rename( tempFile.fileName(), d->m_url.path() );
00173
00174 delete d->m_dst;
00175 d->m_dst = 0;
00176 }
00177
00178 void KoDocumentInfoPropsPage::copy( const QString &path, const KArchiveEntry *entry )
00179 {
00180 kDebug( 30003 ) << "copy " << entry->name() << endl;
00181 if ( entry->isFile() )
00182 {
00183 const KArchiveFile *file = static_cast<const KArchiveFile *>( entry );
00184 kDebug( 30003 ) << "file :" << entry->name() << endl;
00185 kDebug( 30003 ) << "full path is: " << path << entry->name() << endl;
00186 d->m_dst->writeFile( path + entry->name(), entry->user(), entry->group(),
00187 file->data().data(), file->size() );
00188 }
00189 else
00190 {
00191 const KArchiveDirectory *dir = static_cast<const KArchiveDirectory*>( entry );
00192 kDebug( 30003 ) << "dir : " << entry->name() << endl;
00193 kDebug( 30003 ) << "full path is: " << path << entry->name() << endl;
00194
00195 QString p = path + entry->name();
00196 if ( p != "/" )
00197 {
00198 d->m_dst->writeDir( p, entry->user(), entry->group() );
00199 p.append( "/" );
00200 }
00201
00202 QStringList entries = dir->entries();
00203 QStringList::ConstIterator it = entries.begin();
00204 QStringList::ConstIterator end = entries.end();
00205 for (; it != end; ++it )
00206 copy( p, dir->entry( *it ) );
00207 }
00208 }
00209
00210 #include "KoDocumentInfoPropsPage.moc"