Skip to content

Identity toggle with BLF#

Example: Change the current default identity for making calls by toggling to the next active identity on key press. Optionally change all BLF keys to use the newly selected default identity.

We've introduced the translation-texts for this function (@string/id_chooser_fkt and @string/adapt_speed_dials) with 2.8A. This example should also work on older versions, they'll just not translate these texts. Can be fixed by replacing them ("@string/..") with the desired texts directly within the xml below.

  • led green when a default identity is selected. Key shows its name in second (info) line

  • led off while switching

  • led orange when do default identity is selected

identity_toggle.xml
<templates version="42.47.11">
<template prio="10" name="@string/id_chooser_fkt" icon="@drawable/line">
    <keyConfiguration>
      <lua>
        <code><![CDATA[
local gFctName = "identityChooser"
local gCurrentTimerId = 42 -- use ID to discard obsoleted timers when they fire
local gInLimbo = false -- true while waiting for switchBlfKeys-timer to fire
local gCurrentId = 0 -- currently showing identity (1..6)
local gAdaptSpeedDials = pAdaptSpeedDials=="true" -- wether or not to change lin/id in every BLF-key when identity changes

local function getActiveLine()
  local activeLine = config.get("telephony/activeLine/id")
  if activeLine and #activeLine > 0 then
    return tonumber(activeLine)
  end
  return 0
end

local function updateKey()
  local activeLine = getActiveLine()
  local idSettings = "/identities/identity["..tostring(activeLine).."]/"
  if activeLine > 0 then
    gCurrentId = activeLine
    local name = config.get(idSettings.."displayname")
    if name == nil or #name == 0 then
      name = config.get(idSettings.."username")
    end
    --name = "id "..activeLine..": "..name
    key:setInfo(name)
    if gInLimbo then
      debug.log(gFctName..": updateKey "..name.." off", "d")
      key:setLed("off")
    else
      debug.log(gFctName..": updateKey "..name.." green", "d")
      key:setLed("green")
    end
  else
    debug.log(gFctName..": updateKey to orange, no active line", "d")
    gCurrentId = 0
    key:setInfo("")
    key:setLed("orange")
  end
end

function getIdParam(keySetting)
  -- test 2nd param first, cause that's usually the one with the identity
  for _, idx in pairs({2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}) do
    local paramSetting = keySetting.."parameters/parameter["..tostring(idx).."]/"
    local paramName = config.get(paramSetting.."@name")
    if paramName == "@string/identity" then
      return "parameters/parameter["..tostring(idx).."]/@value"
    end
  end
  return nil
end

function switchBlfKeys(newLine)
  key:setLed("green", true)
  gInLimbo = false
  for _, module in pairs({"phoneXtensions", "extensionModule1", "extensionModule2", "extensionModule3"}) do
    for i=1,60 do
      local keySetting = "/keys/"..module.."/key["..tostring(i).."]/"
      local template = config.get(keySetting.."templateName")
      if template == "@string/speed_dial_title" then
        debug.log(gFctName..": changing key "..keySetting, "d")
        local valPath = getIdParam(keySetting)
        if valPath then
          local keyNr = config.get(keySetting.."@keyNumber")
          valPath = "/keys/"..module.."/key[@keyNumber='"..keyNr.."']/"..valPath
          debug.log(gFctName..": changing "..valPath, "d")
          config.set(valPath, tostring(newLine))
        end
      end
    end
  end
  updateKey()
end

local function newSwitchBlfKeysTimerFkt(myId, newLine)
    timer = function ()
        if myId == gCurrentTimerId then
            switchBlfKeys(newLine)
        end
    end
    return timer
end

function onKeyUp()
  debug.log(gFctName.."-key pressed", "d")
  gInLimbo = gAdaptSpeedDials
  local activeLine = getActiveLine()
  for i=1,6 do
    local nextIdx = activeLine + i
    if nextIdx > 6 then nextIdx = nextIdx - 6 end
    local idSettings = "/identities/identity["..tostring(nextIdx).."]/"
    local active = config.get(idSettings.."active")
    local user = config.get(idSettings.."username")
    local host = config.get(idSettings.."registrar")
    if active == "true" and user and #user > 0 and host and #host > 0 then
      debug.log(gFctName..": switching to identity "..tostring(nextIdx), "d")
      config.set("telephony/activeLine/id", tostring(nextIdx))
      updateKey()
      if gAdaptSpeedDials then
        -- wait a bit to reduce load: only when toggeling stops -> adapt BLF-keys
        gCurrentTimerId = gCurrentTimerId + 1
        time.callbackIn(newSwitchBlfKeysTimerFkt(gCurrentTimerId, nextIdx), 2)
      end
      return
    end
  end
end

local function dfltIdentityChanged(path)
  local activeLine = getActiveLine()
  if gCurrentId == activeLine then
    -- no news -> ignore
    return
  end
  debug.log(gFctName.." on identity changed to "..tostring(activeLine), "d")
  gInLimbo = gAdaptSpeedDials
  updateKey()
  if gAdaptSpeedDials then
    switchBlfKeys(activeLine)
  end
end

function initialize()
  if key and key.INDEX and key.MODULE then
    gFctName = "identityChooser["..key.INDEX.."@"..key.MODULE.."]"
  else
    gFctName = "identityChooser"
  end
  config.register("telephony/activeLine/id", dfltIdentityChanged)
  dfltIdentityChanged(nil)
end

initialize()]]></code>
        <params>
          <param name="pAdaptSpeedDials"><value>true</value></param>
        </params>
      </lua>
    </keyConfiguration>
    <parameters>
      <parameter name="@string/adapt_speed_dials" type="boolean">
        <path>//param[@name="pAdaptSpeedDials"]/value</path>
      </parameter>
    </parameters>
  </template>
</templates>