~pkunal-parmar/ubuntu-calendar-app/ICalImport

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.Popups 0.1

import "dateExt.js" as DateExt
import "GlobalEventModel.js" as GlobalModel

MainView {
    id: mainView

    // Argument during startup
    Arguments {
        id: args;

        // Example of argument: calendar:///new-event

        // IMPORTANT
        // Due to bug #1231558 you have to pass arguments BEFORE app:
        // qmlscene calendar:///new-event calendar.qml

        defaultArgument.help: i18n.tr("Calendar app accept three arguments: --starttime, --endtime and --newevet. They will be managed by system. See the source for a full comment about them");
        //defaultArgument.required: false;
        defaultArgument.valueNames: ["URL"]

        /* ARGUMENTS on startup
         * (no one is required)
         *
         * Create a new event
         * Keyword: newevent
         *
         * Create a new event. If starttime or endtime are set they are used to set start and end time of the new event.
         * It accepts no value.
         *
         *
         * Choose the view
         * Keyword: starttime
         *
         * If newevent has been called, starttime is the start time of event. Otherwise is the day on which app is focused on startup.
         * It accepts an integer value of the number of seconds since UNIX epoch in the UTC timezone.
         * 0 means today.
         *
         * Keyword: endtime
         *
         * If newevent is set it's the end time of the event, has to be > of starttime.
         * If newevent isn't set and startime is set, its value is used to choose the right view.
         * If neither of precendet flags are set, endtime is ignored.
         * It accepts an integer value of the number of seconds since UNIX epoch in the UTC timezone.
         */
    }

    objectName: "calendar"
    applicationName: "com.ubuntu.calendar"

    width: units.gu(45)
    height: units.gu(80)

    headerColor: "#266249"
    backgroundColor: "#478158"
    footerColor: "#478158"
    anchorToKeyboard: true

    PageStack {
        id: pageStack

        Component.onCompleted: push(tabPage)

        Page{
            id: tabPage

            property var currentDay: DateExt.today();
            property var globalModel;

            // Arguments on startup
            property bool newevent: false;
            property int starttime: -1;
            property int endtime: -1;

            onCurrentDayChanged: {
                if( monthView.currentMonth !== undefined && !monthView.currentMonth.isSameDay(currentDay))
                    monthView.currentMonth = currentDay.midnight();

                if( !dayView.currentDay.isSameDay(currentDay))
                    dayView.currentDay = currentDay

                if( !weekView.dayStart.isSameDay(currentDay))
                    weekView.dayStart = currentDay

                setStartEndDateToModel();
            }

            function setStartEndDateToModel() {
                if(globalModel) {
                    globalModel.startPeriod =  new Date(currentDay.getFullYear(),0,1,0,0,0,0);
                    globalModel.endPeriod = new Date(currentDay.getFullYear(),11,31,0,0,0,0);
                }
            }

            function newEvent() {
                var startDate = new Date();
                var endDate = new Date();
                var startTime;
                var endTime;

                if (starttime === 0) { // startime 0 means now
                    if (endtime !== -1) { // If also endtime has been invoked
                        endTime = parseInt(endtime);
                        if (endTime > startDate) // If endtime is after startime
                            endDate = new Date(endTime);
                    }
                }
                else if (starttime !== -1) { // If starttime has been invoked
                    startTime = parseInt(starttime);
                    startDate = new Date(startTime);
                    if (endtime !== -1) { // If --endtime has been invoked
                        endTime = parseInt(endtime);
                        if (endTime > startDate)
                            endDate = new Date(endTime);
                    }
                }
                pageStack.push(Qt.resolvedUrl("NewEvent.qml"),{"startDate": startDate, "endDate": endDate});
            }

            // This function calculate the difference between --endtime and --starttime and choose the better view
            function calculateDifferenceStarttimeEndtime(startTime, endTime) {
                var minute = 60 * 1000;
                var hour = 60 * minute;
                var day = 24 * hour;
                var month = 30 * day;

                var difference = endTime - startTime;

                if (difference > month)
                    return 0;   // Year view
                else if (difference > 7 * day)
                    return 1;   // Month view}
                else if (difference > day)
                    return 2;   // Week view
                else
                    return 3;   // Day view
            }

            // This function parse the argument
            function parseArguments(url) {
                var newevenpattern= new RegExp ("newevent");
                var starttimepattern = new RegExp ("starttime=\\d+");
                var endtimepattern = new RegExp ("endtime=\\d+");

                newevent = newevenpattern.test(url);

                if (starttimepattern.test(url))
                    starttime = url.match(/starttime=(\d+)/)[0].replace("starttime=", '');

                if (endtimepattern.test(url))
                    endtime = url.match(/endtime=(\d+)/)[0].replace("endtime=", '');

            }

            Component.onCompleted: {
                // If an url has been set
                if (args.defaultArgument.at(0)) {
                    parseArguments(args.defaultArgument.at(0))
                    tabPage.currentDay = new Date()
                    // If newevent has been called on startup
                    if (newevent) {
                        timer.running = true;
                    }
                    else if (starttime !== -1) { // If no newevent has been setted, but starttime
                        var startTime = parseInt(starttime);
                        tabPage.currentDay = new Date(startTime);

                        // If also endtime has been settend
                        if (endtime !== -1) {
                            var endTime = parseInt(endtime);
                            tabs.selectedTabIndex = calculateDifferenceStarttimeEndtime(startTime, endTime);
                        }
                        else {
                            // If no endtime has been setted, open the starttime date in day view
                            tabs.selectedTabIndex = 3;
                        }
                    } // End of else if (starttime)
                    else {
                    	// Due to bug #1231558 {if (args.defaultArgument.at(0))} is always true
                    	// After the fix we can delete this else
                        tabs.selectedTabIndex= 1;
                    }
                } // End of if about args.values
                else {
                    tabs.selectedTabIndex= 1;
                }

                globalModel = GlobalModel.gloablModel();
                setStartEndDateToModel();
            } // End of Component.onCompleted:

            // This is for wait that the app is load when newEvent is invoked by argument
            Timer {
                id: timer
                interval: 200;
                running: false;
                repeat: false
                onTriggered: {
                    tabPage.newEvent();
                }
            }

            ToolbarItems {
                id: commonToolBar

                ToolbarButton {
                    objectName: "todaybutton"
                    action: Action {
                        iconSource: Qt.resolvedUrl("calendar-today.svg");
                        text: i18n.tr("Today");
                        onTriggered: {
                            tabPage.currentDay = (new Date()).midnight();
                        }
                    }
                }
                ToolbarButton {
                    objectName: "neweventbutton"
                    action: Action {
                        iconSource: Qt.resolvedUrl("new-event.svg");
                        text: i18n.tr("New Event");
                        onTriggered: {
                            pageStack.push(Qt.resolvedUrl("NewEvent.qml"),{"date":tabPage.currentDay});
                        }
                    }
                }
            }

            Tabs{
                id: tabs
                Tab{
                    objectName: "yearTab"
                    title: i18n.tr("Year")
                    page: Page{
                        objectName: "yearPage"
                        anchors.fill: parent
                        tools: commonToolBar
                        YearView{
                            onMonthSelected: {
                                tabs.selectedTabIndex = 1
                                var now = DateExt.today();
                                if( date.getMonth() === now.getMonth()
                                        && date.getFullYear() === now.getFullYear()) {
                                    monthView.currentMonth = now
                                } else {
                                    monthView.currentMonth = date.midnight();
                                }
                            }
                        }
                    }
                }
                Tab {
                    id: monthTab
                    title: i18n.tr("Month")
                    page: MonthView{
                        anchors.fill: parent
                        tools: commonToolBar
                        id: monthView

                        onDateSelected: {
                            tabs.selectedTabIndex  = 3
                            tabPage.currentDay = date;
                        }
                    }
                }
                Tab{
                    objectName: "weekTab"
                    title: i18n.tr("Week")
                    page: Page{
                        anchors.fill: parent
                        tools: commonToolBar
                        WeekView{
                            id: weekView
                            anchors.fill: parent

                            onDayStartChanged: {
                                tabPage.currentDay = dayStart;
                            }
                        }
                    }
                }

                Tab{
                    objectName: "dayTab"
                    title: i18n.tr("Day")
                    page: Page{
                        anchors.fill: parent
                        tools: commonToolBar
                        DayView{
                            id: dayView
                            anchors.fill: parent

                            onCurrentDayChanged: {
                                tabPage.currentDay = currentDay;
                            }
                        }
                    }
                }
            }
        }
    }
}