Gnumed.wxpython.gmCfgWidgets

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

# Source Code for Module Gnumed.wxpython.gmCfgWidgets

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