Small clean of subtitles applet code.
[mafwsubrenderer] / qmafw-gst-subtitles-renderer / src / MafwGstRendererPlaylistFileUtility.cpp
1 #include "MafwGstRendererPlaylistFileUtility.h"
2 #include <glib-object.h>
3 #include <QUrl>
4 #include <QDebug>
5 #include <MafwError.h>
6
7 /********************************************************************
8  * MafwGstRendererPlaylistFileUtility::MafwGstRendererPlaylistFileUtility
9  ********************************************************************/
10 MafwGstRendererPlaylistFileUtility::MafwGstRendererPlaylistFileUtility(QObject* parent):
11         QObject(parent), m_parserId(0), m_firstItem(false)
12 {    
13     g_type_init();
14 }
15
16 /********************************************************************
17  * MafwGstRendererPlaylistFileUtility::MafwGstRendererPlaylistFileUtility
18  ********************************************************************/
19 MafwGstRendererPlaylistFileUtility::~MafwGstRendererPlaylistFileUtility()
20 {
21     qDebug() << __PRETTY_FUNCTION__;
22 }
23
24 /********************************************************************
25  * MafwGstRendererPlaylistFileUtility::getUriList
26  ********************************************************************/
27 QStringList MafwGstRendererPlaylistFileUtility::getUriList()
28 {
29     return m_uriList;
30 }
31
32 /********************************************************************
33  * MafwGstRendererPlaylistFileUtility::playPlaylistFile
34  ********************************************************************/
35 void MafwGstRendererPlaylistFileUtility::parsePlaylistFile(const QUrl& url)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     if (url.isValid() && !url.scheme().isEmpty())
40     {
41         m_uriList.clear();
42         m_parserId = totem_pl_parser_new ();
43         g_object_set(m_parserId, "recurse", false, "disable-unsafe",
44                      true, NULL);
45         g_signal_connect(G_OBJECT(m_parserId), "entry-parsed", G_CALLBACK(uriParsed), this);
46         totem_pl_parser_parse_async(m_parserId,
47                                     url.toString().toAscii().constData(),
48                                     false,
49                                     0,
50                                     GAsyncReadyCallback(readyCb),
51                                     (void *)this);
52         g_object_unref(m_parserId);
53         m_firstItem = true;
54     }
55     else
56     {
57         Q_EMIT parsingReady(false);
58     }
59 }
60
61 /********************************************************************
62  * MafwGstRendererPlaylistFileUtility::takeFirstUri
63  ********************************************************************/
64 QString MafwGstRendererPlaylistFileUtility::takeFirstUri()
65 {
66     if (m_uriList.isEmpty())
67     {
68         qDebug() << __PRETTY_FUNCTION__ << ": there are no more items parsed";
69         return QString();
70     }
71     else
72     {
73         return m_uriList.takeFirst();
74     }
75 }
76
77 /********************************************************************
78  * MafwGstRendererPlaylistFileUtility::setPendingError
79  ********************************************************************/
80 void MafwGstRendererPlaylistFileUtility::setPendingError(MafwError& error)
81 {
82     m_pendingError = error;
83 }
84
85 /********************************************************************
86  * MafwGstRendererPlaylistFileUtility::takePendingError
87  ********************************************************************/
88 MafwError MafwGstRendererPlaylistFileUtility::takePendingError()
89 {
90     MafwError retMe = m_pendingError;
91     m_pendingError = MafwError();
92     return retMe;
93 }
94
95
96 /********************************************************************
97  * MafwGstRendererPlaylistFileUtility::uriParsed
98  ********************************************************************/
99 void MafwGstRendererPlaylistFileUtility::uriParsed(TotemPlParser* parser,
100                       gchar* uri,
101                       gpointer /*metadata*/,
102                       MafwGstRendererPlaylistFileUtility* self)
103 {
104     qDebug() << __PRETTY_FUNCTION__ << parser << uri;
105
106     if (uri && (parser == self->m_parserId))
107     {
108         QString modifiedURI = self->manHandleURI(uri);
109         self->m_uriList.append(modifiedURI);
110         if (self->m_firstItem)
111         {
112             Q_EMIT self->firstItemParsed();
113             self->m_firstItem = false;
114         }
115     }
116 }
117
118 /********************************************************************
119  * MafwGstRendererPlaylistFileUtility::readyCb
120  ********************************************************************/
121 void MafwGstRendererPlaylistFileUtility::readyCb(TotemPlParser* parser,
122                                             GAsyncResult* async_result,
123                                             MafwGstRendererPlaylistFileUtility* self)
124 {
125     qDebug() << __PRETTY_FUNCTION__ << parser;
126     if (parser == self->m_parserId)
127     {
128         GError *error = 0;
129         bool success = true;
130
131         TotemPlParserResult  result = totem_pl_parser_parse_finish(parser,
132                                                                    async_result,
133                                                                    &error);
134         qDebug() << __PRETTY_FUNCTION__ << result;
135         if (result != TOTEM_PL_PARSER_RESULT_SUCCESS)
136         {
137             success = false;
138             qWarning() << __PRETTY_FUNCTION__ << ": Playlist file parsing failed";
139             if (error)
140             {
141                 qWarning() << error->message;
142                 g_error_free (error);
143             }
144         }
145         //Actually parsing is not ready yet, we might have no results yet.
146         Q_EMIT self->parsingReady(success);
147     }
148 }
149
150 /********************************************************************
151  * MafwGstRendererPlaylistFileUtility::manHandleURI
152  ********************************************************************/
153 QString MafwGstRendererPlaylistFileUtility::manHandleURI(const QString &itemUri) const
154 {
155     qDebug() << __FUNCTION__ << "Orig: " << itemUri;
156     QString modifiedUri = itemUri;
157
158     if( itemUri.endsWith(".asf", Qt::CaseInsensitive)
159         && itemUri.startsWith("http://", Qt::CaseInsensitive) )
160     {
161         modifiedUri.replace(0,4, "mmsh");
162     }
163     qDebug() << __FUNCTION__ << "Handled: " << modifiedUri;
164     return modifiedUri;
165 }