Skip to content

Tut_weather_part_4

Sven Roelse edited this page Aug 17, 2019 · 1 revision

Let's make a weather add-on - Part 4

Now for the dialog. I've come up with this:

on *:DIALOG:weather:init:0:{
  ; convert active entries to entries the dialog can work with

  var %anetworks $hget(pnp.weather,anetworks)
  hadd pnp.weather networks %anetworks

  if (%anetworks) {
    ; iterate over networks
    var %n 1
    while ($gettok(%anetworks,%n,44)) {

      ; there's two net specific hashes: -opt and -channels
      ; make sure they are ported from 'anet' to 'net'
      ; data should exist in them already, but cannot hurt to check:
      var %network $v1
      if ($hget(pnp.weather,$+(anet-,%network,-opt))) hadd pnp.weather $+(net-,%network,-opt) $v1
      if ($hget(pnp.weather,$+(anet-,%network,-channels))) hadd pnp.weather $+(net-,%network,-channels) $v1

      ; now let's focus on all the channels for the network, if
      ; they exist!

      ; grab $v1 from previous if-check, handy!
      var %achannels $v1
      if (%achannels) {
        var %c 1
        while ($gettok(%achannels,%c,44)) {
          var %chan $v1
          if ($hget(pnp.weather,$+(achan-,%network,-,%chan))) hadd pnp.weather $+(chan-,%network,-,%chan) $v1
          inc %c
        }
      }
      inc %n
    }
    ; well we have to load ntworks if they exist!
    didtok $dname 2 44 %anetworks
  }
}

alias -l wedloadnet {
  var %network $1
  if (!$hget(pnp.weather,$+(net-,%network,-opt))) hadd pnp.weather $+(net-,%network,-opt) 1,1100010000000001
  tokenize 44 $hget(pnp.weather,$+(net-,%network,-opt))
  if ($1) {
    did -u $dname 5
    did -c $dname 6
    wesetopt $2
  }
  else {
    did -c $dname 5
    did -u $dname 6
  }
  did -r $dname 8
  if ($hget(pnp.weather,$+(net-,%network,-channels))) {
    didtok $dname 8 44 $v1
  }
  did -u $dname 8
}
alias -l wedloadchan {
  var %network $1
  var %chan $2
  if (!$hget(pnp.weather,$+(chan-,%network,-,%chan))) hadd pnp.weather $+(chan-,%network,-,%chan) 1100010000000001
  if ($did(5).state) wesetopt $hget(pnp.weather,$+(chan-,%network,-,%chan))
}
alias -l wedu {
  goto $did(2,0).sel $+ $did(8,0).sel $+ $did(6).state

  :000 | :001 | :010
  ;echo -a 000 001 010 $caller
  did -b $dname 4,5,6,8,9,10,12-29
  did -r $dname 8
  return

  :011
  ;echo -a 011 $caller
  did -r $dname 8
  did -ra $dname 11 Settings
  did -b $dname 5,6,8,9,10,12-29
  did -u $dname 12-29
  return

  :100
  ;echo -a 100 $caller
  did -bu $dname 12-29
  did -e $dname 5,6,8
  did -ra $dname 11 Settings 
  return

  :101
  ;echo -a 101 $caller
  did -e $dname 4,5,6,8,12-29
  did -ra $dname 11 Settings for network: $did(2,0).text
  return

  :110
  ;echo -a 110 $caller
  did -e $dname 8,10,12-29
  did -ra $dname 11 Settings for channel: $replace($did(8,0).text,&,&&) on network: $did(2,0).text
  return

  :111
  ;echo -a 111 $caller
  did -e $dname 10
  did -ra $dname 11 Settings for network: $did(2,0).text
  return
}
on *:DIALOG:weather:sclick:*:{
  if ($did < 1) return

  elseif ($did == 2) {
    if ($did(2,0).sel) wedloadnet $did(2,0).text | wedu
  }
  elseif ($did == 3) {
    _ddadd $dname 2 $did(2,0).text
    hadd pnp.weather networks $addtok($hget(pnp.weather,networks),$did(2,0).text,44)
    did -uf $dname 2
  }
  elseif ($did == 4) {
    hadd pnp.weather networks $remtok($hget(pnp.weather,networks),$did(2,0).text,1,44)
    did -d $dname 2 $did(2,1).sel
    did -r $dname 8
    did -uf $dname 2
    did -b $dname 3,4,5,6,8,9,10,12-29
    did -ra $dname 11 Settings
  }
  elseif ($did == 5) {
    var %do $+(net-,$did(2,0).text,-opt)
    hadd pnp.weather %do $puttok($hget(pnp.weather,$+(net-,$did(2,0).text,-opt)),0,1,44)
    if ($did(8,0).text) wedloadchan $did(2,0).text $did(8,0).text
    wedu 
  }
  elseif ($did == 6) {
    var %do $+(net-,$did(2,0).text,-opt)
    hadd pnp.weather %do $puttok($hget(pnp.weather,$+(net-,$did(2,0).text,-opt)),1,1,44)
    wedloadnet $did(2,0).text
    wedu
  } 
  elseif ($did == 8) {
    if ($did(8,0).sel) wedloadchan $did(2,0).text $did(8,0).text | wedu
  }
  elseif ($did == 9) {
    _ddadd $dname 8 $did(8,0).text
    hadd pnp.weather $+(net-,$did(2,0).text,-channels) $addtok($hget(pnp.weather,$+(net-,$did(2,0).text,-channels)),$did(8,0).text,44)
    did -uf $dname 8
  }
  elseif ($did == 10) {
    hadd pnp.weather $+(net-,$did(2,0).text,-channels) $remtok($hget(pnp.weather,$+(net-,$did(2,0).text,-channels)),$did(8,0).text,1,44)
    hdel pnp.weather $+(chan-,$did(2,0).text,-,$did(8,0).text)
    did -d $dname 8 $did(8,1).sel
    did -uf $dname 8
    if ($did(5).state) did -b $dname 12-29
    else did -e $dname 12-29
    did -b $dname 10
    did -ra $dname 11 Settings    
  }
  elseif ($istok(12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29,$did,32)) {
    echo -a $did
    ;editing for channel? 
    if ($did(5).state) {
      hadd pnp.weather $+(chan-,$did(2,0).text,-,$did(8,0).text) $wegetopt
    }
    ; editing for network?
    elseif ($did(6).state) {
      var %do $+(net-,$did(2,0).text,-opt)
      hadd pnp.weather %do $puttok($hget(pnp.weather,%do),$wegetopt,2,44)
    }
  }
  elseif ($did == 100) {

    ; purging channels that were removed...
    var %rn 1
    while ($gettok($hget(pnp.weather,anetworks),%rn,44)) {
      var %rc 1,%rnetwork $v1
      while ($gettok($hget(pnp.weather,$+(anet-,%rnetwork,-channels)),%rc,44)) {
        var %rchan $v1
        if (!$istok($hget(pnp.weather,$+(net-,%rnetwork,-channels)),%rchan,44)) {
          hdel pnp.weather $+(achan-,%rnetwork,-,%rchan))
        }
        inc %rc
      }
      inc %rn
    }    

    var %networks $hget(pnp.weather,networks)
    hadd pnp.weather anetworks %networks
    if (%networks) {
      ; iterate over networks
      var %n 1
      while ($gettok(%networks,%n,44)) {

        ; there's two net specific hashes: -opt and -channels
        ; make sure they are ported from 'net' to 'anet'

        var %network $v1
        hadd pnp.weather $+(anet-,%network,-opt) $hget(pnp.weather,$+(net-,%network,-opt))
        hadd pnp.weather $+(anet-,%network,-channels) $hget(pnp.weather,$+(net-,%network,-channels))

        ; now let's focus on all the channels for the network, if
        ; they exist!

        ; grab $v1 from previous if-check, handy!
        var %channels $hget(pnp.weather,$+(net-,%network,-channels))
        if (%channels) {
          var %c 1
          while ($gettok(%channels,%c,44)) {
            var %chan $v1
            if ($hget(pnp.weather,$+(chan-,%network,-,%chan))) hadd pnp.weather $+(achan-,%network,-,%chan) $v1
            inc %c
          }
        }        
        inc %n
      }
    }
    ; throw away dialog data
    hdel -w pnp.weather net-*
    hdel -w pnp.weather chan-*
    hdel pnp.weather networks
  }
  elseif ($did == 101) {
    ; throw away changes
    hdel -w pnp.weather net-*
    hdel -w pnp.weather chan-*
    hdel pnp.weather networks
  }
}
on *:DIALOG:weather:edit:2:{
  if ($did(2,0).text == $null) {
    did -b $dname 3
  }
  elseif ($_scandid($dname,2,$did(2,0).text)) {
    did -c $dname 2 $v1
    did -b $dname 3
    wedloadnet $did(2,0).text
  }
  else {
    did -e $dname 3
    did -b $dname 4,8
  }
  wedu
}
on *:DIALOG:weather:edit:8:{
  if ($did(8,0).text == $null) {
    did -b $dname 9 
    did -ra $dname 11 Settings
  }
  elseif ($_scandid($dname,8,$did(8,0).text)) {
    did -c $dname 8 $v1
    did -b $dname 9
    wedloadchan $did(8,0).text
    wedu
  }
  else {
    did -e $dname 9
    did -b $dname 10
  }
  wedu
}

