OSDN Git Service

reformat
[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 ), m_isOpened( 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
89 /* public */
90 bool Thread::isOpened() const
91 {
92     return m_isOpened;
93 }
94
95 /* public */
96 void Thread::setIsOpened( bool isOpened )
97 {
98     m_isOpened = isOpened;
99 }
100
101 /* public */
102 const QValueList< int >& Thread::markList() const
103 {
104     return m_markList;
105 }
106
107 /* public */
108 void Thread::setMarkList( const QValueList< int >& markList )
109 {
110     m_markList = markList;
111 }
112
113 /* public */
114 bool Thread::isMarked( int num )
115 {
116     QValueList< int >::iterator it;
117     for ( it = m_markList.begin(); it != m_markList.end(); ++it ) {
118         if ( ( *it ) == num ) return TRUE;
119     }
120
121     return FALSE;
122 }
123
124 /* public */
125 bool Thread::setMark( int num, bool newStatus )
126 {
127     bool status = isMarked( num );
128     if ( status == newStatus ) return FALSE;
129
130     if ( newStatus ) m_markList += num;
131     else m_markList.remove( num );
132
133     return TRUE;
134 }
135
136
137 /*--------------------------------------------*/
138
139 /* static functions */
140
141 Thread* Thread::getByURL( const KURL& datURL )
142 {
143     if ( m_threadDict == 0 ) {
144         m_threadDict = new QDict<Thread>();
145     }
146
147     Thread* thread = m_threadDict->find( datURL.prettyURL() );
148     if ( thread ) return thread;
149
150     Thread* newThread = new Thread( datURL );
151     m_threadDict->insert( datURL.prettyURL(), newThread );
152
153     return newThread;
154 }
155
156 /* static & public */
157 Thread* Thread::getByURLNew( const KURL& datURL )
158 {
159     if ( m_threadDict == NULL ) return NULL;
160     if ( datURL.isEmpty() ) return NULL;
161     return m_threadDict->find( datURL.prettyURL() );
162 }
163
164 void Thread::replace( const QString& fromURL, const QString& toURL )
165 {
166     if ( m_threadDict == NULL ) return ;
167     QDictIterator<Kita::Thread> it( *m_threadDict );
168     for ( ; it.current(); ++it ) {
169         QString url = it.currentKey();
170         Kita::Thread* thread = it.current();
171         if ( url.find( fromURL ) == 0 ) {
172             m_threadDict->remove( url );
173             url = url.replace( 0, fromURL.length(), toURL );
174             thread->m_datURL = url;
175             m_threadDict->insert( url, thread );
176             it.toFirst();
177         }
178     }
179 }