OSDN Git Service

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