OSDN Git Service

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