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

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
/*
 * 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 QtOrganizer 5.0
import "Defines.js" as Defines
import "Recurrence.js" as Recurrence


QtObject{
    id:eventUtil

    function getWeekDaysIndex(daysOfWeek){
        for (var index = Recurrence.Weekdays; index < Recurrence.OnDiffDays; index++) {
            if (compareArrays(daysOfWeek, Recurrence.weeklyDays[index])) {
                return index;
            }
        }
        return Recurrence.OnDiffDays;
    }

    function compareArrays(daysOfWeek, actualArray) {
        if (daysOfWeek.length !== actualArray.length) return false;
        for (var i = 0; i < actualArray.length; i++) {
            if (daysOfWeek[i] !== actualArray[i]) return false;
        }
        return true;
    }

    function getDaysOfWeek(index, weekDays) {
        var daysOfWeek = [];
        if (index !== Recurrence.OnDiffDays) {
            daysOfWeek = Recurrence.weeklyDays[index];
        } else {
            daysOfWeek = weekDays.length === 0 ? [date.getDay()] : weekDays;
        }
        return daysOfWeek;
    }

    //Function to get Weeknames in narrow Format
    function getDays(daysOfWeek) {
        var days = []
        for (var j = 0; j < daysOfWeek.length; ++j) {
            //push all days
            days.push(Qt.locale().dayName(daysOfWeek[j], Locale.NarrowFormat))
        }
        days = days.join(', ');
        return days;
    }

    function getString(rule,recurrence){
        var dateFormat = Qt.locale().dateFormat(Locale.LongFormat);
        var str = "";
        if (rule.limit === undefined) {
            str = i18n.tr(recurrence)
        } else if (rule.limit !== undefined && parseInt(rule.limit)) {
            // TRANSLATORS: the argument refers to multiple recurrence of event with count .
            // E.g. "Daily; 5 times."
            str = i18n.tr("%1; %2 time", "%1; %2 times", rule.limit).arg(recurrence).arg(rule.limit)
        } else {
            // TRANSLATORS: the argument refers to recurrence until user selected date.
            // E.g. "Daily; until 12/12/2014."
            str = i18n.tr("%1; until %2").arg(recurrence).arg(rule.limit.toLocaleString(Qt.locale(), dateFormat))
        }
        return str;
    }

    function getRecurrenceString(rule) {
        var index = rule.frequency;
        var recurrence = "";
        var str = "";
        //Check if reccurence is weekly or not
        if (index === RecurrenceRule.Weekly) {
            index = getWeekDaysIndex(rule.daysOfWeek.sort())
            // We are using a custom index
            // because we have more options than the Qt RecurrenceRule enum.
        } else if (index === RecurrenceRule.Monthly) {
            index = Recurrence.Monthly // If reccurence is Monthly
        } else if (index === RecurrenceRule.Yearly) {
            index = Recurrence.Yearly // If reccurence is Yearly
        }
        // if reccurrence is on different days.
        if (index === Recurrence.OnDiffDays) {
            // TRANSLATORS: the argument refers to several different days of the week.
            // E.g. "Weekly on Mondays, Tuesdays"
            recurrence += i18n.tr("Weekly on %1").arg(getDays(rule.daysOfWeek.sort()))
        } else {
            recurrence += Defines.recurrenceLabel[index]
        }
        str = getString(rule,recurrence);
        return str;
    }
}