]> Shamusworld >> Repos - architektonas/blob - src/base/scriptlist.cpp
Fixed problem with MDI activation.
[architektonas] / src / base / scriptlist.cpp
1 // scriptlist.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/02/2010  Added this text. :-)
15 //
16
17 #include "scriptlist.h"
18
19 #include "system.h"
20
21 ScriptList * ScriptList::uniqueInstance = NULL;
22
23 /**
24  * Default constructor.
25  */
26 ScriptList::ScriptList(): scriptIterator(scripts)
27 {
28 //Should be dealt with...
29 //#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
30 //    scripts.setAutoDelete(true);
31     //init();
32     //scriptListListeners.setAutoDelete(false);
33     //activeScript = NULL;
34 }
35
36 /*virtual*/ ScriptList::~ScriptList()
37 {
38         while (!scripts.isEmpty())
39                 delete scripts.takeFirst();
40 }
41
42 /**
43  * @return Instance to the unique script list.
44  */
45 /*static*/ ScriptList * ScriptList::instance()
46 {
47         if (uniqueInstance == NULL)
48                 uniqueInstance = new ScriptList();
49
50         return uniqueInstance;
51 }
52
53 /**
54  * Initializes the script list by creating Script
55  * objects, one for each script that could be found.
56  */
57 void ScriptList::init()
58 {
59         DEBUG->print("ScriptList::initScripts");
60
61         scripts.clear();
62         QStringList list = SYSTEM->getScriptList();
63         Script * script;
64
65         for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
66         {
67                 DEBUG->print("script: %s:", (*it).toLatin1().data());
68
69                 QFileInfo fi(*it);
70                 script = new Script(fi.baseName(), fi.absoluteFilePath());
71                 scripts.append(script);
72
73                 DEBUG->print("base: %s", fi.baseName().toLatin1().data());
74                 DEBUG->print("path: %s", fi.absoluteFilePath().toLatin1().data());
75         }
76
77         //Script* f = new Script("normal");
78         //scripts.append(f);
79 }
80
81 /**
82  * Removes all scripts in the scriptlist.
83  */
84 void ScriptList::clearScripts()
85 {
86         scripts.clear();
87 }
88
89 int ScriptList::countScripts()
90 {
91         return scripts.count();
92 }
93
94 /**
95  * Removes a script from the list.
96  * Listeners are notified after the script was removed from
97  * the list but before it gets deleted.
98  */
99 void ScriptList::removeScript(Script * script)
100 {
101     DEBUG->print("ScriptList::removeScript()");
102
103     // here the script is removed from the list but not deleted
104 //    scripts.remove(script);
105         // We have to delete it since AutoDelete does not exist in QList<T>
106         int i = scripts.indexOf(script);
107
108         if (i != -1)
109                 delete scripts.takeAt(i);
110
111     //for (uint i=0; i<scriptListListeners.count(); ++i) {
112     //    ScriptListListener* l = scriptListListeners.at(i);
113     //    l->scriptRemoved(script);
114     //}
115
116     // activate an other script if necessary:
117     //if (activeScript==script) {
118     //    activateScript(scripts.first());
119     //}
120
121     // now it's save to delete the script
122     //delete script;
123 }
124
125 /**
126  * @return Pointer to the script with the given name or
127  * \p NULL if no such script was found. The script will be loaded into
128  * memory if it's not already.
129  */
130 Script * ScriptList::requestScript(const QString & name)
131 {
132         DEBUG->print("ScriptList::requestScript %s",  name.toLatin1().data());
133
134         QString name2 = name.toLower();
135         Script * foundScript = NULL;
136
137         DEBUG->print("name2: %s", name2.toLatin1().data());
138
139         // Search our list of available scripts:
140 //      for(Script * s=scripts.first(); s!=NULL; s=scripts.next())
141         for(int i=0; i<scripts.size(); i++)
142         {
143                 Script * s = scripts[i];
144
145                 if (s->getName() == name2)
146                 {
147                         foundScript = s;
148                         break;
149                 }
150         }
151
152         // Script not found:
153         return foundScript;
154 }
155
156 //! @return First script of the list.
157 Script * ScriptList::firstScript()
158 {
159 //      return scripts.first();
160         scriptIterator.toFront();
161         return scriptIterator.next();
162 }
163
164 /** @return Next script from the list after
165  * calling firstScript() or nextScript().
166  */
167 Script * ScriptList::nextScript()
168 {
169 //      return scripts.next();
170         return scriptIterator.next();
171 }
172
173 /**
174  * @return Pointer to the script with the given name or
175  * \p NULL if no such script was found.
176  */
177 //Script* ScriptList::loadScript(const QString& name) {
178 //}
179
180 /**
181  * Tests the script list and its ability to load scripts.
182  */
183 bool ScriptList::test()
184 {
185     //ScriptList* l = ScriptList::instance();
186
187     //std::cout << "ScriptList: " << *l << std::endl;
188
189     return true;
190 }