OSDN Git Service

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