summaryrefslogtreecommitdiffstats
path: root/uemis-downloader.c
diff options
context:
space:
mode:
Diffstat (limited to 'uemis-downloader.c')
-rw-r--r--uemis-downloader.c159
1 files changed, 89 insertions, 70 deletions
diff --git a/uemis-downloader.c b/uemis-downloader.c
index 9864cfe66..554da290c 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -13,11 +13,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <dirent.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
-#include <glib/gi18n.h>
+#include "gettext.h"
#include "libdivecomputer.h"
#include "uemis.h"
@@ -27,9 +28,9 @@
#if USE_GTK_UI
#include "display-gtk.h"
#endif
-#define ERR_FS_ALMOST_FULL N_("Uemis Zurich: File System is almost full\nDisconnect/reconnect the dive computer\nand click \'Retry\'")
-#define ERR_FS_FULL N_("Uemis Zurich: File System is full\nDisconnect/reconnect the dive computer\nand try again")
-#define ERR_FS_SHORT_WRITE N_("Short write to req.txt file\nIs the Uemis Zurich plugged in correctly?")
+#define ERR_FS_ALMOST_FULL QT_TR_NOOP("Uemis Zurich: File System is almost full\nDisconnect/reconnect the dive computer\nand click \'Retry\'")
+#define ERR_FS_FULL QT_TR_NOOP("Uemis Zurich: File System is full\nDisconnect/reconnect the dive computer\nand try again")
+#define ERR_FS_SHORT_WRITE QT_TR_NOOP("Short write to req.txt file\nIs the Uemis Zurich plugged in correctly?")
#define BUFLEN 2048
#define NUM_PARAM_BUFS 10
@@ -55,7 +56,7 @@ static int mbuf_size = 0;
struct argument_block {
const char *mountpath;
progressbar_t *progress;
- gboolean force_download;
+ bool force_download;
};
#endif
@@ -98,7 +99,7 @@ static void uemis_ts(char *buffer, void *_when)
/* float minutes */
static void uemis_duration(char *buffer, duration_t *duration)
{
- duration->seconds = g_ascii_strtod(buffer, NULL) * 60 + 0.5;
+ duration->seconds = ascii_strtod(buffer, NULL) * 60 + 0.5;
}
/* int cm */
@@ -113,7 +114,7 @@ static void uemis_get_index(char *buffer, int *idx)
}
/* space separated */
-static void uemis_add_string(char *buffer, char **text)
+static void uemis_add_string(const char *buffer, char **text)
{
/* do nothing if this is an empty buffer (Uemis sometimes returns a single
* space for empty buffers) */
@@ -135,8 +136,8 @@ static void uemis_add_string(char *buffer, char **text)
static void uemis_get_weight(char *buffer, weightsystem_t *weight, int diveid)
{
weight->weight.grams = uemis_get_weight_unit(diveid) ?
- lbs_to_grams(g_ascii_strtod(buffer, NULL)) : g_ascii_strtod(buffer, NULL) * 1000;
- weight->description = strdup(_("unknown"));
+ lbs_to_grams(ascii_strtod(buffer, NULL)) : ascii_strtod(buffer, NULL) * 1000;
+ weight->description = strdup(tr("unknown"));
}
static struct dive *uemis_start_dive(uint32_t deviceid)
@@ -172,18 +173,36 @@ static long bytes_available(int file)
static int number_of_file(char *path)
{
int count = 0;
- GDir *dir = g_dir_open(path, 0, NULL);
- while (g_dir_read_name(dir))
- count++;
- g_dir_close(dir);
+ DIR * dirp;
+ struct dirent * entry;
+
+ dirp = opendir(path);
+ while ((entry = readdir(dirp)) != NULL) {
+ if (entry->d_type == DT_REG) { /* If the entry is a regular file */
+ count++;
+ }
+ }
+ closedir(dirp);
return count;
}
+static char *build_filename(const char *path, const char *name)
+{
+ int len = strlen(path) + strlen(name) + 1;
+ char *buf = malloc(len);
+#if WIN32
+ snprintf(buf, len, "%s\%s", path, name);
+#else
+ snprintf(buf, len, "%s/%s", path, name);
+#endif
+ return buf;
+}
+
/* Check if there's a req.txt file and get the starting filenr from it.
* Test for the maximum number of ANS files (I believe this is always
* 4000 but in case there are differences depending on firmware, this
* code is easy enough */
-static gboolean uemis_init(const char *path)
+static bool uemis_init(const char *path)
{
char *ans_path;
int i;
@@ -191,8 +210,8 @@ static gboolean uemis_init(const char *path)
if (!path)
return FALSE;
/* let's check if this is indeed a Uemis DC */
- reqtxt_path = g_build_filename(path, "/req.txt", NULL);
- reqtxt_file = g_open(reqtxt_path, O_RDONLY, 0666);
+ reqtxt_path = build_filename(path,"req.txt");
+ reqtxt_file = open(reqtxt_path, O_RDONLY, 0666);
if (!reqtxt_file) {
#if UEMIS_DEBUG & 1
fprintf(debugfile, ":EE req.txt can't be opened\n");
@@ -218,9 +237,9 @@ static gboolean uemis_init(const char *path)
/* It would be nice if we could simply go back to the first set of
* ANS files. But with a FAT filesystem that isn't possible */
- ans_path = g_build_filename(path, "ANS", NULL);
+ ans_path = build_filename(path, "ANS");
number_of_files = number_of_file(ans_path);
- g_free(ans_path);
+ free(ans_path);
/* initialize the array in which we collect the answers */
for (i = 0; i < NUM_PARAM_BUFS; i++)
param_buff[i] = "";
@@ -274,7 +293,7 @@ static char *next_segment(char *buf, int *offset, int size)
{
int i = *offset;
int seg_size;
- gboolean done = FALSE;
+ bool done = FALSE;
char *segment;
while (!done) {
@@ -319,7 +338,7 @@ static void buffer_add(char **buffer, int *buffer_size, char *buf)
}
/* are there more ANS files we can check? */
-static gboolean next_file(int max)
+static bool next_file(int max)
{
if (filenr >= max)
return FALSE;
@@ -359,7 +378,7 @@ static char *first_object_id_val(char* buf)
/* ultra-simplistic; it doesn't deal with the case when the object_id is
* split across two chunks. It also doesn't deal with the discrepancy between
* object_id and dive number as understood by the dive computer */
-static void show_progress(char *buf, char *what)
+static void show_progress(char *buf, const char *what)
{
char *val = first_object_id_val(buf);
if (val) {
@@ -367,7 +386,7 @@ static void show_progress(char *buf, char *what)
#if UEMIS_DEBUG & 2
fprintf(debugfile,"reading %s %s\n", what, val);
#endif
- uemis_info(_("Reading %s %s"), what, val);
+ uemis_info(tr("Reading %s %s"), what, val);
free(val);
}
}
@@ -380,25 +399,25 @@ static void uemis_increased_timeout(int *timeout)
}
/* send a request to the dive computer and collect the answer */
-static gboolean uemis_get_answer(const char *path, char *request, int n_param_in,
- int n_param_out, char **error_text)
+static bool uemis_get_answer(const char *path, char *request, int n_param_in,
+ int n_param_out, const char **error_text)
{
int i = 0, file_length;
char sb[BUFLEN];
char fl[13];
char tmp[101];
- char *what = _("data");
- gboolean searching = TRUE;
- gboolean assembling_mbuf = FALSE;
- gboolean ismulti = FALSE;
- gboolean found_answer = FALSE;
- gboolean more_files = TRUE;
- gboolean answer_in_mbuf = FALSE;
+ const char *what = tr("data");
+ bool searching = TRUE;
+ bool assembling_mbuf = FALSE;
+ bool ismulti = FALSE;
+ bool found_answer = FALSE;
+ bool more_files = TRUE;
+ bool answer_in_mbuf = FALSE;
char *ans_path;
int ans_file;
int timeout = UEMIS_LONG_TIMEOUT;
- reqtxt_file = g_open(reqtxt_path, O_RDWR | O_CREAT, 0666);
+ reqtxt_file = open(reqtxt_path, O_RDWR | O_CREAT, 0666);
snprintf(sb, BUFLEN, "n%04d12345678", filenr);
str_append_with_delim(sb, request);
for (i = 0; i < n_param_in; i++)
@@ -408,11 +427,11 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
answer_in_mbuf = TRUE;
str_append_with_delim(sb, "");
if (! strcmp(request, "getDivelogs"))
- what = _("divelog entry id");
+ what = tr("divelog entry id");
else if (!strcmp(request, "getDivespot"))
- what = _("divespot data id");
+ what = tr("divespot data id");
else if (!strcmp(request, "getDive"))
- what = _("more data dive id");
+ what = tr("more data dive id");
}
str_append_with_delim(sb, "");
file_length = strlen(sb);
@@ -422,11 +441,11 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
fprintf(debugfile,"::w req.txt \"%s\"\n", sb);
#endif
if (write(reqtxt_file, sb, strlen(sb)) != strlen(sb)) {
- *error_text = _(ERR_FS_SHORT_WRITE);
+ *error_text = tr(ERR_FS_SHORT_WRITE);
return FALSE;
}
if (! next_file(number_of_files)) {
- *error_text = _(ERR_FS_FULL);
+ *error_text = tr(ERR_FS_FULL);
more_files = FALSE;
}
trigger_response(reqtxt_file, "n", filenr, file_length);
@@ -438,8 +457,8 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
return FALSE;
progress_bar_fraction = filenr / 4000.0;
snprintf(fl, 13, "ANS%d.TXT", filenr - 1);
- ans_path = g_build_filename(path, "ANS", fl, NULL);
- ans_file = g_open(ans_path, O_RDONLY, 0666);
+ ans_path = build_filename(build_filename(path, "ANS"), fl);
+ ans_file = open(ans_path, O_RDONLY, 0666);
read(ans_file, tmp, 100);
close(ans_file);
#if UEMIS_DEBUG & 8
@@ -453,7 +472,7 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
pbuf[3] = 0;
fprintf(debugfile, "::t %s \"%s...\"\n", ans_path, pbuf);
#endif
- g_free(ans_path);
+ free(ans_path);
if (tmp[0] == '1') {
searching = FALSE;
if (tmp[1] == 'm') {
@@ -464,29 +483,29 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
assembling_mbuf = FALSE;
if (assembling_mbuf) {
if (! next_file(number_of_files)) {
- *error_text = _(ERR_FS_FULL);
+ *error_text = tr(ERR_FS_FULL);
more_files = FALSE;
assembling_mbuf = FALSE;
}
- reqtxt_file = g_open(reqtxt_path, O_RDWR | O_CREAT, 0666);
+ reqtxt_file = open(reqtxt_path, O_RDWR | O_CREAT, 0666);
trigger_response(reqtxt_file, "n", filenr, file_length);
}
} else {
if (! next_file(number_of_files - 1)) {
- *error_text = _(ERR_FS_FULL);
+ *error_text = tr(ERR_FS_FULL);
more_files = FALSE;
assembling_mbuf = FALSE;
searching = FALSE;
}
- reqtxt_file = g_open(reqtxt_path, O_RDWR | O_CREAT, 0666);
+ reqtxt_file = open(reqtxt_path, O_RDWR | O_CREAT, 0666);
trigger_response(reqtxt_file, "r", filenr, file_length);
uemis_increased_timeout(&timeout);
}
if (ismulti && more_files && tmp[0] == '1') {
int size;
snprintf(fl, 13, "ANS%d.TXT", assembling_mbuf ? filenr - 2 : filenr - 1);
- ans_path = g_build_filename(path, "ANS", fl, NULL);
- ans_file = g_open(ans_path, O_RDONLY, 0666);
+ ans_path = build_filename(build_filename(path, "ANS"), fl);
+ ans_file = open(ans_path, O_RDONLY, 0666);
size = bytes_available(ans_file);
if (size > 3) {
char *buf = malloc(size - 2);
@@ -509,8 +528,8 @@ static gboolean uemis_get_answer(const char *path, char *request, int n_param_in
if (!ismulti) {
snprintf(fl, 13, "ANS%d.TXT", filenr - 1);
- ans_path = g_build_filename(path, "ANS", fl, NULL);
- ans_file = g_open(ans_path, O_RDONLY, 0666);
+ ans_path = build_filename(build_filename(path, "ANS"), fl);
+ ans_file = open(ans_path, O_RDONLY, 0666);
size = bytes_available(ans_file);
if (size > 3) {
buf = malloc(size - 2);
@@ -575,9 +594,9 @@ static void parse_divespot(char *buf)
"%s%s", len ? ", " : "", val);
} else if (!strcmp(type, "float")) {
if (!strcmp(tag, "longitude"))
- longitude = g_ascii_strtod(val, NULL);
+ longitude = ascii_strtod(val, NULL);
else if (!strcmp(tag, "latitude"))
- latitude = g_ascii_strtod(val, NULL);
+ latitude = ascii_strtod(val, NULL);
}
} while (tag && *tag);
uemis_set_divelocation(divespot, locationstring, latitude, longitude);
@@ -592,9 +611,9 @@ static void track_divespot(char *val, int diveid, char **location, degrees_t *la
return;
}
-static char *suit[] = { "", N_("wetsuit"), N_("semidry"), N_("drysuit") };
-static char *suit_type[] = { "", N_("shorty"), N_("vest"), N_("long john"), N_("jacket"), N_("full suit"), N_("2 pcs full suit") };
-static char *suit_thickness[] = { "", "0.5-2mm", "2-3mm", "3-5mm", "5-7mm", "8mm+", N_("membrane") };
+static char *suit[] = { "", QT_TR_NOOP("wetsuit"), QT_TR_NOOP("semidry"), QT_TR_NOOP("drysuit") };
+static char *suit_type[] = { "", QT_TR_NOOP("shorty"), QT_TR_NOOP("vest"), QT_TR_NOOP("long john"), QT_TR_NOOP("jacket"), QT_TR_NOOP("full suit"), QT_TR_NOOP("2 pcs full suit") };
+static char *suit_thickness[] = { "", "0.5-2mm", "2-3mm", "3-5mm", "5-7mm", "8mm+", QT_TR_NOOP("membrane") };
static void parse_tag(struct dive *dive, char *tag, char *val)
{
@@ -616,13 +635,13 @@ static void parse_tag(struct dive *dive, char *tag, char *val)
uemis_add_string(val, &dive->notes);
} else if (!strcmp(tag, "u8DiveSuit")) {
if (*suit[atoi(val)])
- uemis_add_string(_(suit[atoi(val)]), &dive->suit);
+ uemis_add_string(tr(suit[atoi(val)]), &dive->suit);
} else if (!strcmp(tag, "u8DiveSuitType")) {
if (*suit_type[atoi(val)])
- uemis_add_string(_(suit_type[atoi(val)]), &dive->suit);
+ uemis_add_string(tr(suit_type[atoi(val)]), &dive->suit);
} else if (!strcmp(tag, "u8SuitThickness")) {
if (*suit_thickness[atoi(val)])
- uemis_add_string(_(suit_thickness[atoi(val)]), &dive->suit);
+ uemis_add_string(tr(suit_thickness[atoi(val)]), &dive->suit);
}
}
@@ -637,14 +656,14 @@ static void parse_tag(struct dive *dive, char *tag, char *val)
* index into yet another data store that we read out later. In order to
* correctly populate the location and gps data from that we need to remember
* the adresses of those fields for every dive that references the divespot. */
-static void process_raw_buffer(uint32_t deviceid, char *inbuf, char **max_divenr, gboolean keep_number, int *for_dive)
+static void process_raw_buffer(uint32_t deviceid, char *inbuf, char **max_divenr, bool keep_number, int *for_dive)
{
char *buf = strdup(inbuf);
char *tp, *bp, *tag, *type, *val;
- gboolean done = FALSE;
+ bool done = FALSE;
int inbuflen = strlen(inbuf);
char *endptr = buf + inbuflen;
- gboolean log = FALSE;
+ bool log = FALSE;
char *sections[10];
int s, nr_sections = 0;
struct dive *dive = NULL;
@@ -758,22 +777,22 @@ static char *uemis_get_divenr(char *deviceidstr)
return strdup(divenr);
}
-char *do_uemis_import(const char *mountpath, short force_download)
+const char *do_uemis_import(const char *mountpath, short force_download)
{
char *newmax = NULL;
int start, end, i, offset;
uint32_t deviceidnr;
char objectid[10];
char *deviceid = NULL;
- char *result = NULL;
+ const char *result = NULL;
char *endptr;
- gboolean success, keep_number = FALSE, once = TRUE;
+ bool success, keep_number = FALSE, once = TRUE;
if (dive_table.nr == 0)
keep_number = TRUE;
- uemis_info(_("Init Communication"));
+ uemis_info(tr("Init Communication"));
if (! uemis_init(mountpath))
- return _("Uemis init failed");
+ return tr("Uemis init failed");
if (! uemis_get_answer(mountpath, "getDeviceId", 0, 1, &result))
goto bail;
deviceid = strdup(param_buff[0]);
@@ -784,7 +803,7 @@ char *do_uemis_import(const char *mountpath, short force_download)
/* param_buff[0] is still valid */
if (! uemis_get_answer(mountpath, "initSession", 1, 6, &result))
goto bail;
- uemis_info(_("Start download"));
+ uemis_info(tr("Start download"));
if (! uemis_get_answer(mountpath, "processSync", 0, 2, &result))
goto bail;
/* before starting the long download, check if user pressed cancel */
@@ -820,7 +839,7 @@ char *do_uemis_import(const char *mountpath, short force_download)
break;
/* finally, if the memory is getting too full, maybe we better stop, too */
if (progress_bar_fraction > 0.85) {
- result = _(ERR_FS_ALMOST_FULL);
+ result = tr(ERR_FS_ALMOST_FULL);
break;
}
/* clean up mbuf */
@@ -878,7 +897,7 @@ bail:
(void) uemis_get_answer(mountpath, "terminateSync", 0, 3, &result);
if (! strcmp(param_buff[0], "error")) {
if (! strcmp(param_buff[2],"Out of Memory"))
- result = _(ERR_FS_FULL);
+ result = tr(ERR_FS_FULL);
else
result = param_buff[2];
}
@@ -898,7 +917,7 @@ static void *pthread_wrapper(void *_data)
/* this simply ends the dialog without a response and asks not to be fired again
* as we set this function up in every loop while uemis_download is waiting for
* the download to finish */
-static gboolean timeout_func(gpointer _data)
+static bool timeout_func(gpointer _data)
{
GtkDialog *dialog = _data;
if (!import_thread_cancelled)
@@ -907,7 +926,7 @@ static gboolean timeout_func(gpointer _data)
}
GError *uemis_download(const char *mountpath, progressbar_t *progress,
- GtkDialog *dialog, gboolean force_download)
+ GtkDialog *dialog, bool force_download)
{
pthread_t pthread;
void *retval;
@@ -935,7 +954,7 @@ GError *uemis_download(const char *mountpath, progressbar_t *progress,
import_thread_cancelled = TRUE;
} else {
update_progressbar(args.progress, progress_bar_fraction);
- update_progressbar_text(args.progress, _("Cancelled, exiting cleanly..."));
+ update_progressbar_text(args.progress, tr("Cancelled, exiting cleanly..."));
usleep(100000);
}
}