~ubuntu-branches/ubuntu/trusty/baloo/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/upstream_git/0030-FileIndexingJob-Improve-logic.patch/src/file/fileindexingjob.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 Nepomuk KDE project.
3
 
   Copyright (C) 2010-2011 Sebastian Trueg <trueg@kde.org>
4
 
   Copyright (C) 2012-2014 Vishesh Handa <me@vhanda.in>
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) version 3, or any
10
 
   later version accepted by the membership of KDE e.V. (or its
11
 
   successor approved by the membership of KDE e.V.), which shall
12
 
   act as a proxy defined in Section 6 of version 3 of the license.
13
 
 
14
 
   This library is distributed in the hope that it will be useful,
15
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
   Lesser General Public License for more details.
18
 
 
19
 
   You should have received a copy of the GNU Lesser General Public
20
 
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
 
*/
22
 
 
23
 
#include "fileindexingjob.h"
24
 
#include "util.h"
25
 
#include "fileindexerconfig.h"
26
 
#include "database.h"
27
 
 
28
 
#include <qjson/serializer.h>
29
 
 
30
 
#include <KDebug>
31
 
#include <KStandardDirs>
32
 
 
33
 
#include <QtCore/QFileInfo>
34
 
#include <QtCore/QTimer>
35
 
 
36
 
using namespace Baloo;
37
 
 
38
 
FileIndexingJob::FileIndexingJob(const QVector<uint>& files, QObject* parent)
39
 
    : KJob(parent)
40
 
    , m_files(files)
41
 
    , m_process(0)
42
 
{
43
 
    // setup the timer used to kill the indexer process if it seems to get stuck
44
 
    m_processTimer = new QTimer(this);
45
 
    m_processTimer->setSingleShot(true);
46
 
    connect(m_processTimer, SIGNAL(timeout()),
47
 
            this, SLOT(slotProcessTimerTimeout()));
48
 
 
49
 
    m_processTimeout = 5 * 60 * 1000;
50
 
}
51
 
 
52
 
void FileIndexingJob::start()
53
 
{
54
 
    Q_ASSERT(!m_files.isEmpty());
55
 
 
56
 
    m_args = m_files;
57
 
    m_files.clear();
58
 
 
59
 
    start(m_args);
60
 
}
61
 
 
62
 
void FileIndexingJob::start(const QVector<uint>& files)
63
 
{
64
 
    // setup the external process which does the actual indexing
65
 
    const QString exe = KStandardDirs::findExe(QLatin1String("baloo_file_extractor"));
66
 
 
67
 
    // Just in case
68
 
    if (m_process) {
69
 
        m_process->disconnect();
70
 
        m_process->kill();
71
 
 
72
 
        delete m_process;
73
 
        m_process = 0;
74
 
    }
75
 
    m_process = new QProcess(this);
76
 
 
77
 
    QStringList args;
78
 
    Q_FOREACH (const uint& file, files)
79
 
        args << QString::number(file);
80
 
 
81
 
    if (!m_customDbPath.isEmpty()) {
82
 
        args << "--db" << m_customDbPath;
83
 
    }
84
 
    kDebug() << args;
85
 
 
86
 
    connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)),
87
 
            this, SLOT(slotIndexedFile(int,QProcess::ExitStatus)));
88
 
 
89
 
    m_process->setProcessChannelMode(QProcess::SeparateChannels);
90
 
    m_process->start(exe, args);
91
 
 
92
 
    m_processTimer->start(m_processTimeout);
93
 
}
94
 
 
95
 
void FileIndexingJob::slotIndexedFile(int, QProcess::ExitStatus exitStatus)
96
 
{
97
 
    // stop the timer since there is no need to kill the process anymore
98
 
    m_processTimer->stop();
99
 
    m_process->deleteLater();
100
 
    m_process = 0;
101
 
 
102
 
    if (exitStatus == QProcess::NormalExit) {
103
 
        if (m_files.isEmpty()) {
104
 
            emitResult();
105
 
            return;
106
 
        }
107
 
    }
108
 
    else {
109
 
        if (m_args.size() == 1) {
110
 
            uint doc = m_args.first();
111
 
            kError() << "Indexer crashed while indexing" << doc;
112
 
            kError() << "Blacklisting this file";
113
 
            Q_EMIT indexingFailed(doc);
114
 
 
115
 
            if (m_files.isEmpty()) {
116
 
                emitResult();
117
 
                return;
118
 
            }
119
 
        }
120
 
        else {
121
 
            m_files = m_args;
122
 
        }
123
 
    }
124
 
 
125
 
    // Split the number of files into half
126
 
    if (m_files.size() == 1) {
127
 
        m_args = m_files;
128
 
        m_files.clear();
129
 
 
130
 
        start(m_args);
131
 
    }
132
 
    else {
133
 
        int mid = m_files.size()/2;
134
 
        m_args = m_files.mid(mid);
135
 
        m_files.resize(mid);
136
 
 
137
 
        start(m_args);
138
 
    }
139
 
}
140
 
 
141
 
void FileIndexingJob::slotProcessTimerTimeout()
142
 
{
143
 
    // Emulate a crash so that we narrow down the file which is taking too long
144
 
    slotIndexedFile(1, QProcess::CrashExit);
145
 
}
146
 
 
147
 
void FileIndexingJob::setCustomDbPath(const QString& path)
148
 
{
149
 
    m_customDbPath = path;
150
 
}
151
 
 
152
 
void FileIndexingJob::setTimeoutInterval(int msec)
153
 
{
154
 
    m_processTimeout = msec;
155
 
}
156
 
 
157
 
 
158
 
 
159
 
#include "fileindexingjob.moc"