#!/usr/bin/env python

# gnome-fix-broken-links code file
# $Id $

import sys
import locale
import gobject
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import gnome
import gnomevfs
import gnome.ui
from os import path,symlink,unlink
from threading import Thread
import commands
import re

debug = False

def get_shared_path():
	testfile = 'version'
	sharedirs = [".",path.join(path.dirname(sys.argv[0]),"../share/gnome-fix-broken-links")]
	sharepath = None
	for sharedir in sharedirs:
		fname = path.join(path.abspath(sharedir),testfile)
		if path.exists(fname):
			sharepath = path.abspath(sharedir)
			break
	
	if sharepath is None:
		raise Exception, "Fix broken links shared files " + testfile + " cannot be found in any of " + str(sharedirs) + " default paths"
	
	return sharepath

def get_version():
	f = file(path.join(get_shared_path(),"version"))
	vers = f.readlines()
	f.close()
	return vers[0].strip()

sys.path[0] = get_shared_path()


class GNOMEFixBrokenLinksApp(gtk.glade.XML):
	
	state_searching = 1
	state_searched = 2
	
	def __init__ (self,sharepath):
		self.sharepath = sharepath
		gtk.glade.XML.__init__(self,path.join(self.sharepath,'gnome-fix-broken-links.glade'))
		
		self.setup_ui()
		
	def setup_ui(self):
		
		self.signal_autoconnect(self)
		self.window = self.get_widget("main_window")
		
		self.file_list_view = self.get_widget("file_list")
		
		self.file_list = gtk.ListStore(str,str,str) # pathname, filename, folder
		self.file_list_view.set_model(self.file_list)
		cell_renderer = gtk.CellRendererText()
		c= self.file_list_view.insert_column_with_attributes(-1,"File name",cell_renderer,text=1)
		c.set_sort_indicator(True)
		c.set_sort_column_id(1)
		c.set_resizable(True)

		c= self.file_list_view.insert_column_with_attributes(-1,"In folder",cell_renderer,text=2)
		c.set_sort_indicator(True)
		c.set_sort_column_id(1)
		c.set_resizable(True)

	
	
	def set_uri_list(self,uri_list):
		gobject.idle_add(self._finished_finding_files_delegate,uri_list)
		#this should call the delegate, which should operate in a threadsafe manner by calling idle_Add
	
	def _finished_finding_files_delegate(self,uri_list):
		#this picks up from the app the uri list and removes it from the app
		self.file_list.clear()
		
		for a in uri_list:
			p = a
			row = (p,path.basename(p),path.dirname(p))
			self.file_list.append(row)
		
		self.reflect_state(self.state_searched)

	
	def find_possible_targets(self,pathname):
		
		self.broken_link_path = path.abspath(pathname)
		self.get_widget("broken_link").set_filename(self.broken_link_path)
		
		class MultiBrokenLinkFinder(Thread):
		
			def __init__(self,pathname,results_delegate):
				Thread.__init__(self)
				self.pathname = pathname
				self.delegate = results_delegate
				
			def run(self):
				file_name = path.basename(self.pathname)
				
				matches = re.findall("(\w*)",file_name)
				matches = [ m for m in matches if len(m) > 2 ]
				
				# if there is no query term, we simply return empty
				if not matches:
					print "No possible matches for %r"%file_name
					self.delegate([])
					return
				
				terms = " ".join(matches)
				print "Terms: " + terms
				
				def shell_quote(string):
					return "'%s'" % string.replace("'","'\\''")


				results = [] # we begin with an empty result set
				
				
				# start beagle
				status,output = commands.getstatusoutput("beagle-query %s"%shell_quote(terms))
				
				if status == 0:
					print "Output:"
					print output
					print "--------end output---------"
					
					beagle_results = [ r for r in output.split("\n") if r ]
					for result in beagle_results:
						try: p = gnomevfs.get_local_path_from_uri(result)
						except: continue
						results.append(p) #collect results with file:///* uris
					
					print "Finished processing Beagle results"
				else:
					print "Error running beagle-query status %s output: \n%s"%(status,output)
				#end beagle
				
				# start locate
				regexp = terms.replace(" ",".*")
				status,output = commands.getstatusoutput("locate -i -r %s"%shell_quote(regexp))
				if status == 0:
					print "Output:"
					print output
					print "--------end output---------"
					locate_results = [ r for r in output.split("\n") if r ]
					for result in locate_results:
						if result and result[0] == "/":
							results.append(result) #collect results with /* paths
					print "Finished processing slocate results"
				else:
					print "Error running locate status %s output: \n%s"%(status,output)
				
				self.delegate(results)
		
		self.reflect_state(self.state_searching)
		
		t = MultiBrokenLinkFinder(pathname,self.set_uri_list)
		t.start()
		
	def reflect_state(self,state):
		
		if state == self.state_searching:
			self.get_widget("searching_files_alignment").show()
