OSDN Git Service

refactoring.
[kita/kita.git] / kita / src / libkita / thread.cpp
1 /***************************************************************************
2 *   Copyright (C) 2003 by Hideki Ikemoto                                  *
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 <config.h>
14
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 #include <kurl.h>
19 #include <kdebug.h>
20 #include <kprotocolmanager.h>
21 #include <kglobal.h>
22 #include <kstandarddirs.h>
23
24 #include <kio/slaveconfig.h>
25 #include <kio/jobclasses.h>
26 #include <kio/scheduler.h>
27 #include <kio/netaccess.h>
28
29 #include <qwidget.h>
30 #include <qapplication.h>
31 #include <qregexp.h>
32
33 #include "board.h"
34 #include "kita_misc.h"
35 #include "qcp932codec.h"
36 #include "threadinfo.h"
37
38 using namespace Kita;
39
40 QDict<Thread>* Thread::m_threadDict = 0;
41
42 Thread::Thread()
43 {}
44
45 Thread::Thread( const QString& datURL, const QString& threadName )
46         : m_datURL( datURL ), m_threadName( threadName )
47 {}
48
49 Thread::~Thread()
50 {}
51
52 const QString& Thread::datURL() const
53 {
54     return m_datURL;
55 }
56
57 const QString& Thread::name() const
58 {
59     if ( ! Kita::Thread::getByURL( m_datURL ) ->m_threadName.isEmpty() ) {
60         return Kita::Thread::getByURL( m_datURL ) ->m_threadName;
61     }
62     return m_threadName;
63 }
64
65 Thread* Thread::getByURL( const QString& datURL )
66 {
67     if ( m_threadDict == 0 ) {
68         m_threadDict = new QDict<Thread>();
69     }
70
71     Thread* thread = m_threadDict->find( datURL );
72     if ( thread ) return thread;
73
74     Thread* newThread = new Thread( datURL );
75     m_threadDict->insert( datURL, newThread );
76
77     return newThread;
78 }
79
80 /* static & public */
81 Thread* Thread::getByURLNew( const KURL& datURL )
82 {
83     if ( m_threadDict == NULL ) return NULL;
84     if ( datURL.isEmpty() ) return NULL;
85     return m_threadDict->find( datURL.prettyURL() );
86 }
87
88 const QString& Thread::getName( const KURL& datURL )
89 {
90     Thread* thread = Thread::getByURLNew( datURL );
91     if ( thread == NULL ) {
92         return QString::null;
93     }
94     return thread->m_threadName;
95 }
96
97 void Thread::setName( const QString& datURL, QString threadName )
98 {
99     QRegExp qrx(" +$");    
100     threadName.replace( qrx, "" );
101     
102     if ( datURL.isNull() ) {
103         return ;
104     }
105
106     if ( m_threadDict == 0 ) {
107         m_threadDict = new QDict<Thread>();
108     }
109
110     if ( m_threadDict->find( datURL ) ) {
111         Thread * thread = getByURL( datURL );
112         thread->m_threadName = threadName;
113     } else {
114         Thread* newThread = new Thread( datURL, threadName );
115         m_threadDict->insert( datURL, newThread );
116     }
117
118     return ;
119 }
120
121 void Thread::replace( const QString& fromURL, const QString& toURL )
122 {
123     QDictIterator<Kita::Thread> it( *m_threadDict );
124     for( ; it.current(); ++it ) {
125         QString url = it.currentKey();
126         Kita::Thread* thread = it.current();
127         if ( url.find( fromURL ) == 0 ) {
128             m_threadDict->remove( url );
129             url = url.replace( 0, fromURL.length(), toURL );
130             thread->m_datURL = url;
131             Kita::Board::replace( fromURL, toURL );
132             m_threadDict->insert( url, thread );
133             it.toFirst();
134         }
135     }
136 }
137
138 NullThread::NullThread()
139 {}
140
141 NullThread::~NullThread()
142 {}