aboutsummaryrefslogtreecommitdiff
path: root/src/extensions/applicationextension/mextensionhandle.cpp
blob: 2760b7b451d4e4b92aa582dafaac1d2d90d3fae1 (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mextensionhandle.h"
#include "mextensionhandle_p.h"
#include "mappletsharedmutex.h"
#include <mnamespace.h>
#include <mapplication.h>
#include <mwindow.h>
#include "mscenemanager.h"
#include <mlocale.h>
#include <mapplication.h>
#include <mappletmousemessage.h>
#include <mappletorientationmessage.h>
#include <mappletvisibilitymessage.h>
#include <mappletalivemessagerequest.h>
#include <mappletalivemessageresponse.h>
#include <mappletsetgeometrymessage.h>
#include <mappletpixmaptakenintousemessage.h>
#include <mappletpixmapmodifiedmessage.h>
#include <mappletobjectmenumessage.h>
#include <mappletobjectmenurequestmessage.h>
#include <mappletobjectmenuactionselectedmessage.h>
#include <msettingslanguageparser.h>
#include <mappletupdategeometrymessage.h>
#include <mappletsettings.h>
#include <mappletmetadata.h>
#include <mappletcancelmessage.h>

#include <QApplication>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <MDebug>
#include <QFile>
#include <QLocalSocket>
#include <QByteArray>
#include <QCryptographicHash>
#include <QFileInfo>
#include <QDir>
#include <QDBusConnection>
#include <MAction>
#include <mobjectmenu.h>

#ifdef HAVE_CONTEXTSUBSCRIBER
#include "contextproperty.h"
#endif

#include "mwidgetcreator.h"
M_REGISTER_WIDGET(MExtensionHandle)

//! D-Bus path of the Package Manager
const QString MExtensionHandlePrivate::PACKAGE_MANAGER_DBUS_PATH = "/com/nokia/package_manager";

//! D-Bus interface of the Package Manager
const QString MExtensionHandlePrivate::PACKAGE_MANAGER_DBUS_INTERFACE = "com.nokia.package_manager";

//! Interval for checking that the extension is alive
static const uint ALIVE_TIMER_INTERVAL = 5000;

MExtensionHandlePrivate::MExtensionHandlePrivate() :
    restartCount(0),
    aliveResponseTimeout(3000),
    runnerConnectionTimeout(20000),
    applicationVisible(true),
    remoteActions(),
#ifdef HAVE_CONTEXTSUBSCRIBER
    screenBlankProperty(new ContextProperty("Session.State", this)),
#endif
    q_ptr(NULL)
{
    communicationTimer.setSingleShot(true);

    // Connect to the system bus to receive the Package Manager signals
    QDBusConnection::systemBus().connect(QString(), PACKAGE_MANAGER_DBUS_PATH, PACKAGE_MANAGER_DBUS_INTERFACE, "OperationProgress", this, SLOT(operationProgress(QString, QString, int)));
    QDBusConnection::systemBus().connect(QString(), PACKAGE_MANAGER_DBUS_PATH, PACKAGE_MANAGER_DBUS_INTERFACE, "OperationComplete", this, SLOT(operationComplete(QString, QString, QString)));
}

MExtensionHandlePrivate::~MExtensionHandlePrivate()
{
}

void MExtensionHandlePrivate::visibilityChanged()
{
    Q_Q(MExtensionHandle);
    communicator.sendMessage(MAppletVisibilityMessage(q->isVisible() && applicationVisible));
}

void MExtensionHandlePrivate::operationComplete(const QString &operation, const QString &pkg, const QString &error)
{
    Q_Q(MExtensionHandle);

    if (operation == "Install" && q->model()->packageName() == pkg && !error.isEmpty()) {
        // If the installation of a package being installed fails go to the broken state
        q->model()->setInstallationError(error);
        q->model()->setCurrentState(MExtensionHandleModel::BROKEN);
    }
}

void MExtensionHandlePrivate::operationProgress(const QString &operation, const QString &pkg, int percentage)
{
    Q_Q(MExtensionHandle);

    // Applet Instance Manager handles the situation when installation compeletes
    if (operation == "Install" && q->model()->packageName() == pkg) {
        q->model()->setInstallationProgress(percentage);
    }
}

MExtensionHandle::MExtensionHandle(QGraphicsItem *parent) :
    MWidgetController(new MExtensionHandleModel, parent),
    d_ptr(new MExtensionHandlePrivate())
{
    initialize();
}

MExtensionHandle::MExtensionHandle(MExtensionHandlePrivate *dd, MExtensionHandleModel *model, QGraphicsItem *parent) :
    MWidgetController(model, parent),
    d_ptr(dd)
{
    initialize();
}

void MExtensionHandle::initialize()
{
    Q_D(MExtensionHandle);

    if (d)
        d->q_ptr = this;

    // TODO: FIXME - this needs to have the scene specified,
    // temporarily uses currently active MWindow's scene.
    connect(MApplication::activeWindow(),
            SIGNAL(orientationChanged(M::Orientation)),
            this, SLOT(orientationEvent(M::Orientation)));

    connect(this, SIGNAL(visibleChanged()), this, SLOT(visibilityChanged()));

    // Configure the timers
    connect(&d->aliveTimer, SIGNAL(timeout()), this, SLOT(sendAliveMessageRequest()));
    connect(&d->communicationTimer, SIGNAL(timeout()), this, SLOT(communicationTimerTimeout()));

#ifdef HAVE_CONTEXTSUBSCRIBER
    connect(d->screenBlankProperty, SIGNAL(valueChanged()), this, SLOT(updateDisplayState()));
#endif
}

MExtensionHandle::~MExtensionHandle()
{
    kill();
}

void MExtensionHandle::updateDisplayState()
{
    Q_D(MExtensionHandle);

#ifdef HAVE_CONTEXTSUBSCRIBER
    if(state() == MExtensionHandleModel::RUNNING) {
        bool blanked = d->screenBlankProperty->value().toString() == "blanked";

        if(blanked && d->aliveTimer.isActive()) {
            d->aliveTimer.stop();
            d->communicationTimer.stop();
        } else if(!d->aliveTimer.isActive()) {
            d->aliveTimer.start(ALIVE_TIMER_INTERVAL);
            sendAliveMessageRequest();
        }
    }
#endif
}

void MExtensionHandle::initPlaceHolder(const QString &packageName, const QString &installationError)
{
    model()->setPackageName(packageName);
    if (installationError.isEmpty()) {
        model()->setCurrentState(MExtensionHandleModel::INSTALLING);
    } else {
        model()->setCurrentState(MExtensionHandleModel::BROKEN);
        model()->setInstallationError(installationError);
    }
}

void MExtensionHandle::init(const QString &runnerBinary, const QString &metaDataFileName, const QStringList &extraArguments)
{
    Q_D(MExtensionHandle);

    // Construct a server name
    // Hash this object's pointer so we don't reveal the real address. This
    // is a security protection mechanism against any kind of
    // hacks revealing the address might make possible.
    QByteArray const digest = QCryptographicHash::hash(QByteArray::number((quintptr)this), QCryptographicHash::Md5);
    d->serverName = QString::number(QCoreApplication::applicationPid()) + '_' + QString::fromLatin1(digest.toHex());

    // Store the arguments and the binary
    d->arguments.clear();
    d->arguments << d->serverName << metaDataFileName << extraArguments;
    d->runnerBinary = runnerBinary;

    // Set the base name
    model()->setBaseName(QFileInfo(metaDataFileName).baseName());

    // Start the runner process
    run();
}

void MExtensionHandle::run()
{
    Q_D(MExtensionHandle);

    // Create a shared pixmap mutex after destroying any previous mutex
    delete model()->pixmapMutex();
    MAppletSharedMutex *mutex = new MAppletSharedMutex;
    if (!mutex->init(d->serverName)) {
        model()->setCurrentState(MExtensionHandleModel::BROKEN);
        delete mutex;
        mutex = NULL;
        return;
    }

    // Put it in the model so the view can use it
    model()->setPixmapMutex(mutex);

    // Set the state to starting
    model()->setCurrentState(MExtensionHandleModel::STARTING);

    // Listen for connections from the runner
    connect(&d->communicator, SIGNAL(connectionEstablished()), this, SLOT(connectionEstablished()));

    // Initialize socket server
    if (!d->communicator.startServer(d->serverName)) {
        model()->setCurrentState(MExtensionHandleModel::BROKEN);
        return;
    }

    // Connect a signal to recognize termination of the process
    connect(&d->process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
    // Connect a signal to recognize errors of the process
    connect(&d->process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));

    // Connect signals for receiving stdout and stderr data from the process
    connect(&d->process, SIGNAL(readyReadStandardOutput()), this, SLOT(processStdOutputReady()));
    connect(&d->process, SIGNAL(readyReadStandardError()), this, SLOT(processStdErrorReady()));

    d->process.start(d->runnerBinary, d->arguments);
    d->processStartTime.start();

#ifdef Q_OS_UNIX
    // Store the process pid as a property
    setProperty("pid", d->process.pid());
#endif

    // Create a timeout for establishing connection from the runner to the handle
    d->communicationTimer.start(d->runnerConnectionTimeout);
}

void MExtensionHandle::processError(QProcess::ProcessError error)
{
    if (error == QProcess::FailedToStart) {
        setBrokenState();
    }
}

void MExtensionHandle::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    Q_UNUSED(exitCode);
    Q_UNUSED(exitStatus);

    setBrokenState();
}

