OSDN Git Service

f9727977c981edfe3f91f49361517b6a4e8748aa
[kita/kita.git] / kita / src / kitasubjectview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2003 by Hideki Ikemoto                                  *
3  *   ikemo@users.sourceforge.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 files for Qt
12 #include <qtoolbutton.h>
13 #include <qtextbrowser.h>
14 #include <qtextcodec.h>
15 #include <qregexp.h>
16 #include <qfile.h>
17
18 // kdelibs/kio
19 #include <kio/slaveconfig.h>
20 #include <kio/netaccess.h>
21
22 // include files for KDE
23 #include <kfilterdev.h>
24 #include <klistview.h>
25 #include <kiconloader.h>
26 #include <klocale.h>
27
28 #include "kitasubjectview.h"
29 #include "favoritelistview.h"
30 #include "kita.h"
31 #include "kitacacheinfo.h"
32 #include "part/kita2ch.h"
33 #include "libkita/thread.h"
34
35 enum SubjectViewRows {
36   Row_ID,
37   Row_Icon,
38   Row_Subject,
39   Row_ResNum,
40   Row_Vested,
41   Row_Unread,
42   Row_DatName,
43   Row_DatURL
44 };
45
46 KitaSubjectView::KitaSubjectView(QWidget *parent, const char *name)
47   : KitaSubjectViewBase(parent, name)
48 {
49   {
50     SearchButton->setPixmap( SmallIcon("find") );
51     HideButton->setPixmap( SmallIcon("filter") );
52   }
53
54   subjectList->addColumn(i18n("No."));
55   subjectList->addColumn("");
56   subjectList->addColumn(i18n("Title"));
57   subjectList->addColumn(i18n("ResNum"));
58   subjectList->addColumn(i18n("Vested"));
59   subjectList->addColumn(i18n("Unread"));
60   subjectList->addColumn(i18n("Dat"));
61
62   subjectList->setColumnWidth(Row_Subject, 400);
63   subjectList->setColumnWidthMode(Row_Subject, QListView::Manual);
64
65   connect(subjectList, SIGNAL(clicked(QListViewItem*)), SLOT(loadThread(QListViewItem*)));
66   connect( SearchButton, SIGNAL( clicked() ), SLOT( slotSearchButton() ) );
67   connect( SearchCombo, SIGNAL( activated(int) ), SLOT( slotSearchButton() ) );
68   connect(this, SIGNAL(signalSubjectListClicked(QListViewItem*)), subjectList, SIGNAL(clicked(QListViewItem*)));
69   connect( HideButton, SIGNAL( toggled(bool) ), SLOT( slotHideButton(bool) ) );
70   connect( m_subjectTab, SIGNAL( currentChanged(QWidget*) ), SLOT( slotCurrentChanged(QWidget*) ) );
71 }
72
73 KitaSubjectView::~KitaSubjectView()
74 {
75 }
76
77 void KitaSubjectView::reloadSubject()
78 {
79 }
80
81 void KitaSubjectView::loadThread(QListViewItem* item)
82 {
83   if( ! item ) return;
84
85   QString datName = item->text(Row_DatName);
86   KURL datURL = m_board.url();
87   datURL.addPath("/dat/" + datName);
88   emit signalShowThread(m_board, Kita::Thread(datURL));
89 }
90
91 void KitaSubjectView::loadBoard(const Kita::Board& board)
92 {
93   // reset member variables.
94   {
95     m_hitList.clear();
96     m_nextHitIndex = 0;
97     m_prevquery = "";
98   }
99
100   m_board = board;
101   m_board.refreshChildList();
102
103   KitaCacheInfo* cache = KitaCacheInfo::getInstance();
104   m_threadList = m_board.getThreadList();
105
106   // clear list
107   subjectList->clear();
108
109   for(unsigned i=0; i<m_threadList.count(); i++) {
110     Kita::Thread* thread = m_threadList.at(i);
111
112     int vested = cache->getVestedNum(thread->datURL());
113     new KListViewItem(subjectList,
114                       QString("%1").arg(i+1, 4),
115                       "",
116                       thread->name(),
117                       QString("%1").arg(thread->resNum(), 4),
118                       (vested > 0) ? QString::number(vested) : QString(""),
119                       (vested > 0 && thread->resNum() != vested) ? QString::number(thread->resNum() - vested) : QString(""),
120                       thread->datURL().fileName(),
121                       thread->datURL().prettyURL());
122   }
123
124   if( HideButton->isOn() ) {
125     HideButton->toggle();
126   }
127   emit loadBoardCompleted(m_board.url());
128 }
129
130 void KitaSubjectView::slotSearchButton()
131 {
132   insertSearchCombo();
133   QStringList list = parseSearchQuery( SearchCombo->currentText() );
134   searchNext( list );
135 }
136
137 void KitaSubjectView::insertSearchCombo()
138 {
139   int count;
140   bool found = false;
141
142   for( count = 0; count < SearchCombo->count(); ++count ) {
143     if ( SearchCombo->text( count ) ==  SearchCombo->currentText() ) {
144       found = true;
145       break;
146     }
147   }
148   if ( ! found ) SearchCombo->insertItem( SearchCombo->currentText() );
149 }
150
151 QStringList KitaSubjectView::parseSearchQuery(const QString &input)
152 {
153   QStringList tmp = QStringList::split( ' ', input );
154   QStringList ret_list;
155   QRegExp truncSpace("\\s*$");
156   QStringList::iterator it = tmp.begin();
157   for( ; it != tmp.end(); ++it )
158     ret_list += (*it).replace( truncSpace, "" );
159   return ret_list;
160 }
161
162 void KitaSubjectView::searchNext(const QStringList &query)
163 {
164   if ( query.isEmpty() ) return;
165
166   if ( query != m_prevquery ) {
167     searchAll( query );
168     slotHideButton( HideButton->isOn() );
169     m_nextHitIndex = 0; //A next jump-search target reset to '0'.
170     return;
171   }
172
173   if(m_nextHitIndex >= m_hitList.size()) return;
174
175   KListViewItem* item = m_hitList[m_nextHitIndex];
176   subjectList->ensureItemVisible(item);
177   subjectList->setSelected(item, true);
178   m_nextHitIndex++;
179   if(m_nextHitIndex >= m_hitList.size()) m_nextHitIndex = 0;
180 }
181
182 void KitaSubjectView::searchAll(const QStringList &query)
183 {
184   m_hitList.clear();
185   m_prevquery = query;
186
187   QListViewItemIterator listIt( subjectList );
188   while( listIt.current() != 0 ) {
189     KListViewItem* item = static_cast<KListViewItem*>(listIt.current());
190     item->setPixmap(Row_Icon, 0);
191
192     QStringList::const_iterator queryIt = query.begin();
193     for( ; queryIt != query.end(); ++queryIt ) {
194       if( item->text(Row_Subject).contains(*queryIt) ) {
195         item->setPixmap(Row_Icon, SmallIcon("find") );
196         m_hitList.append(item);
197         break;
198       }
199     }
200     ++listIt;
201   }
202 }
203
204 void KitaSubjectView::slotHideButton(bool on)
205 {
206   if(m_hitList.empty()) return;
207
208   QListViewItemIterator listIt( subjectList );
209   while( listIt.current() != 0 ) {
210     KListViewItem* item = static_cast<KListViewItem*>(listIt.current());
211     if( on && ! item->pixmap(Row_Icon)) {
212       item->setVisible(false);
213     } else {
214       item->setVisible(true);
215     }
216     ++listIt;
217   }
218 }
219
220 void KitaSubjectView::slotFontChanged(QFont& font)
221 {
222   subjectList->setFont(font);
223 }
224
225 void KitaSubjectView::updateThread(const Kita::Thread& updated_thread)
226 {
227   for(QListViewItem* item = subjectList->firstChild(); item; item = item->nextSibling() ) {
228     if( item->text(Row_DatURL) == updated_thread.datURL().prettyURL() ) {
229       int resNum = item->text( Row_ResNum ).toInt();
230       int vested = updated_thread.resNum();
231       item->setText( Row_Vested, QString::number( vested ) );
232       item->setText( Row_Unread, vested > 0 && resNum != vested ? QString::number(resNum - vested) : QString(""));
233     }
234   }
235 }
236
237 void KitaSubjectView::slotCurrentChanged(QWidget* widget)
238 {
239   if( QString::compare(widget->name(), "favoriteTab") == 0 ) {
240     m_favoriteListView->update();
241   }
242 }