File: progresslistmodel.cpp

package info (click to toggle)
plasma-workspace 4%3A5.8.6-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 54,996 kB
  • sloc: cpp: 70,006; sh: 868; xml: 867; python: 46; makefile: 16
file content (342 lines) | stat: -rw-r--r-- 11,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
  * This file is part of the KDE project
  * Copyright (C) 2009 Shaun Reich <shaun.reich@kdemail.net>
  * Copyright (C) 2006-2008 Rafael Fernández López <ereslibre@kde.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License version 2 as published by the Free Software Foundation.
  *
  * This library 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 library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
*/

#include "progresslistmodel.h"

#include <QDBusServiceWatcher>

#include "jobviewserveradaptor.h"
#include "kuiserveradaptor.h"
#include "jobviewserver_interface.h"
#include "requestviewcallwatcher.h"
#include "uiserver.h"
#include <QtDBus/qdbusabstractinterface.h>

ProgressListModel::ProgressListModel(QObject *parent)
        : QAbstractItemModel(parent), QDBusContext(), m_jobId(1),
          m_uiServer(0)
{
    m_serviceWatcher = new QDBusServiceWatcher(this);
    m_serviceWatcher->setConnection(QDBusConnection::sessionBus());
    m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
    connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &ProgressListModel::serviceUnregistered);

    // Register necessary services and D-Bus adaptors.
    new JobViewServerAdaptor(this);
    new KuiserverAdaptor(this);

    QDBusConnection sessionBus = QDBusConnection::sessionBus();

    if (!sessionBus.registerService(QLatin1String("org.kde.kuiserver"))) {
        qCDebug(KUISERVER) <<
        "********** Error, we have failed to register service org.kde.kuiserver. Perhaps something  has already taken it?";
    }

    if (!sessionBus.registerService(QLatin1String("org.kde.JobViewServer"))) {
        qCDebug(KUISERVER) <<
        "********** Error, we have failed to register service JobViewServer. Perhaps something already has taken it?";
    }

    if (!sessionBus.registerObject(QLatin1String("/JobViewServer"), this)) {
        qCDebug(KUISERVER) <<
        "********** Error, we have failed to register object /JobViewServer.";
    }

    /* unused
    if (m_registeredServices.isEmpty() && !m_uiServer) {
        m_uiServer = new UiServer(this);
    }
    */
}

ProgressListModel::~ProgressListModel()
{
    QDBusConnection sessionBus = QDBusConnection::sessionBus();
    sessionBus.unregisterService(QStringLiteral("org.kde.JobViewServer"));
    sessionBus.unregisterService(QStringLiteral("org.kde.kuiserver"));

    qDeleteAll(m_jobViews);
    qDeleteAll(m_registeredServices);

    delete m_uiServer;
}

QModelIndex ProgressListModel::parent(const QModelIndex&) const
{
    return QModelIndex();
}

QDBusObjectPath ProgressListModel::requestView(const QString &appName, const QString &appIconName, int capabilities)
{
    return newJob(appName, appIconName, capabilities);
}

Qt::ItemFlags ProgressListModel::flags(const QModelIndex &index) const
{
    Q_UNUSED(index);
    return Qt::ItemIsEnabled;
}

int ProgressListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

QVariant ProgressListModel::data(const QModelIndex &index, int role) const
{
    QVariant result;

    if (!index.isValid()) {
        return result;
    }

    JobView *jobView = m_jobViews.at(index.row());
    Q_ASSERT(jobView);

    switch (role) {
    case JobView::Capabilities:
        result = jobView->capabilities();
        break;
    case JobView::ApplicationName:
        result = jobView->appName();
        break;
    case JobView::Icon:
        result = jobView->appIconName();
        break;
    case JobView::SizeTotal:
        result = jobView->sizeTotal();
        break;
    case JobView::SizeProcessed:
        result = jobView->sizeProcessed();
        break;
    case JobView::TimeTotal:

        break;
    case JobView::TimeElapsed:

        break;
    case JobView::Speed:
        result = jobView->speed();
        break;
    case JobView::Percent:
        result = jobView->percent();
        break;
    case JobView::InfoMessage:
        result = jobView->infoMessage();
        break;
    case JobView::DescFields:

        break;
    case JobView::State:
        result = jobView->state();
        break;
    case JobView::JobViewRole:
        result = QVariant::fromValue<JobView*>(jobView);
        break;
    default:
        break;
    }

    return result;
}

QModelIndex ProgressListModel::index(int row, int column, const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    if (row >= m_jobViews.count() || column > 0) {
        return QModelIndex();
    } else {
        return createIndex(row, column);
    }
}

QModelIndex ProgressListModel::indexForJob(JobView *jobView) const
{
    int index = m_jobViews.indexOf(jobView);

    if (index != -1) {
        return createIndex(index, 0, jobView);
    } else {
        return QModelIndex();
    }
}

int ProgressListModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_jobViews.count();
}

