SVN_SILENT made messages (.desktop file)
[kdepim.git] / akonadiconsole / searchwidget.cpp
blob569e5fff11742d654b8217ca34e1be0bf936eba7
1 /*
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,
19 USA.
22 #include "searchwidget.h"
24 #include <akonadi/control.h>
25 #include <akonadi/itemfetchjob.h>
26 #include <akonadi/itemfetchscope.h>
27 #include <akonadi/itemsearchjob.h>
28 #include <kmessagebox.h>
30 #include <QtGui/QComboBox>
31 #include <QtGui/QGridLayout>
32 #include <QtGui/QLabel>
33 #include <QtGui/QListView>
34 #include <QtGui/QPushButton>
35 #include <QtGui/QStringListModel>
36 #include <QtGui/QTextBrowser>
37 #include <QtGui/QTextEdit>
39 #include <QtCore/QDebug>
41 SearchWidget::SearchWidget( QWidget *parent )
42 : QWidget( parent )
44 Akonadi::Control::widgetNeedsAkonadi( this );
45 QGridLayout *layout = new QGridLayout( this );
47 mQueryCombo = new QComboBox;
48 mQueryWidget = new QTextEdit;
49 mResultView = new QListView;
50 mItemView = new QTextBrowser;
51 QPushButton *button = new QPushButton( "Search" );
53 layout->addWidget( new QLabel( "Query:" ), 0, 0 );
54 layout->addWidget( mQueryCombo, 0, 1, Qt::AlignRight );
56 layout->addWidget( mQueryWidget, 1, 0, 1, 2 );
58 layout->addWidget( new QLabel( "Matching Item UIDs:" ), 2, 0 );
59 layout->addWidget( new QLabel( "View:" ), 2, 1 );
61 layout->addWidget( mResultView, 3, 0, 1, 1 );
62 layout->addWidget( mItemView, 3, 1, 1, 1 );
64 layout->addWidget( button, 4, 1, Qt::AlignRight );
66 mQueryCombo->addItem( "Empty" );
67 mQueryCombo->addItem( "Contacts by email address" );
68 mQueryCombo->addItem( "Contacts by name" );
69 mQueryCombo->addItem( "Email by From/Full Name" );
71 connect( button, SIGNAL(clicked()), this, SLOT(search()) );
72 connect( mQueryCombo, SIGNAL(activated(int)), this, SLOT(querySelected(int)) );
73 connect( mResultView, SIGNAL(activated(QModelIndex)), this, SLOT(fetchItem(QModelIndex)) );
75 mResultModel = new QStringListModel( this );
76 mResultView->setModel( mResultModel );
79 SearchWidget::~SearchWidget()
83 void SearchWidget::search()
85 Akonadi::ItemSearchJob *job = new Akonadi::ItemSearchJob( mQueryWidget->toPlainText() );
86 connect( job, SIGNAL(result(KJob*)), this, SLOT(searchFinished(KJob*)) );
89 void SearchWidget::searchFinished( KJob *job )
91 mResultModel->setStringList( QStringList() );
92 mItemView->clear();
94 if ( job->error() ) {
95 KMessageBox::error( this, job->errorString() );
96 return;
99 QStringList uidList;
100 Akonadi::ItemSearchJob *searchJob = qobject_cast<Akonadi::ItemSearchJob*>( job );
101 const Akonadi::Item::List items = searchJob->items();
102 foreach ( const Akonadi::Item &item, items ) {
103 uidList << QString::number( item.id() );
106 mResultModel->setStringList( uidList );
109 void SearchWidget::querySelected( int index )
111 if ( index == 0 ) {
112 mQueryWidget->clear();
113 } else if ( index == 1 ) {
114 mQueryWidget->setPlainText( ""
115 #ifdef AKONADI_USE_STRIGI_SEARCH
116 "<request>\n"
117 " <query>\n"
118 " <and>\n"
119 " <equals>\n"
120 " <field name=\"type\"/>\n"
121 " <string>PersonContact</string>\n"
122 " </equals>\n"
123 " <equals>\n"
124 " <field name=\"emailAddress\"/>\n"
125 " <string>tokoe@kde.org</string>\n"
126 " </equals>\n"
127 " </and>\n"
128 " </query>\n"
129 "</request>\n"
130 #else
131 "SELECT ?person WHERE {\n"
132 " ?person <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#hasEmailAddress> ?email .\n"
133 " ?email <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#emailAddress> \"tokoe@kde.org\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
134 " }\n"
135 #endif
137 } else if ( index == 2 ) {
138 mQueryWidget->setPlainText( ""
139 #ifdef AKONADI_USE_STRIGI_SEARCH
140 "<request>\n"
141 " <query>\n"
142 " <and>\n"
143 " <equals>\n"
144 " <field name=\"type\"/>\n"
145 " <string>PersonContact</string>\n"
146 " </equals>\n"
147 " <equals>\n"
148 " <field name=\"fullname\"/>\n"
149 " <string>Tobias Koenig</string>\n"
150 " </equals>\n"
151 " </and>\n"
152 " </query>\n"
153 "</request>\n"
154 #else
155 "prefix nco:<http://www.semanticdesktop.org/ontologies/2007/03/22/nco#>\n"
156 "SELECT ?r WHERE {\n"
157 " ?r nco:fullname \"Tobias Koenig\"^^<http://www.w3.org/2001/XMLSchema#string>.\n"
158 "}\n"
159 #endif
161 } else if ( index == 3 ) {
162 mQueryWidget->setPlainText( ""
163 #ifdef AKONADI_USE_STRIGI_SEARCH
164 "<request>\n"
165 " <query>\n"
166 " <and>\n"
167 " <equals>\n"
168 " <field name=\"type\"/>\n"
169 " <string>Email</string>\n"
170 " </equals>\n"
171 " <contains>\n"
172 " <field name=\"from\"/>\n"
173 " <string>Martin Koller</string>\n"
174 " </contains>\n"
175 " </and>\n"
176 " </query>\n"
177 "</request>\n"
178 #else
179 "SELECT ?mail WHERE {\n"
180 " ?mail <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#from> ?person .\n"
181 " ?person <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#fullname> "
182 "'Martin Koller'^^<http://www.w3.org/2001/XMLSchema#string> .\n"
183 "}\n"
184 #endif
189 void SearchWidget::fetchItem( const QModelIndex &index )
191 if ( !index.isValid() )
192 return;
194 const QString uid = index.data( Qt::DisplayRole ).toString();
195 Akonadi::ItemFetchJob *fetchJob = new Akonadi::ItemFetchJob( Akonadi::Item( uid.toLongLong() ) );
196 fetchJob->fetchScope().fetchFullPayload();
197 connect( fetchJob, SIGNAL(result(KJob*)), this, SLOT(itemFetched(KJob*)) );
200 void SearchWidget::itemFetched( KJob *job )
202 mItemView->clear();
204 if ( job->error() ) {
205 KMessageBox::error( this, "Error on fetching item" );
206 return;
209 Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
210 if ( !fetchJob->items().isEmpty() ) {
211 const Akonadi::Item item = fetchJob->items().first();
212 mItemView->setPlainText( QString::fromUtf8( item.payloadData() ) );
216 #include "searchwidget.moc"