<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!DOCTYPE dialog [
  <!ENTITY % dtd1 SYSTEM "chrome://calendar/locale/calendar-invitations-dialog.dtd" >  %dtd1;
]>

<bindings id="calendar-invitations-list-bindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          xmlns:xbl="http://www.mozilla.org/xbl">
  <binding id="calendar-invitations-richlistbox"
           extends="chrome://global/content/bindings/richlistbox.xml#richlistbox"
           xbl:inherits="flex">
    <implementation>
      <!-- methods -->
      <method name="addCalendarItem">
        <parameter name="aItem"/>
        <body><![CDATA[
          var newNode = createXULElement("calendar-invitations-richlistitem");
          this.appendChild(newNode);
          newNode.setAttribute("anonid", "invitations-listitem");
          newNode.calendarItem = aItem;
        ]]></body>
      </method>
    </implementation>
  </binding>

  <binding id="calendar-invitations-richlistitem"
           extends="chrome://global/content/bindings/richlistbox.xml#richlistitem">
    <content>
      <xul:hbox align="start" flex="1">
        <xul:image anonid="icon" class="calendar-invitations-richlistitem-icon"/>
        <xul:vbox flex="1">
          <xul:label anonid="title" class="calendar-invitations-richlistitem-title"
                     crop="end"/>
          <xul:label anonid="date" crop="end"/>
          <xul:label anonid="recurrence" crop="end"/>
          <xul:label anonid="location" crop="end"/>
          <xul:label anonid="organizer" crop="end"/>
          <xul:label anonid="attendee" crop="end"/>
          <xul:label anonid="spacer" value="" hidden="true"/>
        </xul:vbox>
        <xul:vbox>
          <xul:button anonid="accept"
            xbl:inherits="group=itemId"
            type="radio"
            class="calendar-invitations-richlistitem-accept-button
            calendar-invitations-richlistitem-button"
            label="&calendar.invitations.list.accept.button.label;"
            oncommand="accept();"/>
          <xul:button anonid="decline"
            xbl:inherits="group=itemId"
            type="radio"
            class="calendar-invitations-richlistitem-decline-button
            calendar-invitations-richlistitem-button"
            label="&calendar.invitations.list.decline.button.label;"
            oncommand="decline();"/>
        </xul:vbox>
      </xul:hbox>
    </content>

    <implementation>
      <!-- fields -->
      <field name="mDateFormatter">null</field>
      <field name="mCalendarItem">null</field>
      <field name="mInitialParticipationStatus">null</field>
      <field name="mParticipationStatus">null</field>

      <!-- properties -->
      <property name="calendarItem">
        <getter><![CDATA[
          return this.mCalendarItem;
        ]]></getter>
        <setter><![CDATA[
          this.setCalendarItem(val);
          return val;
        ]]></setter>
      </property>

      <property name="initialParticipationStatus">
        <getter><![CDATA[
          return this.mInitialParticipationStatus;
        ]]></getter>
        <setter><![CDATA[
          this.mInitialParticipationStatus = val;
          return val;
        ]]></setter>
      </property>

      <property name="participationStatus">
        <getter><![CDATA[
          return this.mParticipationStatus;
        ]]></getter>
        <setter><![CDATA[
          this.mParticipationStatus = val;
          var icon = document.getAnonymousElementByAttribute(
              this, "anonid", "icon");
          icon.setAttribute("status", val);
          return val;
        ]]></setter>
      </property>

      <!-- constructor -->
      <constructor><![CDATA[
        Components.utils.import("resource://calendar/modules/calUtils.jsm");
        this.mDateFormatter = getDateFormatter();
      ]]></constructor>

      <!-- methods -->
      <method name="setCalendarItem">
        <parameter name="aItem"/>
        <body>
          var alldayEventText = "&calendar.invitations.list.alldayevent.text;";
          var recurrentEventText = "&calendar.invitations.list.recurrentevent.text;";
          var locationText = "&calendar.invitations.list.location.text;";
          var organizerText = "&calendar.invitations.list.organizer.text;";
          var attendeeText = "&calendar.invitations.list.attendee.text;";
          var noneText = "&calendar.invitations.list.none.text;";
          <![CDATA[
            this.mCalendarItem = aItem;
            this.mInitialParticipationStatus =
                this.getCalendarItemParticipationStatus(aItem);
            this.participationStatus = this.mInitialParticipationStatus;

            var titleLabel = document.getAnonymousElementByAttribute(
                this, "anonid", "title");
            titleLabel.setAttribute("value", aItem.title);

            var dateLabel = document.getAnonymousElementByAttribute(
                this, "anonid", "date");
            var dateString = this.mDateFormatter.formatItemInterval(aItem);
            if (aItem.startDate.isDate) {
                dateString += ", " + alldayEventText;
            }
            dateLabel.setAttribute("value", dateString);

            var recurrenceLabel = document.getAnonymousElementByAttribute(
                this, "anonid", "recurrence");
            if (aItem.recurrenceInfo) {
                recurrenceLabel.setAttribute("value", recurrentEventText);
            } else {
                recurrenceLabel.setAttribute("hidden", "true");
                var spacer = document.getAnonymousElementByAttribute(
                    this, "anonid", "spacer");
                spacer.removeAttribute("hidden");
            }

            var locationLabel = document.getAnonymousElementByAttribute(
                this, "anonid", "location");
            var locationString = locationText;
            var locationProperty = aItem.getProperty("LOCATION");
            if (locationProperty && locationProperty.length > 0) {
                locationString += locationProperty;
            } else {
                locationString += noneText;
            }
            locationLabel.setAttribute("value", locationString);

            var organizerLabel = document.getAnonymousElementByAttribute(
                this, "anonid", "organizer");
            var organizerString = organizerText;
            var org = aItem.organizer;
            if (org) {
                if (org.commonName && org.commonName.length > 0) {
                    organizerString += org.commonName;
                } else if (org.id) {
                    organizerString += org.id.replace(/^mailto:/i, "");
                }
            }
            organizerLabel.setAttribute("value", organizerString);

            let attendeeLabel = document.getAnonymousElementByAttribute(
                this, "anonid", "attendee");
            let attendeeString = attendeeText;
            let att = cal.getInvitedAttendee(aItem);
            if (att) {
                if (att.commonName && att.commonName.length > 0) {
                    attendeeString += att.commonName;
                } else if (att.id) {
                    attendeeString += att.id.replace(/^mailto:/i, "");
                }
            }
            attendeeLabel.setAttribute("value", attendeeString);
            this.setAttribute("itemId", aItem.hashId);
          ]]>
        </body>
      </method>

      <method name="getCalendarItemParticipationStatus">
        <parameter name="aItem"/>
        <body><![CDATA[
          let att = cal.getInvitedAttendee(aItem);
          return (att ? att.participationStatus : null);
        ]]></body>
      </method>

      <method name="setCalendarItemParticipationStatus">
        <parameter name="aItem"/>
        <parameter name="aStatus"/>
        <body><![CDATA[
          let calendar = cal.wrapInstance(aItem.calendar, Components.interfaces.calISchedulingSupport);
          if (calendar) {
              var att = calendar.getInvitedAttendee(aItem);
              if (att) {
                  var att_ = att.clone();
                  att_.participationStatus = aStatus;

                  // Update attendee
                  aItem.removeAttendee(att);
                  aItem.addAttendee(att_);
                  return true;
              }
          }
          return false;
        ]]></body>
      </method>

      <method name="accept">
        <body><![CDATA[
          this.participationStatus = "ACCEPTED";
        ]]></body>
      </method>

      <method name="decline">
        <body><![CDATA[
          this.participationStatus = "DECLINED";
        ]]></body>
      </method>
    </implementation>
  </binding>
</bindings>
