moved CalendarFormat type def here

* cal-backend.h: moved CalendarFormat type def here

	* cal-backend.c (cal_backend_load): if extension suggests
	an ical file, attempt to load an iCal file.
	(cal_get_type_from_filename): returns CAL_ICAL if file
	extension is 'ics' or 'ifb', else returns CAL_VCAL
	(icalendar_calendar_load): moved this here from
	icalendar.c because it needs to call the static function
	add_object.

svn path=/trunk/; revision=1831
This commit is contained in:
Seth Alves
2000-02-17 21:30:09 +00:00
parent c975d4ef24
commit 4cd07c4e96
12 changed files with 309 additions and 117 deletions

View File

@ -1,3 +1,15 @@
2000-02-17 Seth Alves <alves@hungry.com>
* cal-backend.h: moved CalendarFormat type def here
* cal-backend.c (cal_backend_load): if extension suggests
an ical file, attempt to load an iCal file.
(cal_get_type_from_filename): returns CAL_ICAL if file
extension is 'ics' or 'ifb', else returns CAL_VCAL
(icalendar_calendar_load): moved this here from
icalendar.c because it needs to call the static function
add_object.
2000-02-17 Federico Mena Quintero <federico@helixcode.com>
* cal-client.c (cal_client_remove_object): Implemented.

View File

@ -20,19 +20,14 @@ endif
bin_PROGRAMS = gnomecal tlacuache $(extra_pilot_bins)
#if HAVE_LIBICAL
ICAL_INCLUDEDIR = -I../libical/src/libical
ICAL_SOURCES = icalendar.c
ICAL_LINK_FLAGS = ../libical/src/libical/libical.a
#endif
INCLUDES = \
-I$(includedir) \
$(GNOME_INCLUDEDIR) \
$(GNOME_CONDUIT_INCLUDEDIR) \
$(PISOCK_INCLUDEDIR) \
$(ICAL_INCLUDEDIR) \
-I../libical/src/libical \
-DGNOMELOCALEDIR=\""$(datadir)/locale"\"
GNOMECAL_CORBA_GENERATED = \
@ -99,6 +94,8 @@ gnomecal_SOURCES = \
gnome-cal.c \
gnome-cal.h \
goto.c \
icalendar.c \
icalendar.h \
layout.c \
layout.h \
main.c \
@ -118,8 +115,7 @@ gnomecal_SOURCES = \
view-utils.h \
view-utils.c \
year-view.c \
year-view.h \
$(ICAL_SOURCES)
year-view.h
calendar_pilot_sync_SOURCES = \
GnomeCal-common.c \
@ -148,6 +144,8 @@ tlacuache_SOURCES = \
cal-util.h \
calobj.c \
calobj.h \
icalendar.c \
icalendar.h \
job.c \
job.h \
timeutil.c \
@ -166,7 +164,8 @@ tlacuache_INCLUDES = \
tlacuache_LDADD = \
$(BONOBO_VFS_GNOME_LIBS) \
../libversit/libversit.la
../libversit/libversit.la \
$(ICAL_LINK_FLAGS)
calendar_pilot_sync_LDADD = \
$(PISOCK_LIBDIR) $(PISOCK_LIBS) \

View File

