• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

mailcommon

  • sources
  • kde-4.12
  • kdepim
  • mailcommon
  • search
headersrulerwidgethandler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "headersrulerwidgethandler.h"
19 #include <pimcommon/widgets/minimumcombobox.h>
20 
21 #include "search/searchpattern.h"
22 #include "widgets/regexplineedit.h"
23 using MailCommon::RegExpLineEdit;
24 
25 #include <KDebug>
26 #include <KLocale>
27 
28 #include <QLineEdit>
29 #include <QStackedWidget>
30 #include <QLabel>
31 
32 using namespace MailCommon;
33 
34 // also see SearchRule::matches() and SearchRule::Function
35 // if you change the following strings!
36 static const struct {
37  SearchRule::Function id;
38  const char *displayName;
39 } HeaderFunctions[] = {
40  { SearchRule::FuncContains, I18N_NOOP( "contains" ) },
41  { SearchRule::FuncContainsNot, I18N_NOOP( "does not contain" ) },
42  { SearchRule::FuncEquals, I18N_NOOP( "equals" ) },
43  { SearchRule::FuncNotEqual, I18N_NOOP( "does not equal" ) },
44  { SearchRule::FuncStartWith, I18N_NOOP( "starts with" ) },
45  { SearchRule::FuncNotStartWith, I18N_NOOP( "does not start with" )},
46  { SearchRule::FuncEndWith, I18N_NOOP( "ends with" ) },
47  { SearchRule::FuncNotEndWith, I18N_NOOP( "does not end with" ) },
48 
49  { SearchRule::FuncRegExp, I18N_NOOP( "matches regular expr." ) },
50  { SearchRule::FuncNotRegExp, I18N_NOOP( "does not match reg. expr." ) },
51  { SearchRule::FuncIsInAddressbook, I18N_NOOP( "is in address book" ) },
52  { SearchRule::FuncIsNotInAddressbook, I18N_NOOP( "is not in address book" ) }
53 };
54 static const int HeadersFunctionCount =
55  sizeof( HeaderFunctions ) / sizeof( *HeaderFunctions );
56 
57 //---------------------------------------------------------------------------
58 
59 QWidget *HeadersRuleWidgetHandler::createFunctionWidget(
60  int number, QStackedWidget *functionStack, const QObject *receiver, bool isNepomukSearch ) const
61 {
62  if ( number != 0 ) {
63  return 0;
64  }
65 
66  PimCommon::MinimumComboBox *funcCombo = new PimCommon::MinimumComboBox( functionStack );
67  funcCombo->setObjectName( QLatin1String("headerRuleFuncCombo") );
68  for ( int i = 0; i < HeadersFunctionCount; ++i ) {
69  if (! (isNepomukSearch &&
70  (HeaderFunctions[i].id == SearchRule::FuncIsInAddressbook || HeaderFunctions[i].id == SearchRule::FuncIsNotInAddressbook))) {
71  funcCombo->addItem( i18n( HeaderFunctions[i].displayName ) );
72  }
73  }
74  funcCombo->adjustSize();
75  QObject::connect( funcCombo, SIGNAL(activated(int)),
76  receiver, SLOT(slotFunctionChanged()) );
77  return funcCombo;
78 }
79 
80 //---------------------------------------------------------------------------
81 
82 QWidget *HeadersRuleWidgetHandler::createValueWidget( int number,
83  QStackedWidget *valueStack,
84  const QObject *receiver ) const
85 {
86  if ( number == 0 ) {
87  RegExpLineEdit *lineEdit = new RegExpLineEdit( valueStack );
88  lineEdit->setObjectName( QLatin1String("regExpLineEdit") );
89  QObject::connect( lineEdit, SIGNAL(textChanged(QString)),
90  receiver, SLOT(slotValueChanged()) );
91  QObject::connect( lineEdit, SIGNAL(returnPressed()),
92  receiver, SLOT(slotReturnPressed()) );
93  return lineEdit;
94  }
95 
96  // blank QLabel to hide value widget for in-address-book rule
97  if ( number == 1 ) {
98  QLabel *label = new QLabel( valueStack );
99  label->setObjectName( QLatin1String("headerRuleValueHider") );
100  label->setBuddy( valueStack );
101  return label;
102  }
103  return 0;
104 }
105 
106 //---------------------------------------------------------------------------
107 
108 SearchRule::Function HeadersRuleWidgetHandler::currentFunction(
109  const QStackedWidget *functionStack ) const
110 {
111  const PimCommon::MinimumComboBox *funcCombo =
112  functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String( "headerRuleFuncCombo" ) );
113 
114  if ( funcCombo && funcCombo->currentIndex() >= 0 ) {
115  return HeaderFunctions[funcCombo->currentIndex()].id;
116  }
117 
118  return SearchRule::FuncNone;
119 }
120 
121 //---------------------------------------------------------------------------
122 
123 SearchRule::Function HeadersRuleWidgetHandler::function( const QByteArray &field,
124  const QStackedWidget *functionStack ) const
125 {
126  if ( !handlesField( field ) ) {
127  return SearchRule::FuncNone;
128  }
129  return currentFunction( functionStack );
130 }
131 
132 //---------------------------------------------------------------------------
133 QString HeadersRuleWidgetHandler::currentValue( const QStackedWidget *valueStack,
134  SearchRule::Function func ) const
135 {
136  //in other cases of func it is a lineedit
137  const RegExpLineEdit *lineEdit = valueStack->findChild<RegExpLineEdit*>( QLatin1String("regExpLineEdit") );
138 
139  if ( lineEdit ) {
140  return lineEdit->text();
141  }
142 
143  // or anything else, like addressbook
144  return QString();
145 }
146 
147 //---------------------------------------------------------------------------
148 
149 QString HeadersRuleWidgetHandler::value( const QByteArray &field,
150  const QStackedWidget *functionStack,
151  const QStackedWidget *valueStack ) const
152 {
153  if ( !handlesField( field ) ) {
154  return QString();
155  }
156  SearchRule::Function func = currentFunction( functionStack );
157  if ( func == SearchRule::FuncIsInAddressbook ) {
158  return "is in address book"; // just a non-empty dummy value
159  } else if ( func == SearchRule::FuncIsNotInAddressbook ) {
160  return "is not in address book"; // just a non-empty dummy value
161  } else {
162  return currentValue( valueStack, func );
163  }
164 }
165 
166 //---------------------------------------------------------------------------
167 
168 QString HeadersRuleWidgetHandler::prettyValue( const QByteArray & field,
169  const QStackedWidget *functionStack,
170  const QStackedWidget *valueStack ) const
171 {
172  if ( !handlesField( field ) ) {
173  return QString();
174  }
175 
176  SearchRule::Function func = currentFunction( functionStack );
177 
178  if ( func == SearchRule::FuncIsInAddressbook ) {
179  return i18n( "is in address book" );
180  } else if ( func == SearchRule::FuncIsNotInAddressbook ) {
181  return i18n( "is not in address book" );
182  } else {
183  return currentValue( valueStack, func );
184  }
185 }
186 
187 //---------------------------------------------------------------------------
188 
189 bool HeadersRuleWidgetHandler::handlesField( const QByteArray &field ) const
190 {
191  return ( field == "To" || field == "From" || field == "CC" || field == "<recipients>");
192 }
193 
194 //---------------------------------------------------------------------------
195 
196 void HeadersRuleWidgetHandler::reset( QStackedWidget *functionStack,
197  QStackedWidget *valueStack ) const
198 {
199  // reset the function combo box
200  PimCommon::MinimumComboBox *funcCombo = functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("headerRuleFuncCombo") );
201 
202  if ( funcCombo ) {
203  funcCombo->blockSignals( true );
204  funcCombo->setCurrentIndex( 0 );
205  funcCombo->blockSignals( false );
206  }
207 
208  // reset the value widget
209  RegExpLineEdit *lineEdit = valueStack->findChild<RegExpLineEdit*>( QLatin1String("regExpLineEdit") );
210  if ( lineEdit ) {
211  lineEdit->blockSignals( true );
212  lineEdit->clear();
213  lineEdit->blockSignals( false );
214  lineEdit->showEditButton( false );
215  valueStack->setCurrentWidget( lineEdit );
216  }
217 
218 }
219 
220 //---------------------------------------------------------------------------
221 
222 bool HeadersRuleWidgetHandler::setRule( QStackedWidget *functionStack,
223  QStackedWidget *valueStack,
224  const SearchRule::Ptr rule, bool isNepomukSearch ) const
225 {
226  if ( !rule || !handlesField( rule->field() ) ) {
227  reset( functionStack, valueStack );
228  return false;
229  }
230 
231  const SearchRule::Function func = rule->function();
232  if ( (isNepomukSearch &&
233  (func == SearchRule::FuncIsInAddressbook || func == SearchRule::FuncIsNotInAddressbook))) {
234  reset( functionStack, valueStack );
235  return false;
236  }
237 
238 
239 
240  int i = 0;
241  for ( ; i < HeadersFunctionCount; ++i ) {
242  if ( func == HeaderFunctions[i].id ) {
243  break;
244  }
245  }
246 
247  PimCommon::MinimumComboBox *funcCombo = functionStack->findChild<PimCommon::MinimumComboBox*>( QLatin1String("headerRuleFuncCombo") );
248 
249  if ( funcCombo ) {
250  funcCombo->blockSignals( true );
251  if ( i < HeadersFunctionCount ) {
252  funcCombo->setCurrentIndex( i );
253  } else {
254  funcCombo->setCurrentIndex( 0 );
255  }
256  funcCombo->blockSignals( false );
257  functionStack->setCurrentWidget( funcCombo );
258  }
259 
260  if ( func == SearchRule::FuncIsInAddressbook ||
261  func == SearchRule::FuncIsNotInAddressbook ) {
262  QWidget *w = valueStack->findChild<QWidget*>( QLatin1String("headerRuleValueHider") );
263  valueStack->setCurrentWidget( w );
264  }
265  else {
266  RegExpLineEdit *lineEdit =
267  valueStack->findChild<RegExpLineEdit*>( QLatin1String("regExpLineEdit") );
268 
269  if ( lineEdit ) {
270  lineEdit->blockSignals( true );
271  lineEdit->setText( rule->contents() );
272  lineEdit->blockSignals( false );
273  lineEdit->showEditButton( func == SearchRule::FuncRegExp ||
274  func == SearchRule::FuncNotRegExp );
275  valueStack->setCurrentWidget( lineEdit );
276  }
277  }
278  return true;
279 }
280 
281 //---------------------------------------------------------------------------
282 
283 bool HeadersRuleWidgetHandler::update( const QByteArray &field,
284  QStackedWidget *functionStack,
285  QStackedWidget *valueStack ) const
286 {
287  if ( !handlesField( field ) ) {
288  return false;
289  }
290 
291  // raise the correct function widget
292  functionStack->setCurrentWidget( functionStack->findChild<QWidget*>( QLatin1String("headerRuleFuncCombo") ) );
293 
294  // raise the correct value widget
295  SearchRule::Function func = currentFunction( functionStack );
296  if ( func == SearchRule::FuncIsInAddressbook ||
297  func == SearchRule::FuncIsNotInAddressbook ) {
298  valueStack->setCurrentWidget( valueStack->findChild<QWidget*>( QLatin1String("headerRuleValueHider") ) );
299  }
300  else {
301  RegExpLineEdit *lineEdit =
302  valueStack->findChild<RegExpLineEdit*>( QLatin1String("regExpLineEdit") );
303 
304  if ( lineEdit ) {
305  lineEdit->showEditButton( func == SearchRule::FuncRegExp ||
306  func == SearchRule::FuncNotRegExp );
307  valueStack->setCurrentWidget( lineEdit );
308  }
309  }
310  return true;
311 }
312 
MailCommon::SearchRule::FuncIsInAddressbook
Definition: searchpattern.h:92
MailCommon::SearchRule::FuncEndWith
Definition: searchpattern.h:100
MailCommon::SearchRule::FuncNotEndWith
Definition: searchpattern.h:101
MailCommon::HeadersRuleWidgetHandler::function
SearchRule::Function function(const QByteArray &field, const QStackedWidget *functionStack) const
Definition: headersrulerwidgethandler.cpp:123
MailCommon::SearchRule::FuncContainsNot
Definition: searchpattern.h:83
MailCommon::SearchRule::FuncIsNotInAddressbook
Definition: searchpattern.h:93
MailCommon::RegExpLineEdit::setText
void setText(const QString &)
Definition: regexplineedit.cpp:102
id
SearchRule::Function id
Definition: headersrulerwidgethandler.cpp:37
MailCommon::SearchRule::FuncNotStartWith
Definition: searchpattern.h:99
QWidget
MailCommon::RegExpLineEdit
Definition: regexplineedit.h:46
MailCommon::RegExpLineEdit::clear
void clear()
Definition: regexplineedit.cpp:92
MailCommon::SearchRule::Ptr
boost::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchpattern.h:69
QObject
MailCommon::SearchRule::FuncStartWith
Definition: searchpattern.h:98
MailCommon::SearchRule::FuncNotRegExp
Definition: searchpattern.h:87
MailCommon::HeadersRuleWidgetHandler::reset
void reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
Definition: headersrulerwidgethandler.cpp:196
MailCommon::HeadersRuleWidgetHandler::createFunctionWidget
QWidget * createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isNepomukSearch) const
Definition: headersrulerwidgethandler.cpp:59
MailCommon::SearchRule::Function
Function
Describes operators for comparison of field and contents.
Definition: searchpattern.h:80
MailCommon::SearchRule::FuncNotEqual
Definition: searchpattern.h:85
MailCommon::SearchRule::FuncNone
Definition: searchpattern.h:81
MailCommon::HeadersRuleWidgetHandler::prettyValue
QString prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
Definition: headersrulerwidgethandler.cpp:168
MailCommon::HeadersRuleWidgetHandler::update
bool update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
Definition: headersrulerwidgethandler.cpp:283
MailCommon::SearchRule::FuncRegExp
Definition: searchpattern.h:86
searchpattern.h
regexplineedit.h
HeadersFunctionCount
static const int HeadersFunctionCount
Definition: headersrulerwidgethandler.cpp:54
MailCommon::SearchRule::FuncContains
Definition: searchpattern.h:82
HeaderFunctions
static const struct @2 HeaderFunctions[]
MailCommon::HeadersRuleWidgetHandler::handlesField
bool handlesField(const QByteArray &field) const
Definition: headersrulerwidgethandler.cpp:189
I18N_NOOP
#define I18N_NOOP(t)
Definition: searchpatternedit.cpp:46
MailCommon::RegExpLineEdit::showEditButton
void showEditButton(bool)
Definition: regexplineedit.cpp:107
displayName
const char * displayName
Definition: headersrulerwidgethandler.cpp:38
MailCommon::HeadersRuleWidgetHandler::value
QString value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
Definition: headersrulerwidgethandler.cpp:149
MailCommon::SearchRule::FuncEquals
Definition: searchpattern.h:84
MailCommon::HeadersRuleWidgetHandler::setRule
bool setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isNepomukSearch) const
Definition: headersrulerwidgethandler.cpp:222
headersrulerwidgethandler.h
MailCommon::HeadersRuleWidgetHandler::createValueWidget
QWidget * createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
Definition: headersrulerwidgethandler.cpp:82
MailCommon::RegExpLineEdit::text
QString text() const
Definition: regexplineedit.cpp:97
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal