You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

270 lines
9.2 KiB

#include <util/calendar_parsing.h>
#include <memory>
#include <iomanip>
#include <string>
namespace util {
std::vector<ical::IcalObject*> parse_cal_dir(std::string directory) {
std::vector<ical::IcalObject*> objects;
std::vector<icalcomponent*> components;
uint counter = 0;
DEBUG << "Parsing: " << directory << " for .ics files";
for (boost::filesystem::directory_entry& entry : boost::filesystem::directory_iterator(directory)) {
ical::IcalObject* object = new ical::IcalObject();
parse_main_component(object, parse_ics_file(entry.path().generic_string()), entry.path().generic_string());
objects.push_back(object);
counter++;
}
DEBUG << "files parsed: " << counter;
return objects;
}
icalcomponent* parse_main_component(ical::IcalObject* object, icalcomponent* component, const std::string &file) {
icalcomponent* c = icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
while(c != 0) {
if(c != 0) {
parse_component(object, c, file);
}
c = parse_main_component(object, c, file);
if(c == 0) {
c = icalcomponent_get_next_component(component, ICAL_ANY_COMPONENT);
}
}
return c;
}
void parse_component(ical::IcalObject* object, icalcomponent* component, const std::string &file) {
switch(icalcomponent_isa(component)) {
case ICAL_VALARM_COMPONENT: break;
case ICAL_NO_COMPONENT:
{
//DEBUG << "parsed component: ICAL_NO_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_ANY_COMPONENT:
{
//DEBUG << "parsed component: ICAL_ANY_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XROOT_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XROOT_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XATTACH_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XATTACH_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VEVENT_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VEVENT_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
object->add_event(new ical::Event(component, file));
break;
}
case ICAL_VTODO_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VTODO_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VJOURNAL_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VJOURNAL_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VCALENDAR_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VCALENDAR_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VAGENDA_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VAGENDA_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VFREEBUSY_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VFREEBUSY_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XAUDIOALARM_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XAUDIOALARM_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XDISPLAYALARM_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XDISPLAYALARM_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XEMAILALARM_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XEMAILALARM_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XPROCEDUREALARM_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XPROCEDUREALARM_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VTIMEZONE_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VTIMEZONE_COMPONENT";
DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
//icaltimezone* zone;
//icalproperty *prop;
//const char *tzid;
//prop = icalcomponent_get_first_property(component, ICAL_TZID_PROPERTY);
//tzid = icalproperty_get_tzid(prop);
//zone->tzid = strdup(tzid);
//zone->component = component;
//zone->location = icaltimezone_get_location_from_vtimezone(component);
//zone->tznames = icaltimezone_get_tznames_from_vtimezone(component);
//DEBUG << icaltimezone_get_tzid(zone);
//int offset = icaltimezone_get_utc_offset(zone, struct icaltimetype *tt, int *is_daylight)
break;
}
case ICAL_XSTANDARD_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XSTANDARD_COMPONENT";
break;
}
case ICAL_XDAYLIGHT_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XDAYLIGHT_COMPONENT";
break;
}
case ICAL_X_COMPONENT:
{
//DEBUG << "parsed component: ICAL_X_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VSCHEDULE_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VSCHEDULE_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VQUERY_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VQUERY_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VREPLY_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VREPLY_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VCAR_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VCAR_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VCOMMAND_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VCOMMAND_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XLICINVALID_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XLICINVALID_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XLICMIMEPART_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XLICMIMEPART_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VAVAILABILITY_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VAVAILABILITY_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XAVAILABLE_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XAVAILABLE_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VPOLL_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VPOLL_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VVOTER_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VVOTER_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XVOTE_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XVOTE_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_VPATCH_COMPONENT:
{
//DEBUG << "parsed component: ICAL_VPATCH_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
case ICAL_XPATCH_COMPONENT:
{
//DEBUG << "parsed component: ICAL_XPATCH_COMPONENT";
//DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
break;
}
default:
DEBUG << "parse_component: something else found";
break;
}
}
std::string slurp(std::ifstream& in) {
std::ostringstream sstr;
sstr << in.rdbuf();
return sstr.str();
}
icalcomponent* parse_ics_file(std::string filename) {
std::ifstream ifs (filename, std::ifstream::in);
return icalparser_parse_string(slurp(ifs).c_str());
}
void debug(icalcomponent* component) {
DEBUG << "\n" << icalcomponent_as_ical_string_r(component);
}
std::string date_string(const uint32_t &epoch, const std::string &format) {
std::time_t time = static_cast<time_t>(epoch);
char buf[80];
strftime(buf, sizeof(buf), format.c_str(), std::localtime(&time));
return buf;
}
}