@@ -129,30 +129,44 @@ def get_active_connection_name():
129129 return None
130130
131131
132- def has_active_connection ():
133- """
134- Check if there is an active network connection
135132
133+
134+
135+ def has_saved_connection ():
136+ """
137+ Check if there is any saved (historical) NetworkManager connection profile.
138+
136139 Returns:
137- bool: True if there is an active connection, False otherwise
140+ bool: True if there is at least one saved connection, False otherwise
138141 """
142+ # Prefer nmcli; fallback to checking NM config directory
139143 try :
140144 result = subprocess .run (
141- ['nmcli' , '-t' , '-f' , 'TYPE,STATE, NAME' , 'connection' , 'show' , '--active ' ],
145+ ['nmcli' , '-t' , '-f' , 'NAME,TYPE ' , 'connection' , 'show' ],
142146 check = True ,
143147 stdout = subprocess .PIPE ,
144148 stderr = subprocess .PIPE ,
145149 text = True
146150 )
147- return bool (result .stdout .strip ())
151+ # Consider any saved connection; optionally prioritize wifi
152+ lines = [l for l in result .stdout .strip ().split ('\n ' ) if l ]
153+ if any (lines ):
154+ return True
148155 except subprocess .CalledProcessError as e :
149- logging .error (f"Command 'nmcli' failed with exit code { e .returncode } " )
150- logging .error (e .stderr )
151- return False
156+ logging .warning (f"nmcli connection show failed: { e .returncode } , fallback to config dir" )
152157 except Exception as e :
153- logging .error (f"An unexpected error occurred: { e } " )
154- return False
158+ logging .warning (f"nmcli connection show unexpected error: { e } " )
155159
160+ # Fallback: check NetworkManager system-connections directory
161+ config_dir = '/etc/NetworkManager/system-connections/'
162+ try :
163+ if os .path .isdir (config_dir ):
164+ for entry in os .scandir (config_dir ):
165+ if entry .is_file ():
166+ return True
167+ except Exception as e :
168+ logging .warning (f"Failed to scan saved connections in { config_dir } : { e } " )
169+ return False
156170def _get_info_nmcli ():
157171 """Get WiFi info using nmcli"""
158172 try :
0 commit comments