main.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/python
  2. #
  3. # Graphical interface for the b64converter middleware.
  4. # This program is part of b64converter_python.
  5. #
  6. # Copyright (C) 2015 Vitor S <https://notabug.org/kzimmermann>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. import os
  22. import sys
  23. import gtk
  24. import b64converter as b64
  25. class MainWindow(object):
  26. '''
  27. Window for the application. Users can either type the path to the file or
  28. use the "Choose" button to pick it from a dialog.
  29. The encode() and decode() methods translate directly to the CLI utility's
  30. functions.
  31. '''
  32. def encode(self, widget, data=None):
  33. '''
  34. Wrapper for b64converter.encode
  35. '''
  36. if self.lbl_file.get_text() == "":
  37. print "No file selected."
  38. self.statusbar.set_text("No file selected.")
  39. else:
  40. try:
  41. b64.encode(self.lbl_file.get_text())
  42. self.statusbar.set_text("Encoded to file %s" %
  43. self.lbl_file.get_text().split(os.sep)[-1] + ".txt")
  44. except IOError:
  45. print "File not found."
  46. self.statusbar.set_text("File not found.")
  47. def decode(self, widget, data=None):
  48. '''
  49. Wrapper for b64converter.decode
  50. '''
  51. if self.lbl_file.get_text() == "":
  52. print "No file selected."
  53. self.statusbar.set_text("No file selected.")
  54. return
  55. # Just so we prevent people from losing data if they don't
  56. # know very well what base64 is
  57. if not self.confirmnotxt.get_active():
  58. try:
  59. print self.lbl_file.get_text().split(".txt")[1]
  60. except IndexError:
  61. print "File doesn't look like base64 text"
  62. self.statusbar.set_text("This is not a text file. Check the box above to override.")
  63. return
  64. try:
  65. b64.decode(self.lbl_file.get_text())
  66. self.statusbar.set_text("Decoded to file %s" %
  67. self.lbl_file.get_text().split(os.sep)[-1].split(".txt")[0])
  68. except IOError:
  69. print "File not found."
  70. self.statusbar.set_text("File not found.")
  71. def choose_file(self, widget, data=None):
  72. '''
  73. Launch file chooser dialog to easily pick a file.
  74. '''
  75. self.dialog = gtk.FileChooserDialog(
  76. title = "Choose a file",
  77. action = gtk.FILE_CHOOSER_ACTION_OPEN,
  78. buttons = (
  79. gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  80. gtk.STOCK_OK, gtk.RESPONSE_OK
  81. )
  82. )
  83. self.chosen_file = self.dialog.run()
  84. if self.chosen_file == gtk.RESPONSE_OK:
  85. self.lbl_file.set_text(self.dialog.get_filename())
  86. else:
  87. print "Nothing changed."
  88. self.dialog.destroy()
  89. def destroy(self, widget, data=None):
  90. gtk.main_quit()
  91. return False
  92. def __init__(self):
  93. # Temporary storage for showing widgets later
  94. self.widgets = []
  95. # Widgets themselves...
  96. self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  97. self.window.set_title("Base 64 Converter")
  98. self.window.set_default_size(420, 100)
  99. self.window.set_border_width(5)
  100. self.instructions = gtk.Label("Select a file and convert to or from base64!")
  101. self.widgets.append(self.instructions)
  102. self.btn_encode = gtk.Button("_Encode")
  103. self.widgets.append(self.btn_encode)
  104. self.btn_decode = gtk.Button("_Decode")
  105. self.widgets.append(self.btn_decode)
  106. self.lbl_file = gtk.Entry()
  107. self.widgets.append(self.lbl_file)
  108. self.btn_filechooser = gtk.Button("Choose file")
  109. self.widgets.append(self.btn_filechooser)
  110. self.statusbar = gtk.Label("Program OK")
  111. self.widgets.append(self.statusbar)
  112. self.confirmnotxt = gtk.CheckButton("_Ignore extension")
  113. self.widgets.append(self.confirmnotxt)
  114. self.confirmnotxtexplanation = gtk.Label(
  115. "By default only *.txt files can be decoded.\nCheck this box to decode any file."
  116. )
  117. self.widgets.append(self.confirmnotxtexplanation)
  118. # Widget containers
  119. self.vgrid = gtk.VBox(False, 2)
  120. self.file_row = gtk.HBox(False, 0)
  121. self.btn_row = gtk.HBox(True, 0)
  122. self.override_row = gtk.HBox(False, 1)
  123. self.widgets.append(self.vgrid)
  124. self.widgets.append(self.file_row)
  125. self.widgets.append(self.btn_row)
  126. self.widgets.append(self.override_row)
  127. # Connect buttons to interactions
  128. self.window.connect("destroy", self.destroy)
  129. self.btn_encode.connect("clicked", self.encode)
  130. self.btn_decode.connect("clicked", self.decode)
  131. self.btn_filechooser.connect("clicked", self.choose_file)
  132. # Pack, show and add everything to main window
  133. self.file_row.pack_start(self.lbl_file, True, True, 0)
  134. self.file_row.pack_start(self.btn_filechooser, False, False, 0)
  135. self.btn_row.pack_start(self.btn_encode, True, True, 0)
  136. self.btn_row.pack_start(self.btn_decode, True, True, 0)
  137. self.override_row.pack_start(self.confirmnotxtexplanation, True, True, 0)
  138. self.override_row.pack_start(self.confirmnotxt, False, False, 0)
  139. self.vgrid.pack_start(self.instructions, True, True, 0)
  140. self.vgrid.pack_start(self.file_row, False, False, 0)
  141. self.vgrid.pack_start(self.btn_row, False, False, 0)
  142. self.vgrid.pack_start(self.override_row, False, True, 0)
  143. self.vgrid.pack_start(self.statusbar, False, False, 0)
  144. for widget in self.widgets:
  145. widget.show()
  146. self.window.add(self.vgrid)
  147. self.window.show()
  148. def main(self):
  149. gtk.main()
  150. return 0
  151. if __name__ == "__main__":
  152. window = MainWindow()
  153. window.main()
  154. print "Exit ok!"