]> Shamusworld >> Repos - architektonas/blob - src/base/rs_scriptlist.cpp
GPL compliance check...
[architektonas] / src / base / rs_scriptlist.cpp
1 // rs_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 "rs_scriptlist.h"
18
19 #include "rs_system.h"
20
21 RS_ScriptList * RS_ScriptList::uniqueInstance = NULL;
22
23 /**
24  * Default constructor.
25  */
26 RS_ScriptList::RS_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*/ RS_ScriptList::~RS_ScriptList()
37 {
38         while (!scripts.isEmpty())
39                 delete scripts.takeFirst();
40 }
41
42 /**
43  * @return Instance to the unique script list.
44  */
45 /*static*/ RS_ScriptList * RS_ScriptList::instance()
46 {
47         if (uniqueInstance == NULL)
48                 uniqueInstance = new RS_ScriptList();
49
50         return uniqueInstance;
51 }
52
53 /**
54  * Initializes the script list by creating RS_Script
55  * objects, one for each script that could be found.
56  */
57 void RS_ScriptList::init()
58 {
59         RS_DEBUG->print("RS_ScriptList::initScripts");
60
61         scripts.clear();
62         QStringList list = RS_SYSTEM->getScriptList();
63         RS_Script * script;
64
65         for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
66         {
67                 RS_DEBUG->print("script: %s:", (*it).toLatin1().data());
68
69                 QFileInfo fi(*it);
70                 script = new RS_Script(fi.baseName(), fi.absoluteFilePath());
71                 scripts.append(script);
72
73                 RS_DEBUG->print("base: %s", fi.baseName().toLatin1().data());
74                 RS_DEBUG->print("path: %s", fi.absoluteFilePath().toLatin1().data());
75         }
76
77         //RS_Script* f = new RS_Script("normal");
78         //scripts.append(f);
79 }
80
81 /**
82  * Removes all scripts in the scriptlist.
83  */
84 void RS_ScriptList::clearScripts()
85 {
86         scripts.clear();
87 }
88
89 int RS_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 RS_ScriptList::removeScript(RS_Script * script)
100 {
101     RS_DEBUG->print("RS_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     //    RS_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 RS_Script * RS_ScriptList::requestScript(const QString & name)
131 {
132         RS_DEBUG->print("RS_ScriptList::requestScript %s",  name.toLatin1().data());
133
134         QString name2 = name.toLower();
135         RS_Script * foundScript = NULL;
136
137         RS_DEBUG->print("name2: %s", name2.toLatin1().data());
138
139         // Search our list of available scripts:
140 //      for(RS_Script * s=scripts.first(); s!=NULL; s=scripts.next())
141         for(int i=0; i<scripts.size(); i++)
142         {
143                 RS_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 RS_Script * RS_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 RS_Script * RS_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 //RS_Script* RS_ScriptList::loadScript(const QString& name) {
178 //}
179
180 /**
181  * Tests the script list and its ability to load scripts.
182  */
183 bool RS_ScriptList::test()
184 {
185     //RS_ScriptList* l = RS_ScriptList::instance();
186
187     //std::cout << "RS_ScriptList: " << *l << std::endl;
188
189     return true;
190 }