ARB
TreeAdmin.cxx
Go to the documentation of this file.
1 // ============================================================= //
2 // //
3 // File : TreeAdmin.cxx //
4 // Purpose : Common tree admin functionality //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in April 2012 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // ============================================================= //
11 
12 #include "TreeAdmin.h"
13 #include <aw_window.hxx>
14 #include <aw_root.hxx>
15 #include <aw_awar.hxx>
16 #include <arbdbt.h>
17 #include <arb_strbuf.h>
18 #include <cctype>
19 #include <aw_msg.hxx>
20 #include <arb_global_defs.h>
21 #include <awt_TreeAwars.hxx>
22 
23 #define ta_assert(cond) arb_assert(cond)
24 
25 #define AWAR_TREE_SOURCE "tmp/ad_tree/tree_source"
26 #define AWAR_TREE_DEST "tmp/ad_tree/tree_dest"
27 
28 namespace TreeAdmin {
29 
30  void create_awars(AW_root *root, AW_default aw_def, bool registerTreeAwar) {
31  AW_awar *awar_srcTree = root->awar_string(AWAR_TREE_SOURCE, NULp, aw_def)->set_srt(SRT_AUTOCORRECT_TREENAME);
32  if (registerTreeAwar) AWT_registerTreeAwarSimple(awar_srcTree);
33  root->awar_string(AWAR_TREE_DEST, NULp, aw_def)->set_srt(SRT_AUTOCORRECT_TREENAME); // no need to register (awar also used for 'new name' in renames via GUI)
34  }
36  return root->awar(AWAR_TREE_SOURCE);
37  }
39  return root->awar(AWAR_TREE_DEST);
40  }
41 
43  return awr->awar(awar_selected_tree);
44  }
45 
46  void delete_tree_cb(AW_window *aww, const Spec *spec) {
47  AW_awar *awar_tree = spec->tree_awar(aww->get_root());
48  char *name = awar_tree->read_string();
49  GBDATA *gb_main = spec->get_gb_main();
51  GBDATA *gb_tree;
52 
53  // 1. TA: switch to next tree
54  {
55  GB_transaction ta(gb_main);
56  gb_tree = GBT_find_tree(gb_main, name);
57  if (!gb_tree) error = "Please select tree to delete";
58  else {
60  GBDATA *gb_next = GBT_find_next_tree(gb_tree);
61  awar_tree->write_string(gb_next ? GBT_get_tree_name(gb_next) : NO_TREE_SELECTED);
62  }
63  error = ta.close(error);
64  }
65 
66  // 2. TA: delete old tree
67  if (!error) {
68  GB_transaction ta(gb_main);
69  error = GB_delete(gb_tree);
70  error = ta.close(error);
71  }
72 
73  if (error) {
74  aw_message(error);
75  awar_tree->write_string(name); // switch back to failed tree
76  }
77 
78  free(name);
79  }
80 
81  static GB_ERROR tree_append_remark(GBDATA *gb_tree, const char *add_to_remark) {
83  GBDATA *gb_remark = GB_search(gb_tree, "remark", GB_STRING);
84  if (!gb_remark) error = GB_await_error();
85  else {
86  char *old_remark = GB_read_string(gb_remark);
87  if (!old_remark) error = GB_await_error();
88  else {
89  GBS_strstruct *new_remark = GBS_stropen(2000);
90 
91  GBS_strcat(new_remark, old_remark);
92  GBS_chrcat(new_remark, '\n');
93  GBS_strcat(new_remark, add_to_remark);
94 
95  error = GB_write_string(gb_remark, GBS_mempntr(new_remark));
96 
97  GBS_strforget(new_remark);
98  }
99  free(old_remark);
100  }
101  return error;
102  }
103 
104  static void tree_copy_or_rename_cb(AW_window *aww, bool do_copy, const Spec& spec) {
105  AW_root *aw_root = aww->get_root();
106  AW_awar *awar_tree = spec.tree_awar(aw_root);
107  char *source = awar_tree->read_string();
108  char *dest = aw_root->awar(AWAR_TREE_DEST)->read_string();
109  GB_ERROR error = NULp;
110 
111  if (!error && !dest[0]) error = "Please enter new tree name";
112  if (!error) {
113  GBDATA *gb_main = spec.get_gb_main();
114  error = GB_begin_transaction(gb_main);
115  if (!error) {
116  if (do_copy) {
117  error = GBT_copy_tree(gb_main, source, dest);
118  if (!error) {
119  GBDATA *gb_new_tree = GBT_find_tree(gb_main, dest);
120  ta_assert(gb_new_tree);
121  error = tree_append_remark(gb_new_tree, GBS_global_string("[created as copy of '%s']", source));
122  }
123  }
124  else {
125  error = GBT_rename_tree(gb_main, source, dest);
126  if (!error) AWT_announce_tree_renamed(source, dest);
127  }
128  }
129 
130  if (!error) awar_tree->write_string(dest);
131  error = GB_end_transaction(gb_main, error);
132  }
133 
134  aww->hide_or_notify(error);
135 
136  free(dest);
137  free(source);
138  }
139 
140  static void tree_rename_cb(AW_window *aww, const Spec *spec) { tree_copy_or_rename_cb(aww, false, *spec); }
141  static void tree_copy_cb (AW_window *aww, const Spec *spec) { tree_copy_or_rename_cb(aww, true, *spec); }
142 
143  static void current_as_dest_treename_cb(AW_window *aww, const Spec *spec) {
144  AW_root *awr = aww->get_root();
146  }
147 
148  static void make_dest_treename_unique_cb(AW_window *aww, const Spec *spec) {
149  // generated a unique treename
150  AW_root *awr = aww->get_root();
151  AW_awar *awar_dest = awr->awar(AWAR_TREE_DEST);
152 
153  char *name = awar_dest->read_string();
154  int len = strlen(name);
155 
156  for (int p = len-1; p >= 0; --p) {
157  bool auto_modified = isdigit(name[p]) || name[p] == '_';
158  if (!auto_modified) break;
159  name[p] = 0;
160  }
161 
162  {
163  GBDATA *gb_main = spec->get_gb_main();
164  GB_transaction ta(gb_main);
165 
166  if (!GBT_find_tree(gb_main, name)) {
167  awar_dest->write_string("");
168  awar_dest->write_string(name);
169  }
170  else {
171  for (int i = 2; ; i++) {
172  const char *testName = GBS_global_string("%s_%i", name, i);
173  if (!GBT_find_tree(gb_main, testName)) { // found unique name
174  awar_dest->write_string(testName);
175  break;
176  }
177  }
178  }
179  }
180 
181  free(name);
182  }
183 
184  static AW_window *create_copy_or_rename_window(AW_root *root, const char *win_id, const char *win_title, const char *go_label, void (*go_cb)(AW_window*, const Spec*), const Spec *spec) {
185  AW_window_simple *aws = new AW_window_simple;
186  aws->init(root, win_id, win_title);
187 
188  aws->at(10, 10);
189  aws->auto_space(10, 10);
190 
191  aws->at_newline();
192  aws->label("Current:");
193  aws->callback(makeWindowCallback(current_as_dest_treename_cb, spec));
194  aws->at_attach_to(true, false, -10, 25);
195  aws->create_button("use_current", spec->tree_awar(aws->get_root())->awar_name);
196 
197  aws->at_newline();
198  aws->label("New: ");
199  aws->at_attach_to(true, false, -10, 30);
200  aws->create_input_field(AWAR_TREE_DEST);
201 
202  aws->at_newline();
203  aws->callback(makeWindowCallback(go_cb, spec));
204  aws->create_autosize_button("GO", go_label, "");
205 
206  aws->callback(AW_POPDOWN);
207  aws->create_autosize_button("CLOSE", "Abort", "A");
208 
209  aws->callback(makeWindowCallback(make_dest_treename_unique_cb, spec));
210  aws->create_autosize_button("UNIQUE", "Unique name", "U");
211 
212  aws->at_newline();
213 
214  return aws;
215  }
216 
217 
219  return create_copy_or_rename_window(root, "RENAME_TREE", "Rename Tree", "Rename Tree", tree_rename_cb, spec);
220  }
221  AW_window *create_copy_window(AW_root *root, const Spec *spec) {
222  return create_copy_or_rename_window(root, "COPY_TREE", "Copy Tree", "Copy Tree", tree_copy_cb, spec);
223  }
224 
225 
226 };
227 
GB_ERROR GB_begin_transaction(GBDATA *gbd)
Definition: arbdb.cxx:2492
const char * GB_ERROR
Definition: arb_core.h:25
AW_awar * set_srt(const char *srt)
Definition: AW_awar.cxx:569
AW_window * create_rename_window(AW_root *root, const Spec *spec)
Definition: TreeAdmin.cxx:218
GB_ERROR GB_write_string(GBDATA *gbd, const char *s)
Definition: arbdb.cxx:1361
#define ta_assert(cond)
Definition: TreeAdmin.cxx:23
void AWT_registerTreeAwarSimple(AW_awar *awar)
GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error)
Definition: arbdb.cxx:2525
void at(int x, int y)
Definition: AW_at.cxx:93
void AWT_announce_tree_renamed(const char *oldname, const char *newname)
#define NO_TREE_SELECTED
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
#define SRT_AUTOCORRECT_TREENAME
Definition: arbdbt.h:79
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
static void tree_copy_cb(AW_window *aww, const Spec *spec)
Definition: TreeAdmin.cxx:141
AW_awar * tree_awar(AW_root *awr) const
Definition: TreeAdmin.cxx:42
GBDATA * get_gb_main() const
Definition: TreeAdmin.h:32
GB_ERROR GB_delete(GBDATA *&source)
Definition: arbdb.cxx:1880
const char * GBT_get_tree_name(GBDATA *gb_tree)
Definition: adtree.cxx:1033
const char * read_char_pntr() const
Definition: AW_awar.cxx:171
GBS_strstruct * GBS_stropen(long init_size)
Definition: arb_strbuf.cxx:39
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
static GB_ERROR tree_append_remark(GBDATA *gb_tree, const char *add_to_remark)
Definition: TreeAdmin.cxx:81
GBDATA * GBT_find_next_tree(GBDATA *gb_tree)
Definition: adtree.cxx:1017
AW_awar * dest_tree_awar(AW_root *root)
Definition: TreeAdmin.cxx:38
void create_awars(AW_root *root, AW_default aw_def, bool registerTreeAwar)
Definition: TreeAdmin.cxx:30
void delete_tree_cb(AW_window *aww, const Spec *spec)
Definition: TreeAdmin.cxx:46
void AWT_announce_tree_deleted(const char *name)
static void current_as_dest_treename_cb(AW_window *aww, const Spec *spec)
Definition: TreeAdmin.cxx:143
void GBS_strcat(GBS_strstruct *strstr, const char *ptr)
Definition: arb_strbuf.cxx:108
static void error(const char *msg)
Definition: mkptypes.cxx:96
GB_ERROR GBT_copy_tree(GBDATA *gb_main, const char *source_name, const char *dest_name)
Definition: adtree.cxx:1251
GB_ERROR GBT_rename_tree(GBDATA *gb_main, const char *source_name, const char *dest_name)
Definition: adtree.cxx:1269
char * read_string() const
Definition: AW_awar.cxx:201
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
void GBS_strforget(GBS_strstruct *strstr)
Definition: arb_strbuf.cxx:76
static AW_window * create_copy_or_rename_window(AW_root *root, const char *win_id, const char *win_title, const char *go_label, void(*go_cb)(AW_window *, const Spec *), const Spec *spec)
Definition: TreeAdmin.cxx:184
void GBS_chrcat(GBS_strstruct *strstr, char ch)
Definition: arb_strbuf.cxx:119
GB_ERROR close(GB_ERROR error)
Definition: arbdbpp.cxx:32
char * GBS_mempntr(GBS_strstruct *strstr)
Definition: arb_strbuf.cxx:86
AW_awar * source_tree_awar(AW_root *root)
Definition: TreeAdmin.cxx:35
AW_window * create_copy_window(AW_root *root, const Spec *spec)
Definition: TreeAdmin.cxx:221
char * GB_read_string(GBDATA *gbd)
Definition: arbdb.cxx:879
#define AWAR_TREE_DEST
Definition: TreeAdmin.cxx:26
void aw_message(const char *msg)
Definition: AW_status.cxx:932
AW_root * get_root()
Definition: aw_window.hxx:348
#define NULp
Definition: cxxforward.h:97
static void tree_rename_cb(AW_window *aww, const Spec *spec)
Definition: TreeAdmin.cxx:140
static void make_dest_treename_unique_cb(AW_window *aww, const Spec *spec)
Definition: TreeAdmin.cxx:148
GB_ERROR write_string(const char *aw_string)
GBDATA * GBT_find_tree(GBDATA *gb_main, const char *tree_name)
Definition: adtree.cxx:947
GB_transaction ta(gb_var)
static void tree_copy_or_rename_cb(AW_window *aww, bool do_copy, const Spec &spec)
Definition: TreeAdmin.cxx:104
GBDATA * gb_main
Definition: adname.cxx:33
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
GBDATA * GB_search(GBDATA *gbd, const char *fieldpath, GB_TYPES create)
Definition: adquery.cxx:531
void hide_or_notify(const char *error)
Definition: AW_window.cxx:1817
#define AWAR_TREE_SOURCE
Definition: TreeAdmin.cxx:25