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, AW_CL cl_species_name) { |
---|
24 | if (base->is_sequence_terminal()) { |
---|
25 | ED4_sequence_terminal *seq_term = base->to_sequence_terminal(); |
---|
26 | const char *species_name = (const char *)cl_species_name; |
---|
27 | return species_name && seq_term && seq_term->species_name && strcmp(species_name, seq_term->species_name)==0; |
---|
28 | } |
---|
29 | return false; |
---|
30 | } |
---|
31 | |
---|
32 | // ----------------- |
---|
33 | // ED4_host |
---|
34 | |
---|
35 | class ED4_host : public ED4_plugin_host, virtual Noncopyable { |
---|
36 | AW_root *aw_root; |
---|
37 | GBDATA *gb_main; |
---|
38 | |
---|
39 | const ED4_sequence_terminal *seq_term; |
---|
40 | mutable ED4_base_position base_pos; |
---|
41 | |
---|
42 | public: |
---|
43 | ED4_host(AW_root *aw_root_, GBDATA *gb_main_) |
---|
44 | : aw_root(aw_root_), |
---|
45 | gb_main(gb_main_), |
---|
46 | seq_term(NULL) |
---|
47 | {} |
---|
48 | virtual ~ED4_host() OVERRIDE {} |
---|
49 | |
---|
50 | AW_root *get_application_root() const OVERRIDE { return aw_root; } |
---|
51 | GBDATA *get_database() const OVERRIDE { return gb_main; } |
---|
52 | |
---|
53 | void announce_current_species(const char *species_name) OVERRIDE { |
---|
54 | ED4_base *base = ED4_ROOT->main_manager->find_first_that(ED4_L_SEQUENCE_STRING, has_species_name, (AW_CL)species_name); |
---|
55 | seq_term = base ? base->to_sequence_terminal() : NULL; |
---|
56 | } |
---|
57 | |
---|
58 | bool SAIs_visualized() const OVERRIDE { return ED4_ROOT->visualizeSAI; } |
---|
59 | const char* get_SAI_background(int start, int end) const OVERRIDE { |
---|
60 | return ED4_getSaiColorString(aw_root, start, end); |
---|
61 | } |
---|
62 | |
---|
63 | const char* get_search_background(int start, int end) const OVERRIDE { |
---|
64 | return seq_term |
---|
65 | ? seq_term->results().buildColorString(seq_term, start, end) |
---|
66 | : 0; |
---|
67 | } |
---|
68 | |
---|
69 | int get_base_position(int seq_position) const OVERRIDE { |
---|
70 | e4_assert(seq_term); |
---|
71 | return base_pos.get_base_position(seq_term, seq_position); |
---|
72 | } |
---|
73 | |
---|
74 | void forward_event(AW_event *event) const OVERRIDE { ED4_remote_event(event); } |
---|
75 | }; |
---|
76 | |
---|
77 | // --------------- |
---|
78 | // PlugIn |
---|
79 | |
---|
80 | class PlugIn { |
---|
81 | char *name; |
---|
82 | ED4_plugin *start_plugin; |
---|
83 | mutable AW_window *window; |
---|
84 | |
---|
85 | public: |
---|
86 | PlugIn(const char *name_, ED4_plugin *start_plugin_) |
---|
87 | : name(strdup(name_)), |
---|
88 | start_plugin(start_plugin_), |
---|
89 | window(NULL) |
---|
90 | {} |
---|
91 | PlugIn(const PlugIn& other) |
---|
92 | : name(strdup(other.name)), |
---|
93 | start_plugin(other.start_plugin), |
---|
94 | window(other.window) |
---|
95 | {} |
---|
96 | DECLARE_ASSIGNMENT_OPERATOR(PlugIn); |
---|
97 | ~PlugIn() { free(name); } |
---|
98 | |
---|
99 | bool has_name(const char *Name) const { return strcmp(Name, name) == 0; } |
---|
100 | |
---|
101 | GB_ERROR activate(AW_root *aw_root, GBDATA *gb_main) const { |
---|
102 | GB_ERROR error = NULL; |
---|
103 | if (!window) { |
---|
104 | static ED4_plugin_host *host = 0; |
---|
105 | if (!host) host = new ED4_host(aw_root, gb_main); |
---|
106 | window = start_plugin(*host); |
---|
107 | error = window ? NULL : GB_await_error(); |
---|
108 | if (error) error = GBS_global_string("Couldn't start plugin '%s'\nReason: %s", name, error); |
---|
109 | } |
---|
110 | if (!error) window->activate(); |
---|
111 | return error; |
---|
112 | } |
---|
113 | }; |
---|
114 | |
---|
115 | static const PlugIn *findPlugin(const char *name) { |
---|
116 | static PlugIn registered[] = { // register plugins here |
---|
117 | PlugIn("SECEDIT", start_SECEDIT_plugin), |
---|
118 | #if defined(ARB_OPENGL) |
---|
119 | PlugIn("RNA3D", start_RNA3D_plugin), |
---|
120 | #endif // ARB_OPENGL |
---|
121 | }; |
---|
122 | |
---|
123 | for (size_t plug = 0; plug<ARRAY_ELEMS(registered); ++plug) { |
---|
124 | if (registered[plug].has_name(name)) { |
---|
125 | return ®istered[plug]; |
---|
126 | } |
---|
127 | } |
---|
128 | return NULL; |
---|
129 | } |
---|
130 | |
---|
131 | void ED4_start_plugin(AW_window *aw, AW_CL cl_gbmain, AW_CL cl_pluginname) { |
---|
132 | const char *pluginname = (const char *)cl_pluginname; |
---|
133 | GBDATA *gb_main = (GBDATA*)cl_gbmain; |
---|
134 | |
---|
135 | const PlugIn *plugin = findPlugin(pluginname); |
---|
136 | |
---|
137 | GB_ERROR error = plugin |
---|
138 | ? plugin->activate(aw->get_root(), gb_main) |
---|
139 | : GBS_global_string("plugin '%s' is unknown", pluginname); |
---|
140 | |
---|
141 | aw_message_if(error); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | // -------------------------------------------------------------------------------- |
---|
146 | |
---|
147 | #ifdef UNIT_TESTS |
---|
148 | #include <test_unit.h> |
---|
149 | |
---|
150 | void TEST_plugin_found() { |
---|
151 | TEST_REJECT_NULL(findPlugin("SECEDIT")); |
---|
152 | TEST_EXPECT_NULL(findPlugin("unknown")); |
---|
153 | #if defined(ARB_OPENGL) |
---|
154 | TEST_REJECT_NULL(findPlugin("RNA3D")); |
---|
155 | #else |
---|
156 | TEST_EXPECT_NULL(findPlugin("RNA3D")); |
---|
157 | #endif |
---|
158 | } |
---|
159 | |
---|
160 | #endif // UNIT_TESTS |
---|
161 | |
---|