You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.1 KiB

#include "api.h"
#include "logging.h"
#include "singletons.h"
Menu* PluginApi::menu_=nullptr;
Notebook* PluginApi::notebook=nullptr;
11 years ago
/////////////////////////////
//// API ServiceProvider ////
/////////////////////////////
PluginApi::PluginApi(Notebook* notebook) {
DEBUG("Adding pointers for the API");
this->notebook = notebook;
menu_ = Singleton::menu();
DEBUG("Initiating plugins(from plugins.py)..");
#ifndef __APPLE__
InitPlugins(); //TODO: fix this
#endif
DEBUG("Plugins initiated..");
}
11 years ago
std::string PluginApi::ProjectPath() {
int MAXPATHLEN = 50;
char temp[MAXPATHLEN];
return ( getcwd(temp, MAXPATHLEN) ? std::string( temp ) : std::string("") );
}
11 years ago
void PluginApi::ReplaceWord(std::string word) {
Glib::RefPtr<Gtk::TextBuffer> buffer = libjuci::BufferFromNotebook();
Gtk::TextIter word_start = libjuci::IterFromNotebook();
Gtk::TextIter word_end = libjuci::IterFromNotebook();
libjuci::IterToWordStart(word_start);
libjuci::IterToWordEnd(word_end);
11 years ago
if (word_start != word_end) {
buffer->erase(word_start, word_end);
Gtk::TextIter current = libjuci::IterFromNotebook();
buffer->insert(current, word);
}
}
11 years ago
void PluginApi::ReplaceLine(std::string line) {
WARNING("use of unimplemented method");
}
11 years ago
std::string PluginApi::GetWord() {
11 years ago
Glib::RefPtr<Gtk::TextBuffer> buffer = libjuci::BufferFromNotebook();
Gtk::TextIter word_start = libjuci::IterFromNotebook();
Gtk::TextIter word_end = libjuci::IterFromNotebook();
libjuci::IterToWordStart(word_start);
libjuci::IterToWordEnd(word_end);
11 years ago
if (word_start < word_end) {
std::string word = buffer->get_text(word_start, word_end);
return word;
}
return "";
11 years ago
}
11 years ago
void PluginApi::InitPlugins() {
libjuci::LoadPlugin(ProjectPath()+"/plugins.py");
11 years ago
}
11 years ago
void PluginApi::AddMenuElement(std::string plugin_name) {
DEBUG("Adding menu element for "+plugin_name);
11 years ago
AddMenuXml(plugin_name, "PluginMenu");
std::string plugin_action_name = plugin_name+"Menu";
Singleton::menu()->action_group->add(Gtk::Action::create(plugin_action_name, plugin_name));
}
11 years ago
void PluginApi::AddSubMenuElement(std::string parent_menu,
11 years ago
std::string menu_name,
std::string menu_func_name,
std::string plugin_path,
std::string menu_keybinding) {
11 years ago
AddSubMenuXml(menu_func_name, parent_menu);
Singleton::menu()->action_group->add(Gtk::Action::create(menu_func_name,
11 years ago
menu_name),
Gtk::AccelKey(menu_keybinding),
[=]() {
libjuci::LoadPluginFunction(menu_func_name, plugin_path);
});
}
11 years ago
void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) {
std::string temp_menu = Singleton::menu()->ui;
std::size_t plugin_menu_pos = temp_menu.find(parent_menu);
// +2 gets you outside of the tag:<'menu_name'>
11 years ago
plugin_menu_pos+=parent_menu.size() +2;
std::string menu_prefix = temp_menu.substr(0, plugin_menu_pos);
std::string menu_suffix = temp_menu.substr(plugin_menu_pos);
std::string menu_input =
" <menu action='"+plugin_name+"Menu'> "
11 years ago
" </menu> ";
Singleton::menu()->ui = menu_prefix + menu_input + menu_suffix;
}
11 years ago
void PluginApi::AddSubMenuXml(std::string plugin_name,
std::string parent_menu) {
std::string temp_menu = Singleton::menu()->ui;
std::size_t parent_menu_pos = temp_menu.find(parent_menu);
// +2 gets you outside of the tag:<'menu_name'>
11 years ago
parent_menu_pos+=parent_menu.size() +2;
std::string menu_prefix = temp_menu.substr(0, parent_menu_pos);
std::string menu_suffix = temp_menu.substr(parent_menu_pos);
11 years ago
11 years ago
std::string menu_input ="<menuitem action='"+plugin_name+"'/>";
Singleton::menu()->ui = menu_prefix + menu_input + menu_suffix;
11 years ago
}
///////////////////////
//// Api to python ////
///////////////////////
void libjuci::ReplaceWord(const std::string word) {
PluginApi::ReplaceWord(word);
}
11 years ago
void libjuci::ReplaceLine(const std::string line) {
11 years ago
std::cout << "unimplemented: " << __func__ << " called"
11 years ago
<< std::endl;
}
11 years ago
std::string libjuci::GetWord() {
return PluginApi::GetWord();
}
11 years ago
void libjuci::AddMenuElement(std::string plugin_name) {
11 years ago
PluginApi::AddMenuElement(plugin_name);
}
void libjuci::AddSubMenuElement(std::string parent_menu,
11 years ago
std::string menu_name,
std::string menu_func_name,
std::string plugin_path,
std::string menu_keybinding) {
11 years ago
PluginApi::AddSubMenuElement(parent_menu,
11 years ago
menu_name,
menu_func_name,
plugin_path,
menu_keybinding);
11 years ago
}
11 years ago
//////////////////////////////
//// Boost.Python methods ////
//////////////////////////////
11 years ago
namespace bp = boost::python;
11 years ago
11 years ago
bp::api::object libjuci::OpenPythonScript(const std::string path,
11 years ago
bp::api::object python_name_space) {
11 years ago
bp::str str_path(path);
return bp::exec_file(str_path, python_name_space);
11 years ago
}
void libjuci::LoadPlugin(const std::string& plugin_name) {
11 years ago
try {
Py_Initialize();
11 years ago
bp::api::object main_module = bp::import("__main__");
bp::api::object main_namespace =
main_module.attr("__dict__");
11 years ago
bp::api::object ignored2 =
OpenPythonScript(plugin_name, main_namespace);
11 years ago
}catch (bp::error_already_set const&) {
PyErr_Print();
}
}
void libjuci::LoadPluginFunction(const std::string &function_name,
11 years ago
const std::string &plugin_path) {
try {
Py_Initialize();
11 years ago
bp::api::object main_module = bp::import("__main__");
bp::api::object main_namespace = main_module.attr("__dict__");
bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace);
bp::str func_name(function_name);
bp::api::object returnValue = bp::eval(func_name, main_namespace);
11 years ago
}catch (bp::error_already_set const&) {
PyErr_Print();
}
}
void libjuci::InitPlugin(const std::string& plugin_path) {
11 years ago
try {
11 years ago
Py_Initialize();
11 years ago
bp::api::object main_module = bp::import("__main__");
bp::api::object main_namespace = main_module.attr("__dict__");
bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace);
11 years ago
bp::object returnValue = bp::eval("initPlugin()", main_namespace);
// do something with return_value
}catch (bp::error_already_set const&) {
11 years ago
PyErr_Print();
}
}
11 years ago
///////////////////////
//// Glib wrappers ////
///////////////////////
void libjuci::IterToWordStart(Gtk::TextIter &iter) {
11 years ago
if (!iter.starts_line()) {
while (!iter.starts_word()) {
iter.backward_char();
}
}
}
void libjuci::IterToWordEnd(Gtk::TextIter &iter) {
11 years ago
if (!iter.ends_line()) {
while (!iter.ends_word()) {
iter.forward_char();
}
}
}
Glib::RefPtr<Gtk::TextBuffer> libjuci::BufferFromNotebook() {
return Glib::RefPtr<Gtk::TextBuffer>(PluginApi::notebook
->CurrentSourceView()->get_buffer());
}
Gtk::TextIter libjuci::IterFromNotebook() {
return libjuci::BufferFromNotebook()->get_insert()->get_iter();
}