~ubuntu-branches/ubuntu/saucy/phonon-backend-gstreamer/saucy-proposed

« back to all changes in this revision

Viewing changes to gstreamer/plugininstaller.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-04-15 14:43:30 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110415144330-7uif3319lxdu4ltt
Tags: 4:4.7.0really4.5.0-0ubuntu2
* New upstream release
* Add kubuntu_02_install_codec.diff to fix codec install

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the KDE project.
 
2
 
 
3
    Copyright (C) 2011 Trever Fischer <tdfischer@fedoraproject.org>
 
4
 
 
5
    This library is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU Lesser General Public License as published by
 
7
    the Free Software Foundation, either version 2.1 or 3 of the License.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU Lesser General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Lesser General Public License
 
15
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
#include "plugininstaller.h"
 
19
#include <gst/gst.h>
 
20
#include <QtCore/QCoreApplication>
 
21
#include <QtGui/QApplication>
 
22
#include <QtGui/QWidget>
 
23
#include <QtCore/QLibrary>
 
24
#include <QtCore/QPointer>
 
25
#include <QtCore/QMetaType>
 
26
#include <QtCore/QDebug>
 
27
 
 
28
#ifdef PLUGIN_INSTALL_API
 
29
#include <gst/pbutils/pbutils.h>
 
30
#endif
 
31
 
 
32
QT_BEGIN_NAMESPACE
 
33
 
 
34
namespace Phonon
 
