Example #1
0
/*  called from the PDB (gimp_plugin_menu_register)  */
gboolean
gimp_plug_in_menu_register (GimpPlugIn  *plug_in,
                            const gchar *proc_name,
                            const gchar *menu_path)
{
  GimpPlugInProcedure *proc  = NULL;
  GError              *error = NULL;

  g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
  g_return_val_if_fail (proc_name != NULL, FALSE);
  g_return_val_if_fail (menu_path != NULL, FALSE);

  if (plug_in->plug_in_def)
    proc = gimp_plug_in_procedure_find (plug_in->plug_in_def->procedures,
                                        proc_name);

  if (! proc)
    proc = gimp_plug_in_procedure_find (plug_in->temp_procedures, proc_name);

  if (! proc)
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Plug-in \"%s\"\n(%s)\n"
                    "attempted to register the menu item \"%s\" "
                    "for the procedure \"%s\".\n"
                    "It has however not installed that procedure.  This "
                    "is not allowed.",
                    gimp_object_get_name (plug_in),
                    gimp_filename_to_utf8 (plug_in->prog),
                    menu_path, proc_name);

      return FALSE;
    }

  switch (GIMP_PROCEDURE (proc)->proc_type)
    {
    case GIMP_INTERNAL:
      return FALSE;

    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
      if (plug_in->call_mode != GIMP_PLUG_IN_CALL_QUERY &&
          plug_in->call_mode != GIMP_PLUG_IN_CALL_INIT)
        return FALSE;

    case GIMP_TEMPORARY:
      break;
    }

  if (! proc->menu_label)
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Plug-in \"%s\"\n(%s)\n"
                    "attempted to register the menu item \"%s\" "
                    "for procedure \"%s\".\n"
                    "The menu label given in gimp_install_procedure() "
                    "already contained a path.  To make this work, "
                    "pass just the menu's label to "
                    "gimp_install_procedure().",
                    gimp_object_get_name (plug_in),
                    gimp_filename_to_utf8 (plug_in->prog),
                    menu_path, proc_name);

      return FALSE;
    }

  if (! strlen (proc->menu_label))
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Plug-in \"%s\"\n(%s)\n"
                    "attempted to register the procedure \"%s\" "
                    "in the menu \"%s\", but the procedure has no label.  "
                    "This is not allowed.",
                    gimp_object_get_name (plug_in),
                    gimp_filename_to_utf8 (plug_in->prog),
                    proc_name, menu_path);

      return FALSE;
    }
  if (! gimp_plug_in_procedure_add_menu_path (proc, menu_path, &error))
    {
      gimp_message_literal (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
			    error->message);
      g_clear_error (&error);

      return FALSE;
    }

  return TRUE;
}
static void
gimp_plug_in_handle_proc_install (GimpPlugIn    *plug_in,
                                  GPProcInstall *proc_install)
{
  GimpPlugInProcedure *proc       = NULL;
  GimpProcedure       *procedure  = NULL;
  gchar               *canonical;
  gboolean             null_name  = FALSE;
  gboolean             valid_utf8 = FALSE;
  gint                 i;

  g_return_if_fail (proc_install != NULL);
  g_return_if_fail (proc_install->name != NULL);

  canonical = gimp_canonicalize_identifier (proc_install->name);

  /*  Sanity check for array arguments  */

  for (i = 1; i < proc_install->nparams; i++)
    {
      if ((proc_install->params[i].type == GIMP_PDB_INT32ARRAY ||
           proc_install->params[i].type == GIMP_PDB_INT8ARRAY  ||
           proc_install->params[i].type == GIMP_PDB_FLOATARRAY ||
           proc_install->params[i].type == GIMP_PDB_STRINGARRAY ||
           proc_install->params[i].type == GIMP_PDB_COLORARRAY)
          &&
          proc_install->params[i - 1].type != GIMP_PDB_INT32)
        {
          gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                        "Plug-In \"%s\"\n(%s)\n\n"
                        "attempted to install procedure \"%s\" "
                        "which fails to comply with the array parameter "
                        "passing standard.  Argument %d is noncompliant.",
                        gimp_object_get_name (plug_in),
                        gimp_file_get_utf8_name (plug_in->file),
                        canonical, i);
          g_free (canonical);
          return;
        }
    }

  /*  Sanity check strings for UTF-8 validity  */

