-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.py
More file actions
181 lines (149 loc) · 5.59 KB
/
Controller.py
File metadata and controls
181 lines (149 loc) · 5.59 KB
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
'''
@author: cgueret
'''
from gi.repository import GLib, Gtk
class Controller(object):
def __init__(self, view, model):
'''
Constructor
'''
# Keep a pointer to the view and the model
self._model = model
self._view = view
# Local variables
self._selected_entity_name = None
# Get a pointer to some objects from the interface
contact_list_obj = self._view.get_object('contacts_list')
button_newprop = self._view.get_object('button_newprop')
# Connect the signals
contact_list_obj.connect("cursor-changed", self._contact_selected_cb)
button_newprop.connect("clicked", self._new_prop_clicked_cb)
self._view.get_object('button_keep').connect("clicked", self._keep_clicked_cb)
# Add a timeout to update contacts and force first call
self._previous_contact_list = None
self._update_contacts_cb(contact_list_obj)
GLib.timeout_add_seconds(2, self._update_contacts_cb, contact_list_obj)
# Hack to clean the grid
self._grid_objs = []
def _contact_selected_cb(self, obj):
'''
Called when a contact is selected
'''
# Get the identifier of the entity the user selected
# If this fails, probably contacts have changed,
# so we update the list of contacts and exit
try:
selected_row = obj.get_selection().get_selected_rows()[1][0][0]
identifier = obj.get_model()[selected_row][0]
except:
self._update_contacts_cb(obj)
return
# Get the entity description from the model
self._selected_entity_name = identifier
# Refresh the display of the description
self.refresh_entity_description()
# Refresh the activation of the buttons
self.refresh_toolbar_buttons()
def _new_prop_clicked_cb(self, obj):
'''
Called when the button to add a new property/value is pressed
'''
# Get the selected property (return if none)
property_obj = self._view.get_object('combobox1')
tree_iter = property_obj.get_active_iter()
if tree_iter == None:
return
model = property_obj.get_model()
property = model[tree_iter][1]
# Get the value (return if blank
value = self._view.get_object('entry1').get_text()
if value == '':
return
# Add the property/value to the description of the entity
self._model.add_property(self._selected_entity_name, property, value)
# Clear the input field
self._view.get_object('entry1').set_text('')
# Refresh the display
self.refresh_entity_description()
def _delete_prop_clicked_cb(self, obj, params):
'''
Called when the button to delete a property/value is pressed
'''
# Delete the property/value to the description of the entity
self._model.delete_property(self._selected_entity_name, params['property'], params['value'])
# Refresh the display
self.refresh_entity_description()
def _update_contacts_cb(self, obj):
'''
Called on a regular basis to update the list of contacts available
'''
contacts = sorted(self._model.get_contacts())
if self._previous_contact_list == None or self._previous_contact_list != contacts:
self._previous_contact_list = contacts
contacts_list = obj.get_model()
contacts_list.clear()
for contact in contacts:
# By default, show the URN
contact_diplay_name = contact
# If the contact is ourselves, display "Me"
if contact == self._model.get_own_contact_name():
contact_diplay_name = 'Me'
# Add the entry
contacts_list.append(row=[contact, contact_diplay_name])
return True
def _keep_clicked_cb(self, obj):
'''
Called then the keep button is pressed
'''
self._model.cache_entity(self._selected_entity_name)
self.refresh_toolbar_buttons()
def refresh_entity_description(self):
'''
Display all the properties of an entity in a grid
'''
# Get the description
entity = self._model.get_entity_description(self._selected_entity_name)
# Get and clear the grid
grid_obj = self._view.get_object('grid1')
for obj in self._grid_objs:
grid_obj.remove(obj)
self._grid_objs = []
# Push the new content starting from the bottom with the new entry widgets
anchor_obj = self._view.get_object('combobox1')
for key, values in entity.iteritems():
for value in values:
# Create the widgets
label_key = Gtk.Label(key)
label_key.set_justify(Gtk.Justification.LEFT)
label_value = Gtk.Label(value)
label_value.set_justify(Gtk.Justification.LEFT)
label_value.set_line_wrap(True)
button = Gtk.Button(stock=Gtk.STOCK_DELETE)
# Pack them
grid_obj.attach_next_to(label_key, anchor_obj, Gtk.PositionType.TOP, 1, 1)
grid_obj.attach_next_to(label_value, label_key, Gtk.PositionType.RIGHT, 1, 1)
grid_obj.attach_next_to(button, label_value, Gtk.PositionType.RIGHT, 1, 1)
# Move to the next line
anchor_obj = label_key
# Connect click handler for property deletion
params = {'property' : key, 'value' : value}
button.connect("clicked", self._delete_prop_clicked_cb, params)
# Hack to clean the grid
self._grid_objs.append(label_key)
self._grid_objs.append(label_value)
self._grid_objs.append(button)
# Show the content of the new grid
grid_obj.show_all()
def refresh_toolbar_buttons(self):
'''
Update the status of the buttons in the toolbar depending on the contact selected
'''
# Keep button is enabled by default...
keep_button_obj = self._view.get_object('button_keep')
keep_button_obj.set_sensitive(True)
# disabled for the owner profile
if self._selected_entity_name == self._model.get_own_contact_name():
keep_button_obj.set_sensitive(False)
# and disabled for contacts that are already in cache
if self._model.is_cached(self._selected_entity_name):
keep_button_obj.set_sensitive(False)