Subversion Repositories public

Rev

Rev 149 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 232
Line 1... Line 1...
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) 2007, 2008 by Andreas Theofilu                          *
2
 *   Copyright (C) 2007 - 2009 by Andreas Theofilu                         *
3
 *   andreas@theosys.at                                                    *
3
 *   andreas@theosys.at                                                    *
4
 *                                                                         *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation version 3 of the License.                *
7
 *   the Free Software Foundation version 3 of the License.                *
Line 15... Line 15...
15
 *   along with this program; if not, write to the                         *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
18
 ***************************************************************************/
19
 
19
 
-
 
20
/*
-
 
21
 * Ported to KDE4 on Jul. 8 2009 by Andreas Theofilu.
-
 
22
 */
20
 
23
 
21
#include "progresswidget.h"
24
#include "progresswidget.h"
22
#include <kapplication.h>
25
#include <kapplication.h>
23
#include <kprogress.h>
-
 
24
#include <kmessagebox.h>
26
#include <kmessagebox.h>
25
#include <ksimpleconfig.h>
27
#include <KConfig>
26
#include <klocale.h>
28
#include <klocale.h>
27
#include <qlabel.h>
29
#include <qlabel.h>
-
 
30
#include <QProgressBar>
28
#include "garmin.h"
31
#include "garmin.h"
-
 
32
#include "../libgant/gant.h"
29
 
33
 
30
#include <iostream>
34
#include <iostream>
31
 
35
 
32
using std::cout;
36
using std::cout;
33
using std::endl;
37
using std::endl;
34
 
38
 
35
extern KApplication *mapp;
39
extern KApplication *mapp;
36
progressWidget *me;
40
progressWidget *me;
37
 
41
 
38
progressWidget::progressWidget ( QWidget* parent, const char* name, bool modal, WFlags fl )
42
progressWidget::progressWidget ( QWidget* parent, Qt::WFlags fl )
39
		: progressWidgetBase ( parent,name, modal,fl )
43
		: QDialog(parent, fl), Ui::Progress ()
40
{
44
{
-
 
45
	setupUi (this);
41
	step = 0;
46
	step = 0;
42
	psteps = 7;
47
	psteps = 7;
43
	me = this;
48
	me = this;
44
}
49
}
45
 
50
 
Line 50... Line 55...
50
 
55
 
51
/*
56
/*
52
 * This function is called from the Garmin library libgarmin.a during
57
 * This function is called from the Garmin library libgarmin.a during
53
 * reading data from a GPS device.
58
 * reading data from a GPS device.
54
 */
59
 */
55
void progressWidget::CallBack(char *str)
60
void progressWidget::CallBack(char *)
56
{
61
{
57
	if (me)
62
	if (me)
58
	   me->IncrementBar();
63
	   me->IncrementBar();
59
}
64
}
60
 
65
 
Line 63... Line 68...
63
	step++;
68
	step++;
64
 
69
 
65
	if (psteps <= step)
70
	if (psteps <= step)
66
	{
71
	{
67
	   psteps += 2;
72
	   psteps += 2;
68
	   barProgress->setTotalSteps(psteps);
73
	   barProgress->setRange(0, psteps);
69
	}
74
	}
70
 
75
 
71
	barProgress->setProgress(step);
76
	barProgress->setValue(step);
72
}
77
}
73
 
78
 
