OSDN Git Service

update FavoriteListView when bookmarked.
[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
18 #include <qmap.h>
19 #include <qapplication.h>
20 #include <qclipboard.h>
21 #include <qdatetime.h>
22 #include <qtoolbutton.h>
23 #include <qlabel.h>
24
25 #include <klocale.h>
26 #include <kpopupmenu.h>
27 #include <krun.h>
28 #include <kdebug.h>
29 #include <klistview.h>
30
31 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
32         : Kita::ThreadListView( parent, name )
33 {
34     subjectList->addColumn( i18n( "Board" ) );
35
36     KindLabel->hide();
37
38     Kita::SignalCollection* signalCollection = Kita::SignalCollection::getInstance();
39     connect( subjectList, SIGNAL( returnPressed( QListViewItem* ) ),
40              SLOT( loadThread( QListViewItem* ) ) );
41     connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
42              SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
43     connect( ReloadButton, SIGNAL( clicked() ),
44              SLOT( slotReloadButton() ) );
45     connect( this, SIGNAL( openBoardRequested( const QString&, bool ) ),
46              signalCollection, SIGNAL( openBoardRequested( const QString&, bool ) ) );
47     connect( signalCollection, SIGNAL( favoritesUpdated() ),
48              SLOT( update() ) );
49 }
50
51 FavoriteListView::~FavoriteListView()
52 {}
53
54 void FavoriteListView::update()
55 {
56     // FIXME: KitaSubjectView::loadBoard()¤Ë¥³¥Ô¡¼
57     {
58         m_hitList.clear();
59         m_nextHitIndex = 0;
60         m_prevquery = "";
61     }
62
63     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
64
65     subjectList->clear();
66
67     KitaThreadInfo* cache = KitaThreadInfo::getInstance();
68
69     QValueList<QString>::const_iterator it;
70     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
71         const Kita::Thread* thread = Kita::Thread::getByURL( *it );
72
73         QDateTime since;
74         since.setTime_t( KURL( thread->datURL() ).fileName().section( '.', 0, 0 ).toInt() );
75
76         int readNum = cache->readNum( thread->datURL() );
77         int resNum = KitaThreadInfo::resNum( thread->datURL() );
78
79         KListViewItem* item = new KListViewItem( subjectList );
80         item->setText( Col_Board, thread->boardName() );
81         item->setText( Col_Subject, Kita::unescape( thread->name() ) );
82         item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
83         if ( resNum > 0 ) {
84             item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
85             if ( resNum != readNum ) {
86                 item->setText( Col_Unread, QString( "%1" ).arg( resNum - readNum, 4 ) );
87             }
88         }
89         item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
90         item->setText( Col_DatURL, thread->datURL() );
91     }
92 }
93
94 void FavoriteListView::loadThread( QListViewItem* item )
95 {
96     if ( ! item ) return ;
97
98     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
99
100     QValueList<QString>::const_iterator it;
101     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
102         const Kita::Thread* thread = Kita::Thread::getByURL( *it );
103
104         if ( thread->datURL() == item->text( Col_DatURL ) ) {
105             emit showThreadRequested( thread->datURL(), false );
106         }
107     }
108 }
109
110 void FavoriteListView::updateThread( const Kita::Thread* updated_thread )
111 {
112     for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
113         if ( item->text( Col_DatURL ) == updated_thread->datURL() ) {
114             int resNum = updated_thread->resNum();
115             int readNum = KitaThreadInfo::readNum( updated_thread->datURL() );
116             item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
117             item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
118         }
119     }
120 }
121
122 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
123 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
124 {
125     if ( item == 0 ) {
126         return ;
127     }
128
129     KPopupMenu popup( 0 );
130     popup.insertItem( i18n( "Open with Web Browser" ), 0 );
131     popup.insertItem( i18n( "Open with new tab" ), 1 );
132     popup.insertItem( i18n( "Copy title and URL" ), 2 );
133     popup.insertItem( i18n( "Remove from Favorites" ), 3 );
134
135     Kita::Thread* thread = Kita::Thread::getByURL( item->text( Col_DatURL ) );
136
137     QClipboard* clipboard = QApplication::clipboard();
138
139     switch ( popup.exec( point ) ) {
140     case 0:
141         KRun::runURL( thread->url(), "text/html" );
142         break;
143     case 1:
144         emit showThreadRequested( thread->datURL(), true );
145         break;
146     case 2:
147         clipboard->setText( thread->name() + "\n" + thread->url() );
148         break;
149     case 3:
150         emit bookmarked( thread->datURL(), false );
151         break;
152     default:
153         break;
154     }
155 }
156
157 void FavoriteListView::slotReloadButton()
158 {
159     QValueList<QString> boardList;
160     
161     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
162     QValueList<QString>::const_iterator it;
163     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
164         const Kita::Thread* thread = Kita::Thread::getByURL( *it );
165         QString boardURL = Kita::datToBoard( thread->datURL() );
166         if ( boardList.contains( boardURL ) == 0 ) {
167             boardList.append( boardURL );
168         }
169     }
170
171     QValueList<QString>::const_iterator it2;
172     for ( it2 = boardList.begin(); it2 != boardList.end(); ++it2 ) {
173         emit openBoardRequested( (*it2), true );
174     }
175 }
176