Try other generators if opening fails with one generator

This is not very common but would happen so be a bit stubborner
This commit is contained in:
Albert Astals Cid 2016-12-08 23:29:13 +01:00
parent e81fb3d639
commit ecf54e4203
2 changed files with 48 additions and 10 deletions

View file

@ -2177,7 +2177,7 @@ QVector<KPluginMetaData> DocumentPrivate::availableGenerators()
return result; return result;
} }
KPluginMetaData DocumentPrivate::generatorForMimeType(const QMimeType& type, QWidget* widget) KPluginMetaData DocumentPrivate::generatorForMimeType(const QMimeType& type, QWidget* widget, const QVector<KPluginMetaData> &triedOffers)
{ {
// First try to find an exact match, and then look for more general ones (e. g. the plain text one) // First try to find an exact match, and then look for more general ones (e. g. the plain text one)
// Ideally we would rank these by "closeness", but that might be overdoing it // Ideally we would rank these by "closeness", but that might be overdoing it
@ -2190,6 +2190,9 @@ KPluginMetaData DocumentPrivate::generatorForMimeType(const QMimeType& type, QWi
for (const KPluginMetaData& md : available) for (const KPluginMetaData& md : available)
{ {
if (triedOffers.contains(md))
continue;
foreach (const QString& supported, md.mimeTypes()) foreach (const QString& supported, md.mimeTypes())
{ {
QMimeType mimeType = mimeDatabase.mimeTypeForName(supported); QMimeType mimeType = mimeDatabase.mimeTypeForName(supported);
@ -2320,18 +2323,53 @@ Document::OpenResult Document::openDocument(const QString & docFile, const QUrl
// 1. load Document // 1. load Document
OpenResult openResult = d->openDocumentInternal( offer, isstdin, docFile, filedata, password ); OpenResult openResult = d->openDocumentInternal( offer, isstdin, docFile, filedata, password );
if ( openResult == OpenError && !triedMimeFromFileContent ) if ( openResult == OpenError )
{ {
QMimeType newmime = db.mimeTypeForFile(docFile, QMimeDatabase::MatchExtension); QVector<KPluginMetaData> triedOffers;
triedMimeFromFileContent = true; triedOffers << offer;
if ( newmime != mime ) offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
while ( offer.isValid() )
{ {
mime = newmime; openResult = d->openDocumentInternal( offer, isstdin, docFile, filedata, password );
offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget);
if ( offer.isValid() ) if ( openResult == OpenError )
{ {
openResult = d->openDocumentInternal( offer, isstdin, docFile, filedata, password ); triedOffers << offer;
offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
} }
else break;
}
if (openResult == OpenError && !triedMimeFromFileContent )
{
QMimeType newmime = db.mimeTypeForFile(docFile, QMimeDatabase::MatchExtension);
triedMimeFromFileContent = true;
if ( newmime != mime )
{
mime = newmime;
offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
while ( offer.isValid() )
{
openResult = d->openDocumentInternal( offer, isstdin, docFile, filedata, password );
if ( openResult == OpenError )
{
triedOffers << offer;
offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
}
else break;
}
}
}
if ( openResult == OpenSuccess )
{
// Clear errors, since we're trying various generators, maybe one of them errored out
// but we finally succeeded
// TODO one can still see the error message animating out but since this is a very rare
// condition we can leave this for future work
emit error( QString(), -1 );
} }
} }
if ( openResult != OpenSuccess ) if ( openResult != OpenSuccess )

View file

@ -282,7 +282,7 @@ class DocumentPrivate
// generator selection // generator selection
static QVector<KPluginMetaData> availableGenerators(); static QVector<KPluginMetaData> availableGenerators();
static QVector<KPluginMetaData> configurableGenerators(); static QVector<KPluginMetaData> configurableGenerators();
static KPluginMetaData generatorForMimeType(const QMimeType& type, QWidget* widget); static KPluginMetaData generatorForMimeType(const QMimeType& type, QWidget* widget, const QVector<KPluginMetaData> &triedOffers = QVector<KPluginMetaData>());
}; };
class DocumentInfoPrivate class DocumentInfoPrivate