2011-09-20 19:40:34 +00:00
|
|
|
/* main.c */
|
2011-08-31 01:40:25 +00:00
|
|
|
#include <stdio.h>
|
2011-09-02 23:40:28 +00:00
|
|
|
#include <string.h>
|
2011-08-31 01:40:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2011-09-08 18:23:11 +00:00
|
|
|
#include <gconf/gconf-client.h>
|
|
|
|
|
2011-08-31 01:40:25 +00:00
|
|
|
#include "dive.h"
|
2011-09-05 19:12:58 +00:00
|
|
|
#include "divelist.h"
|
2011-09-20 18:24:15 +00:00
|
|
|
|
2011-09-08 18:23:11 +00:00
|
|
|
GConfClient *gconf;
|
2011-09-07 02:07:17 +00:00
|
|
|
struct units output_units;
|
|
|
|
|
2011-09-15 16:43:14 +00:00
|
|
|
#define GCONF_NAME(x) "/apps/subsurface/" #x
|
2011-09-08 18:23:11 +00:00
|
|
|
|
2011-09-20 19:40:34 +00:00
|
|
|
/* random helper functions, used here or elsewhere */
|
2011-08-31 01:40:25 +00:00
|
|
|
static int sortfn(const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
const struct dive *a = *(void **)_a;
|
|
|
|
const struct dive *b = *(void **)_b;
|
|
|
|
|
|
|
|
if (a->when < b->when)
|
|
|
|
return -1;
|
|
|
|
if (a->when > b->when)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-20 19:40:34 +00:00
|
|
|
const char *weekday(int wday)
|
|
|
|
{
|
|
|
|
static const char wday_array[7][4] = {
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
};
|
|
|
|
return wday_array[wday];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *monthname(int mon)
|
|
|
|
{
|
|
|
|
static const char month_array[12][4] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
2011-09-21 12:44:34 +00:00
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
2011-09-20 19:40:34 +00:00
|
|
|
};
|
|
|
|
return month_array[mon];
|
|
|
|
}
|
|
|
|
|
2011-08-31 02:48:00 +00:00
|
|
|
/*
|
|
|
|
* This doesn't really report anything at all. We just sort the
|
|
|
|
* dives, the GUI does the reporting
|
|
|
|
*/
|
2011-09-12 20:25:05 +00:00
|
|
|
void report_dives(void)
|
2011-08-31 01:40:25 +00:00
|
|
|
{
|
2011-09-02 23:40:28 +00:00
|
|
|
int i;
|
|
|
|
|
2011-08-31 01:40:25 +00:00
|
|
|
qsort(dive_table.dives, dive_table.nr, sizeof(struct dive *), sortfn);
|
2011-09-02 23:40:28 +00:00
|
|
|
|
|
|
|
for (i = 1; i < dive_table.nr; i++) {
|
|
|
|
struct dive **pp = &dive_table.dives[i-1];
|
|
|
|
struct dive *prev = pp[0];
|
|
|
|
struct dive *dive = pp[1];
|
|
|
|
struct dive *merged;
|
|
|
|
|
|
|
|
if (prev->when + prev->duration.seconds < dive->when)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
merged = try_to_merge(prev, dive);
|
|
|
|
if (!merged)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
free(prev);
|
|
|
|
free(dive);
|
|
|
|
*pp = merged;
|
|
|
|
dive_table.nr--;
|
|
|
|
memmove(pp+1, pp+2, sizeof(*pp)*(dive_table.nr - i));
|
|
|
|
|
|
|
|
/* Redo the new 'i'th dive */
|
|
|
|
i--;
|
|
|
|
}
|
2011-08-31 01:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_argument(const char *arg)
|
|
|
|
{
|
|
|
|
const char *p = arg+1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
switch (*p) {
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Bad argument '%s'\n", arg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} while (*++p);
|
|
|
|
}
|
|
|
|
|
2011-09-10 00:10:17 +00:00
|
|
|
void update_dive(struct dive *new_dive)
|
|
|
|
{
|
|
|
|
static struct dive *buffered_dive;
|
|
|
|
struct dive *old_dive = buffered_dive;
|
|
|
|
|
|
|
|
if (old_dive) {
|
|
|
|
flush_dive_info_changes(old_dive);
|
|
|
|
flush_dive_equipment_changes(old_dive);
|
2011-09-20 17:06:24 +00:00
|
|
|
flush_divelist(old_dive);
|
2011-09-10 00:10:17 +00:00
|
|
|
}
|
|
|
|
if (new_dive) {
|
|
|
|
show_dive_info(new_dive);
|
|
|
|
show_dive_equipment(new_dive);
|
|
|
|
}
|
2011-09-11 20:43:37 +00:00
|
|
|
buffered_dive = new_dive;
|
2011-09-10 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 19:40:34 +00:00
|
|
|
void renumber_dives(int nr)
|
2011-09-11 22:39:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < dive_table.nr; i++) {
|
|
|
|
struct dive *dive = dive_table.dives[i];
|
|
|
|
dive->number = nr + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 01:40:25 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-09-07 02:07:17 +00:00
|
|
|
output_units = SI_units;
|
2011-09-27 17:16:40 +00:00
|
|
|
|
2011-08-31 01:40:25 +00:00
|
|
|
parse_xml_init();
|
|
|
|
|
2011-09-20 19:40:34 +00:00
|
|
|
init_ui(argc, argv);
|
2011-09-05 20:14:53 +00:00
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
const char *a = argv[i];
|
|
|
|
|
|
|
|
if (a[0] == '-') {
|
|
|
|
parse_argument(a);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GError *error = NULL;
|
|
|
|
parse_xml_file(a, &error);
|
|
|
|
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
report_error(error);
|
|
|
|
g_error_free(error);
|
|
|
|
error = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
report_dives();
|
2011-09-20 17:06:24 +00:00
|
|
|
dive_list_update_dives();
|
2011-08-31 02:48:00 +00:00
|
|
|
|
2011-09-20 19:40:34 +00:00
|
|
|
run_ui();
|
2011-08-31 01:40:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|