~pkunal-parmar/ubuntu-calendar-app/Minor-Performance

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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright (C) 2013-2014 Canonical Ltd
 *
 * This file is part of Ubuntu Calendar App
 *
 * Ubuntu Calendar App is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * Ubuntu Calendar App is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import QtQuick 2.3
import Ubuntu.Components 1.1
import Ubuntu.Components.Popups 1.0
import QtOrganizer 5.0
import Ubuntu.SyncMonitor 0.1

import "dateExt.js" as DateExt

MainView {
    id: mainView
    useDeprecatedToolbar: false

    // Work-around until this branch lands:
    // https://code.launchpad.net/~tpeeters/ubuntu-ui-toolkit/optIn-tabsDrawer/+merge/212496
    //property bool windowActive: typeof window != 'undefined'
    //onWindowActiveChanged: window.title = i18n.tr("Calendar")

    // 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 four arguments: --starttime, --endtime, --newevent and --eventid. 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.
         *
         *
         * Open an existing event
         * Keyword: eventid (provisional)
         *
         * It takes a id of an event and open that event on full page
         */
        Argument {
            name: "eventid"
            required: false
            valueNames: ["EVENT_ID"]
        }
    }

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

    width: units.gu(100)
    height: units.gu(80)
    focus: true
    Keys.forwardTo: [pageStack.currentPage]

    headerColor: "#E8E8E8"
    backgroundColor: "#f5f5f5"
    footerColor: "#ECECEC"
    anchorToKeyboard: true

    SyncMonitor {
        id: syncMonitor
    }

    PageStack {
        id: pageStack

        Component.onCompleted: push(tabs)

        // 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: {
                tabs.newEvent();
            }
        }

        Timer {
            id: applyFilterTimer
            interval: 200; running: false; repeat: false
            onTriggered: {
                eventModel.applyFilterFinal();
            }
        }

        UnionFilter {
            id: itemTypeFilter
            DetailFieldFilter{
                id: eventFilter
                detail: Detail.ItemType;
                field: Type.FieldType
                value: Type.Event
                matchFlags: Filter.MatchExactly
            }

            DetailFieldFilter{
                id: eventOccurenceFilter
                detail: Detail.ItemType;
                field: Type.FieldType
                value: Type.EventOccurrence
                matchFlags: Filter.MatchExactly
            }
        }

        CollectionFilter{
            id: collectionFilter
        }

        EventListModel{
            id: eventModel

            autoUpdate: true
            startPeriod: tabs.currentDay
            endPeriod: tabs.currentDay

            filter: IntersectionFilter {
                filters: [ collectionFilter, itemTypeFilter]
            }

            function delayedApplyFilter() {
                applyFilterTimer.restart();
            }

            function applyFilterFinal() {
                var collectionIds = [];
                var collections = eventModel.getCollections();
                for(var i=0; i < collections.length ; ++i) {
                    var collection = collections[i]
                    if(collection.extendedMetaData("collection-selected") === true) {
                        collectionIds.push(collection.collectionId);
                    }
                }
                collectionFilter.ids = collectionIds;
            }

            Component.onCompleted: {
                delayedApplyFilter();

                if (args.values.eventid) {
                    var requestId = "";
                    eventModel.onItemsFetched.connect( function(id,fetchedItems) {
                        if( requestId === id && fetchedItems.length > 0 ) {
                            var event = fetchedItems[0];
                            pageStack.push(Qt.resolvedUrl("EventDetails.qml"),{"event":event,"model": eventModel});
                        }
                    });
                    requestId = eventModel.fetchItems([args.values.eventid]);
                }
            }
        }

        Tabs{
            id: tabs
            Keys.forwardTo: [tabs.currentPage.item]

            property var currentDay: DateExt.today();

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

            selectedTabIndex: monthTab.index

            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, //"model":eventModel});
            }

            // 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 yearTab.index;   // Year view
                else if (difference > 7 * day)
                    return monthTab.index;   // Month view}
                else if (difference > day)
                    return weekTab.index;   // Week view
                else
                    return dayTab.index;   // 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))
                    tabs.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);
                        tabs.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;
                }
            } // End of Component.onCompleted:

            EventActions {
                id: commonHeaderActions
            }

            Keys.onTabPressed: {
                if( event.modifiers & Qt.ControlModifier) {
                    var currentTab = tabs.selectedTabIndex;
                    currentTab ++;
                    if( currentTab >= tabs.tabChildren.length){
                        currentTab = 0;
                    }
                    tabs.selectedTabIndex = currentTab;
                }
            }

            Keys.onBacktabPressed: {
                if( event.modifiers & Qt.ControlModifier) {
                    var currentTab = tabs.selectedTabIndex;
                    currentTab --;
                    if( currentTab < 0){
                        currentTab = tabs.tabChildren.length -1;
                    }
                    tabs.selectedTabIndex = currentTab;
                }
            }

            Tab{
                id: yearTab
                objectName: "yearTab"
                title: i18n.tr("Year")
                page: Loader{
                    id: yearViewLoader
                    objectName: "yearViewLoader"
                    source: tabs.selectedTab == yearTab ? Qt.resolvedUrl("YearView.qml"):""
                    onLoaded: {
                        item.currentYear = tabs.currentDay.getFullYear();
                    }

                    anchors{
                        left: parent.left
                        right: parent.right
                        bottom: parent.bottom
                    }

                    Connections{
                        target: yearViewLoader.item
                        onMonthSelected: {
                            var now = DateExt.today();
                            if( date.getMonth() === now.getMonth()
                                    && date.getFullYear() === now.getFullYear()) {
                                tabs.currentDay = now;
                            } else {
                                tabs.currentDay = date.midnight();
                            }
                            tabs.selectedTabIndex = monthTab.index;
                        }
                    }
                }
            }

            Tab{
                id: monthTab
                objectName: "monthTab"
                title: i18n.tr("Month")
                page: Loader{
                    id: monthViewLoader
                    objectName: "monthViewLoader"
                    source: tabs.selectedTab == monthTab ? Qt.resolvedUrl("MonthView.qml"):""
                    onLoaded: {
                        item.currentMonth = tabs.currentDay.midnight();
                    }

                    anchors{
                        left: parent.left
                        right: parent.right
                        bottom: parent.bottom
                    }

                    Connections{
                        target: monthViewLoader.item
                        onDateSelected: {
                            tabs.currentDay = date;
                            tabs.selectedTabIndex = dayTab.index;
                        }
                    }
                }
            }

            Tab{
                id: weekTab
                objectName: "weekTab"
                title: i18n.tr("Week")
                page: Loader{
                    id: weekViewLoader
                    objectName: "weekViewLoader"
                    source: tabs.selectedTab == weekTab ? Qt.resolvedUrl("WeekView.qml"):""
                    onLoaded: {
                        item.isCurrentPage= Qt.binding(function() { return tabs.selectedTab == weekTab })
                        item.dayStart = tabs.currentDay;
                    }

                    anchors{
                        left: parent.left
                        right: parent.right
                        bottom: parent.bottom
                    }

                    Connections{
                        target: weekViewLoader.item
                        onDayStartChanged: {
                            tabs.currentDay = weekViewLoader.item.dayStart;
                        }

                        onDateSelected: {
                            tabs.currentDay = date;
                            tabs.selectedTabIndex = dayTab.index;
                        }
                    }
                }
            }

            Tab{
                id: dayTab
                objectName: "dayTab"
                title: i18n.tr("Day")
                page: Loader{
                    id: dayViewLoader
                    objectName: "dayViewLoader"
                    source: tabs.selectedTab == dayTab ? Qt.resolvedUrl("DayView.qml"):""
                    onLoaded: {
                        item.isCurrentPage= Qt.binding(function() { return tabs.selectedTab == dayTab })
                        item.currentDay = tabs.currentDay;
                    }

                    anchors{
                        left: parent.left
                        right: parent.right
                        bottom: parent.bottom
                    }

                    Connections{
                        target: dayViewLoader.item
                        onCurrentDayChanged: {
                            tabs.currentDay = dayViewLoader.item.currentDay;
                        }

                        onDateSelected: {
                            tabs.currentDay = date;
                        }
                    }
                }
            }

            Tab {
                id: agendaTab
                objectName: "agendaTab"
                title: i18n.tr("Agenda")
                page: Loader {
                    id: agendaViewLoader
                    objectName: "agendaViewLoader"
                    source: tabs.selectedTab == agendaTab ? Qt.resolvedUrl("AgendaView.qml"):""

                    onLoaded: {
                        item.currentDay = tabs.currentDay;
                    }

                    anchors{
                        left: parent.left
                        right: parent.right
                        bottom: parent.bottom
                    }

                    Connections{
                        target: agendaViewLoader.item
                        onDateSelected: {
                            tabs.currentDay = date;
                            tabs.selectedTabIndex = dayTab.index;
                        }
                    }
                }
            }
        }
    }
}