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 = QDateTime::fromString(dateString, Qt::SystemLocaleDate);
89  dateTime.setTimeSpec(Qt::UTC);
90  }
91  if (!dateTime.isValid()) {
92  dateTime = QDateTime::fromString(dateString, Qt::SystemLocaleShortDate);
93  dateTime.setTimeSpec(Qt::UTC);
94  }
95  if (!dateTime.isValid()) {
96  dateTime = QDateTime::fromString(dateString, Qt::SystemLocaleLongDate);
97  dateTime.setTimeSpec(Qt::UTC);
98  }
99  if (!dateTime.isValid()) {
100  qWarning() << "Could not determine correct datetime format from:" << dateString;
101  return QDateTime();
102  }
103 
104  return dateTime;
105 }
106 
108 {
109  QString cleanedString = string;
110  cleanedString = cleanedString.remove(QLatin1Char('{'));
111  cleanedString = cleanedString.remove(QLatin1Char('}'));
112 
113  QStringList contactStrings = string.split(QLatin1Char(','), Qt::SkipEmptyParts);
114  if (contactStrings.size() == 1)
115  contactStrings = string.split(QLatin1Char(';'), Qt::SkipEmptyParts);
116 
117  if (contactStrings.size() == 1)
118  contactStrings = string.split(QStringLiteral(" ft "), Qt::SkipEmptyParts);
119 
120  if (contactStrings.size() == 1)
121  contactStrings = string.split(QStringLiteral(" feat. "), Qt::SkipEmptyParts);
122 
123  if (contactStrings.size() == 1)
124  contactStrings = string.split(QStringLiteral(" feat "), Qt::SkipEmptyParts);
125 
126  QStringList list;
127  list.reserve(contactStrings.count());
128  for (const QString& contactName : qAsConst(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.
void reserve(int alloc)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QString & remove(int position, int n)
int size() const const
void setTimeSpec(Qt::TimeSpec spec)
int count(const T &value) const const
static QStringList contactsFromString(const QString &string)
Tries to split the string into names.
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
QString getSupportedMimeType(const QString &mimetype) const
Return the inherited mimetype which the extractor directly supports.
SkipEmptyParts
QDateTime fromString(const QString &string, Qt::DateFormat format)
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 03:15:05 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.