mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Fix nickname saving in XML file to deal with utf8 characters
This makes the whole code much cleaner and simpler. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
96db56f89c
commit
df0ea07292
3 changed files with 39 additions and 23 deletions
3
dive.h
3
dive.h
|
@ -532,6 +532,9 @@ extern void flush_divelist(struct dive *dive);
|
||||||
extern void set_dc_nickname(struct dive *dive);
|
extern void set_dc_nickname(struct dive *dive);
|
||||||
extern const char *get_dc_nickname(const char *model, uint32_t deviceid);
|
extern const char *get_dc_nickname(const char *model, uint32_t deviceid);
|
||||||
extern void remember_dc(const char *model, uint32_t deviceid, const char *nickname, gboolean change_conf);
|
extern void remember_dc(const char *model, uint32_t deviceid, const char *nickname, gboolean change_conf);
|
||||||
|
extern gboolean dc_was_saved(struct divecomputer *dc);
|
||||||
|
extern void mark_dc_saved(struct divecomputer *dc);
|
||||||
|
extern void clear_dc_saved_status(void);
|
||||||
|
|
||||||
#define DIVE_ERROR_PARSE 1
|
#define DIVE_ERROR_PARSE 1
|
||||||
|
|
||||||
|
|
24
gtk-gui.c
24
gtk-gui.c
|
@ -44,6 +44,7 @@ struct dcnicknamelist {
|
||||||
const char *model;
|
const char *model;
|
||||||
uint32_t deviceid;
|
uint32_t deviceid;
|
||||||
struct dcnicknamelist *next;
|
struct dcnicknamelist *next;
|
||||||
|
gboolean saved;
|
||||||
};
|
};
|
||||||
static struct dcnicknamelist *nicknamelist;
|
static struct dcnicknamelist *nicknamelist;
|
||||||
char *nicknamestring;
|
char *nicknamestring;
|
||||||
|
@ -2078,6 +2079,29 @@ const char *get_dc_nickname(const char *model, uint32_t deviceid)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean dc_was_saved(struct divecomputer *dc)
|
||||||
|
{
|
||||||
|
struct dcnicknamelist *nn_entry = get_dc_nicknameentry(dc->model, dc->deviceid);
|
||||||
|
return nn_entry && nn_entry->saved;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mark_dc_saved(struct divecomputer *dc)
|
||||||
|
{
|
||||||
|
struct dcnicknamelist *nn_entry = get_dc_nicknameentry(dc->model, dc->deviceid);
|
||||||
|
if (nn_entry)
|
||||||
|
nn_entry->saved = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_dc_saved_status()
|
||||||
|
{
|
||||||
|
struct dcnicknamelist *nn_entry = nicknamelist;
|
||||||
|
|
||||||
|
while (nn_entry) {
|
||||||
|
nn_entry->saved = FALSE;
|
||||||
|
nn_entry = nn_entry->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* do we have a DIFFERENT divecomputer of the same model? */
|
/* do we have a DIFFERENT divecomputer of the same model? */
|
||||||
static struct dcnicknamelist *get_different_dc_nicknameentry(const char *model, int deviceid)
|
static struct dcnicknamelist *get_different_dc_nicknameentry(const char *model, int deviceid)
|
||||||
{
|
{
|
||||||
|
|
35
save-xml.c
35
save-xml.c
|
@ -461,36 +461,27 @@ static void save_trip(FILE *f, dive_trip_t *trip)
|
||||||
fprintf(f, "</trip>\n");
|
fprintf(f, "</trip>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *add_dc_to_string(char *dc_xml, struct divecomputer *dc)
|
static void save_dc_if_needed(FILE *f, struct divecomputer *dc)
|
||||||
{
|
{
|
||||||
char *pattern, *tmp;
|
|
||||||
const char *nickname;
|
const char *nickname;
|
||||||
int len;
|
|
||||||
|
|
||||||
/* we have no dc or no model or no deviceid information... nothing to do here */
|
/* we have no dc or no model or no deviceid information... nothing to do here */
|
||||||
if (!dc || !dc->model || !*dc->model || !dc->deviceid)
|
if (!dc || !dc->model || !*dc->model || !dc->deviceid)
|
||||||
return dc_xml;
|
return;
|
||||||
|
|
||||||
|
if (dc_was_saved(dc))
|
||||||
|
return;
|
||||||
|
|
||||||
|
mark_dc_saved(dc);
|
||||||
nickname = get_dc_nickname(dc->model, dc->deviceid);
|
nickname = get_dc_nickname(dc->model, dc->deviceid);
|
||||||
/* We have no nickname, or it is the same as the model ID - nothing interesting */
|
/* We have no nickname, or it is the same as the model ID - nothing interesting */
|
||||||
if (!nickname || !*nickname || !strcmp(dc->model, nickname))
|
if (!nickname || !*nickname || !strcmp(dc->model, nickname))
|
||||||
return dc_xml;
|
return;
|
||||||
|
|
||||||
len = sizeof(" model='' deviceid=''") + strlen(dc->model) + 8;
|
fprintf(f, "<divecomputerid model='%s' deviceid='%08x'", dc->model, dc->deviceid);
|
||||||
pattern = malloc(len);
|
show_utf8(f, nickname, " nickname='", "'", 1);
|
||||||
snprintf(pattern, len, " model='%s' deviceid='%08x'", dc->model, dc->deviceid);
|
fprintf(f, "/>\n");
|
||||||
if (dc_xml && strstr(dc_xml, pattern)) {
|
return;
|
||||||
/* already have that one */
|
|
||||||
free(pattern);
|
|
||||||
return dc_xml;
|
|
||||||
}
|
|
||||||
|
|
||||||
len += strlen(dc_xml) + strlen(nickname) + sizeof("<divecomputerid nickname=''/>\n");
|
|
||||||
tmp = malloc(len);
|
|
||||||
snprintf(tmp, len, "%s<divecomputerid%s nickname='%s'/>\n", dc_xml, pattern, nickname);
|
|
||||||
free(pattern);
|
|
||||||
free(dc_xml);
|
|
||||||
return tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define VERSION 2
|
#define VERSION 2
|
||||||
|
@ -500,7 +491,6 @@ void save_dives(const char *filename)
|
||||||
int i;
|
int i;
|
||||||
struct dive *dive;
|
struct dive *dive;
|
||||||
dive_trip_t *trip;
|
dive_trip_t *trip;
|
||||||
char *dc_xml = strdup("");
|
|
||||||
|
|
||||||
FILE *f = g_fopen(filename, "w");
|
FILE *f = g_fopen(filename, "w");
|
||||||
|
|
||||||
|
@ -514,11 +504,10 @@ void save_dives(const char *filename)
|
||||||
for_each_dive(i, dive) {
|
for_each_dive(i, dive) {
|
||||||
struct divecomputer *dc = &dive->dc;
|
struct divecomputer *dc = &dive->dc;
|
||||||
while (dc) {
|
while (dc) {
|
||||||
dc_xml = add_dc_to_string(dc_xml, dc);
|
save_dc_if_needed(f, dc);
|
||||||
dc = dc->next;
|
dc = dc->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fprintf(f, dc_xml);
|
|
||||||
fprintf(f, "</settings>\n<dives>\n");
|
fprintf(f, "</settings>\n<dives>\n");
|
||||||
|
|
||||||
for (trip = dive_trip_list; trip != NULL; trip = trip->next)
|
for (trip = dive_trip_list; trip != NULL; trip = trip->next)
|
||||||
|
|
Loading…
Add table
Reference in a new issue