OSDN Git Service

402814acd77191898cecdddbf5c02c6b741a1de4
[kita/kita.git] / kita / 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 <qregexp.h>
14
15
16 using namespace Kita;
17
18 QDict<Thread>* Thread::m_threadDict = 0;
19
20 Thread::Thread( const KURL& datURL )
21         : m_datURL( datURL ), m_threadName( 0 ) , m_resNum( 0 ), m_readNum( 0 ), m_viewPos( 0 )
22 {}
23
24 Thread::~Thread()
25 {}
26
27 const KURL& Thread::datURL() const
28 {
29     return m_datURL;
30 }
31
32 /* public */
33 const QString& Thread::threadName() const
34 {
35     return m_threadName;
36 }
37
38 /* public */
39 void Thread::setThreadName( QString threadName )
40 {
41     /* remove space */
42     QRegExp qrx( " +$" );
43     threadName.replace( qrx, "" );
44
45     /* unescape */
46     threadName.replace( "&lt;", "<" ).replace( "&gt;", ">" ).replace( "&amp;", "&" );
47
48     m_threadName = threadName;
49 }
50
51 /* public */
52 const 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 const 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 const 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 QValueList< int >& Thread::markList() const
90 {
91     return m_markList;
92 }
93
94 /* public */
95 void Thread::setMarkList( const QValueList< int >& markList )
96 {
97     m_markList = markList;
98 }
99
100 /* public */
101 bool Thread::isMarked( int num )
102 {
103     QValueList< 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.remove( 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 QDict<Thread>();
132     }
133
134     Thread* thread = m_threadDict->find( 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 == NULL ) return NULL;
147
148     return m_threadDict->find( datURL.prettyURL() );
149 }
150
151 void Thread::replace( const QString& fromURL, const QString& toURL )
152 {
153     if ( m_threadDict == NULL ) return ;
154     QDictIterator<Kita::Thread> it( *m_threadDict );
155     for ( ; it.current(); ++it ) {
156         QString url = it.currentKey();
157         Kita::Thread* thread = it.current();
158         if ( url.find( fromURL ) == 0 ) {
159             m_threadDict->remove( url );
160             url = url.replace( 0, fromURL.length(), toURL );
161             thread->m_datURL = url;
162             m_threadDict->insert( url, thread );
163             it.toFirst();
164         }
165     }
166 }