r17170 by fschmid - Rewrote the FileUnzip class to use QTemporaryFile.

scribus-commit scribus-commit at lists.scribus.net
Mon Jan 2 09:39:14 UTC 2012


Author: fschmid
Date: Mon Jan  2 09:39:14 2012
New Revision: 17170

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=17170
Log:
Rewrote the FileUnzip class to use QTemporaryFile.

Modified:
    trunk/Scribus/scribus/fileunzip.cpp
    trunk/Scribus/scribus/fileunzip.h
    trunk/Scribus/scribus/plugins/gettext/odtim/odtim.cpp
    trunk/Scribus/scribus/plugins/gettext/sxwim/sxwim.cpp
    trunk/Scribus/scribus/plugins/import/idml/importidmlimpl.cpp
    trunk/Scribus/scribus/plugins/import/oodraw/oodrawimp.cpp

Modified: trunk/Scribus/scribus/fileunzip.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17170&path=/trunk/Scribus/scribus/fileunzip.cpp
==============================================================================
--- trunk/Scribus/scribus/fileunzip.cpp (original)
+++ trunk/Scribus/scribus/fileunzip.cpp Mon Jan  2 09:39:14 2012
@@ -59,13 +59,14 @@
 #include <QByteArray>
 
 #include "scpaths.h"
+#include "util.h"
 
 #define CASESENSITIVITY (0)
 #define WRITEBUFFERSIZE (8192)
 #define MAXFILENAME (256)
 
-int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password);
-int do_extract_onefile(unzFile uf, const QString& filename, int opt_extract_without_path, int opt_overwrite, const char* password);
+int do_extract_currentfile(unzFile uf, QString outfile, const char *password);
+int do_extract_onefile(unzFile uf, const QString& filename, QString outfile, const char *password);
 FILE*    openfile(const QString& filename, const QString& mode);
 unzFile  unzOpenFile(const QString& filename);
 int mymkdir(const QString& dirname);
@@ -86,25 +87,30 @@
 QString FileUnzip::getFile(QString name)
 {
 	QString pwd = QDir::currentPath();
-	QString outDir = ScPaths::getTempFileDir();
-	QFile f(outDir);
-	QFileInfo fi(f);
-	if (!fi.isWritable())
-		outDir = ScPaths::getApplicationDataDir();
-	QDir::setCurrent(outDir);
+	QTemporaryFile *tempImageFile = new QTemporaryFile(QDir::tempPath() + "/scribus_temp_zip_XXXXXX.dat");
+	if (tempImageFile == NULL)
+		return NULL;
+	tempImageFile->open();
+	QString fname = getLongPathName(tempImageFile->fileName());
+	tempImageFile->close();
+	tempFileList.append(tempImageFile);
+	QDir::setCurrent(QDir::tempPath());
 	unzFile uf = unzOpenFile(zipFile);
-	int ret = do_extract_onefile(uf,name, 0, 1, NULL);
+	int ret = do_extract_onefile(uf, name, fname, NULL);
 	unzClose(uf);
 	QDir::setCurrent(pwd);
 	if (ret != 0)
 		return NULL;
 	else
-		return outDir + name;
+		return fname;
 }
 
 FileUnzip::~FileUnzip()
 {
-
+	for (int a = 0; a < tempFileList.count(); a++)
+	{
+		delete tempFileList[a];
+	}
 }
 
 FILE* openfile(const QString& filename, const QString& mode)
@@ -199,114 +205,68 @@
 	return 1;
 }
 