@ -24,6 +24,7 @@
#include "cal-backend.h"
#include "calobj.h"
#include "../libversit/vcc.h"
#include "icalendar.h"
@ -37,6 +38,9 @@ typedef struct {
/* URI where the calendar data is stored */
GnomeVFSURI *uri;
/* format of this calendar (ical or vcal) */
CalendarFormat format;
/* List of Cal objects with their listeners */
GList *clients;
@ -118,6 +122,9 @@ cal_backend_init (CalBackend *backend)
priv = g_new0 (CalBackendPrivate, 1);
backend->priv = priv;
/* FIXME can be CAL_VCAL or CAL_ICAL */
priv->format = CAL_VCAL;
}
/* Saves a calendar */
@ -550,6 +557,103 @@ cal_backend_add_cal (CalBackend *backend, Cal *cal)
priv->clients = g_list_prepend (priv->clients, cal);
}
static icalcomponent*
icalendar_parse_file (char* fname)
{
FILE* fp;
icalcomponent* comp = NULL;
gchar* str;
struct stat st;
int n;
fp = fopen (fname, "r");
if (!fp) {
g_warning ("Cannot open open calendar file.");
return NULL;
}
stat (fname, &st);
str = g_malloc (st.st_size + 2);
n = fread ((gchar*) str, 1, st.st_size, fp);
if (n != st.st_size) {
g_warning ("Read error.");
}
str[n] = '\0';
fclose (fp);
comp = icalparser_parse_string (str);
g_free (str);
return comp;
}
static void
icalendar_calendar_load (CalBackend * cal, char* fname)
{
icalcomponent *comp;
icalcomponent *subcomp;
iCalObject *ical;
comp = icalendar_parse_file (fname);
subcomp = icalcomponent_get_first_component (comp,
ICAL_ANY_COMPONENT);
while (subcomp) {
ical = ical_object_create_from_icalcomponent (subcomp);
if (ical->type != ICAL_EVENT &&
ical->type != ICAL_TODO &&
ical->type != ICAL_JOURNAL) {
g_warning ("Skipping unsupported iCalendar component.");
} else
add_object (cal, ical);
subcomp = icalcomponent_get_next_component (comp,
ICAL_ANY_COMPONENT);
}
}
/*
ics is to be used to designate a file containing (an arbitrary set of)
calendaring and scheduling information.
ifb is to be used to designate a file containing free or busy time
information.
anything else is assumed to be a vcal file.
*/
static CalendarFormat
cal_get_type_from_filename (char *str_uri)
{
int len;
if (str_uri == NULL)
return CAL_VCAL;
len = strlen (str_uri);
if (len < 5)
return CAL_VCAL;
if (str_uri[ len-4 ] == '.' &&
str_uri[ len-3 ] == 'i' &&
str_uri[ len-2 ] == 'c' &&
str_uri[ len-1 ] == 's')
return CAL_ICAL;
if (str_uri[ len-4 ] == '.' &&
str_uri[ len-3 ] == 'i' &&
str_uri[ len-2 ] == 'f' &&
str_uri[ len-1 ] == 'b')
return CAL_ICAL;
return CAL_VCAL;
}
/**
* cal_backend_load:
* @backend: A calendar backend.
@ -586,16 +690,34 @@ cal_backend_load (CalBackend *backend, GnomeVFSURI *uri)
| GNOME_VFS_URI_HIDE_HOST_PORT
| GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD));
vobject = Parse_MIME_FromFileName (str_uri);
/* look at the extension on the filename and decide
if this is a ical or vcal file */
priv->format = cal_get_type_from_filename (str_uri);
/* load */
switch (priv->format) {
case CAL_VCAL:
vobject = Parse_MIME_FromFileName (str_uri);
if (!vobject)
return CAL_BACKEND_LOAD_ERROR;
load_from_vobject (backend, vobject);
cleanVObject (vobject);
cleanStrTbl ();
break;
case CAL_ICAL:
icalendar_calendar_load (backend, str_uri);
break;
default:
return CAL_BACKEND_LOAD_ERROR;
}
g_free (str_uri);
if (!vobject)
return CAL_BACKEND_LOAD_ERROR;
load_from_vobject (backend, vobject);
cleanVObject (vobject);
cleanStrTbl ();
gnome_vfs_uri_ref (uri);
priv->uri = uri;

View File

@ -57,6 +57,11 @@ struct _CalBackendClass {
GtkObjectClass parent_class;
};
typedef enum {
CAL_VCAL,
CAL_ICAL
} CalendarFormat;
GtkType cal_backend_get_type (void);
CalBackend *cal_backend_new (void);

View File

@ -21,6 +21,7 @@
#include "alarm.h"
#include "timeutil.h"
#include "../libversit/vcc.h"
#include "icalendar.h"
#ifdef HAVE_TZNAME
extern char *tzname[2];
@ -334,12 +335,11 @@ calendar_load (Calendar *cal, char *fname)
cleanVObject (vcal);
cleanStrTbl ();
break;
#ifdef HAVE_LIBICAL
hi;
/*
case CAL_ICAL:
icalendar_calendar_load (cal, fname);
break;
#endif
*/
default:
return "Unknown calendar format";
}

View File

