العمل مع تقاويم Gmail

إضافة، تعديل وحذف تقويم

تسمح Aspose.Email للتطبيقات بإدارة تقاويم جيميل باستخدام IGmailClient الذي يوفر ميزات مثل إضافة، حذف وتحديث تقاويم جيميل. يُعيد هذا الفصل العميل قائمة بكائنات من نوع ExtendedCalendar التي تحتوي على معلومات حول عناصر تقويم جيميل. IGmailClient الفصل يُظهر الدوال التالية للتقاويم:

احصل على قائمة بجميع تقاويم العميل

  • deleteCalendar يمكن استخدامها لحذف تقويم
  • fetchCalendar يمكن استخدامها لاسترجاع تقويم معين للعميل
  • updateCalendar تُستخدم هذه الدالة لإدخال مرة أخرى تقويمًا معدلًا للعميل

للوصول إلى التقاويم، يتم تهيئة GoogleTestUser باستخدام بيانات اعتماد حساب جيميل. يُستخدم 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 التي تحتوي على تقاويم جيميل. يوضح المقتطف البرمجي التالي كيفية حذف تقويم معين.

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 تحتوي على الدوال التالية لإدارة قواعد التحكم في الوصول.

يوضح المقتطف البرمجي التالي كيفية استخدام الدوال لإدارة قواعد الوصول:

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