~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

« back to all changes in this revision

Viewing changes to kded/UpdateEvent/UpdateEvent.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2014-04-09 11:38:49 UTC
  • Revision ID: package-import@ubuntu.com-20140409113849-e82p5u55t7ux1jrv
Tags: 2.1.70-0ubuntu4
* Import more upstream commits (no. 14-23) improving l10n and notifications
  LP: #1295423
* Update muon-notifier.install for revised notifier (missed in previous
  uploads).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright © 2009 Jonathan Thomas <echidnaman@kubuntu.org>             *
3
 
 *                                                                         *
4
 
 *   This program is free software; you can redistribute it and/or         *
5
 
 *   modify it under the terms of the GNU General Public License as        *
6
 
 *   published by the Free Software Foundation; either version 2 of        *
7
 
 *   the License or (at your option) version 3 or any later version        *
8
 
 *   accepted by the membership of KDE e.V. (or its successor approved     *
9
 
 *   by the membership of KDE e.V.), which shall act as a proxy            *
10
 
 *   defined in Section 14 of version 3 of the license.                    *
11
 
 *                                                                         *
12
 
 *   This program is distributed in the hope that it will be useful,       *
13
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 
 *   GNU General Public License for more details.                          *
16
 
 *                                                                         *
17
 
 *   You should have received a copy of the GNU General Public License     *
18
 
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
19
 
 ***************************************************************************/
20
 
 
21
 
#include "UpdateEvent.h"
22
 
 
23
 
// Qt includes
24
 
#include <QtCore/QProcess>
25
 
#include <QtCore/QStringBuilder>
26
 
 
27
 
// KDE includes
28
 
#include <KDebug>
29
 
#include <KToolInvocation>
30
 
 
31
 
UpdateEvent::UpdateEvent(QObject* parent, const QString &name)
32
 
        : Event(parent, name)
33
 
        , m_checkerProcess(0)
34
 
        , m_checkingUpdates(false)
35
 
{
36
 
}
37
 
 
38
 
UpdateEvent::~UpdateEvent()
39
 
{
40
 
}
41
 
 
42
 
void UpdateEvent::show(int updates, int securityUpdates)
43
 
{
44
 
    if (!updates && !securityUpdates) {
45
 
        Event::ignore();
46
 
        return;
47
 
    }
48
 
 
49
 
    QString updatesText;
50
 
    QString securityText;
51
 
    QString text;
52
 
    QString icon;
53
 
    QString tTipIcon;
54
 
 
55
 
    if (securityUpdates) {
56
 
        if (m_verbose) {
57
 
            securityText = i18ncp("Notification text", "%1 security update is available",
58
 
                                                       "%1 security updates are available",
59
 
                                                       securityUpdates);
60
 
        } else {
61
 
            securityText = i18ncp("Notification text", "A security update is available",
62
 
                                                       "Security updates are available",
63
 
                                                        securityUpdates);
64
 
        }
65
 
    }
66
 
 
67
 
    if (updates) {
68
 
        if (m_verbose) {
69
 
            updatesText = i18ncp("Notification text", "%1 software update is available",
70
 
                                                      "%1 software updates are available",
71
 
                                                      updates);
72
 
        } else {
73
 
            updatesText = i18ncp("Notification text", "A software update is available",
74
 
                                                      "Software updates are available",
75
 
                                                      updates);
76
 
        }
77
 
    }
78
 
 
79
 
    if (securityUpdates && updates) {
80
 
        icon = "kpackagekit-security";
81
 
        tTipIcon = "security-medium";
82
 
        text = securityText % QLatin1Char('\n') % updatesText;
83
 
    } else if (securityUpdates && !updates) {
84
 
        icon = "kpackagekit-security";
85
 
        tTipIcon = "security-medium";
86
 
        text = securityText;
87
 
    } else {
88
 
        icon = "kpackagekit-updates";
89
 
        tTipIcon = "system-software-update";
90
 
        text = updatesText;
91
 
    }
92
 
 
93
 
    QStringList actions;
94
 
    actions << i18nc("Start the update", "Update");
95
 
    actions << i18nc("Button to dismiss this notification once", "Ignore for now");
96
 
    actions << i18nc("Button to make this notification never show up again",
97
 
                     "Never show again");
98
 
 
99
 
    if (!m_active) {
100
 
        Event::show(icon, text, actions, tTipIcon, "system-software-update");
101
 
    } else {
102
 
        Event::update(icon, text, tTipIcon);
103
 
    }
104
 
}
105
 
 
106
 
void UpdateEvent::run()
107
 
{
108
 
    KToolInvocation::kdeinitExec("/usr/bin/muon-updater");
109
 
 
110
 
    Event::run();
111
 
}
112
 
 
113
 
void UpdateEvent::reloadConfig()
114
 
{
115
 
    Event::reloadConfig();
116
 
 
117
 
    getUpdateInfo();
118
 
}
119
 
 
120
 
void UpdateEvent::getUpdateInfo()
121
 
{
122
 
    if (m_checkingUpdates) {
123
 
        return;
124
 
    }
125
 
 
126
 
    m_checkingUpdates = true;
127
 
    m_checkerProcess = new QProcess(this);
128
 
    connect(m_checkerProcess, SIGNAL(finished(int)), this, SLOT(parseUpdateInfo()));
129
 
    m_checkerProcess->start("/usr/lib/update-notifier/apt-check");
130
 
}
131
 
 
132
 
void UpdateEvent::parseUpdateInfo()
133
 
{
134
 
    // Weirdly enough, apt-check gives output on stderr
135
 
    QByteArray line = m_checkerProcess->readAllStandardError();
136
 
    m_checkerProcess->deleteLater();
137
 
    m_checkerProcess = 0;
138
 
    m_checkingUpdates = false;
139
 
 
140
 
    // Format updates;security
141
 
    int eqpos = line.indexOf(';');
142
 
 
143
 
    if (eqpos > 0) {
144
 
        QByteArray updatesString = line.left(eqpos);
145
 
        QByteArray securityString = line.right(line.size() - eqpos -1);
146
 
 
147
 
        int numSecurityUpdates = securityString.toInt();
148
 
        int numUpdates = updatesString.toInt() - numSecurityUpdates;
149
 
 
150
 
        show(numUpdates, numSecurityUpdates);
151
 
    }
152
 
 
153
 
    // ';' not found, apt-check broke :("
154
 
}