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 
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  {}
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 &registered[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 
const char * GB_ERROR
Definition: arb_core.h:25
const char * ED4_getSaiColorString(AW_root *awr, int start, int end)
ED4_SearchResults & results() const
Definition: ed4_class.hxx:2002
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
ED4_root * ED4_ROOT
Definition: ED4_main.cxx:48
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:354
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:353
const char * get_search_background(int start, int end) const OVERRIDE
Definition: ED4_plugins.cxx:62
bool has_name(const char *Name) const
Definition: ED4_plugins.cxx:98
~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:1310
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:1427
#define e4_assert(bed)
Definition: ED4_plugins.cxx:21
static const PlugIn * findPlugin(const char *name)
bool visualizeSAI
Definition: ed4_class.hxx:1454
#define TEST_EXPECT_NULL(n)
Definition: test_unit.h:1307
AW_window * ED4_plugin(ED4_plugin_host &)
Definition: ed4_plugins.hxx:40
#define OVERRIDE
Definition: cxxforward.h:93
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:348
#define NULp
Definition: cxxforward.h:97
PlugIn(const char *name_, ED4_plugin *start_plugin_)
Definition: ED4_plugins.cxx:85
void forward_event(AW_event *event) const OVERRIDE
Definition: ED4_plugins.cxx:73
int is_sequence_terminal() const
Definition: ed4_class.hxx:1082
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:90
GBDATA * gb_main
Definition: adname.cxx:33
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