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

« back to all changes in this revision

Viewing changes to .pc/kubuntu_02_install_codec.diff/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 Decoder:
 
113
            descType = "decoder";
 
114
            break;
 
115
        case Encoder:
 
116
            descType = "encoder";
 
117
            break;
 
118
        case Codec:
 
119
            descType = "codec";
 
120
            break;
 
121
        default:
 
122
            return 0;
 
123
    }
 
124
 
 
125
    return QString("gstreamer|0.10|%0|%1|%2-%3")
 
126
        .arg( qApp->applicationName() )
 
127
        .arg( description(caps, type) )
 
128
        .arg( descType )
 
129
        .arg( getCapType(caps) );
 
130
}
 
131
 
 
132
QString PluginInstaller::buildInstallationString(const gchar *name, PluginType type)
 
133
{
 
134
    QString descType;
 
135
    switch(type) {
 
136
        case Element:
 
137
            descType = "element";
 
138
            break;
 
139
        default:
 
140
            return 0;
 
141
    }
 
142
    return QString("gstreamer|0.10|%0|%1|%2-%3")
 
143
        .arg( qApp->applicationName() )
 
144
        .arg( description(name, type) )
 
145
        .arg( descType )
 
146
        .arg( name );
 
147
}
 
148
 
 
149
PluginInstaller::PluginInstaller(QObject *parent)
 
150
    : QObject(parent)
 
151
{
 
152
}
 
153
 
 
154
void PluginInstaller::addPlugin(const QString &name, PluginType type)
 
155
{
 
156
    m_pluginList.insert(name, type);
 
157
}
 
158
 
 
159
void PluginInstaller::addPlugin(const GstCaps *caps, PluginType type)
 
160
{
 
161
    m_capList.insert(gst_caps_copy(caps), type);
 
162
}
 
163
 
 
164
#ifdef PLUGIN_INSTALL_API
 
165
void PluginInstaller::run()
 
166
{
 
167
    GstInstallPluginsContext *ctx = gst_install_plugins_context_new();
 
168
    QWidget *activeWindow = QApplication::activeWindow();
 
169
    if (activeWindow) {
 
170
        gst_install_plugins_context_set_xid(ctx, static_cast<int>(activeWindow->winId()));
 
171
    }
 
172
    gchar *details[m_pluginList.size()+m_capList.size()+1];
 
173
    int i = 0;
 
174
    foreach(QString plugin, m_pluginList.keys()) {
 
175
        details[i] = strdup(buildInstallationString(plugin.toLocal8Bit().data(), m_pluginList[plugin]).toLocal8Bit().data());
 
176
        i++;
 
177
    }
 
178
    foreach(GstCaps *caps, m_capList.keys()) {
 
179
        details[i] = strdup(buildInstallationString(caps, m_capList[caps]).toLocal8Bit().data());
 
180
        i++;
 
181
    }
 
182
    details[i] = NULL;
 
183
 
 
184
    GstInstallPluginsReturn status;
 
185
    status = gst_install_plugins_async(details, ctx, pluginInstallationDone, new QPointer<PluginInstaller>(this));
 
186
    gst_install_plugins_context_free(ctx);
 
187
    if (status != GST_INSTALL_PLUGINS_STARTED_OK) {
 
188
        if (status == GST_INSTALL_PLUGINS_HELPER_MISSING)
 
189
            emit failure(tr("Missing codec helper script assistant."));
 
190
        else
 
191
            emit failure(tr("Plugin codec installation failed."));
 
192
    } else {
 
193
        emit started();
 
194
    }
 
195
    for(; i > 0; i--)
 
196
        free(details[i]);
 
197
    reset();
 
198
}
 
199
 
 
200
void PluginInstaller::pluginInstallationDone(GstInstallPluginsReturn result, gpointer data)
 
201
{
 
202
    QPointer<PluginInstaller> *that = static_cast<QPointer<PluginInstaller>*>(data);
 
203
    if (*that) {
 
204
        qRegisterMetaType<GstInstallPluginsReturn>("GstInstallPluginsReturn");
 
205
        (*that)->pluginInstallationResult(result);
 
206
    }
 
207
}
 
208
 
 
209
void PluginInstaller::pluginInstallationResult(GstInstallPluginsReturn result)
 
210
{
 
211
    switch(result) {
 
212
        case GST_INSTALL_PLUGINS_INVALID:
 
213
            emit failure(tr("Phonon attempted to install an invalid codec name."));
 
214
            break;
 
215
        case GST_INSTALL_PLUGINS_CRASHED:
 
216
            emit failure(tr("The codec installer crashed."));
 
217
            break;
 
218
        case GST_INSTALL_PLUGINS_NOT_FOUND:
 
219
            emit failure(tr("The required codec could not be found for installation."));
 
220
            break;
 
221
        case GST_INSTALL_PLUGINS_ERROR:
 
222
            emit failure(tr("An unspecified error occurred during codec installation."));
 
223
            break;
 
224
        case GST_INSTALL_PLUGINS_PARTIAL_SUCCESS:
 
225
            emit failure(tr("Not all codecs could be installed."));
 
226
            break;
 
227
        case GST_INSTALL_PLUGINS_USER_ABORT:
 
228
            emit failure(tr("User aborted codec installation"));
 
229
            break;
 
230
        //These four should never ever be passed in.
 
231
        //If they have, gstreamer has probably imploded in on itself.
 
232
        case GST_INSTALL_PLUGINS_STARTED_OK:
 
233
        case GST_INSTALL_PLUGINS_INTERNAL_FAILURE:
 
234
        case GST_INSTALL_PLUGINS_HELPER_MISSING:
 
235
        case GST_INSTALL_PLUGINS_INSTALL_IN_PROGRESS:
 
236
        //But this one is OK.
 
237
        case GST_INSTALL_PLUGINS_SUCCESS:
 
238
            if (!gst_update_registry()) {
 
239
                emit failure(tr("Could not update plugin registry after update."));
 
240
            } else {
 
241
                emit success();
 
242
            }
 
243
            break;
 
244
    }
 
245
}
 
246
#endif
 
247
 
 
248
PluginInstaller::InstallStatus PluginInstaller::checkInstalledPlugins()
 
249
{
 
250
    bool allFound = true;
 
251
    foreach(QString plugin, m_pluginList.keys()) {
 
252
        if (!gst_default_registry_check_feature_version(plugin.toLocal8Bit().data(), 0, 10, 0)) {
 
253
            allFound = false;
 
254
            break;
 
255
        }
 
256
    }
 
257
    if (!allFound || m_capList.size() > 0) {
 
258
#ifdef PLUGIN_INSTALL_API
 
259
        run();
 
260
        return Installing;
 
261
#else
 
262
        return Missing;
 
263
#endif
 
264
    } else {
 
265
        return Installed;
 
266
    }
 
267
}
 
268
 
 
269
QString PluginInstaller::getCapType(const GstCaps *caps)
 
270
{
 
271
    GstStructure *str = gst_caps_get_structure (caps, 0);
 
272
    return QString::fromUtf8(gst_structure_get_name (str));;
 
273
}
 
274
 
 
275
void PluginInstaller::reset()
 
276
{
 
277
    foreach(GstCaps *caps, m_capList.keys()) {
 
278
        gst_caps_unref(caps);
 
279
    }
 
280
    m_capList.clear();
 
281
    m_pluginList.clear();
 
282
}
 
283
 
 
284
} // ns Gstreamer
 
285
} // ns Phonon
 
286
 
 
287
QT_END_NAMESPACE
 
288
 
 
289
#include "moc_plugininstaller.cpp"