kmail

headerlistquicksearch.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KMail, the KDE mail client.
00003     Copyright (c) 2004 Till Adam <adam@kde.org>
00004 
00005     KMail is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     KMail is distributed in the hope that it will be useful, but
00011     WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     In addition, as a special exception, the copyright holders give
00020     permission to link the code of this program with any edition of
00021     the Qt library by Trolltech AS, Norway (or with modified versions
00022     of Qt that use the same license as Qt), and distribute linked
00023     combinations including the two.  You must obey the GNU General
00024     Public License in all respects for all of the code used other than
00025     Qt.  If you modify this file, you may extend this exception to
00026     your version of the file, but you are not obligated to do so.  If
00027     you do not wish to do so, delete this exception statement from
00028     your version.
00029 */
00030 #include "headerlistquicksearch.h"
00031 
00032 #include <qapplication.h>
00033 #include <qlabel.h>
00034 #include <qcombobox.h>
00035 #include <qvaluevector.h>
00036 #include <qtimer.h>
00037 
00038 #include <kaction.h>
00039 #include <kiconloader.h>
00040 #include <klistview.h>
00041 #include <klocale.h>
00042 #include <ktoolbarbutton.h>
00043 
00044 #include "kmheaders.h"
00045 #include "kmsearchpattern.h"
00046 #include "kmmainwidget.h"
00047 
00048 namespace KMail {
00049 
00050 HeaderListQuickSearch::HeaderListQuickSearch( QWidget *parent,
00051                                               KListView *listView,
00052                                               KActionCollection *actionCollection,
00053                                               const char *name )
00054   : KListViewSearchLine(parent, listView, name), mStatusCombo(0), mStatus(0),  statusList()
00055 {
00056   KAction *resetQuickSearch = new KAction( i18n( "Reset Quick Search" ),
00057                                            QApplication::reverseLayout()
00058                                            ? "clear_left"
00059                                            : "locationbar_erase",
00060                                            0, this,
00061                                            SLOT( reset() ),
00062                                            actionCollection,
00063                                            "reset_quicksearch" );
00064   resetQuickSearch->plug( parent );
00065   resetQuickSearch->setWhatsThis( i18n( "Reset Quick Search\n"
00066                                         "Resets the quick search so that "
00067                                         "all messages are shown again." ) );
00068 
00069   QLabel *label = new QLabel( i18n("Stat&us:"), parent, "kde toolbar widget" );
00070 
00071   mStatusCombo = new QComboBox( parent, "quick search status combo box" );
00072   mStatusCombo->insertItem( SmallIcon( "run" ), i18n("Any Status") );
00073 
00074   insertStatus( StatusUnread );
00075   insertStatus( StatusNew );
00076   insertStatus( StatusImportant );
00077   insertStatus( StatusReplied );
00078   insertStatus( StatusForwarded );
00079   insertStatus( StatusToDo );
00080   insertStatus( StatusHasAttachment );
00081   insertStatus( StatusWatched );
00082   insertStatus( StatusIgnored );
00083   mStatusCombo->setCurrentItem( 0 );
00084   mStatusCombo->installEventFilter( this );
00085   connect( mStatusCombo, SIGNAL ( activated( int ) ),
00086            this, SLOT( slotStatusChanged( int ) ) );
00087 
00088   label->setBuddy( mStatusCombo );
00089 
00090   KToolBarButton * btn = new KToolBarButton( "mail_find", 0, parent,
00091                                             0, i18n( "Open Full Search" ) );
00092   connect( btn, SIGNAL( clicked() ), SIGNAL( requestFullSearch() ) );
00093 
00094   /* Disable the signal connected by KListViewSearchLine since it will call 
00095    * itemAdded during KMHeaders::readSortOrder() which will in turn result
00096    * in getMsgBaseForItem( item ) wanting to access items which are no longer
00097    * there. Rather rely on KMHeaders::msgAdded and its signal. */
00098   disconnect(listView, SIGNAL(itemAdded(QListViewItem *)),
00099              this, SLOT(itemAdded(QListViewItem *)));
00100   KMHeaders *headers = static_cast<KMHeaders*>( listView );
00101   connect( headers, SIGNAL( msgAddedToListView( QListViewItem* ) ),
00102            this, SLOT( itemAdded( QListViewItem* ) ) );
00103 
00104 }
00105 
00106 HeaderListQuickSearch::~HeaderListQuickSearch()
00107 {
00108 }
00109 
00110 
00111 bool HeaderListQuickSearch::eventFilter( QObject *watched, QEvent *event )
00112 {
00113   if ( watched == mStatusCombo ) {
00114     KMMainWidget *mainWidget = 0;
00115 
00116     // Travel up the parents list until we find the main widget
00117     for ( QWidget *curWidget = parentWidget(); curWidget; curWidget = curWidget->parentWidget() ) {
00118       mainWidget = ::qt_cast<KMMainWidget *>( curWidget );
00119       if ( mainWidget )
00120         break;
00121     }
00122 
00123     if ( mainWidget ) {
00124       switch ( event->type() ) {
00125       case QEvent::FocusIn:
00126         mainWidget->setAccelsEnabled( false );
00127         break;
00128       case QEvent::FocusOut:
00129         mainWidget->setAccelsEnabled( true );
00130         break;
00131       default:
00132         // Avoid compiler warnings
00133         break;
00134       }
00135     }
00136   }
00137 
00138   // In either case, always return false, we NEVER want to eat the event
00139   return false;
00140 }
00141 
00142 
00143 bool HeaderListQuickSearch::itemMatches(const QListViewItem *item, const QString &s) const
00144 {
00145   mCurrentSearchTerm = s; // bit of a hack, but works
00146   if ( mStatus != 0 ) {
00147     KMHeaders *headers = static_cast<KMHeaders*>( item->listView() );
00148     const KMMsgBase *msg = headers->getMsgBaseForItem( item );
00149     if ( !msg || ! ( msg->status() & mStatus ) )
00150       return false;
00151   }
00152   return KListViewSearchLine::itemMatches(item, s);
00153 }
00154 
00155 //-----------------------------------------------------------------------------
00156 void HeaderListQuickSearch::reset()
00157 {
00158   clear();
00159   mStatusCombo->setCurrentItem( 0 );
00160   slotStatusChanged( 0 );
00161 }
00162 
00163 void HeaderListQuickSearch::slotStatusChanged( int index )
00164 {
00165   if ( index == 0 )
00166     mStatus = 0;
00167   else
00168     mStatus = KMSearchRuleStatus::statusFromEnglishName( statusList[index - 1] );
00169   updateSearch();
00170 }
00171 
00172 void HeaderListQuickSearch::insertStatus(KMail::StatusValueTypes which)
00173 {
00174   mStatusCombo->insertItem( SmallIcon( KMail::StatusValues[which].icon ),
00175     i18n( KMail::StatusValues[ which ].text ) );
00176   statusList.append( KMail::StatusValues[ which ].text );
00177 }
00178 
00179 
00180 QString HeaderListQuickSearch::currentSearchTerm() const
00181 {
00182     return mCurrentSearchTerm;
00183 }
00184 
00185 
00186 int HeaderListQuickSearch::currentStatus() const
00187 {
00188     return mStatus;
00189 }
00190 
00191 } // namespace KMail
00192 
00193 #include "headerlistquicksearch.moc"