| 1 | // ============================================================ // |
|---|
| 2 | // // |
|---|
| 3 | // File : itemtools.cxx // |
|---|
| 4 | // Purpose : item-specific toolkit // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in July 2015 // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ============================================================ // |
|---|
| 10 | |
|---|
| 11 | #include "items.h" |
|---|
| 12 | |
|---|
| 13 | #include <arb_msg.h> |
|---|
| 14 | #include <arb_str.h> |
|---|
| 15 | |
|---|
| 16 | #include <aw_window.hxx> |
|---|
| 17 | |
|---|
| 18 | #include <arbdb.h> |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | const char *ITEMS_get_key(ItemSelector& selector) { |
|---|
| 22 | // this method generates unique keys, if you have multiple ItemSelectors, |
|---|
| 23 | // which have the same QUERY_ITEM_TYPE, but should be handled differently. |
|---|
| 24 | // In these cases the 'items_name' field of these selectors has to differ! |
|---|
| 25 | // |
|---|
| 26 | // Example: |
|---|
| 27 | // - species queries in mergetool (both QUERY_ITEM_SPECIES, bound to different database) |
|---|
| 28 | |
|---|
| 29 | const char *items_name = selector.items_name; |
|---|
| 30 | |
|---|
| 31 | static SmartCharPtr item_key; |
|---|
| 32 | item_key = GBS_string_2_key(items_name); |
|---|
| 33 | |
|---|
| 34 | return &*item_key; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | static const char *type_key[QUERY_ITEM_TYPES] = { |
|---|
| 38 | "species", |
|---|
| 39 | "organism", |
|---|
| 40 | "gene", |
|---|
| 41 | "experiment", |
|---|
| 42 | "sai" |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | const char *ITEMS_get_type_key(QUERY_ITEM_TYPE itemtype) { |
|---|
| 46 | return type_key[itemtype]; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | const char *ITEMS_get_possible_former_itemname(ItemSelector& selector) { |
|---|
| 50 | const char *item_name = selector.item_name; |
|---|
| 51 | if (GB_check_key(item_name) != NULp) { |
|---|
| 52 | // currently this transforms "target species" + "source species" into "species" |
|---|
| 53 | return type_key[selector.type]; |
|---|
| 54 | } |
|---|
| 55 | return item_name; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | inline char *itemTypeSpecificWindowID(QUERY_ITEM_TYPE itemtype, const char *window_id) { |
|---|
| 59 | char *specific_win_id; |
|---|
| 60 | if (speciesOrOrganism(itemtype)) { |
|---|
| 61 | specific_win_id = ARB_strdup(window_id); // for species return given window id |
|---|
| 62 | } |
|---|
| 63 | else { |
|---|
| 64 | char *item_type_id = ARB_strdup(ITEMS_get_type_key(itemtype)); |
|---|
| 65 | specific_win_id = GBS_global_string_copy("%s_%s", window_id, ARB_strupper(item_type_id)); |
|---|
| 66 | free(item_type_id); |
|---|
| 67 | } |
|---|
| 68 | return specific_win_id; |
|---|
| 69 | } |
|---|
| 70 | inline char *itemSpecificWindowID(const ItemSelector& selector, const char *window_id, bool force_suffix) { |
|---|
| 71 | char *specific_win_id; |
|---|
| 72 | if (selector.type == QUERY_ITEM_SPECIES && !force_suffix && strcmp(selector.item_name, "species") == 0) { |
|---|
| 73 | specific_win_id = ARB_strdup(window_id); // for species return given window id |
|---|
| 74 | } |
|---|
| 75 | else { |
|---|
| 76 | specific_win_id = GBS_global_string_copy("%s_%s", window_id, ITEMS_get_key(selector)); |
|---|
| 77 | } |
|---|
| 78 | return specific_win_id; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | void init_itemType_specific_window(AW_root *aw_root, AW_window_simple *aws, const ItemSelector& itemType, const char *id, const char *title_format, bool plural) { |
|---|
| 82 | char *s_id = itemTypeSpecificWindowID(itemType.type, id); |
|---|
| 83 | char *s_title = GBS_global_string_copy(title_format, plural ? itemType.items_name : itemType.item_name); |
|---|
| 84 | |
|---|
| 85 | aws->init(aw_root, s_id, s_title); |
|---|
| 86 | |
|---|
| 87 | free(s_title); |
|---|
| 88 | free(s_id); |
|---|
| 89 | } |
|---|
| 90 | void init_item_specific_window(AW_root *aw_root, AW_window_simple *aws, const ItemSelector& itemType, const char *id, bool force_suffix, const char *title_format, bool plural) { |
|---|
| 91 | /*! Init new window in an item specific manner. |
|---|
| 92 | * |
|---|
| 93 | * @param aw_root window root. |
|---|
| 94 | * @param aws window to initialize. |
|---|
| 95 | * @param itemType specifies type of item. |
|---|
| 96 | * @param id the base macro id for this window. will be suffixed specific for itemtype. |
|---|
| 97 | * @param force_suffix force suffixing for normal 'species'? (false = similar to behavior of init_itemType_specific_window). recommended value is 'true'. |
|---|
| 98 | * @param title_format format string for title. |
|---|
| 99 | * @param plural use plural in title? |
|---|
| 100 | */ |
|---|
| 101 | |
|---|
| 102 | char *s_id = itemSpecificWindowID(itemType, id, force_suffix); |
|---|
| 103 | char *s_title = GBS_global_string_copy(title_format, plural ? itemType.items_name : itemType.item_name); |
|---|
| 104 | |
|---|
| 105 | aws->init(aw_root, s_id, s_title); |
|---|
| 106 | |
|---|
| 107 | free(s_title); |
|---|
| 108 | free(s_id); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // -------------------------------------------------------------------------------- |
|---|
| 112 | |
|---|
| 113 | #ifdef UNIT_TESTS |
|---|
| 114 | #ifndef TEST_UNIT_H |
|---|
| 115 | #include <test_unit.h> |
|---|
| 116 | #endif |
|---|
| 117 | |
|---|
| 118 | void TEST_itemTypeSpecificWindowID() { |
|---|
| 119 | const char *WINID = "WINID"; |
|---|
| 120 | for (int i = 0; i<QUERY_ITEM_TYPES; ++i) { |
|---|
| 121 | QUERY_ITEM_TYPE itemtype = (QUERY_ITEM_TYPE)i; |
|---|
| 122 | char *specific_winid = itemTypeSpecificWindowID(itemtype, WINID); |
|---|
| 123 | const char *expected_winid = NULp; |
|---|
| 124 | switch (itemtype) { |
|---|
| 125 | case QUERY_ITEM_SPECIES: expected_winid = WINID; break; |
|---|
| 126 | case QUERY_ITEM_ORGANISM: expected_winid = WINID; break; |
|---|
| 127 | case QUERY_ITEM_GENE: expected_winid = "WINID_GENE"; break; |
|---|
| 128 | case QUERY_ITEM_EXPERIMENT: expected_winid = "WINID_EXPERIMENT"; break; |
|---|
| 129 | case QUERY_ITEM_SAI: expected_winid = "WINID_SAI"; break; |
|---|
| 130 | } |
|---|
| 131 | TEST_EXPECT_EQUAL(specific_winid, expected_winid); |
|---|
| 132 | free(specific_winid); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | #endif // UNIT_TESTS |
|---|
| 137 | |
|---|
| 138 | // -------------------------------------------------------------------------------- |
|---|