source: tags/arb_5.5/MERGE/MG_names.cxx

Last change on this file was 5852, checked in by westram, 15 years ago
  • followup to [5851] (more to follow)
    • GB_get_error() → GB_await_error() where appropriate (MERGE)
    • fixed error handling in several functions
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1#include <cstdio>
2#include <cstdlib>
3#include <cstring>
4
5#include <arbdb.h>
6#include <aw_root.hxx>
7#include <aw_device.hxx>
8#include <aw_window.hxx>
9#include <AW_rename.hxx>
10#include "merge.hxx"
11
12// --------------------------------------------------------------------------------
13
14#define AWAR_MERGE_ADDID "tmp/merge1/addid"
15#define AWAR_DEST_ADDID  "tmp/merge2/addid"
16
17#define AWAR_ADDID_MATCH   "tmp/merge/addidmatch"
18#define AWAR_RENAME_STATUS "tmp/merge/renamestat"
19#define AWAR_ALLOW_DUPS    "tmp/merge/allowdups"
20#define AWAR_OVERRIDE      "tmp/merge/override"
21
22// --------------------------------------------------------------------------------
23
24void MG_create_rename_awars(AW_root *aw_root, AW_default aw_def) {
25    aw_root->awar_string(AWAR_ADDID_MATCH, "", aw_def);
26    aw_root->awar_string(AWAR_RENAME_STATUS, "", aw_def);
27    aw_root->awar_int(AWAR_ALLOW_DUPS, 0, aw_def);
28    aw_root->awar_int(AWAR_OVERRIDE, 0, aw_def);
29}
30
31// --------------------------------------------------------------------------------
32
33static const char *addids_match_info(AW_root *aw_root) {
34    char       *addid1 = aw_root->awar(AWAR_MERGE_ADDID)->read_string();
35    char       *addid2 = aw_root->awar(AWAR_DEST_ADDID)->read_string();
36    const char *result = (strcmp(addid1, addid2) == 0) ? "Ok" : "MISMATCH!";
37
38    free(addid2);
39    free(addid1);
40   
41    return result;
42}
43
44static void addids_match_info_refresh_cb(AW_root *aw_root) {
45    aw_root->awar(AWAR_ADDID_MATCH)->write_string(addids_match_info(aw_root));
46    MG_set_renamed(false, aw_root, "Needed (add.field changed)"); 
47}
48
49void MG_create_db_dependent_rename_awars(AW_root *aw_root, GBDATA *gb_merge, GBDATA *gb_dest) {
50    static bool created = false;
51
52    if (!created) {
53        GB_transaction t1(gb_merge);
54        GB_transaction t2(gb_dest);
55        GB_ERROR       error = 0;
56
57        // Awars for additional ID need to be mapped, cause they use same db-path in both DBs
58
59        GBDATA     *gb_addid1 = GB_search(gb_merge, AWAR_NAMESERVER_ADDID, GB_STRING);
60        GBDATA     *gb_addid2 = GB_search(gb_dest, AWAR_NAMESERVER_ADDID, GB_STRING);
61        const char *addid1    = gb_addid1 ? GB_read_char_pntr(gb_addid1) : "";
62        const char *addid2    = gb_addid2 ? GB_read_char_pntr(gb_addid2) : "";
63
64        // use other as default (needed e.g. for import)
65        if (gb_addid1 && !gb_addid2) {
66            gb_addid2             = GB_create(gb_dest, AWAR_NAMESERVER_ADDID, GB_STRING);
67            if (!gb_addid2) error = GB_await_error();
68            else error            = GB_write_string(gb_addid2, addid1);
69        }
70        else if (!gb_addid1 && gb_addid2) {
71            gb_addid1             = GB_create(gb_merge, AWAR_NAMESERVER_ADDID, GB_STRING);
72            if (!gb_addid1) error = GB_await_error();
73            else error            = GB_write_string(gb_addid1, addid2);
74        }
75
76        if (!error) {
77            AW_awar *awar_addid1 = aw_root->awar_string(AWAR_MERGE_ADDID, "xxx", gb_merge);
78            AW_awar *awar_addid2 = aw_root->awar_string(AWAR_DEST_ADDID, "xxx", gb_dest);
79
80            awar_addid1->unmap(); awar_addid1->map(gb_addid1);
81            awar_addid2->unmap(); awar_addid2->map(gb_addid2);
82
83            awar_addid1->add_callback(addids_match_info_refresh_cb);
84            awar_addid2->add_callback(addids_match_info_refresh_cb);
85
86            addids_match_info_refresh_cb(aw_root);
87        }
88       
89        if (error) {
90            error = t1.close(error);
91            error = t2.close(error);
92            aw_message(error);
93        }
94        else {
95        created = true;
96    }
97}
98}
99
100// --------------------------------------------------------------------------------
101
102static bool was_renamed = false;
103
104void MG_set_renamed(bool renamed, AW_root *aw_root, const char *reason) {
105    aw_root->awar(AWAR_RENAME_STATUS)->write_string(reason);
106    was_renamed = renamed;
107}
108
109GB_ERROR MG_expect_renamed() {
110    GB_ERROR error = 0;
111    if (!was_renamed) error = "First you have to rename species in both databases";
112    return error;
113}
114
115// --------------------------------------------------------------------------------
116
117static GB_ERROR renameDB(const char *which, GBDATA *gb_db, bool allowDups) {
118    aw_openstatus(GBS_global_string("Generating new names in %s database", which));
119    aw_status("Contacting name server");
120
121    bool     isDuplicatesWarning;
122    GB_ERROR error = AWTC_pars_names(gb_db, 1, &isDuplicatesWarning);
123
124    if (error) {
125        error = GBS_global_string("While renaming %s DB:\n%s", which, error);
126        if (isDuplicatesWarning && allowDups) {
127            aw_message(error);
128            aw_message("Duplicates error ignored. Be careful during merge!");
129            error = 0;
130        }
131    }
132
133    aw_closestatus();
134    return error;
135}
136
137static void rename_both_databases(AW_window *aww) {
138    GB_ERROR  error     = 0;
139    AW_root  *aw_root   = aww->get_root();
140    char     *match     = aw_root->awar(AWAR_ADDID_MATCH)->read_string();
141    bool      allowDups = aw_root->awar(AWAR_ALLOW_DUPS)->read_int();
142
143    if (strcmp(match, "Ok") == 0) {
144        error = renameDB("source", GLOBAL_gb_merge, allowDups);
145        if (!error) error = renameDB("destination", GLOBAL_gb_dest, allowDups);
146    }
147    else {
148        error = "Denying rename - additional fields have to match!";
149    }
150    free(match);
151
152    if (error) {
153        aw_message(error);
154        MG_set_renamed(false, aw_root, "Failed");
155    }
156    else {
157        MG_set_renamed(true, aw_root, "Ok");
158    }
159}
160
161static void override_toggle_cb(AW_window *aww) {
162    AW_root *aw_root = aww->get_root();
163
164    if (aw_root->awar(AWAR_OVERRIDE)->read_int() == 1) {
165        MG_set_renamed(true, aw_root, "Overridden");
166    }
167    else {
168        MG_set_renamed(false, aw_root, "Not renamed");
169    }
170}
171
172AW_window *MG_merge_names_cb(AW_root *awr){
173    static AW_window_simple *aws = 0;
174    if (!aws) {
175        aws = new AW_window_simple;
176        aws->init( awr, "MERGE_AUTORENAME_SPECIES", "SYNCHRONIZE NAMES");
177        aws->load_xfig("merge/names.fig");
178
179        aws->at("close");aws->callback((AW_CB0)AW_POPDOWN);
180        aws->create_button("CLOSE","CLOSE","C");
181
182        aws->at("help");
183        aws->callback(AW_POPUP_HELP,(AW_CL)"mg_names.hlp");
184        aws->create_button("HELP","HELP","H");
185
186        aws->at("addid1");
187        aws->create_input_field(AWAR_MERGE_ADDID, 10);
188
189        aws->at("addid2");
190        aws->create_input_field(AWAR_DEST_ADDID, 10);
191
192        aws->at("dups");
193        aws->label("Allow merging duplicates (dangerous! see HELP)");
194        aws->create_toggle(AWAR_ALLOW_DUPS);
195
196        aws->at("override");
197        aws->label("Override (even more dangerous! see HELP)");
198        aws->callback(override_toggle_cb);
199        aws->create_toggle(AWAR_OVERRIDE);
200
201        aws->at("match");
202        aws->button_length(12);
203        aws->create_button("MATCH", AWAR_ADDID_MATCH, 0, "+");
204
205        aws->at("status");
206        aws->button_length(25);
207        aws->label("Status:");
208        aws->create_button("STATUS", AWAR_RENAME_STATUS, 0, "+");
209
210        aws->at("rename");
211        aws->callback(rename_both_databases);
212        aws->create_autosize_button("RENAME_DATABASES","Rename species");
213       
214        aws->button_length(0);
215        aws->shadow_width(1);
216        aws->at("icon");
217        aws->callback(AW_POPUP_HELP,(AW_CL)"mg_names.hlp");
218        aws->create_button("HELP_MERGE", "#merge/icon.bitmap");
219    }
220    return aws;
221}
Note: See TracBrowser for help on using the repository browser.