source: tags/svn.1.5.4/MERGE/MG_names.cxx

Last change on this file was 7186, checked in by westram, 14 years ago

merges [7097] [7098] [7100] [7101] [7102] [7103] [7105] from refactor

  • use arb_progress instead of aw_openstatus, aw_closestatus and aw_status
    • allows nested progress bars
    • avoids repeatedly popping up progress bars
    • avoids passing down info about progress to callees or bookkeeping it in method-objects and similar
    • removed many useless status messages (e.g. those that were only visible for ms)
    • corrected incrementation / limit for many progress bars (esp. matrix-calculations, parsimony)
  • hide aw_open/close/status inside libAW
  • hide rests of GB_status inside libCORE
  • replaced ARB_null_status by arb_suppress_progress
    • suppress progress display in unit-tests
  • added GBT_count_alignments
  • stuffed a few memleaks on the way
  • disabled some deprecation warnings
  • use arb_progress in ptserver
  • avoid zero progress in GDE export
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : MG_names.cxx                                      //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "merge.hxx"
12
13#include <AW_rename.hxx>
14#include <aw_root.hxx>
15#include <aw_awar.hxx>
16#include <aw_msg.hxx>
17#include <arb_progress.h>
18
19// --------------------------------------------------------------------------------
20
21#define AWAR_MERGE_ADDID "tmp/merge1/addid"
22#define AWAR_DEST_ADDID  "tmp/merge2/addid"
23
24#define AWAR_ADDID_MATCH   "tmp/merge/addidmatch"
25#define AWAR_RENAME_STATUS "tmp/merge/renamestat"
26#define AWAR_ALLOW_DUPS    "tmp/merge/allowdups"
27#define AWAR_OVERRIDE      "tmp/merge/override"
28
29// --------------------------------------------------------------------------------
30
31void MG_create_rename_awars(AW_root *aw_root, AW_default aw_def) {
32    aw_root->awar_string(AWAR_ADDID_MATCH, "", aw_def);
33    aw_root->awar_string(AWAR_RENAME_STATUS, "", aw_def);
34    aw_root->awar_int(AWAR_ALLOW_DUPS, 0, aw_def);
35    aw_root->awar_int(AWAR_OVERRIDE, 0, aw_def);
36}
37
38// --------------------------------------------------------------------------------
39
40static const char *addids_match_info(AW_root *aw_root) {
41    char       *addid1 = aw_root->awar(AWAR_MERGE_ADDID)->read_string();
42    char       *addid2 = aw_root->awar(AWAR_DEST_ADDID)->read_string();
43    const char *result = (strcmp(addid1, addid2) == 0) ? "Ok" : "MISMATCH!";
44
45    free(addid2);
46    free(addid1);
47
48    return result;
49}
50
51static void addids_match_info_refresh_cb(AW_root *aw_root) {
52    aw_root->awar(AWAR_ADDID_MATCH)->write_string(addids_match_info(aw_root));
53    MG_set_renamed(false, aw_root, "Needed (add.field changed)");
54}
55
56void MG_create_db_dependent_rename_awars(AW_root *aw_root, GBDATA *gb_merge, GBDATA *gb_dest) {
57    static bool created = false;
58
59    if (!created) {
60        GB_transaction t1(gb_merge);
61        GB_transaction t2(gb_dest);
62        GB_ERROR       error = 0;
63
64        // Awars for additional ID need to be mapped, cause they use same db-path in both DBs
65
66        GBDATA     *gb_addid1 = GB_search(gb_merge, AWAR_NAMESERVER_ADDID, GB_STRING);
67        GBDATA     *gb_addid2 = GB_search(gb_dest, AWAR_NAMESERVER_ADDID, GB_STRING);
68        const char *addid1    = gb_addid1 ? GB_read_char_pntr(gb_addid1) : "";
69        const char *addid2    = gb_addid2 ? GB_read_char_pntr(gb_addid2) : "";
70
71        // use other as default (needed e.g. for import)
72        if (gb_addid1 && !gb_addid2) {
73            gb_addid2             = GB_create(gb_dest, AWAR_NAMESERVER_ADDID, GB_STRING);
74            if (!gb_addid2) error = GB_await_error();
75            else error            = GB_write_string(gb_addid2, addid1);
76        }
77        else if (!gb_addid1 && gb_addid2) {
78            gb_addid1             = GB_create(gb_merge, AWAR_NAMESERVER_ADDID, GB_STRING);
79            if (!gb_addid1) error = GB_await_error();
80            else error            = GB_write_string(gb_addid1, addid2);
81        }
82
83        if (!error) {
84            AW_awar *awar_addid1 = aw_root->awar_string(AWAR_MERGE_ADDID, "xxx", gb_merge);
85            AW_awar *awar_addid2 = aw_root->awar_string(AWAR_DEST_ADDID, "xxx", gb_dest);
86
87            awar_addid1->unmap(); awar_addid1->map(gb_addid1);
88            awar_addid2->unmap(); awar_addid2->map(gb_addid2);
89
90            awar_addid1->add_callback(addids_match_info_refresh_cb);
91            awar_addid2->add_callback(addids_match_info_refresh_cb);
92
93            addids_match_info_refresh_cb(aw_root);
94        }
95
96        if (error) {
97            error = t1.close(error);
98            error = t2.close(error);
99            aw_message(error);
100        }
101        else {
102        created = true;
103    }
104}
105}
106
107// --------------------------------------------------------------------------------
108
109static bool was_renamed = false;
110
111void MG_set_renamed(bool renamed, AW_root *aw_root, const char *reason) {
112    aw_root->awar(AWAR_RENAME_STATUS)->write_string(reason);
113    was_renamed = renamed;
114}
115
116GB_ERROR MG_expect_renamed() {
117    GB_ERROR error = 0;
118    if (!was_renamed) error = "First you have to rename species in both databases";
119    return error;
120}
121
122// --------------------------------------------------------------------------------
123
124static GB_ERROR renameDB(const char *which, GBDATA *gb_db, bool allowDups) {
125    arb_progress progress(GBS_global_string("Generating new names in %s database", which));
126    bool         isDuplicatesWarning;
127    GB_ERROR     error = AWTC_pars_names(gb_db, &isDuplicatesWarning);
128
129    if (error) {
130        error = GBS_global_string("While renaming %s DB:\n%s", which, error);
131        if (isDuplicatesWarning && allowDups) {
132            aw_message(error);
133            aw_message("Duplicates error ignored. Be careful during merge!");
134            error = 0;
135        }
136    }
137
138    return error;
139}
140
141static void rename_both_databases(AW_window *aww) {
142    GB_ERROR  error     = 0;
143    AW_root  *aw_root   = aww->get_root();
144    char     *match     = aw_root->awar(AWAR_ADDID_MATCH)->read_string();
145    bool      allowDups = aw_root->awar(AWAR_ALLOW_DUPS)->read_int();
146
147    if (strcmp(match, "Ok") == 0) {
148        error = renameDB("source", GLOBAL_gb_merge, allowDups);
149        if (!error) error = renameDB("destination", GLOBAL_gb_dest, allowDups);
150    }
151    else {
152        error = "Denying rename - additional fields have to match!";
153    }
154    free(match);
155
156    if (error) {
157        aw_message(error);
158        MG_set_renamed(false, aw_root, "Failed");
159    }
160    else {
161        MG_set_renamed(true, aw_root, "Ok");
162    }
163}
164
165static void override_toggle_cb(AW_window *aww) {
166    AW_root *aw_root = aww->get_root();
167
168    if (aw_root->awar(AWAR_OVERRIDE)->read_int() == 1) {
169        MG_set_renamed(true, aw_root, "Overridden");
170    }
171    else {
172        MG_set_renamed(false, aw_root, "Not renamed");
173    }
174}
175
176AW_window *MG_merge_names_cb(AW_root *awr) {
177    static AW_window_simple *aws = 0;
178    if (!aws) {
179        aws = new AW_window_simple;
180        aws->init(awr, "MERGE_AUTORENAME_SPECIES", "SYNCHRONIZE NAMES");
181        aws->load_xfig("merge/names.fig");
182
183        aws->at("close"); aws->callback((AW_CB0)AW_POPDOWN);
184        aws->create_button("CLOSE", "CLOSE", "C");
185
186        aws->at("help");
187        aws->callback(AW_POPUP_HELP, (AW_CL)"mg_names.hlp");
188        aws->create_button("HELP", "HELP", "H");
189
190        aws->at("addid1");
191        aws->create_input_field(AWAR_MERGE_ADDID, 10);
192
193        aws->at("addid2");
194        aws->create_input_field(AWAR_DEST_ADDID, 10);
195
196        aws->at("dups");
197        aws->label("Allow merging duplicates (dangerous! see HELP)");
198        aws->create_toggle(AWAR_ALLOW_DUPS);
199
200        aws->at("override");
201        aws->label("Override (even more dangerous! see HELP)");
202        aws->callback(override_toggle_cb);
203        aws->create_toggle(AWAR_OVERRIDE);
204
205        aws->at("match");
206        aws->button_length(12);
207        aws->create_button("MATCH", AWAR_ADDID_MATCH, 0, "+");
208
209        aws->at("status");
210        aws->button_length(25);
211        aws->label("Status:");
212        aws->create_button("STATUS", AWAR_RENAME_STATUS, 0, "+");
213
214        aws->at("rename");
215        aws->callback(rename_both_databases);
216        aws->create_autosize_button("RENAME_DATABASES", "Rename species");
217
218        aws->button_length(0);
219        aws->shadow_width(1);
220        aws->at("icon");
221        aws->callback(AW_POPUP_HELP, (AW_CL)"mg_names.hlp");
222        aws->create_button("HELP_MERGE", "#merge/icon.bitmap");
223    }
224    return aws;
225}
Note: See TracBrowser for help on using the repository browser.