Skip to content

Commit

Permalink
Various fixes to ical module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Sep 2, 2022
1 parent 8f69f6d commit 0d0a59c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 118 deletions.
111 changes: 56 additions & 55 deletions CalendarFXiCal/src/main/java/com/calendarfx/ical/ICalRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,37 @@ public final class ICalRepository {

public static void loadSources() throws IOException, ParserException {

loadWebSource();
// loadWebSource();

if (familyCalendars.getCalendars().isEmpty()) {
if (true) { //familyCalendars.getCalendars().isEmpty()) {
totalWorkProperty.set(10);
createWebCalendar("https://www.nasa.gov/templateimages/redesign/calendar/iCal/nasa_calendar.ics", "NASA", Calendar.Style.STYLE1, communityCalendars);
workDoneProperty.set(1);
createWebCalendar("https://cantonbecker.com/astronomy-calendar/astrocal.ics", "Moon / Astro", Calendar.Style.STYLE2, communityCalendars);
workDoneProperty.set(2);
createWebCalendar("http://ical.mac.com/ical/US32Holidays.ics", "US Holidays", Calendar.Style.STYLE3, communityCalendars);
workDoneProperty.set(3);
createWebCalendar("https://www.google.com/calendar/ical/6g08e17mnjao5k7ddftfvq5gs8%40group.calendar.google.com/public/basic.ics", "FC Liverpool", Calendar.Style.STYLE5, communityCalendars);
workDoneProperty.set(4);
createWebCalendar("https://www.google.com/calendar/ical/ohg8jr90apq8k0vili2fbs17to%40group.calendar.google.com/public/basic.ics", "Real Madrid", Calendar.Style.STYLE4, communityCalendars);
workDoneProperty.set(5);
createWebCalendar("https://calendar.google.com/calendar/ical/flexcalendarfxdemo%40gmail.com/private-43f02ea9664382e80e6fbe0a541511ee/basic.ics", "Standard", Calendar.Style.STYLE1, familyCalendars);
workDoneProperty.set(6);
createWebCalendar("https://calendar.google.com/calendar/ical/75bjnbr2qr5qgetav71tug2sec%40group.calendar.google.com/private-c6c6a59d97aa2806fe28cfbdb2e2957b/basic.ics", "Home", Calendar.Style.STYLE2, familyCalendars);
workDoneProperty.set(7);
createWebCalendar("https://calendar.google.com/calendar/ical/5rj1uvaobtosjqoqqkpdlj01gg%40group.calendar.google.com/private-4dc56992aed93526cbab07da6cd4b69b/basic.ics", "School", Calendar.Style.STYLE3, familyCalendars);
workDoneProperty.set(8);
createWebCalendar("https://calendar.google.com/calendar/ical/0itqq6d7pukf1tapbll3lbad5c%40group.calendar.google.com/private-7fb78a4b949cede8228d791faba9061e/basic.ics", "Sports", Calendar.Style.STYLE4, familyCalendars);
workDoneProperty.set(9);
createWebCalendar("https://calendar.google.com/calendar/ical/u6em5saa8omkamh68bl7fikclo%40group.calendar.google.com/private-bce22b1e9b43b632c7edfad45d677b59/basic.ics", "Work", Calendar.Style.STYLE5, familyCalendars);
workDoneProperty.set(10);
// createWebCalendar("https://calendar.google.com/calendar/ical/nextspaceflight.com_l328q9n2alm03mdukb05504c44%40group.calendar.google.com/public/basic.ics", "Space Launches", Calendar.Style.STYLE1, communityCalendars);
createWebCalendar("https://i.cal.to/ical/2/fcbayern/bundesliga-spielplan/1406373c.bca6824f-04acfda3.ics", "Bayern München", Calendar.Style.STYLE2, communityCalendars);

// createWebCalendar("https://cantonbecker.com/astronomy-calendar/astrocal.ics", "Moon / Astro", Calendar.Style.STYLE2, communityCalendars);
// workDoneProperty.set(2);
// createWebCalendar("http://ical.mac.com/ical/US32Holidays.ics", "US Holidays", Calendar.Style.STYLE3, communityCalendars);
// workDoneProperty.set(3);
// createWebCalendar("https://www.google.com/calendar/ical/6g08e17mnjao5k7ddftfvq5gs8%40group.calendar.google.com/public/basic.ics", "FC Liverpool", Calendar.Style.STYLE5, communityCalendars);
// workDoneProperty.set(4);
// createWebCalendar("https://www.google.com/calendar/ical/ohg8jr90apq8k0vili2fbs17to%40group.calendar.google.com/public/basic.ics", "Real Madrid", Calendar.Style.STYLE4, communityCalendars);
// workDoneProperty.set(5);
// createWebCalendar("https://calendar.google.com/calendar/ical/flexcalendarfxdemo%40gmail.com/private-43f02ea9664382e80e6fbe0a541511ee/basic.ics", "Standard", Calendar.Style.STYLE1, familyCalendars);
// workDoneProperty.set(6);
// createWebCalendar("https://calendar.google.com/calendar/ical/75bjnbr2qr5qgetav71tug2sec%40group.calendar.google.com/private-c6c6a59d97aa2806fe28cfbdb2e2957b/basic.ics", "Home", Calendar.Style.STYLE2, familyCalendars);
// workDoneProperty.set(7);
// createWebCalendar("https://calendar.google.com/calendar/ical/5rj1uvaobtosjqoqqkpdlj01gg%40group.calendar.google.com/private-4dc56992aed93526cbab07da6cd4b69b/basic.ics", "School", Calendar.Style.STYLE3, familyCalendars);
// workDoneProperty.set(8);
// createWebCalendar("https://calendar.google.com/calendar/ical/0itqq6d7pukf1tapbll3lbad5c%40group.calendar.google.com/private-7fb78a4b949cede8228d791faba9061e/basic.ics", "Sports", Calendar.Style.STYLE4, familyCalendars);
// workDoneProperty.set(9);
// createWebCalendar("https://calendar.google.com/calendar/ical/u6em5saa8omkamh68bl7fikclo%40group.calendar.google.com/private-bce22b1e9b43b632c7edfad45d677b59/basic.ics", "Work", Calendar.Style.STYLE5, familyCalendars);
// workDoneProperty.set(10);

}
}

public static void loadWebSource() {
try (FileInputStream fin = new FileInputStream(new File(System.getProperty("user.home") + SETTINGS_DIR, SETTINGS_FILE));
ObjectInputStream ois = new ObjectInputStream(fin)) {
try (FileInputStream fin = new FileInputStream(new File(System.getProperty("user.home") + SETTINGS_DIR, SETTINGS_FILE)); ObjectInputStream ois = new ObjectInputStream(fin)) {

List<WebCalendarData> webCalendars = (List<WebCalendarData>) ois.readObject();

Expand Down Expand Up @@ -133,46 +134,48 @@ public static ICalCalendar getWebCalendar(String url) {
return null;
}

public static ICalCalendar createWebCalendar(String url, String name, Calendar.Style style, CalendarSource source) throws IOException, ParserException {
public static void createWebCalendar(String url, String name, Calendar.Style style, CalendarSource source) {
if (url == null || url.isEmpty() || name == null || name.isEmpty() || style == null) {
return null;
return;
}

WebCalendarData data = new WebCalendarData(url, name, style, source == familyCalendars);

ICalCalendar cal = putWebCalendar(data, source);
try {
WebCalendarData data = new WebCalendarData(url, name, style, source == familyCalendars);

List<WebCalendarData> webCalendarDatas = new ArrayList<>();
webCalendarDatas.addAll(webCalendars.keySet());
if (!webCalendarDatas.isEmpty()) {
final File directory = new File(System.getProperty("user.home") + SETTINGS_DIR);
boolean directoryExists = true;
if (!directory.exists()) {
directoryExists = directory.mkdirs();
}
putWebCalendar(data, source);

if (directoryExists) {
final File file = new File(directory, SETTINGS_FILE);
List<WebCalendarData> webCalendarDatas = new ArrayList<>();
webCalendarDatas.addAll(webCalendars.keySet());

boolean fileExists = true;
if (!file.exists()) {
fileExists = file.createNewFile();
if (!webCalendarDatas.isEmpty()) {
final File directory = new File(System.getProperty("user.home") + SETTINGS_DIR);
boolean directoryExists = true;
if (!directory.exists()) {
directoryExists = directory.mkdirs();
}

if (fileExists) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
oos.writeObject(webCalendarDatas);
} catch (Exception e) {
e.printStackTrace();
if (directoryExists) {
final File file = new File(directory, SETTINGS_FILE);

boolean fileExists = true;
if (!file.exists()) {
fileExists = file.createNewFile();
}

if (fileExists) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
oos.writeObject(webCalendarDatas);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

return cal;
} catch (Throwable t) {}
}

private static ICalCalendar putWebCalendar(WebCalendarData data, CalendarSource source) throws IOException, ParserException {
private static void putWebCalendar(WebCalendarData data, CalendarSource source) throws IOException, ParserException {
ICalCalendar cal = getWebCalendar(data.getUrl());
if (cal == null) {

Expand All @@ -196,13 +199,11 @@ private static ICalCalendar putWebCalendar(WebCalendarData data, CalendarSource
webCalendars.put(data, cal);

final ICalCalendar fcal = cal;

Platform.runLater(() -> {
source.getCalendars().add(fcal);
}
);
source.getCalendars().add(fcal);
});
}

return cal;
}

private static class WebCalendarData implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.time.Year;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjusters;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -84,49 +85,68 @@ private synchronized void load(int year) {

Period<Instant> period = new Period<>(st.toInstant(), et.toInstant());
Filter<VEvent> filter = new Filter<>(new PeriodRule<>(period));
Collection<VEvent> events = filter.filter(calendar.getComponents(Component.VEVENT));
Collection<VEvent> events = calendar.getComponents(Component.VEVENT);

for (VEvent evt : events) {

Optional<Uid> uid = evt.getProperty(Property.UID);
if (!uid.isPresent()) {
continue;
try {
Optional<Uid> uid = evt.getProperty(Property.UID);
if (!uid.isPresent()) {
continue;
}

if (loadedEventIds.contains(uid.get())) {
continue;
}

loadedEventIds.add(uid.get());

ICalCalendarEntry entry = new ICalCalendarEntry(evt);

Optional<DtStart> dtStartOptional = evt.getProperty(Property.DTSTART);
Optional<DtEnd> dtEndOptional = evt.getProperty(Property.DTEND);

if (!(dtStartOptional.isPresent() && dtEndOptional.isPresent())) {
continue;
}

Temporal start = dtStartOptional.get().getDate();
Temporal end = dtEndOptional.get().getDate();

ZonedDateTime entryStart = null;
ZonedDateTime entryEnd = null;

if (start instanceof Instant) {
entryStart = ZonedDateTime.ofInstant((Instant) start, ZoneId.systemDefault());
entryEnd = ZonedDateTime.ofInstant((Instant) end, ZoneId.systemDefault());
} else if (start instanceof LocalDate) {
entryStart = ZonedDateTime.of((LocalDate) start, LocalTime.MIN, ZoneId.systemDefault());
entryEnd = ZonedDateTime.of((LocalDate) end, LocalTime.MAX, ZoneId.systemDefault());
entry.setFullDay(true);
} else if (start instanceof ZonedDateTime) {
entryStart = (ZonedDateTime) start;
entryEnd = (ZonedDateTime) end;
entryStart = entryStart.withZoneSameInstant(ZoneId.systemDefault());
entryEnd = entryEnd.withZoneSameInstant(ZoneId.systemDefault());
}

if (entryStart == null || entryEnd == null) {
continue;
}

entry.setInterval(new Interval(entryStart, entryEnd));

final Optional<RRule> prop = evt.getProperty(Property.RRULE);
if (prop.isPresent()) {
RRule rrule = prop.get();
entry.setRecurrenceRule("RRULE:" + rrule.getValue());
}

addEntry(entry);
} catch (Throwable t) {
}

if (loadedEventIds.contains(uid.get())) {
continue;
}

loadedEventIds.add(new Uid(evt.getUid().toString()));

ICalCalendarEntry entry = new ICalCalendarEntry(evt);

Optional<DtStart> dtStart = evt.getProperty(Property.DTSTART);
Optional<DtEnd> dtEnd = evt.getProperty(Property.DTEND);

if (!(dtStart.isPresent() && dtEnd.isPresent())) {
continue;
}

ZonedDateTime entryStart = ZonedDateTime.ofInstant(Instant.from(dtStart.get().getDate()), ZoneId.systemDefault());
ZonedDateTime entryEnd = ZonedDateTime.ofInstant(Instant.from(dtEnd.get().getDate()), ZoneId.systemDefault());

if (entryEnd.toLocalDate().isAfter(entryStart.toLocalDate())) {
entryEnd = entryEnd.minusDays(1);
entry.setFullDay(true);
}

entry.setInterval(new Interval(entryStart, entryEnd));

final Optional<RRule> prop = evt.getProperty(Property.RRULE);
if (prop.isPresent()) {
RRule rrule = prop.get();
entry.setRecurrenceRule("RRULE:" + rrule.getValue());
}

addEntry(entry);
}
} catch (Exception ex) {
} catch (Throwable ex) {
ex.printStackTrace();
} finally {
stopBatchUpdates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.calendarfx.model.Entry;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.Location;
import net.fortuna.ical4j.model.property.Summary;

import java.util.Optional;
Expand All @@ -30,5 +31,9 @@ public ICalCalendarEntry(VEvent event) {
if (summary.isPresent()) {
setTitle(summary.get().getValue());
}
Optional<Location> optionalLocation = event.getProperty(Property.LOCATION);
if (optionalLocation.isPresent()) {
setLocation(optionalLocation.get().getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.util.Callback;
import net.fortuna.ical4j.data.ParserException;

import java.io.IOException;
import java.net.MalformedURLException;

/**
* Calendar Source Factory that shows up a dialog to enter a web based URL of an iCal.
*
* <p>
* Created by gdiaz on 5/01/2017.
*/
public final class ICalWebSourceFactory implements Callback<DateControl.CreateCalendarSourceParameter, CalendarSource> {
Expand Down Expand Up @@ -87,25 +83,8 @@ private void accept(ActionEvent evt) {
return;
}

try {
ICalRepository.createWebCalendar(url, name, style, ICalRepository.getCommunityCalendarSource());
pane.clear();
dialog.hide();
} catch (MalformedURLException e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("The URL is not valid!");
alert.show();
} catch (ParserException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Unable to parse the calendar data!");
alert.show();
} catch (IOException e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Unexpected error getting the calendar!");
alert.show();
}
ICalRepository.createWebCalendar(url, name, style, ICalRepository.getCommunityCalendarSource());
pane.clear();
dialog.hide();
}

}

0 comments on commit 0d0a59c

Please sign in to comment.