void MExtensionHandle::connectionEstablished()
{
    Q_D(MExtensionHandle);

    if (state() != MExtensionHandleModel::STARTING) {
        qWarning() << "MExtensionHandle::connectionEstablished() - Internal error, MExtensionHandle state differs from MExtensionHandleModel::STARTING";
        return;
    }

    // Stop runner connection timeout timer
    d->communicationTimer.stop();

    // Don't accept any more connections
    disconnect(&d->communicator, SIGNAL(connectionEstablished()), this, SLOT(connectionEstablished()));
    // Listen for received messages
    connect(&d->communicator, SIGNAL(messageReceived(MAppletMessage)), this, SLOT(messageReceived(MAppletMessage)));

    // Set the state to running
    model()->setCurrentState(MExtensionHandleModel::RUNNING);

    // Send alive requests at certain interval. Send the first alive request immediately.
    d->aliveTimer.start(ALIVE_TIMER_INTERVAL);
    sendAliveMessageRequest();

    // Set the current display state
    updateDisplayState();

    // Send the visibility changed message to the runner
    d->visibilityChanged();

    if (d->oldGeometry.isValid()) {
        // Restore the old geometry
        setGeometry(d->oldGeometry);
    }

    // Ask the runner for the size hints
    updateGeometry();

    update();
}