74
/*$SPECIALIZATION$*/
79
/*$SPECIALIZATION$*/
75
/*
80
/*
76
 * The following function reads out all data from a GPS device. It uses
81
 * The following function reads out all data from a GPS device. It uses
Line 98... Line 103...
98
uint32              l_lap;
103
uint32              l_lap;
99
uint32              l_idx;
104
uint32              l_idx;
100
time_type           start;
105
time_type           start;
101
time_t              start_time;
106
time_t              start_time;
102
char                filename[BUFSIZ];
107
char                filename[BUFSIZ];
103
bool		    Serial;
108
bool		    Serial, Forerunner;
104
struct tm *         tbuf;
109
struct tm *         tbuf;
105
 
110
 
106
	// Find the data path in config file
111
	// Find the data path in config file
107
	KSimpleConfig *cfg = new KSimpleConfig(QString("sportwatcher.rc"), true);
112
	KConfig cfg (QString("sportwatcher.rc"), KConfig::SimpleConfig);
108
	cfg->setGroup(QString("SportWatcher"));
113
	KConfigGroup ic (&cfg, "SportWatcher");
109
	Data = cfg->readEntry("Data");
114
	Data = ic.readEntry("Data", QString("~/.sportwatcher"));
110
	device = cfg->readEntry("Device");
115
	device = ic.readEntry("Device", QString("/dev/ttyUSB1"));
111
	Serial = cfg->readBoolEntry("Serial");
116
	Serial = ic.readEntry("Serial", false);
112
	delete cfg;
117
	Forerunner = ic.readEntry("Forerunner", false);
113
 
118
 
114
	if (Data.isEmpty())
119
	if (Data.isEmpty())
115
	{
120
	{
116
	   KMessageBox::error(this, i18n("No data path was set in the settings!"));
121
	   KMessageBox::error(this, i18n("No data path was set in the settings!"));
117
//	   done(QDialog::Rejected);
-
 
118
	   return false;
122
	   return false;
119
	}
123
	}
120
 
124
 
121
	if (!isVisible())
125
	if (!isVisible())
122
	   setShown(true);
126
	   setShown(true);
123
 
127
 
124
	barProgress->setTotalSteps(psteps);
128
	barProgress->setRange(0, psteps);
125
	step = 0;
129
	step = 0;
126
	garmin_set_device(device.ascii());
130
	garmin_set_device(device.toAscii());
127
	garmin_set_method((Serial) ? 1 : 0);		// Setting to 1 doesn't work!! (reading over garmin_gps kernel module)
131
	garmin_set_method((Serial) ? 1 : 0);		// Setting to 1 doesn't work!! (reading over garmin_gps kernel module)
128
	// Initialize a callback function to have a real progress bar
132
	// Initialize a callback function to have a real progress bar
129
	garmin_set_hook(&progressWidget::CallBack);
133
	garmin_set_hook(&progressWidget::CallBack);
130
	lblInfo->setText(i18n("Looking for a Garmin GPS device..."));
-
 
131
 
134
 
-
 
135
	/*
-
 
136
	 * In case we should read the data out of a Forerunner 50 or
-
 
137
	 * Forerunner 405 or similar, we must do that with the help of
-
 
138
	 * the ANT library. It inserts everything into the garmin structures,
-
 
139
	 * so we can use the same procedure for every device.
-
 
140
	 */
132
	if (garmin_init(&garmin, 0) == 0)
141
	if (Serial && Forerunner)	// Forerunner 50?
133
	{
142
	{
134
	   garmin_close(&garmin);
143
	gant *ga;
135
	   garmin_clear_hook();
144
	uchar *buffer;
136
	   return false;
145
	int size;
-
 
146
	QString auth = Data + QString("/.auth50");
137
	}
147
 
-
 
148
	   ga = new gant(auth);		// Authority file
-
 
149
	   ga->setDevice(device);	// Special device (serial port)
-
 
150
	   ga->read_device ();		// FIXME: Parameter to tell the type of Forerunner
138
 
151
 
139
	barProgress->setProgress(++step);
152
	   if ((buffer = ga->getBuffer (&size)) == 0)
-
 
153
	   {
140
	lblInfo->setText(i18n("Extracting data from Garmin <i>") + QString(garmin.product.product_description) + QString("</i>"));
154
	      delete ga;
141
	lblReading->setText(i18n("Please wait, this may take some time!"));
155
	      return false;
-
 
156
	   }
142
 
157
 
-
 
158
	   // FIXME: Here we should decode the data buffer
143
	mapp->processEvents();
159
	   delete ga;
-
 
160
	}
-
 
161
/*	else if (Serial && !Forerunner)	// Forerunner 405
-
 
162
	{
-
 
163
	gant *ga;
-
 
164
	QString auth = Data + QString("/.auth405");
144
 
165
 
145
	if ((data = garmin_get(&garmin, GET_RUNS)) != NULL )
166
	   ga = new gant(auth);		// Authority file
-
 
167
	   ga->setDevice(device);	// Special device (serial port)
-
 
168
	   ga->read_device ();		// FIXME: Parameter to tell the type of Forerunner
-
 
169
	   // FIXME: Here we need a function to get the data buffer
-
 
170
	   delete ga;
-
 
171
	} */
-
 
172
	else	// We read from a Forerunner 305 or compatible
