aboutsummaryrefslogtreecommitdiff
path: root/mappletrunner/mappletrunner.cpp
blob: 44140cdea9d72abe6146b8df7fe07dd03252557f (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
/****************************************************************************
**
** 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 "mappletrunner.h"
#include <mappletmetadata.h>
#include <mappletsettings.h>
#include <mappleticonchangedmessage.h>
#include <mapplettitlechangedmessage.h>
#include <mapplettextchangedmessage.h>
#include <mappletclient.h>
#include <mappletloader.h>
#include <mfiledatastore.h>
#include <mwidget.h>
#include <QMetaProperty>
#include <QGraphicsLinearLayout>
#include <QDebug>

MAppletRunner::MAppletRunner() : MExtensionRunner(),
    appletInstanceDataStore(NULL),
    appletSettings(NULL)
{
}

MAppletRunner::~MAppletRunner()
{
    delete appletSettings;
}

bool MAppletRunner::init(const QString &serverName, MAppletMetaData &metaData, const QString &appletId, const QString &appletInstanceFileDataPath)
{
    // Check whether the applet ID is valid
    if (appletId.isEmpty()) {
        qWarning() << "Applet ID is not valid.";
        return false;
    }

    // Check whether the metadata is valid
    if (!metaData.isValid()) {
        qWarning() << "Applet metadata is not valid.";
        return false;
    }

    // Try to construct a valid instance data store and bail out if not successful
    appletInstanceDataStore = new MFileDataStore(appletInstanceFileDataPath);
    if (!appletInstanceDataStore->isReadable() || !appletInstanceDataStore->isWritable()) {
        qWarning() << "Couldn't create applet instance data store";
        teardown();
        return false;
    }

    appletSettings = new MAppletSettings(metaData.fileName(), appletId);

    bool success = MExtensionRunner::init(serverName);

    if (success) {
        // Instantiate the applet and get the applet's widget
        widget = MAppletLoader::loadApplet(metaData, *appletInstanceDataStore, *appletSettings->dataAccess());
        success = false;

        if (widget != NULL) {
            // Put the widget into the parent widget's layout for layout request watching
            QGraphicsLinearLayout *layout = dynamic_cast<QGraphicsLinearLayout *>(parentWidget->layout());
            if (layout != NULL) {
                layout->addItem(widget);
                success = true;
            }
        }

        if (success) {
            setAppletProperties();
        }
    }

    return success;
}

void MAppletRunner::setAppletProperties()
{
    if (widget == NULL) {
        return;
    }

    const QMetaObject *mob = widget->metaObject();

    // use applet properties to fill header of the container
    int iconProperty = mob->indexOfProperty("appletIcon");
    int titleProperty = mob->indexOfProperty("appletTitle");
    int textProperty = mob->indexOfProperty("appletText");

    int iconSignal  = mob->indexOfSignal("appletIconChanged(QString)");
    int titleSignal = mob->indexOfSignal("appletTitleChanged(QString)");
    int textSignal  = mob->indexOfSignal("appletTextChanged(QString)");

    if (iconProperty != -1) {
        QString icon((mob->property(iconProperty).read(widget)).toString());
        if (!icon.isNull()) {
            appletIconChanged(icon);
        }
    }

    if (titleProperty != -1) {
        QString title((mob->property(titleProperty).read(widget)).toString());
        if (!title.isNull()) {
            appletTitleChanged(title);
        }
    }

    if (textProperty != -1) {
        QString text((mob->property(textProperty).read(widget)).toString());
        if (!text.isNull()) {
            appletTextChanged(text);
        }
    }

    if (iconSignal != -1) {
        connect(widget, SIGNAL(appletIconChanged(QString)), this, SLOT(appletIconChanged(QString)));
    }

    if (titleSignal != -1) {
        connect(widget, SIGNAL(appletTitleChanged(QString)), this, SLOT(appletTitleChanged(QString)));
    }

    if (textSignal != -1) {
        connect(widget, SIGNAL(appletTextChanged(QString)), this, SLOT(appletTextChanged(QString)));
    }

    // Connect the deprecated signals
    // TODO remove these after the deprecation period
    iconSignal  = mob->indexOfSignal("setAppletIcon(QString)");
    titleSignal = mob->indexOfSignal("setAppletTitle(QString)");
    textSignal  = mob->indexOfSignal("setAppletText(QString)");
    if (iconSignal != -1) {
        connect(widget, SIGNAL(setAppletIcon(QString)), this, SLOT(appletIconChanged(QString)));
    }
    if (titleSignal != -1) {
        connect(widget, SIGNAL(setAppletTitle(QString)), this, SLOT(appletTitleChanged(QString)));
    }
    if (textSignal != -1) {
        connect(widget, SIGNAL(setAppletText(QString)), this, SLOT(appletTextChanged(QString)));
    }

    connect(this, SIGNAL(visibilityChanged(bool)), widget, SIGNAL(visibilityChanged(bool)));
    connect(view, SIGNAL(orientationChanged(M::Orientation)), widget, SIGNAL(orientationChanged(M::Orientation)));
}

void MAppletRunner::teardown()
{
    MExtensionRunner::teardown();

    delete appletInstanceDataStore;
    appletInstanceDataStore = NULL;
}

void MAppletRunner::appletIconChanged(const QString &newIcon)
{
    MAppletIconChangedMessage m;
    m.setIcon(newIcon);
    communicator->sendMessage(m);
}

void MAppletRunner::appletTitleChanged(const QString &newTitle)
{
    MAppletTitleChangedMessage m;
    m.setTitle(newTitle);
    communicator->sendMessage(m);
}

void MAppletRunner::appletTextChanged(const QString &newText)
{
    MAppletTextChangedMessage m;
    m.setText(newText);
    communicator->sendMessage(m);
}