void MExtensionHandle::kill()
{
    Q_D(MExtensionHandle);

    // Stop communication with the runner
    stopCommunication();

    // Stop the runner process
    d->process.disconnect();
    d->process.terminate();
    d->process.waitForFinished();

    // Set the state to stopped
    model()->setCurrentState(MExtensionHandleModel::STOPPED);

    // Destroy the shared mutex
    delete model()->pixmapMutex();
    model()->setPixmapMutex(NULL);
}

void MExtensionHandle::reinit()
{
    Q_D(MExtensionHandle);

    // Get the geometry
    d->oldGeometry = geometry();

    // Stop the current instance
    kill();

    // Restart the runner
    run();
}

void MExtensionHandle::setAliveResponseTimeout(uint timeout)
{
    Q_D(MExtensionHandle);

    d->aliveResponseTimeout = timeout;
}

void MExtensionHandle::messageReceived(const MAppletMessage &message)
{
    Q_D(MExtensionHandle);

    switch (message.type()) {
    case MAppletMessage::PixmapTakenIntoUseMessage: {
        const MAppletPixmapTakenIntoUseMessage *m = dynamic_cast<const MAppletPixmapTakenIntoUseMessage *>(&message);

        if (m != NULL) {
            emit pixmapTakenIntoUse(m->handle());
        }
        break;
    }

    case MAppletMessage::AppletAliveMessageResponse: {
        const MAppletAliveMessageResponse *m = dynamic_cast<const MAppletAliveMessageResponse *>(&message);
        if (m != NULL) {
            d->communicationTimer.stop();
        }
        break;
    }

    case MAppletMessage::UpdateGeometryMessage: {
        const MAppletUpdateGeometryMessage *m = dynamic_cast<const MAppletUpdateGeometryMessage *>(&message);
        if (m != NULL) {
            // Put the size hints in the model and update the geometry
            model()->setSizeHints(m->sizeHints());
            updateGeometry();
        }
        break;
    }

    case MAppletMessage::PixmapModifiedMessage: {
        const MAppletPixmapModifiedMessage *m = dynamic_cast<const MAppletPixmapModifiedMessage *>(&message);
        if (m != NULL) {
            emit pixmapModified(m->geometry());
        }
        break;
    }

    case MAppletMessage::ObjectMenuMessage: {
        //Got the actions from the message. Now draw them.
        const MAppletObjectMenuMessage *m = dynamic_cast<const MAppletObjectMenuMessage *>(&message);
        if (m != NULL) {
            displayContextMenu(m->actionList());
        }
        break;
    }
    default:
        qWarning() << "IPC protocol error! Got message type:" << message.type();
        break;
    }
}

