/ entropy-config.applescript
entropy-config.applescript
  1  (* ==== Properties ==== *)
  2  
  3  property panelWIndow : missing value
  4  
  5  (* ==== Event Handlers ==== *)
  6  
  7  --- subroutines
  8  on assignvalue(theLine)
  9  	set theValue to ""
 10  	repeat with j from ((offset of "=" in theLine) + 1) to (length of theLine)
 11  		set theValue to theValue & (character j of theLine)
 12  	end repeat
 13  	if theValue = "true" then
 14  		set theValue to true
 15  	else if theValue = "false" then
 16  		set theValue to false
 17  	end if
 18  	return theValue
 19  end assignvalue
 20  
 21  on clicked theObject
 22  	-- Read in the preferences
 23  	set theLocation to call method "defaultObjectForKey:" with parameter "entropy"
 24  	-- read config file
 25  	try
 26  		set read_success to true
 27  		set theFile to alias (theLocation & "entropy.conf")
 28  		set fref to (open for access theFile)
 29  		-- need to use unix \n instead of \r
 30  		set configdata to read fref using delimiter "
 31  " as string
 32  		--
 33  		close access fref
 34  		set len to (length of configdata)
 35  		repeat with i from 1 to len
 36  			set theLine to item i of configdata
 37  			if theLine contains "=" then
 38  				--assign variables
 39  				
 40  				if theLine contains "nodename" then
 41  					set nodename to assignvalue(theLine)
 42  					set nodename_line to i
 43  				else if theLine contains "nodeport" then
 44  					set nodeport to assignvalue(theLine)
 45  					set nodeport_line to i
 46  				else if theLine contains "proxyport" then
 47  					set proxyport to assignvalue(theLine)
 48  					set proxyport_line to i
 49  				else if theLine contains "proxyhost" then
 50  					set proxyhost to assignvalue(theLine)
 51  					set proxyhost_line to i
 52  				else if theLine contains "bwlimit=" then
 53  					set bwlimit to assignvalue(theLine)
 54  					set bwlimit_line to i
 55  				else if theLine contains "maxhtl" then
 56  					set maxhtl to assignvalue(theLine)
 57  					set maxhtl_line to i
 58  				else if theLine contains "fcpport" then
 59  					set fcpport to assignvalue(theLine)
 60  					set fcpport_line to i
 61  				else if theLine contains "fcphost" then
 62  					set fcphost to assignvalue(theLine)
 63  					set fcphost_line to i
 64  				else if theLine contains "logLevel" then
 65  					set logLevel to assignvalue(theLine)
 66  					set logLevel_line to i
 67  				else if theLine contains "language=" then
 68  					set language to assignvalue(theLine)
 69  					set language_line to i
 70  				else if theLine contains "password=" then
 71  					set the_password to assignvalue(theLine)
 72  					set the_password_line to i
 73  				else if theLine contains "news_boards=" then
 74  					set news_boards to assignvalue(theLine)
 75  					set news_boards_line to i
 76  				else if theLine contains "news_days=" then
 77  					set news_days to assignvalue(theLine)
 78  					set news_days_line to i
 79  				else if theLine contains "news_nick=" then
 80  					set news_nick to assignvalue(theLine)
 81  					set news_nick_line to i
 82  				end if
 83  			end if
 84  		end repeat
 85  	on error
 86  		display dialog "Error: Encountered problem reading entropy.conf"
 87  		set read_success to false
 88  	end try
 89  	
 90  	-- if good read then display panel
 91  	if read_success then
 92  		
 93  		-- Load the panel. We do this by loading the nib that contains the panel window, and then setting our property to the loaded window. Only do this once, as every time the nib is loaded, it will create new copies of all of the top level objects in the nib.
 94  		try
 95  			if not (exists panelWIndow) then
 96  				load nib "eConfigPanel"
 97  				set panelWIndow to window "eConfig"
 98  			end if
 99  			
100  			-- Set the state of the items in the panel
101  			tell panelWIndow
102  				set contents of text field "nodename" to nodename
103  				set contents of text field "nodeport" to nodeport
104  				set contents of text field "proxyport" to proxyport
105  				set contents of text field "proxyhost" to proxyhost
106  				set contents of text field "bwlimit" to bwlimit
107  				set contents of text field "maxhtl" to maxhtl
108  				set contents of text field "fcpport" to fcpport
109  				set contents of text field "fcphost" to fcphost
110  				set contents of text field "logLevel" to logLevel
111  				set contents of text field "language" to language
112  				set contents of text field "password" to the_password
113  				set contents of text field "news_boards" to news_boards
114  				set contents of text field "news_days" to news_days
115  				set contents of text field "news_nick" to news_nick
116  			end tell
117  			-- display panel
118  			try
119  				--sets theResult to the parameter we pass in config-buttons
120  				set theResult to (display panel panelWIndow)
121  				if theResult is 1 then -- we hit the save button
122  					-- Set the new state of the items from the panel
123  					tell panelWIndow
124  						set nodename to contents of text field "nodename"
125  						set nodeport to contents of text field "nodeport"
126  						set proxyport to contents of text field "proxyport"
127  						set proxyhost to contents of text field "proxyhost"
128  						set bwlimit to contents of text field "bwlimit"
129  						set maxhtl to contents of text field "maxhtl"
130  						set fcpport to contents of text field "fcpport" as string
131  						set fcphost to contents of text field "fcphost"
132  						set logLevel to contents of text field "logLevel"
133  						set language to contents of text field "language"
134  						set the_password to contents of text field "password"
135  						set news_boards to contents of text field "news_boards"
136  						set news_days to contents of text field "news_days"
137  						set news_nick to contents of text field "news_nick"
138  					end tell
139  					-- Set new contents of each line
140  					set item nodename_line of configdata to "nodename=" & nodename
141  					set item nodeport_line of configdata to "nodeport=" & nodeport
142  					set item proxyport_line of configdata to "proxyport=" & proxyport
143  					set item proxyhost_line of configdata to "proxyhost=" & proxyhost
144  					set item bwlimit_line of configdata to "bwlimit=" & bwlimit
145  					set item maxhtl_line of configdata to "maxhtl=" & maxhtl
146  					set item fcpport_line of configdata to "fcpport=" & fcpport
147  					set item fcphost_line of configdata to "fcphost=" & fcphost
148  					set item logLevel_line of configdata to "logLevel=" & logLevel
149  					set item the_password_line of configdata to "password=" & the_password
150  					set item news_boards_line of configdata to "news_boards=" & news_boards
151  					set item news_days_line of configdata to "news_days=" & news_days
152  					set item news_nick_line of configdata to "news_nick=" & news_nick
153  					set item language_line of configdata to "language=" & language
154  					-- write out new file
155  					try
156  						set fref to (open for access theFile with write permission)
157  						repeat with i from 1 to len
158  							write item i of configdata to fref
159  							-- need to use unix \n instead of \r
160  							write "
161  " to fref
162  						end repeat
163  						close access fref
164  						try
165  							-- so cancel doesn't error out
166  							display dialog "New Settings Saved! You must restart Entropy for the settings to take affect."
167  						end try
168  						
169  					on error
170  						display dialog "Error: Encountered problem writing entropy.conf. Please check the file and verify that it is not corrupt. Normally the last few lines with contain errors."
171  						
172  					end try
173  					-- no else since else means we pressed the cancel button
174  				end if -- theResult is 1
175  			on error
176  				display dialog "Error displaying configuration panel (try)"
177  			end try -- display panel
178  		on error
179  			display dialog "Error loading nib configuration panel (try)"
180  		end try -- load panel
181  	end if -- read success	
182  	
183  end clicked