Subversion Repositories public

Rev

Rev 137 | Rev 149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
95 andreas 1
/***************************************************************************
119 andreas 2
 *   Copyright (C) 2007, 2008 by Andreas Theofilu                          *
95 andreas 3
 *   andreas@theosys.at                                                    *
4
 *                                                                         *
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  *
7
 *   the Free Software Foundation version 3 of the License.                *
8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
19
 
20
 
21
#include "progresswidget.h"
132 andreas 22
#include <kapplication.h>
95 andreas 23
#include <kprogress.h>
24
#include <kmessagebox.h>
25
#include <ksimpleconfig.h>
26
#include <klocale.h>
27
#include <qlabel.h>
28
#include "garmin.h"
29
 
137 andreas 30
#include <iostream>
132 andreas 31
 
137 andreas 32
using std::cout;
33
using std::endl;
34
 
132 andreas 35
extern KApplication *mapp;
143 andreas 36
progressWidget *me;
132 andreas 37
 
95 andreas 38
progressWidget::progressWidget ( QWidget* parent, const char* name, bool modal, WFlags fl )
39
		: progressWidgetBase ( parent,name, modal,fl )
40
{
143 andreas 41
	step = 0;
42
	psteps = 7;
43
	me = this;
95 andreas 44
}
45
 
46
progressWidget::~progressWidget()
47
{
143 andreas 48
	me = 0;
95 andreas 49
}
50
 
143 andreas 51
/*
52
 * This function is called from the Garmin library libgarmin.a during
53
 * reading data from a GPS device.
54
 */
137 andreas 55
void progressWidget::CallBack(char *str)
56
{
143 andreas 57
	if (me)
58
	   me->IncrementBar();
137 andreas 59
}
60
 
143 andreas 61
void progressWidget::IncrementBar()
62
{
63
	step++;
64
 
65
	if (psteps <= step)
66
	{
67
	   psteps += 2;
68
	   barProgress->setTotalSteps(psteps);
69
	}
70
 
71
	barProgress->setProgress(step);
72
}
73
 
95 andreas 74
/*$SPECIALIZATION$*/
143 andreas 75
/*
76
 * The following function reads out all data from a GPS device. It uses
77
 * the library libgarmin.a, who is basicly the "garmintools-0.5", written by
78
 * Dave Bailey (thanks Dave!).
79
 */
