| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : ED4_plugins.cxx // |
|---|
| 4 | // Purpose : implement plugin connector + wrap plugin calls // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include <secedit_extern.hxx> |
|---|
| 12 | #include <rna3d_extern.hxx> |
|---|
| 13 | |
|---|
| 14 | #include <aw_msg.hxx> |
|---|
| 15 | #include <arbdb.h> |
|---|
| 16 | #include <arb_defs.h> |
|---|
| 17 | |
|---|
| 18 | #include "ed4_visualizeSAI.hxx" |
|---|
| 19 | #include "ed4_class.hxx" |
|---|
| 20 | |
|---|
| 21 | #define e4_assert(bed) arb_assert(bed) |
|---|
| 22 | |
|---|
| 23 | static bool has_species_name(ED4_base *base, const char *species_name) { |
|---|
| 24 | if (base->is_sequence_terminal()) { |
|---|
| 25 | ED4_sequence_terminal *seq_term = base->to_sequence_terminal(); |
|---|
| 26 | return species_name && seq_term && seq_term->species_name && strcmp(species_name, seq_term->species_name)==0; |
|---|
| 27 | } |
|---|
| 28 | return false; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | // ----------------- |
|---|
| 32 | // ED4_host |
|---|
| 33 | |
|---|
| 34 | class ED4_host : public ED4_plugin_host, virtual Noncopyable { |
|---|
| 35 | AW_root *aw_root; |
|---|
| 36 | GBDATA *gb_main; |
|---|
| 37 | |
|---|
| 38 | const ED4_sequence_terminal *seq_term; |
|---|
| 39 | mutable ED4_base_position base_pos; |
|---|
| 40 | |
|---|
| 41 | public: |
|---|
| 42 | ED4_host(AW_root *aw_root_, GBDATA *gb_main_) |
|---|
| 43 | : aw_root(aw_root_), |
|---|
| 44 | gb_main(gb_main_), |
|---|
| 45 | seq_term(NULp) |
|---|
| 46 | {} |
|---|
| 47 | ~ED4_host() OVERRIDE {} |
|---|
| 48 | |
|---|
| 49 | AW_root *get_application_root() const OVERRIDE { return aw_root; } |
|---|
| 50 | GBDATA *get_database() const OVERRIDE { return gb_main; } |
|---|
| 51 | |
|---|
| 52 | void announce_current_species(const char *species_name) OVERRIDE { |
|---|
| 53 | ED4_base *base = ED4_ROOT->main_manager->find_first_that(LEV_SEQUENCE_STRING, makeED4_basePredicate(has_species_name, species_name)); |
|---|
| 54 | seq_term = base ? base->to_sequence_terminal() : NULp; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | bool SAIs_visualized() const OVERRIDE { return ED4_ROOT->visualizeSAI; } |
|---|
| 58 | const char* get_SAI_background(int start, int end) const OVERRIDE { |
|---|
| 59 | return ED4_getSaiColorString(aw_root, start, end); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | const char* get_search_background(int start, int end) const OVERRIDE { |
|---|
| 63 | return seq_term |
|---|
| 64 | ? seq_term->results().buildColorString(seq_term, start, end) |
|---|
| 65 | : NULp; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | int get_base_position(int seq_position) const OVERRIDE { |
|---|
| 69 | e4_assert(seq_term); |
|---|
| 70 | return base_pos.get_base_position(seq_term, seq_position); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void forward_event(AW_event *event) const OVERRIDE { ED4_remote_event(event); } |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | // --------------- |
|---|
| 77 | // PlugIn |
|---|
| 78 | |
|---|
| 79 | class PlugIn { |
|---|
| 80 | char *name; |
|---|
| 81 | ED4_plugin *start_plugin; |
|---|
| 82 | mutable AW_window *window; |
|---|
| 83 | |
|---|
| 84 | public: |
|---|
| 85 | PlugIn(const char *name_, ED4_plugin *start_plugin_) |
|---|
| 86 | : name(ARB_strdup(name_)), |
|---|
| 87 | start_plugin(start_plugin_), |
|---|
| 88 | window(NULp) |
|---|
| 89 | {} |
|---|
| 90 | PlugIn(const PlugIn& other) |
|---|
| 91 | : name(ARB_strdup(other.name)), |
|---|
| 92 | start_plugin(other.start_plugin), |
|---|
| 93 | window(other.window) |
|---|
| 94 | {} |
|---|
| 95 | DECLARE_ASSIGNMENT_OPERATOR(PlugIn); |
|---|
| 96 | ~PlugIn() { free(name); } |
|---|
| 97 | |
|---|
| 98 | bool has_name(const char *Name) const { return strcmp(Name, name) == 0; } |
|---|
| 99 | |
|---|
| 100 | GB_ERROR activate(AW_root *aw_root, GBDATA *gb_main) const { |
|---|
| 101 | GB_ERROR error = NULp; |
|---|
| 102 | if (!window) { |
|---|
| 103 | static ED4_plugin_host *host = NULp; |
|---|
| 104 | if (!host) host = new ED4_host(aw_root, gb_main); |
|---|
| 105 | window = start_plugin(*host); |
|---|
| 106 | error = window ? NULp : GB_await_error(); |
|---|
| 107 | if (error) error = GBS_global_string("Couldn't start plugin '%s'\nReason: %s", name, error); |
|---|
| 108 | } |
|---|
| 109 | if (!error) window->activate(); |
|---|
| 110 | return error; |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | static const PlugIn *findPlugin(const char *name) { |
|---|
| 115 | static PlugIn registered[] = { // register plugins here |
|---|
| 116 | PlugIn("SECEDIT", start_SECEDIT_plugin), |
|---|
| 117 | #if defined(ARB_OPENGL) |
|---|
| 118 | PlugIn("RNA3D", start_RNA3D_plugin), |
|---|
| 119 | #endif // ARB_OPENGL |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | for (size_t plug = 0; plug<ARRAY_ELEMS(registered); ++plug) { |
|---|
| 123 | if (registered[plug].has_name(name)) { |
|---|
| 124 | return ®istered[plug]; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | return NULp; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | void ED4_start_plugin(AW_window *aw, GBDATA *gb_main, const char *pluginname) { |
|---|
| 131 | const PlugIn *plugin = findPlugin(pluginname); |
|---|
| 132 | |
|---|
| 133 | GB_ERROR error = plugin |
|---|
| 134 | ? plugin->activate(aw->get_root(), gb_main) |
|---|
| 135 | : GBS_global_string("plugin '%s' is unknown", pluginname); |
|---|
| 136 | |
|---|
| 137 | aw_message_if(error); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | // -------------------------------------------------------------------------------- |
|---|
| 142 | |
|---|
| 143 | #ifdef UNIT_TESTS |
|---|
| 144 | #include <test_unit.h> |
|---|
| 145 | |
|---|
| 146 | void TEST_plugin_found() { |
|---|
| 147 | TEST_REJECT_NULL(findPlugin("SECEDIT")); |
|---|
| 148 | TEST_EXPECT_NULL(findPlugin("unknown")); |
|---|
| 149 | #if defined(ARB_OPENGL) |
|---|
| 150 | TEST_REJECT_NULL(findPlugin("RNA3D")); |
|---|
| 151 | #else |
|---|
| 152 | TEST_EXPECT_NULL(findPlugin("RNA3D")); |
|---|
| 153 | #endif |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | #endif // UNIT_TESTS |
|---|
| 157 | |
|---|