/ freenet-config.applescript
freenet-config.applescript
  1  
  2  (* ==== Properties ==== *)
  3  
  4  property panelWIndow : missing value
  5  
  6  (* ==== Event Handlers ==== *)
  7  
  8  --- subroutines
  9  on assignvalue(theLine)
 10  	set theValue to ""
 11  	repeat with j from ((offset of "=" in theLine) + 1) to (length of theLine)
 12  		set theValue to theValue & (character j of theLine)
 13  	end repeat
 14  	if theValue = "true" then
 15  		set theValue to true
 16  	else if theValue = "false" then
 17  		set theValue to false
 18  	end if
 19  	return theValue
 20  end assignvalue
 21  
 22  on clicked theObject
 23  	-- Read in the preferences
 24  	set theLocation to call method "defaultObjectForKey:" with parameter "location"
 25  	
 26  	-- read config file
 27  	try
 28  		set read_success to true
 29  		set theFile to alias (theLocation & "freenet.conf")
 30  		set fref to (open for access theFile)
 31  		-- need to use unix \n instead of \r
 32  		set configdata to read fref using delimiter "
 33  " as string
 34  		--
 35  		close access fref
 36  		set len to (length of configdata)
 37  		repeat with i from 1 to len
 38  			set theLine to item i of configdata
 39  			if theLine contains "=" then
 40  				--assign variables
 41  				considering case -- otherwise we can't distinguish bandwidth variables
 42  					if theLine contains "ipAddress" then
 43  						set ipAddress to assignvalue(theLine)
 44  						set ipAddress_line to i
 45  					else if theLine contains "listenPort" then
 46  						set listenPort to assignvalue(theLine)
 47  						set listenPort_line to i
 48  					else if theLine contains "adminPassword" then
 49  						set adminPassword to assignvalue(theLine)
 50  						set adminPassword_line to i
 51  					else if theLine contains "adminPeer" then
 52  						set adminPeer to assignvalue(theLine)
 53  						set adminPeer_line to i
 54  					else if theLine contains "transient" then
 55  						set transient to assignvalue(theLine)
 56  						set transient_line to i
 57  					else if theLine contains "bandwidthLimit=" then
 58  						set bandwidthlimit to assignvalue(theLine)
 59  						set bandwidthlimit_line to i
 60  					else if theLine contains "maxNodeConnections" then
 61  						set maxNodeConnections to assignvalue(theLine)
 62  						set maxNodeConnections_line to i
 63  					else if theLine contains "maximumThreads" then
 64  						set maximumThreads to assignvalue(theLine)
 65  						set maximumThreads_line to i
 66  					else if theLine contains "maxHopsToLive" then
 67  						set maxHopsToLive to assignvalue(theLine)
 68  						set maxHopsToLive_line to i
 69  					else if theLine contains "announcementHTL" then
 70  						set announcementHTL to assignvalue(theLine)
 71  						set announcementHTL_line to i
 72  					else if theLine contains "initialRequestHTL" then
 73  						set initialRequestHTL to assignvalue(theLine)
 74  						set initialRequestHTL_line to i
 75  					else if theLine contains "mainport.port" then
 76  						set mainportport to assignvalue(theLine)
 77  						set mainportport_line to i
 78  					else if theLine contains "mainport.allowedHosts" then
 79  						set mainportallowedHosts to assignvalue(theLine)
 80  						set mainportallowedHosts_line to i
 81  					else if theLine contains "logLevel" then
 82  						set logLevel to assignvalue(theLine)
 83  						set logLevel_line to i
 84  					else if theLine contains "logInboundContacts" then
 85  						set logInboundContacts to assignvalue(theLine)
 86  						set logInboundContacts_line to i
 87  					else if theLine contains "logOutboundContacts" then
 88  						set logOutboundContacts to assignvalue(theLine)
 89  						set logOutboundContacts_line to i
 90  					else if theLine contains "logInboundRequests" then
 91  						set logInboundRequests to assignvalue(theLine)
 92  						set logInboundRequests_line to i
 93  					else if theLine contains "logOutboundRequests" then
 94  						set logOutboundRequests to assignvalue(theLine)
 95  						set logOutboundRequests_line to i
 96  					else if theLine contains "logInboundInsertRequestDist" then
 97  						set logInboundInsertRequestDist to assignvalue(theLine)
 98  						set logInboundInsertRequestDist_line to i
 99  					else if theLine contains "logSuccessfulInsertRequestDist" then
