Beispiel #1
0
        /// <summary>
        /// Applies the JSON formatted result of an on-before-contextmenu script and fails silently (Error only visible in debug log).
        /// </summary>
        /// <param name="model"></param>
        /// <param name="result"></param>
        /// <param name="parameterName"></param>
        private void applyResultToModel(ref IMenuModel model, JObject result, string parameterName)
        {
            if (result.ContainsKey("clear") && result["clear"].ToObject <bool>())
            {
                model.Clear();
            }

            if (!result.ContainsKey("entries") || result["entries"] == null)
            {
                return;
            }

            JArray entries = result["entries"] as JArray;

            if (entries == null)
            {
                Tools.Logger.Log("Error while getting entries of result from parameter " + parameterName, Tools.Logger.LogLevel.error);
                return;
            }

            foreach (var entry in entries)
            {
                int    id   = 0;
                string text = "missing text";
                if (entry["id"] != null)
                {
                    try {
                        id = entry["id"].ToObject <int>();
                    } catch (Exception) {
                        Tools.Logger.Log("Invalid 'id' for entry of result from parameter " + parameterName, Tools.Logger.LogLevel.error);
                    }
                    if (id > 1998)
                    {
                        Tools.Logger.Log("id for entry of result is too high, only 1998 ids allowd from parameter " + parameterName, Tools.Logger.LogLevel.error);
                        id = 0;
                    }
                }
                id = ((int)CefMenuCommand.UserFirst) + id;
                if (entry["text"] != null)
                {
                    try {
                        text = entry["text"].ToObject <string>();
                    } catch (Exception) {
                        Tools.Logger.Log("Invalid 'text' for entry of result from parameter " + parameterName, Tools.Logger.LogLevel.error);
                    }
                }

                string type = "label";
                if (entry["type"] != null)
                {
                    try {
                        type = entry["type"].ToObject <string>();
                    } catch (Exception) {
                        Tools.Logger.Log("Invalid 'type' for entry of result from parameter " + parameterName, Tools.Logger.LogLevel.error);
                    }
                }

                switch (type)
                {
                case "separator":
                    model.AddSeparator();
                    break;

                case "checkbox":
                    model.AddCheckItem((CefMenuCommand)id, text);
                    bool isChecked = false;
                    if (entry["checked"] != null)
                    {
                        try {
                            isChecked = entry["checked"].ToObject <bool>();
                        } catch (Exception) {
                            Tools.Logger.Log("Invalid value for 'checked' for entry of result from parameter " + parameterName, Tools.Logger.LogLevel.error);
                        }
                        model.SetCheckedAt(model.Count - 1, isChecked);
                    }
                    break;

                default:
                    model.AddItem((CefMenuCommand)id, text);
                    break;
                }
            }
        }