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.
11 #include "mastersettings.h"
13 #include <QTextStream>
15 #include <KStandardDirs>
21 QDomElement
MasterSettings::node() const {
22 if (m_node
.isNull()) {
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
29 // QFile stub(m_filename);
30 // if (!stub.open(QFile::WriteOnly | QFile::Text)) {
31 // kDebug() << "Unable to write to configuration file.";
34 // QTextStream stream(&stub);
35 // stream << "<?xml version=\"1.0\"?>\n"
36 // "<configuration>\n"
37 // "</configuration>\n";
41 // if (!f.open(QFile::ReadOnly | QFile::Text))
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());
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
;
65 m_filename
= KStandardDirs::locateLocal("config", filename
);
68 MasterSettings::~MasterSettings() {
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
) {
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
));
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
<< "->" <<
117 onChange(obj
, method
);
120 void MasterSettings::sync() {
123 if (!f
.open(QFile::WriteOnly
| QFile::Text
))
124 kError() << "Cannot open configuration file for writing";
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
);
144 void MasterSettings::changed() {
145 foreach (Observer
& observer
, m_observers
) {
146 observer
.object
->metaObject()->invokeMethod(observer
.object
, observer
.method
, Qt::DirectConnection
);
151 QString
MasterSettings::filename() const {
155 MasterSettings
& settings() {
156 static MasterSettings static_settings
;
157 return static_settings
;