NCL Composer  0.1.5
 All Classes Functions Variables Pages
ProjectControl.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "modules/ProjectControl.h"
11 #include "modules/ProjectReader.h"
12 
13 namespace composer {
14  namespace core {
15 
16 INIT_SINGLETON (ProjectControl)
17 
19 {
20 }
21 
22 ProjectControl::~ProjectControl()
23 {
24  QMap<QString,Project*>::iterator it;
25  PluginControl *pg = PluginControl::getInstance();
26  for (it = openProjects.begin(); it != openProjects.end(); it++)
27  {
28  Project *project = it.value();
29  if (pg->releasePlugins(project))
30  {
31  delete project;
32  project = NULL;
33  } else {
34  qDebug() << "Error: Failed to releasePlugins ";
35  }
36  }
37 }
38 
39 bool ProjectControl::closeProject(QString location)
40 {
41  if (!openProjects.contains(location)) return false;
42 
43  Project *project = openProjects[location];
44  if (PluginControl::getInstance()->releasePlugins(project))
45  {
46  delete project;
47  project = NULL;
48  openProjects.remove(location);
49  }
50  else
51  {
52  qDebug() << "Error: Failed to close the project :" << location;
53  return false;
54  }
55  return true;
56 }
57 
58 bool ProjectControl::launchProject(QString location)
59 {
60  if (openProjects.contains(location))
61  {
62  emit projectAlreadyOpen(location);
63  return false;
64  }
65 
66  QString ext = location;
67  ext = ext.remove(0, ext.lastIndexOf(".") + 1);
68  LanguageType type = Utilities::getLanguageTypeByExtension( ext );
69 
70  if(type == NONE)
71  {
72  //\todo TEST ON WINDOWS
73 // QProcess spaw;
74 // QStringList args;
75 // args.append(location);
76 //#ifdef Q_WS_MAC
77 // spaw.startDetached("/usr/bin/open", args);
78 //#else
79 // spaw.startDetached("/usr/bin/gnome-open", args);
80 //#endif
81  return false;
82  }
83 
84  /* Requests the LanguageProfile associated with this DocumentType */
85  ILanguageProfile *profile = LanguageControl::getInstance()->
86  getProfileFromType(type);
87 
88  if (!profile)
89  {
90  emit notifyError(tr("No Language Profile Extension "
91  "found for (%1)").
92  arg(ext.toUpper()));
93  return false;
94  }
95 
96  emit startOpenProject(location);
97  QMap<QString,QString> atts;
98  QString documentId = location;
99 // documentId = documentId.remove(0,
100 // location.lastIndexOf(QDir::separator())+1);
101  documentId = documentId.remove(0, location.lastIndexOf("/") + 1);
102  atts["id"] = documentId;
103 
104  ProjectReader pr;
105  Project *project = pr.readFile(location);
106 
107  if(project != NULL)
108  {
109  project->setAtrributes(atts);
110  // The project was readed without a problem.
111  project->setLocation(location);
112  project->setProjectType(type);
113 
114  PluginControl::getInstance()->launchProject(project);
115  openProjects[location] = project;
116  connect ( project, SIGNAL(dirtyProject(bool)),
117  this, SLOT(projectIsDirty(bool)));
118  }
119  else
120  qDebug() << tr("Project could not be open!");
121 
122 
123 
124  emit endOpenProject(location);
125 
126  return true;
127 }
128 
129 void ProjectControl::importFromDocument( QString docLocation,
130  QString projLocation)
131 {
132  if (openProjects.contains(projLocation))
133  {
134  emit projectAlreadyOpen(projLocation);
135  return;
136  }
137 
138  QString ext = docLocation;
139  ext = ext.remove(0, ext.lastIndexOf(".") + 1);
140  LanguageType type = Utilities::getLanguageTypeByExtension( ext );
141 
142  if(type == NONE)
143  {
144  qDebug() << "File format not recognized.";
145  return;
146  }
147 
148  /* Requests the LanguageProfile associated with this DocumentType */
149  ILanguageProfile *profile = LanguageControl::getInstance()->
150  getProfileFromType(type);
151 
152  if (!profile)
153  {
154  emit notifyError(tr("No Language Profile Extension "
155  "found for (%1)").
156  arg(ext.toUpper()));
157  return;
158  }
159 
160  emit startOpenProject(projLocation);
161 
162  QMap<QString,QString> atts;
163  QString projectId = projLocation;
164  projectId.remove(0, projLocation.lastIndexOf("/")+1);
165  atts["id"] = projectId;
166 
167  ProjectReader pr;
168  Project *project = pr.readFile(projLocation);
169  project->setAtrributes(atts);
170 
171  if(project != NULL)
172  {
173  // The project was read without any problem.
174  project->setLocation(projLocation);
175  project->setProjectType(type);
176 
177  PluginControl::getInstance()->launchProject(project);
178  project->setLocation(projLocation);
179 
180  openProjects[projLocation] = project;
181 
182  IDocumentParser *parser;
183  parser = profile->createParser(project);
184  PluginControl::getInstance()->
185  connectParser(parser, PluginControl::getInstance()->
186  getMessageControl(project));
187 
188  QFile input(docLocation);
189  if(input.open(QIODevice::ReadOnly))
190  parser->parseContent(input.readAll());
191  else
192  qDebug() << tr("File could not be open!");
193 
194  input.close();
195  }
196  else
197  qDebug() << tr("Project could not be open!");
198 
199  emit endOpenProject(projLocation);
200 
201  project->setDirty(false);
202 
203 /* PluginControl::getInstance()->getMessageControl(project)
204  ->setCurrentProjectAsDirty(); */
205 }
206 
207 void ProjectControl::saveProject(QString location)
208 {
209  Project *project = openProjects.value(location);
210  QFile fout(location);
211 
212  //qDebug() << "Trying to save: " << location;
213  if(!fout.exists())
214  {
215  qDebug() << "The file (" << location << ") doesn't exists. It will be\
216  created.";
217  }
218 
219  if( !fout.open( QIODevice::WriteOnly ) )
220  {
221  // It could not open
222  qDebug() << "Failed to open file (" << location << ") for writing.";
223  return;
224  }
225 
226  QString content = project->toString();
227  fout.write(qCompress(content.toAscii(), content.size()));
228  fout.close();
229  project->setDirty(false);
230 }
231 
232 void ProjectControl::moveProject(QString location, QString dest, bool saveDest)
233 {
234  QFileInfo fileInfo(dest);
235  if(fileInfo.absoluteDir().exists())
236  {
237  Project *project = openProjects.value(location);
238  project->setLocation(dest);
239  openProjects.insert(dest, project);
240  openProjects.remove(location); //remove de old
241 
242  if(saveDest)
243  saveProject(dest);
244 
245  QMap<QString,QString> atts;
246  QString documentId = dest;
247  documentId = documentId.remove(0, location.lastIndexOf("/") + 1);
248  atts["id"] = documentId;
249  project->setAtrributes(atts);
250  }
251  else
252  qWarning() << "Could not copy the current project from " << location
253  << " to " << dest;
254 }
255 
257 {
258  Project *project = openProjects.value(location);
259  QFile fout(location+"~");
260 
261  qDebug() << "Trying to autosave: " << location;
262  if(!fout.exists())
263  {
264  qDebug() << "The file (" << location << ") doesn't exists. It will be\
265  created.";
266  }
267 
268  if( !fout.open( QIODevice::WriteOnly ) )
269  {
270  // It could not open
271  qDebug() << "Failed to open file (" << location << ") for writing.";
272  return;
273  }
274 
275  QString content = project->toString();
276  fout.write(qCompress(content.toAscii(), content.size()));
277  fout.close();
278 // project->setDirty(false);
279 }
280 
282 {
283  if(openProjects.contains(location))
284  return openProjects.value(location);
285 
286  return NULL;
287 }
288 
289 void ProjectControl::projectIsDirty(bool isDirty)
290 {
291  Project *project = qobject_cast<Project *> (QObject::sender());
292  if(project != NULL)
293  {
294  emit dirtyProject(project->getLocation(), isDirty);
295  }
296  else qDebug() << "Received a dirtyProject message for a NULL project";
297 
298 }
299 
300 } }//end namespace
301