Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactEditor/ContactDetailRingtoneEditor.qml

raw
1 /*
2 * Copyright (C) 2023 Ubports Foundation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import QtContacts 5.0
20 import Lomiri.Content 1.3
21 import Lomiri.Components.Popups 1.3
22
23 import Lomiri.Contacts 0.1
24 import Lomiri.AddressBook.Base 0.1
25
26 //style
27 import Lomiri.Components.Themes.Ambiance 0.1
28
29 ContactDetailBase {
30 id: root
31
32 detailType: ContactDetail.Ringtone
33
34 activeFocusOnTab: true
35 implicitHeight: contents.implicitHeight
36 visible: !empty || newRingtoneRequested
37
38 property bool newRingtoneRequested: false
39 property bool empty: true
40 property Loader overlayLoader: null
41
42 onDetailChanged: {
43 if (detail) {
44 populateValues(detail)
45 }
46 }
47
48 function open() {
49 populateValues(contact.ringtone)
50 newRingtoneRequested = true
51 }
52
53 function isEmpty() {
54 return empty
55 }
56
57 function populateValues(contactDetail) {
58 ringTones.clear()
59
60 if (contactDetail) {
61 const audioUrl = contactDetail.value(Ringtone.AudioRingtoneUrl)
62 const videoUrl = contactDetail.value(Ringtone.VideoRingtoneUrl)
63 const messageUrl = contactDetail.value(Ringtone.VibrationRingtoneUrl)
64
65 root.empty = !audioUrl // && !messageUrl && !videoUrl
66
67 ringTones.append({"type": Ringtone.AudioRingtoneUrl, "label": i18n.dtr("lomiri-addressbook-app", "Incoming call"), "value": audioUrl ? audioUrl.toString() : ""})
68 //disable Ringtone.VideoRingtoneUrl and Ringtone.VibrationRingtoneUrl for now
69 //ringTones.append({"type": Ringtone.VideoRingtoneUrl, "label": i18n.dtr("lomiri-addressbook-app", "Incoming video"), "value": audioUrl ? videoUrl.toString() : ""})
70 //ringTones.append({"type": Ringtone.VibrationRingtoneUrl, "label": i18n.dtr("lomiri-addressbook-app", "Incoming message"), "value": messageUrl ? messageUrl.toString() : ""})
71 }
72 }
73
74 function closeOverlay() {
75 overlayLoader.source = "";
76 overlayLoader.active = false;
77 }
78
79 function openSoundList(currentIndex, ringtoneType) {
80 overlayLoader.onLoaded.connect(function() {
81 var item = overlayLoader.item
82 item.currentRingtoneType = ringtoneType
83 item.currentSoundFile = ringTones.get(currentIndex).value
84 item.closeRequested.connect(closeOverlay)
85 item.soundSelected.connect(function(newSound) {
86 ringTones.setProperty(currentIndex, "value", newSound)
87 })
88
89 })
90
91 overlayLoader.source = Qt.resolvedUrl("ContactDetailRingtoneSoundSelector.qml")
92 overlayLoader.active = true
93 }
94
95 function save() {
96 var detailchanged = false
97 if (root.contact && root.contact.ringtone) {
98 var detail = root.contact.ringtone
99
100 for( var i = 0; i < ringTones.count; i++ ) {
101 var ringTone = ringTones.get(i);
102 console.log('save detail:', ringTone.value, detail.value(ringTone.type))
103 if (ringTone.value !== detail.value(ringTone.type)) {
104 detail.setValue(ringTone.type, ringTone.value)
105 detailchanged = true
106 }
107 }
108 }
109 return detailchanged
110 }
111
112 function soundName(soundPath) {
113
114 if (soundPath.length > 0) {
115 /* The display name starts with an uppercase char, and replace special chars with spaces */
116 var title = soundPath.split('/').pop().split('.').slice(0,-1).join(" ").replace(/[._-]/g, " ")
117 if (title.length > 0) {
118 return title[0].toUpperCase() + title.slice(1)
119 }
120 }
121
122 return i18n.tr("Default sound")
123 }
124
125 ListModel {
126 id: ringTones
127 }
128
129 Column {
130 id: contents
131 spacing: units.gu(1)
132 width: parent.width
133
134 ContactDetailTitle {
135 id: header
136 title: i18n.dtr("lomiri-addressbook-app", "Custom ringtone")
137 }
138
139 Repeater {
140 model: ringTones
141
142 Column {
143
144 anchors {
145 margins: units.gu(2)
146 left: parent.left
147 right: parent.right
148 }
149
150 Item {
151 id: ringToneFieldItem
152
153 anchors {
154 left: parent.left
155 right: parent.right
156 }
157 height: childrenRect.height
158
159 TextField {
160 id: input
161 width: parent.width
162 text: soundName(value)
163
164 placeholderText: i18n.dtr("lomiri-addressbook-app", "Select a custom sound")
165 style: TextFieldStyle {
166 overlaySpacing: 0
167 frameSpacing: 0
168 background: Item {}
169 }
170
171 ProgressionSlot {
172 visible: !clearButton.visible
173 anchors.right: parent.right
174 anchors.verticalCenter: parent.verticalCenter
175 }
176
177 }
178
179 Label {
180 id: typeLabel
181 anchors.top: input.bottom
182 //verticalAlignment: Text.AlignVCenter
183 elide: Text.ElideRight
184 text: label
185 // style
186 fontSize: "small"
187 opacity: 0.8
188 }
189
190 Item {
191 anchors.top: typeLabel.bottom
192 height: units.gu(2)
193 }
194
195 MouseArea {
196 anchors.fill: parent
197 onClicked: openSoundList(index, type)
198 }
199
200 AbstractButton {
201 id: clearButton
202 objectName: "clear_button"
203 activeFocusOnPress: false
204 activeFocusOnTab: false
205 visible: value.length > 0
206 anchors {
207 top: input.top
208 right: input.right
209 margins: units.gu(0.5)
210 verticalCenter: input.verticalCenter
211
212 }
213 z: 100
214 width: visible ? icon.width : 0
215
216 Icon {
217 id: icon
218 anchors.verticalCenter: parent.verticalCenter
219 width: units.gu(2.5)
220 height: width
221 // use icon from icon-theme
222 name: "edit-clear"
223 }
224
225 onClicked: {
226 ringTones.setProperty(index, "value", "")
227 }
228 }
229 }
230 }
231 }
232 }
233
234 Connections {
235 target: addNewFieldButton
236 function onFieldSelected(detailType, fieldName, qmlTypeName) {
237 if (detailType === root.detailType) {
238 root.open()
239 }
240 }
241 }
242 }