00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef HAVE_JS
00037
00038 #include "js_functions.h"
00039
00040 #include "script.h"
00041 #include <typeinfo>
00042 #include "storage.h"
00043 #include "content_manager.h"
00044 #include "config_manager.h"
00045 #include "metadata_handler.h"
00046
00047 using namespace zmm;
00048
00049 extern "C" {
00050
00051 JSBool
00052 js_print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00053 {
00054 uintN i;
00055 JSString *str;
00056
00057 for (i = 0; i < argc; i++)
00058 {
00059 str = JS_ValueToString(cx, argv[i]);
00060 if (!str)
00061 return JS_TRUE;
00062 argv[i] = STRING_TO_JSVAL(str);
00063 log_js("%s\n", JS_GetStringBytes(str));
00064 }
00065 return JS_TRUE;
00066 }
00067
00068 JSBool
00069 js_copyObject(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00070 {
00071 jsval arg;
00072 JSObject *js_cds_obj;
00073 JSObject *js_cds_clone_obj;
00074
00075 Script *self = (Script *)JS_GetPrivate(cx, obj);
00076
00077 try
00078 {
00079 arg = argv[0];
00080 if (!JSVAL_IS_OBJECT(arg))
00081 return JS_TRUE;
00082
00083 if (!JS_ValueToObject(cx, arg, &js_cds_obj))
00084 return JS_TRUE;
00085
00086 argv[0] = OBJECT_TO_JSVAL(js_cds_obj);
00087
00088 Ref<CdsObject> cds_obj = self->jsObject2cdsObject(js_cds_obj, nil);
00089 js_cds_clone_obj = JS_NewObject(cx, NULL, NULL, NULL);
00090 argv[1] = OBJECT_TO_JSVAL(js_cds_clone_obj);
00091
00092 self->cdsObject2jsObject(cds_obj, js_cds_clone_obj);
00093
00094 *rval = OBJECT_TO_JSVAL(js_cds_clone_obj);
00095
00096 return JS_TRUE;
00097
00098 }
00099 catch (ServerShutdownException se)
00100 {
00101 log_warning("Aborting script execution due to server shutdown.\n");
00102 return JS_FALSE;
00103 }
00104 catch (Exception e)
00105 {
00106 log_error("%s\n", e.getMessage().c_str());
00107 e.printStackTrace();
00108 }
00109 return JS_TRUE;
00110 }
00111
00112 JSBool
00113 js_addCdsObject(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00114 {
00115 try
00116 {
00117 jsval arg;
00118 JSString *str;
00119 String path;
00120 String containerclass;
00121
00122 JSObject *js_cds_obj;
00123 JSObject *js_orig_obj = NULL;
00124 Ref<CdsObject> orig_object;
00125
00126 Ref<StringConverter> p2i;
00127 Ref<StringConverter> i2i;
00128
00129 Script *self = (Script *)JS_GetPrivate(cx, obj);
00130
00131 if (self == NULL)
00132 {
00133 log_debug("Could not retrieve class instance from global object\n");
00134 return JS_FALSE;
00135 }
00136
00137 if (self->whoami() == S_PLAYLIST)
00138 {
00139 p2i = StringConverter::p2i();
00140 }
00141 else
00142 {
00143 i2i = StringConverter::i2i();
00144 }
00145
00146 arg = argv[0];
00147 if (!JSVAL_IS_OBJECT(arg))
00148 return JS_TRUE;
00149 if (!JS_ValueToObject(cx, arg, &js_cds_obj))
00150 return JS_TRUE;
00151
00152
00153 argv[0] = OBJECT_TO_JSVAL(js_cds_obj);
00154
00155 str = JS_ValueToString(cx, argv[1]);
00156 if (!str)
00157 path = _("/");
00158 else
00159 path = JS_GetStringBytes(str);
00160
00161 JSString *cont = JS_ValueToString(cx, argv[2]);
00162 if (cont)
00163 {
00164 containerclass = JS_GetStringBytes(cont);
00165 if (!string_ok(containerclass) || containerclass == "undefined")
00166 containerclass = nil;
00167 }
00168
00169 if (self->whoami() == S_PLAYLIST)
00170 js_orig_obj = self->getObjectProperty(obj, _("playlist"));
00171 else if (self->whoami() == S_IMPORT)
00172 js_orig_obj = self->getObjectProperty(obj, _("orig"));
00173
00174 if (js_orig_obj == NULL)
00175 {
00176 log_debug("Could not retrieve orig/playlist object\n");
00177 return JS_TRUE;
00178 }
00179
00180
00181 argv[1] = OBJECT_TO_JSVAL(js_orig_obj);
00182
00183 orig_object = self->jsObject2cdsObject(js_orig_obj, self->getProcessedObject());
00184 if (orig_object == nil)
00185 return JS_TRUE;
00186
00187 Ref<CdsObject> cds_obj;
00188 Ref<ContentManager> cm = ContentManager::getInstance();
00189 int pcd_id = INVALID_OBJECT_ID;
00190
00191 if (self->whoami() == S_PLAYLIST)
00192 {
00193 int otype = self->getIntProperty(js_cds_obj, _("objectType"), -1);
00194 if (otype == -1)
00195 {
00196 log_error("missing objectType property\n");
00197 return JS_TRUE;
00198 }
00199
00200 if (!IS_CDS_ITEM_EXTERNAL_URL(otype) &&
00201 !IS_CDS_ITEM_INTERNAL_URL(otype))
00202 {
00203 String loc = self->getProperty(js_cds_obj, _("location"));
00204 if (string_ok(loc) &&
00205 (IS_CDS_PURE_ITEM(otype) || IS_CDS_ACTIVE_ITEM(otype)))
00206 loc = normalizePath(loc);
00207
00208 pcd_id = cm->addFile(loc, false, false, true);
00209 if (pcd_id == INVALID_OBJECT_ID)
00210 return JS_TRUE;
00211
00212 Ref<CdsObject> mainObj = Storage::getInstance()->loadObject(pcd_id);
00213 cds_obj = self->jsObject2cdsObject(js_cds_obj, mainObj);
00214 }
00215 else
00216 cds_obj = self->jsObject2cdsObject(js_cds_obj, self->getProcessedObject());
00217 }
00218 else
00219 cds_obj = self->jsObject2cdsObject(js_cds_obj, orig_object);
00220
00221 if (cds_obj == nil)
00222 return JS_TRUE;
00223
00224 int id;
00225
00226 if ((self->whoami() == S_PLAYLIST) &&
00227 (ConfigManager::getInstance()->
00228 getBoolOption(CFG_IMPORT_SCRIPTING_PLAYLIST_SCRIPT_LINK_OBJECTS)))
00229 {
00230 path = p2i->convert(path);
00231 id = cm->addContainerChain(path, containerclass,
00232 orig_object->getID());
00233 }
00234 else
00235 {
00236 if (self->whoami() == S_PLAYLIST)
00237 path = p2i->convert(path);
00238 else
00239 path = i2i->convert(path);
00240
00241 id = cm->addContainerChain(path, containerclass);
00242 }
00243
00244 cds_obj->setParentID(id);
00245 if (!IS_CDS_ITEM_EXTERNAL_URL(cds_obj->getObjectType()) &&
00246 !IS_CDS_ITEM_INTERNAL_URL(cds_obj->getObjectType()))
00247 {
00251
00252 if (self->whoami() == S_PLAYLIST)
00253 {
00254 if (pcd_id == INVALID_OBJECT_ID)
00255 return JS_TRUE;
00256
00258 if (IS_CDS_ACTIVE_ITEM(cds_obj->getObjectType()))
00259 cds_obj->setFlag(OBJECT_FLAG_PLAYLIST_REF);
00260 cds_obj->setRefID(pcd_id);
00261 }
00262 else
00263 cds_obj->setRefID(orig_object->getID());
00264
00265 cds_obj->setFlag(OBJECT_FLAG_USE_RESOURCE_REF);
00266 }
00267 else if (IS_CDS_ITEM_EXTERNAL_URL(cds_obj->getObjectType()) ||
00268 IS_CDS_ITEM_INTERNAL_URL(cds_obj->getObjectType()))
00269 {
00270 if ((self->whoami() == S_PLAYLIST) &&
00271 (ConfigManager::getInstance()->
00272 getBoolOption(CFG_IMPORT_SCRIPTING_PLAYLIST_SCRIPT_LINK_OBJECTS)))
00273 {
00274 cds_obj->setFlag(OBJECT_FLAG_PLAYLIST_REF);
00275 cds_obj->setRefID(orig_object->getID());
00276 }
00277 }
00278
00279 cds_obj->setID(INVALID_OBJECT_ID);
00280 cm->addObject(cds_obj);
00281
00282
00283 String tmp = String::from(id);
00284
00285 JSString *str2 = JS_NewStringCopyN(cx, tmp.c_str(), tmp.length());
00286 if (!str2)
00287 return JS_TRUE;
00288 *rval = STRING_TO_JSVAL(str2);
00289
00290 return JS_TRUE;
00291 }
00292 catch (ServerShutdownException se)
00293 {
00294 log_warning("Aborting script execution due to server shutdown.\n");
00295 return JS_FALSE;
00296 }
00297 catch (Exception e)
00298 {
00299 log_error("%s\n", e.getMessage().c_str());
00300 e.printStackTrace();
00301 }
00302 return JS_TRUE;
00303 }
00304
00305 static JSBool convert_charset_generic(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval, charset_convert_t chr)
00306 {
00307 try
00308 {
00309 JSString *str;
00310 String result;
00311
00312 Script *self = (Script *)JS_GetPrivate(cx, obj);
00313
00314 if (self == NULL)
00315 {
00316 log_debug("Could not retrieve class instance from global object\n");
00317 return JS_FALSE;
00318 }
00319
00320 if (JSVAL_IS_STRING(argv[0]))
00321 {
00322 str = JS_ValueToString(cx, argv[0]);
00323 if (str)
00324 result = JS_GetStringBytes(str);
00325 }
00326
00327 if (result != nil)
00328 {
00329 result = self->convertToCharset(result, chr);
00330 JSString *str2 = JS_NewStringCopyN(cx, result.c_str(), result.length());
00331 if (!str2)
00332 return JS_TRUE;
00333 *rval = STRING_TO_JSVAL(str2);
00334 }
00335 }
00336 catch (ServerShutdownException se)
00337 {
00338 log_warning("Aborting script execution due to server shutdown.\n");
00339 return JS_FALSE;
00340 }
00341 catch (Exception e)
00342 {
00343 log_error("%s\n", e.getMessage().c_str());
00344 e.printStackTrace();
00345 }
00346 return JS_TRUE;
00347 }
00348
00349
00350 JSBool js_f2i(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00351 {
00352 return convert_charset_generic(cx, obj, argc, argv, rval, F2I);
00353 }
00354
00355 JSBool js_m2i(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00356 {
00357 return convert_charset_generic(cx, obj, argc, argv, rval, M2I);
00358 }
00359
00360 JSBool js_p2i(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00361 {
00362 return convert_charset_generic(cx, obj, argc, argv, rval, P2I);
00363 }
00364
00365 JSBool js_j2i(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00366 {
00367 return convert_charset_generic(cx, obj, argc, argv, rval, J2I);
00368 }
00369
00370 }
00371
00372 #endif //HAVE_JS