summaryrefslogtreecommitdiffstats
path: root/src/com/android/calendar/icalendar/VCalendar.java
blob: 140d9358217bb8c531eff011684627effa951227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * Copyright (C) 2014 The CyanogenMod Project
 */

package com.android.calendar.icalendar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * Models the Calendar/VCalendar component of the iCalendar format
 */
public class VCalendar {

    // valid property identifiers of the component
    // TODO: only a partial list of attributes have been implemented, implement the rest
    public static String VERSION = "VERSION";
    public static String PRODID = "PRODID";
    public static String CALSCALE = "CALSCALE";
    public static String METHOD = "METHOD";

    public final static String PRODUCT_IDENTIFIER = "-//Cyanogen Inc//com.android.calendar";

    // stores the -arity of the attributes that this component can have
    private final static HashMap<String, Integer> sPropertyList = new HashMap<String, Integer>();

    // initialize approved list of iCal Calendar properties
    static {
        sPropertyList.put(VERSION, 1);
        sPropertyList.put(PRODID, 1);
        sPropertyList.put(CALSCALE, 1);
        sPropertyList.put(METHOD, 1);
    }

    // stores attributes and their corresponding values belonging to the Calendar object
    public HashMap<String, String> mProperties;
    public LinkedList<VEvent> mEvents;      // events that belong to this Calendar object

    /**
     * Constructor
     */
    public VCalendar() {
        mProperties = new HashMap<String, String>();
        mEvents = new LinkedList<VEvent>();
    }

    /**
     * Add specified property
     * @param property
     * @param value
     */
    public boolean addProperty(String property, String value) {
        // since all the required mProperties are unary (only one can exist) , taking a shortcut
        // here
        // TODO: when multiple attributes of a property can exist , enforce that here
        if (sPropertyList.containsKey(property) && value != null) {
            mProperties.put(property, IcalendarUtils.cleanseString(value));
            return true;
        }
        return false;
    }

    /**
     * Add Event to calendar
     * @param event
     */
    public void addEvent(VEvent event) {
        if (event != null) mEvents.add(event);
    }

    /**
     * Returns all the events that are part of this calendar
     */
    public LinkedList<VEvent> getAllEvents() {
        return mEvents;
    }

    /**
     * Returns the first event of the calendar
     */
    public VEvent getFirstEvent() {
        if (mEvents != null && mEvents.size() > 0) {
            return mEvents.get(0);
        } else {
            return null;
        }
    }

    /**
     * Returns the iCal representation of the calendar and all of its inherent components
     */
    public String getICalFormattedString() {
        StringBuilder output = new StringBuilder();

        // Add Event properties
        // TODO: add the ability to specify the order in which to compose the properties
        output.append("BEGIN:VCALENDAR\n");
        for (String property : mProperties.keySet() ) {
            output.append(property + ":" + mProperties.get(property) + "\n");
        }

        // enforce line length requirements
        output = IcalendarUtils.enforceICalLineLength(output);
        // add event
        for (VEvent event : mEvents) {
            output.append(event.getICalFormattedString());
        }

        output.append("END:VCALENDAR\n");

        return output.toString();
    }

    public void populateFromString(ArrayList<String> input) {
        ListIterator<String> iter = input.listIterator();

        while (iter.hasNext()) {
            String line = iter.next();
            if (line.contains("BEGIN:VEVENT")) {
                // Go one previous, so VEvent, parses current line
                iter.previous();

                // Offload to vevent for parsing
                VEvent event = new VEvent();
                event.populateFromEntries(iter);
                mEvents.add(event);
            } else if (line.contains("END:VCALENDAR")) {
                break;
            }
        }
    }

    public String getProperty(String key) {
        return mProperties.get(key);
    }

    /**
     * TODO: Aggressive validation of VCalendar and all of its components to ensure they conform
     * to the ical specification
     * @return
     */
    private boolean validate() {
        return false;
    }
}