OSDN Git Service

633514fdbf222b4205cfb4a2d165db8ad88821fb
[kita/kita.git] / kita / src / threadlistview.cpp
1 /***************************************************************************
2 *   Copyright (C) 2003 by Hideki Ikemoto                                  *
3 *   ikemo@wakaba.jp                                                       *
4 *                                                                         *
5 *   This program 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
11 #include "threadlistview.h"
12
13 #include <kiconloader.h>
14 #include <klistview.h>
15 #include <klocale.h>
16 #include <kurl.h>
17
18 #include <qtoolbutton.h>
19 #include <qcombobox.h>
20 #include <qregexp.h>
21 #include <qheader.h>
22
23 #include "libkita/thread.h"
24 #include "libkita/kitaconfig.h"
25 #include "libkita/signalcollection.h"
26
27 using namespace Kita;
28
29 struct Col_Attr ThreadListView::s_colAttr[] = {
30     // labelName, itemName, keyName, showDefault
31     {"", "Mark", "Col_Mark", true },
32     {"No.", "ID", "Col_ID", true},
33     {"", "Icon", "Col_Icon", true},
34     {"Title", "Subject", "Col_Subject", true},
35     {"ResNum", "ResNum", "Col_ResNum", true},
36     {"ReadNum", "Read", "Col_Read", true},
37     {"ViewPos", "ViewPos", "Col_ViewPos", true},
38     {"Unread", "Unread", "Col_Unread", true},
39     {"Since", "Since", "Col_Since", true},
40     {"Thread's speed", "Speed", "Col_Speed", true},
41     {"Board", "Board", "Col_Board", false},
42     {"Dat URL", "DatURL", "Col_DatURL", false},
43     {"Mark Order", "MarkOrder", "Col_MarkOrder", false},
44     {"ID Order", "IDOrder", "Col_IDOrder", false}
45 };
46
47 ThreadListView::ThreadListView( QWidget* parent, const char* name )
48         : ThreadListViewBase( parent, name )
49 {
50     SearchButton->setPixmap( SmallIcon( "find" ) );
51     HideButton->setPixmap( SmallIcon( "filter" ) );
52     ReloadButton->setPixmap( SmallIcon( "reload" ) );
53     closeButton->setPixmap( SmallIcon( "fileclose" ) );
54
55     QHeader* header = subjectList->header();
56     for( int i = Col_Begin; i <= Col_End; i++ ) {
57         subjectList->addColumn( i18n( s_colAttr[i].labelName ) );
58
59         if ( s_colAttr[i].showDefault != true ) {
60             subjectList->setColumnWidthMode( i, QListView::Manual );
61             header->setResizeEnabled( false, i );
62             subjectList->setColumnWidth( i, 0 );
63         }
64     }
65     header->setStretchEnabled( true, Col_Subject );
66
67     connect( SearchButton, SIGNAL( clicked() ),
68              SLOT( slotSearchButton() ) );
69     connect( SearchCombo, SIGNAL( activated( int ) ),
70              SLOT( slotSearchButton() ) );
71     connect( HideButton, SIGNAL( toggled( bool ) ),
72              SLOT( slotHideButton( bool ) ) );
73     connect( subjectList, SIGNAL( mouseButtonClicked( int, QListViewItem*, const QPoint&, int ) ),
74              SLOT( slotMouseButtonClicked( int, QListViewItem* ) ) );
75     connect( this, SIGNAL( bookmarked( const QString&, bool ) ),
76              Kita::SignalCollection::getInstance(), SIGNAL( bookmarked( const QString&, bool ) ) );
77     connect( this, SIGNAL( openURLRequestExt(
78                                const KURL&, const KParts::URLArgs&, QString, int, int,
79                                const KURL&, const KURL&, const QString&, const QString& ) ),
80              Kita::SignalCollection::getInstance(), SIGNAL( openURLRequestExt(
81                          const KURL& , const KParts::URLArgs&, QString, int, int,
82                          const KURL&, const KURL&, const QString&, const QString& ) ) );
83 }
84
85 ThreadListView::~ThreadListView()
86 {}
87
88 void ThreadListView::slotSearchButton()
89 {
90     insertSearchCombo();
91     QStringList list = parseSearchQuery( SearchCombo->currentText() );
92     searchNext( list );
93 }
94
95 void ThreadListView::insertSearchCombo()
96 {
97     for ( int count = 0; count < SearchCombo->count(); ++count ) {
98         if ( SearchCombo->text( count ) == SearchCombo->currentText() ) {
99             return ;
100         }
101     }
102     SearchCombo->insertItem( SearchCombo->currentText() );
103 }
104
105 QStringList ThreadListView::parseSearchQuery( const QString &input )
106 {
107     QStringList tmp = QStringList::split( ' ', input );
108     QStringList ret_list;
109     QRegExp truncSpace( "\\s*$" );
110     QStringList::iterator it = tmp.begin();
111
112     for ( ; it != tmp.end(); ++it ) {
113         ret_list += ( *it ).replace( truncSpace, "" );
114     }
115     return ret_list;
116 }
117
118 void ThreadListView::searchNext( const QStringList &query )
119 {
120     if ( query.isEmpty() ) return ;
121
122     if ( query != m_prevquery ) {
123         searchAll( query );
124         slotHideButton( HideButton->isOn() );
125         m_nextHitIndex = 0; //A next jump-search target reset to '0'.
126         return ;
127     }
128
129     if ( m_nextHitIndex >= m_hitList.size() ) {
130         return ;
131     }
132
133     KListViewItem* item = m_hitList[ m_nextHitIndex ];
134     subjectList->ensureItemVisible( item );
135     subjectList->setSelected( item, true );
136     m_nextHitIndex++;
137     if ( m_nextHitIndex >= m_hitList.size() ) m_nextHitIndex = 0;
138 }
139
140 void ThreadListView::searchAll( const QStringList &query )
141 {
142     m_hitList.clear();
143     m_prevquery = query;
144
145     QListViewItemIterator listIt( subjectList );
146     while ( listIt.current() != 0 ) {
147         KListViewItem * item = static_cast<KListViewItem*>( listIt.current() );
148         item->setPixmap( Col_Icon, 0 );
149
150         QStringList::const_iterator queryIt = query.begin();
151         for ( ; queryIt != query.end(); ++queryIt ) {
152             if ( item->text( Col_Subject ).contains( *queryIt, false ) ) {
153                 item->setPixmap( Col_Icon, SmallIcon( "find" ) );
154                 m_hitList.append( item );
155                 break;
156             }
157         }
158         ++listIt;
159     }
160 }
161
162 void ThreadListView::slotHideButton( bool on )
163 {
164     if ( m_hitList.empty() ) return ;
165
166     QListViewItemIterator listIt( subjectList );
167     while ( listIt.current() != 0 ) {
168         KListViewItem * item = static_cast<KListViewItem *>( listIt.current() );
169         if ( on && ! item->pixmap( Col_Icon ) ) {
170             item->setVisible( false );
171         } else {
172             item->setVisible( true );
173         }
174         ++listIt;
175     }
176 }
177
178 void ThreadListView::slotMouseButtonClicked( int button, QListViewItem* item )
179 {
180     if ( ! item ) return ;
181
182     KURL datURL = item->text( Col_DatURL );
183
184     switch ( button ) {
185     case MidButton:
186         emit openURLRequestExt( datURL.prettyURL(), KParts::URLArgs(), "kita_open_2chthread", 1 );
187         break;
188     case LeftButton:
189         if ( KitaConfig::alwaysUseTab() ) {
190             emit openURLRequestExt( datURL.prettyURL(), KParts::URLArgs(), "kita_open_2chthread", 1 );
191         } else {
192             emit openURLRequestExt( datURL.prettyURL(), KParts::URLArgs(), "kita_open_2chthread", 0 );
193         }
194         break;
195     }
196 }
197
198 int ThreadListViewItem::compare( QListViewItem* i, int col, bool ascending ) const
199 {
200     switch ( col ) {
201     case Col_ResNum:
202     case Col_Read:
203     case Col_Unread:
204         return i->key( col, ascending ).toInt() - key( col, ascending ).toInt();
205     case Col_ID:
206         return key( Col_IDOrder, ascending ).toInt() - i->key( Col_IDOrder, ascending ).toInt();
207     case Col_Mark:
208         return QString::localeAwareCompare( i->key( Col_MarkOrder, ascending ), key( Col_MarkOrder, ascending ) );
209     case Col_Since:
210         return QString::localeAwareCompare( i->key( col, ascending ), key( col, ascending ) );
211     case Col_Speed:
212         return static_cast<int>( i->key( col, ascending ).toDouble() * 1000 - key( col, ascending ).toDouble() * 1000 );
213     default:
214         return QString::localeAwareCompare( key( col, ascending ), i->key( col, ascending ) );
215     }
216 }
217
218 #include "threadlistview.moc"