~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« 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-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    playlist_parser_script.cc - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
14
    MediaTomb is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License version 2
 
16
    as published by the Free Software Foundation.
 
17
    
 
18
    MediaTomb is distributed in the hope that it will be useful,
 
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
    GNU General Public License for more details.
 
22
    
 
23
    You should have received a copy of the GNU General Public License
 
24
    version 2 along with MediaTomb; if not, write to the Free Software
 
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
    
 
27
    $Id: playlist_parser_script.cc 1388 2007-07-11 17:08:40Z jin_eld $
 
28
*/
 
29
 
 
30
/// \file playlist_parser_script.cc
 
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
    #include "autoconfig.h"
 
34
#endif
 
35
 
 
36
#ifdef HAVE_JS
 
37
 
 
38
#include "playlist_parser_script.h"
 
39
#include "config_manager.h"
 
40
#include "js_functions.h"
 
41
 
 
42
#define ONE_TEXTLINE_BYTES  1024
 
43
 
 
44
using namespace zmm;
 
45
 
 
46
extern "C" {
 
47
 
 
48
static JSBool
 
49
js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 
50
{
 
51
    PlaylistParserScript *self = (PlaylistParserScript *)JS_GetPrivate(cx, obj);
 
52
 
 
53
    String line;
 
54
    
 
55
    try
 
56
    {
 
57
        line = self->readln();
 
58
    }
 
59
    catch (ServerShutdownException se)
 
60
    {
 
61
        log_warning("Aborting script execution due to server shutdown.\n");
 
62
        return JS_FALSE;
 
63
    }
 
64
    catch (Exception e)
 
65
    {
 
66
        e.printStackTrace();
 
67
        return JS_TRUE;
 
68
    }
 
69
 
 
70
    JSString *jsline = JS_NewStringCopyZ(cx, line.c_str());
 
71
 
 
72
    *rval = STRING_TO_JSVAL(jsline);
 
73
  
 
74
    return JS_TRUE;
 
75
}
 
76
    
 
77
} // extern "C"
 
78
 
 
79
PlaylistParserScript::PlaylistParserScript(Ref<Runtime> runtime) : Script(runtime)
 
80
{
 
81
    currentHandle = NULL;
 
82
    currentObjectID = INVALID_OBJECT_ID;
 
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);
 
89
}
 
90
 
 
91
String PlaylistParserScript::readln()
 
92
{
 
93
    if (!currentHandle)
 
94
        throw _Exception(_("Readline not yet setup for use"));
 
95
 
 
96
    if ((currentTask == nil) || (!currentTask->isValid()))
 
97
        return nil;
 
98
 
 
99
    if (fgets(currentLine, ONE_TEXTLINE_BYTES, currentHandle) == NULL)
 
100
        return nil;
 
101
    else
 
102
        return trim_string(String(currentLine));
 
103
}
 
104
 
 
105
void PlaylistParserScript::processPlaylistObject(zmm::Ref<CdsObject> obj, Ref<CMTask> task)
 
106
{
 
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
 
 
171
#endif // HAVE_JS