void MExtensionHandle::displayContextMenu(QList<QString> actions)
{
    setRemoteActions(actions);
    MObjectMenu *menu = new MObjectMenu(this);
    sceneManager()->appearSceneWindow(menu, MSceneWindow::DestroyWhenDone);
}

void MExtensionHandle::stopCommunication()
{
    Q_D(MExtensionHandle);

    disconnect(&d->communicator, SIGNAL(messageReceived(MAppletMessage)), this, SLOT(messageReceived(MAppletMessage)));
    disconnect(&d->communicator, SIGNAL(connectionEstablished()), this, SLOT(connectionEstablished()));
    d->aliveTimer.stop();
    d->communicationTimer.stop();

    d->communicator.closeConnection();
}

void MExtensionHandle::setBrokenState()
{
    Q_D(MExtensionHandle);

    model()->setCurrentState(MExtensionHandleModel::BROKEN);
    stopCommunication();
    setRemoteActions();

    if (d->processStartTime.elapsed() >= 30000) {
        d->restartCount = 0;
    }

    if (d->restartCount < 1) {
        d->restartCount++;
        reinit();
    }
}

void MExtensionHandle::communicationTimerTimeout()
{
    setBrokenState();
}

void MExtensionHandle::sendAliveMessageRequest()
{
    Q_D(MExtensionHandle);

    d->communicator.sendMessage(MAppletAliveMessageRequest());
    d->communicationTimer.start(d->aliveResponseTimeout);
}

void MExtensionHandle::visibilityChanged()
{
    Q_D(MExtensionHandle);
    d->visibilityChanged();
}

//TODO: Remove this redundant slot in next API/ABI break window.
void MExtensionHandle::visibilityEvent(bool visible)
{
    Q_UNUSED(visible);
}

void MExtensionHandle::enterDisplayEvent()
{
    Q_D(MExtensionHandle);

    d->applicationVisible = true;
    d->visibilityChanged();
}

void MExtensionHandle::exitDisplayEvent()
{
    Q_D(MExtensionHandle);

    d->applicationVisible = false;
    d->visibilityChanged();
}

