Práce s kalendáři Gmail
Přidávání, úprava a mazání kalendáře
Aspose.Email umožňuje aplikacím spravovat kalendáře Gmail pomocí IGmailClient která poskytuje funkce jako přidání, mazání a aktualizaci kalendářů Gmail. Tato klientská třída vrací seznam objektů typu ExtendedCalendar, které obsahují informace o položkách kalendáře Gmail. IGmailClient třída poskytuje následující funkce pro kalendáře:
- createCalendar Pro vložení nového kalendáře
- listCalendars
Získat seznam všech kalendářů klienta
- deleteCalendar Lze ji použít k smazání kalendáře
- fetchCalendar Lze ji použít k načtení konkrétního kalendáře klienta
- updateCalendar Tato funkce slouží k opětovnému vložení upraveného kalendáře klienta
Pro přístup ke kalendářům je GoogleTestUser inicializován pomocí přihlašovacích údajů k účtu Gmail. GoogleOAuthHelper se používá k získání přístupového tokenu pro uživatele, který je následně použit k inicializaci IGmailClient.
Vložit, načíst a aktualizovat
Pro vložení kalendáře inicializujte Calendar objekt typu a vložit jej pomocí createCalendar funkce. createCalendar vrací ID nově vloženého kalendáře. Toto ID lze použít k načtení kalendáře ze serveru. Následující úryvek kódu ukazuje, jak vložit, načíst a aktualizovat kalendář.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Insert, get and update calendar
Calendar calendar = new Calendar("Summary", "Description", "Location", "America/Los_Angeles");
// Insert calendar and Retrieve same calendar using id
String id = client.createCalendar(calendar);
Calendar cal = client.fetchCalendar(id);
// Change information in the fetched calendar and Update calendar
cal.setDescription("New Description");
cal.setLocation("New Location");
client.updateCalendar(cal);
}
Smazat konkrétní kalendář
Pro smazání konkrétního kalendáře potřebujeme získat seznam všech kalendářů klienta a poté je podle potřeby smazat. listCalendars vrací seznam ExtendedCalendar který obsahuje kalendáře Gmail. Následující úryvek kódu ukazuje, jak smazat konkrétní kalendář.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Access and delete calendar with summary starting from "Calendar summary"
String summary = "Calendar summary";
// Get calendars list
ExtendedCalendar[] lst = client.listCalendars();
for (ExtendedCalendar extCal : lst) {
// Delete selected calendars
if (extCal.getSummary().startsWith(summary))
client.deleteCalendar(extCal.getId());
}
}
Práce s řízením přístupu ke kalendáři
Aspose.Email poskytuje plnou kontrolu nad řízením přístupu k položkám kalendáře. listAccessRules funkce je vystavena v IGmailClient který vrací seznam AccessControlRule. Informace o jednotlivých pravidlech lze načíst, upravit a uložit zpět pro kalendář klienta. IGmailClient obsahuje následující funkce pro správu pravidel řízení přístupu.
- listAccessRules Tato funkce poskytuje seznam AccessControlRule
- createAccessRule Tato funkce vytvoří nové pravidlo přístupu pro kalendář.
- updateAccessRule Tato funkce slouží k aktualizaci pravidla přístupu.
- fetchAccessRule Lze ji použít k načtení konkrétního pravidla přístupu pro kalendář klienta
- deleteAccessRule Tato funkce slouží k odstranění pravidla přístupu.
Následující úryvek kódu ukazuje, jak jsou funkce použity pro správu pravidel přístupu:
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Retrieve list of calendars for the current client
ExtendedCalendar[] calendarList = client.listCalendars();
// Get first calendar id and retrieve list of AccessControlRule for the first calendar
String calendarId = calendarList[0].getId();
AccessControlRule[] roles1 = client.listAccessRules(calendarId);
// Create a local access control rule and Set rule properties
AccessControlRule rule = new AccessControlRule();
rule.setRole(AccessRole.reader);
rule.setScope(new AclScope(AclScopeType.user, email2));
// Insert new rule for the calendar. It returns the newly created rule
AccessControlRule createdRule = client.createAccessRule(calendarId, rule);
// Get list of rules
AccessControlRule[] roles2 = client.listAccessRules(calendarId);
// Current list length should be 1 more than the earlier one
if (roles1.length + 1 == roles2.length) {
System.out.println("List lengths are ok");
} else {
System.out.println("List lengths are not ok");
return;
}
// Change rule and Update the rule for the selected calendar
createdRule.setRole(AccessRole.writer);
AccessControlRule updatedRule = client.updateAccessRule(calendarId, createdRule);
// Retrieve individaul rule against a calendar
AccessControlRule fetchedRule = client.fetchAccessRule(calendarId, createdRule.getId());
// Delete particular rule against a given calendar and Retrieve the all rules list for the same calendar
client.deleteAccessRule(calendarId, createdRule.getId());
AccessControlRule[] roles3 = client.listAccessRules(calendarId);
// Check that current rules list length should be equal to the original list length before adding and deleting the rule
if (roles1.length == roles3.length) {
System.out.println("List lengths are same");
} else {
System.out.println("List lengths are not equal");
return;
}
}
Práce s nastavením klienta a informacemi o barvách
Aspose.Email podporuje přístup k nastavení klienta pomocí IGmailClient.getSettings. Vrací seznam nastavení uvedených níže:
- dateFieldOrder
- displayAllTimezones
- hideInvitations
- format24HourTime
- defaultCalendarMode
- defaultEventLength
- locale
- remindOnRespondedEventsOnly
- alternateCalendar
- userLocation
- hideWeekends
- showDeclinedEvents
- weekStart
- weather
- customCalendarMode
- timezoneLabel
- timezone
- useKeyboardShortcuts
- country
Podobně lze informace o barvách pro klienty získat pomocí IGmailClient.getColors. Tento objekt s informacemi o barvě vrací seznam popředních barev, pozadí a datum a čas aktualizace.
Přístup k nastavení klienta
Následující úryvek kódu ukazuje, jak jsou funkce použity pro přístup k nastavení klienta:
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Retrieve client settings
Dictionary<String, String> settings = client.getSettings();
if (settings.size() < 1) {
System.out.println("No settings are available.");
return;
}
// Traverse the settings list
for (KeyValuePair<String, String> pair : settings) {
// Get the setting value and test if settings are ok
String value = client.getSetting(pair.getKey());
if (pair.getValue().equals(value)) {
System.out.println("Key = " + pair.getKey() + ", Value = " + pair.getValue());
} else {
System.out.println("Settings could not be retrieved");
}
}
}
Přístup k informacím o barvách
Následující úryvek kódu ukazuje, jak jsou funkce použity pro přístup k nastavení barev klienta.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
ColorsInfo colors = client.getColors();
Dictionary<String, Colors> palettes = colors.getCalendar();
// Traverse the settings list
for (KeyValuePair<String, Colors> pair : palettes) {
System.out.println("Key = " + pair.getKey() + ", Color = " + pair.getValue());
}
System.out.println("Update Date = " + colors.getUpdated());
}
Práce se schůzkami
Aspose.Email poskytuje funkce pro práci s Schůzky v kalendářích Google. Následuje seznam úkolů, které lze provádět se schůzkami v kalendáři Google:
- Přidat schůzky - createAppointment, importAppointment
- Načíst seznam schůzek - listAppointments
- Načíst konkrétní schůzku - fetchAppointment, listAppointmentInstances
- Aktualizovat schůzku - updateAppointment
- Přesunout schůzku z jednoho kalendáře do druhého - moveAppointment
- Smazat schůzku - deleteAppointment
Přidání události
Následující ukázka kódu demonstruje funkci přidání události do kalendáře. V tomto příkladu jsou provedeny následující kroky:
- Vytvořit a vložit kalendář.
- Načíst seznam událostí z nového kalendáře.
- Vytvořit událost.
- Vložit událost.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Create local calendar
Calendar calendar1 = new Calendar("Summary", null, null, "Europe/Kiev");
// Insert calendar and get id of inserted calendar and Get back calendar using an id
String id = client.createCalendar(calendar1);
Calendar cal1 = client.fetchCalendar(id);
String calendarId1 = cal1.getId();
try {
// Retrieve list of appointments from the first calendar
Appointment[] appointments = client.listAppointments(calendarId1);
if (appointments.length > 0) {
System.out.println("Wrong number of appointments");
return;
}
// Get current time and Calculate time after an hour from now
java.util.Calendar c = java.util.Calendar.getInstance();
Date startDate = c.getTime();
c.add(java.util.Calendar.HOUR_OF_DAY, 1);
Date endDate = c.getTime();
// Initialize a mail address collection and set attendees mail address
MailAddressCollection attendees = new MailAddressCollection();
attendees.add("User1.EMail@domain.com");
attendees.add("User3.EMail@domain.com");
// Create an appointment with above attendees
Appointment app1 = new Appointment("Location", startDate, endDate, MailAddress.to_MailAddress(email2), attendees);
// Set appointment summary, description, start/end time zone
app1.setSummary("New Summary");
app1.setDescription("New Description");
app1.setStartTimeZone("Europe/Kiev");
app1.setEndTimeZone("Europe/Kiev");
// Insert appointment in the first calendar inserted above and get back inserted appointment
Appointment app2 = client.createAppointment(calendarId1, app1);
// Retrieve appointment using unique id
Appointment app3 = client.fetchAppointment(calendarId1, app2.getUniqueId());
} catch (Exception ex) {
System.err.println(ex);
}
}
Načíst a aktualizovat událost
Zde je ukázáno načítání a aktualizace kalendáře následovně:
- Načíst konkrétní událost.
- Upravit událost.
- Aktualizovat událost v kalendáři.
Předpokládá se, že kalendář s ID "calendarId" a jedinečným ID události "AppointmentUniqueId" jsou již získány. Následující ukázka kódu ukazuje, jak událost načíst a aktualizovat.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
String calendarId = client.listCalendars()[0].getId();
String AppointmentUniqueId = client.listAppointments(calendarId)[0].getUniqueId();
// Retrieve Appointment
Appointment app3 = client.fetchAppointment(calendarId, AppointmentUniqueId);
// Change the appointment information
app3.setSummary("New Summary");
app3.setDescription("New Description");
app3.setLocation("New Location");
app3.setFlags(AppointmentFlags.AllDayEvent);
java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.HOUR_OF_DAY, 2);
app3.setStartDate(c.getTime());
c.add(java.util.Calendar.HOUR_OF_DAY, 1);
app3.setEndDate(c.getTime());
app3.setStartTimeZone("Europe/Kiev");
app3.setEndTimeZone("Europe/Kiev");
// Update the appointment and get back updated appointment
Appointment app4 = client.updateAppointment(calendarId, app3);
}
Přesunout a smazat událost
Appointment lze přesunout zadáním zdrojového kalendáře, cílového kalendáře a jedinečného ID události ve zdrojovém kalendáři. Následující ukázka kódu ukazuje, jak událost přesunout a smazat.
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
String SourceCalendarId = client.listCalendars()[0].getId();
String DestinationCalendarId = client.listCalendars()[1].getId();
String TargetAppUniqueId = client.listAppointments(SourceCalendarId)[0].getUniqueId();
// Retrieve the list of appointments in the destination calendar before moving the appointment
Appointment[] appointments = client.listAppointments(DestinationCalendarId);
System.out.println("Before moving count = " + appointments.length);
Appointment Movedapp = client.moveAppointment(SourceCalendarId, DestinationCalendarId, TargetAppUniqueId);
// Retrieve the list of appointments in the destination calendar after moving the appointment
appointments = client.listAppointments(DestinationCalendarId);
System.out.println("After moving count = " + appointments.length);
// Delete particular appointment from a calendar using unique id
client.deleteAppointment(DestinationCalendarId, Movedapp.getUniqueId());
// Retrieve the list of appointments. It should be one less than the earlier appointments in the destination calendar
appointments = client.listAppointments(DestinationCalendarId);
System.out.println("After deleting count = " + appointments.length);
}