Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Piote Is an Osm Tag Editor
  5. #
  6. # © 2009, David Paleino <d.paleino@gmail.com>
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be
  17. # included in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  23. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  24. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. # SOFTWARE.
  27.  
  28. import pygtk
  29. pygtk.require("2.0")
  30. import gtk
  31.  
  32. import Piote
  33. from Piote.Utils import *
  34. from Config import Config
  35. from base64 import b64decode, b64encode
  36.  
  37. class PreferencesDialog(gtk.Dialog):
  38. def __init__(self, widget, firstrun=False):
  39. self.cfg = Config()
  40.  
  41. gtk.Dialog.__init__(self,
  42. "Preferences",
  43. None,
  44. gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  45. (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
  46. gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
  47. self.set_resizable(False)
  48.  
  49. frame = gtk.Frame()
  50. label = gtk.Label("<b>Authentication</b>")
  51. label.set_use_markup(True)
  52. frame.set_label_widget(label)
  53. frame.set_shadow_type(gtk.SHADOW_NONE)
  54. align = gtk.Alignment()
  55. align.set_padding(0,0,12,0)
  56. vbox = gtk.VBox()
  57. vbox.pack_start(gtk.Label("Username"))
  58. username = gtk.Entry()
  59. vbox.pack_start(username)
  60. vbox.pack_start(gtk.Label("Password"))
  61. password = gtk.Entry()
  62. password.set_visibility(False)
  63. vbox.pack_start(password)
  64. align.add(vbox)
  65. frame.add(align)
  66. self.vbox.pack_start(frame)
  67.  
  68. frame = gtk.Frame()
  69. label = gtk.Label("<b>API</b>")
  70. label.set_use_markup(True)
  71. frame.set_label_widget(label)
  72. frame.set_shadow_type(gtk.SHADOW_NONE)
  73. align = gtk.Alignment()
  74. align.set_padding(0,0,12,0)
  75. vbox = gtk.VBox()
  76. api = gtk.RadioButton(label="api.openstreetmap.org")
  77. api06dev = gtk.RadioButton(group=api, label="api06.dev.openstreetmap.org")
  78. try:
  79. if self.cfg.get("DEFAULT", "api") == "api.openstreetmap.org":
  80. api.set_active(True)
  81. else:
  82. api06dev.set_active(True)
  83. except NoOptionError:
  84. api.set_active(True)
  85. vbox.pack_start(api)
  86. vbox.pack_start(api06dev)
  87. align.add(vbox)
  88. frame.add(align)
  89. self.vbox.pack_start(frame)
  90.  
  91. username.connect("activate", check_empty, "Username", self)
  92. password.connect("activate", check_empty, "Password", self)
  93.  
  94. api.connect("toggled", self.__api_changed, "api.openstreetmap.org")
  95. api06dev.connect("toggled", self.__api_changed, "api06.dev.openstreetmap.org")
  96.  
  97. # populate fields
  98. try:
  99. username.set_text(self.cfg.get("Authentication", "username"))
  100. password.set_text(b64decode(self.cfg.get("Authentication", "password")))
  101. except (NoSectionError, NoOptionError):
  102. pass
  103.  
  104. self.vbox.show_all()
  105. response = self.run()
  106. self.destroy()
  107.  
  108. if response == gtk.RESPONSE_ACCEPT:
  109. if check_empty(username, "Username") and check_empty(password, "Password"):
  110. try:
  111. self.cfg.add_section("Authentication")
  112. except DuplicateSectionError:
  113. pass
  114. finally:
  115. self.cfg.set("Authentication", "username", username.get_text())
  116. self.cfg.set("Authentication", "password", b64encode(password.get_text()))
  117. self.cfg.set("DEFAULT", "api", Piote.api_url)
  118. try:
  119. self.cfg.write()
  120. except IOError:
  121. print "Cannot write to piote.cfg!"
  122.  
  123. def __api_changed(self, widget, api):
  124. if widget.get_active():
  125. Piote.api_url = api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement