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