1 /***************************************************************************
2 * Copyright (C) 2003 by Hideki Ikemoto *
3 * ikemo@users.sourceforge.jp *
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 ***************************************************************************/
11 #include <kio/netaccess.h>
14 #include <qtextcodec.h>
15 #include <qstringlist.h>
17 #include <klistview.h>
18 #include <kpopupmenu.h>
22 #include <qvaluelist.h>
24 #include "libkita/qcp932codec.h"
25 #include "part/kita2ch.h"
26 #include "kitaboardview.h"
28 #include "libkita/board.h"
29 #include "libkita/category.h"
32 KitaBoardView::KitaBoardView(QWidget *parent, const char *name)
33 : KitaBoardViewBase(parent, name)
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 ) ) );
42 KitaBoardView::~KitaBoardView()
45 QString KitaBoardView::getCategory(const QString& line) const
47 QRegExp regexp("<BR><BR><B>(.*)</B><BR>", false);
48 if(regexp.search(line) != -1) {
55 Kita::Board KitaBoardView::getBoard(const QString& line) const
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);
63 return Kita::Board(KURL());
67 bool KitaBoardView::isBoardUrl(const KURL& url) const
69 QRegExp url_2ch("http://.*\\.2ch\\.net/.*");
70 QRegExp url_bbspink("http://.*\\.bbspink\\.com/.*");
71 QRegExp url_www_2ch("http://www\\.2ch\\.net/.*");
73 if ( url.isEmpty() ) return false;
75 QString str = url.url();
77 if ( url_2ch.search(str) == -1 && url_bbspink.search(str) == -1 ) return false;
78 if ( url_www_2ch.search(str) != -1 ) return false;
83 void KitaBoardView::loadBoardList()
86 QString url = "http://www.ff.iij4u.or.jp/~ch2/bbsmenu.html";
87 if( ! KIO::NetAccess::download(url, tmpFile) )
93 if( ! file.open(IO_ReadOnly) )
98 QTextStream stream(&file);
99 QCp932Codec cp932Codec;
100 stream.setCodec(&cp932Codec);
101 QPtrList<Kita::Category> categoryList = getCategoryList(stream.read());
104 m_boardList->clear();
106 KListViewItem* previous = 0;
107 for(Kita::Category* category = categoryList.first(); category; category = categoryList.next())
109 QValueList<Kita::Board> board_list = category->getBoardList();
111 if( board_list.isEmpty() ) {
115 KListViewItem* categoryItem = new KListViewItem(m_boardList, previous, category->name());
117 KListViewItem* previousBoard = 0;
118 for(QValueListIterator<Kita::Board> it = board_list.begin(); it != board_list.end(); ++it)
120 Kita::Board board = (*it);
121 previousBoard = new KListViewItem(categoryItem, previousBoard, board.name(), board.url().url());
123 previous = categoryItem;
127 QPtrList<Kita::Category> KitaBoardView::getCategoryList(const QString& html) const
129 QPtrList<Kita::Category> result;
132 // result.setAutoDelete( true );
134 QStringList lines = QStringList::split("\n", html);
135 QStringList::iterator it;
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);
144 Kita::Board board = getBoard(*it);
146 if( isBoardUrl(board.url()) ) {
147 current_category->append(board);
151 // printCateogryList(result);
155 void KitaBoardView::printCateogryList(QPtrList<Kita::Category>& list)
157 Kita::Category* category;
158 for(category = list.first(); category; category = list.next()) {
159 if(category->name() == QString::null) continue;
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;
171 void KitaBoardView::loadBoard( QListViewItem* item )
173 QString boardName = item->text(0);
174 KURL url = item->text(1);
176 if(item->text(1).isEmpty()) {
180 Kita::Board board(url, boardName);
181 emit clicked( board );
184 void KitaBoardView::setFont( const QFont& font )
186 m_boardList->setFont(font);
189 void KitaBoardView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
195 KPopupMenu popup( 0 );
196 popup.insertItem( i18n("Open with Web Browser"), 0 );
197 popup.insertItem( i18n("Open with new tab(broken)"), 1 );
199 KURL url = item->text(1);
201 switch( popup.exec( point ) ) {
203 KRun::runURL( url, "text/html" );
206 // emit signalShowThreadWithNewTab( thread );