1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 */
16
17 /** \file
18 * \ingroup bke
19 */
20
21 #include <stddef.h>
22 #include <stdlib.h>
23
24 #include "RNA_types.h"
25
26 #include "BLI_ghash.h"
27 #include "BLI_listbase.h"
28 #include "BLI_string.h"
29 #include "BLI_utildefines.h"
30
31 #include "BKE_addon.h" /* own include */
32 #include "BKE_idprop.h"
33
34 #include "DNA_listBase.h"
35 #include "DNA_userdef_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "CLG_log.h"
40
41 static CLG_LogRef LOG = {"bke.addon"};
42
43 /* -------------------------------------------------------------------- */
44 /** \name Add-on New/Free
45 * \{ */
46
BKE_addon_new(void)47 bAddon *BKE_addon_new(void)
48 {
49 bAddon *addon = MEM_callocN(sizeof(bAddon), "bAddon");
50 return addon;
51 }
52
BKE_addon_find(ListBase * addon_list,const char * module)53 bAddon *BKE_addon_find(ListBase *addon_list, const char *module)
54 {
55 return BLI_findstring(addon_list, module, offsetof(bAddon, module));
56 }
57
BKE_addon_ensure(ListBase * addon_list,const char * module)58 bAddon *BKE_addon_ensure(ListBase *addon_list, const char *module)
59 {
60 bAddon *addon = BKE_addon_find(addon_list, module);
61 if (addon == NULL) {
62 addon = BKE_addon_new();
63 BLI_strncpy(addon->module, module, sizeof(addon->module));
64 BLI_addtail(addon_list, addon);
65 }
66 return addon;
67 }
68
BKE_addon_remove_safe(ListBase * addon_list,const char * module)69 bool BKE_addon_remove_safe(ListBase *addon_list, const char *module)
70 {
71 bAddon *addon = BLI_findstring(addon_list, module, offsetof(bAddon, module));
72 if (addon) {
73 BLI_remlink(addon_list, addon);
74 BKE_addon_free(addon);
75 return true;
76 }
77 return false;
78 }
79
BKE_addon_free(bAddon * addon)80 void BKE_addon_free(bAddon *addon)
81 {
82 if (addon->prop) {
83 IDP_FreeProperty(addon->prop);
84 }
85 MEM_freeN(addon);
86 }
87
88 /** \} */
89
90 /* -------------------------------------------------------------------- */
91 /** \name Add-on Preference API
92 * \{ */
93
94 static GHash *global_addonpreftype_hash = NULL;
95
BKE_addon_pref_type_find(const char * idname,bool quiet)96 bAddonPrefType *BKE_addon_pref_type_find(const char *idname, bool quiet)
97 {
98 if (idname[0]) {
99 bAddonPrefType *apt;
100
101 apt = BLI_ghash_lookup(global_addonpreftype_hash, idname);
102 if (apt) {
103 return apt;
104 }
105
106 if (!quiet) {
107 CLOG_WARN(&LOG, "search for unknown addon-pref '%s'", idname);
108 }
109 }
110 else {
111 if (!quiet) {
112 CLOG_WARN(&LOG, "search for empty addon-pref");
113 }
114 }
115
116 return NULL;
117 }
118
BKE_addon_pref_type_add(bAddonPrefType * apt)119 void BKE_addon_pref_type_add(bAddonPrefType *apt)
120 {
121 BLI_ghash_insert(global_addonpreftype_hash, apt->idname, apt);
122 }
123
BKE_addon_pref_type_remove(const bAddonPrefType * apt)124 void BKE_addon_pref_type_remove(const bAddonPrefType *apt)
125 {
126 BLI_ghash_remove(global_addonpreftype_hash, apt->idname, NULL, MEM_freeN);
127 }
128
BKE_addon_pref_type_init(void)129 void BKE_addon_pref_type_init(void)
130 {
131 BLI_assert(global_addonpreftype_hash == NULL);
132 global_addonpreftype_hash = BLI_ghash_str_new(__func__);
133 }
134
BKE_addon_pref_type_free(void)135 void BKE_addon_pref_type_free(void)
136 {
137 BLI_ghash_free(global_addonpreftype_hash, NULL, MEM_freeN);
138 global_addonpreftype_hash = NULL;
139 }
140
141 /** \} */
142