1/*
2 * SPDX-FileCopyrightText: 2018 Dimitris Kardarakos <dimkard@posteo.net>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7import QtQuick 2.7
8import QtQuick.Controls 2.0 as Controls2
9import QtQuick.Layouts 1.3
10import org.kde.kirigami 2.12 as Kirigami
11import org.kde.calindori 0.1 as Calindori
12
13Kirigami.Page {
14    id: root
15
16    enum Mode {
17        Create,
18        AddExisting,
19        Edit
20    }
21
22    property var loadedCalendar
23    property alias calendarName: nameInput.text
24    property alias ownerName: ownerNameInput.text
25    property alias ownerEmail: ownerEmail.text
26    property int mode: CalendarEditor.Mode.Create
27    property url calendarFile
28
29    signal calendarEditorSaved
30    signal calendarEditorCancelled
31
32    title: mode === CalendarEditor.Mode.Edit ? calendarName : i18n("New calendar")
33
34    function addLocalCalendarCfgEntry() {
35        var insertResult = _calindoriConfig.addInternalCalendar(root.calendarName, root.ownerName, root.ownerEmail);
36
37        if(!(insertResult.success)) {
38            validationFooter.text = insertResult.reason;
39            validationFooter.visible = true;
40            return;
41        }
42
43        validationFooter.visible = false;
44        calendarEditorSaved();
45    }
46
47    function addSharedCalendarCfgEntry() {
48        var addSharedResult = _calindoriConfig.addExternalCalendar(root.calendarName, root.ownerName, root.ownerEmail,  root.calendarFile);
49
50        if(!(addSharedResult.success)) {
51            validationFooter.text = addSharedResult.reason;
52            validationFooter.visible = true;
53            return;
54        }
55
56        _calindoriConfig.setOwnerInfo(root.calendarName, root.ownerName, root.ownerEmail);
57
58        validationFooter.visible = false;
59        calendarEditorSaved();
60    }
61
62    Kirigami.FormLayout {
63        id: calendarInputPage
64
65        anchors.centerIn: parent
66
67        Controls2.TextField {
68            id: nameInput
69
70            visible: root.mode !== CalendarEditor.Mode.Edit
71            Kirigami.FormData.label: i18n("Calendar:")
72        }
73
74        Controls2.Label {
75            id: fileName
76
77            property bool showFileName: (root.mode == CalendarEditor.Mode.AddExisting) && (root.calendarFile != "")
78
79            visible: showFileName
80            Kirigami.FormData.label: i18n("File:")
81            text: showFileName ? Calindori.CalendarController.fileNameFromUrl(root.calendarFile) : ""
82        }
83
84        Kirigami.Separator {
85            Kirigami.FormData.label: i18n("Owner")
86            Kirigami.FormData.isSection: true
87        }
88
89        Controls2.TextField {
90            id: ownerNameInput
91
92            Kirigami.FormData.label: i18n("Name:")
93        }
94
95        Controls2.TextField {
96            id: ownerEmail
97
98            Kirigami.FormData.label: i18n("Email:")
99        }
100
101    }
102
103    actions {
104
105        left: Kirigami.Action {
106            id: cancelAction
107
108            text: i18n("Cancel")
109            icon.name : "dialog-cancel"
110
111            onTriggered: {
112                calendarEditorCancelled();
113            }
114        }
115
116        main: Kirigami.Action {
117            id: saveAction
118
119            text: i18n("Save")
120            enabled: (mode == CalendarEditor.Mode.AddExisting) ? (root.calendarName != "" && root.calendarFile != "") : (root.calendarName != "")
121
122            icon.name : "dialog-ok"
123
124            onTriggered: {
125                if ((mode === CalendarEditor.Mode.AddExisting) || (mode === CalendarEditor.Mode.Create))  {
126                    var canAddResult = _calindoriConfig.canAddCalendar(root.calendarName);
127
128                    if(canAddResult && !(canAddResult.success)) {
129                        validationFooter.text = canAddResult.reason;
130                        validationFooter.visible = true;
131                        return;
132                    }
133                }
134
135                switch(mode) {
136                    case CalendarEditor.Mode.AddExisting:
137                        addSharedCalendarCfgEntry();
138                        break;
139                    case CalendarEditor.Mode.Create:
140                        addLocalCalendarCfgEntry();
141                        break;
142                    case CalendarEditor.Mode.Edit:
143                        _calindoriConfig.setOwnerInfo(root.calendarName, root.ownerName, root.ownerEmail);
144                        calendarEditorSaved();
145                        break;
146                    default:
147                        return;
148                }
149            }
150        }
151
152        right: Kirigami.Action {
153            id: addFile
154
155            visible: root.mode == CalendarEditor.Mode.AddExisting
156            text: i18n("Add")
157            icon.name: "list-add"
158
159            onTriggered: fileChooser.open()
160        }
161
162    }
163
164    footer: Kirigami.InlineMessage {
165        id: validationFooter
166
167        showCloseButton: true
168        type: Kirigami.MessageType.Warning
169        visible: false
170    }
171
172    FileChooser {
173        id: fileChooser
174
175        onAccepted: root.calendarFile = fileUrl
176    }
177
178}
179