Replaced all std::cout with kDebug.
[tagua/yd.git] / src / mastersettings.cpp
blob0fa34bd1e97ab6edeb4d287ceee5d140f26b686f
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
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 "mastersettings.h"
13 #include <QTextStream>
14 #include <KDebug>
15 #include <KStandardDirs>
17 #include "common.h"
18 #include "foreach.h"
21 QDomElement MasterSettings::node() const {
22 if (m_node.isNull()) {
23 QFile f(m_filename);
24 if (!f.open(QFile::ReadOnly | QFile::Text) || !m_doc.setContent(&f)) {
25 kWarning() << "Unable to open configuration file for reading.";
27 // // create a stub configuration file
28 // {
29 // QFile stub(m_filename);
30 // if (!stub.open(QFile::WriteOnly | QFile::Text)) {
31 // kDebug() << "Unable to write to configuration file.";
32 // exit(1);
33 // }
34 // QTextStream stream(&stub);
35 // stream << "<?xml version=\"1.0\"?>\n"
36 // "<configuration>\n"
37 // "</configuration>\n";
38 // }
40 // // reopen it
41 // if (!f.open(QFile::ReadOnly | QFile::Text))
42 // exit(1);
44 m_doc.appendChild(m_doc.createElement("configuration"));
45 // kDebug() << m_doc.toString();
49 const_cast<QDomElement&>(m_node) = m_doc.documentElement();
50 Q_ASSERT(!m_node.isNull());
51 Q_ASSERT(!m_node.ownerDocument().isNull());
54 return m_node;
57 MasterSettings::MasterSettings() {
58 m_filename = KStandardDirs::locateLocal("config", "taguarc.xml");
61 MasterSettings::MasterSettings(const QString& filename, LookupType lookup) {
62 if (lookup == PathLookup)
63 m_filename = filename;
64 else
65 m_filename = KStandardDirs::locateLocal("config", filename);
68 MasterSettings::~MasterSettings() {
69 sync();
72 void MasterSettings::setupObserver(Observer& observer) {
73 connect(observer.object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)));
76 void MasterSettings::onChange(QObject* obj, const char* method) {
77 m_observers.push_front(Observer(obj, method, 0));
78 setupObserver(m_observers.front());
81 void MasterSettings::onChange(QObject* obj, const char* method, const char* dependency) {
82 if (dependency) {
83 // go backwards through all existing observers, searching for something
84 // on which we depend.
85 for (ObserverList::reverse_iterator it = m_observers.rbegin();
86 it != m_observers.rend(); ++it) {
87 const char* observer_class = it->object->metaObject()->className();
88 if (observer_class && strcmp(observer_class, dependency) == 0) {
89 // we hit a dependency wall: we can't be notified
90 // before *it, so add the new Observer just here
91 ObserverList::iterator us = m_observers.insert(it.base(), Observer(obj, method, dependency));
92 setupObserver(*us);
94 // now check for a cyclic dependency
95 const char* this_class = obj->metaObject()->className();
96 for (ObserverList::iterator it2 = m_observers.begin(); it2 != us; ++it2) {
97 if (it2->dependency && strcmp(it2->dependency, this_class) == 0) {
98 // something which is notified before has us a dependency
99 // this means that there is a cyclic dependency it2 -> us -> it.
100 kWarning() << "Removing a cyclic dependency:" <<
101 it2->object->metaObject()->className() << "->" <<
102 this_class << "->" <<
103 observer_class;
105 // remove the cycle
106 it2->dependency = 0;
110 // done
111 return;
116 // no dependency
117 onChange(obj, method);
120 void MasterSettings::sync() {
121 // store to file
122 QFile f(m_filename);
123 if (!f.open(QFile::WriteOnly | QFile::Text))
124 kError() << "Cannot open configuration file for writing";
125 else {
126 QTextStream stream(&f);
127 stream << node().ownerDocument().toString();
132 void MasterSettings::objectDestroyed(QObject* obj) {
133 for (ObserverList::iterator it = m_observers.begin();
134 it != m_observers.end();) {
135 if (it->object == obj) {
136 it = m_observers.erase(it);
138 else {
139 ++it;
144 void MasterSettings::changed() {
145 foreach (Observer& observer, m_observers) {
146 observer.object->metaObject()->invokeMethod(observer.object, observer.method, Qt::DirectConnection);
148 sync();
151 QString MasterSettings::filename() const {
152 return m_filename;
155 MasterSettings& settings() {
156 static MasterSettings static_settings;
157 return static_settings;