~ubuntu-branches/ubuntu/trusty/baloo/trusty-updates

« back to all changes in this revision

Viewing changes to .pc/upstream_git/0023-BasicIndexingJob-Do-not-add-the-type-as-file.patch/src/file/basicindexingjob.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-04-10 21:32:59 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140410213259-b3hkzveqwe4hd3ax
Tags: 4:4.13.0-0ubuntu1
New upstream KDE Software Compilation release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of the KDE Baloo Project
3
 
 * Copyright (C) 2013  Vishesh Handa <me@vhanda.in>
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Lesser General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2.1 of the License, or (at your option) version 3, or any
9
 
 * later version accepted by the membership of KDE e.V. (or its
10
 
 * successor approved by the membership of KDE e.V.), which shall
11
 
 * act as a proxy defined in Section 6 of version 3 of the license.
12
 
 *
13
 
 * This library is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * Lesser General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public
19
 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 
 *
21
 
 */
22
 
 
23
 
#include "basicindexingjob.h"
24
 
#include "database.h"
25
 
#include "xapiandocument.h"
26
 
#include "lib/baloo_xattr_p.h"
27
 
 
28
 
#include <QFileInfo>
29
 
#include <QDateTime>
30
 
#include <QStringList>
31
 
 
32
 
#include <kfilemetadata/typeinfo.h>
33
 
#include <KDebug>
34
 
 
35
 
using namespace Baloo;
36
 
 
37
 
BasicIndexingJob::BasicIndexingJob(QSqlDatabase* db, const FileMapping& file, const QString& mimetype)
38
 
    : m_sqlDb(db)
39
 
    , m_file(file)
40
 
    , m_mimetype(mimetype)
41
 
    , m_id(0)
42
 
{
43
 
}
44
 
 
45
 
BasicIndexingJob::~BasicIndexingJob()
46
 
{
47
 
}
48
 
 
49
 
bool BasicIndexingJob::index()
50
 
{
51
 
    if (m_file.id() == 0) {
52
 
        if (!m_file.create(*m_sqlDb)) {
53
 
            kError() << "Cannot create fileMapping for" << m_file.url();
54
 
            return false;
55
 
        }
56
 
    }
57
 
 
58
 
    QFileInfo fileInfo(m_file.url());
59
 
 
60
 
    XapianDocument doc;
61
 
    doc.addTerm(m_mimetype, "M");
62
 
    doc.indexText(fileInfo.fileName(), 1000);
63
 
    doc.indexText(fileInfo.fileName(), "F", 1000);
64
 
 
65
 
    // Modified Date
66
 
    QDateTime mod = fileInfo.lastModified();
67
 
    const QString dtm = mod.toString(Qt::ISODate);
68
 
 
69
 
    doc.addBoolTerm(dtm, "DT_M");
70
 
    doc.addBoolTerm(mod.date().year(), "DT_MY");
71
 
    doc.addBoolTerm(mod.date().month(), "DT_MM");
72
 
    doc.addBoolTerm(mod.date().day(), "DT_MD");
73
 
 
74
 
    const QString timeTStr = QString::number(mod.toTime_t());
75
 
    doc.addValue(0, timeTStr);
76
 
    doc.addValue(1, QString::number(mod.date().toJulianDay()));
77
 
 
78
 
    // Types
79
 
    QVector<KFileMetaData::Type::Type> tList = typesForMimeType(m_mimetype);
80
 
    Q_FOREACH (KFileMetaData::Type::Type type, tList) {
81
 
        QString tstr = KFileMetaData::TypeInfo(type).name().toLower();
82
 
        doc.addBoolTerm(tstr, "T");
83
 
    }
84
 
    doc.addBoolTerm("file", "T");
85
 
 
86
 
    if (fileInfo.isDir()) {
87
 
        doc.addBoolTerm("folder", "T");
88
 
 
89
 
        // This is an optimization for folders. They do not need to go through
90
 
        // file indexing, so there are no indexers for folders
91
 
        doc.addBoolTerm("Z2");
92
 
    }
93
 
    else {
94
 
        doc.addBoolTerm("Z1");
95
 
    }
96
 
 
97
 
    //
98
 
    // X-Attr terms
99
 
    //
100
 
    QString val;
101
 
 
102
 
    baloo_getxattr(m_file.url(), QLatin1String("user.xdg.tags"), &val);
103
 
    if (!val.isEmpty()) {
104
 
        QStringList tags = val.split(QLatin1Char(','), QString::SkipEmptyParts);
105
 
        Q_FOREACH (const QString& tag, tags) {
106
 
            doc.indexText(tag, QLatin1String("TA"));
107
 
            doc.addBoolTerm(QLatin1String("TAG-") + tag);
108
 
        }
109
 
    }
110
 
 
111
 
    val.clear();
112
 
    baloo_getxattr(m_file.url(), QLatin1String("user.baloo.rating"), &val);
113
 
    if (!val.isEmpty()) {
114
 
        doc.addBoolTerm(val, QLatin1String("R"));
115
 
    }
116
 
 
117
 
    val.clear();
118
 
    baloo_getxattr(m_file.url(), QLatin1String("user.xdg.comment"), &val);
119
 
    if (!val.isEmpty()) {
120
 
        doc.indexText(val, QLatin1String("C"));
121
 
    }
122
 
 
123
 
    m_id = m_file.id();
124
 
    m_doc = doc.doc();
125
 
    return true;
126
 
}
127
 
 
128
 
