~ubuntu-branches/ubuntu/vivid/mediatomb/vivid

« back to all changes in this revision

Viewing changes to src/scripting/playlist_parser_script.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-03-02 13:09:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080302130916-zlljdze3kt7vuq4b
Tags: 0.11.0-1
* New upstream release.
* Include message about which inotify headers will be used when enabling
  inotify runtime support.
* Fixed error with use of INTERFACE in init script. Also removed use of -m
  option.
* Including new config.xml options.
* Added more build dependencies for new upstream release.
* Removed build dependency of libid3-dev, taglib is now preferred.
* mediatomb.xpm and manpage.xml is now included in orig tarball.
* inotify patch is not needed anymore.
* md5 patch has been committed upstream and is no longer needed. Also removed
  README.Debian.
* TwinHelix PNG fix is now used. Removed from TODO.
* Adding dependency of iceweasel for mediatomb package.
* Updated copyright file.
* Updated watch file.
* Updated rules file for proper configure options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
9
9
    
10
 
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
10
    Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
11
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
12
                            Leonhard Wimmer <leo@mediatomb.cc>
13
13
    
24
24
    version 2 along with MediaTomb; if not, write to the Free Software
25
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26
26
    
27
 
    $Id: playlist_parser_script.cc 1388 2007-07-11 17:08:40Z jin_eld $
 
27
    $Id: playlist_parser_script.cc 1698 2008-02-23 20:48:30Z lww $
28
28
*/
29
29
 
30
30
/// \file playlist_parser_script.cc
81
81
    currentHandle = NULL;
82
82
    currentObjectID = INVALID_OBJECT_ID;
83
83
    currentLine = NULL;
84
 
    
85
 
    defineFunction(_("readln"), js_readln, 0);
86
 
    
87
 
    String scriptPath = ConfigManager::getInstance()->getOption(CFG_IMPORT_SCRIPTING_PLAYLIST_SCRIPT); 
88
 
    load(scriptPath);
 
84
 
 
85
#ifdef JS_THREADSAFE
 
86
    JS_SetContextThread(cx);
 
87
    JS_BeginRequest(cx);
 
88
#endif
 
89
  
 
90
    try
 
91
    {
 
92
        defineFunction(_("readln"), js_readln, 0);
 
93
 
 
94
        String scriptPath = ConfigManager::getInstance()->getOption(CFG_IMPORT_SCRIPTING_PLAYLIST_SCRIPT); 
 
95
        load(scriptPath);
 
96
        root = JS_NewScriptObject(cx, script);
 
97
        JS_AddNamedRoot(cx, &root, "PlaylistScript");
 
98
    }
 
99
    catch (Exception ex)
 
100
    {
 
101
#ifdef JS_THREADSAFE
 
102
        JS_EndRequest(cx);
 
103
        JS_ClearContextThread(cx);
 
104
#endif
 
105
        throw ex;
 
106
    }
 
107
    
 
108
#ifdef JS_THREADSAFE
 
109
        JS_EndRequest(cx);
 
110
        JS_ClearContextThread(cx);
 
111
#endif
89
112
}
90
113
 
91
114
String PlaylistParserScript::readln()
92
115
{
 
116
    String ret;
93
117
    if (!currentHandle)
94
118
        throw _Exception(_("Readline not yet setup for use"));
95
119
 
96
120
    if ((currentTask == nil) || (!currentTask->isValid()))
97
121
        return nil;
98
122
 
99
 
    if (fgets(currentLine, ONE_TEXTLINE_BYTES, currentHandle) == NULL)
100
 
        return nil;
101
 
    else
102
 
        return trim_string(String(currentLine));
 
123
    while (true)
 
124
    {
 
125
        if(fgets(currentLine, ONE_TEXTLINE_BYTES, currentHandle) == NULL)
 
126
            return nil;
 
127
 
 
128
        ret = trim_string(String(currentLine));
 
129
        if (string_ok(ret))
 
130
            return ret;
 
131
    }
103
132
}
104
133
 
105
134
void PlaylistParserScript::processPlaylistObject(zmm::Ref<CdsObject> obj, Ref<CMTask> task)
106
135
{
107
 
 
108
 
   if ((currentObjectID != INVALID_OBJECT_ID) || (currentHandle != NULL) ||
109
 
       (currentLine != NULL))
110
 
       throw _Exception(_("recursion not allowed!"));
111
 
 
112
 
   if (!IS_CDS_PURE_ITEM(obj->getObjectType()))
113
 
   {
114
 
       throw _Exception(_("only allowed for pure items"));
115
 
   }
116
 
 
117
 
   currentTask = task;
118
 
   currentObjectID = obj->getID();
119
 
   currentLine = (char *)MALLOC(ONE_TEXTLINE_BYTES);
120
 
   if (!currentLine)
121
 
   {
122
 
       currentObjectID = INVALID_OBJECT_ID;
123
 
       currentTask = nil;
124
 
       throw _Exception(_("failed to allocate memory for playlist parsing!"));
125
 
   }
126
 
   currentLine[0] = '\0';
127
 
 
128
 
   currentHandle = fopen(obj->getLocation().c_str(), "r");
129
 
   if (!currentHandle)
130
 
   {
131
 
       currentObjectID = INVALID_OBJECT_ID;
132
 
       currentTask = nil;
133
 
       FREE(currentLine);
134
 
       throw _Exception(_("failed to open file: ") + obj->getLocation());
135
 
   }
136
 
 
137
 
   JSObject *playlist = JS_NewObject(cx, NULL, NULL, glob);
138
 
 
139
 
   try
140
 
   {
141
 
       setObjectProperty(glob, _("playlist"), playlist);
142
 
       cdsObject2jsObject(obj, playlist);
143
 
 
144
 
       execute();
145
 
   }
146
 
 
147
 
   catch (Exception e)
148
 
   {
149
 
       fclose(currentHandle);
150
 
       currentHandle = NULL;
151
 
 
152
 
       FREE(currentLine);
153
 
       currentLine = NULL;
154
 
 
155
 
       currentObjectID = INVALID_OBJECT_ID;
156
 
       currentTask = nil;
157
 
 
158
 
       throw e;
159
 
   }
160
 
 
161
 
   fclose(currentHandle);
162
 
   currentHandle = NULL;
163
 
 
164
 
   FREE(currentLine);
165
 
   currentLine = NULL;
166
 
 
167
 
   currentObjectID = INVALID_OBJECT_ID;
168
 
   currentTask = nil;
169
 
}
170
 
 
 
