1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
#include "downloadfromdivecomputer.h"
#include "ui_downloadfromdivecomputer.h"
#include "../libdivecomputer.h"
#include "../helpers.h"
#include "../display.h"
#include "../divelist.h"
#include "mainwindow.h"
#include <cstdlib>
#include <QThread>
#include <QDebug>
#include <QStringListModel>
struct product {
const char *product;
dc_descriptor_t *descriptor;
struct product *next;
};
struct vendor {
const char *vendor;
struct product *productlist;
struct vendor *next;
};
struct mydescriptor {
const char *vendor;
const char *product;
dc_family_t type;
unsigned int model;
};
namespace DownloadFromDcGlobal{
const char *err_string;
};
DownloadFromDCWidget *DownloadFromDCWidget::instance()
{
static DownloadFromDCWidget *dialog = new DownloadFromDCWidget();
dialog->setAttribute(Qt::WA_QuitOnClose, false);
return dialog;
}
DownloadFromDCWidget::DownloadFromDCWidget(QWidget* parent, Qt::WindowFlags f) :
QDialog(parent, f), ui(new Ui::DownloadFromDiveComputer), thread(0), downloading(false)
{
ui->setupUi(this);
ui->progressBar->hide();
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(100);
fill_computer_list();
vendorModel = new QStringListModel(vendorList);
ui->vendor->setModel(vendorModel);
if (default_dive_computer_vendor) {
ui->vendor->setCurrentIndex(ui->vendor->findText(default_dive_computer_vendor));
productModel = new QStringListModel(productList[default_dive_computer_vendor]);
ui->product->setModel(productModel);
if (default_dive_computer_product)
ui->product->setCurrentIndex(ui->product->findText(default_dive_computer_product));
}
if (default_dive_computer_device)
ui->device->setText(default_dive_computer_device);
}
void DownloadFromDCWidget::runDialog()
{
// Since the DownloadDialog is only
// created once, we need to do put some starting code here
ui->progressBar->hide();
markChildrenAsEnabled();
exec();
}
void DownloadFromDCWidget::stoppedDownloading()
{
downloading = false;
}
void DownloadFromDCWidget::on_vendor_currentIndexChanged(const QString& vendor)
{
QAbstractItemModel *currentModel = ui->product->model();
if (!currentModel)
return;
productModel = new QStringListModel(productList[vendor]);
ui->product->setModel(productModel);
// Memleak - but deleting gives me a crash.
//currentModel->deleteLater();
}
void DownloadFromDCWidget::fill_computer_list()
{
dc_iterator_t *iterator = NULL;
dc_descriptor_t *descriptor = NULL;
struct mydescriptor *mydescriptor;
QStringList computer;
dc_descriptor_iterator(&iterator);
while (dc_iterator_next (iterator, &descriptor) == DC_STATUS_SUCCESS) {
const char *vendor = dc_descriptor_get_vendor(descriptor);
const char *product = dc_descriptor_get_product(descriptor);
if (!vendorList.contains(vendor))
vendorList.append(vendor);
if (!productList[vendor].contains(product))
productList[vendor].push_back(product);
descriptorLookup[QString(vendor) + QString(product)] = descriptor;
}
dc_iterator_free(iterator);
/* and add the Uemis Zurich which we are handling internally
THIS IS A HACK as we magically have a data structure here that
happens to match a data structure that is internal to libdivecomputer;
this WILL BREAK if libdivecomputer changes the dc_descriptor struct...
eventually the UEMIS code needs to move into libdivecomputer, I guess */
mydescriptor = (struct mydescriptor*) malloc(sizeof(struct mydescriptor));
mydescriptor->vendor = "Uemis";
mydescriptor->product = "Zurich";
mydescriptor->type = DC_FAMILY_NULL;
mydescriptor->model = 0;
if (!vendorList.contains("Uemis"))
vendorList.append("Uemis");
if (!productList["Uemis"].contains("Zurich"))
productList["Uemis"].push_back("Zurich");
descriptorLookup[QString("UemisZurich")] = (dc_descriptor_t *)mydescriptor;
}
void DownloadFromDCWidget::on_cancel_clicked()
{
import_thread_cancelled = true;
if (thread) {
thread->wait();
thread->deleteLater();
thread = 0;
}
// Confusing, but if the user press cancel during a download
// he probably want to cancel the download, not to close the window.
if (!downloading)
close();
}
void DownloadFromDCWidget::on_ok_clicked()
{
if (downloading)
return;
markChildrenAsDisabled();
ui->progressBar->setValue(0);
ui->progressBar->show();
if (thread) {
thread->deleteLater();
}
data.devname = strdup(ui->device->text().toUtf8().data());
data.vendor = strdup(ui->vendor->currentText().toUtf8().data());
data.product = strdup(ui->product->currentText().toUtf8().data());
data.descriptor = descriptorLookup[ui->vendor->currentText() + ui->product->currentText()];
data.force_download = ui->forceDownload->isChecked();
data.deviceid = data.diveid = 0;
set_default_dive_computer(data.vendor, data.product);
set_default_dive_computer_device(data.devname);
thread = new InterfaceThread(this, &data);
connect(thread, SIGNAL(updateInterface(int)),
ui->progressBar, SLOT(setValue(int)), Qt::QueuedConnection); // Qt::QueuedConnection == threadsafe.
connect(thread, SIGNAL(finished()), this, SLOT(close()));
thread->start();
downloading = true;
}
bool DownloadFromDCWidget::preferDownloaded()
{
return ui->preferDownloaded->isChecked();
}
void DownloadFromDCWidget::reject()
{
// we don't want the download window being able to close
// while we're still downloading.
if (!downloading)
QDialog::reject();
}
void DownloadFromDCWidget::markChildrenAsDisabled()
{
ui->device->setDisabled(true);
ui->vendor->setDisabled(true);
ui->product->setDisabled(true);
ui->forceDownload->setDisabled(true);
ui->preferDownloaded->setDisabled(true);
ui->ok->setDisabled(true);
ui->search->setDisabled(true);
}
void DownloadFromDCWidget::markChildrenAsEnabled()
{
ui->device->setDisabled(false);
ui->vendor->setDisabled(false);
ui->product->setDisabled(false);
ui->forceDownload->setDisabled(false);
ui->preferDownloaded->setDisabled(false);
ui->ok->setDisabled(false);
ui->search->setDisabled(false);
}
DownloadThread::DownloadThread(device_data_t* data): data(data)
{
}
void DownloadThread::run()
{
DownloadFromDCWidget *dfdcw = DownloadFromDCWidget::instance();
if (!strcmp(data->vendor, "Uemis"))
do_uemis_import(data->devname, data->force_download);
else
do_libdivecomputer_import(data);
process_dives(TRUE, dfdcw->preferDownloaded());
dfdcw->stoppedDownloading();
}
InterfaceThread::InterfaceThread(QObject* parent, device_data_t* data): QThread(parent), data(data)
{
}
void InterfaceThread::run()
{
DownloadThread *download = new DownloadThread(data);
MainWindow *w = mainWindow();
connect(download, SIGNAL(finished()), w, SLOT(refreshDisplay()));
download->start();
while (download->isRunning()) {
msleep(200);
updateInterface(progress_bar_fraction *100);
}
updateInterface(100);
}
|