137 andreas 80
bool progressWidget::Download()
95 andreas 81
{
100 andreas 82
QString Data, filepath, device;
95 andreas 83
garmin_unit         garmin;
84
garmin_data *       data;
85
garmin_data *       data0;
86
garmin_data *       data1;
87
garmin_data *       data2;
88
garmin_data *       rlaps;
89
garmin_data *       rtracks;
90
garmin_list *       runs   = NULL;
91
garmin_list *       laps   = NULL;
92
garmin_list *       tracks = NULL;
93
garmin_data *       rlist;
94
garmin_list_node *  n;
95
garmin_list_node *  m;
96
uint32              trk;
97
uint32              f_lap;
98
uint32              l_lap;
99
uint32              l_idx;
100
time_type           start;
101
time_t              start_time;
102
char                filename[BUFSIZ];
103
struct tm *         tbuf;
104
 
105
	// Find the data path in config file
106
	KSimpleConfig *cfg = new KSimpleConfig(QString("sportwatcher.rc"), true);
107
	cfg->setGroup(QString("SportWatcher"));
108
	Data = cfg->readEntry("Data");
100 andreas 109
	device = cfg->readEntry("Device");
95 andreas 110
	delete cfg;
111
 
112
	if (Data.isEmpty())
113
	{
114
	   KMessageBox::error(this, i18n("No data path was set in the settings!"));
137 andreas 115
//	   done(QDialog::Rejected);
116
	   return false;
95 andreas 117
	}
118
 
100 andreas 119
	if (!isVisible())
132 andreas 120
	   setShown(true);
100 andreas 121
 
143 andreas 122
	barProgress->setTotalSteps(psteps);
123
	step = 0;
100 andreas 124
	garmin_set_device(device.ascii());
143 andreas 125
	garmin_set_method(0);		// Setting to 1 doesn't work!! (reading over garmin_gps kernel module)
126
	// Initialize a callback function to have a real progress bar
137 andreas 127
	garmin_set_hook(&progressWidget::CallBack);
143 andreas 128
	lblInfo->setText(i18n("Looking for a Garmin GPS device..."));
100 andreas 129
 
95 andreas 130
	if (garmin_init(&garmin, 0) == 0)
131
	{
100 andreas 132
	   garmin_close(&garmin);
137 andreas 133
	   garmin_clear_hook();
134
	   return false;
95 andreas 135
	}
136
 
143 andreas 137
	barProgress->setProgress(++step);
132 andreas 138
	lblInfo->setText(i18n("Extracting data from Garmin <i>") + QString(garmin.product.product_description) + QString("</i>"));
139
	lblReading->setText(i18n("Please wait, this may take some time!"));
95 andreas 140
 
132 andreas 141
	mapp->processEvents();
142
 
95 andreas 143
	if ((data = garmin_get(&garmin, GET_RUNS)) != NULL )
144
	{
145
	   /*
146
	    * We should have a list with three elements:
147
	    *
148
	    * 1) The runs (which identify the track and lap indices)
149
	    * 2) The laps (which are related to the runs)
150
	    * 3) The tracks (which are related to the runs)
151
	    */
152
 
143 andreas 153
	   barProgress->setProgress(++step);
132 andreas 154
	   lblReading->setText(i18n("Sorting out runs ..."));
155
	   mapp->processEvents();
95 andreas 156
	   data0 = garmin_list_data(data, 0);
143 andreas 157
	   barProgress->setProgress(++step);
132 andreas 158
	   lblReading->setText(i18n("Sorting out laps ..."));
159
	   mapp->processEvents();
95 andreas 160
	   data1 = garmin_list_data(data, 1);
143 andreas 161
	   barProgress->setProgress(++step);
132 andreas 162
	   lblReading->setText(i18n("Sorting out tracks ..."));
163
	   mapp->processEvents();
95 andreas 164
	   data2 = garmin_list_data(data, 2);
143 andreas 165
	   barProgress->setProgress(++step);
132 andreas 166
	   lblReading->setText(i18n("Sorting out data blocks done."));
167
	   mapp->processEvents();
95 andreas 168
 
169
	   if ( data0 != NULL && (runs   = (garmin_list *)data0->data) != NULL &&
170
		data1 != NULL && (laps   = (garmin_list *)data1->data) != NULL &&
171
		data2 != NULL && (tracks = (garmin_list *)data2->data) != NULL )
172
	   {
173
 
174
	      /* For each run, get its laps and track points. */
175
	      /* But first see how much runs to set the progress bar */
176
	      for (n = runs->head; n != NULL; n = n->next)
177
		 psteps++;
178
 
179
	      barProgress->setTotalSteps(psteps);
132 andreas 180
	      mapp->processEvents();
95 andreas 181
 
182
	      for (n = runs->head; n != NULL; n = n->next)
183
	      {
143 andreas 184
		 barProgress->setProgress(++step);
132 andreas 185
		 mapp->processEvents();
95 andreas 186
 
187
		 if (get_run_track_lap_info(n->data, &trk, &f_lap, &l_lap) != 0)
188
		 {
189
		    lblReading->setText(i18n("Running: track ") + QString("%1, ").arg(trk) + i18n("laps ") + QString("%1:%1").arg(f_lap).arg(l_lap));
132 andreas 190
		    mapp->processEvents();
95 andreas 191
		    start = 0;
192
 
193
		    /* Get the laps. */
194
		    rlaps = garmin_alloc_data(data_Dlist);
195
 
196
		    for (m = laps->head; m != NULL; m = m->next)
197
		    {
198
		       if (get_lap_index(m->data, &l_idx) != 0)
199
		       {
200
			  if ( l_idx >= f_lap && l_idx <= l_lap )
201
			  {
202
			     garmin_list_append((garmin_list *)rlaps->data, m->data);
203
 
204
			     if ( l_idx == f_lap )
205
			        get_lap_start_time(m->data, &start);
206
			  }
207
		       }
208
		    }
209
 
210
		    /* Get the track points. */
211
 
212
		    rtracks = get_track(tracks,trk);
213
 
214
		    /* Now make a three-element list for this run. */
215
		    rlist = garmin_alloc_data(data_Dlist);
216
		    garmin_list_append((garmin_list *)rlist->data,n->data);
217
		    garmin_list_append((garmin_list *)rlist->data,rlaps);
218
		    garmin_list_append((garmin_list *)rlist->data,rtracks);
219
 
220
		    /*
221
		     * Determine the filename based on the start time of the first lap
222
		     */
223
 
224
		    if ((start_time = start) != 0)
225
		    {
226
		       tbuf = localtime(&start_time);
227
		       filepath.sprintf("%s/%d/%02d",
228
			  Data.ascii(), tbuf->tm_year+1900, tbuf->tm_mon + 1);
229
		       strftime(filename, sizeof(filename), "%Y%m%dT%H%M%S.gmn", tbuf);
230
 
231
		       /* Save rlist to the file. */
232
		       garmin_save(rlist, filename, filepath.ascii());
143 andreas 233
		       lblReading->setText(i18n("Saved file ") + QString(filename) + i18n(" successfully."));
132 andreas 234
		       mapp->processEvents();
95 andreas 235
		    }
143 andreas 236
	            else
237
		       KMessageBox::error(this, i18n("Start time of first lap not found!"));
238
 
95 andreas 239
		    /* Free the temporary lists we were using. */
240
 
241
		    if (rlaps != NULL)
242
		    {
243
		       garmin_free_list_only((garmin_list *)rlaps->data);
244
		       free(rlaps);
245
		    }
246
 
247
		    if (rtracks != NULL)
248
		    {
249
		       garmin_free_list_only((garmin_list *)rtracks->data);
250
		       free(rtracks);
251
		    }
252
 
253
		    if (rlist != NULL)
254
		    {
255
		       garmin_free_list_only((garmin_list *)rlist->data);
256
		       free(rlist);
257
		    }
258
		 }
259
	      }
260
	   }
261
	   else
262
	   {
263
              if (data0 == NULL)
264
		 KMessageBox::error(this, i18n("Toplevel data missing element 0 (runs)"));
265
	      else if (runs == NULL)
266
		 KMessageBox::error(this, i18n("No runs extracted!"));
267
 
268
	      if ( data1 == NULL )
269
		 KMessageBox::error(this, i18n("Toplevel data missing element 1 (laps)"));
270
	      else if (laps == NULL)
271
		 KMessageBox::error(this, i18n("No laps extracted!"));
272
 
273
	      if (data2 == NULL)
274
		 KMessageBox::error(this, i18n("Toplevel data missing element 2 (tracks)"));
275
	      else if (tracks == NULL)
276
		 KMessageBox::error(this, i18n("No tracks extracted!"));
277
	   }
278
 
279
	   garmin_free_data(data);
280
	}
281
	else
282
	   KMessageBox::error(this, i18n("Unable to extract any data!"));
283
 
132 andreas 284
	mapp->processEvents();
100 andreas 285
	garmin_close(&garmin);
137 andreas 286
	garmin_clear_hook();
287
 
288
	if (garmin_count_error() > 0)
289
	   return false;
290
 
291
	return true;
95 andreas 292
}
293
 
