main.qml 6.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *   Copyright 2014 Aaron Seigo <aseigo@kde.org>
 *   Copyright 2015 Marco Martin <notmart@gmail.com>
 *
 *   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 Library 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.
 */

import QtQuick 2.1
import QtQuick.Layouts 1.1
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
25
import org.kde.plasma.plasmoid 2.0
26 27 28

Item {
    id: root
Marco Martin's avatar
Marco Martin committed
29
    property int notificationId: 0
30

31
    Layout.minimumHeight: notificationView.contentHeight
32

33 34
    Plasmoid.backgroundHints: PlasmaCore.Types.NoBackground

Marco Martin's avatar
Marco Martin committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    function addNotification(source, data, actions) {
        // Do not show duplicated notifications
        // Remove notifications that are sent again (odd, but true)
        for (var i = 0; i < notificationsModel.count; ++i) {
            var tmp = notificationsModel.get(i);
            var matches = (tmp.appName == data.appName &&
                           tmp.summary == data.summary &&
                           tmp.body == data.body);
            var sameSource = tmp.source == source;

            if (sameSource && matches) {
                return;
            }

            if (sameSource || matches) {
                notificationsModel.remove(i)
                break;
            }
        }

        data["id"] = ++notificationId;
        data["source"] = source;
        if (data["summary"].length < 1) {
            data["summary"] = data["body"];
            data["body"] = '';
        }
        data["actions"] = actions;

        notificationsModel.insert(0, data);
        if (!data["isPersistent"]) {
            pendingRemovals.push(notificationId);
            pendingTimer.start();
        }
    }

Marco Martin's avatar
Marco Martin committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    function executeAction(source, id) {
        //try to use the service
        if (source.indexOf("notification") !== -1) {
            var service = notificationsSource.serviceForSource(source)
            var op = service.operationDescription("invokeAction")
            op["actionId"] = id

            service.startOperationCall(op)
        //try to open the id as url
        } else if (source.indexOf("Job") !== -1) {
            Qt.openUrlExternally(id)
        }
    }

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    PlasmaCore.DataSource {
        id: notificationsSource

        engine: "notifications"
        interval: 0

        onSourceAdded: {
            connectSource(source);
        }

        onSourceRemoved: {
            for (var i = 0; i < notificationsModel.count; ++i) {
                if (notificationsModel.get(i) == source) {
                    notificationsModel.remove(i);
                    break;
                }
            }
        }

        onNewData: {
            var actions = new Array()
            if (data["actions"] && data["actions"].length % 2 == 0) {
                for (var i = 0; i < data["actions"].length; i += 2) {
                    var action = new Object();
                    action["id"] = data["actions"][i];
                    action["text"] = data["actions"][i+1];
                    actions.push(action);
                }
            }

Marco Martin's avatar
Marco Martin committed
114
            root.addNotification(
115 116 117 118 119 120 121
                    sourceName,
                    data,
                    actions);
        }

    }

Marco Martin's avatar
Marco Martin committed
122 123 124 125 126 127 128 129 130 131 132 133
    PlasmaCore.FrameSvgItem {
        anchors.centerIn: parent
        visible: notificationsModel.count == 0
        imagePath: "widgets/background"
        width: childrenRect.width + margins.left + margins.right
        height: childrenRect.height + margins.top + margins.bottom
        PlasmaComponents.Label {
            anchors.centerIn: parent
            text: i18n("No recent notifications")
        }
    }

134 135 136 137
    ListModel {
        id: notificationsModel

        ListElement {
Marco Martin's avatar
Marco Martin committed
138
            source: "call1Source"
139 140
            appIcon: "call-start"
            summary: "Missed call from Joe"
Marco Martin's avatar
Marco Martin committed
141
            appName: "Phone"
142
            body: "Called at 8:42 from +41 56 373 37 31"
Marco Martin's avatar
Marco Martin committed
143
            actions: []
144 145
        }
        ListElement {
Marco Martin's avatar
Marco Martin committed
146
            source: "im1Source"
147
            appIcon: "im-google"
Marco Martin's avatar
Marco Martin committed
148
            appName: "Message"
149
            summary: "July: Hey! Are you around?"
Marco Martin's avatar
Marco Martin committed
150
            actions: []
151 152
        }
        ListElement {
Marco Martin's avatar
Marco Martin committed
153
            source: "im2Source"
154
            appIcon: "im-google"
Marco Martin's avatar
Marco Martin committed
155
            appName: "Message"
156
            summary: "July: Hello?"
Marco Martin's avatar
Marco Martin committed
157
            actions: []
158 159 160 161 162 163 164 165
        }
    }

    ListView {
        id: notificationView
        spacing: units.smallSpacing
        anchors.fill: parent
        interactive: false
166
        cacheBuffer: 2000
167 168 169 170 171 172 173 174 175

        z: 1
        verticalLayoutDirection: ListView.BottomToTop
        model: notificationsModel

        add: Transition {
                NumberAnimation {
                    properties: "x"
                    from: notificationView.width
Marco Martin's avatar
Marco Martin committed
176 177
                    duration: units.shortDuration
                    easing.type: Easing.InOutQuad
178 179 180 181 182 183 184
                }
            }

        remove: Transition {
            NumberAnimation {
                properties: "x"
                to: notificationView.width
Marco Martin's avatar
Marco Martin committed
185 186
                duration: units.longDuration
                easing.type: Easing.InOutQuad
187 188 189 190
            }
            NumberAnimation {
                properties: "opacity"
                to: 0
Marco Martin's avatar
Marco Martin committed
191 192
                duration: units.longDuration
                easing.type: Easing.InOutQuad
193 194 195 196 197
            }
        }

        removeDisplaced: Transition {
            SequentialAnimation {
Marco Martin's avatar
Marco Martin committed
198 199 200 201 202 203 204 205
                PauseAnimation {
                    duration: units.longDuration
                }
                NumberAnimation {
                    properties: "x,y"
                    duration: units.shortDuration
                    easing.type: Easing.InOutQuad
                }
206 207 208 209 210 211
            }
        }

        delegate: NotificationStripe {}
    }
}