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

« back to all changes in this revision

Viewing changes to calendar.qml

  • Committer: Kunal Parmar
  • Date: 2013-09-28 09:37:52 UTC
  • mto: This revision was merged to the branch mainline in revision 153.
  • Revision ID: pkunal.parmar@gmail.com-20130928093752-6hhmsea2xmtns5uk
Background added to day labels

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013-2014 Canonical Ltd
3
 
 *
4
 
 * This file is part of Ubuntu Calendar App
5
 
 *
6
 
 * Ubuntu Calendar App is free software: you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License version 3 as
8
 
 * published by the Free Software Foundation.
9
 
 *
10
 
 * Ubuntu Calendar App is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
import QtQuick 2.3
19
 
import Ubuntu.Components 1.1
20
 
import Ubuntu.Components.Popups 1.0
21
 
import QtOrganizer 5.0
 
1
import QtQuick 2.0
 
2
import Ubuntu.Components 0.1
 
3
import Ubuntu.Components.Popups 0.1
22
4
 
23
5
import "dateExt.js" as DateExt
24
6
 
25
7
MainView {
26
8
    id: mainView
27
 
    useDeprecatedToolbar: false
28
 
 
29
 
    // Work-around until this branch lands:
30
 
    // https://code.launchpad.net/~tpeeters/ubuntu-ui-toolkit/optIn-tabsDrawer/+merge/212496
31
 
    //property bool windowActive: typeof window != 'undefined'
32
 
    //onWindowActiveChanged: window.title = i18n.tr("Calendar")
33
 
 
34
 
    // Argument during startup
35
 
    Arguments {
36
 
        id: args;
37
 
 
38
 
        // Example of argument: calendar:///new-event
39
 
 
40
 
        // IMPORTANT
41
 
        // Due to bug #1231558 you have to pass arguments BEFORE app:
42
 
        // qmlscene calendar:///new-event calendar.qml
43
 
 
44
 
        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");
45
 
        //defaultArgument.required: false;
46
 
        defaultArgument.valueNames: ["URL"]
47
 
 
48
 
        /* ARGUMENTS on startup
49
 
         * (no one is required)
50
 
         *
51
 
         * Create a new event
52
 
         * Keyword: newevent
53
 
         *
54
 
         * Create a new event. If starttime or endtime are set they are used to set start and end time of the new event.
55
 
         * It accepts no value.
56
 
         *
57
 
         *
58
 
         * Choose the view
59
 
         * Keyword: starttime
60
 
         *
61
 
         * If newevent has been called, starttime is the start time of event. Otherwise is the day on which app is focused on startup.
62
 
         * It accepts an integer value of the number of seconds since UNIX epoch in the UTC timezone.
63
 
         * 0 means today.
64
 
         *
65
 
         * Keyword: endtime
66
 
         *
67
 
         * If newevent is set it's the end time of the event, has to be > of starttime.
68
 
         * If newevent isn't set and startime is set, its value is used to choose the right view.
69
 
         * If neither of precendet flags are set, endtime is ignored.
70
 
         * It accepts an integer value of the number of seconds since UNIX epoch in the UTC timezone.
71
 
         *
72
 
         *
73
 
         * Open an existing event
74
 
         * Keyword: eventid (provisional)
75
 
         *
76
 
         * It takes a id of an event and open that event on full page
77
 
         */
78
 
        Argument {
79
 
            name: "eventid"
80
 
            required: false
81
 
            valueNames: ["EVENT_ID"]
82
 
        }
83
 
    }
84
9
 
85
10
    objectName: "calendar"
86
 
    applicationName: "com.ubuntu.calendar"
 
11
    applicationName: "calendar-app"
87
12
 
88
 
    width: units.gu(100)
 
13
    width: units.gu(45)
89
14
    height: units.gu(80)
90
 
    focus: true
91
 
    Keys.forwardTo: [pageStack.currentPage]
92
 
 
93
 
    headerColor: "#E8E8E8"
94
 
    backgroundColor: "#f5f5f5"
95
 
    footerColor: "#ECECEC"
96
 
    anchorToKeyboard: true
97
 
 
 
15
 
 
16
    headerColor: "#266249"
 
17
    backgroundColor: "#478158"
 
18
    footerColor: "#478158"
98
19
 
99
20
    PageStack {
100
21
        id: pageStack
101
22
 
102
 
        Component.onCompleted: push(tabs)
103
 
 
104
 
        // This is for wait that the app is load when newEvent is invoked by argument
105
 
        Timer {
106
 
            id: timer
107
 
            interval: 200; running: false; repeat: false
108
 
            onTriggered: {
109
 
                tabs.newEvent();
110
 
            }
111
 
        }
112
 
 
113
 
        Timer {
114
 
            id: applyFilterTimer
115
 
            interval: 200; running: false; repeat: false
116
 
            onTriggered: {
117
 
                eventModel.applyFilterFinal();
118
 
            }
119
 
        }
120
 
 
121
 
        UnionFilter {
122
 
            id: itemTypeFilter
123
 
            DetailFieldFilter{
124
 
                id: eventFilter
125
 
                detail: Detail.ItemType;
126
 
                field: Type.FieldType
127
 
                value: Type.Event
128
 
                matchFlags: Filter.MatchExactly
129
 
            }
130
 
 
131
 
            DetailFieldFilter{
132
 
                id: eventOccurenceFilter
133
 
                detail: Detail.ItemType;
134
 
                field: Type.FieldType
135
 
                value: Type.EventOccurrence
136
 
                matchFlags: Filter.MatchExactly
137
 
            }
138
 
        }
139
 
 
140
 
        CollectionFilter{
141
 
            id: collectionFilter
142
 
        }
143
 
 
144
 
        EventListModel{
145
 
            id: eventModel
146
 
 
147
 
            autoUpdate: true
148
 
            startPeriod: tabs.currentDay
149
 
            endPeriod: tabs.currentDay
150
 
 
151
 
            filter: IntersectionFilter {
152
 
                filters: [ collectionFilter, itemTypeFilter]
153
 
            }
154
 
 
155
 
            function delayedApplyFilter() {
156
 
                applyFilterTimer.restart();
157
 
            }
158
 
 
159
 
            function applyFilterFinal() {
160
 
                var collectionIds = [];
161
 
                var collections = eventModel.getCollections();
162
 
                for(var i=0; i < collections.length ; ++i) {
163
 
                    var collection = collections[i]
164
 
                    if(collection.extendedMetaData("collection-selected") === true) {
165
 
                        collectionIds.push(collection.collectionId);
166
 
                    }
167
 
                }
168
 
                collectionFilter.ids = collectionIds;
169
 
            }
170
 
 
171
 
            Component.onCompleted: {
172
 
                delayedApplyFilter();
173
 
 
174
 
                if (args.values.eventid) {
175
 
                    var requestId = "";
176
 
                    eventModel.onItemsFetched.connect( function(id,fetchedItems) {
177
 
                        if( requestId === id && fetchedItems.length > 0 ) {
178
 
                            var event = fetchedItems[0];
179
 
                            pageStack.push(Qt.resolvedUrl("EventDetails.qml"),{"event":event,"model": eventModel});
180
 
                        }
181
 
                    });
182
 
                    requestId = eventModel.fetchItems([args.values.eventid]);
183
 
                }
184
 
            }
185
 
        }
186
 
 
187
 
        Tabs{
188
 
            id: tabs
189
 
            Keys.forwardTo: [tabs.currentPage.item]
 
23
        Component.onCompleted: push(tabPage)
 
24
 
 
25
        Page{
 
26
            id: tabPage
190
27
 
191
28
            property var currentDay: DateExt.today();
192
29
 
193
 
            // Arguments on startup
194
 
            property bool newevent: false;
195
 
            property int starttime: -1;
196
 
            property int endtime: -1;
197
 
 
198
 
            selectedTabIndex: weekTab.index
 
30
            onCurrentDayChanged: {
 
31
                if( monthView.currentMonth !== undefined && !monthView.currentMonth.isSameDay(currentDay))
 
32
                    monthView.currentMonth = currentDay.midnight();
 
33
 
 
34
                if( !dayView.currentDay.isSameDay(currentDay))
 
35
                    dayView.currentDay = currentDay
 
36
 
 
37
                if( !weekView.dayStart.isSameDay(currentDay))
 
38
                    weekView.dayStart = currentDay
 
39
            }
199
40
 
200
41
            function newEvent() {
201
 
                var startDate = new Date();
202
 
                var endDate = new Date();
203
 
                var startTime;
204
 
                var endTime;
205
 
 
206
 
                if (starttime === 0) { // startime 0 means now
207
 
                    if (endtime !== -1) { // If also endtime has been invoked
208
 
                        endTime = parseInt(endtime);
209
 
                        if (endTime > startDate) // If endtime is after startime
210
 
                            endDate = new Date(endTime);
211
 
                    }
212
 
                }
213
 
                else if (starttime !== -1) { // If starttime has been invoked
214
 
                    startTime = parseInt(starttime);
215
 
                    startDate = new Date(startTime);
216
 
                    if (endtime !== -1) { // If --endtime has been invoked
217
 
                        endTime = parseInt(endtime);
218
 
                        if (endTime > startDate)
219
 
                            endDate = new Date(endTime);
220
 
                    }
221
 
                }
222
 
                //pageStack.push(Qt.resolvedUrl("NewEvent.qml"),{"startDate": startDate, "endDate": endDate, //"model":eventModel});
223
 
            }
224
 
 
225
 
            // This function calculate the difference between --endtime and --starttime and choose the better view
226
 
            function calculateDifferenceStarttimeEndtime(startTime, endTime) {
227
 
                var minute = 60 * 1000;
228
 
                var hour = 60 * minute;
229
 
                var day = 24 * hour;
230
 
                var month = 30 * day;
231
 
 
232
 
                var difference = endTime - startTime;
233
 
 
234
 
                if (difference > month)
235
 
                    return yearTab.index;   // Year view
236
 
                else if (difference > 7 * day)
237
 
                    return monthTab.index;   // Month view}
238
 
                else if (difference > day)
239
 
                    return weekTab.index;   // Week view
240
 
                else
241
 
                    return dayTab.index;   // Day view
242
 
            }
243
 
 
244
 
            // This function parse the argument
245
 
            function parseArguments(url) {
246
 
                var newevenpattern= new RegExp ("newevent");
247
 
                var starttimepattern = new RegExp ("starttime=\\d+");
248
 
                var endtimepattern = new RegExp ("endtime=\\d+");
249
 
 
250
 
                newevent = newevenpattern.test(url);
251
 
 
252
 
                if (starttimepattern.test(url))
253
 
                    starttime = url.match(/starttime=(\d+)/)[0].replace("starttime=", '');
254
 
 
255
 
                if (endtimepattern.test(url))
256
 
                    endtime = url.match(/endtime=(\d+)/)[0].replace("endtime=", '');
 
42
                 PopupUtils.open(newEventComponent, tabPage, {"defaultDate": currentDay})
257
43
            }
258
44
 
259
45
            Component.onCompleted: {
260
 
                // If an url has been set
261
 
                if (args.defaultArgument.at(0)) {
262
 
                    parseArguments(args.defaultArgument.at(0))
263
 
                    tabs.currentDay = new Date()
264
 
                    // If newevent has been called on startup
265
 
                    if (newevent) {
266
 
                        timer.running = true;
267
 
                    }
268
 
                    else if (starttime !== -1) { // If no newevent has been setted, but starttime
269
 
                        var startTime = parseInt(starttime);
270
 
                        tabs.currentDay = new Date(startTime);
271
 
 
272
 
                        // If also endtime has been settend
273
 
                        if (endtime !== -1) {
274
 
                            var endTime = parseInt(endtime);
275
 
                            tabs.selectedTabIndex = calculateDifferenceStarttimeEndtime(startTime, endTime);
276
 
                        }
277
 
                        else {
278
 
                            // If no endtime has been setted, open the starttime date in day view
279
 
                            tabs.selectedTabIndex = dayTab.index;
280
 
                        }
281
 
                    } // End of else if (starttime)
282
 
                    else {
283
 
                        // Due to bug #1231558 {if (args.defaultArgument.at(0))} is always true
284
 
                        // After the fix we can delete this else
285
 
                        tabs.selectedTabIndex = weekTab.index;
286
 
                    }
287
 
                } // End of if about args.values
288
 
                else {
289
 
                    tabs.selectedTabIndex = weekTab.index;
290
 
                }
291
 
            } // End of Component.onCompleted:
292
 
 
293
 
            EventActions {
294
 
                id: commonHeaderActions
295
 
            }
296
 
 
297
 
            Keys.onTabPressed: {
298
 
                if( event.modifiers & Qt.ControlModifier) {
299
 
                    var currentTab = tabs.selectedTabIndex;
300
 
                    currentTab ++;
301
 
                    if( currentTab >= tabs.tabChildren.length){
302
 
                        currentTab = 0;
303
 
                    }
304
 
                    tabs.selectedTabIndex = currentTab;
305
 
                }
306
 
            }
307
 
 
308
 
            Keys.onBacktabPressed: {
309
 
                if( event.modifiers & Qt.ControlModifier) {
310
 
                    var currentTab = tabs.selectedTabIndex;
311
 
                    currentTab --;
312
 
                    if( currentTab < 0){
313
 
                        currentTab = tabs.tabChildren.length -1;
314
 
                    }
315
 
                    tabs.selectedTabIndex = currentTab;
316
 
                }
317
 
            }
318
 
 
319
 
            Tab{
320
 
                id: yearTab
321
 
                objectName: "yearTab"
322
 
                title: i18n.tr("Year")
323
 
                page: Loader{
324
 
                    id: yearViewLoader
325
 
                    objectName: "yearViewLoader"
326
 
                    source: tabs.selectedTab == yearTab ? Qt.resolvedUrl("YearView.qml"):""
327
 
                    onLoaded: {
328
 
                        item.currentYear = tabs.currentDay.getFullYear();
329
 
                    }
330
 
 
331
 
                    anchors{
332
 
                        left: parent.left
333
 
                        right: parent.right
334
 
                        bottom: parent.bottom
335
 
                    }
336
 
 
337
 
                    Connections{
338
 
                        target: yearViewLoader.item
339
 
                        onMonthSelected: {
340
 
                            var now = DateExt.today();
341
 
                            if( date.getMonth() === now.getMonth()
342
 
                                    && date.getFullYear() === now.getFullYear()) {
343
 
                                tabs.currentDay = now;
344
 
                            } else {
345
 
                                tabs.currentDay = date.midnight();
346
 
                            }
347
 
                            tabs.selectedTabIndex = monthTab.index;
348
 
                        }
349
 
                    }
350
 
                }
351
 
            }
352
 
 
353
 
            Tab{
354
 
                id: monthTab
355
 
                objectName: "monthTab"
356
 
                title: i18n.tr("Month")
357
 
                page: Loader{
358
 
                    id: monthViewLoader
359
 
                    objectName: "monthViewLoader"
360
 
                    source: tabs.selectedTab == monthTab ? Qt.resolvedUrl("MonthView.qml"):""
361
 
                    onLoaded: {
362
 
                        item.currentMonth = tabs.currentDay.midnight();
363
 
                    }
364
 
 
365
 
                    anchors{
366
 
                        left: parent.left
367
 
                        right: parent.right
368
 
                        bottom: parent.bottom
369
 
                    }
370
 
 
371
 
                    Connections{
372
 
                        target: monthViewLoader.item
373
 
                        onDateSelected: {
374
 
                            tabs.currentDay = date;
375
 
                            tabs.selectedTabIndex = dayTab.index;
376
 
                        }
377
 
                    }
378
 
                }
379
 
            }
380
 
 
381
 
            Tab{
382
 
                id: weekTab
383
 
                objectName: "weekTab"
384
 
                title: i18n.tr("Week")
385
 
                page: Loader{
386
 
                    id: weekViewLoader
387
 
                    objectName: "weekViewLoader"
388
 
                    source: tabs.selectedTab == weekTab ? Qt.resolvedUrl("WeekView.qml"):""
389
 
                    onLoaded: {
390
 
                        item.isCurrentPage= Qt.binding(function() { return tabs.selectedTab == weekTab })
391
 
                        item.dayStart = tabs.currentDay;
392
 
                    }
393
 
 
394
 
                    anchors{
395
 
                        left: parent.left
396
 
                        right: parent.right
397
 
                        bottom: parent.bottom
398
 
                    }
399
 
 
400
 
                    Connections{
401
 
                        target: weekViewLoader.item
402
 
                        onDayStartChanged: {
403
 
                            tabs.currentDay = weekViewLoader.item.dayStart;
404
 
                        }
405
 
 
406
 
                        onDateSelected: {
407
 
                            tabs.currentDay = date;
408
 
                            tabs.selectedTabIndex = dayTab.index;
409
 
                        }
410
 
                    }
411
 
                }
412
 
            }
413
 
 
414
 
            Tab{
415
 
                id: dayTab
416
 
                objectName: "dayTab"
417
 
                title: i18n.tr("Day")
418
 
                page: Loader{
419
 
                    id: dayViewLoader
420
 
                    objectName: "dayViewLoader"
421
 
                    source: tabs.selectedTab == dayTab ? Qt.resolvedUrl("DayView.qml"):""
422
 
                    onLoaded: {
423
 
                        item.isCurrentPage= Qt.binding(function() { return tabs.selectedTab == dayTab })
424
 
                        item.currentDay = tabs.currentDay;
425
 
                    }
426
 
 
427
 
                    anchors{
428
 
                        left: parent.left
429
 
                        right: parent.right
430
 
                        bottom: parent.bottom
431
 
                    }
432
 
 
433
 
                    Connections{
434
 
                        target: dayViewLoader.item
435
 
                        onCurrentDayChanged: {
436
 
                            tabs.currentDay = dayViewLoader.item.currentDay;
437
 
                        }
438
 
 
439
 
                        onDateSelected: {
440
 
                            tabs.currentDay = date;
441
 
                        }
442
 
                    }
443
 
                }
444
 
            }
445
 
 
446
 
            Tab {
447
 
                id: agendaTab
448
 
                objectName: "agendaTab"
449
 
                title: i18n.tr("Agenda")
450
 
                page: Loader {
451
 
                    id: agendaViewLoader
452
 
                    objectName: "agendaViewLoader"
453
 
                    source: tabs.selectedTab == agendaTab ? Qt.resolvedUrl("AgendaView.qml"):""
454
 
 
455
 
                    anchors{
456
 
                        left: parent.left
457
 
                        right: parent.right
458
 
                        bottom: parent.bottom
459
 
                    }
460
 
 
461
 
                    Connections{
462
 
                        target: agendaViewLoader.item
463
 
                        onDateSelected: {
464
 
                            tabs.currentDay = date;
465
 
                            tabs.selectedTabIndex = dayTab.index;
466
 
                        }
467
 
                    }
468
 
                }
 
46
                tabs.selectedTabIndex= 1;
 
47
            }
 
48
 
 
49
            ToolbarItems {
 
50
                id: commonToolBar
 
51
 
 
52
                ToolbarButton {
 
53
                    objectName: "neweventbutton"
 
54
                    action: Action {
 
55
                        iconSource: Qt.resolvedUrl("avatar.png")
 
56
                        text: i18n.tr("New Event")
 
57
                        onTriggered: tabPage.newEvent()
 
58
                    }
 
59
                }                    
 
60
                ToolbarButton {
 
61
                    objectName: "todaybutton"
 
62
                    action: Action {
 
63
                        iconSource: Qt.resolvedUrl("avatar.png");
 
64
                        text: i18n.tr("Today");
 
65
                        onTriggered: {
 
66
                            tabPage.currentDay = (new Date()).midnight();
 
67
                        }
 
68
                    }
 
69
                }
 
70
            }
 
71
 
 
72
            Tabs{
 
73
                id: tabs
 
74
                Tab{
 
75
                    title: i18n.tr("Year")
 
76
                    page: Page{
 
77
                        anchors.fill: parent
 
78
                        tools: commonToolBar
 
79
                        YearView{
 
80
                            onMonthSelected: {
 
81
                                tabs.selectedTabIndex = 1
 
82
                                var now = DateExt.today();
 
83
                                if( date.getMonth() === now.getMonth()
 
84
                                        && date.getFullYear() === now.getFullYear()) {
 
85
                                    monthView.currentMonth = now
 
86
                                } else {
 
87
                                    monthView.currentMonth = date.midnight();
 
88
                                }
 
89
                            }
 
90
                        }
 
91
                    }
 
92
                }
 
93
                Tab {
 
94
                    id: monthTab
 
95
                    title: i18n.tr("Month")
 
96
                    page: MonthView{
 
97
                        anchors.fill: parent
 
98
                        tools: commonToolBar
 
99
                        id: monthView
 
100
 
 
101
                        onDateSelected: {
 
102
                            tabs.selectedTabIndex  = 3
 
103
                            tabPage.currentDay = date;
 
104
                        }
 
105
                    }
 
106
                }
 
107
                Tab{
 
108
                    title: i18n.tr("Week")
 
109
                    page: Page{
 
110
                        anchors.fill: parent
 
111
                        tools: commonToolBar
 
112
                        WeekView{
 
113
                            id: weekView
 
114
                            anchors.fill: parent
 
115
 
 
116
                            onDayStartChanged: {
 
117
                                tabPage.currentDay = dayStart;
 
118
                            }
 
119
                        }
 
120
                    }
 
121
                }
 
122
 
 
123
                Tab{
 
124
                    title: i18n.tr("Day")
 
125
                    page: Page{
 
126
                        anchors.fill: parent
 
127
                        tools: commonToolBar
 
128
                        DayView{
 
129
                            id: dayView
 
130
                            anchors.fill: parent
 
131
 
 
132
                            onCurrentDayChanged: {
 
133
                                tabPage.currentDay = currentDay;
 
134
                            }
 
135
                        }
 
136
                    }
 
137
                }
 
138
            }
 
139
 
 
140
            Component {
 
141
                id: newEventComponent
 
142
                NewEvent {}
469
143
            }
470
144
        }
471
145
    }