-int do_extract_onefile(unzFile uf, const QString& filename, int opt_extract_without_path, int opt_overwrite, const char* password)
-{
-//    int err = UNZ_OK;
+int do_extract_onefile(unzFile uf, const QString& filename, QString outfile, const char *password)
+{
 	QByteArray fname = filename.toLocal8Bit();
     if (unzLocateFile(uf,fname.data(),CASESENSITIVITY)!=UNZ_OK)
         return 2;
 
-    if (do_extract_currentfile(uf,&opt_extract_without_path,
-                                      &opt_overwrite,
-                                      password) == UNZ_OK)
+	if (do_extract_currentfile(uf, outfile, password) == UNZ_OK)
         return 0;
     else
         return 1;
 }
 
-int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int*, const char* password)
+int do_extract_currentfile(unzFile uf, QString outfile, const char* password)
 {
 	char  fn_inzip[256];
 	QString filename_inzip;
-	QString filename_withoutpath;
+	QString write_filename;
 	int err=UNZ_OK;
 	FILE *fout=NULL;
 	void* buf;
 	uInt size_buf;
-
 	unz_file_info file_info;
-	//uLong ratio=0;
-	err = unzGetCurrentFileInfo(uf,&file_info,fn_inzip,sizeof(fn_inzip),NULL,0,NULL,0);
-
+	err = unzGetCurrentFileInfo(uf, &file_info, fn_inzip, sizeof(fn_inzip), NULL, 0, NULL, 0);
 	if (err!=UNZ_OK)
 		return err;
-
 	size_buf = WRITEBUFFERSIZE;
 	buf = (void*)malloc(size_buf);
-	if (buf==NULL)
+	if (buf == NULL)
 		return UNZ_INTERNALERROR;
-
-	QChar p;
-//	uint cIndex = 0;
-	filename_inzip = QString::fromLocal8Bit(fn_inzip); 
-	filename_withoutpath = filename_inzip;
-	for (int i = 0; i < filename_inzip.length(); i++)
+	filename_inzip = QString::fromLocal8Bit(fn_inzip);
+	write_filename = outfile;
+	err = unzOpenCurrentFilePassword(uf, password);
+	if (err == UNZ_OK)
 	{
-		p = filename_inzip[i];
-		if( (p == '/') || (p =='\\'))
-			filename_withoutpath = filename_inzip.mid(i+1);
-	}
-
-	if (filename_withoutpath.isEmpty())
-	{
-		if ((*popt_extract_without_path)==0)
-			mymkdir(filename_inzip);
-	}
-	else
-    {
-		QString write_filename;
-		int skip=0;
-
-		if ((*popt_extract_without_path)==0)
-			write_filename = filename_inzip;
-		else
-			write_filename = filename_withoutpath;
-		
-		err = unzOpenCurrentFilePassword(uf,password);
-
-		if ((skip==0) && (err==UNZ_OK))
-		{
-			fout = openfile(write_filename, "wb");
-            /* some zipfile don't contain directory alone before file */
-			if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
-								(filename_withoutpath != filename_inzip))
-			{
-				uint len = filename_inzip.length() - filename_withoutpath.length() - 1;
-				QString write_dir = filename_inzip.left(len);
-				makedir(write_dir);
-				fout = openfile(write_filename, "wb");
-			}
-		}
-
-		if (fout!=NULL)
+		fout = openfile(write_filename, "wb");
+		if (fout != NULL)
 		{
 			do
 			{
-				err = unzReadCurrentFile(uf,buf,size_buf);
+				err = unzReadCurrentFile(uf, buf, size_buf);
 				if (err<0)
 					break;
 				if (err>0)
+				{
 					if (fwrite(buf,err,1,fout)!=1)
 					{
 						err=UNZ_ERRNO;
 						break;
 					}
+				}
 			}
 			while (err>0);
 			if (fout)
 				fclose(fout);
-
-			if (err==0)
-				change_file_date(write_filename,file_info.dosDate,
-									file_info.tmu_date);
+			if (err == 0)
+				change_file_date(write_filename,file_info.dosDate, file_info.tmu_date);
 		}
-
-		if (err==UNZ_OK)
-			err = unzCloseCurrentFile (uf);
+		if (err == UNZ_OK)
+			err = unzCloseCurrentFile(uf);
 		else
 			unzCloseCurrentFile(uf); /* don't lose the error */
 	}
-
 	free(buf);
 	return err;
 }

Modified: trunk/Scribus/scribus/fileunzip.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17170&path=/trunk/Scribus/scribus/fileunzip.h
==============================================================================
--- trunk/Scribus/scribus/fileunzip.h (original)
+++ trunk/Scribus/scribus/fileunzip.h Mon Jan  2 09:39:14 2012
@@ -30,6 +30,8 @@
 #include "scconfig.h"
 #include "scribusapi.h"
 #include <QString>
