/* $Id$ */ /* * Cantus Tag Editor * Copyright © 2002-2004 by Samuel Abels * Copyright © 2007 by Tim Huetz * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see **/ #ifdef HAVE_CONFIG_H # include #endif #include using namespace std; //#define _DEBUG_ /****************************************************************************** * Constructor/Destructor ******************************************************************************/ /* Constructor. */ PluginHandler::PluginHandler(void) { } /* Destructor. */ PluginHandler::~PluginHandler(void) { #ifdef _DEBUG_ cout << "PluginHandler destructor: Called.\n"; #endif std::map::iterator iter; for (iter = plugins_str.begin(); iter != plugins_str.end(); iter++) { CantusPlugin *plugin = iter->second; plugin->unref(); } } /****************************************************************************** * Public ******************************************************************************/ /* Register a new plugin. * Returns 0 on success, an errorcode < 0 otherwise. */ gint PluginHandler::reg(const gchar *filename, gint priority) { // Allocate space for the plugin. CantusPlugin *plugin = new CantusPlugin(); if (plugin->loadPlugin(filename) < 0) { plugin->unref(); return -1; } // Register the plugin and emit a signal. plugin->setPriority(priority); plugins_str[filename] = plugin; plugins_int[priority] = plugin; #ifdef _DEBUG_ cout << "Successfully registered plugin '" << plugin->getName() << "'\n" << " from '" << filename << "'\n"; #endif plugin->signalPluginDeleted.connect(signal_plugin_deleted); signal_plugin_loaded.emit(plugin); return 0; } /* Unregister a plugin. */ void PluginHandler::unreg(const gchar *filename) { CantusPlugin *plugin = (*plugins_str.find(filename)).second; if (!plugin) { cerr << "InputPlugin::unreg(): No such plugin (" << filename << ").\n"; return; } // Unregister the plugin and emit a signal. plugins_str.erase(filename); plugins_int.erase(plugin->getPriority()); signal_plugin_removed.emit(plugin); plugin->unref(); #ifdef _DEBUG_ cout << "Successfully unregistered plugin '" << plugin->getName() << "'\n" << " from '" << filename << "'.\n"; #endif } /* Return a std::list of all plugins responsible for the given file type. * Don't forget to delete the list AND UNREF EVERY PLUGIN!! */ std::list *PluginHandler::get_responsible_plugins( const gchar *filename) { std::map::iterator iter; std::list *pluginlist = new std::list; for (iter = plugins_int.begin(); iter != plugins_int.end(); iter++) { if (!iter->second->isHandling(filename)) continue; iter->second->ref(); pluginlist->push_back(iter->second); } return pluginlist; } /* Given a GList of filenames, this function returns a GList of all responsible * plugins for the given filetypes. * Don't forget to delete the list AND UNREF EVERY PLUGIN!! */ std::list *PluginHandler::get_responsible_plugins_from_list( GList *filenames) { std::map::iterator iter; std::map resp_hash; std::list *responsible = new std::list; // Grab a hash_map of all responsible plugins. while (filenames) { const gchar *filename = (const gchar*)filenames->data; for (iter = plugins_int.begin(); iter != plugins_int.end(); iter++) { if (resp_hash[iter->first]) // Known to be responsible? continue; if (!iter->second->isHandling(filename)) // Responsible? continue; #ifdef _DEBUG_ std::cout << "PluginHandler::get_responsible_plugins_from_list(): " << "Found: ", iter->second->getName(), ".\n"; #endif iter->second->ref(); #ifdef _DEBUG_ printf("PluginHandler::get_responsible_plugins_from_list(): Ref'd.\n"); #endif resp_hash[iter->first] = iter->second; responsible->push_back(iter->second); } filenames = filenames->next; } #ifdef _DEBUG_ printf("PluginHandler::get_responsible_plugins_from_list(): Finished.\n"); #endif return(responsible); } /* This function returns a list of all plugins. Don't forget to delete the list * AND UNREF EVERY PLUGIN!! */ std::list *PluginHandler::get_plugins(void) { #ifdef _DEBUG_ cout << "PluginHandler::get_plugins(): Called.\n"; #endif std::map::iterator iter; std::list *pluginlist = new std::list; for (iter = plugins_int.begin(); iter != plugins_int.end(); iter++) { iter->second->ref(); pluginlist->push_back(iter->second); } return pluginlist; } /****************************************************************************** * Protected ******************************************************************************/