KFileMetaData

extractorplugin.cpp
1 /*
2  <one line to give the library's name and an idea of what it does.>
3  Copyright (C) 2012 Vishesh Handa <[email protected]>
4  Copyright (C) 2012 Jörg Ehrichs <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 
22 #include "extractorplugin.h"
23 
24 #include <QMimeDatabase>
25 #include <QDebug>
26 
27 using namespace KFileMetaData;
28 
29 ExtractorPlugin::ExtractorPlugin(QObject* parent): QObject(parent)
30 {
31 }
32 
33 ExtractorPlugin::~ExtractorPlugin()
34 {
35 }
36 
37 //
38 // Helper functions
39 //
40 
42 {
43  QDateTime dateTime;
44 
45  if (!dateTime.isValid()) {
46  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy-MM-dd"));
47  dateTime.setTimeSpec(Qt::UTC);
48  }
49  if (!dateTime.isValid()) {
50  dateTime = QDateTime::fromString(dateString, QStringLiteral("dd-MM-yyyy"));
51  dateTime.setTimeSpec(Qt::UTC);
52  }
53  if (!dateTime.isValid()) {
54  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy-MM"));
55  dateTime.setTimeSpec(Qt::UTC);
56  }
57  if (!dateTime.isValid()) {
58  dateTime = QDateTime::fromString(dateString, QStringLiteral("MM-yyyy"));
59  dateTime.setTimeSpec(Qt::UTC);
60  }
61  if (!dateTime.isValid()) {
62  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy.MM.dd"));
63  dateTime.setTimeSpec(Qt::UTC);
64  }
65  if (!dateTime.isValid()) {
66  dateTime = QDateTime::fromString(dateString, QStringLiteral("dd.MM.yyyy"));
67  dateTime.setTimeSpec(Qt::UTC);
68  }
69  if (!dateTime.isValid()) {
70  dateTime = QDateTime::fromString(dateString, QStringLiteral("dd MMMM yyyy"));
71  dateTime.setTimeSpec(Qt::UTC);
72  }
73  if (!dateTime.isValid()) {
74  dateTime = QDateTime::fromString(dateString, QStringLiteral("MM.yyyy"));
75  dateTime.setTimeSpec(Qt::UTC);
76  }
77  if (!dateTime.isValid()) {
78  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy.MM"));
79  dateTime.setTimeSpec(Qt::UTC);
80  }
81  if (!dateTime.isValid()) {
82  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy"));
83  dateTime.setTimeSpec(Qt::UTC);
84  }
85  if (!dateTime.isValid()) {
86  dateTime = QDateTime::fromString(dateString, QStringLiteral("yy"));
87  dateTime.setTimeSpec(Qt::UTC);
88  }
89  if (!dateTime.isValid()) {
90  dateTime = QDateTime::fromString(dateString, Qt::ISODate);
91  }
92  if (!dateTime.isValid()) {
93  dateTime = QDateTime::fromString(dateString, QStringLiteral("dddd d MMM yyyy h':'mm':'ss AP"));
94  dateTime.setTimeSpec(Qt::LocalTime);
95  }
96  if (!dateTime.isValid()) {
97  dateTime = QDateTime::fromString(dateString, QStringLiteral("yyyy:MM:dd hh:mm:ss"));
98  dateTime.setTimeSpec(Qt::LocalTime);
99  }
100  if (!dateTime.isValid()) {
101  dateTime = QDateTime::fromString(dateString, Qt::SystemLocaleDate);
102  dateTime.setTimeSpec(Qt::UTC);
103  }
104  if (!dateTime.isValid()) {
105  dateTime = QDateTime::fromString(dateString, Qt::SystemLocaleShortDate);
106  dateTime.setTimeSpec(Qt::UTC);
107  }
108  if (!dateTime.isValid()) {
109  dateTime = QDateTime::fromString(dateString, Qt::SystemLocaleLongDate);
110  dateTime.setTimeSpec(Qt::UTC);
111  }
112  if (!dateTime.isValid()) {
113  qWarning() << "Could not determine correct datetime format from:" << dateString;
114  return QDateTime();
115  }
116 
117  return dateTime;
118 }
119 
121 {
122  QString cleanedString = string;
123  cleanedString = cleanedString.remove(QLatin1Char('{'));
124  cleanedString = cleanedString.remove(QLatin1Char('}'));
125 
126  QStringList contactStrings = string.split(QLatin1Char(','), QString::SkipEmptyParts);
127  if (contactStrings.size() == 1)
128  contactStrings = string.split(QLatin1Char(';'), QString::SkipEmptyParts);
129 
130  if (contactStrings.size() == 1)
131  contactStrings = string.split(QStringLiteral(" ft "), QString::SkipEmptyParts);
132 
133  if (contactStrings.size() == 1)
134  contactStrings = string.split(QStringLiteral(" feat. "), QString::SkipEmptyParts);
135 
136  if (contactStrings.size() == 1)
137  contactStrings = string.split(QStringLiteral(" feat "), QString::SkipEmptyParts);
138 
139  QStringList list;
140  list.reserve(contactStrings.count());
141  for (const QString& contactName : qAsConst(contactStrings)) {
142  list << contactName.trimmed();
143  }
144 
145  return list;
146 }
147 
149 {
150  const QStringList allTypes = mimetypes();
151  if (allTypes.contains(mimetype)) {
152  return mimetype;
153  }
154 
155  QMimeDatabase db;
156  auto type = db.mimeTypeForName(mimetype);
157  const QStringList ancestors = type.allAncestors();
158  for (auto ancestor : ancestors) {
159  if (allTypes.contains(ancestor)) {
160  return ancestor;
161  }
162  }
163 
164  return QString();
165 }
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.
QDateTime fromString(const QString &string, Qt::DateFormat format)
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jun 2 2020 22:55:51 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.