akregator/src

searchbar.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
5 
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.
10 
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.
15 
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, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include "akregatorconfig.h"
26 #include "articlefilter.h"
27 #include "article.h"
28 #include "searchbar.h"
29 
30 #include <kcombobox.h>
31 #include <kiconloader.h>
32 #include <klineedit.h>
33 #include <tdelocale.h>
34 #include <kstandarddirs.h>
35 
36 #include <tqapplication.h>
37 #include <tqhbox.h>
38 #include <tqlabel.h>
39 #include <tqpixmap.h>
40 #include <tqstring.h>
41 #include <tqtimer.h>
42 #include <tqtoolbutton.h>
43 #include <tqtooltip.h>
44 
47 
48 namespace Akregator
49 {
50 
51 class SearchBar::SearchBarPrivate
52 {
53 public:
56  TQString searchText;
57  TQTimer timer;
58  KLineEdit* searchLine;
59  KComboBox* searchCombo;
60  int delay;
61 };
62 
63 SearchBar::SearchBar(TQWidget* parent, const char* name) : TQHBox(parent, name), d(new SearchBar::SearchBarPrivate)
64 {
65  d->delay = 400;
66  setMargin(2);
67  setSpacing(5);
68  setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed ) );
69  TQToolButton *clearButton = new TQToolButton(this);
70  clearButton->setIconSet( SmallIconSet( TQApplication::reverseLayout() ? "clear_left" : "locationbar_erase" ) );
71 
72  clearButton->setAutoRaise(true);
73 
74  TQLabel* searchLabel = new TQLabel(this);
75  searchLabel->setText( i18n("S&earch:") );
76 
77  d->searchLine = new KLineEdit(this, "searchline");
78  connect(d->searchLine, TQ_SIGNAL(textChanged(const TQString &)),
79  this, TQ_SLOT(slotSearchStringChanged(const TQString &)));
80 
81  searchLabel->setBuddy(d->searchLine);
82 
83  TQLabel* statusLabel = new TQLabel(this);
84  statusLabel->setText( i18n("Status:") );
85 
86  d->searchCombo = new KComboBox(this, "searchcombo");
87  TQPixmap iconAll = TDEGlobal::iconLoader()->loadIcon("application-x-executable", TDEIcon::Small);
88  TQPixmap iconNew(locate("data", "akregator/pics/kmmsgnew.png"));
89  TQPixmap iconUnread(locate("data", "akregator/pics/kmmsgunseen.png"));
90  TQPixmap iconKeep(locate("data", "akregator/pics/kmmsgflag.png"));
91 
92  d->searchCombo->insertItem(iconAll, i18n("All Articles"));
93  d->searchCombo->insertItem(iconUnread, i18n("Unread"));
94  d->searchCombo->insertItem(iconNew, i18n("New"));
95  d->searchCombo->insertItem(iconKeep, i18n("Important"));
96 
97  TQToolTip::add( clearButton, i18n( "Clear filter" ) );
98  TQToolTip::add( d->searchLine, i18n( "Enter space-separated terms to filter article list" ) );
99  TQToolTip::add( d->searchCombo, i18n( "Choose what kind of articles to show in article list" ) );
100 
101  connect(clearButton, TQ_SIGNAL( clicked() ),
102  this, TQ_SLOT(slotClearSearch()) );
103 
104  connect(d->searchCombo, TQ_SIGNAL(activated(int)),
105  this, TQ_SLOT(slotSearchComboChanged(int)));
106 
107  connect(&(d->timer), TQ_SIGNAL(timeout()), this, TQ_SLOT(slotActivateSearch()));
108 }
109 
110 SearchBar::~SearchBar()
111 {
112  delete d;
113  d = 0;
114 }
115 
116 TQString SearchBar::text() const
117 {
118  return d->searchText;
119 }
120 
121 int SearchBar::status() const
122 {
123  return d->searchCombo->currentItem();
124 }
125 
126 void SearchBar::setDelay(int ms)
127 {
128  d->delay = ms;
129 }
130 
131 int SearchBar::delay() const
132 {
133  return d->delay;
134 }
135 
136 void SearchBar::slotClearSearch()
137 {
138  if (status() != 0 || !d->searchLine->text().isEmpty())
139  {
140  d->searchLine->clear();
141  d->searchCombo->setCurrentItem(0);
142  d->timer.stop();
143  slotActivateSearch();
144  }
145 }
146 
147 void SearchBar::slotSetStatus(int status)
148 {
149  d->searchCombo->setCurrentItem(status);
150  slotSearchComboChanged(status);
151 }
152 
153 void SearchBar::slotSetText(const TQString& text)
154 {
155  d->searchLine->setText(text);
156  slotSearchStringChanged(text);
157 }
158 
159 void SearchBar::slotSearchComboChanged(int /*index*/)
160 {
161  if (d->timer.isActive())
162  d->timer.stop();
163 
164  d->timer.start(200, true);
165 }
166 
167 void SearchBar::slotSearchStringChanged(const TQString& search)
168 {
169  d->searchText = search;
170  if (d->timer.isActive())
171  d->timer.stop();
172 
173  d->timer.start(200, true);
174 }
175 
176 void SearchBar::slotActivateSearch()
177 {
178  TQValueList<Criterion> textCriteria;
179  TQValueList<Criterion> statusCriteria;
180 
181  if (!d->searchText.isEmpty())
182  {
183  Criterion subjCrit( Criterion::Title, Criterion::Contains, d->searchText);
184  textCriteria << subjCrit;
185  Criterion crit1( Criterion::Description, Criterion::Contains, d->searchText);
186  textCriteria << crit1;
187  Criterion crit2( Criterion::Author, Criterion::Contains, d->searchText);
188  textCriteria << crit2;
189  }
190 
191  if (d->searchCombo->currentItem())
192  {
193  switch (d->searchCombo->currentItem())
194  {
195  case 1: // Unread
196  {
197  Criterion crit1( Criterion::Status, Criterion::Equals, Article::New);
198  Criterion crit2( Criterion::Status, Criterion::Equals, Article::Unread);
199  statusCriteria << crit1;
200  statusCriteria << crit2;
201  break;
202  }
203  case 2: // New
204  {
205  Criterion crit( Criterion::Status, Criterion::Equals, Article::New);
206  statusCriteria << crit;
207  break;
208  }
209  case 3: // Keep flag set
210  {
211  Criterion crit( Criterion::KeepFlag, Criterion::Equals, true);
212  statusCriteria << crit;
213  break;
214  }
215  default:
216  break;
217  }
218  }
219 
220  d->textFilter = ArticleMatcher(textCriteria, ArticleMatcher::LogicalOr);
221  d->statusFilter = ArticleMatcher(statusCriteria, ArticleMatcher::LogicalOr);
222  Settings::setStatusFilter(d->searchCombo->currentItem());
223  Settings::setTextFilter(d->searchText);
224  emit signalSearch(d->textFilter, d->statusFilter);
225 }
226 
227 }
228 
229 #include "searchbar.moc"
a powerful matcher supporting multiple criterions, which can be combined via logical OR or AND
Criterion for ArticleMatcher.