OSDN Git Service

now 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/board.h"
15 #include "libkita/kita_misc.h"
16 #include "kitacacheinfo.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
27 enum FavoriteListViewRows {
28   Row_Board,
29   Row_Icon,
30   Row_Subject,
31   Row_ResNum,
32   Row_Read,
33   Row_Unread,
34   Row_DatName,
35   Row_DatURL
36 };
37
38 FavoriteListView::FavoriteListView(QWidget* parent, const char *name)
39  : KListView(parent, name)
40 {
41   addColumn(i18n("Board"));
42   addColumn("");
43   addColumn(i18n("Title"));
44   addColumn(i18n("ResNum"));
45   addColumn(i18n("ReadNum"));
46   addColumn(i18n("Unread"));
47   addColumn(i18n("Dat"));
48
49   setColumnWidth(Row_Subject, 400);
50   setColumnWidthMode(Row_Subject, QListView::Manual);
51
52   connect( this, SIGNAL( clicked(QListViewItem*) ),
53                  SLOT( loadThread(QListViewItem*) ) );
54   connect( this, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
55                  SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
56 }
57
58 FavoriteListView::~FavoriteListView()
59 {
60 }
61
62 void FavoriteListView::update()
63 {
64   const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance()->threads();
65 //  FavoriteThreads::const_iterator it;
66
67   clear();
68
69   KitaCacheInfo* cache = KitaCacheInfo::getInstance();
70
71   QDictIterator<Kita::Thread> it( threads );
72   for(; it.current(); ++it) {
73     const Kita::Thread* thread = it.current();
74
75     int readNum = cache->readNum( thread->datURL() );
76     int resNum = cache->resNum( thread->datURL() );  // TODO unused variables.
77
78     new KListViewItem( this,
79                        thread->boardName(),
80                        "",
81                        thread->name(),
82                        "",
83                        QString("%1").arg( readNum, 4 ),
84                        "",
85                        thread->datURL().fileName(),
86                        thread->datURL().url() );
87   }
88 }
89
90 void FavoriteListView::loadThread( QListViewItem* item )
91 {
92   if( ! item ) return;
93
94   const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance()->threads();
95 //  FavoriteThreads::const_iterator it;
96
97   QDictIterator<Kita::Thread> it( threads );
98   for(; it.current(); ++it) {
99     const Kita::Thread* thread = it.current();
100
101     if( thread->datURL().url() == item->text( Row_DatURL ) ) {
102       emit signalShowThread( *thread );
103     }
104   }
105 }
106
107 void FavoriteListView::updateThread( const Kita::Thread& updated_thread )
108 {
109   for( QListViewItem* item = firstChild(); item; item = item->nextSibling() ) {
110     if( item->text( Row_DatURL ) == updated_thread.datURL().url() ) {
111       item->setText( Row_ResNum, QString("%1").arg( updated_thread.resNum(), 4 ) );
112     }
113   }
114 }
115
116 // TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
117 void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
118 {
119   if( item == 0 ) {
120     return;
121   }
122
123   KPopupMenu popup( 0 );
124   popup.insertItem( i18n("Open with Web Browser"), 0 );
125   popup.insertItem( i18n("Open with new tab"), 1 );
126   popup.insertItem( i18n("Copy title and URL"), 2 );
127
128   QString datName = item->text(Row_DatName);
129   KURL datURL = KURL( item->text(Row_DatURL) );
130
131   kdDebug() << "datURL = " << datURL.url() << endl;
132   Kita::Board board = Kita::Board( KURL( datURL, ".." ) );
133   kdDebug() << "board.url = " << board.url().url() << endl;
134   Kita::Board::setName( Kita::datToBoard( datURL.url() ), board.name() );
135   Kita::Thread thread( datURL );
136   kdDebug() << "thread.url = %s" << thread.url() << endl;
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 }