~system-settings-touch/ubuntu-system-settings/trunk

« back to all changes in this revision

Viewing changes to plugins/sound/SoundsList.qml

  • Committer: CI Train Bot
  • Author(s): Ken VanDine
  • Date: 2015-11-24 11:09:42 UTC
  • mfrom: (1554.1.11 custom_ringtone)
  • Revision ID: ci-train-bot@canonical.com-20151124110942-n7rx863nipk7os4w
Allow setting custom ringtone with content-hub
 Fixes: #1268097
Approved by: Jonas G. Drange

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import QtQuick 2.4
3
3
import QtMultimedia 5.0
4
4
import SystemSettings 1.0
 
5
import Ubuntu.Content 1.3
5
6
import Ubuntu.Components 1.3
6
7
import Ubuntu.Components.ListItems 1.3 as ListItem
7
8
import Ubuntu.SystemSettings.Sound 1.0
12
13
ItemPage {
13
14
    property variant soundDisplayNames:
14
15
        Utilities.buildSoundValues(soundFileNames)
15
 
    property variant soundFileNames: backendInfo.listSounds(
16
 
        [soundsDir, "/custom" + soundsDir])
 
16
    property variant soundFileNames: refreshSoundFileNames()
17
17
    property bool showStopButton: false
18
18
    property int soundType // 0: ringtone, 1: message
19
19
    property string soundsDir
 
20
    property var activeTransfer
 
21
 
 
22
    onSoundFileNamesChanged: {
 
23
        soundDisplayNames = Utilities.buildSoundValues(soundFileNames)
 
24
        updateSelectedIndex()
 
25
    }
20
26
 
21
27
    id: soundsPage
 
28
    flickable: scrollWidget
 
29
 
 
30
    function refreshSoundFileNames() {
 
31
        if (soundType === 0)
 
32
            return backendInfo.listSounds([soundsDir, "/custom" + soundsDir, backendInfo.customRingtonePath])
 
33
        return backendInfo.listSounds([soundsDir, "/custom" + soundsDir])
 
34
    }
22
35
 
23
36
    UbuntuSoundPanel {
24
37
        id: backendInfo
55
68
        audioRole: MediaPlayer.alert
56
69
    }
57
70
 
58
 
    Column {
59
 
        id: columnId
60
 
        anchors.left: parent.left
61
 
        anchors.right: parent.right
62
 
 
63
 
        ListItem.SingleControl {
64
 
            id: listId
65
 
            control: Button {
66
 
                text: i18n.tr("Stop playing")
67
 
                width: parent.width - units.gu(4)
68
 
                onClicked:
69
 
                    soundEffect.stop()
70
 
            }
 
71
    function setRingtone(path) {
 
72
        if (soundType == 0) {
 
73
            soundSettings.incomingCallSound = path
 
74
            backendInfo.incomingCallSound = path
 
75
        } else if (soundType == 1) {
 
76
            soundSettings.incomingMessageSound = path
 
77
            backendInfo.incomingMessageSound = path
 
78
        }
 
79
        soundFileNames = refreshSoundFileNames()
 
80
        soundEffect.source = path
 
81
        soundEffect.play()
 
82
    }
 
83
 
 
84
    function updateSelectedIndex() {
 
85
        if (soundType == 0)
 
86
            soundSelector.selectedIndex =
 
87
                    Utilities.indexSelectedFile(soundFileNames,
 
88
                        backendInfo.incomingCallSound)
 
89
        else if (soundType == 1)
 
90
            soundSelector.selectedIndex =
 
91
                    Utilities.indexSelectedFile(soundFileNames,
 
92
                        backendInfo.incomingMessageSound)
 
93
    }
 
94
 
 
95
    Flickable {
 
96
        id: scrollWidget
 
97
        anchors.fill: parent
 
98
        contentWidth: parent.width
 
99
        contentHeight: selectorColumn.height + stopItem.height
 
100
        boundsBehavior: (contentHeight > height) ?
 
101
                            Flickable.DragAndOvershootBounds :
 
102
                            Flickable.StopAtBounds
 
103
        /* Set the direction to workaround https://bugreports.qt-project.org/browse/QTBUG-31905
 
104
           otherwise the UI might end up in a situation where scrolling doesn't work */
 
105
        flickableDirection: Flickable.VerticalFlick
 
106
 
 
107
        Column {
 
108
            id: selectorColumn
 
109
            anchors.left: parent.left
 
110
            anchors.right: parent.right
 
111
 
 
112
            ListItem.ItemSelector {
 
113
                id: soundSelector
 
114
                expanded: true
 
115
                model: soundDisplayNames
 
116
                selectedIndex: {
 
117
                    updateSelectedIndex()
 
118
                }
 
119
                onDelegateClicked: {
 
120
                    setRingtone(soundFileNames[index])
 
121
                }
 
122
            }
 
123
 
 
124
            ListItem.Standard {
 
125
                id: customRingtone
 
126
                text: i18n.tr("Custom Ringtone")
 
127
                visible: soundType === 0
 
128
                progression: true
 
129
                onClicked: {
 
130
                    pageStack.push(picker);
 
131
                }
 
132
            }
 
133
        }
 
134
    }
 
135
 
 
136
    ListItem.SingleControl {
 
137
        id: stopItem
 
138
        anchors.bottom: parent.bottom
 
139
        control: AbstractButton {
 
140
            id: stopButton
 
141
            anchors.verticalCenter: parent.verticalCenter
 
142
            anchors.horizontalCenter: parent.horizontalCenter
 
143
            focus: false
 
144
            width: height
 
145
            height: units.gu(4)
71
146
            enabled: soundEffect.playbackState == Audio.PlayingState
72
 
            visible: showStopButton
73
 
        }
74
 
    }
75
 
 
76
 
    ListItem.ItemSelector {
77
 
        id: soundSelector
78
 
        anchors.top: columnId.bottom
79
 
        anchors.bottom: soundsPage.bottom
80
 
        containerHeight: height
81
 
 
82
 
        expanded: true
83
 
        model: soundDisplayNames
84
 
        selectedIndex: {
85
 
            if (soundType == 0)
86
 
                soundSelector.selectedIndex =
87
 
                        Utilities.indexSelectedFile(soundFileNames,
88
 
                            backendInfo.incomingCallSound)
89
 
            else if (soundType == 1)
90
 
                soundSelector.selectedIndex =
91
 
                        Utilities.indexSelectedFile(soundFileNames,
92
 
                            backendInfo.incomingMessageSound)
93
 
        }
94
 
        onDelegateClicked: {
95
 
            if (soundType == 0) {
96
 
                soundSettings.incomingCallSound = soundFileNames[index]
97
 
                backendInfo.incomingCallSound = soundFileNames[index]
98
 
            } else if (soundType == 1) {
99
 
                soundSettings.incomingMessageSound = soundFileNames[index]
100
 
                backendInfo.incomingMessageSound = soundFileNames[index]
101
 
            }
102
 
            soundEffect.source = soundFileNames[index]
103
 
            soundEffect.play()
104
 
        }
105
 
    }
 
147
            visible: enabled
 
148
 
 
149
            onClicked: soundEffect.stop()
 
150
 
 
151
            Rectangle {
 
152
                anchors.fill: parent
 
153
                radius: width * 0.5
 
154
                border.color: UbuntuColors.warmGrey
 
155
                border.width: 1
 
156
            }
 
157
 
 
158
            Rectangle {
 
159
                width: parent.height * 0.4
 
160
                height: width
 
161
                smooth: true
 
162
                anchors {
 
163
                    verticalCenter: parent.verticalCenter
 
164
                    horizontalCenter: parent.horizontalCenter
 
165
                }
 
166
                color: UbuntuColors.warmGrey
 
167
            }
 
168
        }
 
169
        Rectangle {
 
170
            anchors.fill: parent
 
171
            z: parent.z - 1
 
172
            visible: stopButton.visible
 
173
            color: Theme.palette.normal.background
 
174
        }
 
175
    }
 
176
 
 
177
    Connections {
 
178
        id: contentHubConnection
 
179
        property var ringtoneCallback
 
180
        target: activeTransfer ? activeTransfer : null
 
181
        onStateChanged: {
 
182
            if (activeTransfer.state === ContentTransfer.Charged) {
 
183
                if (activeTransfer.items.length > 0) {
 
184
                    var toneUri = activeTransfer.items[0].url;
 
185
                    ringtoneCallback(toneUri);
 
186
                }
 
187
            }
 
188
        }
 
189
    }
 
190
 
 
191
    Page {
 
192
        id: picker
 
193
        visible: false
 
194
 
 
195
        ContentStore {
 
196
            id: appStore
 
197
            scope: ContentScope.App
 
198
        }
 
199
 
 
200
        ContentPeerPicker {
 
201
            id: peerPicker
 
202
            visible: parent.visible
 
203
            handler: ContentHandler.Source
 
204
            contentType: ContentType.Music
 
205
            showTitle: false
 
206
 
 
207
            onPeerSelected: {
 
208
                pageStack.pop();
 
209
                // requests an active transfer from peer
 
210
                function startContentTransfer(callback) {
 
211
                    if (callback)
 
212
                        contentHubConnection.ringtoneCallback = callback
 
213
                    var transfer = peer.request(appStore);
 
214
                    if (transfer !== null) {
 
215
                        soundsPage.activeTransfer = transfer;
 
216
                    }
 
217
                }
 
218
                peer.selectionType = ContentTransfer.Single;
 
219
                startContentTransfer(function(uri) {
 
220
                    setRingtone(uri.toString().replace("file:///", "/"));
 
221
                });
 
222
            }
 
223
 
 
224
            onCancelPressed: pageStack.pop();
 
225
        }
 
226
    }
 
227
 
 
228
    ContentTransferHint {
 
229
        anchors.fill: parent
 
230
        activeTransfer: soundsPage.activeTransfer
 
231
    }
 
232
 
106
233
}