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"
18 #include "libkita/datmanager.h"
19 #include "libkita/boardmanager.h"
22 #include <qapplication.h>
23 #include <qclipboard.h>
24 #include <qdatetime.h>
25 #include <qtoolbutton.h>
29 #include <kpopupmenu.h>
32 #include <klistview.h>
34 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
35 : Kita::ThreadListView( parent, name )
37 subjectList->addColumn( i18n( "Board" ) );
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() ),
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 QString datURL = ( *it );
78 since.setTime_t( Kita::datToSince( datURL ) );
80 int readNum = cache->readNum( datURL );
81 int resNum = KitaThreadInfo::resNum( datURL );
83 KListViewItem* item = new KListViewItem( subjectList );
84 item->setText( Col_Board, Kita::DatManager::boardName( datURL ) );
85 item->setText( Col_Subject, Kita::unescape( Kita::Thread::getName( datURL ) ) );
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, 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 QString datURL = ( *it );
109 if ( datURL == item->text( Col_DatURL ) ) {
110 emit openURLRequestExt( datURL, KParts::URLArgs(), "kita_open_2chthread", 0 );
115 void FavoriteListView::slotUpdateSubject( const KURL& url )
117 KURL datURL = Kita::ParseMisc::parseURLonly( url );
118 for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
119 if ( item->text( Col_DatURL ) == datURL.prettyURL() ) {
120 int resNum = KitaThreadInfo::resNum( datURL.prettyURL() );
121 int readNum = KitaThreadInfo::readNum( datURL.prettyURL() );
122 item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
123 item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
128 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
129 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
132 Menu_OpenWithBrowser,
135 Menu_CopyTitleAndURL,
136 Menu_RemoveFromFavorites
143 KPopupMenu popup( 0 );
144 popup.insertItem( i18n( "Open with Web Browser" ), Menu_OpenWithBrowser );
145 popup.insertItem( i18n( "Open with new tab" ), Menu_OpenWithNewTab );
146 popup.insertItem( i18n( "Copy URL" ), Menu_CopyURL );
147 popup.insertItem( i18n( "Copy title and URL" ), Menu_CopyTitleAndURL );
148 popup.insertItem( i18n( "Remove from Favorites" ), Menu_RemoveFromFavorites );
150 QString datURL = item->text( Col_DatURL );
151 QString threadURL = Kita::DatManager::threadURL( datURL );
153 QClipboard* clipboard = QApplication::clipboard();
155 switch ( popup.exec( point ) ) {
156 case Menu_OpenWithBrowser:
157 KRun::runURL( Kita::DatManager::threadURL( datURL ), "text/html" );
159 case Menu_OpenWithNewTab:
160 emit openURLRequestExt( datURL, KParts::URLArgs(), "kita_open_2chthread", 1 );
163 clipboard->setText( threadURL );
165 case Menu_CopyTitleAndURL:
166 clipboard->setText( Kita::Thread::getName( datURL ) + "\n" + threadURL );
168 case Menu_RemoveFromFavorites:
169 emit bookmarked( datURL, false );
176 void FavoriteListView::slotReloadButton()
178 QValueList<QString> boardList;
180 const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
181 QValueList<QString>::const_iterator it;
182 for ( it = threadList.begin(); it != threadList.end(); ++it ) {
183 QString datURL = ( *it );
184 QString boardURL = Kita::datToBoard( datURL );
185 if ( boardList.contains( boardURL ) == 0 ) {
186 boardList.append( boardURL );
190 QValueList<QString>::const_iterator it2;
191 for ( it2 = boardList.begin(); it2 != boardList.end(); ++it2 ) {
194 QPtrList<Kita::Thread> threadList;
195 QPtrList<Kita::Thread> tmpList;
196 Kita::BoardManager::getThreadList( ( *it2 ), FALSE, online, threadList, tmpList );