arcsapplicationcomponent.cpp
1 /*
2  name: lib/arcsapplicationcomponent.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcsapplicationcomponent.h>
30 #include <arcs/arcsxmlhandler.h>
31 #include <arcs/arcsappmode.h>
32 #include <iostream>
33 #include <QThread>
34 #include <QEvent>
35 #include <arcs/arcslog.h>
36 
37 ARCSApplicationObject* ARCSApplicationComponent::defaultCore = 0;
38 void* ARCSApplicationComponent::_interface = 0;
39 
40 ARCSApplicationComponent* ARCSApplicationComponent::runningInstance = 0;
41 
43 {
44  if (defaultCore == 0)
45  defaultCore = new ARCSApplicationObject();
46 
48  /* further check in arcsxmlhandler indicates that this flag should be set to ARCS::ARCS_APP_NONE in order to
49  establish that the application is an empty one.
50  practically flags are never used for this constructor which is a shame.
51  */
52  currentMode = ARCS::ARCS_APP_NONE; //flag ; //ARCS::ARCS_APP_NONE;
53  setType("Application");
54  applicationFinished = true;
55  _interface = 0;
56 }
57 
58 
60 {
61  if (!context.getComponent("this"))
62  {
63  ARCSLog::logInformation(ARCS_SOURCE, "needed to make \"this\"");
65  }
66 
67  QVariant var;
68  var.setValue((QObject*)defaultCore);
69  return var;
70 }
71 
72 
73 
74 /*void* ARCSApplicationComponent::getInterface()
75 {
76  ARCSAbstractComponent* cmp = context.getComponent("this");
77  if (cmp)
78  cmp->instanciate();
79  else
80  {
81  std::cout << "[App] Needed to add this." << std::endl;
82  context.addComponent("this", ARCSApplicationComponent::getType());
83  cmp = this;
84  }
85 
86  std::cout << "cmp : "<< qPrintable(cmp->getType()) << ":"
87  << cmp->getGenuineComponentInstance().value<QObject*>() <<std::endl;
88 
89  return cmp->getGenuineComponentInstance().value<QObject*>();
90 }*/
91 
92 
94 {
95  int i;
96  ARCSLog::logInformation(ARCS_SOURCE,"$ Rev: 207 $");
97  applicationFinished = false;
98 
99  runningInstance = this;
100 
102 
103  if ( ! context.instanciatePool())
104  return false;
105 
106 
107  // First loop : starts the processes
108  for ( i = 1; i < processes.count() ; i++)
109  {
110  processes.at(i)->setReferenceApplicationMode(currentMode);
111  processes.at(i)->startProcess();
112  }
113 
114  // Second loop : wait for the processes to enable their first sheet
115  for ( i = 1; i < processes.count() ; i++)
116  processes.at(i)->waitForFirstSheet();
117 
118  processes.at(0)->startProcess();
119 
120  if (processes.at(0)->hasFinished())
121  ARCSLog::logInformation(ARCS_SOURCE,"main process has finished.");
122  //applicationFinished = true;
123  //runningInstance = 0;
124  return true;
125 }
126 
127 
129 {
130  applicationFinished = true;
131 // emit finished();
132 }
133 
135 {
136  runningInstance = 0;
137  return false;
138 }
139 
140 void ARCSApplicationComponent::removeComponent(QString name)
141 {
142  if ( ! context.getComponentList().contains(name))
143  return ;
144 
145  // first we have to scan all process and all sheets in order to remove the component from all of this !
146  for (int i=0; i < processes.count(); i++)
147  {
148  int j;
149  if (processes[i]->getControllerId() == name)
150  processes[i]->setController();
151  QStringList sheetNames = processes[i]->getSheetNames();
152 
153  for (j=0; j < sheetNames.count(); j++ )
154  processes[i]->getSheet(sheetNames[j]).removeComponent(name);
155  }
156 
157  context.removeComponent(name);
158 }
159 
161 {
162  ARCSXMLHandler xmlHandler;
163  if (! xmlHandler.openFile(s))
164  return false;
165  return xmlHandler.parseApplication(this);
166 }
167 
169 {
170  if (s.isEmpty())
171  return true;
172 
173  ARCSXMLHandler xmlHandler;
174  xmlHandler.setContents(s);
175  return xmlHandler.parseApplication(this);
176 }
177 
178 
180 {
182 
183  for (int i = processes.count()-1 ; i >= 0;i--)
184  {
185  processes.at(i)->wait();
186  }
187 
188 
189 }
190 
bool setContents(QString s)
Gives the content to parse.
ARCSAppFlag
Flags to specify in which mode applications should start.
Definition: arcs.h:41
Application objects are used to implement specific interfaces when the application needs to be embedd...
virtual bool parseString(QString s)
Defines a basic seralization mechanism.
This class represents an application.
QStringList getComponentList()
bool openFile(QString s)
Opens a file given its path. The file should describe either an application or a composite component...
bool addComponent(QString name, QString className, QString contents=QString::null)
Adds a component to the list.
None of the application mode has been selected so far.
Definition: arcs.h:43
void removeComponent(QString name)
Removes a component from the component pool.
ARCSAbstractComponent * getComponent(QString name)
Retrieves a component by its id.
virtual bool loadFile(QString fn)
Defines a way to load a component description from a file.
bool instanciatePool()
Instanciates the whole pool of components inside the context.
virtual QVariant getGenuineComponentInstance()
ARCSApplicationComponent(ARCS::ARCSAppFlag flag=ARCS::ARCS_APP_EVENT)
This is an XML Handler for all ARCS native XML formats.