generic: adjust to KMimeType changes

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-04 20:19:23 +03:00
parent c361c2533e
commit 3cf815c995
20 changed files with 32 additions and 56 deletions

View file

@ -45,24 +45,11 @@ static bool comparePlugins(const KService::Ptr &p1, const KService::Ptr &p2)
return (p1->property(QLatin1String( "X-KDE-Priority" )).toInt()) > (p2->property(QLatin1String( "X-KDE-Priority" )).toInt()); return (p1->property(QLatin1String( "X-KDE-Priority" )).toInt()) > (p2->property(QLatin1String( "X-KDE-Priority" )).toInt());
} }
static QString determineMimeType(const QString& filename)
{
if (!QFile::exists(filename)) {
return KMimeType::findByPath(filename)->name();
}
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
return QString();
}
return KMimeType::findByNameAndContent(filename, &file)->name();
}
static KService::List findPluginOffers(const QString& filename, const QString& fixedMimeType) static KService::List findPluginOffers(const QString& filename, const QString& fixedMimeType)
{ {
KService::List offers; KService::List offers;
const QString mimeType = fixedMimeType.isEmpty() ? determineMimeType(filename) : fixedMimeType; const QString mimeType = fixedMimeType.isEmpty() ? KMimeType::findByUrl(KUrl(filename))->name() : fixedMimeType;
if (!mimeType.isEmpty()) { if (!mimeType.isEmpty()) {
offers = KMimeTypeTrader::self()->query(mimeType, QLatin1String( "Kerfuffle/Plugin" ), QLatin1String( "(exist Library)" )); offers = KMimeTypeTrader::self()->query(mimeType, QLatin1String( "Kerfuffle/Plugin" ), QLatin1String( "(exist Library)" ));

View file

@ -80,7 +80,7 @@ public:
if (entry[IsDirectory].toBool()) { if (entry[IsDirectory].toBool()) {
m_icon = KIconLoader::global()->loadMimeTypeIcon(KMimeType::mimeType(QLatin1String("inode/directory"))->iconName(), KIconLoader::Small); m_icon = KIconLoader::global()->loadMimeTypeIcon(KMimeType::mimeType(QLatin1String("inode/directory"))->iconName(), KIconLoader::Small);
} else { } else {
const KMimeType::Ptr mimeType = KMimeType::findByPath(m_entry[FileName].toString(), 0, true); const KMimeType::Ptr mimeType = KMimeType::findByUrl(KUrl(m_entry[FileName].toString()));
m_icon = KIconLoader::global()->loadMimeTypeIcon(mimeType->iconName(), KIconLoader::Small); m_icon = KIconLoader::global()->loadMimeTypeIcon(mimeType->iconName(), KIconLoader::Small);
} }
} }

View file

@ -88,7 +88,8 @@ void ArkViewer::dialogClosed()
void ArkViewer::view(const QString& fileName, QWidget *parent) void ArkViewer::view(const QString& fileName, QWidget *parent)
{ {
KMimeType::Ptr mimeType = KMimeType::findByPath(fileName); const KUrl fileNameUrl = KUrl(fileName);
KMimeType::Ptr mimeType = KMimeType::findByUrl(fileNameUrl);
kDebug() << "MIME type" << mimeType->name(); kDebug() << "MIME type" << mimeType->name();
KService::Ptr viewer = ArkViewer::getViewer(mimeType); KService::Ptr viewer = ArkViewer::getViewer(mimeType);
@ -99,7 +100,7 @@ void ArkViewer::view(const QString& fileName, QWidget *parent)
// So there is no point in using KRun::runUrl() which would need // So there is no point in using KRun::runUrl() which would need
// to do the same again. // to do the same again.
const KUrl::List fileUrlList = KUrl(fileName); const KUrl::List fileUrlList = fileNameUrl;
// The last argument (tempFiles) set to true means that the temporary // The last argument (tempFiles) set to true means that the temporary
// file will be removed when the viewer application exits. // file will be removed when the viewer application exits.
KRun::run(*viewer, fileUrlList, parent, true); KRun::run(*viewer, fileUrlList, parent, true);

View file

@ -104,7 +104,7 @@ void InfoPanel::setIndex(const QModelIndex& index)
if (entry[ IsDirectory ].toBool()) { if (entry[ IsDirectory ].toBool()) {
mimeType = KMimeType::mimeType(QLatin1String( "inode/directory" )); mimeType = KMimeType::mimeType(QLatin1String( "inode/directory" ));
} else { } else {
mimeType = KMimeType::findByPath(entry[ FileName ].toString(), 0, true); mimeType = KMimeType::findByUrl(KUrl(entry[ FileName ].toString()));
} }
iconWidget->setPixmap(getMimeIcon(mimeType->iconName())); iconWidget->setPixmap(getMimeIcon(mimeType->iconName()));
@ -186,7 +186,7 @@ QString InfoPanel::metadataTextFor(const QModelIndex &index)
if (entry[ IsDirectory ].toBool()) { if (entry[ IsDirectory ].toBool()) {
mimeType = KMimeType::mimeType(QLatin1String( "inode/directory" )); mimeType = KMimeType::mimeType(QLatin1String( "inode/directory" ));
} else { } else {
mimeType = KMimeType::findByPath(entry[ FileName ].toString(), 0, true); mimeType = KMimeType::findByUrl(KUrl(entry[ FileName ].toString()));
} }
text += i18n("<b>Type:</b> %1<br/>", mimeType->comment()); text += i18n("<b>Type:</b> %1<br/>", mimeType->comment());

View file

@ -87,10 +87,10 @@ struct LoadingDocumentImplPrivate
{ {
QString mimeType; QString mimeType;
const KUrl& url = q->document()->url(); const KUrl& url = q->document()->url();
if (KProtocolInfo::determineMimetypeFromExtension(url.protocol())) { if (!mData.isEmpty()) {
mimeType = KMimeType::findByNameAndContent(url.fileName(), mData)->name();
} else {
mimeType = KMimeType::findByContent(mData)->name(); mimeType = KMimeType::findByContent(mData)->name();
} else {
mimeType = KMimeType::findByUrl(url)->name();
} }
MimeTypeUtils::Kind kind = MimeTypeUtils::mimeTypeKind(mimeType); MimeTypeUtils::Kind kind = MimeTypeUtils::mimeTypeKind(mimeType);
kDebug() << "mimeType:" << mimeType << ", kind:" << kind; kDebug() << "mimeType:" << mimeType << ", kind:" << kind;

View file

@ -128,7 +128,7 @@ bool KSnapshotObject::save( const KUrl& url, QWidget *widget )
bool KSnapshotObject::saveEqual( const KUrl& url,QWidget *widget ) bool KSnapshotObject::saveEqual( const KUrl& url,QWidget *widget )
{ {
QByteArray type = "PNG"; QByteArray type = "PNG";
QString mime = KMimeType::findByUrl( url.fileName(), 0, url.isLocalFile(), true )->name(); QString mime = KMimeType::findByUrl( url )->name();
const QString formatType = KImageIO::typeForMime(mime, KImageIO::Writing); const QString formatType = KImageIO::typeForMime(mime, KImageIO::Writing);
if ( !formatType.isEmpty() ) if ( !formatType.isEmpty() )
type = formatType.toLatin1(); type = formatType.toLatin1();

View file

@ -1994,25 +1994,13 @@ Document::OpenResult Document::openDocument( const QString & docFile, const KUrl
KService::List offers = KMimeTypeTrader::self()->query(mime->name(),"okular/Generator",constraint); KService::List offers = KMimeTypeTrader::self()->query(mime->name(),"okular/Generator",constraint);
if ( offers.isEmpty() && !triedMimeFromFileContent ) if ( offers.isEmpty() && !triedMimeFromFileContent )
{ {
KMimeType::Ptr newmime = KMimeType::findByFileContent( docFile );
triedMimeFromFileContent = true; triedMimeFromFileContent = true;
KMimeType::Ptr newmime = KMimeType::findByUrl( docFile );
if ( newmime->name() != mime->name() ) if ( newmime->name() != mime->name() )
{ {
mime = newmime; mime = newmime;
offers = KMimeTypeTrader::self()->query( mime->name(), "okular/Generator", constraint ); offers = KMimeTypeTrader::self()->query( mime->name(), "okular/Generator", constraint );
} }
if ( offers.isEmpty() )
{
// There's still no offers, do a final mime search based on the filename
// We need this because sometimes (e.g. when downloading from a webserver) the mimetype we
// use is the one fed by the server, that may be wrong
newmime = KMimeType::findByUrl( docFile );
if ( newmime->name() != mime->name() )
{
mime = newmime;
offers = KMimeTypeTrader::self()->query( mime->name(), "okular/Generator", constraint );
}
}
} }
if (offers.isEmpty()) if (offers.isEmpty())
{ {
@ -2050,7 +2038,7 @@ Document::OpenResult Document::openDocument( const QString & docFile, const KUrl
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 && !triedMimeFromFileContent )
{ {
KMimeType::Ptr newmime = KMimeType::findByFileContent( docFile ); KMimeType::Ptr newmime = KMimeType::findByUrl( docFile );
triedMimeFromFileContent = true; triedMimeFromFileContent = true;
if ( newmime->name() != mime->name() ) if ( newmime->name() != mime->name() )
{ {
@ -3407,7 +3395,7 @@ void Document::processAction( const Action * action )
// Albert: the only pdf i have that has that kind of link don't define // Albert: the only pdf i have that has that kind of link don't define
// an application and use the fileName as the file to open // an application and use the fileName as the file to open
fileName = d->giveAbsolutePath( fileName ); fileName = d->giveAbsolutePath( fileName );
KMimeType::Ptr mime = KMimeType::findByPath( fileName ); KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(fileName) );
// Check executables // Check executables
if ( KRun::isExecutableFile( fileName, mime->name() ) ) if ( KRun::isExecutableFile( fileName, mime->name() ) )
{ {
@ -3415,7 +3403,7 @@ void Document::processAction( const Action * action )
if ( !exe->parameters().isEmpty() ) if ( !exe->parameters().isEmpty() )
{ {
fileName = d->giveAbsolutePath( exe->parameters() ); fileName = d->giveAbsolutePath( exe->parameters() );
mime = KMimeType::findByPath( fileName ); mime = KMimeType::findByUrl( KUrl(fileName) );
if ( KRun::isExecutableFile( fileName, mime->name() ) ) if ( KRun::isExecutableFile( fileName, mime->name() ) )
{ {
// this case is a link pointing to an executable with a parameter // this case is a link pointing to an executable with a parameter
@ -3840,7 +3828,7 @@ QByteArray Document::fontData(const FontInfo &font) const
Document::OpenResult Document::openDocumentArchive( const QString & docFile, const KUrl & url, const QString & password ) Document::OpenResult Document::openDocumentArchive( const QString & docFile, const KUrl & url, const QString & password )
{ {
const KMimeType::Ptr mime = KMimeType::findByPath( docFile, 0, false /* content too */ ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(docFile) );
if ( !mime->is( "application/vnd.kde.okular-archive" ) ) if ( !mime->is( "application/vnd.kde.okular-archive" ) )
return OpenError; return OpenError;
@ -3908,7 +3896,7 @@ Document::OpenResult Document::openDocumentArchive( const QString & docFile, con
} }
} }
const KMimeType::Ptr docMime = KMimeType::findByPath( tempFileName, 0, true /* local file */ ); const KMimeType::Ptr docMime = KMimeType::findByUrl( KUrl(tempFileName) );
d->m_archiveData = archiveData.get(); d->m_archiveData = archiveData.get();
d->m_archivedFileName = documentFileName; d->m_archivedFileName = documentFileName;
const OpenResult ret = openDocument( tempFileName, url, docMime, password ); const OpenResult ret = openDocument( tempFileName, url, docMime, password );

View file

@ -56,7 +56,7 @@ bool Document::open( const QString &fileName )
{ {
close(); close();
const KMimeType::Ptr mime = KMimeType::findByFileContent( fileName ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(fileName) );
// qDebug() << Q_FUNC_INFO << mime->name(); // qDebug() << Q_FUNC_INFO << mime->name();
/** /**

View file

@ -1138,7 +1138,7 @@ Document::OpenResult Part::doOpenFile( const KMimeType::Ptr &mimeA, const QStrin
{ {
*isCompressedFile = true; *isCompressedFile = true;
uncompressOk = handleCompressed( fileNameToOpen, localFilePath(), compressedMime ); uncompressOk = handleCompressed( fileNameToOpen, localFilePath(), compressedMime );
mime = KMimeType::findByPath( fileNameToOpen ); mime = KMimeType::findByUrl( KUrl(fileNameToOpen) );
} }
else else
{ {
@ -1238,7 +1238,7 @@ bool Part::openFile()
const QFileInfo fileInfo( fileNameToOpen ); const QFileInfo fileInfo( fileNameToOpen );
if ( !isstdin && !fileInfo.exists() ) if ( !isstdin && !fileInfo.exists() )
return false; return false;
KMimeType::Ptr pathMime = KMimeType::findByPath( fileNameToOpen ); KMimeType::Ptr pathMime = KMimeType::findByUrl( KUrl(fileNameToOpen) );
if ( !arguments().mimeType().isEmpty() ) if ( !arguments().mimeType().isEmpty() )
{ {
KMimeType::Ptr argMime = KMimeType::mimeType( arguments().mimeType() ); KMimeType::Ptr argMime = KMimeType::mimeType( arguments().mimeType() );
@ -1260,7 +1260,7 @@ bool Part::openFile()
} }
if (mimes[0]->name() == "text/plain") { if (mimes[0]->name() == "text/plain") {
KMimeType::Ptr contentMime = KMimeType::findByFileContent( fileNameToOpen ); KMimeType::Ptr contentMime = KMimeType::findByUrl( KUrl(fileNameToOpen) );
mimes.prepend( contentMime ); mimes.prepend( contentMime );
} }
} }

View file

@ -45,7 +45,7 @@ void AddRemoveAnnotationTest::initTestCase()
void AddRemoveAnnotationTest::init() void AddRemoveAnnotationTest::init()
{ {
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
m_document->openDocument(testFile, KUrl(), mime); m_document->openDocument(testFile, KUrl(), mime);
} }

View file

@ -47,7 +47,7 @@ void AnnotationTest::initTestCase()
cg.writeEntry("annotExportAsArchive", false); cg.writeEntry("annotExportAsArchive", false);
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
m_document->openDocument(testFile, KUrl(), mime); m_document->openDocument(testFile, KUrl(), mime);
} }

View file

@ -33,7 +33,7 @@ void DocumentTest::testCloseDuringRotationJob()
Okular::SettingsCore::instance( "documenttest" ); Okular::SettingsCore::instance( "documenttest" );
Okular::Document *m_document = new Okular::Document( 0 ); Okular::Document *m_document = new Okular::Document( 0 );
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
Okular::DocumentObserver *dummyDocumentObserver = new Okular::DocumentObserver(); Okular::DocumentObserver *dummyDocumentObserver = new Okular::DocumentObserver();
m_document->addObserver( dummyDocumentObserver ); m_document->addObserver( dummyDocumentObserver );

View file

@ -109,7 +109,7 @@ void EditAnnotationContentsTest::cleanupTestCase()
void EditAnnotationContentsTest::init() void EditAnnotationContentsTest::init()
{ {
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
m_document->openDocument(testFile, KUrl(), mime); m_document->openDocument(testFile, KUrl(), mime);
// Undo and Redo should be unavailable when docuemnt is first opened. // Undo and Redo should be unavailable when docuemnt is first opened.

View file

@ -64,7 +64,7 @@ void EditFormsTest::cleanupTestCase()
void EditFormsTest::init() void EditFormsTest::init()
{ {
const QString testFile = KDESRCDIR "data/formSamples.pdf"; const QString testFile = KDESRCDIR "data/formSamples.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
m_document->openDocument( testFile, KUrl(), mime ); m_document->openDocument( testFile, KUrl(), mime );
// Undo and Redo should be unavailable when docuemnt is first opened. // Undo and Redo should be unavailable when docuemnt is first opened.

View file

@ -55,7 +55,7 @@ void ModifyAnnotationPropertiesTest::cleanupTestCase()
void ModifyAnnotationPropertiesTest::init() void ModifyAnnotationPropertiesTest::init()
{ {
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
m_document->openDocument(testFile, KUrl(), mime); m_document->openDocument(testFile, KUrl(), mime);
// Undo and Redo should be unavailable when docuemnt is first opened. // Undo and Redo should be unavailable when docuemnt is first opened.

View file

@ -173,7 +173,7 @@ void SearchTest::test311232()
QObject::connect(&d, SIGNAL(searchFinished(int,Okular::Document::SearchStatus)), &receiver, SLOT(searchFinished(int,Okular::Document::SearchStatus))); QObject::connect(&d, SIGNAL(searchFinished(int,Okular::Document::SearchStatus)), &receiver, SLOT(searchFinished(int,Okular::Document::SearchStatus)));
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
d.openDocument(testFile, KUrl(), mime); d.openDocument(testFile, KUrl(), mime);
const int searchId = 0; const int searchId = 0;

View file

@ -111,7 +111,7 @@ void TranslateAnnotationTest::cleanupTestCase()
void TranslateAnnotationTest::init() void TranslateAnnotationTest::init()
{ {
const QString testFile = KDESRCDIR "data/file1.pdf"; const QString testFile = KDESRCDIR "data/file1.pdf";
const KMimeType::Ptr mime = KMimeType::findByPath( testFile ); const KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(testFile) );
m_document->openDocument(testFile, KUrl(), mime); m_document->openDocument(testFile, KUrl(), mime);
// Undo and Redo should be unavailable when docuemnt is first opened. // Undo and Redo should be unavailable when docuemnt is first opened.

View file

@ -685,7 +685,7 @@ QWidget * FileAttachmentAnnotationWidget::createExtraWidget()
tmplabel->setTextInteractionFlags( Qt::TextSelectableByMouse ); tmplabel->setTextInteractionFlags( Qt::TextSelectableByMouse );
lay->addWidget( tmplabel, 3, 0, 1, 2 ); lay->addWidget( tmplabel, 3, 0, 1, 2 );
KMimeType::Ptr mime = KMimeType::findByPath( ef->name(), 0, true ); KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(ef->name()) );
if ( mime ) if ( mime )
{ {
tmplabel = new QLabel( widget ); tmplabel = new QLabel( widget );

View file

@ -60,7 +60,7 @@ EmbeddedFilesDialog::EmbeddedFilesDialog(QWidget *parent, const Okular::Document
{ {
QTreeWidgetItem *twi = new QTreeWidgetItem(); QTreeWidgetItem *twi = new QTreeWidgetItem();
twi->setText(0, ef->name()); twi->setText(0, ef->name());
KMimeType::Ptr mime = KMimeType::findByPath( ef->name(), 0, true ); KMimeType::Ptr mime = KMimeType::findByUrl( KUrl(ef->name()) );
if (mime) if (mime)
{ {
twi->setIcon(0, KIcon(mime->iconName())); twi->setIcon(0, KIcon(mime->iconName()));

View file

@ -60,7 +60,7 @@ bool ATCreator::create(const QString &path, int /*w*/, int /*h*/, QImage &img)
{ {
bool bRet = false; bool bRet = false;
KMimeType::Ptr type = KMimeType::findByPath(path, 0, true); KMimeType::Ptr type = KMimeType::findByUrl(KUrl(path));
if (type->is("audio/mpeg")) { if (type->is("audio/mpeg")) {
TagLib::MPEG::File mp3File(path.toUtf8()); TagLib::MPEG::File mp3File(path.toUtf8());