#!/usr/bin/ruby
# encoding: utf-8

class TileHelpers
	def TileHelpers.place_back(panel, layers, connect=true)
		text = Gtk::Label.new
		text.width_request = 250
		text.wrap = true
		text.set_markup("<span color='white'>Klicken Sie hier, um abzubrechen und zum Startbildschirm zurückzukehren.</span>")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		panel.put(back, 852, 332)
		panel.put(text, 605, 338)
		if connect == true
			back.signal_connect('button-release-event') { |x, y|
				if y.button == 1 
					layers.each { |k,v|v.hide_all }
					layers["start"].show_all
				end
			}
		end
		return back
	end
	
	def TileHelpers.create_label(text, width)
		label = Gtk::Label.new
		label.width_request = width
		label.wrap = true
		label.set_markup("<span color='white'>" + text + "</span>")
		return label
	end
	
	def TileHelpers.error_dialog(message, title="Nachricht")
		TileHelpers.success_dialog(message, title)
	end
	
	def TileHelpers.success_dialog(message, title="Nachricht")
		dialog = Gtk::Dialog.new(title,
                             $main_application_window,
                             Gtk::Dialog::MODAL,
                             [ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_NONE ] )
		dialog.has_separator = false
		label = Gtk::Label.new(message)
		label.wrap = true
		image = Gtk::Image.new(Gtk::Stock::DIALOG_INFO, Gtk::IconSize::DIALOG)
		hbox = Gtk::HBox.new(false, 5)
		hbox.border_width = 10
		hbox.pack_start_defaults(image);
		hbox.pack_start_defaults(label);
			     
		dialog.vbox.add(hbox)
		dialog.show_all
		dialog.run do |response|
			# finito
		end
		dialog.destroy
	end
	
	def TileHelpers.open_pdf(pdfname)
		system("evince '" + pdfname + "' & ")
	end
	
	def TileHelpers.umount_all
		drives = Array.new
		Dir.entries("/sys/block").each { |l|
			drives.push(MfsDiskDrive.new(l, false)) if l =~ /[a-z]$/ 
		}
		IO.popen("losetup -a") { |l|
			while l.gets
				ltoks = $_.strip.split
				system("losetup -d " + ltoks[0].gsub(":", "")) unless ltoks[2] =~ /^\(\/lesslinux\//	
			end
		}
		drives.each { |d|
			d.partitions.each { |p|
				mnt = p.mount_point
				unless mnt.nil?
					p.umount if mnt[0].to_s =~ /^\/media\// || mnt[0].to_s =~ /^\/cobi\//
				end
			}
		}
		drives.each { |d|
			d.partitions.each { |p|
				mnt = p.mount_point
				unless mnt.nil?
					p.force_umount if mnt[0].to_s =~ /^\/media\// || mnt[0].to_s =~ /^\/cobi\// 
				end
			}
		}
		[ "/cobi", "/media" ].each { |d|
			Dir.entries(d).each { |f|
				if File.directory?(d + "/" + f) && !f.eql?(".") && !f.eql?("..")
					begin
						Dir.unlink(d + "/" + f)
					rescue
					end
				end
			}
		}
	end
	
	def TileHelpers.open_browser(url)
		system("su surfer -c 'firefox #{url}' &") 
	end
	
	def TileHelpers.yes_no_dialog(message, title="Frage")
		moveon = false
		dialog = Gtk::Dialog.new(
			title,
			$main_application_window,
			Gtk::Dialog::MODAL,
			[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK ],
			[ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ]	
		)
		dialog.default_response = Gtk::Dialog::RESPONSE_CANCEL
		dialog.has_separator = false
		label = Gtk::Label.new
		label.wrap = true
		label.set_markup(message)
		image = Gtk::Image.new(Gtk::Stock::DIALOG_INFO, Gtk::IconSize::DIALOG)
		hbox = Gtk::HBox.new(false, 5)
		hbox.border_width = 10
		hbox.pack_start_defaults(image);
		hbox.pack_start_defaults(label);
		dialog.vbox.add(hbox)
		dialog.show_all
		dialog.run { |resp|
			if resp == Gtk::Dialog::RESPONSE_OK
				moveon = true
			end
			dialog.destroy
		}
		return moveon	
	end
	
	def TileHelpers.progress(pgbar, command, text)
		puts "Running " + command[0] + " : " + command.join(" ")  
		pgbar.text = text 
		vte = Vte::Terminal.new
		running = true
		vte.signal_connect("child_exited") { running = false }
		vte.fork_command(command[0], command)
		cycle = 0
		while running == true
			pgbar.pulse
			while (Gtk.events_pending?)
				Gtk.main_iteration
			end
			if cycle % 10 == 0
				t = vte.get_text(false)
				puts t 
			end
			sleep 0.2 
			cycle += 1 
		end
	end	
end







