ARB
ED4_plugins.cxx
Go to the documentation of this file.
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  {}
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  const AW_helix *get_helix() const OVERRIDE {
76  return ED4_ROOT->helix;
77  }
78 };
79 
80 // ---------------
81 // PlugIn
82 
83 class PlugIn {
84  char *name;
85  ED4_plugin *start_plugin;
86  mutable AW_window *window;
87 
88 public:
89  PlugIn(const char *name_, ED4_plugin *start_plugin_)
90  : name(ARB_strdup(name_)),
91  start_plugin(start_plugin_),
92  window(NULp)
93  {}
94  PlugIn(const PlugIn& other)
95  : name(ARB_strdup(other.name)),
96  start_plugin(other.start_plugin),
97  window(other.window)
98  {}
100  ~PlugIn() { free(name); }
101 
102  bool has_name(const char *Name) const { return strcmp(Name, name) == 0; }
103 
104  GB_ERROR activate(AW_root *aw_root, GBDATA *gb_main) const {
105  GB_ERROR error = NULp;
106  if (!window) {
107  static ED4_plugin_host *host = NULp;
108  if (!host) host = new ED4_host(aw_root, gb_main);
109  window = start_plugin(*host);
110  error = window ? NULp : GB_await_error();
111  if (error) error = GBS_global_string("Couldn't start plugin '%s'\nReason: %s", name, error);
112  }
113  if (!error) window->activate();
114  return error;
115  }
116 };
117 
118 static const PlugIn *findPlugin(const char *name) {
119  static PlugIn registered[] = { // register plugins here
120  PlugIn("SECEDIT", start_SECEDIT_plugin),
121 #if defined(ARB_OPENGL)
122  PlugIn("RNA3D", start_RNA3D_plugin),
123 #endif // ARB_OPENGL
124  };
125 
126  for (size_t plug = 0; plug<ARRAY_ELEMS(registered); ++plug) {
127  if (registered[plug].has_name(name)) {
128  return &registered[plug];
129  }
130  }
131  return NULp;
132 }
133 
134 void ED4_start_plugin(AW_window *aw, GBDATA *gb_main, const char *pluginname) {
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 
const char * GB_ERROR
Definition: arb_core.h:25
AW_helix * helix
Definition: ed4_class.hxx:1445
const char * ED4_getSaiColorString(AW_root *awr, int start, int end)
ED4_SearchResults & results() const
Definition: ed4_class.hxx:2003
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
ED4_root * ED4_ROOT
Definition: ED4_main.cxx:49
ED4_plugin start_SECEDIT_plugin
ED4_plugin start_RNA3D_plugin
#define ARRAY_ELEMS(array)
Definition: arb_defs.h:19
void activate()
Definition: aw_window.hxx:365
static Shaders registered
AW_root * get_application_root() const OVERRIDE
Definition: ED4_plugins.cxx:49
static HelixNrInfo * start
int get_base_position(int seq_position) const OVERRIDE
Definition: ED4_plugins.cxx:68
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
const char * get_search_background(int start, int end) const OVERRIDE
Definition: ED4_plugins.cxx:62
bool has_name(const char *Name) const
~ED4_host() OVERRIDE
Definition: ED4_plugins.cxx:47
void ED4_remote_event(AW_event *faked_event)
#define TEST_REJECT_NULL(n)
Definition: test_unit.h:1325
const AW_helix * get_helix() const OVERRIDE
Definition: ED4_plugins.cxx:75
static void error(const char *msg)
Definition: mkptypes.cxx:96
ED4_host(AW_root *aw_root_, GBDATA *gb_main_)
Definition: ED4_plugins.cxx:42
char * buildColorString(const ED4_sequence_terminal *seq_terminal, int start, int end)
void announce_current_species(const char *species_name) OVERRIDE
Definition: ED4_plugins.cxx:52
ED4_main_manager * main_manager
Definition: ed4_class.hxx:1429
#define e4_assert(bed)
Definition: ED4_plugins.cxx:21
static const PlugIn * findPlugin(const char *name)
bool visualizeSAI
Definition: ed4_class.hxx:1455
#define TEST_EXPECT_NULL(n)
Definition: test_unit.h:1322
AW_window * ED4_plugin(ED4_plugin_host &)
Definition: ed4_plugins.hxx:43
#define OVERRIDE
Definition: cxxforward.h:112
GB_ERROR activate(AW_root *aw_root, GBDATA *gb_main) const
bool SAIs_visualized() const OVERRIDE
Definition: ED4_plugins.cxx:57
DECLARE_ASSIGNMENT_OPERATOR(PlugIn)
AW_root * get_root()
Definition: aw_window.hxx:359
#define NULp
Definition: cxxforward.h:116
PlugIn(const char *name_, ED4_plugin *start_plugin_)
Definition: ED4_plugins.cxx:89
void forward_event(AW_event *event) const OVERRIDE
Definition: ED4_plugins.cxx:73
int is_sequence_terminal() const
Definition: ed4_class.hxx:1083
int get_base_position(const ED4_terminal *base, int sequence_position)
void ED4_start_plugin(AW_window *aw, GBDATA *gb_main, const char *pluginname)
PlugIn(const PlugIn &other)
Definition: ED4_plugins.cxx:94
GBDATA * gb_main
Definition: adname.cxx:32
GBDATA * get_database() const OVERRIDE
Definition: ED4_plugins.cxx:50
static bool has_species_name(ED4_base *base, const char *species_name)
Definition: ED4_plugins.cxx:23
void aw_message_if(GB_ERROR error)
Definition: aw_msg.hxx:21
const char * get_SAI_background(int start, int end) const OVERRIDE
Definition: ED4_plugins.cxx:58