24 #include "pimcommon/texteditor/plaintexteditor/plaintexteditorwidget.h"
25 #include "pimcommon/texteditor/plaintexteditor/plaintexteditor.h"
28 #include <KPIMUtils/ProgressIndicatorLabel>
30 #include <akonadi/itemfetchjob.h>
31 #include <akonadi/itemfetchscope.h>
32 #include <akonadi/itemsearchjob.h>
34 #include <KMessageBox>
37 #include <QPlainTextEdit>
38 #include <QGridLayout>
39 #include <QVBoxLayout>
41 #include <QStringListModel>
42 #include <QPushButton>
44 #include <QContextMenuEvent>
61 const QModelIndex index = indexAt( event->pos() );
66 QAction *searchNepomukShow =
new QAction(i18n(
"Search with nepomuk show..."), popup);
67 popup->addAction(searchNepomukShow);
68 QAction *act = popup->exec( event->globalPos() );
70 if (act == searchNepomukShow) {
71 const QString uid = QLatin1String(
"akonadi:?item=") + index.data( Qt::DisplayRole ).toString();
95 QGridLayout *layout =
new QGridLayout;
97 mTextEdit =
new PimCommon::PlainTextEditorWidget(
this );
99 mTextEdit->installEventFilter(
this );
107 mItemView =
new PimCommon::PlainTextEditorWidget;
108 mItemView->setReadOnly(
true);
110 layout->addWidget( mTextEdit, 0, 0, 1, 2);
111 layout->addWidget(
new QLabel( i18n(
"Akonadi Id:") ), 1, 0 );
112 layout->addWidget(
new QLabel( i18n(
"Messages:") ), 1, 1 );
114 layout->addWidget( mResultView, 2, 0, 1, 1 );
115 layout->addWidget( mItemView, 2, 1, 1, 1 );
117 mReduceQuery =
new QPushButton( i18n(
"Reduce query") );
118 connect(mReduceQuery, SIGNAL(clicked()), SLOT(slotReduceQuery()));
120 mSearchButton =
new QPushButton( i18n(
"Search") );
121 mSearchButton->setEnabled(
false);
122 connect( mSearchButton, SIGNAL(clicked()),
this, SLOT(slotSearch()) );
123 mProgressIndicator =
new KPIMUtils::ProgressIndicatorLabel(i18n(
"Searching..."));
125 mResultLabel =
new QLabel;
126 layout->addWidget( mResultLabel, 3, 0, Qt::AlignLeft );
128 layout->addWidget( mProgressIndicator, 4, 0, Qt::AlignLeft );
129 layout->addWidget( mSearchButton, 4, 1, Qt::AlignRight );
130 layout->addWidget( mReduceQuery, 5, 1, Qt::AlignRight );
133 connect( mResultView, SIGNAL(activated(QModelIndex)),
this, SLOT(slotFetchItem(QModelIndex)) );
134 connect(mTextEdit->editor(), SIGNAL(textChanged()), SLOT(slotUpdateSearchButton()));
135 mResultModel =
new QStringListModel(
this );
136 mResultView->setModel( mResultModel );
147 if( watched == mTextEdit && event->type() == QEvent::KeyPress ) {
148 QKeyEvent* kev =
static_cast<QKeyEvent*
>(event);
149 if( kev->key() == Qt::Key_Return &&
150 kev->modifiers() == Qt::ControlModifier ) {
156 return QWidget::eventFilter( watched, event );
159 void SearchDebugWidget::slotUpdateSearchButton()
161 mSearchButton->setEnabled(!mTextEdit->editor()->toPlainText().isEmpty());
166 return mTextEdit->editor()->toPlainText();
169 void SearchDebugWidget::slotSearch()
171 const QString query = mTextEdit->editor()->toPlainText();
173 if (query.isEmpty()) {
174 mResultLabel->setText(i18n(
"Query is empty."));
175 mSearchButton->setEnabled(
false);
176 mReduceQuery->setEnabled(
false);
180 mResultModel->setStringList( QStringList() );
181 mItemView->editor()->clear();
182 mResultLabel->clear();
183 mProgressIndicator->start();
184 mSearchButton->setEnabled(
false);
185 mReduceQuery->setEnabled(
false);
187 Akonadi::ItemSearchJob *job =
new Akonadi::ItemSearchJob( query );
188 connect( job, SIGNAL(result(
KJob*)),
this, SLOT(slotSearchFinished(
KJob*)) );
191 void SearchDebugWidget::indentQuery(QString query)
193 query = query.simplified();
199 while(i < query.size()) {
200 newQuery.append(query[i]);
201 if (query[i] != QLatin1Char(
'"') && query[i] != QLatin1Char(
'<') && query[i] != QLatin1Char(
'\'')) {
202 if (query[i] == QLatin1Char(
'{')) {
204 newQuery.append(QLatin1Char(
'\n'));
205 newQuery.append(QString().fill(QLatin1Char(
' '), indent*space));
206 }
else if (query[i] == QLatin1Char(
'.')) {
207 if(i+2<query.size()) {
208 if(query[i+1] == QLatin1Char(
'}')||query[i+2] == QLatin1Char(
'}')) {
209 newQuery.append(QLatin1Char(
'\n'));
210 newQuery.append(QString().fill(QLatin1Char(
' '), (indent-1)*space));
212 newQuery.append(QLatin1Char(
'\n'));
213 newQuery.append(QString().fill(QLatin1Char(
' '), indent*space));
216 newQuery.append(QLatin1Char(
'\n'));
217 newQuery.append(QString().fill(QLatin1Char(
' '), indent*space));
219 }
else if (query[i] == QLatin1Char(
'}')) {
221 if (i+2<query.size()) {
222 if (query[i+2] == QLatin1Char(
'.')||query[i+1] == QLatin1Char(
'.')) {
223 newQuery.append(QString().fill(QLatin1Char(
' '), 1));
225 newQuery.append(QLatin1Char(
'\n'));
226 newQuery.append(QString().fill(QLatin1Char(
' '), indent*space));
229 newQuery.append(QLatin1Char(
'\n'));
230 newQuery.append(QString().fill(QLatin1Char(
' '), indent*space));
235 while(i < query.size()) {
236 if (query[i] == QLatin1Char(
'"') || query[i] == QLatin1Char(
'>') || query[i] == QLatin1Char(
'\'')) {
237 newQuery.append(query[i]);
240 newQuery.append(query[i]);
246 mTextEdit->editor()->setPlainText( newQuery );
249 void SearchDebugWidget::slotReduceQuery()
251 QString query = mTextEdit->editor()->toPlainText();
256 void SearchDebugWidget::slotSearchFinished(
KJob *job)
258 mProgressIndicator->stop();
259 mSearchButton->setEnabled(
true);
260 mReduceQuery->setEnabled(
true);
262 if ( job->error() ) {
263 KMessageBox::error(
this, job->errorString() );
268 Akonadi::ItemSearchJob *searchJob = qobject_cast<Akonadi::ItemSearchJob*>( job );
269 const Akonadi::Item::List items = searchJob->items();
270 Q_FOREACH (
const Akonadi::Item &item, items ) {
271 uidList << QString::number( item.id() );
274 mResultModel->setStringList( uidList );
275 if (uidList.isEmpty()) {
276 mResultLabel->setText(i18n(
"No message found"));
278 mResultLabel->setText(i18np(
"1 message found",
"%1 messages found", uidList.count()));
282 void SearchDebugWidget::slotFetchItem(
const QModelIndex &index )
284 if ( !index.isValid() )
287 const QString uid = index.data( Qt::DisplayRole ).toString();
288 Akonadi::ItemFetchJob *fetchJob =
new Akonadi::ItemFetchJob( Akonadi::Item( uid.toLongLong() ) );
289 fetchJob->fetchScope().fetchFullPayload();
290 connect( fetchJob, SIGNAL(result(
KJob*)),
this, SLOT(slotItemFetched(
KJob*)) );
293 void SearchDebugWidget::slotItemFetched(
KJob *job )
295 mItemView->editor()->clear();
297 if ( job->error() ) {
298 KMessageBox::error(
this, i18n(
"Error on fetching item") );
302 Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
303 if ( !fetchJob->items().isEmpty() ) {
304 const Akonadi::Item item = fetchJob->items().first();
305 mItemView->editor()->setPlainText( QString::fromUtf8( item.payloadData() ) );
310 #include "searchdebugwidget.moc"
SearchResultListView(QWidget *parent=0)
~SearchDebugListDelegate()
QWidget * createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
void reduceQuery(QString &query)
void contextMenuEvent(QContextMenuEvent *event)
SearchDebugListDelegate(QObject *parent=0)