136
#ifdef JS_THREADSAFE
 
137
    JS_SetContextThread(cx);
 
138
    JS_BeginRequest(cx);
 
139
#endif
 
140
    if ((currentObjectID != INVALID_OBJECT_ID) || (currentHandle != NULL) ||
 
141
            (currentLine != NULL))
 
142
    {
 
143
#ifdef JS_THREADSAFE
 
144
        JS_EndRequest(cx);
 
145
        JS_ClearContextThread(cx);
 
146
#endif
 
147
        throw _Exception(_("recursion not allowed!"));
 
148
    }
 
149
 
 
150
    if (!IS_CDS_PURE_ITEM(obj->getObjectType()))
 
151
    {
 
152
#ifdef JS_THREADSAFE
 
153
        JS_EndRequest(cx);
 
154
        JS_ClearContextThread(cx);
 
155
#endif
 
156
        throw _Exception(_("only allowed for pure items"));
 
157
    }
 
158
 
 
159
    currentTask = task;
 
160
    currentObjectID = obj->getID();
 
161
    currentLine = (char *)MALLOC(ONE_TEXTLINE_BYTES);
 
162
    if (!currentLine)
 
163
    {
 
164
        currentObjectID = INVALID_OBJECT_ID;
 
165
        currentTask = nil;
 
166
#ifdef JS_THREADSAFE
 
167
        JS_EndRequest(cx);
 
168
        JS_ClearContextThread(cx);
 
169
#endif
 
170
        throw _Exception(_("failed to allocate memory for playlist parsing!"));
 
171
    }
 
172
 
 
173
    currentLine[0] = '\0';
 
174
 
 
175
    currentHandle = fopen(obj->getLocation().c_str(), "r");
 
176
    if (!currentHandle)
 
177
    {
 
178
        currentObjectID = INVALID_OBJECT_ID;
 
179
        currentTask = nil;
 
180
        FREE(currentLine);
 
181
#ifdef JS_THREADSAFE
 
182
        JS_EndRequest(cx);
 
183
        JS_ClearContextThread(cx);
 
184
#endif
 
185
        throw _Exception(_("failed to open file: ") + obj->getLocation());
 
186
    }
 
187
 
 
188
    JSObject *playlist = JS_NewObject(cx, NULL, NULL, glob);
 
189
 
 
190
    try
 
191
    {
 
192
        setObjectProperty(glob, _("playlist"), playlist);
 
193
        cdsObject2jsObject(obj, playlist);
 
194
 
 
195
        execute();
 
196
    }
 
197
 
 
198
    catch (Exception e)
 
199
    {
 
200
        fclose(currentHandle);
 
201
        currentHandle = NULL;
 
202
 
 
203
        FREE(currentLine);
 
204
        currentLine = NULL;
 
205
 
 
206
        currentObjectID = INVALID_OBJECT_ID;
 
207
        currentTask = nil;
 
208
 
 
209
#ifdef JS_THREADSAFE
 
210
        JS_EndRequest(cx);
 
211
        JS_ClearContextThread(cx);
 
212
#endif
 
213
        throw e;
 
214
    }
 
215
 
 
216
    fclose(currentHandle);
 
217
    currentHandle = NULL;
 
218
 
 
219
    FREE(currentLine);
 
220
    currentLine = NULL;
 
221
 
 
222
    currentObjectID = INVALID_OBJECT_ID;
 
223
    currentTask = nil;
 
224
 
 
225
    gc_counter++;
 
226
    if (gc_counter > JS_CALL_GC_AFTER_NUM)
 
227
    {
 
228
        JS_MaybeGC(cx);
 
229
        gc_counter = 0;
 
230
    }
 
231
 
 
232
#ifdef JS_THREADSAFE
 
233
    JS_EndRequest(cx);
 
234
    JS_ClearContextThread(cx);
 
235
#endif
 
236
 
 
237
}
 
238
 
 
239
 
 
240
PlaylistParserScript::~PlaylistParserScript()
 
241
{
 
242
#ifdef JS_THREADSAFE
 
243
    JS_SetContextThread(cx);
 
244
    JS_BeginRequest(cx);
 
245
#endif
 
246
 
 
247
    if (root)
 
248
        JS_RemoveRoot(cx, &root);
 
249
 
 
250
#ifdef JS_THREADSAFE
 
251
    JS_EndRequest(cx);
 
252
    JS_ClearContextThread(cx);
 
253
#endif
 
254
 
 
255
}
171
256
#endif // HAVE_JS
 
257