OSDN Git Service

Rename boardmanager.cpp and boardmanager.h
[kita/kita.git] / kita / src / libkita / threadinfo.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 "threadinfo.h"
12
13 #include <QtCore/QDataStream>
14 #include <QtCore/QMap>
15
16 using namespace Kita;
17
18 ThreadInfo* ThreadInfo::instance = 0;
19
20 ThreadInfo::ThreadInfo() : m_readDict()
21 {}
22
23 ThreadInfo::~ThreadInfo()
24 {}
25
26 ThreadInfo* ThreadInfo::getInstance()
27 {
28     if (instance == 0) {
29         instance = new ThreadInfo();
30     }
31     return instance;
32 }
33
34 int ThreadInfo::readNum(const QString& url)
35 {
36     ThreadInfo * instance = ThreadInfo::getInstance();
37     if (instance->m_readDict.contains(url)) {
38         return instance->m_readDict[ url ];
39     } else {
40         return 0;
41     }
42 }
43
44 void ThreadInfo::setReadNum(const QString& url, int num)
45 {
46     ThreadInfo * instance = ThreadInfo::getInstance();
47     instance->m_readDict.insert(url, num);
48 }
49
50 void ThreadInfo::replace(const QString& fromUrl, const QString& toUrl)
51 {
52     QMap<QString, int>::Iterator it;
53     ThreadInfo* instance = ThreadInfo::getInstance();
54     if (instance == 0) return ;
55
56     for (it = instance->m_readDict.begin(); it != instance->m_readDict.end(); ++it) {
57         QString url = it.key();
58         int num = it.value();
59         if (url.indexOf(fromUrl) == 0) {
60             url = url.replace(0, fromUrl.length(), toUrl);
61             instance->m_readDict.erase(it);
62             instance->m_readDict.insert(url, num);
63             it = instance->m_readDict.begin(); // TODO もっと早い方法は?
64         }
65     }
66 }
67
68 void ThreadInfo::removeThreadInfo(const QString& url)
69 {
70     ThreadInfo * instance = ThreadInfo::getInstance();
71     instance->m_readDict.remove(url);
72 }
73 namespace Kita {
74     QDataStream& operator<<(QDataStream& s, ThreadInfo& c)
75     {
76         s << c.m_readDict;
77         return s;
78     }
79
80     QDataStream& operator>>(QDataStream& s, ThreadInfo& c)
81     {
82         s >> c.m_readDict;
83         return s;
84     }
85 }