From c4899aa8f1ba9ec97818bc138c0dd63f4d02b683 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sun, 18 May 2014 14:13:23 +0900 Subject: Download from dive computer: Fix the broken selection of devices The old code was completely bogus - it's confused about what the variable 'i' is counting. This also let's us select the Uemis mount point by default if that's the only valid "device" that we found. Compile tested on Windows, untested on Mac. Signed-off-by: Dirk Hohndel --- linux.c | 14 +++++++++----- macos.c | 12 +++++++++--- qt-ui/downloadfromdivecomputer.cpp | 1 - windows.c | 7 +++++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/linux.c b/linux.c index c694d5d67..ed063b28c 100644 --- a/linux.c +++ b/linux.c @@ -51,7 +51,7 @@ const char *system_default_filename(void) int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) { - int index = -1; + int index = -1, entries = 0; DIR *dp = NULL; struct dirent *ep = NULL; size_t i; @@ -85,7 +85,8 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) } callback(filename, userdata); if (is_default_dive_computer_device(filename)) - index = i; + index = entries; + entries++; break; } } @@ -93,6 +94,7 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) closedir(dp); } if (dc_type != DC_TYPE_SERIAL) { + int num_uemis = 0; file = fopen("/proc/mounts", "r"); if (file == NULL) return index; @@ -114,14 +116,16 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) callback(fname, userdata); if (is_default_dive_computer_device(fname)) - index = i; - i++; + index = entries; + entries++; + num_uemis++; free((void *)fname); } } - free(line); fclose(file); + if (num_uemis == 1 && entries == 1) /* if we found only one and it's a mounted Uemis, pick it */ + index = 0; } return index; } diff --git a/macos.c b/macos.c index 8b2f122dc..b75ddbf6f 100644 --- a/macos.c +++ b/macos.c @@ -47,7 +47,7 @@ const char *system_default_filename(void) int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) { - int index = -1; + int index = -1, entries = 0; DIR *dp = NULL; struct dirent *ep = NULL; size_t i; @@ -75,7 +75,8 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) } callback(filename, userdata); if (is_default_dive_computer_device(filename)) - index = i; + index = entries; + entries++; break; } } @@ -84,6 +85,7 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) } if (dc_type != DC_TYPE_SERIAL) { const char *dirname = "/Volumes"; + int num_uemis = 0; dp = opendir(dirname); if (dp == NULL) { return -1; @@ -99,11 +101,15 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) } callback(filename, userdata); if (is_default_dive_computer_device(filename)) - index = i; + index = entries; + entries++; + num_uemis++; break; } } closedir(dp); + if (num_uemis == 1 && entries == 1) /* if we find exactly one entry and that's a Uemis, select it */ + index = 0; } return index; } diff --git a/qt-ui/downloadfromdivecomputer.cpp b/qt-ui/downloadfromdivecomputer.cpp index 0347932d5..8c0f81aa5 100644 --- a/qt-ui/downloadfromdivecomputer.cpp +++ b/qt-ui/downloadfromdivecomputer.cpp @@ -56,7 +56,6 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) : progress_bar_text = ""; - fill_device_list(DC_TYPE_OTHER); fill_computer_list(); ui.chooseDumpFile->setEnabled(ui.dumpToFile->isChecked()); diff --git a/windows.c b/windows.c index da7e9005b..a8b7c55a6 100644 --- a/windows.c +++ b/windows.c @@ -90,6 +90,7 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) } if (dc_type != DC_TYPE_SERIAL) { int i; + int count_drives = 0; const int bufdef = 512; const char *dlabels[] = {"UEMISSDA", NULL}; char bufname[bufdef], bufval[bufdef], *p; @@ -110,12 +111,14 @@ int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) snprintf(data, sizeof(data), "%s (%s)", p, dlabels[i]); callback(data, userdata); if (is_default_dive_computer_device(p)) - index = i; - i++; + index = count_drives; + count_drives++; } } p = &p[strlen(p) + 1]; } + if (count_drives == 1) /* we found exactly one Uemis "drive" */ + index = 0; /* make it the selected "device" */ } } return index; -- cgit v1.2.3-70-g09d2