Class: RWDialog

Inherits:
Object show all
Defined in:
lib/rwd/rwd.rb

Direct Known Subclasses

RWDLogin, RWDTimeOut

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ RWDialog

Returns a new instance of RWDialog.



793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
# File 'lib/rwd/rwd.rb', line 793

def initialize(xml)
  @rwd_xml			=  $rwd_gui_tinker
  # change from default ev - was xml
  @rwd_exitbrowser		= false
  @rwd_history		= []
  @rwd_ignore_vars		= []
  @rwd_call_after_back	= []
  @rwd_time			= Time.now

  $rwd_appvars	= {}	if $rwd_appvars.nil?
  XML.new(xml).parse(OpenTag, "application") do |type, obj|
    obj.args.deep_dup.each do |k, v|
      $rwd_appvars[k]	= v
    end
  end
end

Class Method Details

.file(rwdfile, *args) ⇒ Object



810
811
812
# File 'lib/rwd/rwd.rb', line 810

def self.file(rwdfile, *args)
  new(File.new(rwdfile).readlines, *args)
end

Instance Method Details

#download(data, filename = "") ⇒ Object



1090
1091
1092
1093
# File 'lib/rwd/rwd.rb', line 1090

def download(data, filename="")
  @rwd_download	= data
  @rwd_download_file	= filename
end

#error(msg, &block) ⇒ Object



1048
1049
1050
1051
# File 'lib/rwd/rwd.rb', line 1048

def error(msg, &block)
  @rwd_msg		= msg
  @rwd_msgtype	= "error"
end

#exitbrowserObject



1095
1096
1097
# File 'lib/rwd/rwd.rb', line 1095

def exitbrowser
  @rwd_exitbrowser	= true
end

#message(msg, &block) ⇒ Object



1043
1044
1045
1046
# File 'lib/rwd/rwd.rb', line 1043

def message(msg, &block)
  @rwd_msg		= msg
  @rwd_msgtype	= "message"
end

#progressbar(refresh, *progress) ⇒ Object



1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/rwd/rwd.rb', line 1058

def progressbar(refresh, *progress)
  @rwd_progress_refresh	= (refresh or 1)
  @rwd_progress_progress	= []

  while not progress.empty?
    s	= (progress.shift or "")
    if s.kind_of?(Array)
      caption, value	= s
      value	= (value or 0.0).to_f
    else
      caption	= s
      value	= (progress.shift or 0.0).to_f
    end

    @rwd_progress_progress << [caption, value]
  end

  if @rwd_progress_thread.nil?
    @rwd_progress_progress.each do |a|
      a[1]	= 0.0
    end

    @rwd_progress_thread =
    Thread.new do
      yield
    end
    Thread.pass
  end

  @rwd_progress_thread	= nil	unless @rwd_progress_thread.alive?
end

#render(res, path, post, download, downloadfile, pda, sessionid) ⇒ Object



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/rwd/rwd.rb', line 837

def render(res, path, post, download, downloadfile, pda, sessionid)
# Avoid a timeout.

  @rwd_time		= Time.now

# Initialize some vars.

  vars		= Hash.new	if vars.nil?
  @rwd_switches	= Hash.new	if @rwd_switches.nil?

  done		= false
  help		= false
  back		= false
  tab			= ""
  @rwd_progress_thread = nil
  @rwd_msgtype	= nil	if @rwd_progress_thread.nil?
  @rwd_download	= nil
  @rwd_download_file	= nil

# Switches are used for checkboxes.

  @rwd_switches.each do |key, value|
    vars[key]	= "off"
  end

  @rwd_switches	= Hash.new

# Copy the vars from the window to vars. vars wil later on be copied to instance variables.

  post.sort.each do |key, value|
    puts  "Post: #{key} -> #{value.from_html.inspect}"	if $rwd_debug
    $rwdtinkerlog.info "Post: #{key} -> #{value.from_html.inspect}\n"

    vars[key]	= value.from_html
    $rwdtinkerlog.info  "rwd: #{value.from_html}\n"
  
  end