+#include <QTemporaryFile>
+#include <QList>
 
 /**
  * @brief Extract files from a zip archive.
@@ -42,6 +44,7 @@
 {
 private:
 	QString zipFile;
+	QList<QTemporaryFile*> tempFileList;
 public:
 	FileUnzip(QString zipFilePath);
 	~FileUnzip();

Modified: trunk/Scribus/scribus/plugins/gettext/odtim/odtim.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17170&path=/trunk/Scribus/scribus/plugins/gettext/odtim/odtim.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/gettext/odtim/odtim.cpp (original)
+++ trunk/Scribus/scribus/plugins/gettext/odtim/odtim.cpp Mon Jan  2 09:39:14 2012
@@ -94,7 +94,6 @@
 	FileUnzip* fun = new FileUnzip(fileName);
 	stylePath   = fun->getFile(STYLE);
 	contentPath = fun->getFile(CONTENT);
-	delete fun;
 	// Qt4 NULL -> isNull()
 	if ((!stylePath.isNull()) && (!contentPath.isNull()))
 	{
@@ -106,21 +105,8 @@
 		creader->parse(contentPath);
 		delete sreader;
 		delete creader;
-		QFile f1(stylePath);
-		f1.remove();
-		QFile f2(contentPath);
-		f2.remove();
 	}
-	else if ((stylePath.isNull()) && (!contentPath.isNull()))
-	{
-		QFile f2(contentPath);
-		f2.remove();
-	}
-	else if ((!stylePath.isNull()) && (contentPath.isNull()))
-	{
-		QFile f1(stylePath);
-		f1.remove();
-	}
+	delete fun;
 }
 
 OdtIm::~OdtIm()

Modified: trunk/Scribus/scribus/plugins/gettext/sxwim/sxwim.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17170&path=/trunk/Scribus/scribus/plugins/gettext/sxwim/sxwim.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/gettext/sxwim/sxwim.cpp (original)
+++ trunk/Scribus/scribus/plugins/gettext/sxwim/sxwim.cpp Mon Jan  2 09:39:14 2012
@@ -92,7 +92,6 @@
 	FileUnzip* fun = new FileUnzip(fileName);
 	stylePath   = fun->getFile(STYLE);
 	contentPath = fun->getFile(CONTENT);
-	delete fun;
 	// Qt4 NULL -> isNull()
 	if ((!stylePath.isNull()) && (!contentPath.isNull()))
 	{
@@ -104,21 +103,8 @@
 		creader->parse(contentPath);
 		delete sreader;
 		delete creader;
-		QFile f1(stylePath);
-		f1.remove();
-		QFile f2(contentPath);
-		f2.remove();
 	}
-	else if ((stylePath.isNull()) && (!contentPath.isNull()))
-	{
-		QFile f2(contentPath);
-		f2.remove();
-	}
-	else if ((!stylePath.isNull()) && (contentPath.isNull()))
-	{
-		QFile f1(stylePath);
-		f1.remove();
-	}
+	delete fun;
 }
 
 SxwIm::~SxwIm()

Modified: trunk/Scribus/scribus/plugins/import/idml/importidmlimpl.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17170&path=/trunk/Scribus/scribus/plugins/import/idml/importidmlimpl.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/import/idml/importidmlimpl.cpp (original)
+++ trunk/Scribus/scribus/plugins/import/idml/importidmlimpl.cpp Mon Jan  2 09:39:14 2012
@@ -95,7 +95,10 @@
 		bool success = preference.processFile(root);
 		root.clear();
 		if(!success)
+		{
+			delete idmlFile;
 			return false;
+		}
 	}
 	docWidth = preference.preference.PageWidth.toDouble();
 	docHeight = preference.preference.PageHeight.toDouble();
@@ -122,36 +125,52 @@
 		for( int i=0;i<MasterSpreads.count();i++ )
 		mspreadPath.append(idmlFile->getFile(MasterSpreads.at(i)));
 		if(!success)
+		{
+			delete idmlFile;
 			return false;
+		}
 	}
 	if(!fontPath.isNull())
 	{
 		bool success = processFont();
 		root.clear();
 		if(!success)
+		{
+			delete idmlFile;
 			return false;
+		}
 	}
 	if(!stylePath.isEmpty())
 	{
 		bool success = processStyles();
 		root.clear();
 		if(!success)
+		{
+			delete idmlFile;
 			return false;
+		}
 	}
 	if(!storyPath.isEmpty())
 	{
 		bool success = processStory();
 		root.clear();
 		if(!success)
+		{
+			delete idmlFile;
 			return false;
+		}
 	}
 	if(!spreadPath.isEmpty())
 	{
 		bool success = processSpreads();
 		root.clear();
 		if(!success)
+		{
+			delete idmlFile;
 			return false;
-	}
+		}
+	}
+	delete idmlFile;
 	return true;
 }
 

Modified: trunk/Scribus/scribus/plugins/import/oodraw/oodrawimp.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17170&path=/trunk/Scribus/scribus/plugins/import/oodraw/oodrawimp.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/import/oodraw/oodrawimp.cpp (original)
+++ trunk/Scribus/scribus/plugins/import/oodraw/oodrawimp.cpp Mon Jan  2 09:39:14 2012
@@ -258,7 +258,6 @@
 	stylePath   = fun->getFile("styles.xml");
 	contentPath = fun->getFile("content.xml");
 	metaPath = fun->getFile("meta.xml");
-	delete fun;
 	// Qt4 NULL -> isNull()
 	if ((!stylePath.isNull()) && (!contentPath.isNull()))
 	{
@@ -266,31 +265,19 @@
 		QString docname = fileName.right(fileName.length() - fileName.lastIndexOf("/") - 1);
 		docname = docname.left(docname.lastIndexOf("."));
 		loadRawText(stylePath, f);
-		if(!inpStyles.setContent(f))
-			return QImage();
 		loadRawText(contentPath, f2);
-		if(!inpContents.setContent(f2))
-			return QImage();
-		QFile::remove(stylePath);
-		QFile::remove(contentPath);
 		HaveMeta = false;
 		if (!metaPath.isEmpty())
 		{
 			loadRawText(metaPath, f3);
 			HaveMeta = inpMeta.setContent(f3);
-			QFile::remove(f3);
-		}
-	}
-	else if ((stylePath.isNull()) && (!contentPath.isNull()))
-	{
-		QFile f2(contentPath);
-		f2.remove();
-	}
-	else if ((!stylePath.isNull()) && (contentPath.isNull()))
-	{
-		QFile f1(stylePath);
-		f1.remove();
-	}
+		}
+	}
+	delete fun;
+	if(!inpStyles.setContent(f))
+		return QImage();
+	if(!inpContents.setContent(f2))
+		return QImage();
 	QString CurDirP = QDir::currentPath();
 	QFileInfo efp(fileName);
 	QDir::setCurrent(efp.path());
@@ -390,12 +377,10 @@
 	QByteArray f, f2, f3;
 	if ( !QFile::exists(fileName) )
 		return false;
-// 	m_styles.setAutoDelete( true );
 	FileUnzip* fun = new FileUnzip(fileName);
 	stylePath   = fun->getFile("styles.xml");
 	contentPath = fun->getFile("content.xml");
 	metaPath = fun->getFile("meta.xml");
-	delete fun;
 	// Qt4 NULL -> isNull()
 	if ((!stylePath.isNull()) && (!contentPath.isNull()))
 	{
@@ -403,31 +388,19 @@
 		QString docname = fileName.right(fileName.length() - fileName.lastIndexOf("/") - 1);
 		docname = docname.left(docname.lastIndexOf("."));
 		loadRawText(stylePath, f);
-		if(!inpStyles.setContent(f))
-			return false;
 		loadRawText(contentPath, f2);
-		if(!inpContents.setContent(f2))
-			return false;
-		QFile::remove(stylePath);
-		QFile::remove(contentPath);
 		HaveMeta = false;
 		if (!metaPath.isEmpty())
 		{
 			loadRawText(metaPath, f3);
 			HaveMeta = inpMeta.setContent(f3);
-			QFile::remove(f3);
-		}
-	}
-	else if ((stylePath.isNull()) && (!contentPath.isNull()))
-	{
-		QFile f2(contentPath);
-		f2.remove();
-	}
-	else if ((!stylePath.isNull()) && (contentPath.isNull()))
-	{
-		QFile f1(stylePath);
-		f1.remove();
-	}
+		}
+	}
+	delete fun;
+	if(!inpStyles.setContent(f))
+		return false;
+	if(!inpContents.setContent(f2))
+		return false;
 	QString CurDirP = QDir::currentPath();
 	QFileInfo efp(fileName);
 	QDir::setCurrent(efp.path());




More information about the scribus-commit mailing list