diff --git a/libs/ui/KisFilterChainLink.cpp b/libs/ui/KisFilterChainLink.cpp index deda7fd2ce..fc94293117 100644 --- a/libs/ui/KisFilterChainLink.cpp +++ b/libs/ui/KisFilterChainLink.cpp @@ -1,102 +1,101 @@ /* This file is part of the Calligra libraries Copyright (C) 2001 Werner Trobin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KisFilterChainLink.h" #include #include -#include #include #include "KisFilterEntry.h" #include "KisImportExportManager.h" #include "KoProgressUpdater.h" #include "KoUpdater.h" namespace { const char *const SIGNAL_PREFIX = "commSignal"; const int SIGNAL_PREFIX_LEN = 10; const char *const SLOT_PREFIX = "commSlot"; const int SLOT_PREFIX_LEN = 8; KoUpdater *createUpdater(KisFilterChain *chain) { QPointer updater = 0; Q_ASSERT(chain); Q_ASSERT(chain->manager()); KoProgressUpdater *pu = chain->manager()->progressUpdater(); if (pu) { updater = pu->startSubtask(1, "filter"); updater->setProgress(0); } return updater; } } namespace CalligraFilter { ChainLink::ChainLink(KisFilterChain *chain, KisFilterEntrySP filterEntry, const QByteArray& from, const QByteArray& to) : m_chain(chain) , m_filterEntry(filterEntry) , m_from(from) , m_to(to) , m_filter(0) , m_updater(createUpdater(chain)) { } ChainLink::~ChainLink() { } KisImportExportFilter::ConversionStatus ChainLink::invokeFilter() { if (!m_filterEntry) { errFile << "This filter entry is null. Strange stuff going on." << endl; return KisImportExportFilter::FilterEntryNull; } m_filter = m_filterEntry->createFilter(m_chain); if (!m_filter) { errFile << "Couldn't create the filter." << endl; return KisImportExportFilter::FilterCreationError; } Q_ASSERT(m_updater); if (m_updater) { // if there is an updater, use that for progress reporting m_filter->setUpdater(m_updater); } KisImportExportFilter::ConversionStatus status = m_filter->convert(m_from, m_to); delete m_filter; m_filter = 0; if (m_updater) { m_updater->setProgress(100); } return status; } void ChainLink::dump() const { dbgFile << " Link:" << m_filterEntry->loader()->fileName(); } } diff --git a/libs/ui/KisFilterGraph.cpp b/libs/ui/KisFilterGraph.cpp index 91d413c917..f9d1f09664 100644 --- a/libs/ui/KisFilterGraph.cpp +++ b/libs/ui/KisFilterGraph.cpp @@ -1,175 +1,174 @@ /* This file is part of the Calligra libraries Copyright (C) 2001 Werner Trobin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KisFilterGraph.h" #include "KisImportExportManager.h" // KisImportExportManager::filterAvailable, private API #include "KisFilterEntry.h" #include "KisDocument.h" #include "PriorityQueue_p.h" #include "KisFilterEdge.h" #include "KisFilterChainLink.h" #include "KisFilterVertex.h" #include -#include #include #include #include // UINT_MAX namespace CalligraFilter { Graph::Graph(const QByteArray& from) : m_from(from) , m_graphValid(false) , d(0) { buildGraph(); shortestPaths(); // Will return after a single lookup if "from" is invalid (->no check here) } Graph::~Graph() { Q_FOREACH (Vertex* vertex, m_vertices) { delete vertex; } m_vertices.clear(); } void Graph::setSourceMimeType(const QByteArray& from) { if (from == m_from) return; m_from = from; m_graphValid = false; // Initialize with "infinity" ... Q_FOREACH (Vertex* vertex, m_vertices) { vertex->reset(); } // ...and re-run the shortest path search for the new source mime shortestPaths(); } KisFilterChainSP Graph::chain(const KisImportExportManager* manager, QByteArray& to) const { if (!isValid() || !manager) return KisFilterChainSP(); Q_ASSERT(!to.isEmpty()); const Vertex* vertex = m_vertices.value(to); if (!vertex || vertex->key() == UINT_MAX) return KisFilterChainSP(); KisFilterChainSP ret(new KisFilterChain(manager)); // Fill the filter chain with all filters on the path const Vertex* tmp = vertex->predecessor(); while (tmp) { const Edge* const edge = tmp->findEdge(vertex); Q_ASSERT(edge); ret->prependChainLink(edge->filterEntry(), tmp->mimeType(), vertex->mimeType()); vertex = tmp; tmp = tmp->predecessor(); } return ret; } void Graph::dump() const { #ifndef NDEBUG dbgFile << "+++++++++ Graph::dump +++++++++"; dbgFile << "From:" << m_from; Q_FOREACH (Vertex *vertex, m_vertices) { vertex->dump(" "); } dbgFile << "+++++++++ Graph::dump (done) +++++++++"; #endif } // Query the trader and create the vertices and edges representing // available mime types and filters. void Graph::buildGraph() { // no constraint here - we want *all* :) const QList filters(KisFilterEntry::query()); Q_FOREACH (KisFilterEntrySP filter, filters) { // First add the "starting points" to the dict Q_FOREACH (const QString& import, filter->import) { const QByteArray key = import.toLatin1(); // latin1 is okay here (werner) // already there? if (!m_vertices.contains(key)) m_vertices.insert(key, new Vertex(key)); } Q_FOREACH (const QString& exportIt, filter->export_) { // First make sure the export vertex is in place const QByteArray key = exportIt.toLatin1(); // latin1 is okay here Vertex* exp = m_vertices.value(key); if (!exp) { exp = new Vertex(key); m_vertices.insert(key, exp); } // Then create the appropriate edges Q_FOREACH (const QString& import, filter->import) { m_vertices[import.toLatin1()]->addEdge(new Edge(exp, filter)); } } } } // As all edges (=filters) are required to have a positive weight // we can use Dijkstra's shortest path algorithm from Cormen's // "Introduction to Algorithms" (p. 527) // Note: I did some adaptions as our data structures are slightly // different from the ones used in the book. Further we simply stop // the algorithm is we don't find any node with a weight != Infinity // (==UINT_MAX), as this means that the remaining nodes in the queue // aren't connected anyway. void Graph::shortestPaths() { // Is the requested start mime type valid? Vertex* from = m_vertices.value(m_from); if (!from) return; // Inititalize start vertex from->setKey(0); // Fill the priority queue with all the vertices PriorityQueue queue(m_vertices); while (!queue.isEmpty()) { Vertex *min = queue.extractMinimum(); // Did we already relax all connected vertices? if (min->key() == UINT_MAX) break; min->relaxVertices(queue); } m_graphValid = true; } } diff --git a/libs/ui/KisImportExportFilter.cpp b/libs/ui/KisImportExportFilter.cpp index 89b7a5c785..1e37ce0273 100644 --- a/libs/ui/KisImportExportFilter.cpp +++ b/libs/ui/KisImportExportFilter.cpp @@ -1,172 +1,171 @@ /* This file is part of the KDE libraries Copyright (C) 2001 Werner Trobin 2002 Werner Trobin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KisImportExportFilter.h" #include -#include #include #include #include "KisImportExportManager.h" #include "KoUpdater.h" #include class Q_DECL_HIDDEN KisImportExportFilter::Private { public: QPointer updater; Private() : updater(0) {} }; KisImportExportFilter::KisImportExportFilter(QObject *parent) : QObject(parent) , m_chain(0) , d(new Private) { } KisDocument *KisImportExportFilter::inputDocument() const { return m_chain->inputDocument(); } KisDocument *KisImportExportFilter::outputDocument() const { return m_chain->outputDocument(); } QString KisImportExportFilter::inputFile() const { return m_chain->inputFile(); } QString KisImportExportFilter::outputFile() const { return m_chain->outputFile(); } bool KisImportExportFilter::getBatchMode() const { return m_chain->manager()->getBatchMode(); } KisImportExportFilter::~KisImportExportFilter() { Q_ASSERT(d->updater); if (d->updater) d->updater->setProgress(100); delete d; } QString KisImportExportFilter::conversionStatusString(ConversionStatus status) { QString msg; switch (status) { case OK: break; case FilterCreationError: msg = i18n("Could not create the filter plugin"); break; case CreationError: msg = i18n("Could not create the output document"); break; case FileNotFound: msg = i18n("File not found"); break; case StorageCreationError: msg = i18n("Cannot create storage"); break; case BadMimeType: msg = i18n("Bad MIME type"); break; case EmbeddedDocError: msg = i18n("Error in embedded document"); break; case WrongFormat: msg = i18n("Format not recognized"); break; case NotImplemented: msg = i18n("Not implemented"); break; case ParsingError: msg = i18n("Parsing error"); break; case PasswordProtected: msg = i18n("Document is password protected"); break; case InvalidFormat: msg = i18n("Invalid file format"); break; case InternalError: case UnexpectedEOF: case UnexpectedOpcode: case StupidError: // ?? what is this ?? case UsageError: msg = i18n("Internal error"); break; case OutOfMemory: msg = i18n("Out of memory"); break; case FilterEntryNull: msg = i18n("Empty Filter Plugin"); break; case NoDocumentCreated: msg = i18n("Trying to load into the wrong kind of document"); break; case DownloadFailed: msg = i18n("Failed to download remote file"); break; case ProgressCancelled: msg = i18n("Cancelled by user"); break; case BadConversionGraph: msg = i18n("Unknown file type"); break; case UserCancelled: // intentionally we do not prompt the error message here break; default: msg = i18n("Unknown error"); break; } return msg; } void KisImportExportFilter::setUpdater(const QPointer& updater) { Q_ASSERT(updater); if (d->updater && !updater) { disconnect(this, SLOT(slotProgress(int))); } else if (!d->updater && updater) { connect(this, SIGNAL(sigProgress(int)), SLOT(slotProgress(int))); } d->updater = updater; } void KisImportExportFilter::slotProgress(int value) { Q_ASSERT(d->updater); if (d->updater) { d->updater->setValue(value); } } diff --git a/libs/ui/KisRemoteFileFetcher.cpp b/libs/ui/KisRemoteFileFetcher.cpp index ebddbc0080..f58c456db6 100644 --- a/libs/ui/KisRemoteFileFetcher.cpp +++ b/libs/ui/KisRemoteFileFetcher.cpp @@ -1,82 +1,81 @@ /* * Copyright (c) 2016 Boudewijn Rempt * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KisRemoteFileFetcher.h" #include #include -#include #include KisRemoteFileFetcher::KisRemoteFileFetcher(QObject *parent) : QObject(parent) , m_request(0) , m_reply(0) { } KisRemoteFileFetcher::~KisRemoteFileFetcher() { delete m_request; delete m_reply; } bool KisRemoteFileFetcher::fetchFile(const QUrl &remote, QIODevice *io) { Q_ASSERT(!remote.isLocalFile()); qDebug() << "going to fetch" << remote; QNetworkAccessManager *manager = new QNetworkAccessManager(this); m_request = new QNetworkRequest(remote); m_request->setRawHeader("User-Agent", QString("Krita-%1").arg(qApp->applicationVersion()).toUtf8()); m_reply = manager->get(*m_request); connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError))); connect(m_reply, SIGNAL(finished()), &m_loop, SLOT(quit())); // Wait until done m_loop.exec(); if (m_reply->error() != QNetworkReply::NoError) { QMessageBox::critical(0, i18nc("@title:window", "Krita"), QString("Could not download %1: %2").arg(remote.toDisplayString()).arg(m_reply->errorString()), QMessageBox::Ok); return false; } if (!io->isOpen()) { io->open(QIODevice::WriteOnly); } io->write(m_reply->readAll()); io->close(); return true; } void KisRemoteFileFetcher::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { qDebug() << "bytesReceived" << bytesReceived << "bytesTotal" << bytesTotal; } void KisRemoteFileFetcher::error(QNetworkReply::NetworkError error) { Q_UNUSED(error); qDebug() << "error" << m_reply->errorString(); m_loop.quit(); }