100  						set logSuccessfulInsertRequestDist to assignvalue(theLine)
101  						set logSuccessfulInsertRequestDist_line to i
102  					end if
103  				end considering
104  			end if
105  		end repeat
106  	on error
107  		display dialog "Error: Encountered problem reading Freenet.conf"
108  		set read_success to false
109  	end try
110  	
111  	-- if good read then display panel
112  	if read_success then
113  		
114  		-- 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.
115  		try
116  			if not (exists panelWIndow) then
117  				load nib "ConfigPanel"
118  				set panelWIndow to window "Config"
119  			end if
120  			
121  			
122  			-- Set the state of the items in the panel
123  			tell panelWIndow
124  				set contents of text field "ipAddress" to ipAddress
125  				set contents of text field "listenPort" to listenPort
126  				set contents of text field "adminPassword" to adminPassword
127  				set contents of text field "adminPeer" to adminPeer
128  				set contents of button "transient" to transient
129  				set contents of text field "bandwidthLimit" to bandwidthlimit
130  				set contents of text field "maxNodeConnections" to maxNodeConnections
131  				set contents of text field "maximumThreads" to maximumThreads
132  				set contents of text field "maxHopsToLive" to maxHopsToLive
133  				set contents of text field "announcementHTL" to announcementHTL
134  				set contents of text field "initialRequestHTL" to initialRequestHTL
135  				set contents of text field "mainportport" to mainportport
136  				set contents of text field "mainportallowedHosts" to mainportallowedHosts
137  				set contents of text field "logLevel" to logLevel
138  				set contents of button "logOutboundContacts" to logOutboundContacts
139  				set contents of button "logInboundContacts" to logInboundContacts
140  				set contents of button "logOutboundRequests" to logOutboundRequests
141  				set contents of button "logInboundRequests" to logInboundRequests
142  				set contents of button "logInboundInsertRequestDist" to logInboundInsertRequestDist
143  				set contents of button "logSuccessfulInsertRequestDist" to logSuccessfulInsertRequestDist
144  			end tell
145  			
146  			-- display panel
147  			try
148  				--sets theResult to the parameter we pass in config-buttons
149  				set theResult to (display panel panelWIndow)
150  				if theResult is 1 then -- we hit the save button
151  					-- Set the new state of the items from the panel
152  					tell panelWIndow
153  						set ipAddress to contents of text field "ipAddress"
154  						set listenPort to contents of text field "listenPort"
155  						set adminPassword to contents of text field "adminPassword"
156  						set adminPeer to contents of text field "adminPeer"
157  						set transient to contents of button "transient" as string
158  						set bandwidthlimit to contents of text field "bandwidthLimit"
159  						set maxNodeConnections to contents of text field "maxNodeConnections"
160  						set maximumThreads to contents of text field "maximumThreads"
161  						set maxHopsToLive to contents of text field "maxHopsToLive"
162  						set announcementHTL to contents of text field "announcementHTL"
163  						set initialRequestHTL to contents of text field "initialRequestHTL"
164  						set mainportport to contents of text field "mainportport"
165  						set mainportallowedHosts to contents of text field "mainportallowedHosts"
166  						set logLevel to contents of text field "logLevel"
167  						set logOutboundContacts to contents of button "logOutboundContacts" as string
168  						set logInboundContacts to contents of button "logInboundContacts" as string
169  						set logOutboundRequests to contents of button "logOutboundRequests" as string
170  						set logInboundRequests to contents of button "logInboundRequests" as string
171  						set logInboundInsertRequestDist to contents of button "logInboundInsertRequestDist" as string
172  						set logSuccessfulInsertRequestDist to contents of button "logSuccessfulInsertRequestDist" as string
173  					end tell
174  					-- Set new contents of each line
175  					set item ipAddress_line of configdata to "ipAddress=" & ipAddress
176  					set item listenPort_line of configdata to "listenPort=" & listenPort
177  					set item adminPassword_line of configdata to "adminPassword=" & adminPassword
178  					set item adminPeer_line of configdata to "adminPeer=" & adminPeer
179  					set item transient_line of configdata to "transient=" & transient
180  					set item bandwidthlimit_line of configdata to "bandwidthLimit=" & bandwidthlimit
181  					set item maxNodeConnections_line of configdata to "maxNodeConnections=" & maxNodeConnections
182  					set item maximumThreads_line of configdata to "maximumThreads=" & maximumThreads
183  					set item maxHopsToLive_line of configdata to "maxHopsToLive=" & maxHopsToLive
184  					set item announcementHTL_line of configdata to "announcementHTL=" & announcementHTL
185  					set item initialRequestHTL_line of configdata to "initialRequestHTL=" & initialRequestHTL
186  					set item mainportport_line of configdata to "mainport.port=" & mainportport
187  					set item mainportallowedHosts_line of configdata to "mainport.allowedHosts=" & mainportallowedHosts
188  					set item logLevel_line of configdata to "logLevel=" & logLevel
189  					set item logOutboundContacts_line of configdata to "logOutboundContacts=" & logOutboundContacts
190  					set item logInboundContacts_line of configdata to "logInboundContacts=" & logInboundContacts
191  					set item logOutboundRequests_line of configdata to "logOutboundRequests=" & logOutboundRequests
192  					set item logInboundRequests_line of configdata to "logInboundRequests=" & logInboundRequests
193  					set item logInboundInsertRequestDist_line of configdata to "logInboundInsertRequestDist=" & logInboundInsertRequestDist
194  					set item logSuccessfulInsertRequestDist_line of configdata to "logSuccessfulInsertRequestDist=" & logSuccessfulInsertRequestDist
195  					-- write out new file
196  					try
197  						set fref to (open for access theFile with write permission)
198  						repeat with i from 1 to len
199  							write item i of configdata to fref
200  							-- need to use unix \n instead of \r
201  							write "
202  " to fref
203  						end repeat
204  						close access fref
205  						try
206  							-- so cancel doesn't error out
207  							display dialog "New Settings Saved! You must restart Freenet for the settings to take affect."
208  						end try
209  					on error
210  						display dialog "Error: Encountered problem writing Freenet.conf. Please check the file and verify that it is not corrupt. Normally the last few lines with contain errors."
211  						
212  					end try
213  					-- no else since else means we pressed the cancel button
214  				end if -- theResult is 1
215  			on error
216  				display dialog "Error displaying configuration panel (try)"
217  			end try -- display panel
218  		on error
219  			display dialog "Error loading nib configuration panel (try)"
220  		end try -- load panel
221  	end if -- read success	
222  	
223  end clicked