NCL Composer  0.1.5
 All Classes Functions Variables Pages
LanguageControl.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/LanguageControl.h"
11 
12 namespace composer {
13  namespace core {
14 
15 INIT_SINGLETON (LanguageControl)
16 
17 LanguageControl::LanguageControl()
18 {
19  qDebug() << "LanguageControl::LanguageControl()";
20 }
21 
22 LanguageControl::~LanguageControl()
23 {
24  QMap<LanguageType, ILanguageProfile*>::iterator it;
25 
26  for(it = profiles.begin(); it != profiles.end(); it++)
27  {
28  ILanguageProfile *pf = it.value();
29  assert(pf != NULL);
30  delete pf;
31  pf = NULL;
32  }
33  profiles.clear();
34 }
35 
36 bool LanguageControl::removeProfile(LanguageType type)
37 {
38  if (!profiles.contains(type))
39  return false;
40 
41  ILanguageProfile *lp = profiles[type];
42  assert(lp != NULL);
43  if (lp == NULL)
44  return false;
45  delete lp;
46  lp = NULL;
47  profiles.remove(type);
48 
49  return true;
50 }
51 
53 {
54  ILanguageProfile *lProfile = NULL;
55  QPluginLoader loader(fileName);
56  //qDebug() << "trying to load: " << fileName;
57  QObject *profile = loader.instance();
58  if (profile) {
59  lProfile = qobject_cast<ILanguageProfile*> (profile);
60  if (lProfile) {
61  LanguageType type = lProfile->getLanguageType();
62  if (profiles.contains(type))
63  {
64  qDebug() << "LanguageControl::loadProfiles" <<
65  "Profile for language (" <<
66  Utilities::getExtensionForLanguageType(type) <<
67  ") already exists";
68  } else
69  {
70  profiles[type] = lProfile;
71  }
72  }
73  } else qDebug() << "Failed to load languageControl (" << fileName << ")"
74  << loader.errorString();
75  return lProfile;
76 }
77 
78 void LanguageControl::loadProfiles(QString profilesDirPath)
79 {
80  QDir profileDir = QDir(profilesDirPath);
81 
82  if(!profileDir.exists()) {
83  emit notifyError(tr("The Language Profile extension "
84  "directory (%1) does not exist!").arg(profilesDirPath));
85  return;
86  }
87  QStringList filter;
88  filter.append("*.so");
89  filter.append("*.dylib");
90  filter.append("*.dll");
91  profileDir.setNameFilters(filter);
92 
93  foreach (QString fileName, profileDir.entryList(QDir::Files
94  | QDir::NoSymLinks)) {
95  loadProfile(profileDir.absoluteFilePath(fileName));
96  }
97 }
98 
100 {
101  if (profiles.contains(type))
102  return profiles[type];
103  else
104  return NULL;
105 }
106 
107 QList<ILanguageProfile*> LanguageControl::getLoadedProfiles()
108 {
109  QMap<LanguageType, ILanguageProfile*>::iterator it;
110  QList<ILanguageProfile*> list;
111 
112  for (it = profiles.begin(); it != profiles.end(); it++)
113  {
114  list.append(it.value());
115  }
116  return list;
117 }
118 
119 } } //end namespace
120 
121