File: mainWindow.py

package info (click to toggle)
snapper-gui 0git.960a94834f-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 260 kB
  • sloc: python: 728; makefile: 4
file content (206 lines) | stat: -rw-r--r-- 8,841 bytes parent folder | download | duplicates (4)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from snappergui import snapper
import pkg_resources, subprocess, dbus
from snappergui.createSnapshot import createSnapshot
from snappergui.createConfig import createConfig
from snappergui.deleteDialog import deleteDialog
from snappergui.changesWindow import changesWindow
from snappergui.snapshotsView import snapshotsView
from gi.repository import Gtk
from time import strftime, localtime
from pwd import getpwuid

class SnapperGUI():
    """docstring for SnapperGUI"""

    def __init__(self, app):
        super(SnapperGUI, self).__init__()
        self.builder = Gtk.Builder()
        self.builder.add_from_file(pkg_resources.resource_filename("snappergui",
                                                                   "glade/mainWindow.glade"))
        self.statusbar = self.builder.get_object("statusbar")
        self.snapshotsTreeView = self.builder.get_object("snapstreeview")
        self.configsGroup = self.builder.get_object("configsGroup")
        self.window = self.builder.get_object("applicationwindow1")
        self.stack = self.builder.get_object("stack1")
        self.builder.connect_signals(self)

        self.window.set_application(app)

        self.configView = {}

        for config in snapper.ListConfigs():
            name = str(config[0])
            self.configView[name] = snapshotsView(name)
            self.stack.add_titled(self.configView[name].scrolledwindow, name, name)
            self.configView[name].selection.connect("changed",
                                                    self.on_snapshots_selection_changed)

        self.init_dbus_signal_handlers()
        self.window.show()

    def snapshot_columns(self,snapshot):
        if(snapshot[3] == -1):
            date = "Now"
        else:
            date = strftime("%a %x %R", localtime(snapshot[3]))
        return [snapshot[0],
                snapshot[1],
                snapshot[2],
                date,
                getpwuid(snapshot[4])[0],
                snapshot[5],
                snapshot[6]]

    def get_current_config(self):
        return self.stack.get_visible_child_name()

    def on_stack_visible_child_changed(self, stack, property):
        self.update_controlls_and_userdatatreeview()

    def on_snapshots_selection_changed(self, selection):
        self.update_controlls_and_userdatatreeview()

    def update_controlls_and_userdatatreeview(self):
        config = self.get_current_config()
        userdatatreeview = self.builder.get_object("userdatatreeview")
        if config is not None:
            model, paths = self.configView[config].selection.get_selected_rows()
        if config is None or len(paths) == 0:
            self.builder.get_object("snapshotActions").set_sensitive(False)
            userdatatreeview.set_model(None)
        else:
            self.builder.get_object("snapshotActions").set_sensitive(True)

            if len(paths) == 1 and not model.iter_has_child(model.get_iter(paths[0])):
                self.builder.get_object("view-changes").set_sensitive(False)
            else:
                self.builder.get_object("view-changes").set_sensitive(True)

            try:
                snapshot_data = snapper.GetSnapshot(config,model[model.get_iter(paths[0])][0])
                userdata_liststore = Gtk.ListStore(str, str)
                for key, value in snapshot_data[7].items():
                    userdata_liststore.append([key, value])
                userdatatreeview.set_model(userdata_liststore)
            except dbus.exceptions.DBusException:
                pass

    def on_create_snapshot(self, widget):
        dialog = createSnapshot(self.window, self.get_current_config())
        response = dialog.run()
        dialog.destroy()
        if response == Gtk.ResponseType.OK:
            newSnapshot = snapper.CreateSingleSnapshot(dialog.config,
                                        dialog.description,
                                        dialog.cleanup,
                                        dialog.userdata)
        elif response == Gtk.ResponseType.CANCEL:
            pass

    def on_create_config(self, widget):
        dialog = createConfig(self.window)
        response = dialog.run()
        dialog.destroy()
        if response == Gtk.ResponseType.OK:
            snapper.CreateConfig(dialog.name,
                                dialog.subvolume,
                                dialog.fstype,
                                dialog.template)
        elif response == Gtk.ResponseType.CANCEL:
            pass

    def on_delete_snapshot(self, widget):
        config = self.get_current_config()
        selection = self.configView[config].selection
        (model, paths) = selection.get_selected_rows()
        snapshots = []
        for path in paths:
            treeiter = model.get_iter(path)
            # avoid duplicates because post snapshots are always added
            if model[treeiter][0] not in snapshots:
                snapshots.append(model[treeiter][0])
            # if snapshot has post add that to delete
            if model.iter_has_child(treeiter):
                child_treeiter = model.iter_children(treeiter)
                snapshots.append(model[child_treeiter][0])
        dialog = deleteDialog(self.window, config,snapshots)
        response = dialog.run()
        if response == Gtk.ResponseType.YES and len(dialog.to_delete) > 0:
            snapper.DeleteSnapshots(config, dialog.to_delete)

    def on_open_snapshot_folder(self, widget):
        config = self.get_current_config()
        selection = self.configView[config].selection
        model, paths = selection.get_selected_rows()
        for path in paths:
            treeiter = model.get_iter(path)
            mountpoint = snapper.GetMountPoint(config, model[treeiter][0])
            if model[treeiter][6] != '':
                snapper.MountSnapshot(config,model[treeiter][0],'true')
            subprocess.Popen(['xdg-open', mountpoint])
            self.statusbar.push(True,
                "The mount point for the snapshot %s from %s is %s"%
                (model[treeiter][0], config, mountpoint))

    def on_viewchanges_clicked(self, widget):
        config = self.get_current_config()
        selection = self.configView[config].selection
        model, paths = selection.get_selected_rows()
        if len(paths) > 1:
            # open a changes window with the first and the last snapshot selected
            begin = model[paths[0]][0]
            end = model[paths[-1]][0]
            window = changesWindow(config, begin, end)
        elif len(paths) == 1 and model.iter_has_child(model.get_iter(paths[0])):
            # open a changes window with the selected pre snapshot and is's post
            child_iter = model.iter_children(model.get_iter(paths[0]))
            begin = model[paths[0]][0]
            end = model.get_value(child_iter,0)
            window = changesWindow(config, begin, end)

    def init_dbus_signal_handlers(self):
        signals = {
        "SnapshotCreated" : self.on_dbus_snapshot_created,
        "SnapshotModified" : self.on_dbus_snapshot_modified,
        "SnapshotsDeleted" : self.on_dbus_snapshots_deleted,
        "ConfigCreated" : self.on_dbus_config_created,
        "ConfigModified" : self.on_dbus_config_modified,
        "ConfigDeleted" : self.on_dbus_config_deleted
        }
        for signal, handler in signals.items():
            snapper.connect_to_signal(signal,handler)

    def on_dbus_snapshot_created(self,config,snapshot):
        self.statusbar.push(True, "Snapshot %s created for %s"% (str(snapshot), config))
        self.configView[config].add_snapshot_to_tree(str(snapshot))

    def on_dbus_snapshot_modified(self,config,snapshot):
        print("Snapshot SnapshotModified")

    def on_dbus_snapshots_deleted(self,config,snapshots):
        snaps_str = ""
        for snapshot in snapshots:
            snaps_str += str(snapshot) + " "
        self.statusbar.push(True, "Snapshots deleted from %s: %s"% (config, snaps_str))
        for deleted in snapshots:
            self.configView[config].remove_snapshot_from_tree(deleted)

    def on_dbus_config_created(self, config):
        self.configView[config] = snapshotsView(config)
        self.configView[config].update_view()
        self.stack.add_titled(self.configView[config]._TreeView, config, config)
        self.statusbar.push(5,"Created new configuration %s"% config)


    def on_dbus_config_modified(self,args):
        print("Config Modified")

    def on_dbus_config_deleted(self,args):
        print("Config Deleted")

    def on_main_destroy(self,args):
        for config in snapper.ListConfigs():
            for snapshot in snapper.ListSnapshots(config[0]):
                if snapshot[6] != '':
                    snapper.UmountSnapshot(config[0],snapshot[0],'true')