# 			self.get_widget("searching_files_alignment").set_text("Searching files with names similar to %r..."%path.basename(self.broken_link_path))
			self.get_widget("done_searching_matches").hide()
			self.get_widget("done_searching_nomatches").hide()
			self.get_widget("file_list_frame").set_sensitive(False)
			self.get_widget("apply").set_sensitive(False)
		elif state == self.state_searched:
			self.get_widget("searching_files_alignment").hide()
			if len(self.file_list):
				self.get_widget("done_searching_matches").show()
				self.get_widget("done_searching_nomatches").hide()
				self.get_widget("file_list_frame").set_sensitive(True)
				self.get_widget("apply").set_sensitive(True)
				self.get_widget("file_list").grab_focus()
			else:
				self.get_widget("done_searching_nomatches").show()
				self.get_widget("done_searching_matches").hide()
				self.get_widget("file_list_frame").set_sensitive(False)
				self.get_widget("apply").set_sensitive(False)
		else:
			raise ProgrammingError, "Cannot reach here"
		
	def on_cancel_clicked (self,w,extra=None):
		self.close_app()
		
	def on_apply_clicked(self,w,extra=None):
		
		selection = self.file_list_view.get_selection()
	
	
		model,i = selection.get_selected()
		try: target_path = model.get_value(i,0)
		except: return
		link_path = self.broken_link_path
			
			
		self.fix_link(link_path,target_path)
		self.close_app()
		
		
	def fix_link(self,link_path,target_path):
		
		print "Linking %s to %s"%(link_path,target_path)
		
		unlink(link_path)
		symlink(target_path,link_path)
		
		
		
	def on_main_window_delete_event(self,w,extra=None):
		self.close_app()
		return True
		
	def close_app(self):
		gtk.main_quit()

