2 This file is part of Akonadi.
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 #include "searchwidget.h"
24 #include <AkonadiWidgets/controlgui.h>
25 #include <AkonadiCore/itemfetchjob.h>
26 #include <AkonadiCore/itemfetchscope.h>
27 #include <AkonadiCore/itemsearchjob.h>
28 #include <AkonadiCore/SearchQuery>
29 #include <kpimtextedit/plaintexteditorwidget.h>
32 #include <KMessageBox>
33 #include <QTextBrowser>
36 #include <QGridLayout>
39 #include <QPushButton>
40 #include <QStringListModel>
42 SearchWidget::SearchWidget(QWidget
*parent
)
45 Akonadi::ControlGui::widgetNeedsAkonadi(this);
46 QGridLayout
*layout
= new QGridLayout(this);
48 mQueryCombo
= new KComboBox
;
49 mQueryWidget
= new KPIMTextEdit::PlainTextEditorWidget
;
50 mResultView
= new QListView
;
51 mItemView
= new QTextBrowser
;
52 QPushButton
*button
= new QPushButton(QStringLiteral("Search"));
54 layout
->addWidget(new QLabel(QStringLiteral("Query:")), 0, 0);
55 layout
->addWidget(mQueryCombo
, 0, 1, Qt::AlignRight
);
57 layout
->addWidget(mQueryWidget
, 1, 0, 1, 2);
59 layout
->addWidget(new QLabel(QStringLiteral("Matching Item UIDs:")), 2, 0);
60 layout
->addWidget(new QLabel(QStringLiteral("View:")), 2, 1);
62 layout
->addWidget(mResultView
, 3, 0, 1, 1);
63 layout
->addWidget(mItemView
, 3, 1, 1, 1);
65 layout
->addWidget(button
, 4, 1, Qt::AlignRight
);
67 mQueryCombo
->addItem(QStringLiteral("Empty"));
68 mQueryCombo
->addItem(QStringLiteral("Contacts by email address"));
69 mQueryCombo
->addItem(QStringLiteral("Contacts by name"));
70 mQueryCombo
->addItem(QStringLiteral("Email by From/Full Name"));
72 connect(button
, &QPushButton::clicked
, this, &SearchWidget::search
);
73 connect(mQueryCombo
, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated
), this, &SearchWidget::querySelected
);
74 connect(mResultView
, &QListView::activated
, this, &SearchWidget::fetchItem
);
76 mResultModel
= new QStringListModel(this);
77 mResultView
->setModel(mResultModel
);
80 SearchWidget::~SearchWidget()
84 void SearchWidget::search()
86 Akonadi::ItemSearchJob
*job
= new Akonadi::ItemSearchJob(
87 Akonadi::SearchQuery::fromJSON(mQueryWidget
->toPlainText().toUtf8()));
88 connect(job
, &Akonadi::ItemSearchJob::result
, this, &SearchWidget::searchFinished
);
91 void SearchWidget::searchFinished(KJob
*job
)
93 mResultModel
->setStringList(QStringList());
97 KMessageBox::error(this, job
->errorString());
102 Akonadi::ItemSearchJob
*searchJob
= qobject_cast
<Akonadi::ItemSearchJob
*>(job
);
103 const Akonadi::Item::List items
= searchJob
->items();
104 foreach (const Akonadi::Item
&item
, items
) {
105 uidList
<< QString::number(item
.id());
108 mResultModel
->setStringList(uidList
);
111 void SearchWidget::querySelected(int index
)
114 mQueryWidget
->clear();
115 } else if (index
== 1) {
116 mQueryWidget
->setPlainText(QStringLiteral("SELECT ?person WHERE {\n"
117 " ?person <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#hasEmailAddress> ?email .\n"
118 " ?email <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#emailAddress> \"tokoe@kde.org\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
121 } else if (index
== 2) {
122 mQueryWidget
->setPlainText(QStringLiteral("prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>\n"
123 "SELECT ?r WHERE {\n"
124 " ?r nco:fullname \"Tobias Koenig\"^^<http://www.w3.org/2001/XMLSchema#string>.\n"
127 } else if (index
== 3) {
128 mQueryWidget
->setPlainText(QStringLiteral("SELECT ?mail WHERE {\n"
129 " ?mail <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#from> ?person .\n"
130 " ?person <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#fullname> "
131 "'Martin Koller'^^<http://www.w3.org/2001/XMLSchema#string> .\n"
137 void SearchWidget::fetchItem(const QModelIndex
&index
)
139 if (!index
.isValid()) {
143 const QString uid
= index
.data(Qt::DisplayRole
).toString();
144 Akonadi::ItemFetchJob
*fetchJob
= new Akonadi::ItemFetchJob(Akonadi::Item(uid
.toLongLong()));
145 fetchJob
->fetchScope().fetchFullPayload();
146 connect(fetchJob
, &Akonadi::ItemFetchJob::result
, this, &SearchWidget::itemFetched
);
149 void SearchWidget::itemFetched(KJob
*job
)
154 KMessageBox::error(this, QStringLiteral("Error on fetching item"));
158 Akonadi::ItemFetchJob
*fetchJob
= qobject_cast
<Akonadi::ItemFetchJob
*>(job
);
159 if (!fetchJob
->items().isEmpty()) {
160 const Akonadi::Item item
= fetchJob
->items().first();
161 mItemView
->setPlainText(QString::fromUtf8(item
.payloadData()));