#define VALIDATE(str)         (g_utf8_validate ((str), -1, NULL))
#define VALIDATE_OR_NULL(str) ((str) == NULL || g_utf8_validate ((str), -1, NULL))

  if (VALIDATE_OR_NULL (proc_install->menu_path) &&
      VALIDATE         (canonical)               &&
      VALIDATE_OR_NULL (proc_install->blurb)     &&
      VALIDATE_OR_NULL (proc_install->help)      &&
      VALIDATE_OR_NULL (proc_install->author)    &&
      VALIDATE_OR_NULL (proc_install->copyright) &&
      VALIDATE_OR_NULL (proc_install->date))
    {
      null_name  = FALSE;
      valid_utf8 = TRUE;

      for (i = 0; i < proc_install->nparams && valid_utf8 && !null_name; i++)
        {
          if (! proc_install->params[i].name)
            {
              null_name = TRUE;
            }
          else if (! (VALIDATE         (proc_install->params[i].name) &&
                      VALIDATE_OR_NULL (proc_install->params[i].description)))
            {
              valid_utf8 = FALSE;
            }
        }

      for (i = 0; i < proc_install->nreturn_vals && valid_utf8 && !null_name; i++)
        {
          if (! proc_install->return_vals[i].name)
            {
              null_name = TRUE;
            }
          else if (! (VALIDATE         (proc_install->return_vals[i].name) &&
                      VALIDATE_OR_NULL (proc_install->return_vals[i].description)))
            {
              valid_utf8 = FALSE;
            }
        }
    }

#undef VALIDATE
#undef VALIDATE_OR_NULL

  if (null_name)
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Plug-In \"%s\"\n(%s)\n\n"
                    "attempted to install a procedure NULL parameter name.",
                    gimp_object_get_name (plug_in),
                    gimp_file_get_utf8_name (plug_in->file));
      g_free (canonical);
      return;
    }

  if (! valid_utf8)
    {
      gimp_message (plug_in->manager->gimp, NULL, GIMP_MESSAGE_ERROR,
                    "Plug-In \"%s\"\n(%s)\n\n"
                    "attempted to install a procedure with invalid UTF-8 strings.",
                    gimp_object_get_name (plug_in),
                    gimp_file_get_utf8_name (plug_in->file));
      g_free (canonical);
      return;
    }

  /*  Create the procedure object  */

  switch (proc_install->type)
    {
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
      procedure = gimp_plug_in_procedure_new (proc_install->type,
                                              plug_in->file);
      break;

    case GIMP_TEMPORARY:
      procedure = gimp_temporary_procedure_new (plug_in);
      break;
    }

  proc = GIMP_PLUG_IN_PROCEDURE (procedure);

  proc->mtime                 = time (NULL);
  proc->installed_during_init = (plug_in->call_mode == GIMP_PLUG_IN_CALL_INIT);

  gimp_object_take_name (GIMP_OBJECT (procedure), canonical);
  gimp_procedure_set_strings (procedure,
                              proc_install->name,
                              proc_install->blurb,
                              proc_install->help,
                              proc_install->author,
                              proc_install->copyright,
                              proc_install->date,
                              NULL);

  gimp_plug_in_procedure_set_image_types (proc, proc_install->image_types);

  for (i = 0; i < proc_install->nparams; i++)
    {
      GParamSpec *pspec =
        gimp_pdb_compat_param_spec (plug_in->manager->gimp,
                                    proc_install->params[i].type,
                                    proc_install->params[i].name,
                                    proc_install->params[i].description);

      gimp_procedure_add_argument (procedure, pspec);
    }

  for (i = 0; i < proc_install->nreturn_vals; i++)
    {
      GParamSpec *pspec =
        gimp_pdb_compat_param_spec (plug_in->manager->gimp,
                                    proc_install->return_vals[i].type,
                                    proc_install->return_vals[i].name,
                                    proc_install->return_vals[i].description);

      gimp_procedure_add_return_value (procedure, pspec);
    }

  /*  Sanity check menu path  */

  if (proc_install->menu_path && strlen (proc_install->menu_path))
    {
      if (proc_install->menu_path[0] == '<')
        {
          GError *error = NULL;

          if (! gimp_plug_in_procedure_add_menu_path (proc,
                                                      proc_install->menu_path,
                                                      &error))
            {
              gimp_message_literal (plug_in->manager->gimp,
                                    NULL, GIMP_MESSAGE_WARNING,
                                    error->message);
              g_clear_error (&error);
            }
        }
      else
        {
          proc->menu_label = g_strdup (proc_install->menu_path);
        }
    }

  /*  Install the procedure  */

  switch (proc_install->type)
    {
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
      gimp_plug_in_def_add_procedure (plug_in->plug_in_def, proc);
      break;

    case GIMP_TEMPORARY:
      gimp_plug_in_add_temp_proc (plug_in, GIMP_TEMPORARY_PROCEDURE (proc));
      break;
    }

  g_object_unref (proc);
}