main.qml 5.72 KB
Newer Older
Marco Martin's avatar
Marco Martin committed
1 2
/**
 *   Copyright 2014 Aaron Seigo <aseigo@kde.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *   Copyright 2014 Marco Martin <mart@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program 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 Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

Marco Martin's avatar
Marco Martin committed
21
import QtQuick 2.3
22 23
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
24
import QtQuick.LocalStorage 2.0
Marco Martin's avatar
Marco Martin committed
25
import org.kde.plasma.core 2.0 as PlasmaCore
Marco Martin's avatar
Marco Martin committed
26
import org.kde.plasma.extras 2.0 as PlasmaExtras
27 28 29

ApplicationWindow {
    id: root
Marco Martin's avatar
Marco Martin committed
30 31

//BEGIN PROPERTIES
32
    width: 1080
33
    height: 800
Marco Martin's avatar
Marco Martin committed
34

35 36
    //keep track if we were visible when ringing
    property bool wasVisible
37
    //support a single provider for now
38
    property string providerId: ofonoWrapper.providerId
39 40
    //was the last call an incoming one?
    property bool isIncoming
41

Marco Martin's avatar
Marco Martin committed
42
//END PROPERTIES
43

44
//BEGIN SIGNAL HANDLERS
45 46 47 48 49
    Connections {
        target: dialerUtils
        onMissedCallsActionTriggered: {
            root.visible = true;
        }
50 51 52
        onCallEnded: {
            insertCallInHistory(callContactNumber, callDuration, incomingCall ? 1 : 2);
        }
53 54 55 56
    }

    onVisibleChanged: {
        //reset missed calls if the status is not STATUS_INCOMING when got visible
57
        if (visible && ofonoWrapper.status != "incoming") {
58
            dialerUtils.resetMissedCalls();
59 60 61 62
        }
    }
//END SIGNAL HANDLERS

63 64
//BEGIN FUNCTIONS
    function call(number) {
65
        dialerUtils.dial(number);
66 67 68 69 70 71 72 73
    }

    function insertCallInHistory(number, duration, callType) {
        //DATABSE
        var db = LocalStorage.openDatabaseSync("PlasmaPhoneDialer", "1.0", "Call history of the Plasma Phone dialer", 1000000);

        db.transaction(
            function(tx) {
Marco Martin's avatar
Marco Martin committed
74
                var rs = tx.executeSql("INSERT INTO History VALUES(NULL, ?, datetime('now'), ?, ? )", [number, duration, callType]);
75 76 77 78

                var rs = tx.executeSql('SELECT * FROM History where id=?', [rs.insertId]);

                for(var i = 0; i < rs.rows.length; i++) {
Marco Martin's avatar
Marco Martin committed
79
                    var row = rs.rows.item(i);
Marco Martin's avatar
Marco Martin committed
80
                    row.date = Qt.formatDate(row.time, "yyyy-MM-dd");
Marco Martin's avatar
Marco Martin committed
81 82
                    row.originalIndex = historyModel.count;
                    historyModel.append(row);
83 84 85 86 87
                }
            }
        )
    }

Marco Martin's avatar
Marco Martin committed
88 89 90
    //index is historyModel row number, not db id and not sortmodel row number
    function removeCallFromHistory(index) {
        var item = historyModel.get(index);
Marco Martin's avatar
Marco Martin committed
91 92 93 94 95 96 97 98 99

        if (!item) {
            return;
        }

        var db = LocalStorage.openDatabaseSync("PlasmaPhoneDialer", "1.0", "Call history of the Plasma Phone dialer", 1000000);

        db.transaction(
            function(tx) {
Marco Martin's avatar
Marco Martin committed
100
                tx.executeSql("DELETE from History WHERE id=?", [item.id]);
Marco Martin's avatar
Marco Martin committed
101 102 103
            }
        )

Marco Martin's avatar
Marco Martin committed
104
        historyModel.remove(index);
Marco Martin's avatar
Marco Martin committed
105
    }
Marco Martin's avatar
Marco Martin committed
106 107 108 109 110 111 112 113 114 115 116 117 118

    function clearHistory() {
        var db = LocalStorage.openDatabaseSync("PlasmaPhoneDialer", "1.0", "Call history of the Plasma Phone dialer", 1000000);

        db.transaction(
            function(tx) {
                tx.executeSql("DELETE from History");
            }
        )

        historyModel.clear();
    }

119 120 121 122 123 124 125 126 127 128 129
//END FUNCTIONS

//BEGIN DATABASE
    Component.onCompleted: {
        //DATABSE
        var db = LocalStorage.openDatabaseSync("PlasmaPhoneDialer", "1.0", "Call history of the Plasma Phone dialer", 1000000);

        db.transaction(
            function(tx) {
                // Create the database if it doesn't already exist
                //callType: wether is incoming, outgoing, unanswered
Marco Martin's avatar
Marco Martin committed
130
                tx.executeSql('CREATE TABLE IF NOT EXISTS History(id INTEGER PRIMARY KEY AUTOINCREMENT, number TEXT, time DATETIME, duration INTEGER, callType INTEGER)');
131 132 133 134

                var rs = tx.executeSql('SELECT * FROM History');

                for(var i = 0; i < rs.rows.length; i++) {
Marco Martin's avatar
Marco Martin committed
135
                    var row = rs.rows.item(i);
Marco Martin's avatar
Marco Martin committed
136
                    row.date = Qt.formatDate(row.time, "yyyy-MM-dd");
Marco Martin's avatar
Marco Martin committed
137 138
                    row.originalIndex = historyModel.count;
                    historyModel.append(row);
139 140 141 142 143 144
                }
            }
        )
    }
//END DATABASE

Marco Martin's avatar
Marco Martin committed
145
//BEGIN MODELS
146 147 148 149
    ListModel {
        id: historyModel
    }

Marco Martin's avatar
Marco Martin committed
150
    OfonoWrapper {
151
        id: ofonoWrapper
152
    }
153

Marco Martin's avatar
Marco Martin committed
154
//END MODELS
155

Marco Martin's avatar
Marco Martin committed
156 157
//BEGIN UI
    PlasmaExtras.ConditionalLoader {
158
        anchors.fill: parent
159 160 161 162

        property bool shouldShow: root.visible && (dialerUtils.callState == "idle" || dialerUtils.callState == "failed")

        when: shouldShow
163
        source: Qt.resolvedUrl("Dialer/DialPage.qml")
164 165
        z: shouldShow ? 2 : 0
        opacity: shouldShow ? 1 : 0
Marco Martin's avatar
Marco Martin committed
166 167 168 169 170 171
        Behavior on opacity {
            OpacityAnimator {
                duration: units.shortDuration
                easing.type: Easing.InOutQuad
            }
        }
172
    }
173

Marco Martin's avatar
Marco Martin committed
174 175
    PlasmaExtras.ConditionalLoader {
        anchors.fill: parent
176 177 178 179

        property bool shouldShow: dialerUtils.callState != "idle" && dialerUtils.callState != "failed"

        when: shouldShow
Marco Martin's avatar
Marco Martin committed
180
        source: Qt.resolvedUrl("Call/CallPage.qml")
181 182
        opacity: shouldShow ? 1 : 0
        z:  shouldShow ? 2 : 0
Marco Martin's avatar
Marco Martin committed
183 184 185 186 187 188
        Behavior on opacity {
            OpacityAnimator {
                duration: units.shortDuration
                easing.type: Easing.InOutQuad
            }
        }
Marco Martin's avatar
Marco Martin committed
189 190 191
    }

//END UI
192
}