Subversion Repositories public

Rev

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