OSDN Git Service

Add BoardDatabase class
[kita/kita.git] / kita / src / favoritelistview.cpp
index 61724bf..5dedde1 100644 (file)
 
 #include "favoritelistview.h"
 
+#include <QtCore/QDateTime>
+#include <QtGui/QApplication>
+#include <QtGui/QClipboard>
+#include <QtGui/QTableWidgetItem>
+
+#include <kaction.h>
+#include <kmenu.h>
+#include <krun.h>
+
+#include "threadlistviewitem.h"
+#include "viewmediator.h"
+#include "libkita/boarddatabase.h"
+#include "libkita/datmanager.h"
 #include "libkita/favoritethreads.h"
 #include "libkita/kita_misc.h"
-#include "libkita/threadinfo.h"
+#include "libkita/thread.h"
 
-#include <qmap.h>
-#include <qapplication.h>
-#include <qclipboard.h>
-#include <qdatetime.h>
-#include <qtoolbutton.h>
-
-#include <klocale.h>
-#include <kpopupmenu.h>
-#include <krun.h>
-#include <kdebug.h>
-#include <klistview.h>
+using namespace Kita;
 
-FavoriteListView::FavoriteListView( QWidget* parent, const char* name )
-        : Kita::ThreadListView( parent, name )
+/**
+ *
+ */
+FavoriteListView::FavoriteListView(QWidget* parent) : ThreadListView(parent)
 {
-    subjectList->addColumn( i18n( "Board" ) );
+    kindLabel->hide();
 
-    ReloadButton->hide();
+    connect(subjectList, SIGNAL(itemClicked(QTableWidgetItem*)),
+             SLOT(loadThread(QTableWidgetItem*)));
+    connect(reloadButton, SIGNAL(clicked()),
+             SLOT(reload()));
 
-    connect( subjectList, SIGNAL( clicked( QListViewItem* ) ),
-             SLOT( loadThread( QListViewItem* ) ) );
-    connect( subjectList, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
-             SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
+    showColumn(ColumnBoard);
 }
 
-FavoriteListView::~FavoriteListView()
-{}
-
-void FavoriteListView::update()
+/**
+ *
+ */
+void FavoriteListView::refresh()
 {
-    // FIXME: KitaSubjectView::loadBoard()¤Ë¥³¥Ô¡¼
-    {
-        m_hitList.clear();
-        m_nextHitIndex = 0;
-        m_prevquery = "";
+    // clear
+    m_hitList.clear();
+    m_nextHitIndex = 0;
+    m_prevquery = QStringList();
+
+    subjectList->clearContents();
+
+    subjectList->setSortingEnabled(false);
+    int count = FavoriteThreads::count();
+    subjectList->setRowCount(count);
+    for (int i = 0; i < count; i++) {
+        for (int j = 0, k = subjectList->columnCount(); j < k; j++) {
+            ThreadListViewItem* item = new ThreadListViewItem(j);
+            subjectList->setItem(i, j, item);
+        }
     }
-
-    const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance() ->threads();
-    //  FavoriteThreads::const_iterator it;
-
-    subjectList->clear();
-
-    KitaThreadInfo* cache = KitaThreadInfo::getInstance();
-
-    QDictIterator<Kita::Thread> it( threads );
-    for ( ; it.current(); ++it ) {
-        const Kita::Thread* thread = it.current();
+    subjectList->setSortingEnabled(true);
+    // insert item.
+    for (int i = 0; i < count; i++) {
+        QString datUrl = FavoriteThreads::getDatUrl(i);
 
         QDateTime since;
-        since.setTime_t( KURL( thread->datURL() ).fileName().section( '.', 0, 0 ).toInt() );
-
-        int readNum = cache->readNum( thread->datURL() );
-        int resNum = KitaThreadInfo::resNum( thread->datURL() );
-
-        KListViewItem* item = new KListViewItem( subjectList );
-        item->setText( Col_Board, thread->boardName() );
-        item->setText( Col_Subject, Kita::unescape( thread->name() ) );
-        item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
-        if ( resNum > 0 ) {
-            item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
-            if ( resNum != readNum ) {
-                item->setText( Col_Unread, QString( "%1" ).arg( resNum - readNum, 4 ) );
-            }
+        since.setTime_t(datToSince(datUrl));
+
+        DatManager datManager(datUrl);
+        int viewPos = datManager.getViewPos();
+        int resNum = datManager.getResNum();
+
+        BoardDatabase db(datUrl);
+        subjectList->item(i, ColumnBoard)->setText(db.boardName());
+        subjectList->item(i, ColumnSubject)
+            ->setText(datManager.threadName());
+        subjectList->item(i, ColumnReadNum)
+            ->setText(QString("%1").arg(viewPos, 4));
+        if (resNum > 0) {
+            subjectList->item(i, ColumnResNum)
+                ->setText(QString("%1").arg(resNum, 4));
+        }
+        if (resNum != 0 && resNum != viewPos) {
+            subjectList->item(i, ColumnUnread)
+                ->setText(QString("%1").arg(resNum - viewPos, 4));
         }
-        item->setText( Col_Since, since.toString( "yy/MM/dd hh:mm" ) );
-        item->setText( Col_DatURL, thread->datURL() );
+        subjectList->item(i, ColumnSince)
+            ->setText(since.toString("yy/MM/dd hh:mm"));
+        subjectList->item(i, ColumnDatUrl)->setText(datUrl);
+    }
+    subjectList->sortItems(ColumnBoard);
+    for (int i = 0, j = subjectList->columnCount(); i < j; i++) {
+        subjectList->resizeColumnToContents(i);
+    }
+    for (int i = 0, j = subjectList->rowCount(); i < j; i++) {
+        subjectList->resizeRowToContents(i);
     }
 }
 
-void FavoriteListView::loadThread( QListViewItem* item )
+/**
+ *
+ */
+void FavoriteListView::loadThread(QTableWidgetItem* item)
 {
-    if ( ! item ) return ;
+    if (! item) return ;
 
-    const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance() ->threads();
-    //  FavoriteThreads::const_iterator it;
+    QString itemUrl = subjectList->item(item->row(), ColumnDatUrl)->text();
 
-    QDictIterator<Kita::Thread> it( threads );
-    for ( ; it.current(); ++it ) {
-        const Kita::Thread* thread = it.current();
+    for (int i = 0; FavoriteThreads::count() > i; i++) {
+        QString datUrl = FavoriteThreads::getDatUrl(i);
 
-        if ( thread->datURL() == item->text( Col_DatURL ) ) {
-            emit signalShowThread( thread );
+        if (datUrl == itemUrl) {
+            ViewMediator::getInstance()->openThread(datUrl);
         }
     }
 }
 
-void FavoriteListView::updateThread( const Kita::Thread* updated_thread )
+/**
+ *
+ */
+void FavoriteListView::reload()
 {
-    for ( QListViewItem * item = subjectList->firstChild(); item; item = item->nextSibling() ) {
-        if ( item->text( Col_DatURL ) == updated_thread->datURL() ) {
-            int resNum = updated_thread->resNum();
-            int readNum = KitaThreadInfo::readNum( updated_thread->datURL() );
-            item->setText( Col_ResNum, QString( "%1" ).arg( resNum, 4 ) );
-            item->setText( Col_Read, QString( "%1" ).arg( readNum, 4 ) );
+    QList<QString> boardList;
+
+    for (int i = 0; FavoriteThreads::count() > i; i++) {
+        QString datUrl = FavoriteThreads::getDatUrl(i);
+        BoardDatabase db(datUrl);
+        QString boardUrl = db.boardUrl();
+        if (boardList.contains(boardUrl) == 0) {
+            boardList.append(boardUrl);
         }
     }
-}
-
-// TODO: KitaSubjectView¥¯¥é¥¹¤ÎƱ¥á¥½¥Ã¥É¤È¤Û¤È¤ó¤ÉƱ¤¸
-void FavoriteListView::slotContextMenuRequested( QListViewItem* item, const QPoint& point, int )
-{
-    if ( item == 0 ) {
-        return ;
-    }
 
-    KPopupMenu popup( 0 );
-    popup.insertItem( i18n( "Open with Web Browser" ), 0 );
-    popup.insertItem( i18n( "Open with new tab" ), 1 );
-    popup.insertItem( i18n( "Copy title and URL" ), 2 );
-
-    Kita::Thread* thread = Kita::Thread::getByURL( item->text( Col_DatURL ) );
-
-    QClipboard* clipboard = QApplication::clipboard();
-
-    switch ( popup.exec( point ) ) {
-    case 0:
-        KRun::runURL( thread->url(), "text/html" );
-        break;
-    case 1:
-        emit signalShowThreadWithNewTab( thread );
-        break;
-    case 2:
-        clipboard->setText( thread->name() + "\n" + thread->url() );
-        break;
-    default:
-        break;
+    QList<QString>::const_iterator it;
+    for (it = boardList.begin(); it != boardList.end(); ++it) {
+        bool online = true;
+        QList<Thread*> threadList;
+        QList<Thread*> tmpList;
+        BoardDatabase db((*it));
+        db.getThreadList(false, online, threadList, tmpList);
     }
 }