def __init__(self): tk.Tk.__init__(self) self.geometry("350x450+500+150") self.title(window_config['window_title']) self.resizable(False, True) self.configure(background=window_config['bg_color']) try: self.img = PIL.ImageTk.PhotoImage( PIL.Image.open(window_config['center_img'])) self.img_label = tk.Label(self, image=self.img, borderwidth=0, highlightthickness=0) self.img_label.pack(side=tk.TOP) except: print("Coulnd't load image !") self.username_box = PlaceholderEntry( self, placeholder=window_config['username_text'], width=30, font=window_config['font']) self.username_box.pack(side=tk.TOP, pady=10) self.password_box = PasswordEntry( self, placeholder=window_config['password_text'], width=30, font=window_config['font']) self.password_box.pack(side=tk.TOP, pady=10) self.password_box.bind('<Return>', lambda e: self.loginCommand()) self.login_btn = tk.Button(self, text=window_config['loginbtn_text'], width=30, relief=tk.FLAT, command=self.loginCommand, bg='white') self.login_btn.pack(side=tk.TOP, pady=3) self.guest_btn = tk.Button(self, text=window_config['guestbtn_text'], width=30, relief=tk.FLAT, command=self.guestCommand, bg='white') self.guest_btn.pack(side=tk.TOP, pady=10)
def addVidForm(self): self.vid_form = tk.Frame(self, borderwidth=5, bg='#3A79D1') self.vid_form.pack(side=tk.TOP, fill=tk.X, pady=10, padx=10, expand=False) self.vid_entry = PlaceholderEntry(self.vid_form, placeholder='Video Path', bg='white', font='Sans 11', width=40, relief=tk.RAISED) self.vid_entry.pack(side=tk.LEFT, padx=10) self.user_label = tk.Label(self.vid_form, text='Owner: ', font='Sans 11', bg='#3A79D1', fg='white') self.user_label.pack(side=tk.LEFT) list_of_items = self.list_of_users self.combobox = Combobox_Autocomplete(self.vid_form, list_of_items, relief=tk.RAISED, font='Sans 11', bg='white') self.combobox.pack(side=tk.LEFT) self.vid_form_btn = FocusButton(self.vid_form, in_color='#32bf1d', out_color='#149600', text='Add Video', relief=tk.FLAT, fg='white', command=self.addVideo) self.vid_form_btn.pack(side=tk.RIGHT)
def __init__(self, download_dir, source_dir): self.download_dir = download_dir tk.Tk.__init__(self) self.title("Torrent Sucks") self.geometry("500x500+300+100") self.resizable(False, True) self.config(bg="#474040") self.frame = tk.Frame(self, bg='#474040') self.frame.pack(side=tk.TOP, fill=tk.BOTH) self.search_bar = PlaceholderEntry(self.frame, "Please Enter the Filename", width=50) self.search_bar.pack(side=tk.LEFT, pady=20, padx=20) self.search_button = FocusButton(self.frame, text="Search", in_color="gray", out_color="black", fg="white", command=self.search) self.search_button.pack(side=tk.RIGHT, padx=20, pady=20) self.listbox_frame = tk.Frame(self, bg="#474040") self.listbox_frame.pack(side=tk.TOP, fill=tk.BOTH) self.listbox = tk.Listbox(self.listbox_frame, width=70, height=20) self.listbox.pack(side=tk.TOP, pady=20, padx=20) self.download_button = FocusButton(self.listbox_frame, text="Download", in_color="#52bf9c", out_color="#6ed1ff", fg="black", command=self.download) self.download_button.pack(side=tk.RIGHT, pady=20, padx=20) self.status_text = tk.Label(self.listbox_frame, text="", bg="#474040", font='Helvetica 10 bold') self.status_text.pack(side=tk.LEFT, pady=20, padx=20) #Start listener global host self.listener = Listener(host, source_dir) self.host, self.port = self.listener.get_self() print("Running On: {}:{}".format(self.host, self.port)) self.listen_thread = multiprocessing.Process( target=self.listener.listen) self.listen_thread.start() self.protocol("WM_DELETE_WINDOW", self.on_closing) self.search_file = "" #Start Sender self.sender = Sender(source_dir, self.host, self.port) global ft_server #Uncomment this if server is on print("Connecting to Server...") # print(ft_server[0]) # print(ft_server[1]) status = self.sender.start_conn(ft_server[0], ft_server[1]) if (status == 0): tkinter.messagebox.showerror( title="Error", message= "Couldn't correctly connect to FT server!\nCheck the ip configuration in Client.py, and restart the app!" )
class ClientApp(tk.Tk): def __init__(self, download_dir, source_dir): self.download_dir = download_dir tk.Tk.__init__(self) self.title("Torrent Sucks") self.geometry("500x500+300+100") self.resizable(False, True) self.config(bg="#474040") self.frame = tk.Frame(self, bg='#474040') self.frame.pack(side=tk.TOP, fill=tk.BOTH) self.search_bar = PlaceholderEntry(self.frame, "Please Enter the Filename", width=50) self.search_bar.pack(side=tk.LEFT, pady=20, padx=20) self.search_button = FocusButton(self.frame, text="Search", in_color="gray", out_color="black", fg="white", command=self.search) self.search_button.pack(side=tk.RIGHT, padx=20, pady=20) self.listbox_frame = tk.Frame(self, bg="#474040") self.listbox_frame.pack(side=tk.TOP, fill=tk.BOTH) self.listbox = tk.Listbox(self.listbox_frame, width=70, height=20) self.listbox.pack(side=tk.TOP, pady=20, padx=20) self.download_button = FocusButton(self.listbox_frame, text="Download", in_color="#52bf9c", out_color="#6ed1ff", fg="black", command=self.download) self.download_button.pack(side=tk.RIGHT, pady=20, padx=20) self.status_text = tk.Label(self.listbox_frame, text="", bg="#474040", font='Helvetica 10 bold') self.status_text.pack(side=tk.LEFT, pady=20, padx=20) #Start listener global host self.listener = Listener(host, source_dir) self.host, self.port = self.listener.get_self() print("Running On: {}:{}".format(self.host, self.port)) self.listen_thread = multiprocessing.Process( target=self.listener.listen) self.listen_thread.start() self.protocol("WM_DELETE_WINDOW", self.on_closing) self.search_file = "" #Start Sender self.sender = Sender(source_dir, self.host, self.port) global ft_server #Uncomment this if server is on print("Connecting to Server...") # print(ft_server[0]) # print(ft_server[1]) status = self.sender.start_conn(ft_server[0], ft_server[1]) if (status == 0): tkinter.messagebox.showerror( title="Error", message= "Couldn't correctly connect to FT server!\nCheck the ip configuration in Client.py, and restart the app!" ) # If server is not ready this will say "Could not connect to server" def search(self): filename = self.search_bar.get().strip() self.search_file = filename self.listbox.delete(0, tk.END) succ, status_message = self.sender.search(filename) if not succ: self.status_text.configure(text=status_message, fg="red") else: self.status_text.configure(text="") for result in self.sender.get_results(): self.listbox.insert(tk.END, result.strip()) def download(self): #<jpg, 54280, 07/30/2018, 127.0.0.1, 55682> data = self.listbox.get(tk.ACTIVE).strip()[1:-1].split(',') data = list(map(lambda str: str.strip(), data)) status_message = "Nothing happened" status_color = "gray" if (len(data) != 5): status_message = "Failed to download data format is not correct!" status_color = "red" else: req_file = { "name": self.search_file, #self.search_bar.get().strip() "type": data[0], "size": int(data[1]), "date": data[2], "ip": data[3], "port": int(data[4]), "dir": self.download_dir } succ, status_message = download_handler(req_file) if (succ): status_color = "green" else: status_color = "red" self.status_text.configure(text=status_message, fg=status_color) def on_closing(self): print("Closing!") self.sender.close() self.listen_thread.terminate() sys.exit()
def addRegFrame(self): self.reg_frame = tk.Frame(self, bg = '#3A79D1') self.username = PlaceholderEntry(self.reg_frame, placeholder = 'Username', relief = tk.RAISED) self.username.grid(row = 0, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.email = PlaceholderEntry(self.reg_frame, placeholder = 'Email', relief = tk.RAISED) self.email.grid(row = 0, column = 1, columnspan = 2, sticky = 'nsew', padx = 10, pady = 5) self.first_name = PlaceholderEntry(self.reg_frame, placeholder = 'First Name', relief = tk.RAISED) self.first_name.grid(row = 1, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.second_name = PlaceholderEntry(self.reg_frame, placeholder = 'Surname', relief = tk.RAISED) self.second_name.grid(row = 1, column = 1, columnspan = 2, sticky = 'nsew', padx = 10, pady = 5) self.city = PlaceholderEntry(self.reg_frame, placeholder = 'City', relief = tk.RAISED) self.city.grid(row = 2, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.street = PlaceholderEntry(self.reg_frame, placeholder = 'Street', relief = tk.RAISED) self.street.grid(row = 2, column = 1, sticky = 'nsew', padx = 10, pady = 5) self.house = PlaceholderEntry(self.reg_frame, placeholder = 'House', relief = tk.RAISED) self.house.grid(row = 2, column = 2, sticky = 'nsew', padx = 10, pady = 5) self.password = PasswordEntry(self.reg_frame, placeholder = 'Password', relief = tk.RAISED) self.password.grid(row = 3, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.password_confirmation = PasswordEntry(self.reg_frame, placeholder = 'Password Confirmation', width = 25, relief = tk.RAISED) self.password_confirmation.grid(row = 3, column = 1, sticky = 'nsew', padx = 10, pady = 5) self.add_btn = FocusButton(self.reg_frame, in_color = '#32bf1d', out_color = '#149600', text = 'Register User', fg = 'white', command = self.registerUser) self.add_btn.grid(row = 3, column = 2, sticky = 'nsew', padx = 10, pady = 5) self.reg_frame.pack(side = tk.TOP, fill = tk.X)
class UsersList(tk.Frame): def __init__(self, parent, list_of_users, list_of_ids, session, **kwargs): tk.Frame.__init__(self, parent, **kwargs) self.session = session self.list_of_ids = list_of_ids self.list_of_users = list_of_users self.addRegFrame() self.addUserList() def addUserList(self): try: url = "https://iguard-backend.herokuapp.com/api/v1/KDY7AehrzAlOVJd-i09GVA/users" response = self.session.get(url = url) except: Errors.networkConnectionError() return self.user_list = VerticalScrolledFrame(self, bg = 'white') self.user_list.interior.configure(bg = 'white') self.user_list.pack(side = tk.BOTTOM, fill = 'both', expand = True) self.user_frames = [] self.users = response.json() for user in self.users: self.addUser(user) def addUser(self, user): user_frame = tk.Frame(self.user_list.interior, bg = '#3A79D1') self.list_of_users.append(user['login']) self.list_of_ids.append(user['id']) login_label = tk.Label(user_frame, text = "login: {}".format(user['login']), font = 'Sans 11', height = 2, bg = '#3A79D1', fg = 'white') login_label.pack(side = tk.LEFT, fill = tk.Y, expand = True) name_label = tk.Label(user_frame, text = "Full Name: {} {}".format(user['name'], user['surname']), font = 'Sans 11', height = 2, bg = '#3A79D1', fg = 'white') name_label.pack(side = tk.LEFT, fill = tk.Y, expand = True) email_label = tk.Label(user_frame, text = "email: {}".format(user['email']), font = 'Sans 11', height = 2, bg = '#3A79D1', fg = 'white') email_label.pack(side = tk.LEFT, fill = tk.Y, expand = True) delete_btn = FocusButton(user_frame, text = 'Delete', in_color = '#f05454', out_color = '#c90404', fg = 'white', relief = tk.FLAT) delete_btn.configure(command = lambda id = user['id'], login = user['login'] : self.deleteUser(id, login)) delete_btn.pack(side = tk.RIGHT, fill = tk.Y, expand = False, pady = 10, padx = 10) user_frame.pack(side = tk.BOTTOM, fill = tk.X, expand = True, padx = 10, pady = 10) self.user_frames.append(user_frame) def findAndShowUser(self, username): for user_frame in self.user_frames: user_frame.pack_forget() for i in range(len(self.list_of_users)): if username in self.list_of_users[i]: self.user_frames[i].pack(side = tk.BOTTOM, fill = tk.X, expand = True, padx = 10, pady = 10) def deleteUser(self, id, login): if messagebox.askokcancel("Delete User", "Do you want to delete {}?".format(login), parent = self.user_list.interior): try: url = 'https://iguard-backend.herokuapp.com/api/v1/KDY7AehrzAlOVJd-i09GVA/user/{}'.format(id) res = self.session.delete(url = url) print(res.text) except: Errors.networkConnectionError() return for i, user in enumerate(self.list_of_users): if(user == login): self.user_frames[i].destroy() del self.user_frames[i] del self.list_of_users[i] del self.list_of_ids[i] break def addRegFrame(self): self.reg_frame = tk.Frame(self, bg = '#3A79D1') self.username = PlaceholderEntry(self.reg_frame, placeholder = 'Username', relief = tk.RAISED) self.username.grid(row = 0, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.email = PlaceholderEntry(self.reg_frame, placeholder = 'Email', relief = tk.RAISED) self.email.grid(row = 0, column = 1, columnspan = 2, sticky = 'nsew', padx = 10, pady = 5) self.first_name = PlaceholderEntry(self.reg_frame, placeholder = 'First Name', relief = tk.RAISED) self.first_name.grid(row = 1, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.second_name = PlaceholderEntry(self.reg_frame, placeholder = 'Surname', relief = tk.RAISED) self.second_name.grid(row = 1, column = 1, columnspan = 2, sticky = 'nsew', padx = 10, pady = 5) self.city = PlaceholderEntry(self.reg_frame, placeholder = 'City', relief = tk.RAISED) self.city.grid(row = 2, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.street = PlaceholderEntry(self.reg_frame, placeholder = 'Street', relief = tk.RAISED) self.street.grid(row = 2, column = 1, sticky = 'nsew', padx = 10, pady = 5) self.house = PlaceholderEntry(self.reg_frame, placeholder = 'House', relief = tk.RAISED) self.house.grid(row = 2, column = 2, sticky = 'nsew', padx = 10, pady = 5) self.password = PasswordEntry(self.reg_frame, placeholder = 'Password', relief = tk.RAISED) self.password.grid(row = 3, column = 0, sticky = 'nsew', padx = 10, pady = 5) self.password_confirmation = PasswordEntry(self.reg_frame, placeholder = 'Password Confirmation', width = 25, relief = tk.RAISED) self.password_confirmation.grid(row = 3, column = 1, sticky = 'nsew', padx = 10, pady = 5) self.add_btn = FocusButton(self.reg_frame, in_color = '#32bf1d', out_color = '#149600', text = 'Register User', fg = 'white', command = self.registerUser) self.add_btn.grid(row = 3, column = 2, sticky = 'nsew', padx = 10, pady = 5) self.reg_frame.pack(side = tk.TOP, fill = tk.X) def registerUser(self): params = { 'login' : self.username.get_data(), 'name' : self.first_name.get_data(), 'surname' : self.second_name.get_data(), 'email' : self.email.get_data(), 'city' : self.city.get_data(), 'street' : self.street.get_data(), 'house' : self.house.get_data(), 'password' : self.password.get_data(), 'password_confirmation' : self.password_confirmation.get_data() } try: url = "https://iguard-backend.herokuapp.com/api/v1/KDY7AehrzAlOVJd-i09GVA/register/user" res = dict(self.session.post(url = url, data = params).json()) except: Errors.networkConnectionError() return if "errors" in res.keys(): error_text = "" for error in res["errors"]: error_text += error + "\n" errorLabel = tk.Label(self, bg = 'red', fg = 'white', text = error_text) errorLabel.pack(side = tk.TOP, fill = tk.X) errorLabel.bind("<Button-1>", lambda e : errorLabel.destroy()) else: self.addUser(res)
class CamerasFrame(tk.Frame): def __init__(self, parent, bg='#3A79D1', **kwargs): tk.Frame.__init__(self, parent, bg=bg, **kwargs) self.list_of_users = [] self.list_of_ids = [] self.scrollable = VerticalScrolledFrame(self, bg='white', **kwargs) self.scrollable.interior.configure(bg='white') self.scrollable.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) self.vid_list = [] self.addVidForm() try: with open('config.json') as f: self.config = json.load(f) self.cameras = self.config['cameras'] except: Errors.configNotFoundError() return for i in range(len(self.cameras)): self.addVideoLabel(self.cameras[i]['video_source'], self.cameras[i]['id'], self.cameras[i]['user']) def findCamera(self, idx): for i in range(len(self.cameras)): if (self.cameras[i]['id'] == idx): return i return -1 def findByUser(self, username, show_all=False): for vid_frame in self.vid_list: vid_frame.pack_forget() if (show_all): for vid_frame in self.vid_list: vid_frame.pack(side=tk.BOTTOM, fill=tk.X, pady=10, expand=True, padx=10) else: for vid_frame in self.vid_list: if (vid_frame.owner == username): vid_frame.pack(side=tk.BOTTOM, fill=tk.X, pady=10, expand=True, padx=10) def addVideoLabel(self, vid_source, idx, username): vid_frame = CamFrame(parent=self.scrollable.interior, owner=username, borderwidth=5, bg='white') vid_frame.pack(side=tk.BOTTOM, fill=tk.X, pady=10, expand=True, padx=10) vid_label = tk.Label(vid_frame, text="Video Path : {}".format(vid_source), font='Sans 11', bg='white') username_label = tk.Label(vid_frame, text="User: {}".format(username), font='Sans 11', bg='white') vid_button = FocusButton(vid_frame, in_color='#049bc9', out_color='#3A79D1', text="Configure Lines", command=lambda idx=idx: self.drawLines(idx), fg='white', relief=tk.FLAT) del_button = FocusButton(vid_frame, in_color='#f05454', out_color='#c90404', text="Delete", command=lambda idx=idx: self.deleteVid(idx), fg='white', relief=tk.FLAT) del_button.pack(side=tk.RIGHT, padx=10) vid_button.pack(side=tk.RIGHT) vid_label.pack(side=tk.LEFT) username_label.pack(side=tk.LEFT, fill=tk.X, expand=True) self.vid_list.append(vid_frame) def drawLines(self, idx): id = self.findCamera(idx) video = Video(self.cameras[id]['video_source'], self.cameras[id]['line_source']) self.PaintApp = PaintApp(tk.Toplevel(self), video.getDrawFrame(), video.point_source) self.PaintApp.window.wm_attributes("-topmost", 1) def deleteVid(self, id): idx = self.findCamera(id) if messagebox.askokcancel("Delete", "Do you want to delete {}?".format( self.cameras[idx]['video_source']), parent=self): try: url = 'https://iguard-backend.herokuapp.com/api/v1/KDY7AehrzAlOVJd-i09GVA/camera/{}'.format( self.cameras[idx]['id']) with requests.Session() as session: req = session.delete(url=url) except: Errors.networkConnectionError() return res = dict(req.json()) if 'success' in res.keys(): del self.cameras[idx] self.updateConfig() self.vid_list[idx].destroy() del self.vid_list[idx] else: Errors.unknownError() return def addVidForm(self): self.vid_form = tk.Frame(self, borderwidth=5, bg='#3A79D1') self.vid_form.pack(side=tk.TOP, fill=tk.X, pady=10, padx=10, expand=False) self.vid_entry = PlaceholderEntry(self.vid_form, placeholder='Video Path', bg='white', font='Sans 11', width=40, relief=tk.RAISED) self.vid_entry.pack(side=tk.LEFT, padx=10) self.user_label = tk.Label(self.vid_form, text='Owner: ', font='Sans 11', bg='#3A79D1', fg='white') self.user_label.pack(side=tk.LEFT) list_of_items = self.list_of_users self.combobox = Combobox_Autocomplete(self.vid_form, list_of_items, relief=tk.RAISED, font='Sans 11', bg='white') self.combobox.pack(side=tk.LEFT) self.vid_form_btn = FocusButton(self.vid_form, in_color='#32bf1d', out_color='#149600', text='Add Video', relief=tk.FLAT, fg='white', command=self.addVideo) self.vid_form_btn.pack(side=tk.RIGHT) def addVideo(self): vid_path = self.vid_entry.get() self.vid_entry.clear() if len(vid_path) > 0: coord_path = "../datasets/coords{}_{}.yml".format( len(self.cameras), random.randint(1, 100)) video = Video(vid_path, coord_path) if video.getDrawFrame() is None: Errors.invalidVideoAdress() return login = self.combobox.get_value() user_idx = -1 for i in range(len(self.list_of_users)): if (self.list_of_users[i] == login): user_idx = i if (user_idx >= 0): req = None try: url = 'https://iguard-backend.herokuapp.com/api/v1/KDY7AehrzAlOVJd-i09GVA/register/camera?user_id={}'.format( self.list_of_ids[user_idx]) with requests.Session() as session: req = session.post(url=url) except: Errors.networkConnectionError() return res = dict(req.json()) if "errors" in res.keys(): error_text = "" for error in res["errors"]: error_text += error + "\n" messagebox.showerror("Error", error_text, parent=self) return print(res) camera = { 'id': res['id'], 'user': login, 'video_source': vid_path, 'line_source': coord_path } self.cameras.append(camera) self.addVideoLabel(video.video_source, res['id'], login) self.drawLines(res['id']) self.updateConfig() def updateConfig(self): self.config['cameras'] = self.cameras with open('config.json', 'w') as f: json.dump(self.config, f, indent=5)
class AuthWindow(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.geometry("350x450+500+150") self.title(window_config['window_title']) self.resizable(False, True) self.configure(background=window_config['bg_color']) try: self.img = PIL.ImageTk.PhotoImage( PIL.Image.open(window_config['center_img'])) self.img_label = tk.Label(self, image=self.img, borderwidth=0, highlightthickness=0) self.img_label.pack(side=tk.TOP) except: print("Coulnd't load image !") self.username_box = PlaceholderEntry( self, placeholder=window_config['username_text'], width=30, font=window_config['font']) self.username_box.pack(side=tk.TOP, pady=10) self.password_box = PasswordEntry( self, placeholder=window_config['password_text'], width=30, font=window_config['font']) self.password_box.pack(side=tk.TOP, pady=10) self.password_box.bind('<Return>', lambda e: self.loginCommand()) self.login_btn = tk.Button(self, text=window_config['loginbtn_text'], width=30, relief=tk.FLAT, command=self.loginCommand, bg='white') self.login_btn.pack(side=tk.TOP, pady=3) self.guest_btn = tk.Button(self, text=window_config['guestbtn_text'], width=30, relief=tk.FLAT, command=self.guestCommand, bg='white') self.guest_btn.pack(side=tk.TOP, pady=10) def loginCommand(self): global admin_session params = { 'username': self.username_box.get_data(), 'password': self.password_box.get_data() } url = 'https://iguard-backend.herokuapp.com/api/v1/KDY7AehrzAlOVJd-i09GVA/login' admin_session = requests.Session() try: response = admin_session.post(url, data=params) except: Errors.networkConnectionError() return response = dict(response.json()) if 'is_admin' not in response.keys(): Errors.authorizationError() else: if response['is_admin'] is None: Errors.notAdminAuthorizationError() else: global auth_state auth_state = 1 self.destroy() def guestCommand(self): global auth_state auth_state = 2 self.destroy()