OSDN Git Service

Use `//' style comments
[kita/kita.git] / src / libkita / thread.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
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 "thread.h"
12
13 #include <QtCore/QRegExp>
14
15 using namespace Kita;
16
17 QMultiHash<QString, Thread*>* Thread::m_threadDict = 0;
18
19 Thread::Thread(const KUrl& datUrl)
20         : m_datUrl(datUrl), m_threadName(0) , m_resNum(0), m_readNum(0), m_viewPos(0)
21 {}
22
23 Thread::~Thread()
24 {}
25
26 const KUrl& Thread::datUrl() const
27 {
28     return m_datUrl;
29 }
30
31 /* public */
32 const QString& Thread::threadName() const
33 {
34     return m_threadName;
35 }
36
37 /* public */
38 void Thread::setThreadName(const QString& name)
39 {
40     QString threadName = name;
41     // remove space
42     QRegExp qrx(" +$");
43     threadName.remove(qrx);
44
45     // unescape
46     threadName.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&");
47
48     m_threadName = threadName;
49 }
50
51 /* public */
52 int Thread::resNum() const
53 {
54     return m_resNum;
55 }
56
57 /* public */
58 void Thread::setResNum(int num)
59 {
60     m_resNum = num;
61 }
62
63 /* public */
64 int Thread::readNum() const
65 {
66     return m_readNum;
67 }
68
69 /* public */
70 void Thread::setReadNum(int num)
71 {
72     m_readNum = num;
73     if (m_resNum < m_readNum) setResNum(m_readNum);
74 }
75
76 /* public */
77 int Thread::viewPos() const
78 {
79     return m_viewPos;
80 }
81
82 /* public */
83 void Thread::setViewPos(int num)
84 {
85     m_viewPos = num;
86 }
87
88 /* public */
89 const QList<int>& Thread::markList() const
90 {
91     return m_markList;
92 }
93
94 /* public */
95 void Thread::setMarkList(const QList<int>& markList)
96 {
97     m_markList = markList;
98 }
99
100 /* public */
101 bool Thread::isMarked(int num)
102 {
103     QList<int>::iterator it;
104     for (it = m_markList.begin(); it != m_markList.end(); ++it) {
105         if ((*it) == num) return true;
106     }
107
108     return false;
109 }
110
111 /* public */
112 bool Thread::setMark(int num, bool newStatus)
113 {
114     bool status = isMarked(num);
115     if (status == newStatus) return false;
116
117     if (newStatus) m_markList += num;
118     else m_markList.removeAll(num);
119
120     return true;
121 }
122
123
124 /*--------------------------------------------*/
125
126 /* static functions */
127
128 Thread* Thread::getByUrl(const KUrl& datUrl)
129 {
130     if (m_threadDict == 0) {
131         m_threadDict = new QMultiHash<QString, Thread*>();
132     }
133
134     Thread* thread = m_threadDict->value(datUrl.prettyUrl());
135     if (thread) return thread;
136
137     Thread* newThread = new Thread(datUrl);
138     m_threadDict->insert(datUrl.prettyUrl(), newThread);
139
140     return newThread;
141 }
142
143 /* static & public */
144 Thread* Thread::getByUrlNew(const KUrl& datUrl)
145 {
146     if (m_threadDict == 0) return 0;
147
148     return m_threadDict->value(datUrl.prettyUrl());
149 }
150
151 void Thread::replace(const QString& fromUrl, const QString& toUrl)
152 {
153     if (m_threadDict == 0) return ;
154         QHashIterator<QString, Thread*> it(*m_threadDict);
155     while (it.hasNext()) {
156         it.next();
157         QString url = it.key();
158         Thread* thread = it.value();
159         if (url.indexOf(fromUrl) == 0) {
160             m_threadDict->remove(url);
161             url = url.replace(0, fromUrl.length(), toUrl);
162             thread->m_datUrl = url;
163             m_threadDict->insert(url, thread);
164             it.toFront();
165         }
166     }
167 }