QString KoOdfCollectionLoader::findMimeTypeByUrl(const KUrl& url) { // // The following code was copied from KoDocument::openFile() // QString typeName = KMimeType::findByUrl(url, 0, true)->name(); // Allow to open backup files, don't keep the mimetype application/x-trash. if (typeName == "application/x-trash") { QString path = url.path(); KMimeType::Ptr mime = KMimeType::mimeType(typeName); QStringList patterns = mime ? mime->patterns() : QStringList(); // Find the extension that makes it a backup file, and remove it for(QStringList::Iterator it = patterns.begin(); it != patterns.end(); ++it) { QString ext = *it; if (!ext.isEmpty() && ext[0] == '*') { ext.remove(0, 1); if (path.endsWith(ext)) { path.truncate(path.length() - ext.length()); break; } } } typeName = KMimeType::findByPath(path, 0, true)->name(); } return typeName; }
void KMimeTypeTest::testPatterns() { QFETCH(QString, mimeType); QFETCH(QString, patterns); QFETCH(QString, mainExtension); KMimeType::Ptr mime = KMimeType::mimeType(mimeType); QVERIFY(mime); // Sort both lists; order is unreliable since shared-mime-info uses hashes internally. QStringList expectedPatterns = patterns.split(';', QString::SkipEmptyParts); expectedPatterns.sort(); QStringList mimePatterns = mime->patterns(); if (mimeType == "application/vnd.oasis.opendocument.text" && mimePatterns.contains("*.fodt")) { QSKIP("Skipping test which would fail due to an upstream bug, see https://bugs.freedesktop.org/show_bug.cgi?id=31242"); } if (mimeType == "application/vnd.oasis.opendocument.presentation" && mimePatterns.contains("*.fodp")) { QSKIP("Skipping test which would fail due to an upstream bug, see https://bugs.freedesktop.org/show_bug.cgi?id=31242"); } // shared-mime-info 0.30 adds *,v to text/plain, let's add it from this test so that it works // with older versions too. if (mimeType == "text/plain" && !mimePatterns.contains("*,v")) { mimePatterns.append("*,v"); } mimePatterns.sort(); // Not robust enough, other packages can add additional patterns, like libfm.xml adds *.inf to text/plain //QCOMPARE(mimePatterns.join(";"), expectedPatterns.join(";")); Q_FOREACH (const QString &expected, expectedPatterns) { QVERIFY2(mimePatterns.contains(expected), qPrintable(mimeType + " did not have pattern " + expected)); }
void KMimeTypeTest::testPatterns() { QFETCH(QString, mimeType); QFETCH(QString, patterns); QFETCH(QString, mainExtension); KMimeType::Ptr mime = KMimeType::mimeType( mimeType ); QVERIFY(mime); // Sort both lists; order is unreliable since shared-mime-info uses hashes internally. QStringList expectedPatterns = patterns.split(';'); expectedPatterns.sort(); QStringList mimePatterns = mime->patterns(); if (mimeType == "application/vnd.oasis.opendocument.text" && mimePatterns.contains("*.fodt")) { QSKIP("Skipping test which would fail due to an upstream bug, see https://bugs.freedesktop.org/show_bug.cgi?id=31242", SkipSingle); } if (mimeType == "application/vnd.oasis.opendocument.presentation" && mimePatterns.contains("*.fodp")) { QSKIP("Skipping test which would fail due to an upstream bug, see https://bugs.freedesktop.org/show_bug.cgi?id=31242", SkipSingle); } // shared-mime-info 0.30 adds *,v to text/plain, let's add it from this test so that it works // with older versions too. if (mimeType == "text/plain" && !mimePatterns.contains("*,v")) mimePatterns.append("*,v"); mimePatterns.sort(); QCOMPARE(mimePatterns.join(";"), expectedPatterns.join(";")); QCOMPARE(mime->mainExtension(), mainExtension); }
void Wizard::installExtraMimes(QString baseName, QStringList globs) { QString mimefile = baseName; mimefile.replace('/', '-'); KMimeType::Ptr mime = KMimeType::mimeType(baseName); if (!mime) { kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found"; } else { QStringList extensions = mime->patterns(); QString comment = mime->comment(); foreach(const QString &glob, globs) { if (!extensions.contains(glob)) extensions << glob; } kDebug() << "EXTS: " << extensions; QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml"); kDebug() << "INSTALLING NEW MIME TO: " << packageFileName; QFile packageFile(packageFileName); if (!packageFile.open(QIODevice::WriteOnly)) { kError() << "Couldn't open" << packageFileName << "for writing"; return; } QXmlStreamWriter writer(&packageFile); writer.setAutoFormatting(true); writer.writeStartDocument(); const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info"; writer.writeDefaultNamespace(nsUri); writer.writeStartElement("mime-info"); writer.writeStartElement(nsUri, "mime-type"); writer.writeAttribute("type", baseName); if (!comment.isEmpty()) { writer.writeStartElement(nsUri, "comment"); writer.writeCharacters(comment); writer.writeEndElement(); // comment } foreach(const QString& pattern, extensions) { writer.writeStartElement(nsUri, "glob"); writer.writeAttribute("pattern", pattern); writer.writeEndElement(); // glob } writer.writeEndElement(); // mime-info writer.writeEndElement(); // mime-type writer.writeEndDocument(); }
void KexiFileWidget::updateFilters() { if (d->filtersUpdated) return; d->filtersUpdated = true; d->lastFileName.clear(); clearFilter(); QString filter; KMimeType::Ptr mime; QStringList allfilters; const bool normalOpeningMode = d->mode & Opening && !(d->mode & Custom); const bool normalSavingMode = d->mode & SavingFileBasedDB && !(d->mode & Custom); if (normalOpeningMode || normalSavingMode) { mime = KMimeType::mimeType(KexiDB::defaultFileBasedDriverMimeType()); if (mime && !d->excludedMimeTypes.contains(mime->name().toLower())) { filter += KexiUtils::fileDialogFilterString(mime); allfilters += mime->patterns(); } } if (normalOpeningMode || d->mode & SavingServerBasedDB) { mime = KMimeType::mimeType("application/x-kexiproject-shortcut"); if (mime && !d->excludedMimeTypes.contains(mime->name().toLower())) { filter += KexiUtils::fileDialogFilterString(mime); allfilters += mime->patterns(); } } if (normalOpeningMode || d->mode & SavingServerBasedDB) { mime = KMimeType::mimeType("application/x-kexi-connectiondata"); if (mime && !d->excludedMimeTypes.contains(mime->name().toLower())) { filter += KexiUtils::fileDialogFilterString(mime); allfilters += mime->patterns(); } } //! @todo hardcoded for MSA: if (normalOpeningMode) { mime = KMimeType::mimeType("application/vnd.ms-access"); if (mime && !d->excludedMimeTypes.contains(mime->name().toLower())) { filter += KexiUtils::fileDialogFilterString(mime); allfilters += mime->patterns(); } mime = KMimeType::mimeType("application/vnd.oasis.opendocument.database"); if (mime && !d->excludedMimeTypes.contains(mime->name().toLower())) { filter += KexiUtils::fileDialogFilterString(mime); allfilters += mime->patterns(); } } foreach(const QString& mimeName, d->additionalMimeTypes) { if (mimeName == "all/allfiles") continue; if (d->excludedMimeTypes.contains(mimeName.toLower())) continue; filter += KexiUtils::fileDialogFilterString(mimeName); mime = KMimeType::mimeType(mimeName); allfilters += mime->patterns(); } if (!d->excludedMimeTypes.contains("all/allfiles")) { filter += filterWidget()->defaultFilter(); } //remove duplicates made because upper- and lower-case extenstions are used: QStringList allfiltersUnique = allfilters.toSet().toList(); qSort(allfiltersUnique); if (allfiltersUnique.count() > 1) {//prepend "all supoported files" entry filter.prepend(allfilters.join(" ") + "|" + i18n("All Supported Files (%1)", allfiltersUnique.join(", ")) + "\n"); } if (filter.right(1) == "\n") filter.truncate(filter.length() - 1); setFilter(filter); if (d->mode & Opening) { KFileWidget::setMode(KFile::ExistingOnly | KFile::LocalOnly | KFile::File); setOperationMode(KFileWidget::Opening); } else { KFileWidget::setMode(KFile::LocalOnly | KFile::File); setOperationMode(KFileWidget::Saving); } }