1 | #include <QCommandLineParser> |
2 | #include <QCoreApplication> |
3 | #include <QDebug> |
4 | #include <QFileInfo> |
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 | qDebug() << "Exactly one argument is accepted" ; |
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 | qDebug() << "File" << url << "not found" ; |
28 | return 1; |
29 | } |
30 | |
31 | if (!fi.isFile() || !fi.isReadable()) { |
32 | qDebug() << "File" << url << "is not a readable file" ; |
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 | qDebug() << "File" << url << "is not a valid MobiPocket file" ; |
42 | return 1; |
43 | } |
44 | |
45 | qDebug() << "===\nFile metadata:" ; |
46 | for (const auto &meta : doc.metadata().asKeyValueRange()) { |
47 | qDebug() << meta.first << meta.second; |
48 | } |
49 | qDebug() << "DRM protected:" << (doc.hasDRM() ? "yes" : "no" ); |
50 | if (showFulltext && !doc.hasDRM()) { |
51 | qDebug() << "===\nRaw text:" ; |
52 | qDebug() << doc.text(); |
53 | } |
54 | qDebug() << "===\n" ; |
55 | } |
56 | |