summaryrefslogtreecommitdiffstats
path: root/interfaces/src/appframework.cpp
blob: 7420642a3dd7dbd78f1384edbf5958da67bee829 (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
/*
 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "include/appframework.hpp"

#include <QtCore/QJsonObject>

class AppInfo::Private : public QSharedData
{
public:
    Private();
    Private(const Private &other);

    QString id;
    QString version;
    int width;
    int height;
    QString name;
    QString description;
    QString shortname;
    QString author;
    QString iconPath;
};

AppInfo::Private::Private()
    : width(-1)
    , height(-1)
{
}

AppInfo::Private::Private(const Private &other)
    : QSharedData(other)
    , id(other.id)
    , version(other.version)
    , width(other.width)
    , height(other.height)
    , name(other.name)
    , description(other.description)
    , shortname(other.shortname)
    , author(other.author)
    , iconPath(other.iconPath)
{
}

AppInfo::AppInfo()
    : d(new Private)
{
}

AppInfo::AppInfo(const AppInfo &other)
    : d(other.d)
{
}

AppInfo::~AppInfo()
{
}

AppInfo &AppInfo::operator =(const AppInfo &other)
{
    d = other.d;
    return *this;
}

QString AppInfo::id() const
{
    return d->id;
}

QString AppInfo::version() const
{
    return d->version;
}

int AppInfo::width() const
{
    return d->width;
}

int AppInfo::height() const
{
    return d->height;
}

QString AppInfo::name() const
{
    return d->name;
}

QString AppInfo::description() const
{
    return d->description;
}

QString AppInfo::shortname() const
{
    return d->shortname;
}

QString AppInfo::author() const
{
    return d->author;
}

QString AppInfo::iconPath() const
{
    return d->iconPath;
}

void AppInfo::read(const QJsonObject &json)
{
    d->id = json["id"].toString();
    d->version = json["version"].toString();
    d->width = json["width"].toInt();
    d->height = json["height"].toInt();
    d->name = json["name"].toString();
    d->description = json["description"].toString();
    d->shortname = json["shortname"].toString();
    d->author = json["author"].toString();
    d->iconPath = json["iconPath"].toString();
}

QDBusArgument &operator <<(QDBusArgument &argument, const AppInfo &appInfo)
{
    argument.beginStructure();
    argument << appInfo.d->id;
    argument << appInfo.d->version;
    argument << appInfo.d->width;
    argument << appInfo.d->height;
    argument << appInfo.d->name;
    argument << appInfo.d->description;
    argument << appInfo.d->shortname;
    argument << appInfo.d->author;
    argument << appInfo.d->iconPath;
    argument.endStructure();

    return argument;
}

const QDBusArgument &operator >>(const QDBusArgument &argument, AppInfo &appInfo)
{
    argument.beginStructure();
    argument >> appInfo.d->id;
    argument >> appInfo.d->version;
    argument >> appInfo.d->width;
    argument >> appInfo.d->height;
    argument >> appInfo.d->name;
    argument >> appInfo.d->description;
    argument >> appInfo.d->shortname;
    argument >> appInfo.d->author;
    argument >> appInfo.d->iconPath;
    argument.endStructure();
    return argument;
}
ved_event); } else { afb_req_fail(request, "failed", "Invalid event"); return; } } //Get scan types & append them to scan config scantype = get_scan_types(request); if(scantype < 0) return; ScanTypeAppend(scantype); afb_req_success(request, NULL, NULL); } /* * @brief Unsubscribe for an event * * @param struct afb_req : an afb request structure * */ static void unsubscribe(afb_req_t request) { json_object *jrequest = afb_req_json(request); char *value = NULL; if( json_object_object_get_ex(jrequest,"value",NULL) && json_object_object_get_ex(jrequest,"types",NULL) ) { //If 'types' is provided, We just remove the specified types. gint scantype = get_scan_types(request); if(scantype < 0) return; /* * If any scan type remained, we escape unsubscribe the event * otherwise continue to unsubscribe the event */ if(ScanTypeRemove(scantype)) { afb_req_success(request, NULL, NULL); return; } } value = afb_req_value(request, "value"); if(value) { if(!strcasecmp(value, "media_added")) { afb_req_unsubscribe(request, media_added_event); } else if(!strcasecmp(value, "media_removed")) { afb_req_unsubscribe(request, media_removed_event); } else { afb_req_fail(request, "failed", "Invalid event"); return; } } afb_req_success(request, NULL, NULL); } static json_object *new_json_object_from_device(GList *list) { json_object *jarray = json_object_new_array(); json_object *jresp = json_object_new_object(); json_object *jstring = NULL; GList *l; for (l = list; l; l = l->next) { struct Media_Item *item = l->data; json_object *jdict = json_object_new_object(); jstring = json_object_new_string(item->path); json_object_object_add(jdict, "path", jstring); jstring = json_object_new_string(item->type); json_object_object_add(jdict, "type", jstring); if (item->metadata.title) { jstring = json_object_new_string(item->metadata.title); json_object_object_add(jdict, "title", jstring); } if (item->metadata.artist) { jstring = json_object_new_string(item->metadata.artist); json_object_object_add(jdict, "artist", jstring); } if (item->metadata.album) { jstring = json_object_new_string(item->metadata.album); json_object_object_add(jdict, "album", jstring); } if (item->metadata.genre) { jstring = json_object_new_string(item->metadata.genre); json_object_object_add(jdict, "genre", jstring); } if (item->metadata.duration) { json_object *jint = json_object_new_int(item->metadata.duration); json_object_object_add(jdict, "duration", jint); } json_object_array_add(jarray, jdict); } if (jstring == NULL) return NULL; json_object_object_add(jresp, "Media", jarray); return jresp; } static void media_results_get (afb_req_t request) { GList *list = NULL; json_object *jresp = NULL; gint scan_type = 0; scan_type = get_scan_types(request); if(scan_type < 0) return; ListLock(); if(scan_type & LMS_AUDIO_SCAN) list = media_lightmediascanner_scan(list, NULL, LMS_AUDIO_SCAN); if(scan_type & LMS_VIDEO_SCAN) list = media_lightmediascanner_scan(list, NULL, LMS_VIDEO_SCAN); if(scan_type & LMS_IMAGE_SCAN) list = media_lightmediascanner_scan(list, NULL, LMS_IMAGE_SCAN); if (list == NULL) { afb_req_fail(request, "failed", "media scan error"); ListUnlock(); return; } jresp = new_json_object_from_device(list); g_list_free_full(list,free_media_item); ListUnlock(); if (jresp == NULL) { afb_req_fail(request, "failed", "media parsing error"); return; } afb_req_success(request, jresp, "Media Results Displayed"); } static void media_broadcast_device_added (GList *list) { json_object *jresp = new_json_object_from_device(list); if (jresp != NULL) { afb_event_push(media_added_event, jresp); } } static void media_broadcast_device_removed (const char *obj_path) { json_object *jresp = json_object_new_object(); json_object *jstring = json_object_new_string(obj_path); json_object_object_add(jresp, "Path", jstring); afb_event_push(media_removed_event, jresp); } static const afb_verb_t binding_verbs[] = { { .verb = "media_result", .callback = media_results_get, .info = "Media scan result" }, { .verb = "subscribe", .callback = subscribe, .info = "Subscribe for an event" }, { .verb = "unsubscribe", .callback = unsubscribe, .info = "Unsubscribe for an event" }, { } }; static int init(afb_api_t api) { Binding_RegisterCallback_t API_Callback; API_Callback.binding_device_added = media_broadcast_device_added; API_Callback.binding_device_removed = media_broadcast_device_removed; BindingAPIRegister(&API_Callback); media_added_event = afb_daemon_make_event("media_added"); media_removed_event = afb_daemon_make_event("media_removed"); return MediaPlayerManagerInit(); } const afb_binding_t afbBindingV3 = { .api = "mediascanner", .specification = "mediaplayer API", .init = init, .verbs = binding_verbs, };