# Stack handling for rwd_action, rwd_window and rwd_tab.

  @rwd_action			= vars["rwd_action"]		if vars.include?("rwd_action")
  @rwd_action, @rwd_args	= @rwd_action.split(/\//, 2)	unless @rwd_action.nil?
  @rwd_action, rest		= @rwd_action.split(/\?/)	unless @rwd_action.nil?

  @rwd_refresh_action		= (@rwd_action or @rwd_refresh_action or "main")

  unless rest.nil?
    rest.each do |s|
      k, v	= s.split(/=/, 2)
      vars[k]	= v
    end
  end

  if @rwd_action == "rwd_cancel"
    @rwd_progress_thread.kill		unless @rwd_progress_thread.nil?
    @rwd_progress_thread	= nil
    @rwd_action		= "rwd_back"
  end

  @rwd_history	= [["main", [], "main", ""]]	if @rwd_history.empty?

  if @rwd_action =~ /^rwd_tab_/
    @rwd_tab	= @rwd_action.sub(/^rwd_tab_/, "")
    @rwd_history[-1][3]	= @rwd_tab
  else
    case @rwd_action
    when "rwd_back"
      @rwd_history.pop
      @rwd_action			= (@rwd_history[-1] or [nil, nil, nil])[0]
      @rwd_args			= (@rwd_history[-1] or [nil, nil, nil])[1]
      @rwd_window			= (@rwd_history[-1] or [nil, nil, nil])[2]
      @rwd_tab			= (@rwd_history[-1] or [nil, nil, nil])[3]
      back				= true
    when "rwd_help"
      help				= true
    when "rwd_main"
      @rwd_action			= nil
      @rwd_window			= nil
      @rwd_tab			= nil
      @rwd_history			= []
    when "rwd_quit"
      done				= true
    else
    end

# History stuff

    @rwd_history	= @rwd_history[-100..-1]	if @rwd_history.length >= 100
    @rwd_action	= "main"			if @rwd_action.nil?
    @rwd_action	= "main"			if @rwd_action.empty?
    @rwd_window	= "main"			if @rwd_window.nil?
    @rwd_window	= "main"			if @rwd_window.empty?
    @rwd_tab		= ""				if @rwd_tab.nil?
    @rwd_args		= []				if @rwd_args.nil?
    @rwd_args		= []				if @rwd_action == "main"

    vars["rwd_action"]	= @rwd_action
    vars["rwd_window"]	= @rwd_window
    vars["rwd_tab"]		= @rwd_tab

# Copy vars from window to instance.

    vars.each do |k, v|
     $rwdtinkerlog.info "rwd: Post: #{k} -> #{v}\n"

     		if ((not k.empty?) and k.scan(/^rwd_/).empty? and not @rwd_ignore_vars.include?("@#{k}"))
	
	 instance_eval "@#{k}	= ''"
	instance_variable_set("@#{k}", vars.fetch("#{k}"))   
	$rwdtinkerlog.info "rwd: Post: #{k} -> #{v}\n"

end	 
    end

# Callback.

    if (not back) or @rwd_call_after_back.include?(@rwd_action)
      unless @rwd_action =~ /^rwd_/
	
	$rwdtinkerlog.debug  "Method: #{@rwd_action}(#{@rwd_args})"	
     
        if methods.include?(@rwd_action)
          method(@rwd_action).call(*@rwd_args)
        else
          puts "Method '%s' is not defined." % @rwd_action
        end
      end

# History stuff

      @rwd_history	= [["main", [], "main", ""]]	if @rwd_action == "main"
      @rwd_history	= [["main", [], "main", ""]]	if @rwd_history.empty?

      a		= [@rwd_action, @rwd_args, @rwd_window, @rwd_tab]

      @rwd_history.push a				if (@rwd_history[-1] != a or not @rwd_msgtype.nil?)

      if @rwd_window == "rwd_back"
        @rwd_history.pop
        @rwd_history.pop
        @rwd_action			= (@rwd_history[-1] or [nil, nil, nil])[0]
        @rwd_args			= (@rwd_history[-1] or [nil, nil, nil])[1]
        @rwd_window			= (@rwd_history[-1] or [nil, nil, nil])[2]
        @rwd_tab			= (@rwd_history[-1] or [nil, nil, nil])[3]
      end
    end
  end

# Copy vars from instance to window.

  instance_variables.each do |k|
    k.sub!(/^@/, "")
    instance_eval "vars['#{k}'] = @#{k}.to_s"	if (k.scan(/^rwd_/).empty? and not @rwd_ignore_vars.include?("@#{k}"))
  end

# just ignore.

  vars.sort.each do |key, value|
    puts  "Pre: #{key} -> #{value.inspect}"	if $rwd_debug
  end


# Answer to browser.

  if done
    res << RWDDone.new(@rwd_exitbrowser).render(pda)
  else
    if not @rwd_download.nil?
      puts "Download: #{@rwd_window}"		if $rwd_debug

      download	<< @rwd_download
      downloadfile	<< @rwd_download_file
    else
      if not @rwd_progress_thread.nil?
        res << RWDProgressBar.new(@rwd_progress_refresh, @rwd_progress_progress).render(pda, @rwd_refresh_action)
      else
        if not @rwd_msgtype.nil?
          res << RWDMessage.new(@rwd_msg).render(pda)	if @rwd_msgtype == "message"
          res << RWDError.new(@rwd_msg).render(pda)	if @rwd_msgtype == "error"
          res << @rwd_msg				if @rwd_msgtype == "text"
        else
         puts   "Window: #{@rwd_window}"		if $rwd_debug
          puts  "Tab: #{@rwd_tab}"		if $rwd_debug

begin # error block
    if @rwd_window.empty? 
     @rwd_window = "main"
     end
          res << RWDWindow.new($rwd_gui_tinker, @rwd_window).render(pda, @rwd_refresh_action, vars, @rwd_switches, help, @rwd_tab)
rescue

$rwdtinkerlog.error "rwd: error in render"

 end
end
      end
    end
  end

  return done
end

#samewindow?Boolean

Returns:

  • (Boolean)


1039
1040
1041
# File 'lib/rwd/rwd.rb', line 1039

def samewindow?
  @rwd_history[-1][2] == @rwd_window
end

#serve(port = nil, auth = nil, realm = self.class.to_s) ⇒ Object



814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# File 'lib/rwd/rwd.rb', line 814

def serve(port=nil, auth=nil, realm=self.class.to_s)
  exit	if $rwd_exit

  raise "RWD is not initialized."	if @rwd_xml.nil?

  low, high	= ENV["RWDPORTS"].split(/[^\d+]/)
  high	= low	if high.nil?
  low, high	= low.to_i, high.to_i

  io		= nil

  port, io	= TCPServer.freeport(low, high, (not auth.nil?))	if port.nil?
  raise "No free TCP port."						if port.nil?

  port	= port.to_i

  @rwd_server	= RWDServer.new(self, port, io, auth, realm)

  self
end

#text(msg) ⇒ Object



1053
1054
1055
1056
# File 'lib/rwd/rwd.rb', line 1053

def text(msg)
  @rwd_msg		= "<html><body><pre>#{msg}</pre></body></html>"
  @rwd_msgtype	= "text"
end

#timeout(timeout, interval = 1) ⇒ Object



1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/rwd/rwd.rb', line 1099

def timeout(timeout, interval=1)
  @rwd_timeout	= timeout

  unless @rwd_timeout_thread
    @rwd_timeout_thread =
    Thread.new do
      loop do
        if Time.now - @rwd_time > @rwd_timeout
  
  $rwdtinkerlog.info "rwd: Exiting due to timeout (#{@rwd_timeout} seconds)."
          
          exit 1
        end
        sleep interval
      end
    end
  end
end