OSDN Git Service

use url() instead of prettyURL()
[kita/kita.git] / kita / src / kitaboardview.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 <kio/netaccess.h>
12
13 #include <qfile.h>
14 #include <qtextcodec.h>
15 #include <qstringlist.h>
16 #include <qregexp.h>
17 #include <klistview.h>
18 #include <kpopupmenu.h>
19 #include <klocale.h>
20 #include <krun.h>
21 #include <kdebug.h>
22 #include <qvaluelist.h>
23
24 #include "libkita/qcp932codec.h"
25 #include "part/kita2ch.h"
26 #include "kitaboardview.h"
27 #include "kita.h"
28 #include "libkita/board.h"
29 #include "libkita/category.h"
30
31
32 KitaBoardView::KitaBoardView(QWidget *parent, const char *name)
33     : KitaBoardViewBase(parent, name)
34 {
35   m_boardList->setSorting(-1);
36   m_boardList->addColumn("name");
37   connect(m_boardList, SIGNAL(executed(QListViewItem*)), SLOT( loadBoard(QListViewItem*) ));
38   connect( m_boardList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
39                         SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
40 }
41
42 KitaBoardView::~KitaBoardView()
43 {}
44
45 QString KitaBoardView::getCategory(const QString& line) const
46 {
47   QRegExp regexp("<BR><BR><B>(.*)</B><BR>", false);
48   if(regexp.search(line) != -1) {
49     return regexp.cap(1);
50   } else {
51     return QString::null;
52   }
53 }
54
55 Kita::Board KitaBoardView::getBoard(const QString& line) const
56 {
57   QRegExp regexp("<A HREF=(.*)>(.*)</A>", false);
58   if( regexp.search(line) != -1 ) {
59     QString board_url = regexp.cap(1);
60     QString board_title = regexp.cap(2);
61     return Kita::Board(board_url, board_title);
62   } else {
63     return Kita::Board(KURL());
64   }
65 }
66
67 bool KitaBoardView::isBoardUrl(const KURL& url) const
68 {
69   QRegExp url_2ch("http://.*\\.2ch\\.net/.*");
70   QRegExp url_bbspink("http://.*\\.bbspink\\.com/.*");
71   QRegExp url_www_2ch("http://www\\.2ch\\.net/.*");
72
73   if ( url.isEmpty() ) return false;
74
75   QString str = url.url();
76
77   if ( url_2ch.search(str) == -1 && url_bbspink.search(str) == -1 ) return false;
78   if ( url_www_2ch.search(str) != -1 ) return false;
79
80   return true;
81 }
82
83 void KitaBoardView::loadBoardList()
84 {
85   QString tmpFile;
86   QString url = "http://www.ff.iij4u.or.jp/~ch2/bbsmenu.html";
87   if( ! KIO::NetAccess::download(url, tmpFile) )
88   {
89     return;
90   }
91
92   QFile file(tmpFile);
93   if( ! file.open(IO_ReadOnly) )
94   {
95     return;
96   }
97
98   QTextStream stream(&file);
99   QCp932Codec cp932Codec;
100   stream.setCodec(&cp932Codec);
101   QPtrList<Kita::Category> categoryList = getCategoryList(stream.read());
102
103   // clear list
104   m_boardList->clear();
105
106   KListViewItem* previous = 0;
107   for(Kita::Category* category = categoryList.first(); category; category = categoryList.next())
108   {
109     QValueList<Kita::Board> board_list = category->getBoardList();
110
111     if( board_list.isEmpty() ) {
112       continue;
113     }
114
115     KListViewItem* categoryItem = new KListViewItem(m_boardList, previous, category->name());
116
117     KListViewItem* previousBoard = 0;
118     for(QValueListIterator<Kita::Board> it = board_list.begin(); it != board_list.end(); ++it)
119     {
120       Kita::Board board = (*it);
121       previousBoard = new KListViewItem(categoryItem, previousBoard, board.name(), board.url().url());
122     }
123     previous = categoryItem;
124   }
125 }
126
127 QPtrList<Kita::Category> KitaBoardView::getCategoryList(const QString& html) const
128 {
129   QPtrList<Kita::Category> result;
130
131   // XXX: maybe crash 
132   //  result.setAutoDelete( true );
133
134   QStringList lines = QStringList::split("\n", html);
135   QStringList::iterator it;
136
137   Kita::Category* current_category = 0;
138   for(it = lines.begin(); it != lines.end(); ++it) {
139     QString category_name = getCategory(*it);
140     if( category_name != QString::null ) {
141       current_category = new Kita::Category(category_name);
142       result.append(current_category);
143     } else {
144       Kita::Board board = getBoard(*it);
145
146       if( isBoardUrl(board.url()) ) {
147         current_category->append(board);
148       }
149     }
150   }
151 //  printCateogryList(result);
152   return result;
153 }
154
155 void KitaBoardView::printCateogryList(QPtrList<Kita::Category>& list)
156 {
157   Kita::Category* category;
158   for(category = list.first(); category; category = list.next()) {
159     if(category->name() == QString::null) continue;
160
161     QValueList<Kita::Board> board_list = category->getBoardList();
162     QValueListIterator<Kita::Board> it2;
163     for(it2 = board_list.begin(); it2 != board_list.end(); ++it2) {
164       kdDebug() << "category:" << category->name()
165       << "name:" << (const char*)(*it2).name()
166       << "url:" << (*it2).url().url() << endl;
167     }
168   }
169 }
170
171 void KitaBoardView::loadBoard( QListViewItem* item )
172 {
173   QString boardName = item->text(0);
174   KURL url = item->text(1);
175
176   if(item->text(1).isEmpty()) {
177     return;
178   }
179
180   Kita::Board board(url, boardName);
181   emit clicked( board );
182 }
183
184 void KitaBoardView::setFont( const QFont& font )
185 {
186   m_boardList->setFont(font);
187 }
188
189 void KitaBoardView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
190 {
191   if( item == 0 ) {
192     return;
193   }
194
195   KPopupMenu popup( 0 );
196   popup.insertItem( i18n("Open with Web Browser"), 0 );
197   popup.insertItem( i18n("Open with new tab(broken)"), 1 );
198
199   KURL url = item->text(1);
200
201   switch( popup.exec( point ) ) {
202   case 0:
203     KRun::runURL( url, "text/html" );
204     break;
205   case 1:
206 //    emit signalShowThreadWithNewTab( thread );
207     break;
208   default:
209     break;
210   }
211 }