OSDN Git Service

2e3063ce4b61cabeb6c9171f33e4965f72a8194e
[kita/kita.git] / kita / src / libkita / threadindex.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 #include "threadindex.h"
11
12 #include <kconfig.h>
13
14 #include <qfile.h>
15
16 #include "cache.h"
17 #include "thread.h"
18 #include "threadinfo.h"
19 #include "kita_misc.h"
20 #include "datmanager.h"
21
22 using namespace Kita;
23
24 QString ThreadIndex::getSubject( const KURL& url )
25 {
26     QString indexPath = Kita::Cache::getIndexPath( url );
27     KConfig config( indexPath );
28     return getSubjectPrivate( config );
29 }
30
31 void ThreadIndex::setSubject( const KURL& url, const QString& str )
32 {
33     QString indexPath = Kita::Cache::getIndexPath( url );
34     KConfig config( indexPath );
35     setSubjectPrivate( str, config );
36 }
37
38 int ThreadIndex::getResNum( const KURL& url )
39 {
40     QString indexPath = Kita::Cache::getIndexPath( url );
41     KConfig config( indexPath );
42     return getResNumPrivate( url, config );
43 }
44
45 void ThreadIndex::setResNum( const KURL& url, int resNum )
46 {
47     QString indexPath = Kita::Cache::getIndexPath( url );
48     KConfig config( indexPath );
49     setResNumPrivate( resNum, config );
50 }
51
52 int ThreadIndex::getReadNum( const KURL& url )
53 {
54     QString indexPath = Kita::Cache::getIndexPath( url );
55     KConfig config( indexPath );
56     return getReadNumPrivate( url, config, TRUE );
57 }
58
59 void ThreadIndex::setReadNum( const KURL& url, int readNum )
60 {
61     QString indexPath = Kita::Cache::getIndexPath( url );
62     KConfig config( indexPath );
63     setReadNumPrivate( readNum, config );
64 }
65
66 int ThreadIndex::getViewPos( const KURL& url )
67 {
68     QString indexPath = Kita::Cache::getIndexPath( url );
69     KConfig config( indexPath );
70     return getViewPosPrivate( config );
71 }
72
73 void ThreadIndex::setViewPos( const KURL& url, int viewPos )
74 {
75     QString indexPath = Kita::Cache::getIndexPath( url );
76     KConfig config( indexPath );
77     setViewPosPrivate( viewPos, config );
78 }
79
80 void ThreadIndex::setMarkList( const KURL& url, const QValueList< int >& markList )
81 {
82     QString indexPath = Kita::Cache::getIndexPath( url );
83     KConfig config( indexPath );
84     setMarkListPrivate( markList, config );
85 }
86
87
88 /*------------------------------------------------------------*/
89
90
91 /* load thread information */  /* public */ /* static */
92 void ThreadIndex::loadIndex( Kita::Thread* thread, const KURL& url, bool checkCached )
93 {
94     QString indexPath = Kita::Cache::getIndexPath( url );
95     KConfig config( indexPath );
96
97     /* load read number */
98     int readNum = getReadNumPrivate( url, config, checkCached );
99     if( readNum == 0 ) return;  /* cache does not exist. */
100     thread->setReadNum( readNum );
101    
102     /* load thread name */
103     QString subject = getSubjectPrivate( config );
104     if( subject == QString::null && thread->threadName() != QString::null ){
105        subject = thread->threadName();
106        config.writeEntry( "Subject", subject );
107     }
108     if( subject == QString::null ) thread->setThreadName( "?" );
109     else thread->setThreadName( subject );
110
111     /* load res number */
112     thread->setResNum( getResNumPrivate( url, config ) );
113
114     /* load view pos */
115     thread->setViewPos( getViewPosPrivate( config ) );
116     if( thread->viewPos() > thread->readNum() ) thread->setReadNum( thread->viewPos() );
117
118     /* load mark */
119     thread->setMarkList( getMarkListPrivate( config ) );
120 }
121
122
123
124 /* save thread information */  /* public */ /* static */
125 void ThreadIndex::saveIndex( const Kita::Thread* thread, const KURL& url )
126 {
127     /* If readNum == 0, delete idx file */
128     if( thread->readNum() == 0 ){
129         
130         QString indexPath = Kita::DatManager::getCacheIndexPath( url );
131         QFile::remove( indexPath );
132         qDebug("delete %s",indexPath.ascii());
133     }
134
135     QString indexPath = Kita::Cache::getIndexPath( url );
136     KConfig config( indexPath );
137         
138     /* save thread name */
139     setSubjectPrivate( thread->threadName(), config );
140         
141     /* save res number */
142     setResNumPrivate( thread->resNum(), config );
143
144     /* save read number */
145     setReadNumPrivate( thread->readNum(), config );
146
147     /* save view pos */
148     setViewPosPrivate( thread->viewPos(), config );
149
150     /* save mark */
151     setMarkListPrivate( thread->markList(), config );
152     
153     /* save "cache" */
154     KURL datURL = Kita::getDatURL( url );
155     int num = ( thread->viewPos() ? thread->viewPos() : thread->readNum() );
156     KitaThreadInfo::setResNum( datURL.prettyURL(), thread->resNum() );
157     KitaThreadInfo::setReadNum( datURL.prettyURL(), num );
158 }
159
160
161 /*------------------------------------------------------------------*/
162
163 /* private */ /* static */
164 QString ThreadIndex::getSubjectPrivate( KConfig& config )
165 {
166     return config.readEntry( "Subject" );
167 }
168
169 /* private */ /* static */
170 void ThreadIndex::setSubjectPrivate( const QString& str, KConfig& config )
171 {
172     config.writeEntry( "Subject", str );
173 }
174
175 /*-------*/
176
177 /* private */ /* static */
178 int ThreadIndex::getResNumPrivate( const KURL& url, KConfig& config )
179 {
180     int resNum = config.readNumEntry( "ResNum" );
181     
182     /* use obsoleted "cache" file */
183     if( !resNum ){
184         KURL datURL = Kita::getDatURL( url );
185         resNum = KitaThreadInfo::readNum( datURL.prettyURL() );
186         if( resNum ) config.writeEntry( "ResNum", resNum );
187     }
188
189     return resNum;
190 }
191
192 /* private */ /* static */
193 void ThreadIndex::setResNumPrivate( int resNum, KConfig& config )
194 {
195     config.writeEntry( "ResNum", resNum );
196 }
197
198 /*-------*/
199
200 /* private */ /* static */
201 int ThreadIndex::getReadNumPrivate( const KURL& url, KConfig& config, bool checkCached )
202 {
203     /* If cache does not exist, return 0 */
204     if( checkCached ){
205         
206         QString path = Kita::DatManager::getCachePath( url );
207         if( ! QFile::exists( path ) ){
208             qDebug("%s does not exits",path.ascii());
209             return 0;
210         }
211     }
212     
213     int readNum = config.readNumEntry( "ReadNum" );
214
215     if( !readNum ){
216
217         /* use ViewPos instead of ReadNum. */
218         readNum = config.readNumEntry( "ViewPos" );
219                         
220         /* use obsoleted "cache" file */
221         if( !readNum ){
222             KURL datURL = Kita::getDatURL( url );
223             readNum = KitaThreadInfo::readNum( datURL.prettyURL() );
224         }
225
226         if( readNum ) config.writeEntry( "ReadNum", readNum );
227     }
228
229     return readNum;
230 }
231
232 /* private */ /* static */
233 void ThreadIndex::setReadNumPrivate( int readNum, KConfig& config )
234 {
235     config.writeEntry( "ReadNum", readNum );
236 }
237
238 /*-------*/
239
240 /* private */ /* static */
241 int ThreadIndex::getViewPosPrivate( KConfig& config )
242 {
243     return config.readNumEntry( "ViewPos" );
244 }
245
246 /* private */ /* static */
247 void ThreadIndex::setViewPosPrivate( int viewPos, KConfig& config )
248 {
249     config.writeEntry( "ViewPos", viewPos );
250 }
251
252 /*-------*/
253
254 /* private */ /* static */
255 QValueList< int > ThreadIndex::getMarkListPrivate( KConfig& config )
256 {
257     return config.readIntListEntry( "Mark" );
258 }
259
260 /* private */ /* static */
261 void ThreadIndex::setMarkListPrivate( const QValueList< int >& markList, KConfig& config )
262 {
263     config.writeEntry( "Mark", markList );
264 }