1 /***************************************************************************
2 * Copyright (C) 2004 by Kita Developers *
3 * ikemo@users.sourceforge.jp *
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 ***************************************************************************/
11 #include "threadindex.h"
13 #include <QtCore/QFile>
16 #include <KConfigGroup>
19 #include "datmanager.h"
20 #include "kita_misc.h"
22 #include "threadinfo.h"
26 ThreadIndex::ThreadIndex(const KUrl& url) : m_url(url)
29 m_indexPath = cache.getIndexPath();
32 QString ThreadIndex::getSubject() const
34 KConfig config(m_indexPath);
35 return getSubjectPrivate(config);
38 void ThreadIndex::setSubject(const QString& str)
40 KConfig config(m_indexPath);
41 setSubjectPrivate(config, str);
44 int ThreadIndex::getResNum() const
46 KConfig config(m_indexPath);
47 return getResNumPrivate(config);
50 void ThreadIndex::setResNum(int resNum)
52 KConfig config(m_indexPath);
53 setResNumPrivate(config, resNum);
56 int ThreadIndex::getReadNum() const
58 KConfig config(m_indexPath);
59 return getReadNumPrivate(config, true);
62 void ThreadIndex::setReadNum(int readNum)
64 KConfig config(m_indexPath);
65 setReadNumPrivate(config, readNum);
68 int ThreadIndex::getViewPos() const
70 KConfig config(m_indexPath);
71 return getViewPosPrivate(config);
74 void ThreadIndex::setViewPos(int viewPos)
76 KConfig config(m_indexPath);
77 setViewPosPrivate(config, viewPos);
80 void ThreadIndex::setMarkList(const QList<int>& markList)
82 KConfig config(m_indexPath);
83 setMarkListPrivate(config, markList);
87 /*------------------------------------------------------------*/
90 /* load thread information */ /* public */
91 void ThreadIndex::loadIndex(Thread* thread, bool checkCached) const
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);
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);
106 if (subject.isEmpty()) thread->setThreadName("?");
107 else thread->setThreadName(subject);
109 /* load res number */
110 thread->setResNum(getResNumPrivate(config));
113 thread->setViewPos(getViewPosPrivate(config));
114 if (thread->viewPos() > thread->readNum())
115 thread->setReadNum(thread->viewPos());
118 thread->setMarkList(getMarkListPrivate(config));
123 /* save thread information */ /* public */
124 void ThreadIndex::saveIndex(const Thread* thread)
126 /* If readNum == 0, delete idx file */
127 if (thread->readNum() == 0) {
128 QFile::remove(m_indexPath);
130 KConfig config(m_indexPath);
132 /* save thread name */
133 setSubjectPrivate(config, thread->threadName());
135 /* save res number */
136 setResNumPrivate(config, thread->resNum());
138 /* save read number */
139 setReadNumPrivate(config, thread->readNum());
142 setViewPosPrivate(config, thread->viewPos());
145 setMarkListPrivate(config, thread->markList());
148 KUrl datUrl = getDatUrl(m_url);
149 int num = (thread->viewPos() ? thread->viewPos() : thread->readNum());
150 ThreadInfo::setReadNum(datUrl.prettyUrl(), num);
154 /*------------------------------------------------------------------*/
157 QString ThreadIndex::getSubjectPrivate(const KConfig& config) const
159 return config.group("").readEntry("Subject");
163 void ThreadIndex::setSubjectPrivate(KConfig& config, const QString& str)
165 config.group("").writeEntry("Subject", str);
171 int ThreadIndex::getResNumPrivate(KConfig& config) const
173 int resNum = config.group("").readEntry("ResNum", 0);
175 /* use obsoleted "cache" file */
177 KUrl datUrl = getDatUrl(m_url);
178 resNum = ThreadInfo::readNum(datUrl.prettyUrl());
180 config.group("").writeEntry("ResNum", resNum);
187 void ThreadIndex::setResNumPrivate(KConfig& config, int resNum)
189 config.group("").writeEntry("ResNum", resNum);
195 int ThreadIndex::getReadNumPrivate(KConfig& config, bool checkCached) const
197 /* If cache does not exist, return 0 */
200 QString path = cache.getPath();
201 if (!QFile::exists(path)) {
206 int readNum = config.group("").readEntry("ReadNum", 0);
210 /* use ViewPos instead of ReadNum. */
211 readNum = config.group("").readEntry("ViewPos", 0);
213 /* use obsoleted "cache" file */
215 KUrl datUrl = getDatUrl(m_url);
216 readNum = ThreadInfo::readNum(datUrl.prettyUrl());
220 config.group("").writeEntry("ReadNum", readNum);
227 void ThreadIndex::setReadNumPrivate(KConfig& config, int readNum)
229 config.group("").writeEntry("ReadNum", readNum);
235 int ThreadIndex::getViewPosPrivate(const KConfig& config) const
237 return config.group("").readEntry("ViewPos", 0);
241 void ThreadIndex::setViewPosPrivate(KConfig& config, int viewPos)
243 config.group("").writeEntry("ViewPos", viewPos);
249 QList<int> ThreadIndex::getMarkListPrivate(const KConfig& config) const
251 QList<int> default_value;
252 return config.group("").readEntry("Mark", default_value);
256 void ThreadIndex::setMarkListPrivate(KConfig& config,
257 const QList<int>& markList)
259 config.group("").writeEntry("Mark", markList);