DBUS_EXPORT_API void dock_bus_message_notify(gchar* appid, gchar* itemid) { JSObjectRef info = json_create(); json_append_string(info, "appid", appid); json_append_string(info, "itemid", itemid); js_post_message("message_notify", info); }
PRIVATE void _insert_category(JSObjectRef categories, int array_index, int id, const char* name) { JSObjectRef item = json_create(); json_append_number(item, "ID", id); json_append_string(item, "Name", name); json_array_insert(categories, array_index, item); }
JS_EXPORT_API JSObjectRef installer_get_installation_info () { GRAB_CTX (); JSObjectRef json = json_create (); json_append_string (json, "target", opt_target); json_append_string (json, "home", opt_home); json_append_string (json, "username", opt_username); json_append_string (json, "hostname", opt_hostname); json_append_string (json, "password", opt_password); json_append_string (json, "layout", opt_layout); json_append_string (json, "variant", opt_variant); json_append_string (json, "timezone", opt_timezone); json_append_string (json, "locale", opt_locale); json_append_string (json, "grub", opt_grub); UNGRAB_CTX (); return json; }
static gboolean not_recognize_handler(gpointer data) { g_debug("[%s] not recognized", __func__); g_debug("[%s] send auth-failed signal", __func__); JSObjectRef json = json_create(); recognition_info.DELAY_TIME = 4.0; char* msg = g_strdup_printf(_("%sRetry automatically in %ds"), ERR_POST_PREFIX, (int)recognition_info.DELAY_TIME); json_append_string(json, "error", msg); g_free(msg); js_post_message("auth-failed", json); g_timer_start(recognition_info.timer); recognition_info.reco_state = NOT_START_RECOGNIZING; return G_SOURCE_REMOVE; }
static void json_append_object(lua_State *l, json_config_t *cfg, strbuf_t *json) { int comma, keytype; json_encode_descend(l, cfg); /* Object */ strbuf_append_char(json, '{'); lua_pushnil(l); /* table, startkey */ comma = 0; while (lua_next(l, -2) != 0) { if (comma) strbuf_append_char(json, ','); else comma = 1; /* table, key, value */ keytype = lua_type(l, -2); if (keytype == LUA_TNUMBER) { strbuf_append_char(json, '"'); json_append_number(l, json, -2, cfg); strbuf_append_mem(json, "\":", 2); } else if (keytype == LUA_TSTRING) { json_append_string(l, json, -2); strbuf_append_char(json, ':'); } else { json_encode_exception(l, cfg, -2, "table key must be a number or string"); /* never returns */ } /* table, key, value */ json_append_data(l, cfg, json); lua_pop(l, 1); /* table, key */ } strbuf_append_char(json, '}'); cfg->current_depth--; }
/* Serialise Lua data into JSON string. */ static void json_append_data(lua_State *l, struct luaL_serializer *cfg, int current_depth, strbuf_t *json) { struct luaL_field field; luaL_checkfield(l, cfg, -1, &field); switch (field.type) { case MP_UINT: return json_append_uint(cfg, json, field.ival); case MP_STR: case MP_BIN: return json_append_string(cfg, json, field.sval.data, field.sval.len); case MP_INT: return json_append_int(cfg, json, field.ival); case MP_FLOAT: return json_append_number(cfg, json, field.fval); case MP_DOUBLE: return json_append_number(cfg, json, field.dval); case MP_BOOL: if (field.bval) strbuf_append_mem(json, "true", 4); else strbuf_append_mem(json, "false", 5); break; case MP_NIL: json_append_nil(cfg, json); break; case MP_MAP: if (current_depth >= cfg->encode_max_depth) return json_append_nil(cfg, json); /* Limit nested maps */ json_append_object(l, cfg, current_depth, json); return; case MP_ARRAY: /* Array */ if (current_depth >= cfg->encode_max_depth) return json_append_nil(cfg, json); /* Limit nested arrays */ json_append_array(l, cfg, current_depth, json, field.size); return; case MP_EXT: /* handled by luaL_convertfield */ assert(false); return; } }
static void json_append_object(lua_State *l, struct luaL_serializer *cfg, int current_depth, strbuf_t *json) { int comma; /* Object */ strbuf_append_char(json, '{'); lua_pushnil(l); /* table, startkey */ comma = 0; while (lua_next(l, -2) != 0) { if (comma) strbuf_append_char(json, ','); else comma = 1; struct luaL_field field; luaL_checkfield(l, cfg, -2, &field); if (field.type == MP_UINT) { strbuf_append_char(json, '"'); json_append_uint(cfg, json, field.ival); strbuf_append_mem(json, "\":", 2); } else if (field.type == MP_INT) { strbuf_append_char(json, '"'); json_append_int(cfg, json, field.ival); strbuf_append_mem(json, "\":", 2); } else if (field.type == MP_STR) { json_append_string(cfg, json, field.sval.data, field.sval.len); strbuf_append_char(json, ':'); } else { luaL_error(l, "table key must be a number or string"); } /* table, key, value */ json_append_data(l, cfg, current_depth + 1, json); lua_pop(l, 1); /* table, key */ } strbuf_append_char(json, '}'); }
/* Serialise Lua data into JSON string. */ static void json_append_data(lua_State *l, json_config_t *cfg, int current_depth, strbuf_t *json) { int len; switch (lua_type(l, -1)) { case LUA_TSTRING: json_append_string(l, json, -1); break; case LUA_TNUMBER: json_append_number(l, cfg, json, -1); break; case LUA_TBOOLEAN: if (lua_toboolean(l, -1)) strbuf_append_mem(json, "true", 4); else strbuf_append_mem(json, "false", 5); break; case LUA_TTABLE: current_depth++; json_check_encode_depth(l, cfg, current_depth, json); len = lua_array_length(l, cfg, json); if (len > 0) json_append_array(l, cfg, current_depth, json, len); else json_append_object(l, cfg, current_depth, json); break; case LUA_TNIL: strbuf_append_mem(json, "null", 4); break; case LUA_TLIGHTUSERDATA: if (lua_touserdata(l, -1) == NULL) { strbuf_append_mem(json, "null", 4); break; } default: /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, * and LUA_TLIGHTUSERDATA) cannot be serialised */ json_encode_exception(l, cfg, json, -1, "type not supported"); /* never returns */ } }
void reco() { // RESOURCE_DIR defined in CMakeLists.txt char* args[] = {"/usr/bin/python", RESOURCE_DIR"/greeter/scripts/reco", recognition_info.current_username, NULL}; GError* err = NULL; char* is_same_person = NULL; g_spawn_sync(NULL, args, NULL, 0, NULL, NULL, &is_same_person, NULL, NULL, &err); if (err != NULL) { g_warning("[%s] %s", __func__, err->message); g_error_free(err); } g_debug("[%s] #%s#", __func__, is_same_person); if (g_strcmp0(is_same_person, "True") == 0) { recognition_info.reco_state = RECOGNIZED; g_free(is_same_person); } else { g_debug("[%s] not recognized", __func__); recognition_info.reco_times += 1; if (recognition_info.reco_times == MAX_RECO_TIME) { g_debug("[%s] reach the max recognition time", __func__); JSObjectRef json = json_create(); char* msg = g_strdup_printf("%s%s", ERR_POST_PREFIX, _("Please login with your password or click your picture to retry.")); json_append_string(json, "error", msg); g_free(msg); js_post_message("failed-too-much", json); recognition_info.detect_is_enabled = FALSE; recognition_info.reco_state = RECOGNIZE_FINISH; g_free(is_same_person); return; } recognition_info.reco_state = NOT_RECOGNIZED; } }
JS_EXPORT_API JSValueRef dcore_get_plugin_info(char const* path) { char* info_file_path = g_build_filename(path, "info.ini", NULL); GKeyFile* info_file = g_key_file_new(); g_key_file_load_from_file(info_file, info_file_path, G_KEY_FILE_NONE, NULL); g_free(info_file_path); JSObjectRef json = json_create(); char* id = g_key_file_get_string(info_file, "Plugin", "ID", NULL); json_append_string(json, "ID", id == NULL ? "" : id); g_free(id); char* name = g_key_file_get_string(info_file, "Plugin", "name", NULL); json_append_string(json, "name", name == NULL ? "" : name); g_free(name); char* description = g_key_file_get_string(info_file, "Plugin", "description", NULL); json_append_string(json, "description", description == NULL ? "" : description); g_free(description); int width = g_key_file_get_integer(info_file, "Plugin", "width", NULL); json_append_number(json, "width", width); int height = g_key_file_get_integer(info_file, "Plugin", "height", NULL); json_append_number(json, "height", height); GError* error = NULL; double x = g_key_file_get_double(info_file, "Plugin", "x", &error); if (error) { json_append_value(json, "x", jsvalue_null()); g_error_free(error); } else { json_append_number(json, "x", x); } error = NULL; double y = g_key_file_get_double(info_file, "Plugin", "y", &error); if (error) { json_append_value(json, "y", jsvalue_null()); g_error_free(error); } else { json_append_number(json, "y", y); } char* type = g_key_file_get_string(info_file, "Plugin", "type", NULL); json_append_string(json, "type", type == NULL ? "" : type); g_free(type); char* author = g_key_file_get_string(info_file, "Author", "author", NULL); json_append_string(json, "author", author == NULL ? "" : author); g_free(author); char* email = g_key_file_get_string(info_file, "Author", "email", NULL); json_append_string(json, "email", email == NULL ? "" : email); g_free(email); char* textdomain = g_key_file_get_string(info_file, "Locale", "textdomain", NULL); json_append_string(json, "textdomain", textdomain == NULL ? "" : textdomain); g_free(textdomain); gsize length = 0; char** js = g_key_file_get_string_list(info_file, "Resource", "js", &length, NULL); JSObjectRef js_arr = json_array_create(); trans_to_js_array(js, length, js_arr); json_append_value(json, "js", js_arr); g_strfreev(js); char** css = g_key_file_get_string_list(info_file, "Resource", "css", &length, NULL); JSObjectRef css_arr = json_array_create(); trans_to_js_array(css, length, css_arr); json_append_value(json, "css", css_arr); g_strfreev(css); char** screenshot = g_key_file_get_string_list(info_file, "Resource", "screenshot", &length, NULL); JSObjectRef ss_arr = json_array_create(); trans_to_js_array(screenshot, length, ss_arr); json_append_value(json, "screenshot", ss_arr); g_strfreev(screenshot); g_key_file_free(info_file); return json; }
JS_EXPORT_API gboolean lock_try_unlock (const gchar *username,const gchar *password) { if (lock_is_guest()) { js_post_signal("auth-succeed"); gtk_main_quit(); return TRUE; } gboolean succeed = FALSE; GDBusProxy *lock_proxy = NULL; GVariant *lock_succeed = NULL; GError *error = NULL; lock_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL, "com.deepin.dde.lock", "/com/deepin/dde/lock", "com.deepin.dde.lock", NULL, &error); if (error != NULL) { g_warning ("connect com.deepin.dde.lock failed"); g_error_free (error); } error = NULL; lock_succeed = g_dbus_proxy_call_sync (lock_proxy, "UnlockCheck", g_variant_new ("(ss)", username, password), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); //g_assert (lock_succeed != NULL); if (error != NULL) { g_warning ("try unlock:UnlockCheck %s\n", error->message); g_error_free (error); } error = NULL; g_variant_get (lock_succeed, "(b)", &succeed); g_variant_unref (lock_succeed); g_object_unref (lock_proxy); if (succeed) { js_post_signal("auth-succeed"); } else { JSObjectRef error_message = json_create(); json_append_string(error_message, "error", _("Wrong Password")); js_post_message("auth-failed", error_message); } return succeed; }
void json_encode_item(const mxArray *obj) { mxArray *item; mxChar *chars; unsigned int field, nfields; const char *fieldname; mxLogical *logical_ptr; double *double_ptr; float *single_ptr; uint8_t *uint8_ptr; int8_t *int8_ptr; uint16_t *uint16_ptr; int16_t *int16_ptr; uint32_t *uint32_ptr; int32_t *int32_ptr; uint64_t *uint64_ptr; int64_t *int64_ptr; unsigned int i, n; n = mxGetNumberOfElements(obj); if (mxIsChar(obj)) { json_append_char('"'); chars = mxGetChars(obj); for (i=0; i<n; i++) { switch (chars[i]) { case '"': case '\\': case '/': json_append_char('\\'); json_append_char(*(char*)&chars[i]); break; case '\b': json_append_string("\\b"); break; case '\f': json_append_string("\\f"); break; case '\n': json_append_string("\\n"); break; case '\r': json_append_string("\\r"); break; case '\t': json_append_string("\\t"); break; default: if ((chars[i] < 32) || (chars[i] > 126)) json_append_string("\\u%04hx",chars[i]); else json_append_char(*(char*)&chars[i]); } } json_append_char('"'); } else if (n == 0) { json_append_string("[]"); } else { if (n > 1) json_append_char('['); switch (mxGetClassID(obj)) { case mxSTRUCT_CLASS: nfields = mxGetNumberOfFields(obj); for (i=0; i<n; i++) { json_append_char('{'); for (field=0; field<nfields; field++) { fieldname = mxGetFieldNameByNumber(obj,field); item = mxGetFieldByNumber(obj,i,field); if (item != NULL) { json_append_string("\"%s\":",fieldname); json_encode_item(item); json_append_char(','); } } if (nfields > 0) json_strpos--; json_append_string("},"); } break; case mxCELL_CLASS: for (i=0; i<n; i++) { json_encode_item(mxGetCell(obj,i)); json_append_char(','); } break; case mxLOGICAL_CLASS: logical_ptr = mxGetData(obj); for (i=0; i<n; i++) { if (logical_ptr[i]) { json_append_string("true,"); } else { json_append_string("false,"); } } break; case mxDOUBLE_CLASS: double_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_float(double_ptr[i]); break; case mxSINGLE_CLASS: single_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_float(single_ptr[i]); break; case mxINT8_CLASS: int8_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%i,",int8_ptr[i]); break; case mxUINT8_CLASS: uint8_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%u,",uint8_ptr[i]); break; case mxINT16_CLASS: int16_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%i,",int16_ptr[i]); break; case mxUINT16_CLASS: uint16_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%u,",uint16_ptr[i]); break; case mxINT32_CLASS: int32_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%i,",int32_ptr[i]); break; case mxUINT32_CLASS: uint32_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%u,",uint32_ptr[i]); break; case mxINT64_CLASS: int64_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%lli,",int64_ptr[i]); break; case mxUINT64_CLASS: uint64_ptr = mxGetData(obj); for (i=0; i<n; i++) json_append_string("%llu,",uint64_ptr[i]); break; default: error_unsupported_class(obj); } if (json_strpos) json_strpos--; if (n > 1) json_append_char(']'); } }