OSDN Git Service

Add BoardDatabase class
[kita/kita.git] / kita / src / favoritelistview.cpp
index eefb93c..5dedde1 100644 (file)
 /***************************************************************************
- *   Copyright (C) 2003 by Hideki Ikemoto                                  *
- *   ikemo@users.sourceforge.jp                                            *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- ***************************************************************************/
+*   Copyright (C) 2003 by Hideki Ikemoto                                  *
+*   ikemo@users.sourceforge.jp                                            *
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+***************************************************************************/
 
 #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/board.h"
-#include "kitacacheinfo.h"
+#include "libkita/kita_misc.h"
+#include "libkita/thread.h"
 
-#include <qmap.h>
-#include <qapplication.h>
-#include <qclipboard.h>
+using namespace Kita;
 
-#include <klocale.h>
-#include <kpopupmenu.h>
-#include <krun.h>
-#include <kdebug.h>
-
-enum FavoriteListViewRows {
-  Row_Board,
-  Row_Icon,
-  Row_Subject,
-  Row_ResNum,
-  Row_Read,
-  Row_Unread,
-  Row_DatName,
-  Row_DatURL
-};
-
-FavoriteListView::FavoriteListView(QWidget* parent, const char *name)
- : KListView(parent, name)
+/**
+ *
+ */
+FavoriteListView::FavoriteListView(QWidget* parent) : ThreadListView(parent)
 {
-  addColumn(i18n("Board"));
-  addColumn("");
-  addColumn(i18n("Title"));
-  addColumn(i18n("ResNum"));
-  addColumn(i18n("ReadNum"));
-  addColumn(i18n("Unread"));
-  addColumn(i18n("Dat"));
-
-  setColumnWidth(Row_Subject, 400);
-  setColumnWidthMode(Row_Subject, QListView::Manual);
-
-  connect( this, SIGNAL( clicked(QListViewItem*) ),
-                 SLOT( loadThread(QListViewItem*) ) );
-  connect( this, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
-                 SLOT( slotContextMenuRequested( QListViewItem*, const QPoint&, int ) ) );
-}
+    kindLabel->hide();
 
-FavoriteListView::~FavoriteListView()
-{
+    connect(subjectList, SIGNAL(itemClicked(QTableWidgetItem*)),
+             SLOT(loadThread(QTableWidgetItem*)));
+    connect(reloadButton, SIGNAL(clicked()),
+             SLOT(reload()));
+
+    showColumn(ColumnBoard);
 }
 
-void FavoriteListView::update()
+/**
+ *
+ */
+void FavoriteListView::refresh()
 {
-  const QDict<Kita::Thread>& threads = FavoriteThreads::getInstance()->threads();
-//  FavoriteThreads::const_iterator it;
-
-  clear();
-
-  KitaCacheInfo* cache = KitaCacheInfo::getInstance();
-
-  QDictIterator<Kita::Thread> it( threads );
-  for(; it.current(); ++it) {
-    const Kita::Thread* thread = it.current();
-
-    int readNum = cache->readNum( thread->datURL() );
-    int resNum = cache->resNum( thread->datURL() );  // TODO unused variables.
-
-    new KListViewItem( this,
-                       thread->boardName(),
-                       "",
-                       thread->name(),
-                       "",
-                       QString("%1").arg( readNum, 4 ),
-                       "",
-                       thread->datURL().fileName(),
-                       thread->datURL().url() );
-  }
+    // 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);
+        }
+    }
+    subjectList->setSortingEnabled(true);
+    // insert item.
+    for (int i = 0; i < count; i++) {
+        QString datUrl = FavoriteThreads::getDatUrl(i);
+
+        QDateTime since;
+        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));
+        }
+        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().url() == item->text( Row_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 = firstChild(); item; item = item->nextSibling() ) {
-    if( item->text( Row_DatURL ) == updated_thread.datURL().url() ) {
-      item->setText( Row_ResNum, QString("%1").arg( updated_thread.resNum(), 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 );
-
-  QString datName = item->text(Row_DatName);
-  KURL datURL = KURL( item->text(Row_DatURL) );
-
-  kdDebug() << "datURL = " << datURL.url() << endl;
-  Kita::Board board = Kita::Board( KURL( datURL, ".." ) );
-  kdDebug() << "board.url = " << board.url().url() << endl;
-  Kita::Thread thread( board.name(), datURL );
-  kdDebug() << "thread.url = %s" << thread.url() << endl;
-  thread.setName( item->text( Row_Subject ) );
-  thread.setResNum( item->text( Row_Read ).toInt() );
-
-  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);
+    }
 }