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 <tqapplication.h>
00033 #include <tqlabel.h>
00034 #include <tqcombobox.h>
00035 #include <tqvaluevector.h>
00036 #include <tqtimer.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( TQWidget *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                                            TQApplication::reverseLayout()
00058                                            ? "clear_left"
00059                                            : "locationbar_erase",
00060                                            0, this,
00061                                            TQT_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   TQLabel *label = new TQLabel( i18n("Stat&us:"), parent, "kde toolbar widget" );
00070 
00071   mStatusCombo = new TQComboBox( parent, "quick search status combo box" );
00072   mStatusCombo->setSizeLimit( 12 );
00073   mStatusCombo->insertItem( SmallIcon( "run" ), i18n("Any Status") );
00074 
00075   insertStatus( StatusUnread );
00076   insertStatus( StatusNew );
00077   insertStatus( StatusImportant );
00078   insertStatus( StatusReplied );
00079   insertStatus( StatusForwarded );
00080   insertStatus( StatusToDo );
00081   insertStatus( StatusHasAttachment );
00082   insertStatus( StatusInvitation );
00083   insertStatus( StatusWatched );
00084   insertStatus( StatusIgnored );
00085   mStatusCombo->setCurrentItem( 0 );
00086   mStatusCombo->installEventFilter( this );
00087   connect( mStatusCombo, TQT_SIGNAL ( activated( int ) ),
00088            this, TQT_SLOT( slotStatusChanged( int ) ) );
00089 
00090   label->setBuddy( mStatusCombo );
00091 
00092   KToolBarButton * btn = new KToolBarButton( "mail_find", 0, parent,
00093                                             0, i18n( "Open Full Search" ) );
00094   connect( btn, TQT_SIGNAL( clicked() ), TQT_SIGNAL( requestFullSearch() ) );
00095 
00096   /* Disable the signal connected by KListViewSearchLine since it will call
00097    * itemAdded during KMHeaders::readSortOrder() which will in turn result
00098    * in getMsgBaseForItem( item ) wanting to access items which are no longer
00099    * there. Rather rely on KMHeaders::msgAdded and its signal. */
00100   disconnect(listView, TQT_SIGNAL(itemAdded(TQListViewItem *)),
00101              this, TQT_SLOT(itemAdded(TQListViewItem *)));
00102   KMHeaders *headers = static_cast<KMHeaders*>( listView );
00103   connect( headers, TQT_SIGNAL( msgAddedToListView( TQListViewItem* ) ),
00104            this, TQT_SLOT( itemAdded( TQListViewItem* ) ) );
00105 
00106 }
00107 
00108 HeaderListQuickSearch::~HeaderListQuickSearch()
00109 {
00110 }
00111 
00112 
00113 bool HeaderListQuickSearch::eventFilter( TQObject *watched, TQEvent *event )
00114 {
00115   if ( watched == mStatusCombo ) {
00116     KMMainWidget *mainWidget = 0;
00117 
00118     // Travel up the parents list until we find the main widget
00119     for ( TQWidget *curWidget = parentWidget(); curWidget; curWidget = curWidget->parentWidget() ) {
00120       mainWidget = ::qt_cast<KMMainWidget *>( curWidget );
00121       if ( mainWidget )
00122         break;
00123     }
00124 
00125     if ( mainWidget ) {
00126       switch ( event->type() ) {
00127       case TQEvent::FocusIn:
00128         mainWidget->setAccelsEnabled( false );
00129         break;
00130       case TQEvent::FocusOut:
00131         mainWidget->setAccelsEnabled( true );
00132         break;
00133       default:
00134         // Avoid compiler warnings
00135         break;
00136       }
00137     }
00138   }
00139 
00140   // In either case, always return false, we NEVER want to eat the event
00141   return false;
00142 }
00143 
00144 
00145 bool HeaderListQuickSearch::itemMatches(const TQListViewItem *item, const TQString &s) const
00146 {
00147   mCurrentSearchTerm = s; // bit of a hack, but works
00148   if ( mStatus != 0 ) {
00149     KMHeaders *headers = static_cast<KMHeaders*>( item->listView() );
00150     const KMMsgBase *msg = headers->getMsgBaseForItem( item );
00151     if ( !msg || ! ( msg->status() & mStatus ) )
00152       return false;
00153   }
00154 
00155   // The full email address is not visible, but we still want it to be searchable.
00156   // KListViewSearchLine::itemMatches() only searches in visible columns.
00157   const HeaderItem *headerItem = static_cast<const HeaderItem*>( item );
00158   if ( headerItem->from().lower().contains( s.lower() ) ) {
00159     return true;
00160   }
00161   if ( headerItem->to().lower().contains( s.lower() ) ) {
00162     return true;
00163   }
00164 
00165   return KListViewSearchLine::itemMatches(item, s);
00166 }
00167 
00168 //-----------------------------------------------------------------------------
00169 void HeaderListQuickSearch::reset()
00170 {
00171   clear();
00172   mStatusCombo->setCurrentItem( 0 );
00173   slotStatusChanged( 0 );
00174 }
00175 
00176 void HeaderListQuickSearch::slotStatusChanged( int index )
00177 {
00178   if ( index == 0 )
00179     mStatus = 0;
00180   else
00181     mStatus = KMSearchRuleStatus::statusFromEnglishName( statusList[index - 1] );
00182   updateSearch();
00183 }
00184 
00185 void HeaderListQuickSearch::insertStatus(KMail::StatusValueTypes which)
00186 {
00187   mStatusCombo->insertItem( SmallIcon( KMail::StatusValues[which].icon ),
00188     i18n( KMail::StatusValues[ which ].text ) );
00189   statusList.append( KMail::StatusValues[ which ].text );
00190 }
00191 
00192 
00193 TQString HeaderListQuickSearch::currentSearchTerm() const
00194 {
00195     return mCurrentSearchTerm;
00196 }
00197 
00198 
00199 int HeaderListQuickSearch::currentStatus() const
00200 {
00201     return mStatus;
00202 }
00203 
00204 } // namespace KMail
00205 
00206 #include "headerlistquicksearch.moc"