From 82a1e9ac26b22b01c677b1247c3dd2f84b30bcfd Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Sun, 6 Apr 2014 10:49:36 -0700 Subject: [PATCH] Unify profile update and profile replace concepts This also changes the order of parameters in PROFILE-MOVE messages. Signed-off-by: Sean Robinson --- wifiradar/__init__.py | 64 +++++++++++++++++++------------------------- wifiradar/gui/g2/__init__.py | 29 +++++++------------- 2 files changed, 37 insertions(+), 56 deletions(-) diff --git a/wifiradar/__init__.py b/wifiradar/__init__.py index 6b74755..7b315de 100644 --- a/wifiradar/__init__.py +++ b/wifiradar/__init__.py @@ -136,9 +136,7 @@ class Main(object): elif msg.topic == 'PROFILE-EDIT-REQUEST': essid, bssid = msg.details self._profile_edit_request(essid, bssid) - elif msg.topic == 'PROFILE-UPDATE': - self._profile_update(msg.details) - elif msg.topic == 'PROFILE-REPLACE': + elif msg.topic == 'PROFILE-EDITED': new_profile, old_profile = msg.details self._profile_replace(new_profile, old_profile) elif msg.topic == 'PROFILE-REMOVE': @@ -172,18 +170,35 @@ class Main(object): profile['bssid'] = bssid self.msg_pipe.send(Message('PROFILE-EDIT', profile)) - def _profile_update(self, profile): - """ + def _profile_replace(self, new_profile, old_profile): + """ Update :data:`old_profile` with :data:`new_profile`. """ - if profile['roaming']: - apname = make_section_name(profile['essid'], '') + new_apname = make_section_name(new_profile['essid'], + new_profile['bssid']) + old_apname = make_section_name(old_profile['essid'], + old_profile['bssid']) + if old_apname == new_apname: + # Simple update of old_profile with new_profile. + self.config.set_section(new_apname, new_profile) + self.msg_pipe.send(Message('PROFILE-UPDATE', new_profile)) else: - apname = make_section_name(profile['essid'], profile['bssid']) - if apname not in self.config.profiles(): - self.config.set_section(apname, profile) - self.config.auto_profile_order.insert(0, apname) - self.msg_pipe.send(Message('PROFILE-UPDATE', profile)) - self.msg_pipe.send(Message('PROFILE-MOVE', (profile, 0))) + # Replace old_profile with new_profile. + old_position = self._profile_remove(old_apname) + # Add the updated profile like it's new, but in the old position. + self.config.auto_profile_order.insert(old_position, new_apname) + self.config.set_section(new_apname, new_profile) + self.msg_pipe.send(Message('PROFILE-UPDATE', new_profile)) + self.msg_pipe.send(Message('PROFILE-MOVE', (old_position, new_profile))) + if old_profile['known'] is False and new_profile['known'] is True: + # The profile has been upgraded from scanned to configured. + self.config.auto_profile_order.insert(0, new_apname) + self.msg_pipe.send(Message('PROFILE-MOVE', (0, new_profile))) + try: + self.config.write() + except IOError as e: + self.msg_pipe.send(Message('ERROR', + 'Could not save configuration file:\n' + + '{}\n\n{}'.format(self.config.filename, e.strerror))) def _profile_remove(self, apname): """ Remove the profile named in :data:`apname`. This method returns @@ -207,29 +222,6 @@ class Main(object): '{}\n\n{}'.format(self.config.filename, e.strerror))) return position - def _profile_replace(self, new_profile, old_profile): - """ - """ - if old_profile['roaming']: - apname = make_section_name(old_profile['essid'], '') - else: - apname = make_section_name(old_profile['essid'], - old_profile['bssid']) - if apname in self.config.profiles(): - old_position = self.config.auto_profile_order.index(apname) - self.config.remove_section(apname) - self.config.auto_profile_order.remove(apname) - if old_profile['roaming']: - apname = make_section_name(old_profile['essid'], '') - else: - apname = make_section_name(old_profile['essid'], - old_profile['bssid']) - self.config.set_section(apname, new_profile) - self.config.auto_profile_order.insert(old_position, apname) - self.msg_pipe.send(Message('PROFILE-UNLIST', old_profile)) - self.msg_pipe.send(Message('PROFILE-UPDATE', new_profile)) - self.msg_pipe.send(Message('PROFILE-MOVE', (new_profile, old_position))) - def _preferences_edit_request(self): """ Pass a :class:`ConfigManager` to the UI for editing. """ diff --git a/wifiradar/gui/g2/__init__.py b/wifiradar/gui/g2/__init__.py index c6b2e72..5abd4b7 100644 --- a/wifiradar/gui/g2/__init__.py +++ b/wifiradar/gui/g2/__init__.py @@ -277,7 +277,7 @@ class RadarWindow: with gtk.gdk.lock: self.remove_profile(msg.details) elif msg.topic == 'PROFILE-MOVE': - profile, new_position = msg.details + new_position, profile = msg.details with gtk.gdk.lock: if profile['roaming']: old_position = self.get_row_by_ap(profile['essid']) @@ -487,7 +487,7 @@ class RadarWindow: self.msg_pipe.send(Message('ERROR', 'Cannot save empty ESSID')) else: if profile: - self.msg_pipe.send(Message('PROFILE-UPDATE', profile)) + self.msg_pipe.send(Message('PROFILE-EDITED', profile)) finally: profile_editor.destroy() @@ -509,24 +509,13 @@ class RadarWindow: """ Allow the user to edit :data:`profile`. """ profile_editor = profile_ed.ProfileEditor(self, profile) - try: - # Try editing the profile. - edited_profile = profile_editor.run() - except ValueError: - self.msg_pipe.send(Message('ERROR', 'Cannot save empty ESSID')) - else: - if edited_profile is not None: - # A profile was returned by the editor. - self.msg_pipe.send(Message('SCAN-STOP', '')) - # Replace old profile. - self.msg_pipe.send(Message('PROFILE-REPLACE', - (edited_profile, profile))) - self.msg_pipe.send(Message('SCAN-START', '')) - # Request saving the configuration file after the update. - self.msg_pipe.send(Message('CONFIG-SAVE', '')) - finally: - # Always remove profile editor window from screen - profile_editor.destroy() + edited_profile = profile_editor.run() + profile_editor.destroy() + + if edited_profile is not None: + # Replace old profile. + self.msg_pipe.send(Message('PROFILE-EDITED', + (edited_profile, profile))) def delete_profile(self, apname): """ Delete the profile associated with :data:`apname` (i.e. make -- 2.11.4.GIT