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

412.3.5 by Mihir Soni
added copyright
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
412.3.2 by Mihir Soni
Used page instead of dialog
19
import QtQuick 2.3
20
import Ubuntu.Components 1.1
21
import QtOrganizer 5.0
22
import "Defines.js" as Defines
471.2.11 by Mihir Soni
Removed hard code index values
23
import "Recurrence.js" as Recurrence
412.3.2 by Mihir Soni
Used page instead of dialog
24
25
26
QtObject{
27
    id:eventUtil
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
28
412.3.2 by Mihir Soni
Used page instead of dialog
29
    function getWeekDaysIndex(daysOfWeek){
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
30
        for (var index = Recurrence.Weekdays; index < Recurrence.OnDiffDays; index++) {
31
            if (compareArrays(daysOfWeek, Recurrence.weeklyDays[index])) {
32
                return index;
471.2.11 by Mihir Soni
Removed hard code index values
33
            }
471.2.1 by Mihir Soni
Rewritting getRecurrenceString function in event Util
34
        }
471.2.11 by Mihir Soni
Removed hard code index values
35
        return Recurrence.OnDiffDays;
412.3.2 by Mihir Soni
Used page instead of dialog
36
    }
37
471.2.7 by Mihir Soni
fixed bug #1370431
38
    function compareArrays(daysOfWeek, actualArray) {
412.3.2 by Mihir Soni
Used page instead of dialog
39
        if (daysOfWeek.length !== actualArray.length) return false;
40
        for (var i = 0; i < actualArray.length; i++) {
41
            if (daysOfWeek[i] !== actualArray[i]) return false;
42
        }
43
        return true;
44
    }
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
45
471.2.7 by Mihir Soni
fixed bug #1370431
46
    function getDaysOfWeek(index, weekDays) {
412.3.2 by Mihir Soni
Used page instead of dialog
47
        var daysOfWeek = [];
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
48
        if (index !== Recurrence.OnDiffDays) {
49
            daysOfWeek = Recurrence.weeklyDays[index];
50
        } else {
412.3.2 by Mihir Soni
Used page instead of dialog
51
            daysOfWeek = weekDays.length === 0 ? [date.getDay()] : weekDays;
52
        }
53
        return daysOfWeek;
54
    }
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
55
412.3.3 by Mihir Soni
Logic upated
56
    //Function to get Weeknames in narrow Format
57
    function getDays(daysOfWeek) {
58
        var days = []
471.2.3 by Mihir Soni
reformmated code again
59
        for (var j = 0; j < daysOfWeek.length; ++j) {
412.3.3 by Mihir Soni
Logic upated
60
            //push all days
471.2.3 by Mihir Soni
reformmated code again
61
            days.push(Qt.locale().dayName(daysOfWeek[j], Locale.NarrowFormat))
412.3.3 by Mihir Soni
Logic upated
62
        }
63
        days = days.join(', ');
64
        return days;
65
    }
66
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
67
    function getString(rule,recurrence){
68
        var dateFormat = Qt.locale().dateFormat(Locale.LongFormat);
69
        var str = "";
70
        if (rule.limit === undefined) {
71
            str = i18n.tr(recurrence)
72
        } else if (rule.limit !== undefined && parseInt(rule.limit)) {
73
            // TRANSLATORS: the argument refers to multiple recurrence of event with count .
74
            // E.g. "Daily; 5 times."
75
            str = i18n.tr("%1; %2 time", "%1; %2 times", rule.limit).arg(recurrence).arg(rule.limit)
76
        } else {
77
            // TRANSLATORS: the argument refers to recurrence until user selected date.
78
            // E.g. "Daily; until 12/12/2014."
79
            str = i18n.tr("%1; until %2").arg(recurrence).arg(rule.limit.toLocaleString(Qt.locale(), dateFormat))
80
        }
81
        return str;
82
    }
83
471.2.1 by Mihir Soni
Rewritting getRecurrenceString function in event Util
84
    function getRecurrenceString(rule) {
85
        var index = rule.frequency;
86
        var recurrence = "";
87
        var str = "";
88
        //Check if reccurence is weekly or not
89
        if (index === RecurrenceRule.Weekly) {
90
            index = getWeekDaysIndex(rule.daysOfWeek.sort())
91
            // We are using a custom index
92
            // because we have more options than the Qt RecurrenceRule enum.
93
        } else if (index === RecurrenceRule.Monthly) {
471.2.11 by Mihir Soni
Removed hard code index values
94
            index = Recurrence.Monthly // If reccurence is Monthly
471.2.1 by Mihir Soni
Rewritting getRecurrenceString function in event Util
95
        } else if (index === RecurrenceRule.Yearly) {
471.2.11 by Mihir Soni
Removed hard code index values
96
            index = Recurrence.Yearly // If reccurence is Yearly
471.2.1 by Mihir Soni
Rewritting getRecurrenceString function in event Util
97
        }
98
        // if reccurrence is on different days.
471.2.11 by Mihir Soni
Removed hard code index values
99
        if (index === Recurrence.OnDiffDays) {
471.2.1 by Mihir Soni
Rewritting getRecurrenceString function in event Util
100
            // TRANSLATORS: the argument refers to several different days of the week.
101
            // E.g. "Weekly on Mondays, Tuesdays"
102
            recurrence += i18n.tr("Weekly on %1").arg(getDays(rule.daysOfWeek.sort()))
103
        } else {
104
            recurrence += Defines.recurrenceLabel[index]
105
        }
471.2.13 by Mihir Soni
More Code smilplfied , addressed all comments
106
        str = getString(rule,recurrence);
412.3.2 by Mihir Soni
Used page instead of dialog
107
        return str;
108
    }
109
}