kmail

headerlistquicksearch.cpp
1 /*
2  This file is part of KMail, the KDE mail client.
3  Copyright (c) 2004 Till Adam <adam@kde.org>
4 
5  KMail is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  KMail is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19  In addition, as a special exception, the copyright holders give
20  permission to link the code of this program with any edition of
21  the TQt library by Trolltech AS, Norway (or with modified versions
22  of TQt that use the same license as TQt), and distribute linked
23  combinations including the two. You must obey the GNU General
24  Public License in all respects for all of the code used other than
25  TQt. If you modify this file, you may extend this exception to
26  your version of the file, but you are not obligated to do so. If
27  you do not wish to do so, delete this exception statement from
28  your version.
29 */
30 #include "headerlistquicksearch.h"
31 
32 #include <tqapplication.h>
33 #include <tqlabel.h>
34 #include <tqcombobox.h>
35 #include <tqvaluevector.h>
36 #include <tqtimer.h>
37 
38 #include <tdeaction.h>
39 #include <kiconloader.h>
40 #include <tdelistview.h>
41 #include <tdelocale.h>
42 #include <tdetoolbarbutton.h>
43 
44 #include "kmheaders.h"
45 #include "kmsearchpattern.h"
46 #include "kmmainwidget.h"
47 
48 namespace KMail {
49 
50 HeaderListQuickSearch::HeaderListQuickSearch( TQWidget *parent,
51  TDEListView *listView,
52  TDEActionCollection *actionCollection,
53  const char *name )
54  : TDEListViewSearchLine(parent, listView, name), mStatusCombo(0), mStatus(0), statusList()
55 {
56  TDEAction *resetQuickSearch = new TDEAction( i18n( "Reset Quick Search" ),
57  TQApplication::reverseLayout()
58  ? "clear_left"
59  : "locationbar_erase",
60  0, this,
61  TQ_SLOT( reset() ),
62  actionCollection,
63  "reset_quicksearch" );
64  resetQuickSearch->plug( parent );
65  resetQuickSearch->setWhatsThis( i18n( "Reset Quick Search\n"
66  "Resets the quick search so that "
67  "all messages are shown again." ) );
68 
69  TQLabel *label = new TQLabel( i18n("Stat&us:"), parent, "tde toolbar widget" );
70 
71  mStatusCombo = new TQComboBox( parent, "quick search status combo box" );
72  mStatusCombo->setSizeLimit( 12 );
73  mStatusCombo->insertItem( SmallIcon( "system-run" ), i18n("Any Status") );
74 
75  inserStatus( StatusUnread );
76  inserStatus( StatusNew );
77  inserStatus( StatusImportant );
78  inserStatus( StatusReplied );
79  inserStatus( StatusForwarded );
80  inserStatus( StatusToDo );
81  inserStatus( StatusHasAttachment );
82  inserStatus( StatusInvitation );
83  inserStatus( StatusWatched );
84  inserStatus( StatusIgnored );
85  mStatusCombo->setCurrentItem( 0 );
86  mStatusCombo->installEventFilter( this );
87  connect( mStatusCombo, TQ_SIGNAL ( activated( int ) ),
88  this, TQ_SLOT( slotStatusChanged( int ) ) );
89 
90  label->setBuddy( mStatusCombo );
91 
92  TDEToolBarButton * btn = new TDEToolBarButton( "mail_find", 0, parent,
93  0, i18n( "Open Full Search" ) );
94  connect( btn, TQ_SIGNAL( clicked() ), TQ_SIGNAL( requestFullSearch() ) );
95 
96  /* Disable the signal connected by TDEListViewSearchLine since it will call
97  * itemAdded during KMHeaders::readSortOrder() which will in turn result
98  * in getMsgBaseForItem( item ) wanting to access items which are no longer
99  * there. Rather rely on KMHeaders::msgAdded and its signal. */
100  disconnect(listView, TQ_SIGNAL(itemAdded(TQListViewItem *)),
101  this, TQ_SLOT(itemAdded(TQListViewItem *)));
102  KMHeaders *headers = static_cast<KMHeaders*>( listView );
103  connect( headers, TQ_SIGNAL( msgAddedToListView( TQListViewItem* ) ),
104  this, TQ_SLOT( itemAdded( TQListViewItem* ) ) );
105 
106 }
107 
108 HeaderListQuickSearch::~HeaderListQuickSearch()
109 {
110 }
111 
112 
113 bool HeaderListQuickSearch::eventFilter( TQObject *watched, TQEvent *event )
114 {
115  if ( watched == mStatusCombo ) {
116  KMMainWidget *mainWidget = 0;
117 
118  // Travel up the parents list until we find the main widget
119  for ( TQWidget *curWidget = parentWidget(); curWidget; curWidget = curWidget->parentWidget() ) {
120  mainWidget = ::tqt_cast<KMMainWidget *>( curWidget );
121  if ( mainWidget )
122  break;
123  }
124 
125  if ( mainWidget ) {
126  switch ( event->type() ) {
127  case TQEvent::FocusIn:
128  mainWidget->setAccelsEnabled( false );
129  break;
130  case TQEvent::FocusOut:
131  mainWidget->setAccelsEnabled( true );
132  break;
133  default:
134  // Avoid compiler warnings
135  break;
136  }
137  }
138  }
139 
140  // In either case, always return false, we NEVER want to eat the event
141  return false;
142 }
143 
144 
145 bool HeaderListQuickSearch::itemMatches(const TQListViewItem *item, const TQString &s) const
146 {
147  mCurrentSearchTerm = s; // bit of a hack, but works
148  if ( mStatus != 0 ) {
149  KMHeaders *headers = static_cast<KMHeaders*>( item->listView() );
150  const KMMsgBase *msg = headers->getMsgBaseForItem( item );
151  if ( !msg || ! ( msg->status() & mStatus ) )
152  return false;
153  }
154 
155  // The full email address is not visible, but we still want it to be searchable.
156  // TDEListViewSearchLine::itemMatches() only searches in visible columns.
157  const HeaderItem *headerItem = static_cast<const HeaderItem*>( item );
158  if ( headerItem->from().lower().contains( s.lower() ) ) {
159  return true;
160  }
161  if ( headerItem->to().lower().contains( s.lower() ) ) {
162  return true;
163  }
164 
165  return TDEListViewSearchLine::itemMatches(item, s);
166 }
167 
168 //-----------------------------------------------------------------------------
169 void HeaderListQuickSearch::reset()
170 {
171  clear();
172  mStatusCombo->setCurrentItem( 0 );
173  slotStatusChanged( 0 );
174 }
175 
176 void HeaderListQuickSearch::slotStatusChanged( int index )
177 {
178  if ( index == 0 )
179  mStatus = 0;
180  else
181  mStatus = KMSearchRuleStatus::statusFromEnglishName( statusList[index - 1] );
182  updateSearch();
183 }
184 
185 void HeaderListQuickSearch::inserStatus(KMail::StatusValueTypes which)
186 {
187  mStatusCombo->insertItem( SmallIcon( KMail::StatusValues[which].icon ),
188  i18n( KMail::StatusValues[ which ].text ) );
189  statusList.append( KMail::StatusValues[ which ].text );
190 }
191 
192 
193 TQString HeaderListQuickSearch::currentSearchTerm() const
194 {
195  return mCurrentSearchTerm;
196 }
197 
198 
199 int HeaderListQuickSearch::currenStatus() const
200 {
201  return mStatus;
202 }
203 
204 } // namespace KMail
205 
206 #include "headerlistquicksearch.moc"
The widget that shows the contents of folders.
Definition: kmheaders.h:47
const KMMsgBase * getMsgBaseForItem(const TQListViewItem *item) const
gets the message represented by the item as a KMMsgBase.
Definition: kmheaders.cpp:2609
Visual representation of a member of the set of displayables (mails in the current folder).
Definition: headeritem.h:164
folderdiaquotatab.h
Definition: aboutdata.cpp:40