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

« back to all changes in this revision

Viewing changes to .pc/upstream_git/0054-Email-Test-Print-io-debug-information.patch/src/pim/agent/tests/emailtest.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 "emailindexer.h"
24
 
 
25
 
#include <QApplication>
26
 
#include <QTimer>
27
 
#include <KDebug>
28
 
 
29
 
#include <Akonadi/CollectionFetchJob>
30
 
#include <Akonadi/Collection>
31
 
#include <Akonadi/ItemFetchJob>
32
 
#include <Akonadi/ItemFetchScope>
33
 
#include <Akonadi/Item>
34
 
 
35
 
class App : public QApplication {
36
 
    Q_OBJECT
37
 
public:
38
 
    App(int& argc, char** argv, int flags = ApplicationFlags);
39
 
 
40
 
private Q_SLOTS:
41
 
    void main();
42
 
 
43
 
    void slotRootCollectionsFetched(KJob* job);
44
 
    void indexNextCollection();
45
 
    void itemReceived(const Akonadi::Item::List& item);
46
 
    void slotIndexed();
47
 
    void slotCommitTimerElapsed();
48
 
 
49
 
private:
50
 
    Akonadi::Collection::List m_collections;
51
 
    EmailIndexer m_indexer;
52
 
 
53
 
    QTime m_totalTime;
54
 
    int m_indexTime;
55
 
    int m_numEmails;
56
 
 
57
 
    QTimer m_commitTimer;
58
 
};
59
 
 
60
 
int main(int argc, char** argv)
61
 
{
62
 
    App app(argc, argv);
63
 
    return app.exec();
64
 
}
65
 
 
66
 
App::App(int& argc, char** argv, int flags)
67
 
    : QApplication(argc, argv, flags)
68
 
    , m_indexer("/tmp/xap", "/tmp/xapC")
69
 
{
70
 
    QTimer::singleShot(0, this, SLOT(main()));
71
 
}
72
 
 
73
 
void App::main()
74
 
{
75
 
    m_commitTimer.setInterval(1000);
76
 
    connect(&m_commitTimer, SIGNAL(timeout()), this, SLOT(slotCommitTimerElapsed()));
77
 
    m_commitTimer.start();
78
 
 
79
 
    Akonadi::CollectionFetchJob* job = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(),
80
 
                                                                       Akonadi::CollectionFetchJob::Recursive);
81
 
    connect(job, SIGNAL(finished(KJob*)), this, SLOT(slotRootCollectionsFetched(KJob*)));
82
 
    job->start();
83
 
 
84
 
    m_numEmails = 0;
85
 
    m_indexTime = 0;
86
 
    m_totalTime.start();
87
 
}
88
 
 
89
 
void App::slotRootCollectionsFetched(KJob* kjob)
90
 
{
91
 
    Akonadi::CollectionFetchJob* job = qobject_cast<Akonadi::CollectionFetchJob*>(kjob);
92
 
    m_collections = job->collections();
93
 
 
94
 
    QMutableListIterator<Akonadi::Collection> it(m_collections);
95
 
    while (it.hasNext()) {
96
 
        const Akonadi::Collection& c = it.next();
97
 
        const QStringList mimeTypes = c.contentMimeTypes();
98
 
        if (!c.contentMimeTypes().contains("message/rfc822"))
99
 
            it.remove();
100
 
    }
101
 
 
102
 
    if (m_collections.size()) {
103
 
        indexNextCollection();
104
 
    }
105
 
    else {
106
 
        kDebug() << "No collections to index";
107
 
    }
108
 
}
109
 
 
110
 
void App::indexNextCollection()
111
 
{
112
 
    Akonadi::ItemFetchJob *fetchJob = new Akonadi::ItemFetchJob(m_collections.takeFirst(), this);
113
 
    fetchJob->fetchScope().fetchAllAttributes(true);
114
 
    fetchJob->fetchScope().fetchFullPayload(true);
115
 
    fetchJob->fetchScope().setFetchModificationTime(false);
116
 
    fetchJob->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
117
 
    fetchJob->setDeliveryOption(Akonadi::ItemFetchJob::EmitItemsIndividually);
118
 
 
119
 
    connect(fetchJob, SIGNAL(itemsReceived(Akonadi::Item::List)), this, SLOT(itemReceived(Akonadi::Item::List)));
120
 
    connect(fetchJob, SIGNAL(result(KJob*)), this, SLOT(slotIndexed()));
121
 
}
122
 
 
123
 
void App::itemReceived(const Akonadi::Item::List& itemList)
124
 
{
125
 
    QTime timer;
126
 
    timer.start();
127
 
 
128
 
    Q_FOREACH (const Akonadi::Item& item, itemList) {
129
 
        m_indexer.index(item);
130
 
    }
131
 
 
132
 
    m_indexTime += timer.elapsed();
133
 
    m_numEmails += itemList.size();
134
 
}
135
 
 
136
 
void App::slotCommitTimerElapsed()
137
 
{
138
 
    QTime timer;
139
 
    timer.start();
140
 
 
141
 
    m_indexer.commit();
142
 
    m_indexTime += timer.elapsed();
143
 
 
144
 
    kDebug() << "Emails:" << m_numEmails;
145
 
    kDebug() << "Total Time:" << m_totalTime.elapsed()/1000.0 << " seconds";
146
 
    kDebug() << "Index Time:" << m_indexTime/1000.0 << " seconds";
147
 
}
148
 
 
149
 
void App::slotIndexed()
150
 
{
151
 
    if (!m_collections.isEmpty()) {
152
 
        QTimer::singleShot(0, this, SLOT(indexNextCollection()));
153
 
        return;
154
 
    }
155
 
 
156
 
    m_indexer.commit();
157
 
 
158
 
    kDebug() << "Emails:" << m_numEmails;
159
 
    kDebug() << "Total Time:" << m_totalTime.elapsed()/1000.0 << " seconds";
160
 
    kDebug() << "Index Time:" << m_indexTime/1000.0 << " seconds";
161
 
    quit();
162
 
}
163
 
 
164
 
 
165
 
#include "emailtest.moc"