1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#include "exportmanager.h"
#include "services/pluginmanager.h"
#include "plugins/exportplugin.h"
#include "services/notifymanager.h"
#include "exportworker.h"
#include <QThreadPool>
#include <QTextCodec>
#include <QBuffer>
#include <QDebug>
#include <QDir>
#include <QFile>
ExportManager::ExportManager(QObject *parent) :
PluginServiceBase(parent)
{
}
ExportManager::~ExportManager()
{
safe_delete(config);
}
QStringList ExportManager::getAvailableFormats(ExportMode exportMode) const
{
QStringList formats;
for (ExportPlugin* plugin : PLUGINS->getLoadedPlugins<ExportPlugin>())
{
if (exportMode == UNDEFINED || plugin->getSupportedModes().testFlag(exportMode))
formats << plugin->getFormatName();
}
return formats;
}
void ExportManager::configure(const QString& format, const StandardExportConfig& config)
{
configure(format, new StandardExportConfig(config));
}
void ExportManager::configure(const QString& format, StandardExportConfig* config)
{
if (exportInProgress)
{
qWarning() << "Tried to configure export while another export is in progress.";
return;
}
plugin = getPluginForFormat(format);
if (!plugin)
{
invalidFormat(format);
return;
}
safe_delete(this->config);
this->config = config;
}
bool ExportManager::isExportInProgress() const
{
return exportInProgress;
}
void ExportManager::exportQueryResults(Db* db, const QString& query)
{
if (!checkInitialConditions())
return;
if (!plugin->getSupportedModes().testFlag(QUERY_RESULTS))
{
notifyError(tr("Export plugin %1 doesn't support exporing query results.").arg(plugin->getFormatName()));
emit exportFailed();
emit exportFinished();
return;
}
exportInProgress = true;
mode = QUERY_RESULTS;
ExportWorker* worker = prepareExport();
if (!worker)
return;
worker->prepareExportQueryResults(db, query);
QThreadPool::globalInstance()->start(worker);
}
void ExportManager::exportTable(Db* db, const QString& database, const QString& table)
{
static const QString sql = QStringLiteral("SELECT * FROM %1");
if (!checkInitialConditions())
return;
if (!plugin->getSupportedModes().testFlag(TABLE))
{
notifyError(tr("Export plugin %1 doesn't support exporing tables.").arg(plugin->getFormatName()));
emit exportFailed();
emit exportFinished();
return;
}
exportInProgress = true;
mode = TABLE;
ExportWorker* worker = prepareExport();
if (!worker)
return;
worker->prepareExportTable(db, database, table);
QThreadPool::globalInstance()->start(worker);
}
void ExportManager::exportDatabase(Db* db, const QStringList& objectListToExport)
{
if (!checkInitialConditions())
return;
if (!plugin->getSupportedModes().testFlag(DATABASE))
{
notifyError(tr("Export plugin %1 doesn't support exporing databases.").arg(plugin->getFormatName()));
emit exportFailed();
emit exportFinished();
return;
}
exportInProgress = true;
mode = DATABASE;
ExportWorker* worker = prepareExport();
if (!worker)
return;
worker->prepareExportDatabase(db, objectListToExport);
QThreadPool::globalInstance()->start(worker);
}
void ExportManager::interrupt()
{
emit orderWorkerToInterrupt();
}
ExportPlugin* ExportManager::getPluginForFormat(const QString& formatName) const
{
for (ExportPlugin* plugin : PLUGINS->getLoadedPlugins<ExportPlugin>())
if (plugin->getFormatName() == formatName)
return plugin;
return nullptr;
}
void ExportManager::invalidFormat(const QString& format)
{
notifyError(tr("Export format '%1' is not supported. Supported formats are: %2.").arg(format).arg(getAvailableFormats().join(", ")));
}
bool ExportManager::checkInitialConditions()
{
if (exportInProgress)
{
qWarning() << "Tried to call export while another export is in progress.";
emit exportFailed();
emit exportFinished();
return false;
}
if (!plugin)
{
qWarning() << "Tried to call export while no export plugin was configured.";
emit exportFailed();
emit exportFinished();
return false;
}
return true;
}
ExportWorker* ExportManager::prepareExport()
{
bool usesOutput = plugin->getSupportedModes().testFlag(FILE) || plugin->getSupportedModes().testFlag(CLIPBOARD);
QIODevice* output = nullptr;
if (usesOutput)
{
output = getOutputStream();
if (!output)
{
emit exportFailed();
emit exportFinished();
exportInProgress = false;
return nullptr;
}
}
ExportWorker* worker = new ExportWorker(plugin, config, output);
connect(worker, SIGNAL(finished(bool,QIODevice*)), this, SLOT(finalizeExport(bool,QIODevice*)));
connect(worker, SIGNAL(finishedStep(int)), this, SIGNAL(finishedStep(int)));
connect(this, SIGNAL(orderWorkerToInterrupt()), worker, SLOT(interrupt()));
return worker;
}
void ExportManager::handleClipboardExport()
{
if (plugin->getMimeType().isNull())
{
QString str = codecForName(config->codec)->toUnicode(bufferForClipboard->buffer());
emit storeInClipboard(str);
}
else
emit storeInClipboard(bufferForClipboard->buffer(), plugin->getMimeType());
}
void ExportManager::finalizeExport(bool result, QIODevice* output)
{
if (result)
{
if (config->intoClipboard)
{
notifyInfo(tr("Export to the clipboard was successful."));
handleClipboardExport();
}
else if (!config->outputFileName.isEmpty())
notifyInfo(tr("Export to the file '%1' was successful.").arg(config->outputFileName));
else
notifyInfo(tr("Export was successful."));
emit exportSuccessful();
}
else
{
emit exportFailed();
}
emit exportFinished();
if (output)
{
output->close();
delete output;
}
bufferForClipboard = nullptr;
exportInProgress = false;
}
QIODevice* ExportManager::getOutputStream()
{
QFile::OpenMode openMode;
if (config->intoClipboard)
{
openMode = QIODevice::WriteOnly;
if (!plugin->isBinaryData())
openMode |= QIODevice::Text;
bufferForClipboard = new QBuffer();
bufferForClipboard->open(openMode);
return bufferForClipboard;
}
else if (!config->outputFileName.trimmed().isEmpty())
{
openMode = QIODevice::WriteOnly|QIODevice::Truncate;
if (!plugin->isBinaryData())
openMode |= QIODevice::Text;
QFile* file = new QFile(config->outputFileName);
if (!file->open(openMode))
{
notifyError(tr("Could not export to file %1. File cannot be open for writting.").arg(config->outputFileName));
delete file;
return nullptr;
}
return file;
}
else
{
qCritical() << "ExportManager::getOutputStream(): neither clipboard or output file was specified";
}
return nullptr;
}
bool ExportManager::isAnyPluginAvailable()
{
return !PLUGINS->getLoadedPlugins<ExportPlugin>().isEmpty();
}
|