source: tags/arb-6.0/MERGE/MG_names.cxx

Last change on this file was 11244, checked in by westram, 10 years ago
  • use typed callbacks in MERGE
  • removed MG_popup_if_renamed (using [11242] instead)
  • 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 <aw_window.hxx>
18#include <arb_progress.h>
19
20// --------------------------------------------------------------------------------
21
22#define AWAR_ADDID_SRC  AWAR_MERGE_TMP_SRC "addid"
23#define AWAR_ADDID_DST  AWAR_MERGE_TMP_DST "addid"
24
25#define AWAR_ADDID_MATCH   AWAR_MERGE_TMP "addidmatch"
26#define AWAR_RENAME_STATUS AWAR_MERGE_TMP "renamestat"
27#define AWAR_ALLOW_DUPS    AWAR_MERGE_TMP "allowdups"
28#define AWAR_OVERRIDE      AWAR_MERGE_TMP "override"
29
30// --------------------------------------------------------------------------------
31
32void MG_create_rename_awars(AW_root *aw_root, AW_default aw_def) {
33    aw_root->awar_string(AWAR_ADDID_MATCH, "", aw_def);
34    aw_root->awar_string(AWAR_RENAME_STATUS, "", aw_def);
35    aw_root->awar_int(AWAR_ALLOW_DUPS, 0, aw_def);
36    aw_root->awar_int(AWAR_OVERRIDE, 0, aw_def);
37}
38
39// --------------------------------------------------------------------------------
40
41static const char *addids_match_info(AW_root *aw_root) {
42    char *src_addid = aw_root->awar(AWAR_ADDID_SRC)->read_string();
43    char *dst_addid = aw_root->awar(AWAR_ADDID_DST)->read_string();
44
45    const char *result = (strcmp(src_addid, dst_addid) == 0) ? "Ok" : "MISMATCH!";
46
47    free(dst_addid);
48    free(src_addid);
49
50    return result;
51}
52
53static void addids_match_info_refresh_cb(AW_root *aw_root) {
54    aw_root->awar(AWAR_ADDID_MATCH)->write_string(addids_match_info(aw_root));
55    MG_set_renamed(false, aw_root, "Needed (add.field changed)");
56}
57
58void MG_create_db_dependent_rename_awars(AW_root *aw_root, GBDATA *gb_src, GBDATA *gb_dst) {
59    static bool created = false;
60
61    if (!created) {
62        GB_transaction src_ta(gb_src);
63        GB_transaction dst_ta(gb_dst);
64        GB_ERROR       error = 0;
65
66        // Awars for additional ID need to be mapped, cause they use same db-path in both DBs
67
68        GBDATA *gb_src_addid = GB_search(gb_src, AWAR_NAMESERVER_ADDID, GB_STRING);
69        GBDATA *gb_dst_addid = GB_search(gb_dst, AWAR_NAMESERVER_ADDID, GB_STRING);
70
71        const char *src_addid = gb_src_addid ? GB_read_char_pntr(gb_src_addid) : "";
72        const char *dst_addid = gb_dst_addid ? GB_read_char_pntr(gb_dst_addid) : "";
73
74        // use other as default (needed e.g. for import)
75        if (gb_src_addid && !gb_dst_addid) {
76            gb_dst_addid             = GB_create(gb_dst, AWAR_NAMESERVER_ADDID, GB_STRING);
77            if (!gb_dst_addid) error = GB_await_error();
78            else error               = GB_write_string(gb_dst_addid, src_addid);
79        }
80        else if (!gb_src_addid && gb_dst_addid) {
81            gb_src_addid             = GB_create(gb_src, AWAR_NAMESERVER_ADDID, GB_STRING);
82            if (!gb_src_addid) error = GB_await_error();
83            else error               = GB_write_string(gb_src_addid, dst_addid);
84        }
85
86        if (!error) {
87            AW_awar *awar_src_addid = aw_root->awar_string(AWAR_ADDID_SRC, "xxx", gb_src);
88            AW_awar *awar_dst_addid = aw_root->awar_string(AWAR_ADDID_DST, "xxx", gb_dst);
89
90            awar_src_addid->unmap(); awar_src_addid->map(gb_src_addid);
91            awar_dst_addid->unmap(); awar_dst_addid->map(gb_dst_addid);
92
93            awar_src_addid->add_callback(addids_match_info_refresh_cb);
94            awar_dst_addid->add_callback(addids_match_info_refresh_cb);
95
96            addids_match_info_refresh_cb(aw_root);
97        }
98
99        if (error) {
100            error = src_ta.close(error);
101            error = dst_ta.close(error);
102            aw_message(error);
103        }
104        else {
105        created = true;
106    }
107}
108}
109
110// --------------------------------------------------------------------------------
111
112static bool was_renamed = false;
113
114void MG_set_renamed(bool renamed, AW_root *aw_root, const char *reason) {
115    aw_root->awar(AWAR_RENAME_STATUS)->write_string(reason);
116    was_renamed = renamed;
117}
118
119GB_ERROR MG_expect_renamed() {
120    GB_ERROR error = 0;
121    if (!was_renamed) error = "First you have to synchronize species IDs in both databases";
122    return error;
123}
124
125// --------------------------------------------------------------------------------
126
127static GB_ERROR renameDB(const char *which, GBDATA *gb_db, bool allowDups) {
128    arb_progress progress(GBS_global_string("Generating new names in %s database", which));
129    bool         isDuplicatesWarning;
130    GB_ERROR     error = AWTC_pars_names(gb_db, &isDuplicatesWarning);
131
132    if (error) {
133        error = GBS_global_string("While renaming %s DB:\n%s", which, error);
134        if (isDuplicatesWarning && allowDups) {
135            aw_message(error);
136            aw_message("Duplicates error ignored. Be careful during merge!");
137            error = 0;
138        }
139    }
140
141    return error;
142}
143
144static void rename_both_databases(AW_window *aww) {
145    GB_ERROR  error     = 0;
146    AW_root  *aw_root   = aww->get_root();
147    char     *match     = aw_root->awar(AWAR_ADDID_MATCH)->read_string();
148    bool      allowDups = aw_root->awar(AWAR_ALLOW_DUPS)->read_int();
149
150    if (strcmp(match, "Ok") == 0) {
151        error = renameDB("source", GLOBAL_gb_src, allowDups);
152        if (!error) error = renameDB("destination", GLOBAL_gb_dst, allowDups);
153    }
154    else {
155        error = "Denying rename - additional fields have to match!";
156    }
157    free(match);
158
159    if (error) {
160        aw_message(error);
161        MG_set_renamed(false, aw_root, "Failed");
162    }
163    else {
164        MG_set_renamed(true, aw_root, "Ok");
165    }
166}
167
168static void override_toggle_cb(AW_window *aww) {
169    AW_root *aw_root = aww->get_root();
170
171    if (aw_root->awar(AWAR_OVERRIDE)->read_int() == 1) {
172        MG_set_renamed(true, aw_root, "Overridden");
173    }
174    else {
175        MG_set_renamed(false, aw_root, "Not renamed");
176    }
177}
178
179AW_window *MG_create_merge_names_window(AW_root *awr) {
180    AW_window_simple *aws = new AW_window_simple;
181
182    aws->init(awr, "MERGE_AUTORENAME_SPECIES", "Synchronize IDs");
183    aws->load_xfig("merge/names.fig");
184
185    aws->at("close"); aws->callback((AW_CB0)AW_POPDOWN);
186    aws->create_button("CLOSE", "CLOSE", "C");
187
188    aws->at("help");
189    aws->callback(makeHelpCallback("mg_names.hlp"));
190    aws->create_button("HELP", "HELP", "H");
191
192    aws->at("addid1");
193    aws->create_input_field(AWAR_ADDID_SRC, 10);
194
195    aws->at("addid2");
196    aws->create_input_field(AWAR_ADDID_DST, 10);
197
198    aws->at("dups");
199    aws->label("Allow merging duplicates (dangerous! see HELP)");
200    aws->create_toggle(AWAR_ALLOW_DUPS);
201
202    aws->at("override");
203    aws->label("Override (even more dangerous! see HELP)");
204    aws->callback(override_toggle_cb);
205    aws->create_toggle(AWAR_OVERRIDE);
206
207    aws->at("match");
208    aws->button_length(12);
209    aws->create_button(0, AWAR_ADDID_MATCH, 0, "+");
210
211    aws->at("status");
212    aws->button_length(25);
213    aws->label("Status:");
214    aws->create_button(0, AWAR_RENAME_STATUS, 0, "+");
215
216    aws->at("rename");
217    aws->callback(rename_both_databases);
218    aws->create_autosize_button("RENAME_DATABASES", "Synchronize");
219
220    aws->button_length(0);
221    aws->shadow_width(1);
222    aws->at("icon");
223    aws->callback(makeHelpCallback("mg_names.hlp"));
224    aws->create_button("HELP_MERGE", "#merge/icon.xpm");
225
226    return aws;
227}
Note: See TracBrowser for help on using the repository browser.