1 /***************************************************************************
2 * Copyright (C) 2003 by Hideki Ikemoto *
3 * ikemo@users.sourceforge.jp *
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 ***************************************************************************/
11 #include "favoritelistview.h"
13 #include "libkita/favoritethreads.h"
14 #include "libkita/kita_misc.h"
15 #include "libkita/threadinfo.h"
16 #include "libkita/signalcollection.h"
19 #include <qapplication.h>
20 #include <qclipboard.h>
21 #include <qdatetime.h>
22 #include <qtoolbutton.h>
26 #include <kpopupmenu.h>
29 #include <klistview.h>
31 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
32 : Kita::ThreadListView( parent, name )
34 subjectList->addColumn( i18n( "Board" ) );
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() ),
51 FavoriteListView::~FavoriteListView()
54 void FavoriteListView::update()
56 // FIXME: KitaSubjectView::loadBoard()¤Ë¥³¥Ô¡¼
63 const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
67 KitaThreadInfo* cache = KitaThreadInfo::getInstance();
69 QValueList<QString>::const_iterator it;
70 for ( it = threadList.begin(); it != threadList.end(); ++it ) {
71 const Kita::Thread* thread = Kita::Thread::getByURL( *it );
74 since.setTime_t( KURL( thread->datURL() ).fileName().section( '.', 0, 0 ).toInt() );
76 int readNum = cache->readNum( thread->datURL() );
77 int resNum = KitaThreadInfo::resNum( thread->datURL() );
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 ) );
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 ) );
89 item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
90 item->setText( Col_DatURL, thread->datURL() );
94 void FavoriteListView::loadThread( QListViewItem* item )
96 if ( ! item ) return ;
98 const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
100 QValueList<QString>::const_iterator it;
101 for ( it = threadList.begin(); it != threadList.end(); ++it ) {
102 const Kita::Thread* thread = Kita::Thread::getByURL( *it );
104 if ( thread->datURL() == item->text( Col_DatURL ) ) {
105 emit showThreadRequested( thread->datURL(), false );
110 void FavoriteListView::updateThread( const Kita::Thread* updated_thread )
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 ) );
122 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
123 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
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 );
135 Kita::Thread* thread = Kita::Thread::getByURL( item->text( Col_DatURL ) );
137 QClipboard* clipboard = QApplication::clipboard();
139 switch ( popup.exec( point ) ) {
141 KRun::runURL( thread->url(), "text/html" );
144 emit showThreadRequested( thread->datURL(), true );
147 clipboard->setText( thread->name() + "\n" + thread->url() );
150 emit bookmarked( thread->datURL(), false );
157 void FavoriteListView::slotReloadButton()
159 QValueList<QString> boardList;
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 );
171 QValueList<QString>::const_iterator it2;
172 for ( it2 = boardList.begin(); it2 != boardList.end(); ++it2 ) {
173 emit openBoardRequested( (*it2), true );