@ -2,14 +2,10 @@
#define CALENDAR_H
#include "calobj.h"
#include "cal-backend.h"
BEGIN_GNOME_DECLS
typedef enum {
CAL_VCAL,
CAL_ICAL
} CalendarFormat;
typedef struct {
/* This calendar's title */
char *title;

View File

@ -20,19 +20,14 @@ endif
bin_PROGRAMS = gnomecal tlacuache $(extra_pilot_bins)
#if HAVE_LIBICAL
ICAL_INCLUDEDIR = -I../libical/src/libical
ICAL_SOURCES = icalendar.c
ICAL_LINK_FLAGS = ../libical/src/libical/libical.a
#endif
INCLUDES = \
-I$(includedir) \
$(GNOME_INCLUDEDIR) \
$(GNOME_CONDUIT_INCLUDEDIR) \
$(PISOCK_INCLUDEDIR) \
$(ICAL_INCLUDEDIR) \
-I../libical/src/libical \
-DGNOMELOCALEDIR=\""$(datadir)/locale"\"
GNOMECAL_CORBA_GENERATED = \
@ -99,6 +94,8 @@ gnomecal_SOURCES = \
gnome-cal.c \
gnome-cal.h \
goto.c \
icalendar.c \
icalendar.h \
layout.c \
layout.h \
main.c \
@ -118,8 +115,7 @@ gnomecal_SOURCES = \
view-utils.h \
view-utils.c \
year-view.c \
year-view.h \
$(ICAL_SOURCES)
year-view.h
calendar_pilot_sync_SOURCES = \
GnomeCal-common.c \
@ -148,6 +144,8 @@ tlacuache_SOURCES = \
cal-util.h \
calobj.c \
calobj.h \
icalendar.c \
icalendar.h \
job.c \
job.h \
timeutil.c \
@ -166,7 +164,8 @@ tlacuache_INCLUDES = \
tlacuache_LDADD = \
$(BONOBO_VFS_GNOME_LIBS) \
../libversit/libversit.la
../libversit/libversit.la \
$(ICAL_LINK_FLAGS)
calendar_pilot_sync_LDADD = \
$(PISOCK_LIBDIR) $(PISOCK_LIBS) \

View File

@ -21,6 +21,7 @@
#include "alarm.h"
#include "timeutil.h"
#include "../libversit/vcc.h"
#include "icalendar.h"
#ifdef HAVE_TZNAME
extern char *tzname[2];
@ -334,12 +335,11 @@ calendar_load (Calendar *cal, char *fname)
cleanVObject (vcal);
cleanStrTbl ();
break;
#ifdef HAVE_LIBICAL
hi;
/*
case CAL_ICAL:
icalendar_calendar_load (cal, fname);
break;
#endif
*/
default:
return "Unknown calendar format";
}

View File

