From 7efce795b0c362c383f59130b661187a304b62ff Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Tue, 1 Apr 2014 18:41:30 -0700 Subject: [PATCH] Split profile editing across UI and core logic Signed-off-by: Sean Robinson --- wifiradar/__init__.py | 21 +++++++++ wifiradar/gui/g2/__init__.py | 102 ++++++++++++------------------------------- 2 files changed, 49 insertions(+), 74 deletions(-) diff --git a/wifiradar/__init__.py b/wifiradar/__init__.py index df19193..4fd4aeb 100644 --- a/wifiradar/__init__.py +++ b/wifiradar/__init__.py @@ -134,6 +134,27 @@ def main(config): config.remove_section(apname) self.config.auto_profile_order.remove(apname) msg_pipe.send(Message('PROFILE-UNLIST', profile)) + elif msg.topic == 'PROFILE-REPLACE': + new_profile, old_profile = msg.details + 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 config.profiles(): + old_position = config.auto_profile_order.index(apname) + config.remove_section(apname) + 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']) + config.set_section(apname, profile) + config.auto_profile_order.insert(old_position, apname) + msg_pipe.send(Message('PROFILE-UNLIST', old_profile)) + msg_pipe.send(Message('PROFILE-UPDATE', new_profile)) + msg_pipe.send(Message('PROFILE-MOVE', (new_profile, old_position))) else: logger.warning('unrecognized Message: "{}"'.format(msg)) ui_proc.join() diff --git a/wifiradar/gui/g2/__init__.py b/wifiradar/gui/g2/__init__.py index 8a48364..124e81b 100644 --- a/wifiradar/gui/g2/__init__.py +++ b/wifiradar/gui/g2/__init__.py @@ -269,6 +269,11 @@ class RadarWindow: self.update_plist_items(msg.details) elif msg.topic == 'PROFILE-UNLIST': self.remove_profile(msg.details) + elif msg.topic == 'PROFILE-MOVE': + profile, new_position = msg.details + old_position = self.get_row_by_ap(profile['essid'], + profile['bssid']) + self.pstore.move_before(old_position, new_position) elif msg.topic == 'PREFS-EDIT': self.edit_preferences() else: @@ -484,81 +489,30 @@ class RadarWindow: apname = misc.make_section_name(essid, '') else: # AP list says this is NOT a roaming profile - apname = misc.make_section_name(essid, bssid) - profile = self.config.get_profile(apname) - if profile: - # A profile was found in the config file - profile['bssid'] = self.access_points[apname]['bssid'] - profile_editor = profile_ed.ProfileEditor(self, profile) - try: - # try editing the profile - edited_profile = profile_editor.run() - except ValueError: - error_dlg = transients.ErrorDialog(profile_editor.dialog, "Cannot save empty ESSID") - del error_dlg - return False - finally: - # Always remove profile editor window from screen - profile_editor.destroy() - if edited_profile: - # A profile was returned by the editor - old_index = None - row = None - if edited_profile['essid'] != profile['essid'] or edited_profile['bssid'] != profile['bssid'] or edited_profile['roaming'] != profile['roaming']: - # ESSID, BSSID, or roaming was changed in profile editor - self.msg_pipe.send(Message('SCAN-STOP', '')) - if profile['roaming']: - # The old profile was a roaming profile - old_ap = misc.make_section_name(profile['essid'], '') - else: - # The old profile was NOT a roaming profile - old_ap = misc.make_section_name(profile['essid'], profile['bssid']) - # Find where old profile was in auto order - old_index = self.config.auto_profile_order.index(old_ap) - # Remove old profile and get its place in AP list - row = self.delete_profile(old_ap) - self.msg_pipe.send(Message('PROFILE-REMOVE', old_ap)) - # Add AP to the list displayed to user - self.msg_pipe.send(Message('PROFILE-UPDATE', edited_profile)) - self.msg_pipe.send(Message('SCAN-START', '')) - if edited_profile['roaming']: - # New profile is a roaming profile - apname = misc.make_section_name(edited_profile['essid'], '') - ap_display = edited_profile['essid'] + "\n" + ' Multiple APs' - # Remove all other profiles that match the new profile ESSID - while True: - prow_iter = self.get_row_by_ap(edited_profile['essid']) - if prow_iter: - self.pstore.remove(prow_iter) - else: - break - else: - # New profile is NOT a roaming profile - apname = misc.make_section_name(edited_profile['essid'], edited_profile['bssid']) - ap_display = edited_profile['essid'] + "\n" + edited_profile['bssid'] - # Insert the new profile in the same position as the one being replaced - if old_index is not None: - # Old profile was in auto order list - self.config.auto_profile_order.insert(old_index, apname) - if (( row is not None) and (self.pstore.iter_is_valid(row)) ): - self.pstore.insert_before(row, [ap_display, None, None, None, None, None, None, None, None]) - self.access_points[apname] = edited_profile - self.config.set_section(apname, edited_profile) - try: - # Save updated profile to config file - self.config.write() - except IOError as e: - if e.errno == errno.ENOENT: - error_dlg = transients.ErrorDialog(self.window, "Could not save configuration file:\n%s\n\n%s" % (self.config.filename, e.strerror)) - del error_dlg - else: - raise e + apname = make_section_name(essid, bssid) + self.msg_pipe(Message('PROFILE-EDIT-REQUEST', apname)) + + def edit_profile(self, profile): + """ 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: - # The AP does not already have a profile - profile = misc.get_new_profile() - profile['essid'] = self.pstore.get_value(selected_iter, 0) - profile['bssid'] = self.pstore.get_value(selected_iter, 1) - self.create_new_profile(widget, profile, data) + # 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() def delete_profile(self, apname): """ Delete the profile associated with :data:`apname` (i.e. make -- 2.11.4.GIT