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 #include "commonstrings.h"
8 
9 #include "importpub.h"
10 #include "importpubplugin.h"
11 #include "prefscontext.h"
12 #include "prefsfile.h"
13 #include "prefsmanager.h"
14 #include "scpage.h"
15 #include "scraction.h"
16 #include "scribuscore.h"
17 #include "undomanager.h"
18 #include "util_formats.h"
19 
20 #include "ui/customfdialog.h"
21 #include "ui/scmwmenumanager.h"
22 
importpub_getPluginAPIVersion()23 int importpub_getPluginAPIVersion()
24 {
25 	return PLUGIN_API_VERSION;
26 }
27 
importpub_getPlugin()28 ScPlugin* importpub_getPlugin()
29 {
30 	ImportPubPlugin* plug = new ImportPubPlugin();
31 	Q_CHECK_PTR(plug);
32 	return plug;
33 }
34 
importpub_freePlugin(ScPlugin * plugin)35 void importpub_freePlugin(ScPlugin* plugin)
36 {
37 	ImportPubPlugin* plug = qobject_cast<ImportPubPlugin*>(plugin);
38 	Q_ASSERT(plug);
39 	delete plug;
40 }
41 
ImportPubPlugin()42 ImportPubPlugin::ImportPubPlugin() :
43 	importAction(new ScrAction(ScrAction::DLL, QPixmap(), QPixmap(), "", QKeySequence(), this))
44 {
45 	// Set action info in languageChange, so we only have to do it in one
46 	// place. This includes registering file format support.
47 	registerFormats();
48 	languageChange();
49 }
50 
languageChange()51 void ImportPubPlugin::languageChange()
52 {
53 	importAction->setText( tr("Import PUB..."));
54 	FileFormat* fmt = getFormatByExt("pub");
55 	fmt->trName = tr("MS Publisher");
56 	fmt->filter = tr("MS Publisher (*.pub *.PUB)");
57 }
58 
~ImportPubPlugin()59 ImportPubPlugin::~ImportPubPlugin()
60 {
61 	unregisterAll();
62 }
63 
fullTrName() const64 QString ImportPubPlugin::fullTrName() const
65 {
66 	return QObject::tr("PUB Importer");
67 }
68 
69 
getAboutData() const70 const ScActionPlugin::AboutData* ImportPubPlugin::getAboutData() const
71 {
72 	AboutData* about = new AboutData;
73 	about->authors = "Franz Schmid <franz@scribus.info>";
74 	about->shortDescription = tr("Imports PUB Files");
75 	about->description = tr("Imports most MS Publisher files into the current document, converting their vector data into Scribus objects.");
76 	about->license = "GPL";
77 	Q_CHECK_PTR(about);
78 	return about;
79 }
80 
deleteAboutData(const AboutData * about) const81 void ImportPubPlugin::deleteAboutData(const AboutData* about) const
82 {
83 	Q_ASSERT(about);
84 	delete about;
85 }
86 
registerFormats()87 void ImportPubPlugin::registerFormats()
88 {
89 	FileFormat fmt(this);
90 	fmt.trName = tr("MS Publisher");
91 	fmt.filter = tr("MS Publisher (*.pub *.PUB)");
92 	fmt.formatId = 0;
93 	fmt.fileExtensions = QStringList() << "pub";
94 	fmt.load = true;
95 	fmt.save = false;
96 	fmt.thumb = true;
97 	fmt.colorReading = true;
98 	fmt.mimeTypes = QStringList();
99 	fmt.mimeTypes.append("application/x-mspublisher");
100 	fmt.priority = 64; // Priority
101 	registerFormat(fmt);
102 }
103 
fileSupported(QIODevice *,const QString & fileName) const104 bool ImportPubPlugin::fileSupported(QIODevice* /* file */, const QString & fileName) const
105 {
106 	return true;
107 }
108 
loadFile(const QString & fileName,const FileFormat &,int flags,int)109 bool ImportPubPlugin::loadFile(const QString & fileName, const FileFormat &, int flags, int /*index*/)
110 {
111 	// There's only one format to handle, so we just call import(...)
112 	return import(fileName, flags);
113 }
114 
import(QString fileName,int flags)115 bool ImportPubPlugin::import(QString fileName, int flags)
116 {
117 	if (!checkFlags(flags))
118 		return false;
119 	if (fileName.isEmpty())
120 	{
121 		flags |= lfInteractive;
122 		PrefsContext* prefs = PrefsManager::instance().prefsFile->getPluginContext("importpub");
123 		QString wdir = prefs->get("wdir", ".");
124 		CustomFDialog diaf(ScCore->primaryMainWindow(), wdir, QObject::tr("Open"), tr("All Supported Formats")+" (*.pub *.PUB);;All Files (*)");
125 		if (diaf.exec())
126 		{
127 			fileName = diaf.selectedFile();
128 			prefs->set("wdir", fileName.left(fileName.lastIndexOf("/")));
129 		}
130 		else
131 			return true;
132 	}
133 	m_Doc=ScCore->primaryMainWindow()->doc;
134 	UndoTransaction activeTransaction;
135 	bool emptyDoc = (m_Doc == nullptr);
136 	bool hasCurrentPage = (m_Doc && m_Doc->currentPage());
137 	TransactionSettings trSettings;
138 	trSettings.targetName   = hasCurrentPage ? m_Doc->currentPage()->getUName() : "";
139 	trSettings.targetPixmap = Um::IImageFrame;
140 	trSettings.actionName   = Um::ImportPublisher;
141 	trSettings.description  = fileName;
142 	trSettings.actionPixmap = Um::IXFIG;
143 	if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
144 		UndoManager::instance()->setUndoEnabled(false);
145 	if (UndoManager::undoEnabled())
146 		activeTransaction = UndoManager::instance()->beginTransaction(trSettings);
147 	PubPlug *dia = new PubPlug(m_Doc, flags);
148 	Q_CHECK_PTR(dia);
149 	dia->import(fileName, trSettings, flags, !(flags & lfScripted));
150 	if (activeTransaction)
151 		activeTransaction.commit();
152 	if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
153 		UndoManager::instance()->setUndoEnabled(true);
154 	delete dia;
155 	return true;
156 }
157 
readThumbnail(const QString & fileName)158 QImage ImportPubPlugin::readThumbnail(const QString& fileName)
159 {
160 	if (fileName.isEmpty())
161 		return QImage();
162 	UndoManager::instance()->setUndoEnabled(false);
163 	m_Doc = nullptr;
164 	PubPlug *dia = new PubPlug(m_Doc, lfCreateThumbnail);
165 	Q_CHECK_PTR(dia);
166 	QImage ret = dia->readThumbnail(fileName);
167 	UndoManager::instance()->setUndoEnabled(true);
168 	delete dia;
169 	return ret;
170 }
171