-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGui.py
114 lines (102 loc) · 4.99 KB
/
Gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from utils import makePrediction
import re
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MainWindow(Gtk.Window):
"""
Class for the main window of the GUI. Uses the file glade_file.glade to build the UI.
"""
encryptedFilePath = ""
decryptedFilePath = ""
def __init__(self):
"""
Create all the GUI elements from the glade file on initialization.
"""
self.gladeFile = "res/glade_file.glade"
self.builder = Gtk.Builder()
self.builder.add_from_file(self.gladeFile)
self.builder.connect_signals(self)
self.window = self.builder.get_object("window1")
self.window.set_size_request(550, 500)
self.window.connect("destroy", Gtk.main_quit)
self.header = Gtk.HeaderBar()
self.header.props.title = "Integrated Approach for Cryptology using Deep Neural Network"
self.header.set_show_close_button(True)
self.decryptButton = self.builder.get_object("decrypt")
self.decryptButton.connect("clicked", self.decrypt)
self.encryptedText = self.builder.get_object("encrypted_text")
self.encryptedTextBuffer = self.encryptedText.get_buffer()
self.decryptedText = self.builder.get_object("decrypted_text")
self.decryptedTextBuffer = self.decryptedText.get_buffer()
self.window.set_titlebar(self.header)
self.window.show_all()
def decrypt(self, widget):
"""
Called on clicking the "Decrypt" button. Performs decryption of the encrypted text.
Displays the output to the decrypted text window and if a text file is selected,
stores the data to the text file.
"""
if self.encryptedTextBuffer == "":
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "ERROR")
dialog.format_secondary_text("Please write the encrypted data to decrypt or select file!")
dialog.run()
dialog.destroy()
else:
start_iter = self.encryptedTextBuffer.get_start_iter()
end_iter = self.encryptedTextBuffer.get_end_iter()
text = self.encryptedTextBuffer.get_text(start_iter, end_iter, True)
tokens = text.split('\n')
output = makePrediction(tokens[:-1])
self.decryptedTextBuffer.set_text(output)
if self.decryptedFilePath != "":
with open(self.decryptedFilePath,"w") as f:
f.write(output)
def encryptedFileSelect(self, widget):
"""
Called on selecting the encrypted file to decrypt. Reads the file and displays the
text to the encrypted text window.
:param widget: The current widget object
"""
self.encryptedFilePath = widget.get_filename()
if not re.search('.txt$', self.encryptedFilePath): #Check to ensure that the input file is of type .txt
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "ERROR")
dialog.format_secondary_text("Please select a \"Text\" file!")
dialog.run()
dialog.destroy()
self.encryptedFilePath = ""
widget.set_filename("None")
else:
with open(self.encryptedFilePath) as file:
data = file.read()
self.encryptedTextBuffer.set_text(data)
def decryptedFileSelect(self, widget):
"""
Called on selecting the file to decrypt to. Stores the path of the file.
:param widget: The current widget object
"""
self.decryptedFilePath = widget.get_filename()
if not re.search('.txt$', self.decryptedFilePath): #Ensure that the selected file is a text file.
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "ERROR")
dialog.format_secondary_text("Please select a \"Text\" file!")
dialog.run()
dialog.destroy()
self.decryptedFilePath = ""
widget.set_filename("None")
elif not os.stat(self.decryptedFilePath).st_size == 0: #Handle case when selected file is not empty.
dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "WARNING")
dialog.format_secondary_text("The file where the decrypted data is to be written is not empty."
"\nAre you sure you want to continue?")
response = dialog.run()
if response == Gtk.ResponseType.NO:
dialog1 = Gtk.FileChooserDialog("Please choose a new file", None,
Gtk.FileChooserAction.OPEN, (Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
response1 = dialog1.run()
if response1 == Gtk.ResponseType.OK:
self.decryptedFilePath = dialog1.get_filename()
self.decrypted_file_file_set_cb(self, widget)
dialog1.destroy()
dialog.destroy()
window = MainWindow()
Gtk.main()