QDBusObjectPath ProgressListModel::newJob(const QString &appName, const QString &appIcon, int capabilities)
{
    // Since s_jobId is an unsigned int, if we received an overflow and go back to 0,
    // be sure we do not assign 0 to a valid job, 0 is reserved only for
    // reporting problems.
    if (!m_jobId) ++m_jobId;
    JobView *newJob = new JobView(m_jobId);
    ++m_jobId;

    QString callerService = message().service();
    m_jobViewsOwners.insertMulti(callerService, newJob);
    m_serviceWatcher->addWatchedService(callerService);

    newJob->setAppName(appName);
    newJob->setAppIconName(appIcon);
    newJob->setCapabilities(capabilities);

    beginInsertRows(QModelIndex(), 0, 0);
    m_jobViews.prepend(newJob);
    endInsertRows();

    //The model will now get notified when a job changes -- so it can emit dataChanged(..)
    connect(newJob, &JobView::changed, this, &ProgressListModel::jobChanged);
    connect(newJob, &JobView::finished, this, &ProgressListModel::jobFinished);
    connect(newJob, &JobView::destUrlSet, this, &ProgressListModel::emitJobUrlsChanged);
    connect(this, SIGNAL(serviceDropped(const QString&)), newJob, SLOT(serviceDropped(const QString&)));


    //Forward this new job over to existing DBus clients.
    foreach(QDBusAbstractInterface* interface, m_registeredServices) {

        newJob->pendingCallStarted();
        QDBusPendingCall pendingCall = interface->asyncCall(QLatin1String("requestView"), appName, appIcon, capabilities);
        RequestViewCallWatcher *watcher = new RequestViewCallWatcher(newJob, interface->service(), pendingCall, this);

        connect(watcher, &RequestViewCallWatcher::callFinished, newJob, &JobView::pendingCallFinished);
    }

    return newJob->objectPath();
}

QStringList ProgressListModel::gatherJobUrls()
{
    QStringList jobUrls;

    foreach(JobView* jobView, m_jobViews) {
        jobUrls.append(jobView->destUrl().toString());
    }
    return jobUrls;
}

void ProgressListModel::jobFinished(JobView *jobView)
{
        // Job finished, delete it if we are not in self-ui mode, *and* the config option to keep finished jobs is set
        //TODO: does not check for case for the config
        if (!m_uiServer) {
            qCDebug(KUISERVER) << "removing jobview from list, it finished";
            m_jobViews.removeOne(jobView);
            //job dies, dest. URL's change..
            emit jobUrlsChanged(gatherJobUrls());
        }
}

void ProgressListModel::jobChanged(uint jobId)
{
    emit dataChanged(createIndex(jobId - 1, 0), createIndex(jobId + 1, 0));
    layoutChanged();
}

void ProgressListModel::emitJobUrlsChanged()
{
    emit jobUrlsChanged(gatherJobUrls());
}

void ProgressListModel::registerService(const QString &serviceName, const QString &objectPath)
{
    QDBusConnection sessionBus = QDBusConnection::sessionBus();

    if (!serviceName.isEmpty() && !objectPath.isEmpty()) {
        if (sessionBus.interface()->isServiceRegistered(serviceName).value() &&
                !m_registeredServices.contains(serviceName)) {

            org::kde::JobViewServer *client =
                new org::kde::JobViewServer(serviceName, objectPath, sessionBus);

            if (client->isValid()) {

                delete m_uiServer;
                m_uiServer = 0;

                m_serviceWatcher->addWatchedService(serviceName);
                m_registeredServices.insert(serviceName, client);


                //tell this new client to create all of the same jobs that we currently have.
                //also connect them so that when the method comes back, it will return a
                //QDBusObjectPath value, which is where we can contact that job, within "org.kde.JobViewV2"
                //TODO: KDE5 remember to replace current org.kde.JobView interface with the V2 one.. (it's named V2 for compat. reasons).

                //TODO: this falls victim to what newJob used to be vulnerable to...async calls returning too slowly and a terminate ensuing before that.
                // it may not be a problem (yet), though.
                foreach(JobView* jobView, m_jobViews) {

                    QDBusPendingCall pendingCall = client->asyncCall(QLatin1String("requestView"), jobView->appName(), jobView->appIconName(), jobView->capabilities());

                    RequestViewCallWatcher *watcher = new RequestViewCallWatcher(jobView, serviceName, pendingCall, this);
                    connect(watcher, &RequestViewCallWatcher::callFinished, jobView, &JobView::pendingCallFinished);
                }
            } else {
                delete client;
            }
        }
    }
}

bool ProgressListModel::requiresJobTracker()
{
    return m_registeredServices.isEmpty();
}

void ProgressListModel::serviceUnregistered(const QString &name)
{
    m_serviceWatcher->removeWatchedService(name);
    if (m_registeredServices.contains(name)) {

        emit serviceDropped(name);
        m_registeredServices.remove(name);

        /* unused (FIXME)
        if (m_registeredServices.isEmpty()) {
            //the last service dropped, we *need* to show our GUI
            m_uiServer = new UiServer(this);
        }
         */
    }

    QList<JobView*> jobs = m_jobViewsOwners.values(name);
    if (!jobs.isEmpty()) {
        m_jobViewsOwners.remove(name);
        Q_FOREACH(JobView *job, jobs) {
            job->terminate(job->errorText());
        }
    }
}

QStringList ProgressListModel::registeredJobContacts()
{
    QStringList output;
    foreach (JobView* jobView, m_jobViews) {
        output.append(jobView->jobContacts());
    }
    return output;
}