OSDN Git Service

refactoring.
[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
19 #include <qmap.h>
20 #include <qapplication.h>
21 #include <qclipboard.h>
22 #include <qdatetime.h>
23 #include <qtoolbutton.h>
24 #include <qlabel.h>
25
26 #include <klocale.h>
27 #include <kpopupmenu.h>
28 #include <krun.h>
29 #include <kdebug.h>
30 #include <klistview.h>
31
32 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
33         : Kita::ThreadListView( parent, name )
34 {
35     subjectList->addColumn( i18n( "Board" ) );
36
37     KindLabel->hide();
38
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() ),
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         const Kita::Thread* thread = Kita::Thread::getByURL( *it );
76
77         QDateTime since;
78         since.setTime_t( Kita::datToSince( thread->datURL() ) );
79
80         int readNum = cache->readNum( thread->datURL() );
81         int resNum = KitaThreadInfo::resNum( thread->datURL() );
82
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 ) );
87         if ( resNum > 0 ) {
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 ) );
91             }
92         }
93         item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
94         item->setText( Col_DatURL, thread->datURL() );
95     }
96     subjectList->setSorting( Col_Board );
97 }
98
99 void FavoriteListView::loadThread( QListViewItem* item )
100 {
101     if ( ! item ) return ;
102
103     const QValueList<QString> threadList = FavoriteThreads::getInstance() -> threadList();
104
105     QValueList<QString>::const_iterator it;
106     for ( it = threadList.begin(); it != threadList.end(); ++it ) {
107         const Kita::Thread* thread = Kita::Thread::getByURL( *it );
108
109         if ( thread->datURL() == item->text( Col_DatURL ) ) {
110             emit showThreadRequested( thread->datURL(), false );
111         }
112     }
113 }
114
115 void FavoriteListView::slotUpdateSubject( const KURL& url )
116 {
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 ) );
125         }
126     }
127 }
128
129 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
130 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
131 {
132     if ( item == 0 ) {
133         return ;
134     }
135
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 );
141
142     Kita::Thread* thread = Kita::Thread::getByURL( item->text( Col_DatURL ) );
143
144     QClipboard* clipboard = QApplication::clipboard();
145
146     switch ( popup.exec( point ) ) {
147     case 0:
148         KRun::runURL( thread->url(), "text/html" );
149         break;
150     case 1:
151         emit showThreadRequested( thread->datURL(), true );
152         break;
153     case 2:
154         clipboard->setText( thread->name() + "\n" + thread->url() );
155         break;
156     case 3:
157         emit bookmarked( thread->datURL(), false );
158         break;
159     default:
160         break;
161     }
162 }
163
164 void FavoriteListView::slotReloadButton()
165 {
166     QValueList<QString> boardList;
167     
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 );
175         }
176     }
177
178     QValueList<QString>::const_iterator it2;
179     for ( it2 = boardList.begin(); it2 != boardList.end(); ++it2 ) {
180         // ³ÆÈĤÎsubject.txt¤ò¼èÆÀ¡£
181         Kita::Board::getThreadList( (*it2 ) );
182     }
183 }
184