OSDN Git Service

readNum fix.
[kita/kita.git] / kita / src / favoritelistview.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 "favoritelistview.h"
12
13 #include "libkita/favoritethreads.h"
14 #include "libkita/kita_misc.h"
15 #include "libkita/threadinfo.h"
16 #include "libkita/signalcollection.h"
17 #include "libkita/parsemisc.h"
18 #include "libkita/datmanager.h"
19 #include "libkita/boardmanager.h"
20
21 #include <qmap.h>
22 #include <qapplication.h>
23 #include <qclipboard.h>
24 #include <qdatetime.h>
25 #include <qtoolbutton.h>
26 #include <qlabel.h>
27
28 #include <klocale.h>
29 #include <kpopupmenu.h>
30 #include <krun.h>
31 #include <kdebug.h>
32 #include <klistview.h>
33
34 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
35         : Kita::ThreadListView( parent, name )
36 {
37     subjectList->addColumn( i18n( "Board" ) );
38
39     KindLabel->hide();
40
41     Kita::SignalCollection* signalCollection = Kita::SignalCollection::getInstance();
42     connect( subjectList, SIGNAL( returnPressed( QListViewItem* ) ),
43              SLOT( loadThread( QListViewItem* ) ) );
44     connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
45              SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
46     connect( ReloadButton, SIGNAL( clicked() ),
47              SLOT( slotReloadButton() ) );
48     connect( signalCollection, SIGNAL( favoritesUpdated() ),
49              SLOT( update() ) );
50
51     connect( signalCollection, SIGNAL( updateSubjectTab( const KURL& ) ),
52              SLOT( slotUpdateSubject( const KURL& ) ) );
53 }
54
55 FavoriteListView::~FavoriteListView()
56 {}
57
58 void FavoriteListView::update()
59 {
60     // FIXME: KitaSubjectView::loadBoard()¤Ë¥³¥Ô¡¼
61     {
62         m_hitList.clear();
63         m_nextHitIndex = 0;
64         m_prevquery = "";
65     }
66
67     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
68
69     subjectList->clear();
70
71     KitaThreadInfo* cache = KitaThreadInfo::getInstance();
72
73     QValueList<QString>::const_iterator it;
74     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
75         QString datURL = ( *it );
76
77         QDateTime since;
78         since.setTime_t( Kita::datToSince( datURL ) );
79
80 // FIXME: use 'ReadNum'
81 //        int readNum = cache->readNum( datURL );
82         int readNum = Kita::DatManager::getKokoyonNum( datURL );
83 //        int resNum = KitaThreadInfo::resNum( datURL );
84         int resNum = Kita::DatManager::getResNum( datURL );
85
86         KListViewItem* item = new KListViewItem( subjectList );
87         item->setText( Col_Board, Kita::DatManager::boardName( datURL ) );
88         item->setText( Col_Subject, Kita::unescape( Kita::Thread::getName( datURL ) ) );
89         item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
90         if ( resNum > 0 ) {
91             item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
92             if ( resNum != readNum ) {
93                 item->setText( Col_Unread, QString( "%1" ).arg( resNum - readNum, 4 ) );
94             }
95         }
96         item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
97         item->setText( Col_DatURL, datURL );
98     }
99     subjectList->setSorting( Col_Board );
100 }
101
102 void FavoriteListView::loadThread( QListViewItem* item )
103 {
104     if ( ! item ) return ;
105
106     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
107
108     QValueList<QString>::const_iterator it;
109     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
110         QString datURL = ( *it );
111
112         if ( datURL == item->text( Col_DatURL ) ) {
113             emit openURLRequestExt( datURL, KParts::URLArgs(), "kita_open_2chthread", 0 );
114         }
115     }
116 }
117
118 void FavoriteListView::slotUpdateSubject( const KURL& url )
119 {
120     KURL datURL = Kita::ParseMisc::parseURLonly( url );
121     for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
122         if ( item->text( Col_DatURL ) == datURL.prettyURL() ) {
123             int resNum = KitaThreadInfo::resNum( datURL.prettyURL() );
124             int readNum = KitaThreadInfo::readNum( datURL.prettyURL() );
125             item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
126             item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
127         }
128     }
129 }
130
131 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
132 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
133 {
134     enum {
135         Menu_OpenWithBrowser,
136         Menu_OpenWithNewTab,
137         Menu_CopyURL,
138         Menu_CopyTitleAndURL,
139         Menu_RemoveFromFavorites
140     };
141
142     if ( item == 0 ) {
143         return ;
144     }
145
146     KPopupMenu popup( 0 );
147     popup.insertItem( i18n( "Open with Web Browser" ), Menu_OpenWithBrowser );
148     popup.insertItem( i18n( "Open with new tab" ), Menu_OpenWithNewTab );
149     popup.insertItem( i18n( "Copy URL" ), Menu_CopyURL );
150     popup.insertItem( i18n( "Copy title and URL" ), Menu_CopyTitleAndURL );
151     popup.insertItem( i18n( "Remove from Favorites" ), Menu_RemoveFromFavorites );
152
153     QString datURL = item->text( Col_DatURL );
154     QString threadURL = Kita::DatManager::threadURL( datURL );
155
156     QClipboard* clipboard = QApplication::clipboard();
157
158     switch ( popup.exec( point ) ) {
159     case Menu_OpenWithBrowser:
160         KRun::runURL( Kita::DatManager::threadURL( datURL ), "text/html" );
161         break;
162     case Menu_OpenWithNewTab:
163         emit openURLRequestExt( datURL, KParts::URLArgs(), "kita_open_2chthread", 1 );
164         break;
165     case Menu_CopyURL:
166         clipboard->setText( threadURL );
167         break;
168     case Menu_CopyTitleAndURL:
169         clipboard->setText( Kita::Thread::getName( datURL ) + "\n" + threadURL );
170         break;
171     case Menu_RemoveFromFavorites:
172         emit bookmarked( datURL, false );
173         break;
174     default:
175         break;
176     }
177 }
178
179 void FavoriteListView::slotReloadButton()
180 {
181     QValueList<QString> boardList;
182
183     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
184     QValueList<QString>::const_iterator it;
185     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
186         QString datURL = ( *it );
187         QString boardURL = Kita::datToBoard( datURL );
188         if ( boardList.contains( boardURL ) == 0 ) {
189             boardList.append( boardURL );
190         }
191     }
192
193     QValueList<QString>::const_iterator it2;
194     for ( it2 = boardList.begin(); it2 != boardList.end(); ++it2 ) {
195
196         bool online = TRUE;
197         QPtrList<Kita::Thread> threadList;
198         QPtrList<Kita::Thread> tmpList;
199         Kita::BoardManager::getThreadList( ( *it2 ), FALSE, online, threadList, tmpList );
200     }
201 }
202