294
int progressWidget::get_run_track_lap_info ( garmin_data * run,
295
			 uint32 *      track_index,
296
			 uint32 *      first_lap_index,
297
			 uint32 *      last_lap_index )
298
{
299
  D1000 * d1000;
300
  D1009 * d1009;
301
  D1010 * d1010;
302
 
303
  int ok = 1;
304
 
305
  switch ( run->type ) {
306
  case data_D1000:
307
    d1000            = (D1000 *)run->data;
308
    *track_index     = d1000->track_index;
309
    *first_lap_index = d1000->first_lap_index;
310
    *last_lap_index  = d1000->last_lap_index;
311
    break;
312
  case data_D1009:
313
    d1009            = (D1009 *)run->data;
314
    *track_index     = d1009->track_index;
315
    *first_lap_index = d1009->first_lap_index;
316
    *last_lap_index  = d1009->last_lap_index;
317
    break;
318
  case data_D1010:
319
    d1010            = (D1010 *)run->data;
320
    *track_index     = d1010->track_index;
321
    *first_lap_index = d1010->first_lap_index;
322
    *last_lap_index  = d1010->last_lap_index;
323
    break;
324
  default:
132 andreas 325
    fprintf(stderr, "get_run_track_lap_info: run type %d invalid!\n",run->type);
95 andreas 326
    ok = 0;
327
    break;
328
  }
329
 
330
  return ok;
331
}
332
 
