Scroom  0.14
PluginManager Class Reference

#include <pluginmanager.hh>

Inheritance diagram for PluginManager:
Inheritance graph
Collaboration diagram for PluginManager:
Collaboration graph

Public Types

using Ptr = boost::shared_ptr< PluginManager >
 
- Public Types inherited from ScroomPluginInterface
using Ptr = boost::shared_ptr< ScroomPluginInterface >
 

Public Member Functions

bool doWork () override
 
void addHook (bool devMode)
 
void registerNewPresentationInterface (const std::string &identifier, NewPresentationInterface::Ptr newPresentationInterface) override
 
void registerNewAggregateInterface (const std::string &identifier, NewAggregateInterface::Ptr newAggregateInterface) override
 
void registerOpenPresentationInterface (const std::string &extension, OpenPresentationInterface::Ptr openPresentationInterface) override
 
void registerOpenTiledBitmapInterface (const std::string &identifier, boost::shared_ptr< Scroom::TiledBitmap::OpenTiledBitmapInterface > openTiledBitmapInterface) override
 
void registerOpenInterface (const std::string &extension, OpenInterface::Ptr openInterface) override
 
void registerViewObserver (const std::string &identifier, ViewObserver::Ptr observer) override
 
void registerPresentationObserver (const std::string &identifier, PresentationObserver::Ptr observer) override
 
const std::map< NewPresentationInterface::Ptr, std::string > & getNewPresentationInterfaces ()
 
const std::map< std::string, NewAggregateInterface::Ptr > & getNewAggregateInterfaces ()
 
const std::map< OpenPresentationInterface::Ptr, std::string > & getOpenPresentationInterfaces ()
 
const std::map< Scroom::TiledBitmap::OpenTiledBitmapInterface::Ptr, std::string > & getOpenTiledBitmapInterfaces ()
 
const std::map< OpenInterface::Ptr, std::string > & getOpenInterfaces ()
 
const std::map< ViewObserver::Ptr, std::string > & getViewObservers ()
 
const std::map< PresentationObserver::Ptr, std::string > & getPresentationObservers ()
 
- Public Member Functions inherited from Scroom::Utils::Base
 Base ()=default
 
 Base (const Base &)=delete
 
 Base (Base &&)=delete
 
Baseoperator= (const Base &)=delete
 
Baseoperator= (Base &&)=delete
 
virtual ~Base ()=default
 
template<typename R >
boost::shared_ptr< R > shared_from_this ()
 
template<typename R >
boost::shared_ptr< R const > shared_from_this () const
 

Static Public Member Functions

static Ptr create ()
 
static PluginManager::Ptr getInstance ()
 

Private Types

enum  PluginManagerState { FINDING_DIRECTORIES, SCANNING_DIRECTORIES, LOADING_FILES, DONE }
 

Private Member Functions

void setStatusBarMessage (const char *message)
 
 PluginManager ()=default
 

Private Attributes

bool devMode {false}
 
PluginManagerState state {FINDING_DIRECTORIES}
 
std::list< std::string > dirs
 
std::list< std::string >::iterator currentDir
 
std::list< std::string > files
 
std::list< std::string >::iterator currentFile
 
std::list< PluginInformationpluginInformationList
 
std::map< NewPresentationInterface::Ptr, std::string > newPresentationInterfaces
 
std::map< std::string, NewAggregateInterface::PtrnewAggregateInterfaces
 
std::map< OpenPresentationInterface::Ptr, std::string > openPresentationInterfaces
 
std::map< Scroom::TiledBitmap::OpenTiledBitmapInterface::Ptr, std::string > openTiledBitmapInterfaces
 
std::map< OpenInterface::Ptr, std::string > openInterfaces
 
std::map< ViewObserver::Ptr, std::string > viewObservers
 
std::map< PresentationObserver::Ptr, std::string > presentationObservers
 

Member Typedef Documentation

◆ Ptr

using PluginManager::Ptr = boost::shared_ptr<PluginManager>

Member Enumeration Documentation

◆ PluginManagerState

Enumerator
FINDING_DIRECTORIES 
SCANNING_DIRECTORIES 
LOADING_FILES 
DONE 
48  {
52  DONE
53  };

