Branch: master

5a97ffda 2014-11-20 14:44:12 Timothy Pearson
Make compressed PPD loading and usage functional
M tdeprint/cups/kmcupsmanager.cpp
M tdeprint/cups/make_driver_db_cups.cpp
M tdeprint/driver.cpp
M tdeprint/driverparse.c
M tdeprint/ppdloader.cpp
diff --git a/tdeprint/cups/kmcupsmanager.cpp b/tdeprint/cups/kmcupsmanager.cpp
index 7f06e06..5271dcd 100644
--- a/tdeprint/cups/kmcupsmanager.cpp
+++ b/tdeprint/cups/kmcupsmanager.cpp
@@ -114,6 +114,8 @@
 	d.append(":/usr/local/share/foomatic/db/source");
 #else
 	d.append(":/usr/share/foomatic/db/source");
+	// compressed foomatic support
+	d.append(":/usr/lib/cups/driver/foomatic-db-compressed-ppds");
 #endif
 	return d;
 }
@@ -626,6 +628,8 @@
 {
 	if (filename.startsWith("ppd:"))
 		return loadDriverFile(filename.mid(4));
+	else if (filename.startsWith("compressed-ppd:"))
+		return loadDriverFile(filename);
 	else if (filename.startsWith("foomatic/"))
 		return loadMaticDriver(filename);
 	else
@@ -685,7 +689,7 @@
 
 DrMain* KMCupsManager::loadDriverFile(const TQString& fname)
 {
-	if (TQFile::exists(fname))
+	if ((fname.startsWith("compressed-ppd:")) || TQFile::exists(fname))
 	{
 		TQString msg; /* possible error message */
 		DrMain *driver = PPDLoader::loadDriver( fname, &msg );
@@ -704,7 +708,11 @@
 void KMCupsManager::saveDriverFile(DrMain *driver, const TQString& filename)
 {
 	kdDebug( 500 ) << "Saving PPD file with template=" << driver->get( "template" ) << endl;
-	TQIODevice *in = KFilterDev::deviceForFile( driver->get( "template" ) );
+	TQString templateFile = driver->get( "template" );
+	if (templateFile.startsWith("compressed-ppd:")) {
+		templateFile = driver->get( "temporary-cppd" );
+	}
+	TQIODevice *in = KFilterDev::deviceForFile( templateFile );
 	TQFile	out(filename);
 	if (in && in->open(IO_ReadOnly) && out.open(IO_WriteOnly))
 	{
diff --git a/tdeprint/cups/make_driver_db_cups.cpp b/tdeprint/cups/make_driver_db_cups.cpp
index 021c77b..c658181 100644
--- a/tdeprint/cups/make_driver_db_cups.cpp
+++ b/tdeprint/cups/make_driver_db_cups.cpp
@@ -276,10 +276,15 @@
 				}
 			}
 
+			manufacturer = manufacturer.stripWhiteSpace();
+			modelName = modelName.stripWhiteSpace();
+			driver = driver.stripWhiteSpace();
+
 			TQStringList driverList = TQStringList::split(",", driver, TRUE);
 			driver = driverList[0];
 			if (driver.startsWith("D")) {
 				driver = driver.mid(1);
+				driver = driver.stripWhiteSpace();
 			}
 			model = manufacturer + " " + modelName + " " + driver;
 			description = description + " [" + languageVersion + "]";
diff --git a/tdeprint/driver.cpp b/tdeprint/driver.cpp
index 6afc75d..92eb7fc 100644
--- a/tdeprint/driver.cpp
+++ b/tdeprint/driver.cpp
@@ -110,6 +110,8 @@
 	// remove a possible temporary file
 	if (has("temporary"))
 		TQFile::remove(get("temporary"));
+	if (has("temporary-cppd"))
+		TQFile::remove(get("temporary-cppd"));
 }
 
 DriverItem* DrMain::createTreeView(TQListView *parent)
diff --git a/tdeprint/driverparse.c b/tdeprint/driverparse.c
index 4f73358..891bdfe 100644
--- a/tdeprint/driverparse.c
+++ b/tdeprint/driverparse.c
@@ -117,10 +117,11 @@
 void freeFiles(void)
 {
 	int	i;
-	for (i=0; i<nfiles; i++)
+	for (i=0; i<nfiles; i++) {
 		free(files[i]);
 		free(fileorigins[i]);
 		free(filemetadata[i]);
+	}
 	free(files);
 	free(fileorigins);
 	free(filemetadata);
diff --git a/tdeprint/ppdloader.cpp b/tdeprint/ppdloader.cpp
index b829e2e..a194c1c 100644
--- a/tdeprint/ppdloader.cpp
+++ b/tdeprint/ppdloader.cpp
@@ -24,8 +24,10 @@
 #include <kfilterdev.h>
 #include <kdebug.h>
 #include <tdelocale.h>
+#include <tdetempfile.h>
 #include <tqfile.h>
 #include <math.h>
+#include <stdlib.h>
 
 void tdeprint_ppdscanner_init( TQIODevice* );
 void tdeprint_ppdscanner_terminate( bool deleteIt = true );
@@ -119,12 +121,62 @@
 
 DrMain* PPDLoader::readFromFile( const TQString& filename )
 {
+	bool ppdFilenameIsTempFile = false;
+	TQString ppdFilename = filename;
+
+	if (filename.startsWith("compressed-ppd:")) {
+		KTempFile tempFile(TQString::null, "ppd", 0600);
+		tempFile.setAutoDelete(false);
+		ppdFilename = tempFile.name();
+
+		TQStringList filenameParts = TQStringList::split(":", filename);
+		TQString databaseFilename = TQString::null;
+		TQString compressedFilename = TQString::null;
+		int i = 0;
+		for (TQStringList::Iterator it = filenameParts.begin(); it != filenameParts.end(); ++it) {
+			if (i == 1) {
+				databaseFilename = *it;
+			}
+			else if (i > 1) {
+				compressedFilename += *it;
+			}
+			i++;
+		}
+
+		TQString command = databaseFilename + " cat " + compressedFilename;
+
+		FILE* file = popen(command.ascii(), "r");
+		if (file) {
+			char * line = NULL;
+			size_t len = 0;
+			ssize_t read;
+
+			FILE* tmpFileStream = tempFile.fstream();
+
+			while ((read = getline(&line, &len, file)) != -1) {
+				fputs(line, tmpFileStream);
+			}
+			if (line) {
+				free(line);
+			}
+
+			tempFile.close();
+			pclose(file);
+		}
+		else {
+			fprintf(stderr, "Can't open driver file : %s\n", compressedFilename.ascii());
+			return 0;
+		}
+
+		ppdFilenameIsTempFile = true;
+	}
+
 	// Initialization
 	m_groups.clear();
 	m_option = NULL;
 	m_fonts.clear();
 	// Open driver file
-	TQIODevice *d = KFilterDev::deviceForFile( filename );
+	TQIODevice *d = KFilterDev::deviceForFile( ppdFilename );
 	if ( d && d->open( IO_ReadOnly ) )
 	{
 		DrMain *driver = new DrMain;
@@ -153,6 +205,9 @@
 			processPageSizes( driver );
 			if ( !m_fonts.isEmpty() )
 				driver->set( "fonts", m_fonts.join( "," ) );
+			if (ppdFilenameIsTempFile) {
+				driver->set("temporary-cppd", ppdFilename);
+			}
 			return driver;
 		}
 		else
@@ -161,7 +216,7 @@
 		m_ps.clear();
 	}
 	else
-		kdWarning( 500 ) << "PPD read error, unable to open device for file " << filename << endl;
+		kdWarning( 500 ) << "PPD read error, unable to open device for file " << ppdFilename << endl;
 	return 0;
 }