OSDN Git Service

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