~ci-train-bot/ubuntu-system-settings/ubuntu-system-settings-ubuntu-yakkety-landing-046

« back to all changes in this revision

Viewing changes to plugins/cellular/Components/DataMultiSim.qml

  • Committer: Bileto Bot
  • Author(s): Antti Kaijanmäki
  • Date: 2016-06-24 13:42:03 UTC
  • mfrom: (1647.4.17 mobile-data-switch)
  • Revision ID: ci-train-bot@canonical.com-20160624134203-ih37s9evoh7ndnyf
use Connectivity API for cellular data and roaming control. (LP: #1373463)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import SystemSettings 1.0
22
22
import Ubuntu.Components 1.3
23
23
import Ubuntu.Components.ListItems 1.3 as ListItem
 
24
import Ubuntu.Connectivity 1.0
24
25
 
25
26
Column {
26
27
 
27
 
    property string prevOnlineModem: parent.prevOnlineModem
 
28
    SortFilterModel {
 
29
        id: sortedModems
 
30
        model: Connectivity.modems
 
31
        sort.property: "Index"
 
32
        sort.order: Qt.AscendingOrder
 
33
    }
28
34
 
29
 
    function getNameFromIndex (index) {
30
 
        var dummy = i18n.tr("Cellular data")
31
 
        if (index === 0) {
32
 
            return i18n.tr("Off");
33
 
        } else if (index > 0) {
34
 
            return sims[index - 1].title;
 
35
    ListItem.Standard {
 
36
        text: i18n.tr("Cellular data")
 
37
        control: Switch {
 
38
            id: dataSwitch
 
39
            objectName: "data"
 
40
            checked: Connectivity.mobileDataEnabled
 
41
            enabled: simSelector.currentSim !== null
 
42
            onTriggered: {
 
43
                Connectivity.mobileDataEnabled = checked
 
44
                /*
 
45
                 * We do this binding here to workaround bug:
 
46
                 * https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1494387
 
47
                 *
 
48
                 * The bug causes the checked binding to be overridden if plain onTriggered is used.
 
49
                 */
 
50
                checked = Qt.binding(function() {
 
51
                    return Connectivity.mobileDataEnabled
 
52
                })
 
53
            }
35
54
        }
36
55
    }
37
56
 
38
 
    height: childrenRect.height
39
 
 
40
 
    SettingsItemTitle { text: i18n.tr("Cellular data:") }
41
 
 
42
57
    ListItem.ItemSelector {
43
 
        id: use
44
 
        objectName: "data"
 
58
        id: simSelector
 
59
        showDivider: false
45
60
        expanded: true
46
 
        model: {
47
 
            // create a model of 'off' and all sim paths
48
 
            var m = ['off'];
49
 
            sims.forEach(function (sim) {
50
 
                m.push(sim.path);
51
 
            });
52
 
            return m;
53
 
        }
 
61
        model: sortedModems
 
62
        selectedIndex: -1
 
63
 
54
64
        delegate: OptionSelectorDelegate {
55
 
            objectName: "use" + modelData
56
 
            text: getNameFromIndex(index)
 
65
            objectName: "use/ril_" + (model.Index - 1)
 
66
            text: {
 
67
                if (model.Sim) {
 
68
                    return  sims[model.Index - 1].name + " (" +
 
69
                            (model.Sim.PrimaryPhoneNumber !== "" ?
 
70
                                model.Sim.PrimaryPhoneNumber : model.Sim.Imsi) + ")"
 
71
                }
 
72
                else {
 
73
                    return i18n.tr("No SIM detected")
 
74
                }
 
75
            }
 
76
            subText: {
 
77
                if (model.Sim) {
 
78
                    return ""
 
79
                }
 
80
                else {
 
81
                    return i18n.tr("Insert a SIM, then restart the device.")
 
82
                }
 
83
            }
 
84
            enabled: model.Sim !== null
57
85
        }
58
 
        selectedIndex: {
59
 
            if (prevOnlineModem) {
60
 
                return model.indexOf(prevOnlineModem);
 
86
 
 
87
        property var currentSim : null
 
88
        onSelectedIndexChanged: {
 
89
            if (selectedIndex === -1) {
 
90
                currentSim = null
61
91
            } else {
62
 
                return [true, sims[0].connMan.powered, sims[1].connMan.powered]
63
 
                    .lastIndexOf(true);
64
 
            }
65
 
        }
66
 
 
 
92
                currentSim = model.get(selectedIndex).Sim
 
93
            }
 
94
        }
 
95
        onCurrentSimChanged: {
 
96
            if (currentSim !== null) {
 
97
                roaming.checked = Qt.binding(function() {
 
98
                    return currentSim.DataRoamingEnabled
 
99
                })
 
100
            }
 
101
        }
 
102
 
 
103
        function setSelectedIndex() {
 
104
            if (Connectivity.simForMobileData === null) {
 
105
                simSelector.selectedIndex = -1
 
106
                return
 
107
            }
 
108
 
 
109
            for (var i = 0; i < sortedModems.count; i++) {
 
110
                if (sortedModems.get(i).Sim === Connectivity.simForMobileData) {
 
111
                    simSelector.selectedIndex = i
 
112
                    return
 
113
                }
 
114
            }
 
115
            simSelector.selectedIndex = -1
 
116
        }
 
117
        Connections {
 
118
            target: Connectivity
 
119
            onSimForMobileDataUpdated: {
 
120
                simSelector.setSelectedIndex()
 
121
            }
 
122
        }
 
123
        Component.onCompleted: {
 
124
            setSelectedIndex()
 
125
        }
 
126
 
 
127
        onTriggered:  {
 
128
            // @bug: https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1577351
 
129
            // Connectivity.simForMobileData = currentSim
 
130
            /*
 
131
             * There is a bug in onTriggered that causes it to be fired *before* selectedIndex has been updated
 
132
             * and thus there is no way of knowing what index was actually selected by the user.
 
133
             *
 
134
             * This is worked around below by using onDelegateClicked which gives us the missing index.
 
135
             */
 
136
        }
67
137
        onDelegateClicked: {
68
 
            // power all sims on or off
69
 
            sims.forEach(function (sim) {
70
 
                sim.connMan.powered = (model[index] === sim.path);
71
 
            });
 
138
            var sim = sortedModems.get(index).Sim
 
139
            if (sim === null) {
 
140
                return
 
141
            }
 
142
            Connectivity.simForMobileData = sim
72
143
        }
73
144
    }
74
145
 
75
146
    ListItem.Standard {
76
 
        id: dataRoamingItem
77
147
        text: i18n.tr("Data roaming")
78
 
        enabled: use.selectedIndex !== 0
79
148
        control: Switch {
80
 
            id: dataRoamingControl
 
149
            id: roaming
81
150
            objectName: "roaming"
82
 
 
83
 
            property bool serverChecked: poweredSim && poweredSim.connMan.roamingAllowed
84
 
            onServerCheckedChanged: checked = serverChecked
85
 
            Component.onCompleted: checked = serverChecked
86
 
            onTriggered: {
87
 
                if (poweredSim) {
88
 
                    poweredSim.connMan.roamingAllowed = checked;
89
 
                }
 
151
            enabled: simSelector.currentSim !== null && dataSwitch.checked
 
152
            checked: simSelector.currentSim ? simSelector.currentSim.DataRoamingEnabled : false
 
153
            function trigger() {
 
154
                simSelector.currentSim.DataRoamingEnabled = !checked
90
155
            }
91
156
        }
92
 
        showDivider: false
93
157
    }
94
158
}