OSDN Git Service

use pointer
[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/board.h"
15 #include "libkita/kita_misc.h"
16 #include "libkita/threadinfo.h"
17
18 #include <qmap.h>
19 #include <qapplication.h>
20 #include <qclipboard.h>
21
22 #include <klocale.h>
23 #include <kpopupmenu.h>
24 #include <krun.h>
25 #include <kdebug.h>
26 #include <klistview.h>
27
28 FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
29         : Kita::ThreadListView( parent, name )
30 {
31     connect( subjectList, SIGNAL( clicked( QListViewItem* ) ),
32              SLOT( loadThread( QListViewItem* ) ) );
33     connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
34              SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
35 }
36
37 FavoriteListView::~FavoriteListView()
38 {}
39
40 void FavoriteListView::update()
41 {
42     // FIXME: KitaSubjectView::loadBoard()¤Ë¥³¥Ô¡¼
43     {
44         m_hitList.clear();
45         m_nextHitIndex = 0;
46         m_prevquery = "";
47     }
48
49     const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance() ->threads();
50     //  FavoriteThreads::const_iterator it;
51
52     subjectList->clear();
53
54     KitaThreadInfo* cache = KitaThreadInfo::getInstance();
55
56     QDictIterator<Kita::Thread> it( threads );
57     for ( ; it.current(); ++it ) {
58         const Kita::Thread* thread = it.current();
59
60         int readNum = cache->readNum( thread->datURL() );
61         int resNum = cache->resNum( thread->datURL() );  // TODO unused variables.
62
63         KListViewItem* item = new KListViewItem( subjectList );
64         item->setText( Row_Board, thread->boardName() );
65         item->setText( Row_Subject, thread->name() );
66         item->setText( Row_Read, QString( "%1" ).arg( readNum, 4 ) );
67         item->setText( Row_DatName, KURL( thread->datURL() ).fileName() );
68         item->setText( Row_DatURL, thread->datURL() );
69     }
70 }
71
72 void FavoriteListView::loadThread( QListViewItem* item )
73 {
74     if ( ! item ) return ;
75
76     const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance() ->threads();
77     //  FavoriteThreads::const_iterator it;
78
79     QDictIterator<Kita::Thread> it( threads );
80     for ( ; it.current(); ++it ) {
81         const Kita::Thread* thread = it.current();
82
83         if ( thread->datURL() == item->text( Row_DatURL ) ) {
84             emit signalShowThread( thread );
85         }
86     }
87 }
88
89 void FavoriteListView::updateThread( const Kita::Thread* updated_thread )
90 {
91     for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
92         if ( item->text( Row_DatURL ) == updated_thread->datURL() ) {
93             item->setText( Row_ResNum, QString( "%1" ).arg( updated_thread->resNum(), 4 ) );
94         }
95     }
96 }
97
98 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
99 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
100 {
101     if ( item == 0 ) {
102         return ;
103     }
104
105     KPopupMenu popup( 0 );
106     popup.insertItem( i18n( "Open with Web Browser" ), 0 );
107     popup.insertItem( i18n( "Open with new tab" ), 1 );
108     popup.insertItem( i18n( "Copy title and URL" ), 2 );
109
110     QString datName = item->text( Row_DatName );
111     KURL datURL = KURL( item->text( Row_DatURL ) );
112
113     Kita::Thread* thread = new Kita::Thread( datURL.url() );
114     thread->setName( item->text( Row_Subject ) );
115     thread->setResNum( item->text( Row_Read ).toInt() );
116
117     QClipboard* clipboard = QApplication::clipboard();
118
119     switch ( popup.exec( point ) ) {
120     case 0:
121         KRun::runURL( thread->url(), "text/html" );
122         break;
123     case 1:
124         emit signalShowThreadWithNewTab( thread );
125         break;
126     case 2:
127         clipboard->setText( thread->name() + "\n" + thread->url() );
128         break;
129     default:
130         break;
131     }
132 }