Branch: Tag:

2008-12-18

2008-12-18 15:05:05 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Added ModuleChoice.
ProviderChoice is now based on ModuleChoice.
Made ModuleChoice more robust against flipping values than what ProviderChoice used to be.
Fixes [bug 4615 (#4615)].

Rev: server/etc/modules/Variable.pmod/module.pmod:1.103

1: - // $Id: module.pmod,v 1.102 2008/01/29 10:37:05 mathias Exp $ + // $Id: module.pmod,v 1.103 2008/12/18 15:05:05 grubba Exp $      #include <module.h>   #include <roxen.h>
1392:    }   }    - // FIXME: Consider making a ModuleChoice as well. -  - //! Select a module that provides the specified interface. - class ProviderChoice + //! Select a module in the current configuration. + class ModuleChoice   {    inherit StringChoice; -  constant type = "ProviderChoice"; -  static Configuration conf; -  static string provides; -  static string default_id; -  static string local_id = ""; -  static int isset; +  constant type = "ModuleChoice"; +  protected Configuration conf; +  protected string module_id; +  protected string default_id;       int low_set(RoxenModule to)    {    RoxenModule old = changed_values[_id]; -  if (to == old) return 0; +     if (!old) { -  if (local_id != "") { -  old = transform_from_form(local_id); -  } -  if (!old) { +  if (module_id) { +  old = transform_from_form(module_id); +  } else {    old = default_value(); -  if (old) local_id = _name(old); +  if (old) { +  module_id = _name(old);    } -  changed_values[_id] = to; -  if (to == old) return 0; +     } -  +  if (old) { +  changed_values[_id] = old; +  } +  } +  if (to == old) return 0;    changed_values[_id] = to; -  local_id = _name(to); +  if (module_id == _name(to)) return 0; // Reloaded module or similar. +  module_id = _name(to);    if( get_changed_callback() )    get_changed_callback()( this_object() );    return 1;
1431:    int set(string|RoxenModule to)    {    if (stringp(to)) { -  local_id = to; +  module_id = to;    to = transform_from_form(to); -  isset = 1; +     }    return ::set(to);    }
1442:    {    RoxenModule res = changed_values[_id];    if (!res) { -  if (local_id != "") { +  if (module_id) {    // The module might have been reloaded.    // Try locating it again. -  res = transform_from_form(local_id); +  res = transform_from_form(module_id);    if (res) low_set(res); -  } else if(!isset) { +  } else {    res = default_value(); -  if(res) { -  set(res); +  if (res) low_set(res);    }    } -  } +     return res;    }       array get_choice_list()    { -  array res = conf->get_providers(provides); +  array res = indices(conf->otomod); +  // FIXME: Sort on priority as well?    sort(map(res, _title), res);    return res;    }    -  static string _name(RoxenModule val) +  protected string _name(RoxenModule val)    {    return val?val->module_local_id():"";    }    -  static string _title(RoxenModule val) +  protected string _title(RoxenModule val)    {    return val?val->module_name:"";    }    -  RoxenModule transform_from_form(string local_id, mapping|void v) +  RoxenModule transform_from_form(string module_id, mapping|void v)    { -  return conf->find_module(local_id); +  return conf->find_module(module_id);    }       RoxenModule default_value()    {    if (default_id) {    return transform_from_form(default_id); -  } else { -  array(RoxenModule) providers = conf->get_providers(provides); -  if (sizeof(providers)) { -  return providers[0]; +     } -  +  array(RoxenModule) modules = get_choice_list(); +  if (sizeof(modules)) { +  return modules[0]; +  }    return UNDEFINED;    } -  } +        array(string|mixed) verify_set( mixed new_value )    {    if (!new_value) { -  new_value = query(); -  } -  if (!new_value) { +     return ({ "Not configured", 0 });    }    return ({ 0, new_value });
1505:       //! @param default_id    //! The @[RoxenModule.module_local_id] of the default value. +  //! @param conf +  //! The current configuration. +  protected void create(string default_id, int flags, +  string std_name, string std_doc, +  Configuration conf) +  { +  this_program::default_id = default_id; +  this_program::conf = conf; +  ::create(0, ({}), flags, std_name, std_doc); +  } + } +  + //! Select a module that provides the specified interface. + class ProviderChoice + { +  inherit ModuleChoice; +  constant type = "ProviderChoice"; +  protected string provides; +  +  array get_choice_list() +  { +  array res = conf->get_providers(provides); +  // FIXME: Sort on priority as well? +  sort(map(res, _title), res); +  return res; +  } +  +  //! @param default_id +  //! The @[RoxenModule.module_local_id] of the default value.    //! @param provides    //! The provider string to match modules against.    //! @param conf    //! The current configuration. -  static void create(string default_id, int flags, +  protected void create(string default_id, int flags,    string std_name, string std_doc,    string provides, Configuration conf)    {    this_program::provides = provides; -  this_program::default_id = default_id; -  this_program::conf = conf; -  ::create(0, ({}), flags, std_name, std_doc); +  ::create(default_id, flags, std_name, std_doc, conf);    }   }