KFileMetaData

extractorplugin.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Vishesh Handa <[email protected]>
3  SPDX-FileCopyrightText: 2012 Jörg Ehrichs <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 
9 #include "extractorplugin.h"
10 
11 #include <QMimeDatabase>
12 #include <QDebug>
13 
14 using namespace KFileMetaData;
15 
16 ExtractorPlugin::ExtractorPlugin(QObject* parent): QObject(parent)
17 {
18 }
19 
20 ExtractorPlugin::~ExtractorPlugin()
21 {
22 }
23 
24 //
25 // Helper functions
26 //
27 
29 {
30  QDateTime dateTime;
31 
32  if (!dateTime.isValid()) {
33  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy-MM-dd"));
34  dateTime.setTimeSpec(Qt::UTC);
35  }
36  if (!dateTime.isValid()) {
37  dateTime = QDateTime::fromString(dateString, QStringLiteral("dd-MM-yyyy"));
38  dateTime.setTimeSpec(Qt::UTC);
39  }
40  if (!dateTime.isValid()) {
41  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy-MM"));
42  dateTime.setTimeSpec(Qt::UTC);
43  }
44  if (!dateTime.isValid()) {
45  dateTime = QDateTime::fromString(dateString, QStringLiteral("MM-yyyy"));
46  dateTime.setTimeSpec(Qt::UTC);
47  }
48  if (!dateTime.isValid()) {
49  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy.MM.dd"));
50  dateTime.setTimeSpec(Qt::UTC);
51  }
52  if (!dateTime.isValid()) {
53  dateTime = QDateTime::fromString(dateString, QStringLiteral("dd.MM.yyyy"));
54  dateTime.setTimeSpec(Qt::UTC);
55  }
56  if (!dateTime.isValid()) {
57  dateTime = QDateTime::fromString(dateString, QStringLiteral("dd MMMM yyyy"));
58  dateTime.setTimeSpec(Qt::UTC);
59  }
60  if (!dateTime.isValid()) {
61  dateTime = QDateTime::fromString(dateString, QStringLiteral("MM.yyyy"));
62  dateTime.setTimeSpec(Qt::UTC);
63  }
64  if (!dateTime.isValid()) {
65  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy.MM"));
66  dateTime.setTimeSpec(Qt::UTC);
67  }
68  if (!dateTime.isValid()) {
69  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy"));
70  dateTime.setTimeSpec(Qt::UTC);
71  }
72  if (!dateTime.isValid()) {
73  dateTime = QDateTime::fromString(dateString, QStringLiteral("yy"));
74  dateTime.setTimeSpec(Qt::UTC);
75  }
76  if (!dateTime.isValid()) {
77  dateTime = QDateTime::fromString(dateString, Qt::ISODate);
78  }
79  if (!dateTime.isValid()) {
80  dateTime = QDateTime::fromString(dateString, QStringLiteral("dddd d MMM yyyy h':'mm':'ss AP"));
81  dateTime.setTimeSpec(Qt::LocalTime);
82  }
83  if (!dateTime.isValid()) {
84  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy:MM:dd hh:mm:ss"));
85  dateTime.setTimeSpec(Qt::LocalTime);
86  }
87  if (!dateTime.isValid()) {
88  dateTime = QLocale().toDateTime(dateString, QLocale::ShortFormat);
89  dateTime.setTimeSpec(Qt::UTC);
90  }
91  if (!dateTime.isValid()) {
92  dateTime = QLocale().toDateTime(dateString, QLocale::LongFormat);
93  dateTime.setTimeSpec(Qt::UTC);
94  }
95  if (!dateTime.isValid()) {
96  qWarning() << "Could not determine correct datetime format from:" << dateString;
97  return QDateTime();
98  }
99 
100  return dateTime;
101 }
102 
104 {
105  QString cleanedString = string;
106  cleanedString = cleanedString.remove(QLatin1Char('{'));
107  cleanedString = cleanedString.remove(QLatin1Char('}'));
108 
109  QStringList contactStrings = string.split(QLatin1Char(','), Qt::SkipEmptyParts);
110  if (contactStrings.size() == 1) {
111  contactStrings = string.split(QLatin1Char(';'), Qt::SkipEmptyParts);
112  }
113 
114  if (contactStrings.size() == 1) {
115  contactStrings = string.split(QStringLiteral(" ft "), Qt::SkipEmptyParts);
116  }
117 
118  if (contactStrings.size() == 1) {
119  contactStrings = string.split(QStringLiteral(" feat. "), Qt::SkipEmptyParts);
120  }
121 
122  if (contactStrings.size() == 1) {
123  contactStrings = string.split(QStringLiteral(" feat "), Qt::SkipEmptyParts);
124  }
125 
126  QStringList list;
127  list.reserve(contactStrings.count());
128  for (const QString& contactName : std::as_const(contactStrings)) {
129  list << contactName.trimmed();
130  }
131 
132  return list;
133 }
134 
136 {
137  const QStringList allTypes = mimetypes();
138  if (allTypes.contains(mimetype)) {
139  return mimetype;
140  }
141 
142  QMimeDatabase db;
143  auto type = db.mimeTypeForName(mimetype);
144  const QStringList ancestors = type.allAncestors();
145  for (auto ancestor : ancestors) {
146  if (allTypes.contains(ancestor)) {
147  return ancestor;
148  }
149  }
150 
151  return QString();
152 }
static QDateTime dateTimeFromString(const QString &dateString)
Tries to extract a valid date time from the string provided.
int count(const T &value) const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void reserve(int alloc)
int size() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
SkipEmptyParts
QDateTime toDateTime(const QString &string, QLocale::FormatType format) const const
void setTimeSpec(Qt::TimeSpec spec)
QString getSupportedMimeType(const QString &mimetype) const
Return the inherited mimetype which the extractor directly supports.
QDateTime fromString(const QString &string, Qt::DateFormat format)
QString & remove(int position, int n)
static QStringList contactsFromString(const QString &string)
Tries to split the string into names.
bool isValid() const const
virtual QStringList mimetypes() const =0
Provide a list of mimetypes which are supported by this plugin.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 03:46:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.