Subversion Repositories Scribus

Rev

Rev 19249 | Rev 22609 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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