-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.py
115 lines (97 loc) · 3.79 KB
/
interface.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
115
# -*- coding: utf-8 -*-
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import threading
from os import path
from platform import system
from .menubar import create_menus
from .tools import about, MainGrid
from .dialog import del_dir_dialog, Settings
from watcher.watcher import Watcher
import watcher
SYSTEM = system()
if SYSTEM == 'Linux':
from subprocess import Popen
elif SYSTEM == 'Windows':
from os import startfile
else:
watcher.mod.tell('Import Error')
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app, config):
Gtk.Window.__init__(self, title='SafeMyWork 1.0', application=app)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_border_width(5)
self.grid = MainGrid(self)
self.add(self.grid)
self.grid.switch_start.do_grab_focus(self.grid.switch_start)
create_menus(self)
self.config = config
self.watcher = Watcher(self.config)
self.timer = None
self.thread = None
self.initialize_config()
def initialize_config(self):
for watched_dir in self.config['watched_dirs']:
self.grid.watched_list.append_text(watched_dir)
def save_config(self, config):
watcher.mod.tell('Save config')
watcher.conf.save_config(config)
def watch(self, loop):
"""Launch watch"""
self.grid.spinner.start()
self.watcher.watch()
self.grid.spinner.stop()
if loop:
self.timer = threading.Timer(self.config['time_delta']*60, self.start_watching, args=(loop,))
self.timer.start()
def start_watching(self, loop):
"""Start watching : watch + timer"""
can = True
if loop:
can = self.grid.switch_start.get_active()
for thread in threading.enumerate():
if thread.name == 'watcher_loop' or thread.name == 'watcher_alone':
if thread.is_alive():
can = False
if can:
self.thread = threading.Thread(target=self.watch, name='watcher_loop', args=(loop,))
self.thread.start()
if loop:
self.grid.text.set_text('Surveillance active')
def stop_watching(self):
"""Cancel timer"""
if self.timer is not None:
self.timer.cancel()
self.grid.text.set_text('En attente...')
def abort_watch(self):
"""Abort current watch"""
self.watcher.stop = True
def show_saved(self, *args):
if SYSTEM == 'Linux':
Popen(['xdg-open', self.config['archive_dir']])
elif SYSTEM == 'Windows':
startfile(self.config['archive_dir'])
def settings(self, action, parameter):
dialog_settings = Settings(self)
dialog_settings.run()
def add_watched_dir(self, button):
tree_iter = self.grid.watched_list.get_active_iter()
if tree_iter is None:
new_dir = self.grid.watched_list.get_child().get_text()
if new_dir != '' and new_dir not in self.config['watched_dirs'] and path.exists(new_dir):
self.grid.watched_list.append_text(new_dir)
self.config['watched_dirs'].append(new_dir)
self.grid.text.set_text('Dossier ajouté')
def del_watched_dir(self, button):
tree_iter = self.grid.watched_list.get_active_iter()
if tree_iter is not None:
model = self.grid.watched_list.get_model()
directory = model[tree_iter][0]
must_del, dialog = del_dir_dialog(self, directory)
dialog.destroy()
if must_del:
self.config['watched_dirs'].remove(directory)
self.grid.watched_list.remove(int(self.grid.watched_list.get_active()))
self.grid.text.set_text('Dossier supprimé')