Class: TreeNodeWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/gui/treenode_window.rb

Instance Method Summary collapse

Constructor Details

#initializeTreeNodeWindow

main



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/gui/treenode_window.rb', line 219

def initialize()

  @re=Regexp.new('^([\d.]+) \((\d+)\)$')
  @column_title_expand="Expand all networks"
  @column_title_collapse="Collapse all networks"
  @node_selected=nil

  @treenode = Gtk::Window.new
  @treenode.set_size_request(200,200)
  @treenode.set_title "Tree node"
  @treenode.set_icon("#{PIXMAP_PATH}/logo_icon.png")
  @tree_store=Gtk::TreeStore.new(Gdk::Color,String)

  vbox=Gtk::VBox.new
  vbox.show
  @treenode.add vbox

  scrolled_window = Gtk::ScrolledWindow.new(nil, nil)
  scrolled_window.border_width=2
  scrolled_window.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC)  

  vbox.pack_start(scrolled_window, true, true, 0)
  scrolled_window.show

  tv = Gtk::TreeView.new(@tree_store)
  renderer = Gtk::CellRendererText.new
  column = Gtk::TreeViewColumn.new(@column_title_expand, renderer, {
                                		 :background_gdk => 0,
                                		 :text => 1
                                	  })
	tv.set_headers_clickable(true)
	tv.set_headers_visible(true)
   column.set_clickable(true)
	column.set_alignment(0.5)

#collapse/expand button click events
   column.signal_connect("clicked") {
		if column.title == @column_title_expand
			column.title=@column_title_collapse
			tv.expand_all
		else
			column.title=@column_title_expand
			tv.collapse_all
		end			
	}
  tv.append_column(column)
  tv.show
  scrolled_window.add(tv)