@ -2,14 +2,10 @@
#define CALENDAR_H
#include "calobj.h"
#include "cal-backend.h"
BEGIN_GNOME_DECLS
typedef enum {
CAL_VCAL,
CAL_ICAL
} CalendarFormat;
typedef struct {
/* This calendar's title */
char *title;

View File

@ -13,12 +13,9 @@
#include <gnome.h>
#include <unistd.h>
#include <sys/stat.h>
#include "ical.h"
#include "calobj.h"
#include "calendar.h"
#include "icalendar.h"
#include "cal-backend.h"
void icalendar_calendar_load (Calendar* cal, char* fname);
static icalcomponent* icalendar_parse_file (char* fname);
static time_t icaltime_to_timet (struct icaltimetype* i);
static CalendarAlarm* parse_alarm (icalproperty *prop);
static iCalPerson* parse_person (icalproperty *prop, gchar *value);
@ -39,14 +36,8 @@ copy_to_list (GList** store, gchar* src)
return *store;
}
/* eh?
static icalcomponent*
ical_object_to_icalcomponent (iCalObject* ical)
{
icalcomponent *comp;
*/
static iCalObject *
iCalObject *
ical_object_create_from_icalcomponent (icalcomponent* comp)
{
iCalObject *ical = NULL;
@ -372,61 +363,6 @@ this may not be correct */
return ical;
}
void
icalendar_calendar_load (Calendar* cal, char* fname)
{
icalcomponent *comp;
icalcomponent *subcomp;
iCalObject *ical;
comp = icalendar_parse_file (fname);
subcomp = icalcomponent_get_first_component (comp,
ICAL_ANY_COMPONENT);
while (subcomp) {
ical = ical_object_create_from_icalcomponent (subcomp);
if (ical->type != ICAL_EVENT &&
ical->type != ICAL_TODO &&
ical->type != ICAL_JOURNAL) {
g_warning ("Skipping unsupported iCalendar component.");
} else
calendar_add_object (cal, ical);
subcomp = icalcomponent_get_next_component (comp,
ICAL_ANY_COMPONENT);
}
}
static icalcomponent*
icalendar_parse_file (char* fname)
{
FILE* fp;
icalcomponent* comp = NULL;
gchar* str;
struct stat st;
int n;
fp = fopen (fname, "r");
if (!fp) {
g_warning ("Cannot open open calendar file.");
return NULL;
}
stat (fname, &st);
str = g_malloc (st.st_size + 2);
n = fread ((gchar*) str, 1, st.st_size, fp);
if (n != st.st_size) {
g_warning ("Read error.");
}
str[n] = '\0';
fclose (fp);
comp = icalparser_parse_string (str);
g_free (str);
return comp;
}
static time_t icaltime_to_timet (struct icaltimetype* i)
{

View File

@ -24,6 +24,7 @@
#include "cal-backend.h"
#include "calobj.h"
#include "../libversit/vcc.h"
#include "icalendar.h"
@ -37,6 +38,9 @@ typedef struct {
/* URI where the calendar data is stored */
GnomeVFSURI *uri;
/* format of this calendar (ical or vcal) */
CalendarFormat format;
/* List of Cal objects with their listeners */
GList *clients;
@ -118,6 +122,9 @@ cal_backend_init (CalBackend *backend)
priv = g_new0 (CalBackendPrivate, 1);
backend->priv = priv;
/* FIXME can be CAL_VCAL or CAL_ICAL */
priv->format = CAL_VCAL;
}
/* Saves a calendar */
@ -550,6 +557,103 @@ cal_backend_add_cal (CalBackend *backend, Cal *cal)
priv->clients = g_list_prepend (priv->clients, cal);
}
static icalcomponent*
icalendar_parse_file (char* fname)
{
FILE* fp;
icalcomponent* comp = NULL;
gchar* str;
struct stat st;
int n;
fp = fopen (fname, "r");
if (!fp) {
g_warning ("Cannot open open calendar file.");
return NULL;
}
stat (fname, &st);
str = g_malloc (st.st_size + 2);
n = fread ((gchar*) str, 1, st.st_size, fp);
if (n != st.st_size) {
g_warning ("Read error.");
}
str[n] = '\0';
fclose (fp);
comp = icalparser_parse_string (str);
g_free (str);
return comp;
}
static void
icalendar_calendar_load (CalBackend * cal, char* fname)
{
icalcomponent *comp;
icalcomponent *subcomp;
iCalObject *ical;
comp = icalendar_parse_file (fname);
subcomp = icalcomponent_get_first_component (comp,
ICAL_ANY_COMPONENT);
while (subcomp) {
ical = ical_object_create_from_icalcomponent (subcomp);
if (ical->type != ICAL_EVENT &&
ical->type != ICAL_TODO &&
ical->type != ICAL_JOURNAL) {
g_warning ("Skipping unsupported iCalendar component.");
} else
add_object (cal, ical);
subcomp = icalcomponent_get_next_component (comp,
ICAL_ANY_COMPONENT);
}
}
/*
ics is to be used to designate a file containing (an arbitrary set of)
calendaring and scheduling information.
ifb is to be used to designate a file containing free or busy time
information.
anything else is assumed to be a vcal file.
*/
static CalendarFormat
cal_get_type_from_filename (char *str_uri)
{
int len;
if (str_uri == NULL)
return CAL_VCAL;
len = strlen (str_uri);
if (len < 5)
return CAL_VCAL;
if (str_uri[ len-4 ] == '.' &&
str_uri[ len-3 ] == 'i' &&
str_uri[ len-2 ] == 'c' &&
str_uri[ len-1 ] == 's')
return CAL_ICAL;
if (str_uri[ len-4 ] == '.' &&
str_uri[ len-3 ] == 'i' &&
str_uri[ len-2 ] == 'f' &&
str_uri[ len-1 ] == 'b')
return CAL_ICAL;
return CAL_VCAL;
}
/**
* cal_backend_load:
* @backend: A calendar backend.
@ -586,16 +690,34 @@ cal_backend_load (CalBackend *backend, GnomeVFSURI *uri)
| GNOME_VFS_URI_HIDE_HOST_PORT
| GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD));
vobject = Parse_MIME_FromFileName (str_uri);
/* look at the extension on the filename and decide
if this is a ical or vcal file */
priv->format = cal_get_type_from_filename (str_uri);
/* load */
switch (priv->format) {
case CAL_VCAL:
vobject = Parse_MIME_FromFileName (str_uri);
if (!vobject)
return CAL_BACKEND_LOAD_ERROR;
load_from_vobject (backend, vobject);
cleanVObject (vobject);
cleanStrTbl ();
break;
case CAL_ICAL:
icalendar_calendar_load (backend, str_uri);
break;
default:
return CAL_BACKEND_LOAD_ERROR;
}
g_free (str_uri);
if (!vobject)
return CAL_BACKEND_LOAD_ERROR;
load_from_vobject (backend, vobject);
cleanVObject (vobject);
cleanStrTbl ();
gnome_vfs_uri_ref (uri);
priv->uri = uri;

View File

@ -57,6 +57,11 @@ struct _CalBackendClass {
GtkObjectClass parent_class;
};
typedef enum {
CAL_VCAL,
CAL_ICAL
} CalendarFormat;
GtkType cal_backend_get_type (void);
CalBackend *cal_backend_new (void);