void MExtensionHandle::orientationEvent(M::Orientation orientation)
{
    Q_D(MExtensionHandle);

    d->communicator.sendMessage(MAppletOrientationMessage(orientation));
}

void MExtensionHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(MExtensionHandle);

    // Scale the mouse events based on the current pixmap scaling factor
    d->communicator.sendMessage(MAppletMouseMessage(MAppletMessage::MousePressMessage, event->pos() / model()->scale(), event->button(), event->buttons()));
}

void MExtensionHandle::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    if (state() == MExtensionHandleModel::RUNNING) {
        Q_D(MExtensionHandle);
        QPointF scaledPos = event->pos() / model()->scale();
        d->communicator.sendMessage(MAppletObjectMenuRequestMessage(scaledPos));
    } else {
        MWidgetController::contextMenuEvent(event);
    }
}

void MExtensionHandle::cancelEvent(MCancelEvent *event)
{
    Q_UNUSED(event);
    Q_D(MExtensionHandle);

    d->communicator.sendMessage(MAppletCancelMessage());
}

void MExtensionHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (state() == MExtensionHandleModel::BROKEN) {
        MWidgetController::mouseReleaseEvent(event);
    } else {
        Q_D(MExtensionHandle);

        // Scale the mouse events based on the current pixmap scaling factor
        d->communicator.sendMessage(MAppletMouseMessage(MAppletMessage::MouseReleaseMessage, event->pos() / model()->scale(), event->button(), event->buttons()));
    }
}

void MExtensionHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(MExtensionHandle);

    // Scale the mouse events based on the current pixmap scaling factor
    d->communicator.sendMessage(MAppletMouseMessage(MAppletMessage::MouseMoveMessage, event->pos() / model()->scale(), event->button(), event->buttons()));
}

void MExtensionHandle::sendGeometryMessage(QRectF rect, Qt::HANDLE pixmapHandle)
{
#ifndef Q_WS_X11
    Q_UNUSED(rect);
    Q_UNUSED(pixmapHandle);
#else
    Q_D(MExtensionHandle);

    // Send the size the extension should know about and the X Pixmap handle to the extension
    d->communicator.sendMessage(MAppletSetGeometryMessage(rect, pixmapHandle));
#endif
}

void MExtensionHandle::processStdOutputReady()
{
    Q_D(MExtensionHandle);
    mDebug("MExtensionHandle") << "processStdOutputReady()\n   -------------->";
    mDebug("") << qPrintable(QString(d->process.readAllStandardOutput()));
    mDebug("") << " <--------------\n";
}

void MExtensionHandle::processStdErrorReady()
{
    Q_D(MExtensionHandle);
    mDebug("MExtensionHandle") << "processStdErrorReady()\n   -------------->";
    mDebug("") << qPrintable(QString(d->process.readAllStandardError()));
    mDebug("") << " <--------------\n";
}

void MExtensionHandle::setRemoteActions(QList<QString> items)
{
    Q_D(MExtensionHandle);

    // Remove previous remote actions
    foreach(MAction * action, d->remoteActions) {
        removeAction(action);
        delete action;
    }
    d->remoteActions.clear();

    foreach(const QString & item, items) {
        MAction *action = new MAction(item, this);
        d->remoteActions.append(action);
        connect(action, SIGNAL(triggered()), this, SLOT(remoteActionTriggered()));
        addAction(action);
    }
}

void MExtensionHandle::remoteActionTriggered()
{
    Q_D(MExtensionHandle);

    MAction *action = qobject_cast<MAction *>(sender());
    if (action != NULL) {
        int index = d->remoteActions.indexOf(action);
        if (index >= 0) {
            d->communicator.sendMessage(MAppletObjectMenuActionSelectedMessage(index));
        }
    }
}

MExtensionHandleModel::State MExtensionHandle::state() const
{
    return model()->currentState();
}

void MExtensionHandle::setSizeHints(const QVector<QSizeF> &sizeHints)
{
    model()->setSizeHints(sizeHints);
}

#include "moc_mextensionhandle.cpp"