# 	def toggle_main_window(self,caller=None,data=None):
# 		if self.window.get_property("visible") == False:
# 			self.window.present()
# 			self.window.grab_focus()
# 		else:
# 			self.window.hide()
# 			if not self.config["background_monitoring_shown"] and self.ups:
# 				self.show_info_dialog("UPS monitor will continue monitoring your UPS in the background.  You can access the UPS monitor window by clicking the UPS monitor status icon  in the notification area.","UPS monitor keeps monitoring your UPS")
# 				self.config["background_monitoring_shown"] = True
# 		return True
# 
# 	def main_window_closed(self,caller=None,data=None):
# 		if self.ups is None:
# 			self.quit()
# 		return self.toggle_main_window()
# 
# 
# 	def save_session(self):
# 		self.smclient.request_save(gnome.ui.SAVE_BOTH,False,gnome.ui.INTERACT_NONE,False,True)
# #		self.smclient.flush()
# 	
# 	def connect_signals(self):
# 		self.signal_autoconnect(self)
# 
# 
# 	def prepare_widgets(self):
# 
# 		self.get_widget("window").set_icon_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 		self.get_widget("prefs_window").set_icon_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 		self.get_widget("info_dialog").set_icon_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 		self.get_widget("warning_dialog").set_icon_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 		self.get_widget("error_dialog").set_icon_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 		self.get_widget("about_dialog").set_icon_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 
# 
# 		self.get_widget("about_image").set_from_file(path.join(self.sharepath,"ups-monitor.png"))
# 		self.get_widget("battery_image").set_from_file(path.join(self.sharepath,"battery-level.png"))
# 		self.get_widget("load_image").set_from_file(path.join(self.sharepath,"load.png"))
# 		self.get_widget("remaining_time_image").set_from_file(path.join(self.sharepath,"remaining-time.png"))
# 		
# 		title = "<span weight=\"bold\"><big><big>UPS monitor</big></big></span>"
# 		version = "<b>version "+get_version()+"</b>"
# 		subtitle = "A graphical application to monitor the status of your UPS"
# 		
# 		self.get_widget("label_about").set_markup(title + "\n" + version + "\n" + subtitle)
# 		
# 		self.get_widget("prefs_window").set_transient_for(self.get_widget("window"))
# 
# 		self.tray_icon = UPSTrayMonitor(self)
# 
# 	def disable_display(self):
# 		self.get_widget("display").set_sensitive(False)
# 	
# 	def enable_display(self):
# 		self.get_widget("display").set_sensitive(True)
# 	
# 	def quit(self,caller=None,extraobject=None):
# 		self.save_config()
# 		gtk.main_quit()
# 
# 	def open_preferences(self,caller):
# 		self.show_prefs_window()
# 	
# 	def update_statusbar(self,message,ok=True):
# 		self.get_widget("statusbar").set_markup(message)
# 		if ok:
# 			self.get_widget("status_icon_ok").show()
# 			self.get_widget("status_icon_error").hide()
# 		else:
# 			self.get_widget("status_icon_ok").hide()
# 			self.get_widget("status_icon_error").show()
# 	
# 	def show_dialog(self,dialogtype,message,title=None):
# 		if title:
# 			dialogtitle = title
# 			title = "<span weight='bold' size='larger'>" + title + "</span>\n\n"
# 		else:
# 			dialogtitle = "UPS monitor - error"
# 			title = ""
# 		self.get_widget(dialogtype + "_message").set_markup(title+message)
# 		self.get_widget(dialogtype + "_dialog").set_title(dialogtitle)
# 		self.get_widget(dialogtype + "_dialog").present()
# 
# 	def show_error_dialog(self,message,title=None):
# 		return self.show_dialog("error",message,title)
# 	
# 	def show_info_dialog(self,message,title=None):
# 		return self.show_dialog("info",message,title)
# 	
# 	def show_warning_dialog(self,message,title=None):
# 		return self.show_dialog("warning",message,title)
# 	
# 	def show_about_dialog(self,caller=None):
# 		self.get_widget("about_dialog").present()
# 
# 	def dismiss_error_dialog(self,caller=None,e=None):
# 		self.get_widget("error_dialog").hide()
# 		return True
# 		
# 	def dismiss_about_dialog(self,caller=None,e=None):
# 		self.get_widget("about_dialog").hide()
# 		return True
# 		
# 	def dismiss_info_dialog(self,caller=None,e=None):
# 		self.get_widget("info_dialog").hide()
# 		return True
# 		
# 	def dismiss_warning_dialog(self,caller=None,e=None):
# 		self.get_widget("warning_dialog").hide()
# 		return True
# 		
# 	def hide_prefs_window(self,caller=None,e=None):
# 		self.get_widget("prefs_window").hide()
# 		return True
# 		
# 	def show_prefs_window(self,caller=None,e=None):
# 
# 		self.get_widget("prefs_window").present()
# 		return True
# 		
# 		
# 		
# 	def load_config(self):
# 		self.config = dict()
# 
# 		upsaddress = None
# 		username = None
# 		password = None
# 		visible = True
# 		background_monitoring_shown = False
# 
# 		try:
# 			fn = path.expanduser("~") + "/.ups-monitor.conf"
# 			f = open(fn,"r")
# 			lines = f.readlines()
# 			
# 			for line in lines:
# 				key, value = split(line,"=")
# 				key = strip(key)
# 				value = strip(value)
# 				if key == "address":
# 					upsaddress = value
# 				if key == "username":
# 					username = value
# 				if key == "password":
# 					password = value
# 				if key == "app_visible":
# 					if value == "true":
# 						visible = True
# 					else:
# 						visible = False
# 				if key == "background_monitoring_shown":
# 					if value == "true":
# 						background_monitoring_shown = True
# 					else:
# 						background_monitoring_shown = False
# 			f.close()
# 
# 		except IOError, e:
# 			pass
# 
# 		if upsaddress is not None:
# 			self.config["ups"] = (upsaddress,username,password)
# 
# 		self.config["visible"] = visible
# 		self.config["background_monitoring_shown"] = background_monitoring_shown
# 
# 	
# 	def monitor_last_ups(self):
# 		try:
# 			upsaddress,username,password = self.config["ups"]
# 			self.monitor(upsaddress,username,password)
# 		except Exception, e:
# 			raise NoLastUPSError, "No last UPS to monitor"
# 			
# 	def save_config(self):
# 		debug_print( "Saving config")
# 		fn = path.expanduser("~") + "/.ups-monitor.conf"
# 		umask(077)
# 		f = open(fn,"w")
# 		if self.ups is not None:
# 			debug_print("Saving UPS configuration")
# 			string = "address = " + self.ups.get_address() + "\n"
# 			f.write(string)
# 			if  self.ups.get_username()  is not None:
# 				string = "username = " + self.ups.get_username() + "\n"
# 				f.write(string)
# 			if  self.ups.get_password()  is not None:
# 				string = "password = " + self.ups.get_password() + "\n"
# 				f.write(string)
# 		if self.window.get_property("visible"):
# 			string = "app_visible = true" + "\n"
# 		else:
# 			string = "app_visible = false" + "\n"
# 		f.write(string)
# 		if self.config["background_monitoring_shown"]:
# 			string = "background_monitoring_shown = true" + "\n"
# 		else:
# 			string = "background_monitoring_shown = false" + "\n"
# 		f.write(string)
# 		f.close()
# 	
# 	def monitor(self,ups_address,username,password):
# 		
# 		try:
# 			self.update_statusbar("Connecting to  <b>" + ups_address + "</b>...")
# 			u = UPS(ups_address,username,password)
# 			u.connect()
# 		except InvalidAddressError, e:
# 			self.show_error_dialog("The UPS address <b>" + ups_address + "</b> cannot be contacted or is incorrect.  Please make sure that the server address is correct, the UPS server is working and it has an active network connection.","Cannot monitor " + ups_address)
# 			self.update_statusbar("Cannot monitor <b>" + ups_address + "</b>",False)
# 			return False
# 		except AuthenticationFailedError, e:
# 			self.show_error_dialog("The supplied user name or password for <b>" + ups_address + "</b> is incorrect.  Please make sure you typed the password correctly.  If all else fails, contact your network UPS administrator.", "Cannot monitor " + ups_address)
# 			self.update_statusbar("Cannot monitor <b>" + ups_address + "</b>",False)
# 			return False
# 		except UPSNotFoundError, e:
# 			self.show_error_dialog("UPS <b>" + ups_address + "</b> is not attached to the server.  Please confirm that you entered the correct UPS name." , "Cannot monitor " + ups_address)
# 			self.update_statusbar("Cannot monitor <b>" + ups_address + "</b>",False)
# 			return False
# 		
#  		if self.ups:
#  			self.ups.stop_monitoring()
# 		
# 		self.ups = u
#  		self.update_statusbar("Connected to <b>"  + get_friendly_computer_name(u.get_address()) + "</b>")
# 		self.ups.start_monitoring(self.refresh_display,self.refresh_display)
# 			
# 		return True
# 
# 
# 	def refresh_display(self):
# 		if not self.ups.is_connected() or not self.ups.is_monitoring():
# 			self.show_error_dialog("The network connection to <b>" + get_friendly_computer_name(self.ups.get_host()) +"</b> is lost.  Check for network availability and ensure the UPS server is still online and running.","UPS " + self.ups.get_address() + " is no longer being monitored")
# 			self.update_statusbar("Cannot continue monitoring <b>" + self.ups.get_address() + "</b>",False)
# 			self.disable_display()
# 			self.tray_icon.set_error("The network connection to " + get_friendly_computer_name(self.ups.get_host()) + " is lost.  UPS monitoring stopped.")
# 
# 			self.ups = None
# 			self.timeout_id = None
# 			return False
# 		
# 		u = self.ups
# 		
# 
# 		if u.status == "OK":
# 			self.enable_display()
# 			self.tray_icon.set_error(None)
# 		if u.status == "DRIVER-NOT-CONNECTED":
# 	 		self.update_statusbar(get_friendly_computer_name(u.get_host()).capitalize() + " lost contact with UPS <b>" + u.get_ups_name() + "</b>", False)
# 			self.disable_display()
# 			self.tray_icon.set_error(get_friendly_computer_name(u.get_host()).capitalize() + " lost contact with UPS " + u.get_ups_name() + "")
# 			return True
# 		if u.status == "DATA-STALE":
# 	 		self.update_statusbar(get_friendly_computer_name(u.get_host()).capitalize() + " has stale information about UPS <b>"  + u.get_ups_name()  + "</b>", False)
# 			self.disable_display()
# 			self.tray_icon.set_error(get_friendly_computer_name(u.get_host()).capitalize() + " has stale information about UPS "  + u.get_ups_name()  + "")
# 			return True
# 					
# 		if u.manufacturer:
# 			self.get_widget("label_manufacturer").set_text(u.manufacturer)
# 		else:
# 			self.get_widget("label_manufacturer").set_text("(unknown)")
# 		
# 		if u.model:
# 			self.get_widget("label_model").set_text(u.model)
# 		else:
# 			self.get_widget("label_model").set_text("(unknown)")
# 		
# 		if u.serial:
# 			self.get_widget("label_serial").set_text(u.serial)
# 		else:
# 			self.get_widget("label_serial").set_text("(unknown)")
# 
# 
# 		if u.power_source == "AC":
# 	 		self.update_statusbar("UPS <b>"  + u.get_ups_name()  + "</b> attached to <b>" + get_friendly_computer_name(u.get_host())  + "</b> is <b>on AC power</b>")
# 			self.tray_icon.set_alarm(None)
# 			self.tray_icon.set_power_source("AC")
# 			if self.little_time_left_warning:
# 				self.dismiss_warning_dialog()
# 	 			self.little_time_left_warning = None
# 			if self.session_saved:
# 				self.session_saved = None
# 			self.get_widget("label_power_source").set_text("Wall socket")
# 
# 		elif u.power_source == "BAT":
# 			self.get_widget("label_power_source").set_text("Battery backup")
# 			self.tray_icon.set_power_source("BAT")
# 			if self.session_saved is None:
# 				self.session_saved = True
# 				self.save_session()
# 
# 			if u.remaining_time is not None and u.remaining_time < 60:
# 				self.tray_icon.set_alarm("Less than a minute of battery power remaining")
# 				if self.little_time_left_warning is None:
# 					self.little_time_left_warning = True
# 			 		self.update_statusbar("UPS <b>"  + u.get_ups_name()  + "</b> attached to <b>" + get_friendly_computer_name(u.get_host())  + "</b> is <b>low on battery</b>")
# 					self.show_warning_dialog(get_friendly_computer_name(u.get_host()).capitalize() + " will power off soon to prevent long-term UPS battery damage, unless utility power returns. Save any open documents and close your session.","Less than a minute of battery power remaining")
# 			else:
# 		 		self.update_statusbar("UPS <b>"  + u.get_ups_name()  + "</b> attached to <b>" + get_friendly_computer_name(u.get_host())  + "</b> is <b>on battery backup</b>")
# 				self.tray_icon.set_alarm(None)
# 
# 		else:
# 	 		self.update_statusbar("UPS <b>"  + u.get_ups_name()  + "</b> attached to <b>" + get_friendly_computer_name(u.get_host())  + "</b> is online")
# 			self.get_widget("label_power_source").set_text("(unknown)")
# 			
# 		self.last_power_source = u.power_source
# 		
# 		if u.battery_status == "NORMAL":
# 			self.get_widget("label_battery_status").set_text("OK")
# 		elif u.battery_status == "LOW":
# 			self.get_widget("label_battery_status").set_text("Low")
# 		elif u.battery_status == "REPLACE":
# 			self.get_widget("label_battery_status").set_text("Needs replacement")
# 		else:
# 			self.get_widget("label_battery_status").set_text("(unknown)")
# 
# 		if u.ups_status == "NORMAL":
# 			self.get_widget("label_ups_status").set_text("Normal")
# 		elif u.ups_status == "BYPASS":
# 			self.get_widget("label_ups_status").set_text("Backup unavailable")
# 		elif u.ups_status == "CALIBRATING":
# 			self.get_widget("label_ups_status").set_text("Calibrating")
# 		elif u.ups_status == "OFFLINE":
# 			self.get_widget("label_ups_status").set_text("Offline")
# 		elif u.ups_status == "OVERLOADED":
# 			self.get_widget("label_ups_status").set_text("Overloaded")
# 		elif u.ups_status == "TRIMMING":
# 			self.get_widget("label_ups_status").set_text("Limiting high voltage")
# 		elif u.ups_status == "BOOSTING":
# 			self.get_widget("label_ups_status").set_text("Boosting low voltage")
# 		else:
# 			self.get_widget("label_ups_status").set_text("(unknown)")
# 		
# 		if u.battery_voltage:
# 			self.get_widget("label_battery_voltage").set_text(str(u.battery_voltage) + " V")
# 		else:
# 			self.get_widget("label_battery_voltage").set_text("(unknown)")
# 
# 		if u.battery_charge:
# 			self.get_widget("meter_battery_charge").set_text(str(int(u.battery_charge*100)) + "%")
# 			self.get_widget("meter_battery_charge").set_fraction(u.battery_charge)
# 		else:
# 			self.get_widget("meter_battery_charge").set_text("(unknown)")
# 			self.get_widget("meter_battery_charge").set_fraction(0.0)
# 		
# 		if u.ups_load:
# 			self.get_widget("meter_ups_load").set_text(str(int(u.ups_load*100)) + "%")
# 			if u.ups_load <= 1:
# 				self.get_widget("meter_ups_load").set_fraction(u.ups_load)
# 			else:
# 				self.get_widget("meter_ups_load").set_fraction(1.0)
# 				
# 		else:
# 			self.get_widget("meter_ups_load").set_text("(unknown)")
# 			self.get_widget("meter_ups_load").set_fraction(0.0)
# 		
# 		if u.remaining_time:
# 			self.get_widget("meter_remaining_time").set_text(str(u.remaining_time) + " seconds")
# 			self.get_widget("meter_remaining_time").set_fraction(float(u.remaining_time)/u.max_remaining_time)
# 				
# 		else:
# 			self.get_widget("meter_remaining_time").set_text("(unknown)")
# 			self.get_widget("meter_remaining_time").set_fraction(0.0)
# 		
# 
# 		return True
# 			
# 						
# 	def prepare_prefs_window(self,caller=None):
# 
# 		localupses = None
# 		
# 		combobox = self.get_widget("hbox_local_upses").get_data("combo")
# 		if not combobox:
# 			combobox = gtk.Combo()
# 			combobox.set_sensitive(False)
# 			self.get_widget("hbox_local_upses").pack_end(combobox)
# 			self.get_widget("hbox_local_upses").set_data("combo",combobox)
# 			combobox.show()
# 		
# 		try:
# 			localupses = read_ups_config("/etc/ups/ups.conf").keys()
#  			combobox.set_popdown_strings(localupses)
# 			self.get_widget("radio_local_upses").set_sensitive(True)
# 			combobox.set_sensitive(True)
# 			self.get_widget("radio_local_upses").set_active(True)
# 		except Exception:
# 			pass
# 
# 		if self.ups:
# 			if find(self.ups.get_address(),"@localhost") != -1 and self.get_widget("radio_local_upses").get_property("sensitive"):
# 				addr = self.ups.get_address()[:find(self.ups.get_address(),"@localhost")]
# 				self.get_widget("hbox_local_upses").get_data("combo").get_children()[0].set_text(addr)
# 				self.get_widget("radio_local_upses").set_active(True)
# 			else:
# 				self.get_widget("ups_address").set_text(self.ups.get_address())
# 				self.get_widget("radio_network_upses").set_active(True)
# 
# 			if self.ups.get_username() is not None:
# 				self.get_widget("authenticate").set_active(True)
# 				self.get_widget("username").set_text(self.ups.get_username())
# 			if self.ups.get_password() is not None:
# 				self.get_widget("authenticate").set_active(True)
# 				self.get_widget("password").set_text(self.ups.get_password())
# 
# 		if self.get_widget("radio_local_upses").get_active():
# 			combobox.get_children()[0].grab_focus()
# 		else:
# 			self.get_widget("ups_address").grab_focus()
# 								
# 	def update_auth_container_status(self,caller):
# 		if self.get_widget("authenticate").get_active():
# 			for a in ["label_username","label_password","username","password"]:
# 				self.get_widget(a).set_sensitive(True)
# 		else:
# 			for a in ["label_username","label_password","username","password"]:
# 				self.get_widget(a).set_sensitive(False)
# 
# 	def process_radio_local_upses(self,caller):
# 		self.get_widget("hbox_local_upses").get_data("combo").set_sensitive(caller.get_active())
# 			
# 	def process_radio_network_upses(self,caller):
# 		self.get_widget("ups_address").set_sensitive(caller.get_active())
# 			
# 	
# 	def process_prefs_ok(self,caller):
# 		cont = True
# 		if self.get_widget("radio_local_upses").get_active():
# 			combobox = self.get_widget("hbox_local_upses").get_data("combo")
# 			addr = combobox.get_children()[0].get_text()
# 		else:
# 			addr = self.get_widget("ups_address").get_text()
# 		if len(addr) < 1 :
# 			cont = False
# 			self.show_info_dialog("Please choose an UPS or enter the full UPS address (ups@server) before continuing")
# 		if self.get_widget("authenticate").get_active():
# 			user = self.get_widget("username").get_text()
# 			password = self.get_widget("password").get_text()
# 			if len(user) < 1 or len(password) < 1:
# 				cont = False
# 				self.show_info_dialog("Please type in a user name and a password before continuing")
# 		else:
# 			user = None
# 			password = None
# 		
# 		if cont:
# 			
# 			if self.monitor(addr,user,password) is True:
# 				self.hide_prefs_window()
# 
# 	def process_prefs_cancel(self,caller):
# 		self.hide_prefs_window()
# 		
# 
# 	def run_uri(self,uri): gnome.url_show(uri)
# 
# 	
# 	def open_website(self,caller = None):
# 		self.run_uri("http://www.amautacorp.com/staff/Rudd-O/projects/ups-front/")
# 		
# 	def open_author_website(self,caller = None):
# 		self.run_uri("http://www.amautacorp.com/staff/Rudd-O/")
# 		
# 	def send_mail_to_author(self,caller = None):
# 		self.run_uri("mailto:rudd-o@amautacorp.com")
# 	
# 	def open_gpl(self,caller = None):
# 		self.run_uri("http://www.fsf.org/copyleft/gpl.txt")
# 		
# 	def setup_session_management(self):
# 		self.smclient = gnome.ui.master_client()
# 		self.smclient.connect_object("save-yourself",self.save_yourself,None)
# 		self.smclient.connect_object("die",self.die,None)
# 		self.smclient.connect_object("save-complete",self.save_complete,None)
# 		self.smclient.connect_object("shutdown-cancelled",self.shutdown_cancelled,None)
# 
# 	def save_yourself(self,*args):
# 		self.save_config()
# 		if args[0] == 1: self.smclient.request_phase_2()
# 
# 	def die(self,u=None):
# 		self.quit()
# 		
# 	def save_complete(self,u=None):
# 		return
# 		
# 	def shutdown_cancelled(self,u=None):
# 		return		

def main():

	program = gnome.program_init("gnome-fix-broken-links",get_version())
	program.parse_args()
# 	print program.get_property("gnome-datadir")

	gtk.threads_init()
	
	app = GNOMEFixBrokenLinksApp(get_shared_path())
	
	app.find_possible_targets(sys.argv[1])

	gtk.threads_enter()
	gtk.main()
	gtk.threads_leave()

if __name__ == "__main__":
	main()
