OSDN Git Service

fa6f512da776bf6c5f5a2cf7c36970337d8bc7a9
[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 "kitasubjectview.h"
12
13 // include files for Qt
14 #include <qtoolbutton.h>
15 #include <qtextbrowser.h>
16 #include <qtextcodec.h>
17 #include <qregexp.h>
18 #include <qfile.h>
19 #include <qclipboard.h>
20 #include <qapplication.h>
21 #include <qdatetime.h>
22 #include <qlabel.h>
23
24 // kdelibs/kio
25 #include <kio/slaveconfig.h>
26 #include <kio/netaccess.h>
27
28 // include files for KDE
29 #include <kwin.h>
30 #include <kfilterdev.h>
31 #include <klistview.h>
32 #include <kiconloader.h>
33 #include <klocale.h>
34 #include <kpopupmenu.h>
35 #include <krun.h>
36 #include <kdebug.h>
37
38 #include "libkita/threadinfo.h"
39 #include "libkita/thread.h"
40 #include "libkita/kita_misc.h"
41 #include "libkita/kitaconfig.h"
42 #include "libkita/favoritethreads.h"
43 #include "part/kita2ch.h"
44
45 KitaSubjectView::KitaSubjectView( QWidget* parent, const char* name )
46         : Kita::ThreadListView( parent, name )
47         , m_unreadNum( 0 ), m_readNum( 0 ), m_newNum( 0 )
48 {
49     connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
50              SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
51     connect( subjectList, SIGNAL( returnPressed( QListViewItem* ) ),
52              SLOT( loadThread( QListViewItem* ) ) );
53     connect( ReloadButton, SIGNAL( clicked() ),
54              SLOT( slotReloadButton() ) );
55 }
56
57 KitaSubjectView::~KitaSubjectView()
58 {}
59
60 void KitaSubjectView::reloadSubject()
61 {}
62
63 const QString KitaSubjectView::boardURL()
64 {
65     return m_boardURL;
66 }
67
68 const QString KitaSubjectView::boardName()
69 {
70     return Kita::Board::getName( m_boardURL );
71 }
72
73 void KitaSubjectView::loadThread( QListViewItem* item )
74 {
75     if ( ! item ) return ;
76
77     KURL datURL = item->text( Col_DatURL );
78
79     Kita::Thread* thread = Kita::Thread::getByURL( datURL.url() );
80
81     if ( KitaConfig::alwaysUseTab() ) {
82         emit showThreadRequested( thread->datURL(), true );
83     } else {
84         emit showThreadRequested( thread->datURL(), false );
85     }
86 }
87 /*
88 Í¥Àè½ç°Ì
89 ts_readed   : ´ûÆɤÇ1000Ķ¤¨¤Æ¤¤¤ë¤â¤Î
90 ts_normal   : Ì¤¼èÆÀ¤Ç1Æü°Ê¾å·Ð¤Ã¤Æ¤¤¤ë¤â¤Î
91 ts_new      : Ì¤¼èÆÀ¤Ç1Æü°ÊÆâ¤Î¤â¤Î
92 ts_read   : ´ûÆɤÇ1000°Ê²¼¤Î¤â¤Î
93 ts_hasunread: Ì¤Æɤ¢¤ê
94 */
95
96 enum {
97     ts_readed,
98     ts_normal,
99     ts_new,
100     ts_read,
101     ts_hasunread
102 };
103
104 void KitaSubjectView::loadBoard( const QString& boardURL )
105 {
106     setActiveWindow();
107     topLevelWidget() ->raise();
108     QDateTime currentdat = QDateTime::currentDateTime();
109
110     // reset member variables.
111     // FIXME: FavoriteListView::update()¤Ë¥³¥Ô¡¼
112     {
113         m_hitList.clear();
114         m_nextHitIndex = 0;
115         m_prevquery = "";
116     }
117
118     m_boardURL = boardURL;
119     m_unreadNum = 0;
120     m_readNum = 0;
121     m_newNum = 0;
122
123     KitaThreadInfo* cache = KitaThreadInfo::getInstance();
124     QPtrList<Kita::Thread> threadList = Kita::Board::getThreadList( boardURL );
125
126     // clear list
127     subjectList->clear();
128
129     for ( unsigned i = 0; i < threadList.count(); i++ ) {
130         Kita::Thread* thread = threadList.at( i );
131
132         QDateTime since;
133         since.setTime_t( KURL( thread->datURL() ).fileName().section( '.', 0, 0 ).toInt() );
134
135         int readNum = cache->readNum( thread->datURL() );
136         int resNum = thread->resNum();
137
138         Kita::ThreadListViewItem* item = new Kita::ThreadListViewItem( subjectList );
139         item->setText( Col_ID, QString( "%1" ).arg( i + 1, 4 ) );
140         item->setText( Col_Subject, Kita::unescape( thread->name() ) );
141         item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
142         item->setText( Col_Read, ( readNum > 0 ) ? QString( "%1" ).arg( readNum, 4 ) : QString( "" ) );
143         item->setText( Col_Unread, ( readNum > 0 && resNum != readNum ) ? QString( "%1" ).arg( resNum - readNum, 4 ) : QString( "" ) );
144         item->setText( Col_Board, thread->boardName() );
145         item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
146         item->setText( Col_DatURL, thread->datURL() );
147
148         // FIXME: updateThread¤Ë¥³¥Ô¡¼¤·¤Æ¤¤¤ë
149         // TODO: 'N'¤Î¥¢¥¤¥³¥ó¤òºîÀ®
150         if ( readNum > 0 && resNum != readNum ) {
151             // Ì¤Æɤ¢¤ê
152             item->setPixmap( Col_Mark, SmallIcon( "unread" ) );
153             m_unreadNum++;
154             item->setText( Col_MarkOrder, QString::number( ts_hasunread ) );
155         } else if ( readNum > 0 ) {
156             // ´ûÆÉ
157             item->setPixmap( Col_Mark, SmallIcon( "read" ) );
158             m_readNum++;
159             //if( readNum > 1000) item->setText( Col_MarkOrder, "0" );
160             //else item->setText( Col_MarkOrder, "3" );
161             item->setText( Col_MarkOrder, QString::number( ( readNum > 1000 ) ? ts_readed : ts_read ) );
162         } else if ( since.secsTo( currentdat ) < 3600 * KitaConfig::MarkTime() ) {
163             item->setPixmap( Col_Mark, SmallIcon( "newthread" ) );
164             m_newNum++;
165             item->setText( Col_MarkOrder, QString::number( ts_new ) );
166         } else {
167             item->setText( Col_MarkOrder, QString::number( ts_normal ) );
168         }
169     }
170
171     if ( HideButton->isOn() ) {
172         HideButton->toggle();
173     }
174     emit loadBoardCompleted( boardURL );
175
176     switch ( KitaConfig::sortOrder() ) {
177     case KitaConfig::Order_Mark:
178         subjectList->setSorting( Col_Mark );
179         break;
180     case KitaConfig::Order_ID:
181         subjectList->setSorting( Col_ID );
182         break;
183     default:
184         // do nothing
185         break;
186     }
187
188     subjectList->setFocus();
189     UpdateKindLabel();
190 }
191
192 void KitaSubjectView::UpdateKindLabel()
193 {
194     QString fmtstr;
195     fmtstr += QString( "<font color=#C50000>%1</font>" ).arg( m_unreadNum );
196     fmtstr += QString( "/<font color=#00C200>%1</font>" ).arg( m_readNum );
197     fmtstr += QString( "/<font color=#5AAAFF>%1</font>" ).arg( m_newNum );
198     KindLabel->setText( fmtstr );
199 }
200
201 void KitaSubjectView::setFont( const QFont& font )
202 {
203     subjectList->setFont( font );
204 }
205
206 void KitaSubjectView::updateThread( const Kita::Thread* updated_thread )
207 {
208     for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
209         if ( item->text( Col_DatURL ) == updated_thread->datURL() ) {
210             int resNum = updated_thread->resNum();
211             int readNum = KitaThreadInfo::readNum( updated_thread->datURL() );
212             item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
213             item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
214             item->setText( Col_Unread, readNum > 0 && resNum != readNum ? QString( "%1" ).arg( resNum - readNum, 4 ) : QString( "" ) );
215
216             switch ( item->text( Col_MarkOrder ).toInt() ) {
217                 case ts_readed    :
218                 case ts_read      : m_readNum--; break;
219                 case ts_new       : m_newNum--; break;
220                 case ts_hasunread : m_unreadNum--; break;
221             }
222
223             if ( readNum > 0 && resNum != readNum ) {
224                 // Ì¤Æɤ¢¤ê
225                 item->setPixmap( Col_Mark, SmallIcon( "unread" ) );
226                 m_unreadNum++;
227                 item->setText( Col_MarkOrder, QString::number( ts_hasunread ) );
228             } else if ( readNum > 0 ) {
229                 // ´ûÆÉ
230                 item->setPixmap( Col_Mark, SmallIcon( "read" ) );
231                 m_readNum++;
232                 item->setText( Col_MarkOrder, QString::number( ts_read ) );
233             } else {
234                 item->setText( Col_MarkOrder, QString::number( ts_normal ) );
235             }
236             UpdateKindLabel();
237         }
238     }
239 }
240
241 void KitaSubjectView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
242 {
243     if ( item == 0 ) {
244         return ;
245     }
246
247     Kita::Thread* thread = Kita::Thread::getByURL( item->text( Col_DatURL ) );
248     bool isFavorites = FavoriteThreads::getInstance()->contains( thread->datURL() );
249     
250     KPopupMenu popup( 0 );
251     popup.insertItem( i18n( "Open with Web Browser" ), 0 );
252     popup.insertItem( i18n( "Open with new tab" ), 1 );
253     popup.insertItem( i18n( "Copy title and URL" ), 2 );
254     if ( isFavorites ) {
255         popup.insertItem( i18n( "Remove from Favorites" ), 3 );
256     } else {
257         popup.insertItem( i18n( "Add to Favorites" ), 3 );
258     }
259     
260     QClipboard* clipboard = QApplication::clipboard();
261
262     switch ( popup.exec( point ) ) {
263     case 0:
264         KRun::runURL( thread->url(), "text/html" );
265         break;
266     case 1:
267         emit showThreadRequested( thread->datURL(), true );
268         break;
269     case 2:
270         clipboard->setText( thread->name() + "\n" + thread->url() );
271         break;
272     case 3:
273         if ( isFavorites ) {
274             emit bookmarked( thread->datURL(), false );
275         } else {
276             emit bookmarked( thread->datURL(), true );
277         }
278     default:
279         break;
280     }
281 }
282
283 void KitaSubjectView::slotReloadButton()
284 {
285     if ( ! m_boardURL.isEmpty() ) {
286         loadBoard( m_boardURL );
287     }
288 }