kmail

headerlistquicksearch.cpp

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 
00043 #include "kmheaders.h"
00044 #include "kmsearchpattern.h"
00045 #include "kmmainwidget.h"
00046 
00047 namespace KMail {
00048 
00049 HeaderListQuickSearch::HeaderListQuickSearch( QWidget *parent,
00050                                               KListView *listView,
00051                                               KActionCollection *actionCollection,
00052                                               const char *name )
00053   : KListViewSearchLine(parent, listView, name), mStatusCombo(0), mStatus(0),  statusList()
00054 {
00055   KAction *resetQuickSearch = new KAction( i18n( "Reset Quick Search" ),
00056                                            QApplication::reverseLayout()
00057                                            ? "clear_left"
00058                                            : "locationbar_erase",
00059                                            0, this,
00060                                            SLOT( reset() ),
00061                                            actionCollection,
00062                                            "reset_quicksearch" );
00063   resetQuickSearch->plug( parent );
00064   resetQuickSearch->setWhatsThis( i18n( "Reset Quick Search\n"
00065                                         "Resets the quick search so that "
00066                                         "all messages are shown again." ) );
00067 
00068   QLabel *label = new QLabel( i18n("Stat&us:"), parent, "kde toolbar widget" );
00069 
00070   mStatusCombo = new QComboBox( parent, "quick search status combo box" );
00071   mStatusCombo->insertItem( SmallIcon( "run" ), i18n("Any Status") );
00072 
00073   insertStatus( StatusUnread );
00074   insertStatus( StatusNew );
00075   insertStatus( StatusImportant );
00076   insertStatus( StatusReplied );
00077   insertStatus( StatusForwarded );
00078   insertStatus( StatusToDo );
00079   insertStatus( StatusHasAttachment );
00080   insertStatus( StatusWatched );
00081   insertStatus( StatusIgnored );
00082   mStatusCombo->setCurrentItem( 0 );
00083   mStatusCombo->installEventFilter( this );
00084   connect( mStatusCombo, SIGNAL ( activated( int ) ),
00085            this, SLOT( slotStatusChanged( int ) ) );
00086 
00087   label->setBuddy( mStatusCombo );
00088 
00089   /* Disable the signal connected by KListViewSearchLine since it will call 
00090    * itemAdded during KMHeaders::readSortOrder() which will in turn result
00091    * in getMsgBaseForItem( item ) wanting to access items which are no longer
00092    * there. Rather rely on KMHeaders::msgAdded and its signal. */
00093   disconnect(listView, SIGNAL(itemAdded(QListViewItem *)),
00094              this, SLOT(itemAdded(QListViewItem *)));
00095   KMHeaders *headers = static_cast<KMHeaders*>( listView );
00096   connect( headers, SIGNAL( msgAddedToListView( QListViewItem* ) ),
00097            this, SLOT( itemAdded( QListViewItem* ) ) );
00098 
00099 }
00100 
00101 HeaderListQuickSearch::~HeaderListQuickSearch()
00102 {
00103 }
00104 
00105 
00106 bool HeaderListQuickSearch::eventFilter( QObject *watched, QEvent *event )
00107 {
00108   if ( watched == mStatusCombo ) {
00109     KMMainWidget *mainWidget = 0;
00110 
00111     // Travel up the parents list until we find the main widget
00112     for ( QWidget *curWidget = parentWidget(); curWidget; curWidget = curWidget->parentWidget() ) {
00113       mainWidget = ::qt_cast<KMMainWidget *>( curWidget );
00114       if ( mainWidget )
00115         break;
00116     }
00117 
00118     if ( mainWidget ) {
00119       switch ( event->type() ) {
00120       case QEvent::FocusIn:
00121         mainWidget->setAccelsEnabled( false );
00122         break;
00123       case QEvent::FocusOut:
00124         mainWidget->setAccelsEnabled( true );
00125         break;
00126       default:
00127         // Avoid compiler warnings
00128         break;
00129       }
00130     }
00131   }
00132 
00133   // In either case, always return false, we NEVER want to eat the event
00134   return false;
00135 }
00136 
00137 
00138 bool HeaderListQuickSearch::itemMatches(const QListViewItem *item, const QString &s) const
00139 {
00140   if ( mStatus != 0 ) {
00141     KMHeaders *headers = static_cast<KMHeaders*>( item->listView() );
00142     const KMMsgBase *msg = headers->getMsgBaseForItem( item );
00143     if ( !msg || ! ( msg->status() & mStatus ) )
00144       return false;
00145   }
00146   return KListViewSearchLine::itemMatches(item, s);
00147 }
00148 
00149 //-----------------------------------------------------------------------------
00150 void HeaderListQuickSearch::reset()
00151 {
00152   clear();
00153   mStatusCombo->setCurrentItem( 0 );
00154   slotStatusChanged( 0 );
00155 }
00156 
00157 void HeaderListQuickSearch::slotStatusChanged( int index )
00158 {
00159   if ( index == 0 )
00160     mStatus = 0;
00161   else
00162     mStatus = KMSearchRuleStatus::statusFromEnglishName( statusList[index - 1] );
00163   updateSearch();
00164 }
00165 
00166 void HeaderListQuickSearch::insertStatus(KMail::StatusValueTypes which)
00167 {
00168   mStatusCombo->insertItem( SmallIcon( KMail::StatusValues[which].icon ),
00169     i18n( KMail::StatusValues[ which ].text ) );
00170   statusList.append( KMail::StatusValues[ which ].text );
00171 }
00172 
00173 } // namespace KMail
00174 
00175 #include "headerlistquicksearch.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys