| 1 | #include <QCommandLineParser> |
| 2 | #include <QCoreApplication> |
| 3 | #include <QFileInfo> |
| 4 | #include <QTextStream> |
| 5 | |
| 6 | #include "mobipocket.h" |
| 7 | |
| 8 | int main(int argc, char **argv) |
| 9 | { |
| 10 | QCoreApplication app(argc, argv); |
| 11 | |
| 12 | QCommandLineParser parser; |
| 13 | parser.addOption(commandLineOption: {{QStringLiteral("f" ), QStringLiteral("fulltext" )}, QStringLiteral("Show full text" )}); |
| 14 | parser.addPositionalArgument(QStringLiteral("filename" ), QStringLiteral("File to process" )); |
| 15 | parser.process(app); |
| 16 | |
| 17 | if (parser.positionalArguments().size() != 1) { |
| 18 | QTextStream(stderr) << "Exactly one argument is accepted" << Qt::endl; |
| 19 | parser.showHelp(exitCode: 1); |
| 20 | } |
| 21 | bool showFulltext = parser.isSet(QStringLiteral("fulltext" )); |
| 22 | |
| 23 | auto fi = QFileInfo(parser.positionalArguments().at(i: 0)); |
| 24 | QString url = fi.absoluteFilePath(); |
| 25 | |
| 26 | if (!fi.exists()) { |
| 27 | QTextStream(stderr) << "File " << url << " not found" << Qt::endl; |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | if (!fi.isFile() || !fi.isReadable()) { |
| 32 | QTextStream(stderr) << "File " << url << " is not a readable file" << Qt::endl; |
| 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | QFile file(url); |
| 37 | file.open(flags: QFile::ReadOnly); |
| 38 | Mobipocket::Document doc(&file); |
| 39 | |
| 40 | if (!doc.isValid()) { |
| 41 | QTextStream(stderr) << "File " << url << " is not a valid MobiPocket file" << Qt::endl; |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | QTextStream out(stdout); |
| 46 | out << "===\nFile metadata:" << Qt::endl; |
| 47 | for (const auto &meta : doc.metadata().asKeyValueRange()) { |
| 48 | out << meta.first << " \"" << meta.second << "\"" << Qt::endl; |
| 49 | } |
| 50 | out << "DRM protected:" << (doc.hasDRM() ? " yes" : " no" ) << Qt::endl; |
| 51 | if (showFulltext && !doc.hasDRM()) { |
| 52 | out << "===\nRaw text:" << Qt::endl; |
| 53 | out << "\"" << doc.text() << "\"" << Qt::endl; |
| 54 | } |
| 55 | out << "===" << Qt::endl << Qt::endl; |
| 56 | return 0; |
| 57 | } |
| 58 | |