Add gettext support for lxproxy
[lxde/lxadmin.git] / src / lxproxy / lxproxy.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ### BEGIN LICENSE
4 # Copyright (C) YYYY Leszek Lesner leszek@zevenos.com
5 #This program is free software: you can redistribute it and/or modify it
6 #under the terms of the GNU General Public License version 3, as published
7 #by the Free Software Foundation.
8 #
9 #This program is distributed in the hope that it will be useful, but
10 #WITHOUT ANY WARRANTY; without even the implied warranties of
11 #MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12 #PURPOSE. See the GNU General Public License for more details.
13 #
14 #You should have received a copy of the GNU General Public License along
15 #with this program. If not, see <http://www.gnu.org/licenses/>.
16 ### END LICENSE
17
18 import sys
19 import os
20 import gtk
21 from user import home
22
23 import lxadmin.defs as defs
24
25 import gettext
26
27 gettext.bindtextdomain('lxadmin', defs.LOCALE_DIR)
28 gettext.textdomain('lxadmin')
29 _ = gettext.gettext
30
31 # Check if we are working in the source tree or from the installed
32 # package and mangle the python path accordingly
33 if os.path.dirname(sys.argv[0]) != ".":
34 if sys.argv[0][0] == "/":
35 fullPath = os.path.dirname(sys.argv[0])
36 sys.path.insert(0, os.path.join(defs.SHARED_DATA_DIR,"lxproxy"))
37 else:
38 fullPath = os.getcwd() + "/" + os.path.dirname(sys.argv[0])
39 else:
40 fullPath = os.getcwd()
41 sys.path.insert(0, os.path.dirname(fullPath))
42 DEFAULTDICT = {"export http_proxy": "http://user:password@hostname:8080",
43 "export https_proxy": "http://user:password@hostname:8080",
44 "export ftp_proxy": "http://user:password@hostname:8080"}
45
46 import AboutLxproxyDialog
47 from lxproxyconfig import getdatapath
48
49 class LxproxyWindow(gtk.Window):
50 __gtype_name__ = "LxproxyWindow"
51
52 def __init__(self):
53 """__init__ - This function is typically not called directly.
54 Creation a LxproxyWindow requires redeading the associated ui
55 file and parsing the ui definition extrenally,
56 and then calling LxproxyWindow.finish_initializing().
57
58 Use the convenience function NewLxproxyWindow to create
59 LxproxyWindow object.
60
61 """
62 pass
63
64 def finish_initializing(self, builder):
65 """finish_initalizing should be called after parsing the ui definition
66 and creating a LxproxyWindow object with it in order to finish
67 initializing the start of the new LxproxyWindow instance.
68
69 """
70 #get a reference to the builder and set up the signals
71 self.builder = builder
72 self.builder.connect_signals(self)
73 global httptxt, httpstxt, ftptxt, sockstxt, httpspin, httpsspin, ftpspin, socksspin,directradio,manualradio
74 directradio = self.builder.get_object("directradio")
75 manualradio = self.builder.get_object("manualradio")
76 httptxt = self.builder.get_object("httptxt")
77 httpstxt = self.builder.get_object("httpstxt")
78 ftptxt = self.builder.get_object("ftptxt")
79 sockstxt = self.builder.get_object("sockstxt")
80 httpspin = self.builder.get_object("httpspin")
81 httpsspin = self.builder.get_object("httpsspin")
82 ftpspin = self.builder.get_object("ftpspin")
83 socksspin = self.builder.get_object("socksspin")
84 socksbox = self.builder.get_object("hbox4")
85 socksbox.hide()
86 if manualradio.get_active():
87 httptxt.set_sensitive(True)
88 httpstxt.set_sensitive(True)
89 ftptxt.set_sensitive(True)
90 sockstxt.set_sensitive(True)
91 httpspin.set_sensitive(True)
92 httpsspin.set_sensitive(True)
93 ftpspin.set_sensitive(True)
94 socksspin.set_sensitive(True)
95 else:
96 httptxt.set_sensitive(False)
97 httpstxt.set_sensitive(False)
98 ftptxt.set_sensitive(False)
99 sockstxt.set_sensitive(False)
100 httpspin.set_sensitive(False)
101 httpsspin.set_sensitive(False)
102 ftpspin.set_sensitive(False)
103 socksspin.set_sensitive(False)
104 httpspin.set_value(8080)
105 httpsspin.set_value(8080)
106 ftpspin.set_value(8080)
107 socksspin.set_value(8080)
108
109 self.readfile()
110
111 #uncomment the following code to read in preferences at start up
112 #dlg = PreferencesLxproxyDialog.NewPreferencesLxproxyDialog()
113 #self.preferences = dlg.get_preferences()
114
115 #code for other initialization actions should be added here
116
117 def readfile(self):
118 profile_location = os.path.expanduser('~/.profile')
119 if not os.path.exists(profile_location):
120 tmpfile = open(profile_location, 'w')
121 tmpfile.write('')
122 tmpfile.close()
123 try:
124 data = profile_location.read()
125 profile_location.close()
126 config = self.parse(data)
127 except:
128 print "Could not open proxyconfig"
129
130 def parse(self, s):
131 #Fetch a *copy* of the default dictionary.
132 ret = DEFAULTDICT.copy()
133 # Split lines
134 lines = s.split("\n")
135 for line in lines:
136 line = line.strip()
137 #if line and line[0] != "#":
138 if line.startswith('export'):
139 manualradio.set_active(True)
140 values = line.split("=")
141 ret[values[0]] = values[1]
142
143 try:
144 #print ret # Debugging
145 port=ret["export http_proxy"].split(":")
146 httptxt.set_text(port[0]+":"+port[1]+":"+port[2])
147 httpspin.set_value(float(port[3]))
148 port=ret["export https_proxy"].split(":")
149 httpstxt.set_text(port[0]+":"+port[1]+":"+port[2])
150 httpsspin.set_value(float(port[3]))
151 port=ret["export ftp_proxy"].split(":")
152 ftptxt.set_text(port[0]+":"+port[1]+":"+port[2])
153 ftpspin.set_value(float(port[3]))
154 except:
155 try:
156 port=ret["export http_proxy"].split(":")
157 httptxt.set_text(port[0]+":"+port[1])
158 httpspin.set_value(float(port[2]))
159 port=ret["export https_proxy"].split(":")
160 httpstxt.set_text(port[0]+":"+port[1])
161 httpsspin.set_value(float(port[2]))
162 port=ret["export ftp_proxy"].split(":")
163 ftptxt.set_text(port[0]+":"+port[1])
164 ftpspin.set_value(float(port[2]))
165 except:
166 httptxt.set_text(ret["export http_proxy"])
167 httpstxt.set_text(ret["export https_proxy"])
168 ftptxt.set_text(ret["export ftp_proxy"])
169
170
171 def on_closebtn_clicked(self,widget):
172 if directradio.get_active():
173 file(home+'/.profile2', 'w').writelines([l for l in file(home+'/.profile').readlines() if 'export http_proxy' not in l])
174 os.system("mv "+ home+"/.profile2 " + home + "/.profile")
175 file(home+'/.profile2', 'w').writelines([l for l in file(home+'/.profile').readlines() if 'export https_proxy' not in l])
176 os.system("mv "+ home+"/.profile2 " + home + "/.profile")
177 file(home+'/.profile2', 'w').writelines([l for l in file(home+'/.profile').readlines() if 'export ftp_proxy' not in l])
178 os.system("mv "+ home+"/.profile2 " + home + "/.profile")
179 file(home+'/.profile2', 'w').writelines([l for l in file(home+'/.profile').readlines() if 'export HTTP_PROXY' not in l])
180 os.system("mv "+ home+"/.profile2 " + home + "/.profile")
181 file(home+'/.profile2', 'w').writelines([l for l in file(home+'/.profile').readlines() if 'export HTTPS_PROXY' not in l])
182 os.system("mv "+ home+"/.profile2 " + home + "/.profile")
183 file(home+'/.profile2', 'w').writelines([l for l in file(home+'/.profile').readlines() if 'export FTP_PROXY' not in l])
184 os.system("mv "+ home+"/.profile2 " + home + "/.profile")
185 else:
186 os.system("echo 'export http_proxy="+httptxt.get_text()+":"+str(httpspin.get_value())[:-2]+"' >> "+ home+ "/.profile")
187 os.system("echo 'export https_proxy="+httpstxt.get_text()+":"+str(httpsspin.get_value())[:-2]+"' >> "+ home+ "/.profile")
188 os.system("echo 'export ftp_proxy="+ftptxt.get_text()+":"+str(ftpspin.get_value())[:-2]+"' >> "+ home+ "/.profile")
189 os.system("echo 'export HTTP_PROXY=$http_proxy' >> "+ home+ "/.profile")
190 os.system("echo 'export HTTPS_PROXY=$https_proxy' >> "+ home+ "/.profile")
191 os.system("echo 'export FTP_PROXY=$ftp_proxy' >> "+ home+ "/.profile")
192 self.quit(self,widget)
193
194 def on_manualradio_toggled(self,widget):
195 httptxt.set_sensitive(True)
196 httpstxt.set_sensitive(True)
197 ftptxt.set_sensitive(True)
198 sockstxt.set_sensitive(True)
199 httpspin.set_sensitive(True)
200 httpsspin.set_sensitive(True)
201 ftpspin.set_sensitive(True)
202 socksspin.set_sensitive(True)
203
204 def on_directradio_toggled(self,widget):
205 httptxt.set_sensitive(False)
206 httpstxt.set_sensitive(False)
207 ftptxt.set_sensitive(False)
208 sockstxt.set_sensitive(False)
209 httpspin.set_sensitive(False)
210 httpsspin.set_sensitive(False)
211 ftpspin.set_sensitive(False)
212 socksspin.set_sensitive(False)
213
214
215 def about(self, widget, data=None):
216 """about - display the about box for lxproxy """
217 about = AboutLxproxyDialog.NewAboutLxproxyDialog()
218 response = about.run()
219 about.destroy()
220
221 def preferences(self, widget, data=None):
222 """preferences - display the preferences window for lxproxy """
223 prefs = PreferencesLxproxyDialog.NewPreferencesLxproxyDialog()
224 response = prefs.run()
225 if response == gtk.RESPONSE_OK:
226 #make any updates based on changed preferences here
227 pass
228 prefs.destroy()
229
230 def quit(self, widget, data=None):
231 """quit - signal handler for closing the LxproxyWindow"""
232 self.destroy()
233
234 def on_destroy(self, widget, data=None):
235 """on_destroy - called when the LxproxyWindow is close. """
236 #clean up code for saving application state should be added here
237
238 gtk.main_quit()
239
240 def NewLxproxyWindow():
241 """NewLxproxyWindow - returns a fully instantiated
242 LxproxyWindow object. Use this function rather than
243 creating a LxproxyWindow directly.
244 """
245
246 #look for the ui file that describes the ui
247 #ui_filename = os.path.join(getdatapath(), 'ui', 'lxproxy', 'LxproxyWindow.ui')
248 #if not os.path.exists(ui_filename):
249 # ui_filename = None
250 #
251 builder = gtk.Builder()
252 builder.add_from_file(getdatapath('LxproxyWindow.ui'))
253 window = builder.get_object("lxproxy_window")
254 window.finish_initializing(builder)
255 return window
256
257 if __name__ == "__main__":
258 #support for command line options
259 import logging, optparse
260 parser = optparse.OptionParser(version="%prog %ver")
261 parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Show debug messages")
262 (options, args) = parser.parse_args()
263
264 #set the logging level to show debug messages
265 if options.verbose:
266 logging.basicConfig(level=logging.DEBUG)
267 logging.debug('logging enabled')
268
269 #run the application
270 window = NewLxproxyWindow()
271 window.show()
272 gtk.main()
273