OSDN Git Service

Move the directories
[kita/kita.git] / src / libkita / thread.cpp
diff --git a/src/libkita/thread.cpp b/src/libkita/thread.cpp
new file mode 100644 (file)
index 0000000..ae541a3
--- /dev/null
@@ -0,0 +1,167 @@
+/***************************************************************************
+*   Copyright (C) 2004 by Kita Developers                                 *
+*   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 "thread.h"
+
+#include <QtCore/QRegExp>
+
+using namespace Kita;
+
+QMultiHash<QString, Thread*>* Thread::m_threadDict = 0;
+
+Thread::Thread(const KUrl& datUrl)
+        : m_datUrl(datUrl), m_threadName(0) , m_resNum(0), m_readNum(0), m_viewPos(0)
+{}
+
+Thread::~Thread()
+{}
+
+const KUrl& Thread::datUrl() const
+{
+    return m_datUrl;
+}
+
+/* public */
+const QString& Thread::threadName() const
+{
+    return m_threadName;
+}
+
+/* public */
+void Thread::setThreadName(const QString& name)
+{
+    QString threadName = name;
+    /* remove space */
+    QRegExp qrx(" +$");
+    threadName.remove(qrx);
+
+    /* unescape */
+    threadName.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&");
+
+    m_threadName = threadName;
+}
+
+/* public */
+int Thread::resNum() const
+{
+    return m_resNum;
+}
+
+/* public */
+void Thread::setResNum(int num)
+{
+    m_resNum = num;
+}
+
+/* public */
+int Thread::readNum() const
+{
+    return m_readNum;
+}
+
+/* public */
+void Thread::setReadNum(int num)
+{
+    m_readNum = num;
+    if (m_resNum < m_readNum) setResNum(m_readNum);
+}
+
+/* public */
+int Thread::viewPos() const
+{
+    return m_viewPos;
+}
+
+/* public */
+void Thread::setViewPos(int num)
+{
+    m_viewPos = num;
+}
+
+/* public */
+const QList<int>& Thread::markList() const
+{
+    return m_markList;
+}
+
+/* public */
+void Thread::setMarkList(const QList<int>& markList)
+{
+    m_markList = markList;
+}
+
+/* public */
+bool Thread::isMarked(int num)
+{
+    QList<int>::iterator it;
+    for (it = m_markList.begin(); it != m_markList.end(); ++it) {
+        if ((*it) == num) return true;
+    }
+
+    return false;
+}
+
+/* public */
+bool Thread::setMark(int num, bool newStatus)
+{
+    bool status = isMarked(num);
+    if (status == newStatus) return false;
+
+    if (newStatus) m_markList += num;
+    else m_markList.removeAll(num);
+
+    return true;
+}
+
+
+/*--------------------------------------------*/
+
+/* static functions */
+
+Thread* Thread::getByUrl(const KUrl& datUrl)
+{
+    if (m_threadDict == 0) {
+        m_threadDict = new QMultiHash<QString, Thread*>();
+    }
+
+    Thread* thread = m_threadDict->value(datUrl.prettyUrl());
+    if (thread) return thread;
+
+    Thread* newThread = new Thread(datUrl);
+    m_threadDict->insert(datUrl.prettyUrl(), newThread);
+
+    return newThread;
+}
+
+/* static & public */
+Thread* Thread::getByUrlNew(const KUrl& datUrl)
+{
+    if (m_threadDict == 0) return 0;
+
+    return m_threadDict->value(datUrl.prettyUrl());
+}
+
+void Thread::replace(const QString& fromUrl, const QString& toUrl)
+{
+    if (m_threadDict == 0) return ;
+       QHashIterator<QString, Thread*> it(*m_threadDict);
+    while (it.hasNext()) {
+        it.next();
+        QString url = it.key();
+        Thread* thread = it.value();
+        if (url.indexOf(fromUrl) == 0) {
+            m_threadDict->remove(url);
+            url = url.replace(0, fromUrl.length(), toUrl);
+            thread->m_datUrl = url;
+            m_threadDict->insert(url, thread);
+            it.toFront();
+        }
+    }
+}