| 1 | /* |
|---|---|
| 2 | SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include <QGuiApplication> |
| 8 | #include <QCommandLineParser> |
| 9 | #include <QDebug> |
| 10 | #include <QFileInfo> |
| 11 | #include <QMimeDatabase> |
| 12 | #include <QTextStream> |
| 13 | |
| 14 | #include "extractor.h" |
| 15 | #include "extractorcollection.h" |
| 16 | #include "propertyinfo.h" |
| 17 | #include "simpleextractionresult.h" |
| 18 | #include "typeinfo.h" |
| 19 | |
| 20 | #include <iostream> |
| 21 | |
| 22 | int main(int argc, char** argv) |
| 23 | { |
| 24 | QGuiApplication app(argc, argv); |
| 25 | |
| 26 | QCommandLineParser parser; |
| 27 | parser.addOption(commandLineOption: {{QStringLiteral("f"), QStringLiteral( "fulltext")}, QStringLiteral( "Extract full text")}); |
| 28 | parser.addPositionalArgument(QStringLiteral("filename"), QStringLiteral( "File to process")); |
| 29 | parser.process(app); |
| 30 | |
| 31 | if (parser.positionalArguments().size() != 1) { |
| 32 | qDebug() << "Exactly one argument is accepted"; |
| 33 | parser.showHelp(exitCode: 1); |
| 34 | } |
| 35 | bool extractFulltext = parser.isSet(QStringLiteral("fulltext")); |
| 36 | |
| 37 | using KFileMetaData::ExtractionResult; |
| 38 | const ExtractionResult::Flags extractionLevel = (extractFulltext |
| 39 | ? ExtractionResult::ExtractMetaData | ExtractionResult::ExtractPlainText |
| 40 | : ExtractionResult::ExtractMetaData); |
| 41 | |
| 42 | auto fi = QFileInfo(parser.positionalArguments().at(i: 0)); |
| 43 | QString url = fi.absoluteFilePath(); |
| 44 | |
| 45 | if (!fi.exists()) { |
| 46 | qDebug() << "File"<< url << "not found"; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | if (!fi.isFile() || !fi.isReadable()) { |
| 51 | qDebug() << "File"<< url << "is not a readable file"; |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | QMimeDatabase mimeDb; |
| 56 | QString mimetype = mimeDb.mimeTypeForFile(fileName: url).name(); |
| 57 | |
| 58 | KFileMetaData::ExtractorCollection extractors; |
| 59 | QList<KFileMetaData::Extractor*> exList = extractors.fetchExtractors(mimetype); |
| 60 | |
| 61 | QTextStream out(stdout); |
| 62 | out << url << " "<< mimetype << "\n"; |
| 63 | |
| 64 | for (KFileMetaData::Extractor* ex : std::as_const(t&: exList)) { |
| 65 | const QString extractorName = ex->extractorProperties().value(QStringLiteral("Name")).toString(); |
| 66 | out << "\t"; |
| 67 | if (!extractorName.isEmpty()) { |
| 68 | out << extractorName; |
| 69 | } else { |
| 70 | out << "Extractor"; |
| 71 | } |
| 72 | out << " For "<< ex->mimetypes().join(sep: QLatin1String( "\n\t\t\t")) << "\n"; |
| 73 | |
| 74 | KFileMetaData::SimpleExtractionResult result(url, mimetype, extractionLevel); |
| 75 | ex->extract(result: &result); |
| 76 | |
| 77 | out << "\t\tTypes:"; |
| 78 | for (const auto t : result.types()) { |
| 79 | out << " "<< KFileMetaData::TypeInfo(t).name(); |
| 80 | } |
| 81 | out << "\n"; |
| 82 | |
| 83 | const KFileMetaData::PropertyMultiMap multiMap= result.properties(); |
| 84 | KFileMetaData::PropertyMultiMap::const_iterator it = multiMap.constBegin(); |
| 85 | for (; it != multiMap.constEnd(); it++) { |
| 86 | out << "\t\t"<< KFileMetaData::PropertyInfo(it.key()).displayName() << ": " |
| 87 | << it.value().toString() << " ("<< it.value().typeName() << ")\n"; |
| 88 | } |
| 89 | if (extractFulltext) { |
| 90 | out << "\t\tText: "<< result.text() << "\n"; |
| 91 | } |
| 92 | out << "\t-------------\n"; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 |
