summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/macros/lib/manager.cpp
blob: 0a0d59e0a58c64c3b32b1a80f7a6730443fff980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
 * This file is part of the KDE project
 * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
 * copyright (C) 2005 by Tobi Krebs (tobi.krebs@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "manager.h"
#include "action.h"
#include "function.h"
#include "macro.h"
#include "exception.h"

#include <tqobject.h>
#include <tqwidget.h>
#include <tqdom.h>
#include <kxmlguibuilder.h>
#include <kdebug.h>

using namespace KoMacro;

namespace KoMacro {

	/**
	* @internal d-pointer class to be more flexible on future extension of the
	* functionality without to much risk to break the binary compatibility.
	*/
	class Manager::Private
	{
		public:
			KXMLGUIClient* const xmlguiclient;
			TQMap<TQString, KSharedPtr<Macro > > macros;

			TQStringList actionnames;
			TQMap<TQString, KSharedPtr<Action> > actions;

			TQMap<TQString, TQGuardedPtr<TQObject> > objects;

			Private(KXMLGUIClient* const xmlguiclient)
				: xmlguiclient(xmlguiclient)
			{
			}
	};

	/// Pointer to our static singleton.
	static ::KoMacro::Manager* _self = 0;

	/// Automatically deletes our singleton on termination.
	static KStaticDeleter< ::KoMacro::Manager > _manager;

}

void Manager::init(KXMLGUIClient* xmlguiclient)
{
	if(! _self) {
		::KoMacro::Manager* manager = new ::KoMacro::Manager(xmlguiclient);
		_manager.setObject(_self, manager);
	}
	else {
		throw Exception("Already initialized.");
	}
}

Manager* Manager::self()
{
	//Q_ASSERT(_self);
	return _self;
}

Manager::Manager(KXMLGUIClient* const xmlguiclient)
	: d( new Private(xmlguiclient) ) // create the private d-pointer instance.
{
	kdDebug() << "Manager::Manager() Ctor" << endl;
	TQObject* obj = dynamic_cast<TQObject*>(xmlguiclient);
	if(obj) {
		d->objects.tqreplace(obj->name(), obj);
	}

	//TESTCASE
	d->objects.tqreplace("TestCase", new TQWidget());
}

Manager::~Manager()
{
	// destroy the private d-pointer instance.
	delete d;
}

KXMLGUIClient* Manager::guiClient() const
{
	return d->xmlguiclient;
}

bool Manager::hasMacro(const TQString& macroname)
{
	return d->macros.tqcontains(macroname);
}

KSharedPtr<Macro> Manager::getMacro(const TQString& macroname)
{
	return d->macros[macroname];
}

void Manager::addMacro(const TQString& macroname, KSharedPtr<Macro> macro)
{
	d->macros.tqreplace(macroname, macro);
}

void Manager::removeMacro(const TQString& macroname)
{
	d->macros.remove(macroname);
}

KSharedPtr<Macro> Manager::createMacro(const TQString& macroname)
{
	KSharedPtr<Macro> macro = KSharedPtr<Macro>( new Macro(macroname) );
	return macro;
}

KSharedPtr<Action> Manager::action(const TQString& name) const
{
	return d->actions[name];
}

Action::Map Manager::actions() const
{
	return d->actions;
}

TQStringList Manager::actionNames() const
{
	return d->actionnames;
}

void Manager::publishAction(KSharedPtr<Action> action)
{
	const TQString name = action->name();
	if(! d->actions.tqcontains(name)) {
		d->actionnames.append(name);
	}
	d->actions.tqreplace(name, action);
}

void Manager::publishObject(const TQString& name, TQObject* object)
{
	Q_ASSERT(! d->objects.tqcontains(name));
	d->objects.tqreplace(name, object);
}

TQGuardedPtr<TQObject> Manager::object(const TQString& name) const
{
	return d->objects[name];
}

TQMap<TQString, TQGuardedPtr<TQObject> > Manager::objects() const
{
	return d->objects;
}