Package Gnumed :: Package wxpython :: Module gmCfgWidgets |
[frames] | no frames]

# Source Code for Module Gnumed.wxpython.gmCfgWidgets

      1  """GNUmed configuration related widgets.
      2  """
      3  #================================================================
      4  __version__ = '$Revision: 1.4 $'
      5  __author__ = 'karsten.hilbert@gmx.net'
      6  __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
      7
      8  # stdlib
      9  import logging, sys
     10
     11
     12  # 3rd party
     13  import wx
     14
     15
     16  # GNUmed
     17  if __name__ == '__main__':
     18          sys.path.insert(0, '../../')
     19  from Gnumed.pycommon import gmCfg
     20  from Gnumed.pycommon import gmNetworkTools
     21  from Gnumed.pycommon import gmTools
     22  from Gnumed.pycommon import gmDispatcher
     23  from Gnumed.pycommon import gmCfg2
     24  from Gnumed.business import gmPraxis
     25  from Gnumed.wxpython import gmGuiHelpers
     26  from Gnumed.wxpython import gmListWidgets
     27
     28
     29  _log = logging.getLogger('gm.ui')
     30  _log.info(__version__)
     31
     32  #==============================================================================
     33 -def check_for_updates():
     34
     35          dbcfg = gmCfg.cCfgSQL()
     36
     37          url = dbcfg.get2 (
     38                  option = u'horstspace.update.url',
     39                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
     40                  bias = 'workplace',
     41                  default = u'http://www.gnumed.de/downloads/gnumed-versions.txt'
     42          )
     43
     44          consider_latest_branch = bool(dbcfg.get2 (
     45                  option = u'horstspace.update.consider_latest_branch',
     46                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
     47                  bias = 'workplace',
     48                  default = True
     49          ))
     50
     51          _cfg = gmCfg2.gmCfgData()
     52
     53          found, msg = gmNetworkTools.check_for_update (
     54                  url = url,
     55                  current_branch = _cfg.get(option = 'client_branch'),
     56                  current_version = _cfg.get(option = 'client_version'),
     57                  consider_latest_branch = consider_latest_branch
     58          )
     59
     60          if found is False:
     61                  gmDispatcher.send(signal = 'statustext', msg = _('Your client (%s) is up to date.') % _cfg.get(option = 'client_version'))
     62                  return
     63
     64          gmGuiHelpers.gm_show_info (
     65                  msg,
     66                  _('Checking for client updates')
     67          )
     68  #================================================================
     69 -def list_configuration(parent=None):
     70
     71          if parent is None:
     72                  parent = wx.GetApp().GetTopWindow()
     73
     74          #---------------
     75          def refresh(lctrl):
     76                  opts = gmCfg.get_all_options(order_by = u'owner, workplace, option')
     77
     78                  items = [ [
     79                          o['owner'],
     80                          o['workplace'],
     81                          o['option'],
     82                          o['value'],
     83                          o['type'],
     84                          gmTools.coalesce(o['description'], u'')
     85                  ] for o in opts ]
     86                  lctrl.set_string_items(items)
     87                  lctrl.set_data(opts)
     88          #---------------
     89          def tooltip(item):
     90                  return (
     91                          u'%s %s (#%s) %s\n'
     92                          u'\n'
     93                          u' %s @ %s\n'
     94                          u'\n'
     95                          u' %s: %s\n'
     96                          u'%s'
     97                  ) % (
     98                          gmTools.u_box_horiz_single * 3,
     99                          item['option'],
    100                          item['pk_cfg_item'],
    101                          gmTools.u_box_horiz_single * 3,
    102                          item['owner'],
    103                          item['workplace'],
    104                          item['type'],
    105                          gmTools.wrap(
    106                                  text = item['value'],
    107                                  width = 40,
    108                                  subsequent_indent = u' ' * 8
    109                          ),
    110                          gmTools.wrap (
    111                                  text = gmTools.coalesce(item['description'], u'', u'\n%s'),
    112                                  width = 40,
    113                                  initial_indent = u' ',
    114                                  subsequent_indent = u' '
    115                          )
    116                  )
    117          #---------------
    118          def delete(item):
    119                  delete_it = gmGuiHelpers.gm_show_question (
    120                          aTitle = _('Deleting option'),
    121                          aMessage = u'%s\n\n%s %s (#%s) %s\n\n%s\n\n%s' % (
    122                                  tooltip(item),
    123                                  gmTools.u_box_horiz_single * 3,
    124                                  item['option'],
    125                                  item['pk_cfg_item'],
    126                                  gmTools.u_box_horiz_single * 3,
    127                                  _('Do you really want to delete this option ?'),
    128                                  _('(GNUmed will re-create options as needed.)')
    129                          )
    130                  )
    131                  if not delete_it:
    132                          return False
    133
    134                  from Gnumed.wxpython.gmAuthWidgets import get_dbowner_connection
    135                  conn = get_dbowner_connection(procedure = _('Deleting option'))
    136                  if conn is None:
    137                          gmDispatcher.send(signal = 'statustext', msg = _('Cannot connect as database owner. Unable to delete option.'))
    138                          return False
    139
    140                  cfg = gmCfg.cCfgSQL()
    141                  cfg.delete(conn = conn, pk_option = item['pk_cfg_item'])
    142                  return True
    143          #---------------
    144          gmListWidgets.get_choices_from_list (
    145                  parent = parent,
    146                  msg = _('All configured options currently in the database.'),
    147                  caption = _('All configured options'),
    148                  columns = [ _('User'), _('Workplace'), _('Option'), _('Value'), _('Type'), _('Description') ],
    149                  refresh_callback = refresh,
    150                  delete_callback = delete,
    151                  ignore_OK_button = True,
    152                  list_tooltip_callback = tooltip
    153          )
    154  #================================================================
    155 -def configure_string_from_list_option(parent=None, message=None, option=None, bias='user', default_value=u'', choices=None, columns=None, data=None, caption=None):
    156
    157          dbcfg = gmCfg.cCfgSQL()
    158
    159          current_value = dbcfg.get2 (
    160                  option = option,
    161                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    162                  bias = bias,
    163                  default = default_value
    164          )
    165
    166          if parent is None:
    167                  parent = wx.GetApp().GetTopWindow()
    168
    169          if caption is None:
    170                  caption = _('Configuration')
    171
    172          selections = None
    173          if current_value is not None:
    174                  try:
    175                          selections = [choices.index(current_value)]
    176                  except ValueError:
    177                          pass
    178
    179          choice = gmListWidgets.get_choices_from_list (
    180                  parent = parent,
    181                  msg = message,
    182                  caption = caption,
    183                  choices = choices,
    184                  columns = columns,
    185                  data = data,
    186                  selections = selections,
    187                  single_selection = True,
    188                  can_return_empty = False
    189          )
    190
    191          # aborted
    192          if choice is None:
    193                  return
    194
    195          # same value selected again
    196          if choice == current_value:
    197                  return
    198
    199          dbcfg = gmCfg.cCfgSQL()
    200          dbcfg.set (
    201                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    202                  option = option,
    203                  value = choice
    204          )
    205
    206          return
    207  #================================================================
    208 -def configure_string_option(parent=None, message=None, option=None, bias=u'user', default_value=u'', validator=None):
    209
    210          dbcfg = gmCfg.cCfgSQL()
    211
    212          current_value = dbcfg.get2 (
    213                  option = option,
    214                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    215                  bias = bias,
    216                  default = default_value
    217          )
    218
    219          if current_value is not None:
    220                  current_value = u'%s' % current_value
    221
    222          if parent is None:
    223                  parent = wx.GetApp().GetTopWindow()
    224
    225          while True:
    226                  dlg = wx.TextEntryDialog (
    227                          parent = parent,
    228                          message = message,
    229                          caption = _('Configuration'),
    230                          defaultValue = gmTools.coalesce(current_value, u''),
    231                          style = wx.OK | wx.CANCEL | wx.CENTRE
    232                  )
    233                  result = dlg.ShowModal()
    234                  if result == wx.ID_CANCEL:
    235                          dlg.Destroy()
    236                          return None
    237
    238                  user_val = dlg.GetValue().strip()
    239                  dlg.Destroy()
    240
    241                  if user_val == current_value:
    242                          return user_val
    243
    244                  validated, user_val = validator(user_val)
    245                  if validated:
    246                          break
    247
    248                  gmDispatcher.send (
    249                          signal = u'statustext',
    250                          msg = _('Value [%s] not valid for option <%s>.') % (user_val, option),
    251                          beep = True
    252                  )
    253
    254          dbcfg = gmCfg.cCfgSQL()
    255          dbcfg.set (
    256                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    257                  option = option,
    258                  value = user_val
    259          )
    260
    261          return user_val
    262  #================================================================
    263 -def configure_boolean_option(parent=None, question=None, option=None, button_tooltips=None):
    264
    265          if parent is None:
    266                  parent = wx.GetApp().GetTopWindow()
    267
    268          tooltips = [
    269                  _('Set "%s" to <True>.') % option,
    270                  _('Set "%s" to <False>.') % option,
    271                  _('Abort the dialog and do not change the current setting.')
    272          ]
    273          if button_tooltips is not None:
    274                  for idx in range(len(button_tooltips)):
    275                          tooltips[idx] = button_tooltips[idx]
    276
    277          dlg = gmGuiHelpers.c3ButtonQuestionDlg (
    278                  parent,
    279                  -1,
    280                  caption = _('Configuration'),
    281                  question = question,
    282                  button_defs = [
    283                          {'label': _('Yes'), 'tooltip': tooltips[0]},
    284                          {'label': _('No'), 'tooltip': tooltips[1]},
    285                          {'label': _('Cancel'), 'tooltip': tooltips[2], 'default': True}
    286                  ]
    287          )
    288
    289          decision = dlg.ShowModal()
    290          dbcfg = gmCfg.cCfgSQL()
    291          if decision == wx.ID_YES:
    292                  dbcfg.set (
    293                          workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    294                          option = option,
    295                          value = True
    296                  )
    297          elif decision == wx.ID_NO:
    298                  dbcfg.set (
    299                          workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    300                          option = option,
    301                          value = False
    302                  )
    303
    304          return
    305  #================================================================
    306  if __name__ == '__main__':
    307
    308          from Gnumed.pycommon import gmI18N
    309          gmI18N.activate_locale()
    310          gmI18N.install_domain()
    311
    312          if (len(sys.argv) > 1):
    313                  if sys.argv[1] == 'test':
    314                          check_for_updates()
    315
    316  #================================================================
    317