OSDN Git Service

change inheritance
[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 <qtoolbutton.h>
19 #include <qmap.h>
20 #include <qapplication.h>
21 #include <qclipboard.h>
22
23 #include <klocale.h>
24 #include <kiconloader.h>
25 #include <kpopupmenu.h>
26 #include <krun.h>
27 #include <kdebug.h>
28 #include <klistview.h>
29
30 enum FavoriteListViewRows {
31   Row_Board,
32   Row_Icon,
33   Row_Subject,
34   Row_ResNum,
35   Row_Read,
36   Row_Unread,
37   Row_DatName,
38   Row_DatURL
39 };
40
41 FavoriteListView::FavoriteListView(QWidget* parent, const char *name)
42  : KitaSubjectViewBase(parent, name)
43 {
44   {
45     SearchButton->setPixmap( SmallIcon("find") );
46     HideButton->setPixmap( SmallIcon("filter") );
47   }
48
49   subjectList->addColumn( i18n( "Board" ) );
50   subjectList->addColumn( "" );
51   subjectList->addColumn( i18n( "Title" ) );
52   subjectList->addColumn( i18n( "ResNum" ) );
53   subjectList->addColumn( i18n( "ReadNum" ) );
54   subjectList->addColumn( i18n( "Unread" ) );
55   subjectList->addColumn( i18n( "Dat" ) );
56
57   subjectList->setColumnWidth(Row_Subject, 400);
58   subjectList->setColumnWidthMode(Row_Subject, QListView::Manual);
59
60   connect( subjectList, SIGNAL( clicked( QListViewItem* ) ),
61                  SLOT( loadThread( QListViewItem* ) ) );
62   connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
63                  SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
64 }
65
66 FavoriteListView::~FavoriteListView()
67 {
68 }
69
70 void FavoriteListView::update()
71 {
72   const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance()->threads();
73 //  FavoriteThreads::const_iterator it;
74
75   subjectList->clear();
76
77   KitaThreadInfo* cache = KitaThreadInfo::getInstance();
78
79   QDictIterator<Kita::Thread> it( threads );
80   for(; it.current(); ++it) {
81     const Kita::Thread* thread = it.current();
82
83     int readNum = cache->readNum( thread->datURL() );
84     int resNum = cache->resNum( thread->datURL() );  // TODO unused variables.
85
86     KListViewItem* item = new KListViewItem( subjectList );
87     item->setText( Row_Board, thread->boardName() );
88     item->setText( Row_Subject, thread->name() );
89     item->setText( Row_Read, QString("%1").arg( readNum, 4 ) );
90     item->setText( Row_DatName, KURL( thread->datURL() ).fileName() );
91     item->setText( Row_DatURL, thread->datURL() );
92   }
93 }
94
95 void FavoriteListView::loadThread( QListViewItem* item )
96 {
97   if( ! item ) return;
98
99   const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance()->threads();
100 //  FavoriteThreads::const_iterator it;
101
102   QDictIterator<Kita::Thread> it( threads );
103   for(; it.current(); ++it) {
104     const Kita::Thread* thread = it.current();
105
106     if( thread->datURL() == item->text( Row_DatURL ) ) {
107       emit signalShowThread( *thread );
108     }
109   }
110 }
111
112 void FavoriteListView::updateThread( const Kita::Thread& updated_thread )
113 {
114   for( QListViewItem* item = subjectList->firstChild(); item; item = item->nextSibling() ) {
115     if( item->text( Row_DatURL ) == updated_thread.datURL() ) {
116       item->setText( Row_ResNum, QString("%1").arg( updated_thread.resNum(), 4 ) );
117     }
118   }
119 }
120
121 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
122 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
123 {
124   if( item == 0 ) {
125     return;
126   }
127
128   KPopupMenu popup( 0 );
129   popup.insertItem( i18n("Open with Web Browser"), 0 );
130   popup.insertItem( i18n("Open with new tab"), 1 );
131   popup.insertItem( i18n("Copy title and URL"), 2 );
132
133   QString datName = item->text(Row_DatName);
134   KURL datURL = KURL( item->text(Row_DatURL) );
135
136   Kita::Thread thread( datURL.url() );
137   thread.setName( item->text( Row_Subject ) );
138   thread.setResNum( item->text( Row_Read ).toInt() );
139
140   QClipboard* clipboard = QApplication::clipboard();
141
142   switch( popup.exec( point ) ) {
143   case 0:
144     KRun::runURL( thread.url(), "text/html" );
145     break;
146   case 1:
147     emit signalShowThreadWithNewTab( thread );
148     break;
149   case 2:
150     clipboard->setText( thread.name() + "\n" + thread.url() );
151     break;
152   default:
153     break;
154   }
155 }