146
	{
173
	{
-
 
174
	   lblInfo->setText(i18n("Looking for a Garmin GPS device..."));
147
	   /*
175
 
148
	    * We should have a list with three elements:
176
	   if (garmin_init(&garmin, 0) == 0)
149
	    *
177
	   {
150
	    * 1) The runs (which identify the track and lap indices)
178
	      garmin_close(&garmin);
151
	    * 2) The laps (which are related to the runs)
179
	      garmin_clear_hook();
152
	    * 3) The tracks (which are related to the runs)
180
	      return false;
153
	    */
181
	   }
-
 
182
 
-
 
183
	   barProgress->setValue(++step);
-
 
184
	   lblInfo->setText(i18n("Extracting data from Garmin <i>") + QString(garmin.product.product_description) + QString("</i>"));
-
 
185
	   lblReading->setText(i18n("Please wait, this may take some time!"));
154
 
186
 
155
	   barProgress->setProgress(++step);
-
 
156
	   lblReading->setText(i18n("Sorting out runs ..."));
-
 
157
	   mapp->processEvents();
-
 
158
	   data0 = garmin_list_data(data, 0);
-
 
159
	   barProgress->setProgress(++step);
-
 
160
	   lblReading->setText(i18n("Sorting out laps ..."));
-
 
161
	   mapp->processEvents();
-
 
162
	   data1 = garmin_list_data(data, 1);
-
 
163
	   barProgress->setProgress(++step);
-
 
164
	   lblReading->setText(i18n("Sorting out tracks ..."));
-
 
165
	   mapp->processEvents();
-
 
166
	   data2 = garmin_list_data(data, 2);
-
 
167
	   barProgress->setProgress(++step);
-
 
168
	   lblReading->setText(i18n("Sorting out data blocks done."));
-
 
169
	   mapp->processEvents();
187
	   mapp->processEvents();
170
 
188
 
171
	   if ( data0 != NULL && (runs   = (garmin_list *)data0->data) != NULL &&
189
	   if ((data = garmin_get(&garmin, GET_RUNS)) != NULL )
172
		data1 != NULL && (laps   = (garmin_list *)data1->data) != NULL &&
-
 
173
		data2 != NULL && (tracks = (garmin_list *)data2->data) != NULL )
-
 
174
	   {
190
	   {
-
 
191
	      /*
-
 
192
	       * We should have a list with three elements:
-
 
193
	       *
-
 
194
	       * 1) The runs (which identify the track and lap indices)
-
 
195
	       * 2) The laps (which are related to the runs)
-
 
196
	       * 3) The tracks (which are related to the runs)
-
 
197
	       */
175
 
198
 
-
 
199
	      barProgress->setValue(++step);
-
 
200
	      lblReading->setText(i18n("Sorting out runs ..."));
-
 
201
	      mapp->processEvents();
176
	      /* For each run, get its laps and track points. */
202
	      data0 = garmin_list_data(data, 0);
-
 
203
	      barProgress->setValue(++step);
177
	      /* But first see how much runs to set the progress bar */
204
	      lblReading->setText(i18n("Sorting out laps ..."));
-
 
205
	      mapp->processEvents();
-
 
206
	      data1 = garmin_list_data(data, 1);
178
	      for (n = runs->head; n != NULL; n = n->next)
207
	      barProgress->setValue(++step);
-
 
208
	      lblReading->setText(i18n("Sorting out tracks ..."));
179
		 psteps++;
209
	      mapp->processEvents();
180
	
-
 
-
 
210
	      data2 = garmin_list_data(data, 2);
181
	      barProgress->setTotalSteps(psteps);
211
	      barProgress->setValue(++step);
-
 
212
	      lblReading->setText(i18n("Sorting out data blocks done."));
182
	      mapp->processEvents();
213
	      mapp->processEvents();
183
 
214
 
184
	      for (n = runs->head; n != NULL; n = n->next)
215
	      if ( data0 != NULL && (runs   = (garmin_list *)data0->data) != NULL &&
-
 
216
		data1 != NULL && (laps   = (garmin_list *)data1->data) != NULL &&
-
 
217
		data2 != NULL && (tracks = (garmin_list *)data2->data) != NULL )
185
	      {
218
	      {
-
 
219
 
-
 
220
		 /* For each run, get its laps and track points. */
-
 
221
		 /* But first see how much runs to set the progress bar */
-
 
222
		 for (n = runs->head; n != NULL; n = n->next)
-
 
223
		    psteps++;
-
 
224
	
186
		 barProgress->setProgress(++step);
225
		 barProgress->setRange(0, psteps);
187
		 mapp->processEvents();
226
		 mapp->processEvents();
188
 
227
 
189
		 if (get_run_track_lap_info(n->data, &trk, &f_lap, &l_lap) != 0)
228
		 for (n = runs->head; n != NULL; n = n->next)
190
		 {
229
		 {
191
		    lblReading->setText(i18n("Running: track ") + QString("%1, ").arg(trk) + i18n("laps ") + QString("%1:%1").arg(f_lap).arg(l_lap));
230
		    barProgress->setValue(++step);
192
		    mapp->processEvents();
231
		    mapp->processEvents();
193
		    start = 0;
-
 
194
 
-
 
195
		    /* Get the laps. */
-
 
196
		    rlaps = garmin_alloc_data(data_Dlist);
-
 
197
 
232
 
198
		    for (m = laps->head; m != NULL; m = m->next)
233
		    if (get_run_track_lap_info(n->data, &trk, &f_lap, &l_lap) != 0)
199
		    {
234
		    {
-
 
235
		       lblReading->setText(i18n("Running: track ") + QString("%1, ").arg(trk) + i18n("laps ") + QString("%1:%1").arg(f_lap).arg(l_lap));
-
 
236
		       mapp->processEvents();
-
 
237
		       start = 0;
-
 
238
 
-
 
239
		       /* Get the laps. */
200
		       if (get_lap_index(m->data, &l_idx) != 0)
240
		       rlaps = garmin_alloc_data(data_Dlist);
-
 
241
 
-
 
242
		       for (m = laps->head; m != NULL; m = m->next)
201
		       {
243
		       {
202
			  if ( l_idx >= f_lap && l_idx <= l_lap )
244
			  if (get_lap_index(m->data, &l_idx) != 0)
203
			  {
245
			  {
-
 
246
			     if ( l_idx >= f_lap && l_idx <= l_lap )
-
 
247
			     {
204
			     garmin_list_append((garmin_list *)rlaps->data, m->data);
248
			        garmin_list_append((garmin_list *)rlaps->data, m->data);
205
 
249
 
206
			     if ( l_idx == f_lap )
250
			        if ( l_idx == f_lap )
207
			        get_lap_start_time(m->data, &start);
251
				   get_lap_start_time(m->data, &start);
-
 
252
			     }
208
			  }
253
			  }
209
		       }
254
		       }
210
		    }
-
 
211
 
255
 
212
		    /* Get the track points. */
256
		       /* Get the track points. */
213
 
257
 
214
		    rtracks = get_track(tracks,trk);
258
		       rtracks = get_track(tracks,trk);
215
 
259
 
216
		    /* Now make a three-element list for this run. */
260
		       /* Now make a three-element list for this run. */
217
		    rlist = garmin_alloc_data(data_Dlist);
261
		       rlist = garmin_alloc_data(data_Dlist);
218
		    garmin_list_append((garmin_list *)rlist->data,n->data);
262
		       garmin_list_append((garmin_list *)rlist->data,n->data);
219
		    garmin_list_append((garmin_list *)rlist->data,rlaps);
263
		       garmin_list_append((garmin_list *)rlist->data,rlaps);
220
		    garmin_list_append((garmin_list *)rlist->data,rtracks);
264
		       garmin_list_append((garmin_list *)rlist->data,rtracks);
221
 
265
 
222
		    /*
266
		       /*
223
		     * Determine the filename based on the start time of the first lap
267
		        * Determine the filename based on the start time of the first lap
224
		     */
268
		        */
225
 
269
 
226
		    if ((start_time = start) != 0)
270
		       if ((start_time = start) != 0)
227
		    {
271
		       {
228
		       tbuf = localtime(&start_time);
272
			  QByteArray ba = Data.toAscii();
229
		       filepath.sprintf("%s/%d/%02d",
-
 
230
			  Data.ascii(), tbuf->tm_year+1900, tbuf->tm_mon + 1);
-
 
231
		       strftime(filename, sizeof(filename), "%Y%m%dT%H%M%S.gmn", tbuf);
-
 
232
 
-
 
233
		       /* Save rlist to the file. */
-
 
234
		       garmin_save(rlist, filename, filepath.ascii());
-
 
235
		       lblReading->setText(i18n("Saved file ") + QString(filename) + i18n(" successfully."));
-
 
236
		       mapp->processEvents();
-
 
237
		    }
-
 
238
	            else
-
 
239
		       KMessageBox::error(this, i18n("Start time of first lap not found!"));
-
 
240
 
273
 
-
 
274
			  tbuf = localtime(&start_time);
-
 
275
			  filepath.sprintf("%s/%d/%02d",
-
 
276
			     ba.data(), tbuf->tm_year+1900, tbuf->tm_mon + 1);
-
 
277
			  strftime(filename, sizeof(filename), "%Y%m%dT%H%M%S.gmn", tbuf);
-
 
278
 
241
		    /* Free the temporary lists we were using. */
279
			  /* Save rlist to the file. */
-
 
280
			  garmin_save(rlist, filename, filepath.toAscii());
-
 
281
			  lblReading->setText(i18n("Saved file ") + QString(filename) + i18n(" successfully."));
-
 
282
			  mapp->processEvents();
-
 
283
		       }
-
 
284
	               else
-
 
285
			  KMessageBox::error(this, i18n("Start time of first lap not found!"));
242
 
286
 
243
		    if (rlaps != NULL)
-
 
244
		    {
-
 
245
		       garmin_free_list_only((garmin_list *)rlaps->data);
287
		       /* Free the temporary lists we were using. */
246
		       free(rlaps);
-
 
247
		    }
-
 
248
 
288
 
249
		    if (rtracks != NULL)
289
		       if (rlaps != NULL)
250
		    {
290
		       {
251
		       garmin_free_list_only((garmin_list *)rtracks->data);
291
			  garmin_free_list_only((garmin_list *)rlaps->data);
252
		       free(rtracks);
292
			  free(rlaps);
253
		    }
293
		       }
254
 
294
 
-
 
295
		       if (rtracks != NULL)
-
 
296
		       {
-
 
297
			  garmin_free_list_only((garmin_list *)rtracks->data);
-
 
298
			  free(rtracks);
-
 
299
		       }
-
 
300
 
255
		    if (rlist != NULL)
301
		       if (rlist != NULL)
256
		    {
302
		       {
257
		       garmin_free_list_only((garmin_list *)rlist->data);
303
			  garmin_free_list_only((garmin_list *)rlist->data);
258
		       free(rlist);
304
			  free(rlist);
-
 
305
		       }
259
		    }
306
		    }
260
		 }
307
		 }
261
	      }
308
	      }
-
 
309
	      else
-
 
310
	      {
-
 
311
		 if (data0 == NULL)
-
 
312
		    KMessageBox::error(this, i18n("Toplevel data missing element 0 (runs)"));
-
 
313
		 else if (runs == NULL)
-
 
314
		    KMessageBox::error(this, i18n("No runs extracted!"));
-
 
315
 
-
 
316
		 if ( data1 == NULL )
-
 
317
		    KMessageBox::error(this, i18n("Toplevel data missing element 1 (laps)"));
-
 
318
		 else if (laps == NULL)
-
 
319
		    KMessageBox::error(this, i18n("No laps extracted!"));
-
 
320
 
-
 
321
		 if (data2 == NULL)
-
 
322
		    KMessageBox::error(this, i18n("Toplevel data missing element 2 (tracks)"));
-
 
323
		 else if (tracks == NULL)
-
 
324
		    KMessageBox::error(this, i18n("No tracks extracted!"));
-
 
325
	      }
-
 
326
 
-
 
327
	      garmin_free_data(data);
262
	   }
328
	   }
263
	   else
329
	   else
264
	   {
-
 
265
              if (data0 == NULL)
-
 
266
		 KMessageBox::error(this, i18n("Toplevel data missing element 0 (runs)"));
-
 
267
	      else if (runs == NULL)
-
 
268
		 KMessageBox::error(this, i18n("No runs extracted!"));
-
 
269
 
-
 
270
	      if ( data1 == NULL )
-
 
271
		 KMessageBox::error(this, i18n("Toplevel data missing element 1 (laps)"));
-
 
272
	      else if (laps == NULL)
-
 
273
		 KMessageBox::error(this, i18n("No laps extracted!"));
330
	      KMessageBox::error(this, i18n("Unable to extract any data!"));
274
 
-
 
275
	      if (data2 == NULL)
-
 
276
		 KMessageBox::error(this, i18n("Toplevel data missing element 2 (tracks)"));
-
 
277
	      else if (tracks == NULL)
-
 
278
		 KMessageBox::error(this, i18n("No tracks extracted!"));
-
 
279
	   }
-
 
280
 
-
 
281
	   garmin_free_data(data);
-
 
282
	}
331
	}
283
	else
-
 
284
	   KMessageBox::error(this, i18n("Unable to extract any data!"));
-
 
285
 
332
 
286
	mapp->processEvents();
333
	mapp->processEvents();
287
	garmin_close(&garmin);
334
	garmin_close(&garmin);
288
	garmin_clear_hook();
335
	garmin_clear_hook();
289
 
336