Constructor & Destructor Documentation

◆ PluginManager()

PluginManager::PluginManager ( )
privatedefault

Referenced by create().

Here is the caller graph for this function:

Member Function Documentation

◆ addHook()

void PluginManager::addHook ( bool  devMode)
247 {
248  devMode = devMode_;
249  gdk_threads_add_idle(on_idle, static_cast<WorkInterface*>(this));
250  // progressbar = GTK_PROGRESS_BAR(lookup_widget(scroom, "progressbar"));
251  // statusbar = GTK_STATUSBAR(lookup_widget(scroom, "statusbar"));
252  //
253  // status_context_id = gtk_statusbar_get_context_id(statusbar, "Plugin Manager");
255 }
Here is the call graph for this function:

◆ create()

PluginManager::Ptr PluginManager::create ( )
static
31 { return Ptr(new PluginManager()); }
Here is the call graph for this function:

◆ doWork()

bool PluginManager::doWork ( )
overridevirtual

Implements WorkInterface.

36 {
37  bool retval = true;
38 
39  switch(state)
40  {
42  {
43  setStatusBarMessage("Locating plugin directories");
44  char* path = getenv(SCROOM_PLUGIN_DIRS.c_str());
45  dirs.clear();
46 
47  if(!devMode)
48  {
49 #ifdef _WIN32
50  // We want to keep everything portable on windows so we look for the plugin folder in the same directory as the .exe
51  std::string plugin_path = (boost::dll::program_location().parent_path() / "plugins").generic_string();
52  dirs.push_back(plugin_path);
53 #else
54  dirs.emplace_back(PLUGIN_DIR);
55 #endif
56  }
57 
58  if(path != nullptr)
59  {
60  spdlog::debug("{} = {}", SCROOM_PLUGIN_DIRS, path);
61 
62  for(char* i = path; *i != '\0'; i++)
63  {
64 
65 // Windows uses semicolons for delimiting environment variables, Linux uses colons
66 #ifdef _WIN32
67  const char envDelim = ';';
68 #else
69  const char envDelim = ':';
70 #endif
71  if(*i != envDelim)
72  {
73  continue;
74  }
75 
76  *i = '\0';
77  dirs.emplace_back(path);
78  path = i + 1;
79  }
80 
81  dirs.emplace_back(path);
82  }
83 
84  currentDir = dirs.begin();
85  files.clear();
87  break;
88  }
89 
91  {
92  setStatusBarMessage("Scanning plugin directories");
93  if(currentDir == dirs.end())
94  {
96  currentFile = files.begin();
97  break;
98  }
99 
100  spdlog::debug("Scanning directory: {}", *currentDir);
101  const char* folder = currentDir->c_str();
102  namespace fs = boost::filesystem;
103  boost::system::error_code ec;
104 
105  if(!fs::is_directory(folder, ec))
106  {
107  spdlog::error("Can't open directory: {}", folder);
108  currentDir++;
109  break;
110  }
111 
112  for(const auto& entry: fs::directory_iterator(folder))
113  {
114  if(fs::is_regular_file(entry) || fs::is_symlink(entry) || fs::is_other(entry))
115  {
116  files.push_back(entry.path().generic_string());
117  }
118  }
119 
120  currentDir++;
121  break;
122  }
123 
124  case LOADING_FILES:
125  {
126  setStatusBarMessage("Loading Plugins");
127  if(currentFile == files.end())
128  {
129  state = DONE;
130  break;
131  }
132 
133 #ifdef _WIN32
134  // Only read .dll files
135  if(currentFile->compare(currentFile->size() - 4, 4, ".dll") == 0)
136  {
137 #else
138  // Only read .so files
139  if(currentFile->compare(currentFile->size() - 3, 3, ".so") == 0)
140  {
141 #endif
142  spdlog::debug("Reading file: {}", *currentFile);
143  GModule* plugin = g_module_open(currentFile->c_str(), static_cast<GModuleFlags>(0));
144  if(plugin)
145  {
146  gpointer pgpi = nullptr;
147  bool symbol_found = false;
148 
149  if(g_module_symbol(plugin, "_Z20getPluginInformationv", &pgpi))
150  {
151  symbol_found = true;
152  }
153  else if(g_module_symbol(plugin, "getPluginInformation", &pgpi))
154  {
155  symbol_found = true;
156  spdlog::warn("Plugin {} uses C-style GetPluginInformation - You need to recompile it", *currentFile);
157  }
158 
159  if(symbol_found)
160  {
161  using PluginFunc = boost::shared_ptr<PluginInformationInterface> (*)();
162 
163  auto gpi = reinterpret_cast<PluginFunc>(pgpi);
164  if(gpi)
165  {
166  PluginInformationInterface::Ptr pi = (*gpi)();
167  if(pi)
168  {
169  if(pi->pluginApiVersion == PLUGIN_API_VERSION)
170  {
171  pluginInformationList.emplace_back(plugin, pi);
172  pi->registerCapabilities(shared_from_this<PluginManager>());
173  plugin = nullptr;
174  gpi = nullptr;
175  pi.reset();
176  }
177  else
178  {
179  spdlog::error("Plugin {} has incorrect API version {}, instead of {}",
180  *currentFile,
181  pi->pluginApiVersion,
183  }
184  }
185  else
186  {
187  spdlog::error("GetPluginInformation returned NULL for file {}", *currentFile);
188  }
189  }
190  else
191  {
192  spdlog::error("Can't find the getPluginInterface function in file {}: {}", *currentFile, g_module_error());
193  }
194  }
195  else
196  {
197  spdlog::warn("Can't lookup symbols in file {}: {}", *currentFile, g_module_error());
198  }
199 
200  if(plugin)
201  {
202  g_module_close(plugin);
203  }
204  }
205  else
206  {
207  spdlog::error("Something went wrong for file {}: {}", *currentFile, g_module_error());
208  }
209  }
210  currentFile++;
211  break;
212  }
213  case DONE:
214  {
215  setStatusBarMessage("Done loading plugins");
216 
217  std::vector<std::pair<std::string, std::string>> pluginInfo;
218  pluginInfo.reserve(pluginInformationList.size());
219  size_t maxPluginNameLength = 0;
220  for(const auto& plugin: pluginInformationList)
221  {
222  pluginInfo.emplace_back(plugin.pluginInformation->getPluginName(), plugin.pluginInformation->getPluginVersion());
223  maxPluginNameLength = std::max(maxPluginNameLength, plugin.pluginInformation->getPluginName().size());
224  }
225  std::sort(pluginInfo.begin(), pluginInfo.end());
226  spdlog::info("Loaded plugins:");
227  for(const auto& [name, version]: pluginInfo)
228  {
229  spdlog::info(" {:{}} : {}", name, maxPluginNameLength, version);
230  }
231 
233  retval = false;
234  break;
235  }
236  }
237  return retval;
238 }
Here is the call graph for this function:

◆ getInstance()

◆ getNewAggregateInterfaces()

const std::map< std::string, NewAggregateInterface::Ptr > & PluginManager::getNewAggregateInterfaces ( )
307 {
308  return newAggregateInterfaces;
309 }

◆ getNewPresentationInterfaces()

const std::map< NewPresentationInterface::Ptr, std::string > & PluginManager::getNewPresentationInterfaces ( )
302 {
304 }

◆ getOpenInterfaces()

const std::map< OpenInterface::Ptr, std::string > & PluginManager::getOpenInterfaces ( )
321 { return openInterfaces; }

◆ getOpenPresentationInterfaces()

const std::map< OpenPresentationInterface::Ptr, std::string > & PluginManager::getOpenPresentationInterfaces ( )
312 {
314 }

◆ getOpenTiledBitmapInterfaces()

const std::map< Scroom::TiledBitmap::OpenTiledBitmapInterface::Ptr, std::string > & PluginManager::getOpenTiledBitmapInterfaces ( )
317 {
319 }

◆ getPresentationObservers()

const std::map< PresentationObserver::Ptr, std::string > & PluginManager::getPresentationObservers ( )
326 {
327  return presentationObservers;
328 }

◆ getViewObservers()

const std::map< ViewObserver::Ptr, std::string > & PluginManager::getViewObservers ( )
323 { return viewObservers; }

◆ registerNewAggregateInterface()

void PluginManager::registerNewAggregateInterface ( const std::string &  identifier,
NewAggregateInterface::Ptr  newAggregateInterface 
)
overridevirtual

Implements ScroomPluginInterface.

266 {
267  newAggregateInterfaces[identifier] = newAggregateInterface;
268 }

◆ registerNewPresentationInterface()

void PluginManager::registerNewPresentationInterface ( const std::string &  identifier,
NewPresentationInterface::Ptr  newPresentationInterface 
)
overridevirtual

Implements ScroomPluginInterface.

259 {
260  newPresentationInterfaces[newPresentationInterface] = identifier;
261 
263 }
Here is the call graph for this function:

◆ registerOpenInterface()

void PluginManager::registerOpenInterface ( const std::string &  extension,
OpenInterface::Ptr  openInterface 
)
overridevirtual

Implements ScroomPluginInterface.

285 {
286  openInterfaces[openInterface] = extension;
287 }

◆ registerOpenPresentationInterface()

void PluginManager::registerOpenPresentationInterface ( const std::string &  extension,
OpenPresentationInterface::Ptr  openPresentationInterface 
)
overridevirtual

Implements ScroomPluginInterface.

272 {
273  openPresentationInterfaces[openPresentationInterface] = extension;
274 }

◆ registerOpenTiledBitmapInterface()

void PluginManager::registerOpenTiledBitmapInterface ( const std::string &  identifier,
boost::shared_ptr< Scroom::TiledBitmap::OpenTiledBitmapInterface openTiledBitmapInterface 
)
overridevirtual

Implements ScroomPluginInterface.

279 {
280  openTiledBitmapInterfaces[openTiledBitmapInterface] = extension;
281  openPresentationInterfaces[ToOpenPresentationInterface(openTiledBitmapInterface)] = extension;
282 }
Here is the call graph for this function:

◆ registerPresentationObserver()

void PluginManager::registerPresentationObserver ( const std::string &  identifier,
PresentationObserver::Ptr  observer 
)
overridevirtual

Implements ScroomPluginInterface.

297 {
298  presentationObservers[observer] = identifier;
299 }

◆ registerViewObserver()

void PluginManager::registerViewObserver ( const std::string &  identifier,
ViewObserver::Ptr  observer 
)
overridevirtual

Implements ScroomPluginInterface.

290 {
291  viewObservers[observer] = identifier;
292 
293  on_new_viewobserver(observer);
294 }
Here is the call graph for this function:

◆ setStatusBarMessage()

void PluginManager::setStatusBarMessage ( const char *  message)
private
241 {
242  // gtk_statusbar_pop(statusbar, status_context_id);
243  // gtk_statusbar_push(statusbar, status_context_id, message);
244 }

Referenced by doWork().

Here is the caller graph for this function:

Member Data Documentation

◆ currentDir

std::list<std::string>::iterator PluginManager::currentDir
private

Referenced by doWork().

◆ currentFile

std::list<std::string>::iterator PluginManager::currentFile
private

Referenced by doWork().

◆ devMode

bool PluginManager::devMode {false}
private

Referenced by addHook(), and doWork().

◆ dirs

std::list<std::string> PluginManager::dirs
private

Referenced by doWork().

◆ files

std::list<std::string> PluginManager::files
private

Referenced by doWork().

◆ newAggregateInterfaces

std::map<std::string, NewAggregateInterface::Ptr> PluginManager::newAggregateInterfaces
private

◆ newPresentationInterfaces

std::map<NewPresentationInterface::Ptr, std::string> PluginManager::newPresentationInterfaces
private

◆ openInterfaces

std::map<OpenInterface::Ptr, std::string> PluginManager::openInterfaces
private

◆ openPresentationInterfaces

std::map<OpenPresentationInterface::Ptr, std::string> PluginManager::openPresentationInterfaces
private

◆ openTiledBitmapInterfaces

std::map<Scroom::TiledBitmap::OpenTiledBitmapInterface::Ptr, std::string> PluginManager::openTiledBitmapInterfaces
private

◆ pluginInformationList

std::list<PluginInformation> PluginManager::pluginInformationList
private

Referenced by doWork().

◆ presentationObservers

std::map<PresentationObserver::Ptr, std::string> PluginManager::presentationObservers
private

◆ state

PluginManagerState PluginManager::state {FINDING_DIRECTORIES}
private

Referenced by addHook(), and doWork().

◆ viewObservers

std::map<ViewObserver::Ptr, std::string> PluginManager::viewObservers
private

The documentation for this class was generated from the following files:
PluginManager::LOADING_FILES
@ LOADING_FILES
Definition: pluginmanager.hh:51
PluginManager::FINDING_DIRECTORIES
@ FINDING_DIRECTORIES
Definition: pluginmanager.hh:49
PLUGIN_API_VERSION
#define PLUGIN_API_VERSION
Definition: plugininformationinterface.hh:15
PluginManager::Ptr
boost::shared_ptr< PluginManager > Ptr
Definition: pluginmanager.hh:44
SCROOM_PLUGIN_DIRS
const std::string SCROOM_PLUGIN_DIRS
Definition: pluginmanager.cc:27
Scroom::TiledBitmap::ToOpenPresentationInterface
OpenPresentationInterface::Ptr ToOpenPresentationInterface(OpenTiledBitmapInterface::Ptr openTiledBitmapInterface)
Definition: tiledbitmappresentation.cc:424
PluginManager::pluginInformationList
std::list< PluginInformation > pluginInformationList
Definition: pluginmanager.hh:62
PluginManager::files
std::list< std::string > files
Definition: pluginmanager.hh:60
on_idle
gboolean on_idle(gpointer user_data)
Definition: callbacks.cc:272
PluginManager::newAggregateInterfaces
std::map< std::string, NewAggregateInterface::Ptr > newAggregateInterfaces
Definition: pluginmanager.hh:64
PluginInformationInterface::Ptr
boost::shared_ptr< PluginInformationInterface > Ptr
Definition: plugininformationinterface.hh:20
on_new_viewobserver
void on_new_viewobserver(const ViewObserver::Ptr &viewObserver)
Definition: callbacks.cc:726
PluginManager::presentationObservers
std::map< PresentationObserver::Ptr, std::string > presentationObservers
Definition: pluginmanager.hh:69
PluginManager::devMode
bool devMode
Definition: pluginmanager.hh:56
PluginManager::openInterfaces
std::map< OpenInterface::Ptr, std::string > openInterfaces
Definition: pluginmanager.hh:67
PluginManager::dirs
std::list< std::string > dirs
Definition: pluginmanager.hh:58
PluginManager::openTiledBitmapInterfaces
std::map< Scroom::TiledBitmap::OpenTiledBitmapInterface::Ptr, std::string > openTiledBitmapInterfaces
Definition: pluginmanager.hh:66
WorkInterface
Definition: workinterface.hh:12
PluginManager::currentFile
std::list< std::string >::iterator currentFile
Definition: pluginmanager.hh:61
on_done_loading_plugins
void on_done_loading_plugins()
Definition: callbacks.cc:282
PluginManager::openPresentationInterfaces
std::map< OpenPresentationInterface::Ptr, std::string > openPresentationInterfaces
Definition: pluginmanager.hh:65
PluginManager::state
PluginManagerState state
Definition: pluginmanager.hh:57
PluginManager::PluginManager
PluginManager()=default
PluginManager::newPresentationInterfaces
std::map< NewPresentationInterface::Ptr, std::string > newPresentationInterfaces
Definition: pluginmanager.hh:63
pluginManager
static PluginManager::Ptr pluginManager
Definition: pluginmanager.cc:29
PluginManager::viewObservers
std::map< ViewObserver::Ptr, std::string > viewObservers
Definition: pluginmanager.hh:68
PluginManager::SCANNING_DIRECTORIES
@ SCANNING_DIRECTORIES
Definition: pluginmanager.hh:50
PluginManager::setStatusBarMessage
void setStatusBarMessage(const char *message)
Definition: pluginmanager.cc:240
PluginManager::DONE
@ DONE
Definition: pluginmanager.hh:52
PluginManager::currentDir
std::list< std::string >::iterator currentDir
Definition: pluginmanager.hh:59
on_newPresentationInterfaces_update
void on_newPresentationInterfaces_update(const std::map< NewPresentationInterface::Ptr, std::string > &newPresentationInterfaces)
Definition: callbacks.cc:615