alias -l wesetopt {
  if ($left($1,1)) did -c $dname 12
  else did -u $dname 12
  if ($mid($1,2,1)) did -c $dname 13
  else did -u $dname 13
  if ($mid($1,3,1)) did -c $dname 14
  else did -u $dname 14
  if ($mid($1,4,1)) did -c $dname 15
  else did -u $dname 15
  if ($mid($1,5,1)) did -c $dname 16
  else did -u $dname 16
  if ($mid($1,6,1)) did -c $dname 17
  else did -u $dname 17
  if ($mid($1,7,1)) did -c $dname 18
  else did -u $dname 18
  if ($mid($1,8,1)) did -c $dname 19
  else did -u $dname 19
  if ($mid($1,9,1)) did -c $dname 20
  else did -u $dname 20
  if ($mid($1,10,1)) did -c $dname 21
  else did -u $dname 21
  if ($mid($1,11,1)) did -c $dname 22
  else did -u $dname 22
  if ($mid($1,12,1)) did -c $dname 23
  else did -u $dname 23
  if ($mid($1,13,1)) did -c $dname 24
  else did -u $dname 24
  if ($mid($1,14,1)) did -c $dname 25
  else did -u $dname 25
  if ($mid($1,15,1)) did -c $dname 26
  else did -u $dname 26
  if ($right($1,1) == 1) { 
    did -u $dname 27
    did -c $dname 28
    did -u $dname 29 
  }
  elseif ($right($1,1) == 2) {
    did -u $dname 27
    did -u $dname 28
    did -c $dname 29
  }
  else { 
    did -c $dname 27
    did -u $dname 28
    did -u $dname 29
  }
}
alias -l wegetopt {
  var %opt,%i 12
  while (%i <= 26) {
    %opt = %opt $+ $did(%i).state 
    inc %i
  }
  if ($did(27).state) %opt = %opt $+ 0
  elseif ($did(28).state) %opt = %opt $+ 1
  elseif ($did(29).state) %opt = %opt $+ 2
  else %opt = %opt $+ 1
  return %opt 
}