333
int progressWidget::get_lap_index ( garmin_data * lap, uint32 * lap_index )
334
{
335
  D1001 * d1001;
336
  D1011 * d1011;
337
  D1015 * d1015;
338
 
339
  int ok = 1;
340
 
341
  switch ( lap->type ) {
342
  case data_D1001:
343
    d1001      = (D1001 *)lap->data;
344
    *lap_index = d1001->index;
345
    break;
346
  case data_D1011:
347
    d1011      = (D1011 *)lap->data;
348
    *lap_index = d1011->index;
349
    break;
350
  case data_D1015:
351
    d1015      = (D1015 *)lap->data;
352
    *lap_index = d1015->index;
353
    break;
354
  default:
355
    fprintf(stderr, "get_lap_index: lap type %d invalid!\n",lap->type);
356
    ok = 0;
357
    break;
358
  }
359
 
360
  return ok;
361
}
362
 
363
 
364
int progressWidget::get_lap_start_time (garmin_data *lap, time_type *start_time)
365
{
366
  D1001 * d1001;
367
  D1011 * d1011;
368
  D1015 * d1015;
369
 
370
  int ok = 1;
371
 
372
  switch ( lap->type ) {
373
  case data_D1001:
374
    d1001       = (D1001 *)lap->data;
375
    *start_time = d1001->start_time + TIME_OFFSET;
376
    break;
377
  case data_D1011:
378
    d1011       = (D1011 *)lap->data;
379
    *start_time = d1011->start_time + TIME_OFFSET;
380
    break;
381
  case data_D1015:
382
    d1015       = (D1015 *)lap->data;
383
    *start_time = d1015->start_time + TIME_OFFSET;
384
    break;
385
  default:
132 andreas 386
    fprintf(stderr, "get_lap_start_time: lap type %d invalid!\n",lap->type);
95 andreas 387
    ok = 0;
388
    break;
389
  }
390
 
391
  return ok;
392
}
393
 
394
 
395
garmin_data *progressWidget::get_track(garmin_list *points, uint32 trk_index)
396
{
397
  garmin_list_node * n;
398
  garmin_data *      track = NULL;
399
  D311 *             d311;
400
  int                done = 0;
401
 
402
  /* Look for a data_D311 with an index that matches. */
403
 
404
  for ( n = points->head; n != NULL; n = n->next ) {    
405
    if ( n->data != NULL ) {
406
      switch ( n->data->type ) {
407
      case data_D311:
408
	if ( track == NULL ) {
409
	  d311 = (D311 *)n->data->data;
410
	  if ( d311->index == trk_index ) {
411
	    track = garmin_alloc_data(data_Dlist);
412
	    garmin_list_append((garmin_list *)track->data,n->data);
413
	  }
414
	} else {
415
	  /* We've reached the end of the track */
416
	  done = 1;
417
	}
418
	break;
419
      case data_D300:
420
      case data_D301:
421
      case data_D302:
422
      case data_D303:
423
      case data_D304:
424
	if ( track != NULL ) {
425
	  garmin_list_append((garmin_list *)track->data,n->data);
426
	}
427
	break;
428
      default:
132 andreas 429
	fprintf(stderr, "get_track: point type %d invalid!\n",n->data->type);
95 andreas 430
	break;
431
      }
432
    }
433
 
434
    if ( done != 0 ) break;
435
  }
436
 
437
  return track;
438
}
439
 
440
 
441
#include "progresswidget.moc"
442