1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 
8 #include "uniconvplugin.h"
9 #include "scraction.h"
10 #include "util_formats.h"
11 #include "prefsmanager.h"
12 
13 #include <QDebug>
14 #include <QMessageBox>
15 #include <QString>
16 #include <QProcess>
17 #include <QTemporaryFile>
18 
19 
20 #include <QCursor>
21 #include <QDrag>
22 #include <QFile>
23 #include <QList>
24 #include <QMimeData>
25 #include <QPainterPath>
26 #include <QRegExp>
27 #include <cmath>
28 
29 #include "commonstrings.h"
30 #include "scribuscore.h"
31 #include "ui/scmessagebox.h"
32 #include "undomanager.h"
33 
uniconvertorplugin_getPluginAPIVersion()34 int uniconvertorplugin_getPluginAPIVersion()
35 {
36 	return PLUGIN_API_VERSION;
37 }
38 
uniconvertorplugin_getPlugin()39 ScPlugin* uniconvertorplugin_getPlugin()
40 {
41 	UniconvImportPlugin* plug = new UniconvImportPlugin();
42 	Q_CHECK_PTR(plug);
43 	return plug;
44 }
45 
uniconvertorplugin_freePlugin(ScPlugin * plugin)46 void uniconvertorplugin_freePlugin(ScPlugin* plugin)
47 {
48 	UniconvImportPlugin* plug = qobject_cast<UniconvImportPlugin*>(plugin);
49 	Q_ASSERT(plug);
50 	delete plug;
51 }
52 
UniconvImportPlugin()53 UniconvImportPlugin::UniconvImportPlugin()
54 {
55 	// Set action info in languageChange, so we only have to do
56 	// it in one place. This includes registering file format
57 	// support.
58 	registerFormats();
59 	languageChange();
60 }
61 
~UniconvImportPlugin()62 UniconvImportPlugin::~UniconvImportPlugin()
63 {
64 	unregisterAll();
65 }
66 
languageChange()67 void UniconvImportPlugin::languageChange()
68 {
69 	QString name = tr("Uniconvertor Import");
70 	FileFormat* fmt = getFormatByExt("cdt");
71 	fmt->trName = name;
72 	fmt->filter = name + " (" +  FormatsManager::instance()->extensionListForFormat( FormatsManager::UNICONV, 0)+")";
73 }
74 
fullTrName() const75 QString UniconvImportPlugin::fullTrName() const
76 {
77 	return QObject::tr("Uniconvertor Import");
78 }
79 
getAboutData() const80 const ScActionPlugin::AboutData* UniconvImportPlugin::getAboutData() const
81 {
82 	AboutData* about = new AboutData;
83 	about->authors = "Hermann Kraus <herm@scribus.info>";
84 	about->shortDescription = tr("Imports Vector Files with UniConvertor");
85 	about->description = tr("Converts many vector formats to SVG and then "
86 		"loads the resulting SVG.");
87 	about->license = "GPL";
88 	Q_CHECK_PTR(about);
89 	return about;
90 }
91 
deleteAboutData(const AboutData * about) const92 void UniconvImportPlugin::deleteAboutData(const AboutData* about) const
93 {
94 	Q_ASSERT(about);
95 	delete about;
96 }
97 
registerFormats()98 void UniconvImportPlugin::registerFormats()
99 {
100 	QString name = tr("Uniconvertor Import");
101 	FileFormat fmt(this);
102 	fmt.trName = name;
103 	fmt.formatId = 0;
104 	fmt.filter = name + " (" +  FormatsManager::instance()->extensionListForFormat( FormatsManager::UNICONV, 0)+")"; // QFileDialog filter
105 	fmt.fileExtensions = QStringList() << "cdt" << "ccx" << "cmx" <<"aff" << "sk" << "sk1" << "plt" << "dxf" << "dst" << "pes" << "exp" << "pcs";
106 	fmt.load = true;
107 	fmt.save = false;
108 	//TODO: fmt.mimeTypes = QStringList(""); // MIME types
109 	fmt.priority = 64; // Lowest priority
110 	registerFormat(fmt);
111 }
112 
fileSupported(QIODevice *,const QString & fileName) const113 bool UniconvImportPlugin::fileSupported(QIODevice* /* file */,
114 	const QString & fileName) const
115 {
116 	// TODO: It's hard to tell if a file is supported without first loading it
117 	return true;
118 }
119 
loadFile(const QString & fileName,const FileFormat &,int flags,int)120 bool UniconvImportPlugin::loadFile(const QString & fileName,
121 	const FileFormat & /* fmt */, int flags, int /*index*/)
122 {
123 	// For now, "load file" and import are the same thing for this plugin
124 	return import(fileName, flags);
125 }
126 
import(const QString & fileName,int flags)127 bool UniconvImportPlugin::import(const QString& fileName, int flags)
128 {
129 	if (!checkFlags(flags))
130 		return false;
131 
132 	m_Doc = ScCore->primaryMainWindow()->doc;
133 	ScribusMainWindow *mw =
134 		(m_Doc==nullptr) ? ScCore->primaryMainWindow() : m_Doc->scMW();
135 
136 	//Get a temporary filename ending in .svg (sadly
137 	//uniconvertor has no other way of specifying the output format
138 	QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + "/scribus_uniconv_XXXXXX.svg");
139 	tempFile->open();
140 	QString tempFileName = tempFile->fileName();
141 	tempFile->close();
142 
143 	//prepare arguments for uniconvertor call
144 	QStringList arguments;
145 	arguments << fileName <<  tempFileName;
146 
147 	//execute uniconvertor
148 	QProcess uniconv;
149 	uniconv.setProcessChannelMode(QProcess::MergedChannels);
150 	uniconv.start(PrefsManager::instance().uniconvExecutable(), arguments);
151 
152 	//handle errors
153 	if (!uniconv.waitForStarted(120000)) {
154 		qWarning() << "Uniconvertor failed:" <<
155 			PrefsManager::instance().uniconvExecutable() << arguments;
156 		ScMessageBox::warning(mw, CommonStrings::trWarning,
157 			tr("Starting Uniconvertor failed! The executable name in "
158 			"File->Preferences->External Tools may be incorrect or the "
159 			"software has been uninstalled since preferences "
160 			"were set. (%1)").arg(uniconv.errorString()));
161 		delete tempFile;
162 		return false;
163 	}
164 	if (!uniconv.waitForFinished(120000)) {
165 		qDebug() << "Uniconv exit code:" << uniconv.exitCode();
166 		ScMessageBox::warning(mw, CommonStrings::trWarning,
167 			tr("Uniconvertor did not exit correctly: %1").arg(uniconv.errorString(), QString(uniconv.readAll())));
168 		delete tempFile;
169 		return false;
170 	}
171 	if (uniconv.exitCode()) {
172 		qDebug() << "Uniconv exit code:" << uniconv.exitCode();
173 		ScMessageBox::warning(mw, CommonStrings::trWarning,
174 			tr("Uniconvertor failed to convert the file: %1").arg(QString(uniconv.readAll())));
175 		delete tempFile;
176 		return false;
177 	}
178 
179 	//Import SVG
180 	const FileFormat *fmt = LoadSavePlugin::getFormatByExt("svg");
181 	if (!fmt) {
182 		ScMessageBox::warning(mw, CommonStrings::trWarning, tr("The SVG Import plugin could not be found"));
183 		delete tempFile;
184 		return false;
185 	}
186 	fmt->loadFile(tempFileName, flags);
187 	delete tempFile;
188 	return true;
189 }
190