کار با تقویم‌های Gmail

افزودن، ویرایش و حذف یک تقویم

Aspose.Email به برنامه‌ها اجازه می‌دهد تا تقویم‌های Gmail را با استفاده از مدیریت کنند IGmailClient که ویژگی‌های افزودن، حذف و به‌روزرسانی تقویم‌های Gmail را فراهم می‌کند. این کلاس مشتری فهرستی از اشیاء نوع ExtendedCalendar را برمی‌گرداند که حاوی اطلاعات موارد تقویم Gmail هستند. IGmailClient کلاس توابع زیر را برای تقویم‌ها در اختیار می‌گذارد:

دریافت فهرست تمام تقویم‌های یک مشتری

  • deleteCalendar می‌تواند برای حذف یک تقویم استفاده شود
  • fetchCalendar می‌تواند برای دریافت تقویم خاص یک مشتری استفاده شود
  • updateCalendar این تابع برای درج مجدد تقویم اصلاح‌شده یک مشتری استفاده می‌شود

برای دسترسی به تقویم‌ها، GoogleTestUser با استفاده از اعتبارهای حساب Gmail مقداردهی می‌شود. GoogleOAuthHelper برای دریافت توکن دسترسی کاربر استفاده می‌شود که سپس برای مقداردهی اولیه به کار می‌رود. IGmailClient.

درج، دریافت و به‌روزرسانی

برای درج یک تقویم، یک را مقداردهی اولیه کنید Calendar نوع شیء و با استفاده از درج کنید createCalendar تابع. createCalendar شناسه تقویم تازه وارد شده را برمی‌گرداند. این شناسه می‌تواند برای دریافت تقویم از سرور استفاده شود. قطعه کد زیر نشان می‌دهد چگونه تقویم را درج، دریافت و به‌روزرسانی کنید.

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);
}

حذف تقویم خاص

برای حذف یک تقویم خاص، باید فهرست تمام تقویم‌های یک مشتری را دریافت کنیم و سپس به‌صورت مورد نیاز حذف کنیم. listCalendars فهرست زیر را باز می‌گرداند ExtendedCalendar که شامل تقویم‌های Gmail است. قطعه کد زیر نشان می‌دهد چگونه تقویم خاصی را حذف کنید.

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());
    }
}

کار با کنترل دسترسی تقویم

Aspose.Email کنترل کامل بر دسترسی به موارد تقویم را فراهم می‌کند. listAccessRules تابع توسط IGmailClient که فهرست زیر را باز می‌گرداند AccessControlRule. اطلاعات هر قانون می‌تواند واکشی، تغییر و برای تقویم مشتری ذخیره شود. IGmailClient حاوی توابع زیر برای مدیریت قوانین کنترل دسترسی است.

  • listAccessRules این تابع فهرست زیر را فراهم می‌کند AccessControlRule
  • createAccessRule این تابع یک قانون دسترسی جدید برای تقویم ایجاد می‌کند.
  • updateAccessRule این تابع برای به‌روزرسانی یک قانون دسترسی استفاده می‌شود.
  • fetchAccessRule می‌توان از آن برای واکشی قانون دسترسی خاص برای تقویم یک مشتری استفاده کرد
  • deleteAccessRule این تابع برای حذف یک قانون دسترسی استفاده می‌شود.

قطعه کد زیر نشان می‌دهد چگونه توابع مورد استفاده برای مدیریت قوانین دسترسی را به کار می‌برید:

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;
    }
}

کار با تنظیمات مشتری و اطلاعات رنگ

Aspose.Email از دسترسی به تنظیمات مشتری با استفاده از پشتیبانی می‌کند IGmailClient.getSettings. این تابع فهرست تنظیمات را همان‌طور که در زیر آمده است باز می‌گرداند:

  1. dateFieldOrder
  2. displayAllTimezones
  3. hideInvitations
  4. format24HourTime
  5. defaultCalendarMode
  6. defaultEventLength
  7. locale
  8. remindOnRespondedEventsOnly
  9. alternateCalendar
  10. userLocation
  11. hideWeekends
  12. showDeclinedEvents
  13. weekStart
  14. weather
  15. customCalendarMode
  16. timezoneLabel
  17. timezone
  18. useKeyboardShortcuts
  19. country

به‌طور مشابه می‌توان اطلاعات رنگ برای مشتریان را با استفاده از دریافت کرد IGmailClient.getColors. این شیء اطلاعات رنگ فهرست رنگ‌های پیش‌زمینه، رنگ‌های پس‌زمینه و تاریخ و زمان به‌روزرسانی را باز می‌گرداند.

دستیابی به تنظیمات مشتری

قطعه کد زیر نشان می‌دهد چگونه توابع مورد استفاده برای دسترسی به تنظیمات مشتری را به کار می‌برید:

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");
        }
    }
}

دسترسی به اطلاعات رنگ

قطعه کد زیر نشان می‌دهد چگونه توابع مورد استفاده برای دسترسی به تنظیمات رنگ مشتری را به کار می‌برید.

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());
}

کار با قرارها

Aspose.Email ویژگی‌هایی برای کار با ارائه می‌دهد قرارها در تقویم‌های گوگل. لیست کارهایی که می‌توان بر روی قرارها در تقویم گوگل انجام داد، به شرح زیر است:

  1. اضافه کردن قرارها - createAppointment, importAppointment
  2. دریافت فهرست قرارهای ملاقات - listAppointments
  3. دریافت قرار ملاقات خاص - fetchAppointment, listAppointmentInstances
  4. به‌روزرسانی یک قرار ملاقات - updateAppointment
  5. انتقال قرار ملاقات از یک تقویم به تقویم دیگر - moveAppointment
  6. حذف قرار ملاقات - deleteAppointment

افزودن یک قرار ملاقات

نمونه کد زیر ویژگی افزودن یک قرار ملاقات به تقویم را نشان می‌دهد. در این نمونه مراحل زیر دنبال می‌شود:

  1. یک تقویم ایجاد و وارد کنید.
  2. فهرست قرار ملاقات‌ها را از یک تقویم جدید بازیابی کنید.
  3. یک قرار ملاقات ایجاد کنید.
  4. قرار ملاقات را وارد کنید.
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);
    }
    
}

بازیابی و به‌روزرسانی قرار ملاقات

در اینجا بازیابی و به‌روزرسانی تقویم به‌صورت زیر نشان داده شده است:

  1. یک قرار ملاقات خاص را بازیابی کنید.
  2. قرار ملاقات را ویرایش کنید.
  3. قرار ملاقات را در تقویم به‌روز کنید.

فرض می‌شود یک تقویم با شناسه "calendarId" و یک قرار ملاقات دارای شناسه یکتای "AppointmentUniqueId" قبلاً استخراج شده‌اند. کد نمونه زیر نشان می‌دهد چگونه قرار ملاقات را بازیابی و به‌روزرسانی کنید.

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);
}

جابجایی و حذف قرار ملاقات

Appointment قابل جابجایی است با ارائه تقویم مبدأ، تقویم مقصد و شناسه یکتا یک قرار ملاقات در تقویم مبدأ. کد نمونه زیر نشان می‌دهد چگونه قرار ملاقات را جابجا و حذف کنید.

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);
}