35
{
 
36
namespace Gstreamer
 
37
{
 
38
bool PluginInstaller::s_ready = false;
 
39
 
 
40
bool PluginInstaller::init()
 
41
{
 
42
    if (!s_ready) {
 
43
#ifdef PLUGIN_INSTALL_API
 
44
        gst_pb_utils_init();
 
45
        s_ready = true;
 
46
#endif
 
47
    }
 
48
    return s_ready;
 
49
}
 
50
 
 
51
QString PluginInstaller::description(const GstCaps *caps, PluginType type)
 
52
{
 
53
#ifdef PLUGIN_INSTALL_API
 
54
    if (init()) {
 
55
        QString pluginStr;
 
56
        gchar *pluginDesc = NULL;
 
57
        switch(type) {
 
58
            case Decoder:
 
59
                pluginDesc = gst_pb_utils_get_decoder_description(caps);
 
60
                break;
 
61
            case Encoder:
 
62
                pluginDesc = gst_pb_utils_get_encoder_description(caps);
 
63
                break;
 
64
            case Codec:
 
65
                pluginDesc = gst_pb_utils_get_codec_description(caps);
 
66
                break;
 
67
            default:
 
68
                return 0;
 
69
        }
 
70
        pluginStr = QString::fromUtf8(pluginDesc);
 
71
        g_free (pluginDesc);
 
72
        return pluginStr;
 
73
    }
 
74
#else
 
75
    Q_UNUSED(type)
 
76
    Q_UNUSED(caps)
 
77
#endif
 
78
    return getCapType(caps);
 
79
}
 
80
 
 
81
QString PluginInstaller::description(const gchar *name, PluginType type)
 
82
{
 
83
#ifdef PLUGIN_INSTALL_API
 
84
    if (init()) {
 
85
        QString pluginStr;
 
86
        gchar *pluginDesc = NULL;
 
87
        switch(type) {
 
88
            case Source:
 
89
                pluginDesc = gst_pb_utils_get_source_description(name);
 
90
                break;
 
91
            case Sink:
 
92
                pluginDesc = gst_pb_utils_get_sink_description(name);
 
93
                break;
 
94
            case Element:
 
95
                pluginDesc = gst_pb_utils_get_element_description(name);
 
96
                break;
 
97
            default:
 
98
                return 0;
 
99
        }
 
100
        pluginStr = QString::fromUtf8(pluginDesc);
 
101
        g_free (pluginDesc);
 
102
        return pluginStr;
 
103
    }
 
104
#endif
 
105
    return name;
 
106
}
 
107
 
 
108
QString PluginInstaller::buildInstallationString(const GstCaps *caps, PluginType type)
 
109
{
 
110
    QString descType;
 
111
    switch(type) {
 
112
        case Codec:
 
113
        case Decoder:
 
114
            descType = "decoder";
 
115
            break;
 
116
        case Encoder:
 
117
            descType = "encoder";
 
118
            break;
 
119
        default:
 
120
            return 0;
 
121
    }
 
122
 
 
123
    return QString("gstreamer|0.10|%0|%1|%2-%3")
 
124
        .arg( qApp->applicationName() )
 
125
        .arg( description(caps, type) )
 
126
        .arg( descType )
 
127
        .arg( getCapType(caps) );
 
128
}
 
129
 
 
130
QString PluginInstaller::buildInstallationString(const gchar *name, PluginType type)
 
131
{
 
132
    QString descType;
 
133
    switch(type) {
 
134
        case Element:
 
135
            descType = "element";
 
136
            break;
 
137
        default:
 
138
            return 0;
 
139
    }
 
140
    return QString("gstreamer|0.10|%0|%1|%2-%3")
 
141
        .arg( qApp->applicationName() )
 
142
        .arg( description(name, type) )
 
143
        .arg( descType )
 
144
        .arg( name );
 
145
}
 
146
 
 
147
PluginInstaller::PluginInstaller(QObject *parent)
 
148
    : QObject(parent)
 
149
{
 
150
}
 
151
 
 
152
void PluginInstaller::addPlugin(const QString &name, PluginType type)
 
153
{
 
154
    m_pluginList.insert(name, type);
 
155
}
 
156
 
 
157
void PluginInstaller::addPlugin(const GstCaps *caps, PluginType type)
 
158
{
 
159
    m_capList.insert(gst_caps_copy(caps), type);
 
160
}
 
161
 
 
162
#ifdef PLUGIN_INSTALL_API
 
163
void PluginInstaller::run()
 
164
{
 
165
    GstInstallPluginsContext *ctx = gst_install_plugins_context_new();
 
166
    QWidget *activeWindow = QApplication::activeWindow();
 
167
    if (activeWindow) {
 
168
        gst_install_plugins_context_set_xid(ctx, static_cast<int>(activeWindow->winId()));
 
169
    }
 
170
    gchar *details[m_pluginList.size()+m_capList.size()+1];
 
171
    int i = 0;
 
172
    foreach(QString plugin, m_pluginList.keys()) {
 
173
        details[i] = strdup(buildInstallationString(plugin.toLocal8Bit().data(), m_pluginList[plugin]).toLocal8Bit().data());
 
174
        i++;
 
175
    }
 
176
    foreach(GstCaps *caps, m_capList.keys()) {
 
177
        details[i] = strdup(buildInstallationString(caps, m_capList[caps]).toLocal8Bit().data());
 
178
        i++;
 
179
    }
 
180
    details[i] = NULL;
 
181
 
 
182
    GstInstallPluginsReturn status;
 
183
    status = gst_install_plugins_async(details, ctx, pluginInstallationDone, new QPointer<PluginInstaller>(this));
 
184
    gst_install_plugins_context_free(ctx);
 
185
    if (status != GST_INSTALL_PLUGINS_STARTED_OK) {
 
186
        if (status == GST_INSTALL_PLUGINS_HELPER_MISSING)
 
187
            emit failure(tr("Missing codec helper script assistant."));
 
188
        else
 
189
            emit failure(tr("Plugin codec installation failed."));
 
190
    } else {
 
191
        emit started();
 
192
    }
 
193
    for(; i > 0; i--)
 
194
        free(details[i]);
 
195
    reset();
 
196
}
 
197
 
 
198
void PluginInstaller::pluginInstallationDone(GstInstallPluginsReturn result, gpointer data)
 
199
{
 
200
    QPointer<PluginInstaller> *that = static_cast<QPointer<PluginInstaller>*>(data);
 
201
    if (*that) {
 
202
        qRegisterMetaType<GstInstallPluginsReturn>("GstInstallPluginsReturn");
 
203
        (*that)->pluginInstallationResult(result);
 
204
    }
 
205
}
 
206
 
 
207
void PluginInstaller::pluginInstallationResult(GstInstallPluginsReturn result)
 
208
{
 
209
    switch(result) {
 
210
        case GST_INSTALL_PLUGINS_INVALID:
 
211
            emit failure(tr("Phonon attempted to install an invalid codec name."));
 
212
            break;
 
213
        case GST_INSTALL_PLUGINS_CRASHED:
 
214
            emit failure(tr("The codec installer crashed."));
 
215
            break;
 
216
        case GST_INSTALL_PLUGINS_NOT_FOUND:
 
217
            emit failure(tr("The required codec could not be found for installation."));
 
218
            break;
 
219
        case GST_INSTALL_PLUGINS_ERROR:
 
220
            emit failure(tr("An unspecified error occurred during codec installation."));
 
221
            break;
 
222
        case GST_INSTALL_PLUGINS_PARTIAL_SUCCESS:
 
223
            emit failure(tr("Not all codecs could be installed."));
 
224
            break;
 
225
        case GST_INSTALL_PLUGINS_USER_ABORT:
 
226
            emit failure(tr("User aborted codec installation"));
 
227
            break;
 
228
        //These four should never ever be passed in.
 
229
        //If they have, gstreamer has probably imploded in on itself.
 
230
        case GST_INSTALL_PLUGINS_STARTED_OK:
 
231
        case GST_INSTALL_PLUGINS_INTERNAL_FAILURE:
 
232
        case GST_INSTALL_PLUGINS_HELPER_MISSING:
 
233
        case GST_INSTALL_PLUGINS_INSTALL_IN_PROGRESS:
 
234
        //But this one is OK.
 
235
        case GST_INSTALL_PLUGINS_SUCCESS:
 
236
            if (!gst_update_registry()) {
 
237
                emit failure(tr("Could not update plugin registry after update."));
 
238
            } else {
 
239
                emit success();
 
240
            }
 
241
            break;
 
242
    }
 
243
}
 
244
#endif
 
245
 
 
246
PluginInstaller::InstallStatus PluginInstaller::checkInstalledPlugins()
 
247
{
 
248
    bool allFound = true;
 
249
    foreach(QString plugin, m_pluginList.keys()) {
 
250
        if (!gst_default_registry_check_feature_version(plugin.toLocal8Bit().data(), 0, 10, 0)) {
 
251
            allFound = false;
 
252
            break;
 
253
        }
 
254
    }
 
255
    if (!allFound || m_capList.size() > 0) {
 
256
#ifdef PLUGIN_INSTALL_API
 
257
        run();
 
258
        return Installing;
 
259
#else
 
260
        return Missing;
 
261
#endif
 
262
    } else {
 
263
        return Installed;
 
264
    }
 
265
}
 
266
 
 
267
QString PluginInstaller::getCapType(const GstCaps *caps)
 
268
{
 
269
    GstStructure *str = gst_caps_get_structure (caps, 0);
 
270
    return QString::fromUtf8(gst_structure_get_name (str));;
 
271
}
 
272
 
 
273
void PluginInstaller::reset()
 
274
{
 
275
    foreach(GstCaps *caps, m_capList.keys()) {
 
276
        gst_caps_unref(caps);
 
277
    }
 
278
    m_capList.clear();
 
279
    m_pluginList.clear();
 
280
}
 
281
 
 
282
} // ns Gstreamer
 
283
} // ns Phonon
 
284
 
 
285
QT_END_NAMESPACE
 
286
 
 
287
#include "moc_plugininstaller.cpp"