Commit fba90677 authored by Albert Astals Cid's avatar Albert Astals Cid

Fine tune mime type detection used for opening files

If the argument mimetype and filepath mimetype disagree, first try to use the filepath mimetype and if that fails use the argument one. On top of that if text/plain is the first, first try with content mimetype since the text backend never fails to open a file

I can now open https://bugs.freedesktop.org/attachment.cgi?id=99508 (which is served as text/plain) and PS files with .txt extension (bug 325044)

REVIEW: 119737
BUGS: 325044
FIXED-IN: 4.14.0
parent 8aa921d2
......@@ -1219,34 +1219,26 @@ static void addFileToWatcher( KDirWatch *watcher, const QString &filePath)
if ( fi.isSymLink() ) watcher->addFile( fi.readLink() );
}
bool Part::openFile()
Document::OpenResult Part::doOpenFile( const KMimeType::Ptr &mimeA, const QString &fileNameToOpenA, bool *isCompressedFile )
{
KMimeType::Ptr mime;
QString fileNameToOpen = localFilePath();
const bool isstdin = url().isLocalFile() && url().fileName( KUrl::ObeyTrailingSlash ) == QLatin1String( "-" );
const QFileInfo fileInfo( fileNameToOpen );
if ( !isstdin && !fileInfo.exists() )
return false;
if ( !arguments().mimeType().isEmpty() )
{
mime = KMimeType::mimeType( arguments().mimeType() );
}
if ( !mime )
{
mime = KMimeType::findByPath( fileNameToOpen );
}
bool isCompressedFile = false;
Document::OpenResult openResult = Document::OpenError;
bool uncompressOk = true;
KMimeType::Ptr mime = mimeA;
QString fileNameToOpen = fileNameToOpenA;
QString compressedMime = compressedMimeFor( mime->name() );
if ( compressedMime.isEmpty() )
compressedMime = compressedMimeFor( mime->parentMimeType() );
if ( !compressedMime.isEmpty() )
{
isCompressedFile = true;
*isCompressedFile = true;
uncompressOk = handleCompressed( fileNameToOpen, localFilePath(), compressedMime );
mime = KMimeType::findByPath( fileNameToOpen );
}
Document::OpenResult openResult = Document::OpenError;
else
{
*isCompressedFile = false;
}
isDocumentArchive = false;
if ( uncompressOk )
{
......@@ -1331,6 +1323,56 @@ bool Part::openFile()
}
}
return openResult;
}
bool Part::openFile()
{
QList<KMimeType::Ptr> mimes;
QString fileNameToOpen = localFilePath();
const bool isstdin = url().isLocalFile() && url().fileName( KUrl::ObeyTrailingSlash ) == QLatin1String( "-" );
const QFileInfo fileInfo( fileNameToOpen );
if ( !isstdin && !fileInfo.exists() )
return false;
KMimeType::Ptr pathMime = KMimeType::findByPath( fileNameToOpen );
if ( !arguments().mimeType().isEmpty() )
{
KMimeType::Ptr argMime = KMimeType::mimeType( arguments().mimeType() );
// Select the "childmost" mimetype, if none of them
// inherits the other trust more what pathMime says
// but still do a second try if that one fails
if ( argMime->is( pathMime->name() ) )
{
mimes << argMime;
}
else if ( pathMime->is( argMime->name() ) )
{
mimes << pathMime;
}
else
{
mimes << pathMime << argMime;
}
if (mimes[0]->name() == "text/plain") {
KMimeType::Ptr contentMime = KMimeType::findByFileContent( fileNameToOpen );
mimes.prepend( contentMime );
}
}
else
{
mimes << pathMime;
}
KMimeType::Ptr mime;
Document::OpenResult openResult = Document::OpenError;
bool isCompressedFile = false;
while ( !mimes.isEmpty() && openResult == Document::OpenError ) {
mime = mimes.takeFirst();
openResult = doOpenFile( mime, fileNameToOpen, &isCompressedFile );
}
bool canSearch = m_document->supportsSearching();
emit mimeTypeChanged( mime );
......
......@@ -234,6 +234,8 @@ class OKULAR_PART_EXPORT Part : public KParts::ReadWritePart, public Okular::Doc
void noticeMessage( const QString &message, int duration = -1 );
private:
Document::OpenResult doOpenFile(const KMimeType::Ptr &mime, const QString &fileNameToOpen, bool *isCompressedFile);
void setupViewerActions();
void setViewerShortcuts();
void setupActions();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment