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"
17 #include "libkita/parsemisc.h"
20 #include <qapplication.h>
21 #include <qclipboard.h>
22 #include <qdatetime.h>
23 #include <qtoolbutton.h>
27 #include <kpopupmenu.h>
30 #include <klistview.h>
32 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
33 : Kita::ThreadListView( parent, name )
35 subjectList->addColumn( i18n( "Board" ) );
39 Kita::SignalCollection* signalCollection = Kita::SignalCollection::getInstance();
40 connect( subjectList, SIGNAL( returnPressed( QListViewItem* ) ),
41 SLOT( loadThread( QListViewItem* ) ) );
42 connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
43 SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
44 connect( ReloadButton, SIGNAL( clicked() ),
45 SLOT( slotReloadButton() ) );
46 connect( this, SIGNAL( openBoardRequested( const QString&, bool ) ),
47 signalCollection, SIGNAL( openBoardRequested( const QString&, bool ) ) );
48 connect( signalCollection, SIGNAL( favoritesUpdated() ),
51 connect( signalCollection, SIGNAL( updateSubjectTab( const KURL& ) ),
52 SLOT( slotUpdateSubject( const KURL& ) ) );
55 FavoriteListView::~FavoriteListView()
58 void FavoriteListView::update()
60 // FIXME: KitaSubjectView::loadBoard()¤Ë¥³¥Ô¡¼
67 const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
71 KitaThreadInfo* cache = KitaThreadInfo::getInstance();
73 QValueList<QString>::const_iterator it;
74 for ( it = threadList.begin(); it != threadList.end(); ++it ) {
75 const Kita::Thread* thread = Kita::Thread::getByURL( *it );
78 since.setTime_t( Kita::datToSince( thread->datURL() ) );
80 int readNum = cache->readNum( thread->datURL() );
81 int resNum = KitaThreadInfo::resNum( thread->datURL() );
83 KListViewItem* item = new KListViewItem( subjectList );
84 item->setText( Col_Board, thread->boardName() );
85 item->setText( Col_Subject, Kita::unescape( thread->name() ) );
86 item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
88 item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
89 if ( resNum != readNum ) {
90 item->setText( Col_Unread, QString( "%1" ).arg( resNum - readNum, 4 ) );
93 item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
94 item->setText( Col_DatURL, thread->datURL() );
96 subjectList->setSorting( Col_Board );
99 void FavoriteListView::loadThread( QListViewItem* item )
101 if ( ! item ) return ;
103 const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
105 QValueList<QString>::const_iterator it;
106 for ( it = threadList.begin(); it != threadList.end(); ++it ) {
107 const Kita::Thread* thread = Kita::Thread::getByURL( *it );
109 if ( thread->datURL() == item->text( Col_DatURL ) ) {
110 emit showThreadRequested( thread->datURL(), false );
115 void FavoriteListView::slotUpdateSubject( const KURL& url )
117 KURL datURL = Kita::ParseMisc::parseURLonly( url );
118 Kita::Thread* updated_thread = Kita::Thread::getByURLNew( datURL );
119 for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
120 if ( item->text( Col_DatURL ) == datURL.prettyURL() ) {
121 int resNum = updated_thread->resNum();
122 int readNum = KitaThreadInfo::readNum( updated_thread->datURL() );
123 item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
124 item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
129 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
130 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
136 KPopupMenu popup( 0 );
137 popup.insertItem( i18n( "Open with Web Browser" ), 0 );
138 popup.insertItem( i18n( "Open with new tab" ), 1 );
139 popup.insertItem( i18n( "Copy title and URL" ), 2 );
140 popup.insertItem( i18n( "Remove from Favorites" ), 3 );
142 Kita::Thread* thread = Kita::Thread::getByURL( item->text( Col_DatURL ) );
144 QClipboard* clipboard = QApplication::clipboard();
146 switch ( popup.exec( point ) ) {
148 KRun::runURL( thread->url(), "text/html" );
151 emit showThreadRequested( thread->datURL(), true );
154 clipboard->setText( thread->name() + "\n" + thread->url() );
157 emit bookmarked( thread->datURL(), false );
164 void FavoriteListView::slotReloadButton()
166 QValueList<QString> boardList;
168 const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
169 QValueList<QString>::const_iterator it;
170 for ( it = threadList.begin(); it != threadList.end(); ++it ) {
171 const Kita::Thread* thread = Kita::Thread::getByURL( *it );
172 QString boardURL = Kita::datToBoard( thread->datURL() );
173 if ( boardList.contains( boardURL ) == 0 ) {
174 boardList.append( boardURL );
178 QValueList<QString>::const_iterator it2;
179 for ( it2 = boardList.begin(); it2 != boardList.end(); ++it2 ) {
180 // ³ÆÈĤÎsubject.txt¤ò¼èÆÀ¡£
181 Kita::Board::getThreadList( (*it2 ) );