]> Shamusworld >> Repos - architektonas/blob - src/base/rs_scriptlist.cpp
Initial import
[architektonas] / src / base / rs_scriptlist.cpp
1 /****************************************************************************
2 ** $Id: rs_scriptlist.cpp 1960 2005-03-12 12:22:01Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_scriptlist.h"
28
29 #include "rs_system.h"
30
31 RS_ScriptList * RS_ScriptList::uniqueInstance = NULL;
32
33 /**
34  * Default constructor.
35  */
36 RS_ScriptList::RS_ScriptList(): scriptIterator(scripts)
37 {
38 //Should be dealt with...
39 //#warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
40 //    scripts.setAutoDelete(true);
41     //init();
42     //scriptListListeners.setAutoDelete(false);
43     //activeScript = NULL;
44 }
45
46 /*virtual*/ RS_ScriptList::~RS_ScriptList()
47 {
48         while (!scripts.isEmpty())
49                 delete scripts.takeFirst();
50 }
51
52 /**
53  * @return Instance to the unique script list.
54  */
55 /*static*/ RS_ScriptList * RS_ScriptList::instance()
56 {
57         if (uniqueInstance == NULL)
58                 uniqueInstance = new RS_ScriptList();
59
60         return uniqueInstance;
61 }
62
63 /**
64  * Initializes the script list by creating RS_Script
65  * objects, one for each script that could be found.
66  */
67 void RS_ScriptList::init()
68 {
69         RS_DEBUG->print("RS_ScriptList::initScripts");
70
71         scripts.clear();
72         QStringList list = RS_SYSTEM->getScriptList();
73         RS_Script * script;
74
75         for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
76         {
77                 RS_DEBUG->print("script: %s:", (*it).toLatin1().data());
78
79                 QFileInfo fi(*it);
80                 script = new RS_Script(fi.baseName(), fi.absoluteFilePath());
81                 scripts.append(script);
82
83                 RS_DEBUG->print("base: %s", fi.baseName().toLatin1().data());
84                 RS_DEBUG->print("path: %s", fi.absoluteFilePath().toLatin1().data());
85         }
86
87         //RS_Script* f = new RS_Script("normal");
88         //scripts.append(f);
89 }
90
91 /**
92  * Removes all scripts in the scriptlist.
93  */
94 void RS_ScriptList::clearScripts()
95 {
96         scripts.clear();
97 }
98
99 int RS_ScriptList::countScripts()
100 {
101         return scripts.count();
102 }
103
104 /**
105  * Removes a script from the list.
106  * Listeners are notified after the script was removed from
107  * the list but before it gets deleted.
108  */
109 void RS_ScriptList::removeScript(RS_Script * script)
110 {
111     RS_DEBUG->print("RS_ScriptList::removeScript()");
112
113     // here the script is removed from the list but not deleted
114 //    scripts.remove(script);
115         // We have to delete it since AutoDelete does not exist in QList<T>
116         int i = scripts.indexOf(script);
117
118         if (i != -1)
119                 delete scripts.takeAt(i);
120
121     //for (uint i=0; i<scriptListListeners.count(); ++i) {
122     //    RS_ScriptListListener* l = scriptListListeners.at(i);
123     //    l->scriptRemoved(script);
124     //}
125
126     // activate an other script if necessary:
127     //if (activeScript==script) {
128     //    activateScript(scripts.first());
129     //}
130
131     // now it's save to delete the script
132     //delete script;
133 }
134
135 /**
136  * @return Pointer to the script with the given name or
137  * \p NULL if no such script was found. The script will be loaded into
138  * memory if it's not already.
139  */
140 RS_Script * RS_ScriptList::requestScript(const QString & name)
141 {
142         RS_DEBUG->print("RS_ScriptList::requestScript %s",  name.toLatin1().data());
143
144         QString name2 = name.toLower();
145         RS_Script * foundScript = NULL;
146
147         RS_DEBUG->print("name2: %s", name2.toLatin1().data());
148
149         // Search our list of available scripts:
150 //      for(RS_Script * s=scripts.first(); s!=NULL; s=scripts.next())
151         for(int i=0; i<scripts.size(); i++)
152         {
153                 RS_Script * s = scripts[i];
154
155                 if (s->getName() == name2)
156                 {
157                         foundScript = s;
158                         break;
159                 }
160         }
161
162         // Script not found:
163         return foundScript;
164 }
165
166 //! @return First script of the list.
167 RS_Script * RS_ScriptList::firstScript()
168 {
169 //      return scripts.first();
170         scriptIterator.toFront();
171         return scriptIterator.next();
172 }
173
174 /** @return Next script from the list after
175  * calling firstScript() or nextScript().
176  */
177 RS_Script * RS_ScriptList::nextScript()
178 {
179 //      return scripts.next();
180         return scriptIterator.next();
181 }
182
183 /**
184  * @return Pointer to the script with the given name or
185  * \p NULL if no such script was found.
186  */
187 //RS_Script* RS_ScriptList::loadScript(const QString& name) {
188 //}
189
190 /**
191  * Tests the script list and its ability to load scripts.
192  */
193 bool RS_ScriptList::test()
194 {
195     //RS_ScriptList* l = RS_ScriptList::instance();
196
197     //std::cout << "RS_ScriptList: " << *l << std::endl;
198
199     return true;
200 }