#select a node when a row is selected
tv.signal_connect("row-activated") {|trv,path|
#1 is the second column
value=trv.model.get_value(trv.model.get_iter(path),1)
if value and value.match(/\s\(\d+/)
	#if it's a network
	network_value=value.gsub(/\s\(.+$/,'')
	if $network[network_value]
		set_map(network_value)
	end
else
	if $host[value]
		#change the map if it's needed
		set_map($host[value].map)	
		if @node_selected != nil
			@node_selected.node_view.unselect()
		end
		@node_selected=$host[value]
		$host[value].node_view.select()
	end
end
}

#delete node when delete key is pressed
tv.signal_connect("key_press_event") {|trv,e|
	if e.keyval==Gdk::Keyval::GDK_Delete
		path,focus_col=trv.cursor()
		iter=trv.model.get_iter(path)

		value=trv.model.get_value(iter,1)
		#check if value of the cell contains a () so it's a network
		if value and !value.match(/\s\(\d+/)
			verify_delnodewin($host[value])
		else
                               if value
			   network_val=value.gsub(/\s\(.+$/,'')
			   if (get_map() != network_val) && !$network[network_val].contain_network(get_map())
				   verify_delnodewin($network[network_val])
			   end
			end
		end
	end
}
  @treenode.signal_connect("delete_event") do
		@treenode.hide
  end
  @treenode.signal_connect("key_press_event") {|w,e|
	if e.keyval==Gdk::Keyval::GDK_Escape
     	@treenode.hide()
  	end
  }
end

Instance Method Details

#add_row(node) ⇒ Object

add a row in the tree when a new node is found



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gui/treenode_window.rb', line 74

def add_row(node)
	if node
		#test if node is already present
		iter=search_node(node)
		if !iter
			#find where to insert it
			if node.map == ROOTMAPADDR
				iter_map=0
				endloop=true
			else
				iter_map=search_node($network[node.map])
			end

			root_iter = @tree_store.append(@tree_store.get_iter("#{iter_map}"))
 	   			hexa_rgb=$status_color[$status_value.index(node.status)]
			root_iter[0] = hexa_to_rgb_color(hexa_rgb)
			if node.class == Host
				root_iter[1] = node.ip
			else
				root_iter[1] = "#{node.ip} (0)"
			end
			if node && node.class == Host
				#search for network map

				endloop=false
				begin
					if node.map == ROOTMAPADDR
						iter_map=0
						endloop=true
					else
						iter_map=search_node($network[node.map])
					end

					if iter_map != nil
						#adjust network map nb hosts
						str_tmp=@tree_store.get_value(@tree_store.get_iter("#{iter_map}"),1)
						if str_tmp
							str_tmp.scan(@re) do |network_ip, nb_host|
								@tree_store.set_value(@tree_store.get_iter("#{iter_map}"),1,"#{network_ip} (#{nb_host.to_i()+1})")
								node=$network[network_ip]
							end
						end
					end
				end while !endloop
			end
		end
	end
end

#clearObject

clear the tree



32
33
34
# File 'lib/gui/treenode_window.rb', line 32

def clear()
	@tree_store.clear()
end

#del_row(node) ⇒ Object

del a row in the tree when a node is removed



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gui/treenode_window.rb', line 39

def del_row(node)
	iter=search_node(node)
	if iter != nil	
		@tree_store.remove(@tree_store.get_iter(iter))
		
		#reduce count value
		if node
			#search for parent iter
		
			endloop=false
			begin
				if node.map == ROOTMAPADDR
					iter=0
					endloop=true
				else
					iter=search_node($network[node.map])
				end
				#adjust network map nb hosts
				str_tmp=@tree_store.get_value(@tree_store.get_iter("#{iter}"),1)
				if str_tmp
					str_tmp.scan(@re) do |network_ip, nb_host|
						nb_host=1 unless nb_host.to_i() > 0
						@tree_store.set_value(@tree_store.get_iter("#{iter}"),1,"#{network_ip} (#{nb_host.to_i()-1})")
						node=$network[network_ip]
					end
				end
			end while !endloop
		end
	end
end

#hideObject

hide the tree



25
26
27
# File 'lib/gui/treenode_window.rb', line 25

def hide()
	@treenode.hide()
end

#reloadObject

reload the whole tree, used when no updates was done for a long time aka tree was hidden



210
211
212
213
214
# File 'lib/gui/treenode_window.rb', line 210

def reload()
  clear()
  node=$network[ROOTMAPADDR]
  reload_sub(nil,node)
end

#reload_sub(tree_root, network_node) ⇒ Object

recursive reload the whole tree



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/gui/treenode_window.rb', line 170

def reload_sub(tree_root,network_node)
   if network_node
	  root_iter = @tree_store.append(tree_root)
	  hexa_rgb=$status_color[$status_value.index(network_node.status)]
	  root_iter[0] = hexa_to_rgb_color(hexa_rgb)

 		  cpt=0
	  
	  network_node.get_node().sort{|ip1,ip2|
		ip1_tmp=fill_ip_str(ip1[0])
		ip2_tmp=fill_ip_str(ip2[0])
		ip1_tmp = "0.0.0.0" if ip1_tmp.nil? or ip1_tmp.empty?
		ip2_tmp = "0.0.0.0" if ip2_tmp.nil? or ip2_tmp.empty?
		ip1_tmp<=>ip2_tmp
	  }.each {|ipt, node|
		 if node.class != Network
			 cpt+=1
			 child_iter = @tree_store.append(root_iter)
 	   			 hexa_rgb=$status_color[$status_value.index(node.status)]
			 child_iter[0] = hexa_to_rgb_color(hexa_rgb)
			 child_iter[1] = node.ip
		 else
			#network is also considered as a node
			cpt+=1+reload_sub(root_iter, node)
		 end
	    		 root_iter[1] = "#{network_node.ip} (#{cpt})"
	  }
	  #special case when there is no node in network
	  if cpt == 0
    		root_iter[1] = "#{network_node.ip} (#{cpt})"
	  end
	  return cpt
         end
  return 0
      
end

#search_node(node) ⇒ Object

return an iter if node is found



126
127
128
129
130
131
# File 'lib/gui/treenode_window.rb', line 126

def search_node(node)
	if node
		search_sub_node(node, 0)
	end

end

#search_sub_node(node, iter) ⇒ Object

recursive search function to find sub node



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/gui/treenode_window.rb', line 136

def search_sub_node(node, iter)
	iter_val=iter		
	iter_pos=0
	while @tree_store.get_iter("#{iter_val}:#{iter_pos}") != nil do
		value=@tree_store.get_value(@tree_store.get_iter("#{iter_val}:#{iter_pos}"),1)
		#print "#{value}\n"
		if value and value.index(node.ip) == 0
			return 	"#{iter_val}:#{iter_pos}"		
		end
		#check if it's a network
		if value and value.match(/\s\(\d+/)
			result=search_sub_node(node, "#{iter_val}:#{iter_pos}")
			if result
				return result
			end
		end
		iter_pos+=1
	end
	return nil
end

#showObject

show the tree



15
16
17
18
19
20
# File 'lib/gui/treenode_window.rb', line 15

def show()
	if !$treenode_win.visible?
 			 reload()
	end
	@treenode.show()
end

#update_row(node) ⇒ Object

update the row severity corresponding to the node



160
161
162
163
164
165
# File 'lib/gui/treenode_window.rb', line 160

def update_row(node)
	iter=search_node(node)
	if iter != nil
		@tree_store.set_value(@tree_store.get_iter("#{iter}"), 0, get_color_from_status($status[$status_value.index(node.status)]))
	end
end

#visible?Boolean

check if the tree is already visible

Returns:

  • (Boolean)


8
9
10
# File 'lib/gui/treenode_window.rb', line 8

def visible?()
	return @treenode.visible?()
end