QVector<KFileMetaData::Type::Type> BasicIndexingJob::typesForMimeType(const QString& mimeType) const
129
 
{
130
 
    using namespace KFileMetaData;
131
 
    QVector<Type::Type> types;
132
 
 
133
 
    // Basic types
134
 
    if (mimeType.contains(QLatin1String("audio")))
135
 
        types << Type::Audio;
136
 
    if (mimeType.contains(QLatin1String("video")))
137
 
        types << Type::Video;
138
 
    if (mimeType.contains(QLatin1String("image")))
139
 
        types << Type::Image;
140
 
    if (mimeType.contains(QLatin1String("document")))
141
 
        types << Type::Document;
142
 
    if (mimeType.contains(QLatin1String("text")))
143
 
        types << Type::Text;
144
 
    //if (mimeType.contains(QLatin1String("font")))
145
 
    //    types << "Font";
146
 
 
147
 
    if (mimeType.contains(QLatin1String("powerpoint"))) {
148
 
        types << Type::Presentation;
149
 
        types << Type::Document;
150
 
    }
151
 
    if (mimeType.contains(QLatin1String("excel"))) {
152
 
        types << Type::Spreadsheet;
153
 
        types << Type::Document;
154
 
    }
155
 
    //if (mimeType.contains(QLatin1String("text/html")))
156
 
    //    types << "Html";
157
 
 
158
 
    static QMultiHash<QString, Type::Type> typeMapper;
159
 
    if (typeMapper.isEmpty()) {
160
 
        // Microsoft
161
 
        typeMapper.insert(QLatin1String("text/plain"), Type::Document);
162
 
        typeMapper.insert(QLatin1String("application/msword"), Type::Document);
163
 
        typeMapper.insert(QLatin1String("application/vnd.ms-powerpoint"), Type::Document);
164
 
        typeMapper.insert(QLatin1String("application/vnd.ms-powerpoint"), Type::Presentation);
165
 
        typeMapper.insert(QLatin1String("application/vnd.ms-excel"), Type::Document);
166
 
        typeMapper.insert(QLatin1String("application/vnd.ms-excel"), Type::Spreadsheet);
167
 
 
168
 
        // Office 2007
169
 
        typeMapper.insert(QLatin1String("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
170
 
                          Type::Document);
171
 
        typeMapper.insert(QLatin1String("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
172
 
                          Type::Document);
173
 
        typeMapper.insert(QLatin1String("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
174
 
                          Type::Presentation);
175
 
        typeMapper.insert(QLatin1String("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
176
 
                          Type::Document);
177
 
        typeMapper.insert(QLatin1String("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
178
 
                          Type::Spreadsheet);
179
 
 
180
 
        // Open document formats - http://en.wikipedia.org/wiki/OpenDocument_technical_specification
181
 
        typeMapper.insert(QLatin1String("application/vnd.oasis.opendocument.text"), Type::Document);
182
 
        typeMapper.insert(QLatin1String("application/vnd.oasis.opendocument.presentation"), Type::Document);
183
 
        typeMapper.insert(QLatin1String("application/vnd.oasis.opendocument.presentation"), Type::Presentation);
184
 
        typeMapper.insert(QLatin1String("application/vnd.oasis.opendocument.spreadsheet"), Type::Document);
185
 
        typeMapper.insert(QLatin1String("application/vnd.oasis.opendocument.spreadsheet"), Type::Spreadsheet);
186
 
 
187
 
        // Others
188
 
        typeMapper.insert(QLatin1String("application/pdf"), Type::Document);
189
 
        typeMapper.insert(QLatin1String("application/postscript"), Type::Document);
190
 
        typeMapper.insert(QLatin1String("application/x-dvi"), Type::Document);
191
 
        typeMapper.insert(QLatin1String("application/rtf"), Type::Document);
192
 
 
193
 
        // Ebooks
194
 
        typeMapper.insert(QLatin1String("application/epub+zip"), Type::Document);
195
 
        typeMapper.insert(QLatin1String("application/x-mobipocket-ebook"), Type::Document);
196
 
 
197
 
        // Archives - http://en.wikipedia.org/wiki/List_of_archive_formats
198
 
        typeMapper.insert(QLatin1String("application/x-tar"), Type::Archive);
199
 
        typeMapper.insert(QLatin1String("application/x-bzip2"), Type::Archive);
200
 
        typeMapper.insert(QLatin1String("application/x-gzip"), Type::Archive);
201
 
        typeMapper.insert(QLatin1String("application/x-lzip"), Type::Archive);
202
 
        typeMapper.insert(QLatin1String("application/x-lzma"), Type::Archive);
203
 
        typeMapper.insert(QLatin1String("application/x-lzop"), Type::Archive);
204
 
        typeMapper.insert(QLatin1String("application/x-compress"), Type::Archive);
205
 
        typeMapper.insert(QLatin1String("application/x-7z-compressed"), Type::Archive);
206
 
        typeMapper.insert(QLatin1String("application/x-ace-compressed"), Type::Archive);
207
 
        typeMapper.insert(QLatin1String("application/x-astrotite-afa"), Type::Archive);
208
 
        typeMapper.insert(QLatin1String("application/x-alz-compressed"), Type::Archive);
209
 
        typeMapper.insert(QLatin1String("application/vnd.android.package-archive"), Type::Archive);
210
 
        typeMapper.insert(QLatin1String("application/x-arj"), Type::Archive);
211
 
        typeMapper.insert(QLatin1String("application/vnd.ms-cab-compressed"), Type::Archive);
212
 
        typeMapper.insert(QLatin1String("application/x-cfs-compressed"), Type::Archive);
213
 
        typeMapper.insert(QLatin1String("application/x-dar"), Type::Archive);
214
 
        typeMapper.insert(QLatin1String("application/x-lzh"), Type::Archive);
215
 
        typeMapper.insert(QLatin1String("application/x-lzx"), Type::Archive);
216
 
        typeMapper.insert(QLatin1String("application/x-rar-compressed"), Type::Archive);
217
 
        typeMapper.insert(QLatin1String("application/x-stuffit"), Type::Archive);
218
 
        typeMapper.insert(QLatin1String("application/x-stuffitx"), Type::Archive);
219
 
        typeMapper.insert(QLatin1String("application/x-gtar"), Type::Archive);
220
 
        typeMapper.insert(QLatin1String("application/zip"), Type::Archive);
221
 
 
222
 
        // Special images
223
 
        /*
224
 
        typeMapper.insert(QLatin1String("image/vnd.microsoft.icon"), "Icon");
225
 
        typeMapper.insert(QLatin1String("image/svg+xml"), "Image");
226
 
 
227
 
        // Fonts
228
 
        typeMapper.insert(QLatin1String("application/vnd.ms-fontobject"), "Font");
229
 
        typeMapper.insert(QLatin1String("application/vnd.ms-opentype"), "Font");
230
 
        */
231
 
    }
232
 
 
233
 
    types << typeMapper.values(mimeType).toVector();
234
 
    return types;
235
 
}
236