All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ApplicationPlugins.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/af/ApplicationPlugins.cpp
22 
23  \brief A singleton for managing the application plugins.
24 */
25 
26 // TerraLib
27 #include "../../common/OSSettingsDir.h"
28 #include "../../common/SystemApplicationSettings.h"
29 #include "../../common/Translator.h"
30 #include "ApplicationPlugins.h"
31 #include "Exception.h"
32 
33 // Boost
34 #include <boost/filesystem.hpp>
35 
37 {
38 // look for the plugins file in order to load it
39  std::string plugins_conf_file_name = te::common::SystemApplicationSettings::getInstance().getValue("Application.PluginsFile.<xmlattr>.xlink:href");
40 
41  if(plugins_conf_file_name.empty())
42  plugins_conf_file_name = TERRALIB_APPLICATION_PLUGINS_FILE;
43 
44 // first: current application dir
45  boost::filesystem::path plugins_conf_file = boost::filesystem::current_path();
46 
47  plugins_conf_file /= TERRALIB_CONFIG_DIR;
48 
49  plugins_conf_file /= plugins_conf_file_name;
50 
51  if(boost::filesystem::is_regular_file(plugins_conf_file))
52  {
53  load(plugins_conf_file.string());
54  return;
55  }
56 
57 // second: system application data dir
58  const std::string& system_settings_path = te::common::OSSettingsDir::getInstance().getSystemSettingsPath();
59 
60  if(!system_settings_path.empty())
61  {
62  plugins_conf_file = system_settings_path;
63 
64  plugins_conf_file /= TERRALIB_DIR;
65 
66  plugins_conf_file /= plugins_conf_file_name;
67 
68  if(boost::filesystem::is_regular_file(plugins_conf_file))
69  {
70  load(plugins_conf_file.string());
71  return;
72  }
73  }
74 
75 // third: user dir
76  const std::string& user_settings_path = te::common::OSSettingsDir::getInstance().getUserSettingsPath();
77 
78  if(!user_settings_path.empty())
79  {
80  plugins_conf_file = user_settings_path;
81 
82  plugins_conf_file /= TERRALIB_DIR;
83 
84  plugins_conf_file /= plugins_conf_file_name;
85 
86  if(boost::filesystem::is_regular_file(plugins_conf_file))
87  {
88  load(plugins_conf_file.string());
89  return;
90  }
91  }
92 
93 // the last chance...
94  char* mgis_dir = getenv(TERRALIB_DIR_ENVVAR);
95 
96  if(mgis_dir != 0)
97  {
98  plugins_conf_file = mgis_dir;
99 
100  plugins_conf_file /= TERRALIB_CONFIG_DIR;
101 
102  plugins_conf_file /= plugins_conf_file_name;
103 
104  if(boost::filesystem::is_regular_file(plugins_conf_file))
105  {
106  load(plugins_conf_file.string());
107  return;
108  }
109  }
110 
111  throw Exception(TR_QT_AF("Can not find application plugins config file!"));
112 }
113 
114 void te::qt::af::ApplicationPlugins::load(const std::string& fileName)
115 {
116  ApplicationSettings::load(fileName);
117 }
118 
119 const std::map<std::string, std::string>& te::qt::af::ApplicationPlugins::getPluginsFiles() const
120 {
121  return m_plugins_files;
122 }
123 
124 void te::qt::af::ApplicationPlugins::addPlugin(const std::string& pluginName, const std::string& pluginFileName)
125 {
126  if(m_plugins_files.find(pluginName) == m_plugins_files.end())
127  m_plugins_files[pluginName] = pluginFileName;
128 }
129 
130 void te::qt::af::ApplicationPlugins::removePlugin(const std::string& pluginName)
131 {
132  std::map<std::string, std::string>::iterator it = m_plugins_files.find(pluginName);
133 
134  if(it != m_plugins_files.end())
135  m_plugins_files.erase(it);
136 }
137 
139 {
140  return m_file;
141 }
142 
143 void te::qt::af::ApplicationPlugins::addPlugins(const std::map<std::string, std::string>& plgs)
144 {
145  std::map<std::string, std::string>::const_iterator it;
146 
147  for(it=plgs.begin(); it!=plgs.end(); ++it)
148  addPlugin(it->first, it->second);
149 }
150 
151 void te::qt::af::ApplicationPlugins::removePlugins(const std::vector<std::string>& plgs)
152 {
153  std::vector<std::string>::const_iterator it;
154 
155  for(it=plgs.begin(); it!=plgs.end(); ++it)
156  removePlugin(*it);
157 }
158 
160  : QObject()
161 {
162 }
163 
165 {
166 }
167 
168 
#define TERRALIB_CONFIG_DIR
Folder location with application config files.
const std::map< std::string, std::string > & getPluginsFiles() const
const std::string & getUserSettingsPath() const
It returns the folder location to store per user data.
An exception class for the TerraLib Application Framework.
void addPlugin(const std::string &pluginName, const std::string &pluginFileName)
#define TERRALIB_DIR
The folder name on the system where terralib data is stored (on windows: app/terralib).
Definition: Config.h:47
#define TERRALIB_DIR_ENVVAR
Environment variable name with the TerraLib install path.
Definition: Config.h:56
void addPlugins(const std::map< std::string, std::string > &plgs)
#define TERRALIB_APPLICATION_PLUGINS_FILE
The file that contains the application plugins.
Definition: Config.h:44
A singleton for managing the application plugins.
void removePlugin(const std::string &pluginName)
ApplicationPlugins()
It initializes the singleton.
void removePlugins(const std::vector< std::string > &plgs)
#define TR_QT_AF(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:77
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:120
const std::string & getSystemSettingsPath() const
It returns the folder location to store application data applied to all users.
const std::string & getFileName() const