emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 2dad73c 13/13: Merge commit '9893d7f17a2ee7f83587c305c256b


From: Ian Dunn
Subject: [elpa] master 2dad73c 13/13: Merge commit '9893d7f17a2ee7f83587c305c256bd1300995125'
Date: Sat, 15 Apr 2017 16:02:46 -0400 (EDT)

branch: master
commit 2dad73c22f2ac19899a40c68e3f16b2f56d932ae
Merge: c30e4e1 9893d7f
Author: Ian Dunn <address@hidden>
Commit: Ian Dunn <address@hidden>

    Merge commit '9893d7f17a2ee7f83587c305c256bd1300995125'
---
 packages/enwc/enwc-backend.el |  10 +-
 packages/enwc/enwc-nm.el      |  87 +++++------
 packages/enwc/enwc-test.el    |  66 +++++++++
 packages/enwc/enwc-wicd.el    |  12 +-
 packages/enwc/enwc.el         | 325 ++++++++++++++++++++++++------------------
 5 files changed, 309 insertions(+), 191 deletions(-)

diff --git a/packages/enwc/enwc-backend.el b/packages/enwc/enwc-backend.el
index 076907f..a8b8f92 100644
--- a/packages/enwc/enwc-backend.el
+++ b/packages/enwc/enwc-backend.el
@@ -39,7 +39,7 @@
   ;; Scan interface
   network-ids
   scan
-  wireless-nw-props
+  nw-props
   ;; Connect/disconnect
   connect
   disconnect
@@ -98,11 +98,11 @@ Signals an error if a backend with KEY already exists and 
FORCEP is nil."
 (defun enwc--scan (backend)
   (funcall (enwc-backend-scan backend)))
 
-(defun enwc--network-ids (backend)
-  (funcall (enwc-backend-network-ids backend)))
+(defun enwc--network-ids (backend &optional wired-p)
+  (funcall (enwc-backend-network-ids backend) wired-p))
 
-(defun enwc--wireless-nw-props (backend id)
-  (funcall (enwc-backend-wireless-nw-props backend) id))
+(defun enwc--nw-props (backend id &optional wired-p)
+  (funcall (enwc-backend-nw-props backend) id wired-p))
 
 (defun enwc--connect (backend id &optional wired-p)
   (funcall (enwc-backend-connect backend) id wired-p))
diff --git a/packages/enwc/enwc-nm.el b/packages/enwc/enwc-nm.el
index b3275e0..391f4ee 100644
--- a/packages/enwc/enwc-nm.el
+++ b/packages/enwc/enwc-nm.el
@@ -197,24 +197,21 @@
 (defun enwc-nm-get-device-by-name (name)
   (enwc-nm-dbus-default-call-method "GetDeviceByIpIface" :string name))
 
-(defun enwc-nm--ap-to-conn (ap)
-  "Get the connection that corresponds to AP."
-  (let ((ssid (dbus-byte-array-to-string (enwc-nm-get-wireless-network-property
-                                          ap "Ssid")))
-        (conns (enwc-nm-list-connections))
-        conn)
-    (while (and conns (not conn))
-      (setq conn (pop conns))
+(defun enwc-nm--ap-to-conn (nw)
+  "Get the connection that corresponds to NW."
+  (let ((ap-ssid (dbus-byte-array-to-string
+                  (enwc-nm-get-wireless-network-property nw "Ssid")))
+        (profile-table (make-hash-table :test #'equal)))
+    ;; Create a hash table of connections, indexed by ssid
+    ;; TODO: Store this somewhere else
+    (dolist (conn (enwc-nm-list-connections))
       (let ((settings (enwc-nm-get-settings conn)))
-        (if settings
-            (let ((this-ssid (enwc-nm-get-dbus-dict-entry 
"802-11-wireless/ssid"
-                                                          settings)))
-              (unless (and this-ssid
-                           (string-equal (dbus-byte-array-to-string this-ssid)
-                                         ssid))
-                (setq conn nil)))
-          (setq conn nil))))
-    conn))
+        (map-put profile-table
+                 (dbus-byte-array-to-string (enwc-nm-get-dbus-dict-entry
+                                             "802-11-wireless/ssid"
+                                             settings))
+                 conn)))
+    (map-elt profile-table ap-ssid)))
 
 (defun enwc-nm-connection-p (conn)
   "Return non-nil if CONN is a connection object."
@@ -222,6 +219,12 @@
        (stringp conn)
        (string-match "^/org/freedesktop/NetworkManager/Settings/[0-9]+$" 
conn)))
 
+(defun enwc-nm--profile-wired-p (conn)
+  "Return non-nil if CONN is a wired profile."
+  (let ((props (enwc-nm-get-settings conn)))
+    (string= (enwc-nm-get-dbus-dict-entry "connection/type" props)
+             "802-3-ethernet")))
+
 ;;;;;;;;;;;;;;;;;;
 ;; Get networks ;;
 ;;;;;;;;;;;;;;;;;;
@@ -238,19 +241,12 @@ This returns a list of D-Bus paths to the access points."
 
 (defun enwc-nm-get-wired-profiles ()
   (let ((profs-list (enwc-nm-list-connections)))
-    (mapcar
-     (lambda (x)
-       (let ((props (enwc-nm-get-settings x)))
-         (when (string= (enwc-nm-get-dbus-dict-entry "connection/type" props)
-                        "802-3-ethernet")
-           (enwc-nm-get-dbus-dict-entry "connection/id" props))))
-     profs-list)))
+    (cl-remove-if-not #'enwc-nm--profile-wired-p profs-list)))
 
 ;;;;;;;;;;;;;
 ;; Connect ;;
 ;;;;;;;;;;;;;
 
-;; Default
 (defun enwc-nm-connect (nw &optional wired)
   "The NetworkManager connect function.
 This gets the connection path from NW, and connects to it."
@@ -259,29 +255,11 @@ This gets the connection path from NW, and connects to 
it."
     (enwc-nm-wireless-connect nw)))
 
 (defun enwc-nm-wireless-connect (nw)
-  (let ((ap-ssid (dbus-byte-array-to-string
-                  (dbus-get-property :system
-                                     enwc-nm-dbus-service
-                                     nw
-                                     
"org.freedesktop.NetworkManager.AccessPoint"
-                                     "Ssid")))
-        (profile-table (make-hash-table :test #'equal)))
-    ;; Create a hash table of connections, indexed by ssid
-    ;; TODO: Store this somewhere else
-    (dolist (conn (enwc-nm-list-connections))
-      (let ((settings (dbus-call-method :system
-                                        enwc-nm-dbus-service
-                                        conn
-                                        
"org.freedesktop.NetworkManager.Settings.Connection"
-                                        "GetSettings")))
-        (map-put profile-table
-                 (dbus-byte-array-to-string (caadr (assoc-string "ssid" (cadr 
(assoc-string "802-11-wireless" settings)))))
-                 conn)))
-    (when-let (conn (map-elt profile-table ap-ssid))
-      (enwc-nm-dbus-default-call-method "ActivateConnection"
-                                        :object-path conn
-                                        :object-path enwc-nm-wireless-dev
-                                        :object-path conn))))
+  (when-let ((conn (enwc-nm--ap-to-conn nw)))
+    (enwc-nm-dbus-default-call-method "ActivateConnection"
+                                      :object-path conn
+                                      :object-path enwc-nm-wireless-dev
+                                      :object-path conn)))
 
 (defun enwc-nm-wired-connect (nw)
   (enwc-nm-dbus-default-call-method "ActivateConnection"
@@ -369,6 +347,15 @@ If both are 0, then it returns WEP, otherwise WPA."
         "WEP"
       "WPA")))
 
+(defun enwc-nm-get-wired-nw-props (nw)
+  (let ((settings (enwc-nm-get-settings nw)))
+    `((name . ,(enwc-nm-get-dbus-dict-entry "connection/id" settings)))))
+
+(defun enwc-nm-get-nw-props (nw &optional wired-p)
+  (if wired-p
+      (enwc-nm-get-wired-nw-props nw)
+    (enwc-nm-get-wireless-nw-props nw)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Get Current network id ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -488,7 +475,7 @@ representing another layer in the dictionary."
       (setq cur-ent (assoc cur-str cur-ent))
       (when cur-ent
         (setq cur-ent (cadr cur-ent))))
-    cur-ent))
+    (when cur-ent (car cur-ent))))
 
 (defun enwc-nm--recurse-dbus-entry (dict value entries)
   "Look in DICT for ENTRIES, and set the final one to VALUE."
@@ -730,7 +717,7 @@ Unregister all of the D-Bus signals set up during load."
   :disconnect #'enwc-nm-disconnect
   :current-nw-id #'enwc-nm-get-current-nw-id
   :is-connecting-p #'enwc-nm-check-connecting
-  :wireless-nw-props #'enwc-nm-get-wireless-nw-props
+  :nw-props #'enwc-nm-get-nw-props
   :is-wired-p #'enwc-nm-is-wired))
 
 (provide 'enwc-nm)
diff --git a/packages/enwc/enwc-test.el b/packages/enwc/enwc-test.el
new file mode 100644
index 0000000..5b3c6f1
--- /dev/null
+++ b/packages/enwc/enwc-test.el
@@ -0,0 +1,66 @@
+;;; enwc-test.el --- Tests for ENWC
+
+;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
+
+;; Author: Ian Dunn <address@hidden>
+;; Keywords: external, network, wicd, manager, nm
+;; Version: 2.0
+;; Package-Requires: ((emacs "25.1"))
+;; Homepage: https://savannah.nongnu.org/p/enwc
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'enwc)
+(require 'ert)
+
+;; Test enwc-setup
+
+(ert-deftest enwc-test-select-interface-save ()
+  ;; Test saving the interfaces and using the ip selection back-end
+  (let ((enwc-interface-list-function 'enwc--ip-interface-list)
+        (enwc-ask-to-save-interfaces t)
+        ;; Temporarily save our changes to a different file
+        (custom-file (make-temp-file "enwc-custom-"))
+        (enwc-wired-device "")
+        (enwc-wireless-device ""))
+    (enwc--setup-select-interfaces)
+    (should (not (string-empty-p enwc-wired-device)))
+    (should (not (string-empty-p enwc-wireless-device)))
+    ;; Test whether the interfaces were saved
+    (let ((enwc-wired-device "")
+          (enwc-wireless-device ""))
+      (load custom-file)
+      (should (not (string-empty-p enwc-wired-device)))
+      (should (not (string-empty-p enwc-wireless-device))))
+    ;; Delete our temporary custom file
+    (delete-file custom-file)))
+
+(ert-deftest enwc-test-select-interface-no-save ()
+  ;; Test not saving the interfaces and using the ifconfig selection back-end
+  (let ((enwc-interface-list-function 'enwc--ifconfig-interface-list)
+        (enwc-ask-to-save-interfaces nil)
+        ;; Temporarily save our "changes" to a different file
+        (custom-file (make-temp-file "enwc-custom-"))
+        (enwc-wired-device "")
+        (enwc-wireless-device ""))
+    (enwc--setup-select-interfaces)
+    (should (not (string-empty-p enwc-wired-device)))
+    (should (not (string-empty-p enwc-wireless-device)))
+    ;; Test whether the interfaces were saved
+    (let ((enwc-wired-device "")
+          (enwc-wireless-device ""))
+      (load custom-file)
+      (should (string-empty-p enwc-wired-device))
+      (should (string-empty-p enwc-wireless-device)))
+    ;; Delete our temporary custom file
+    (delete-file custom-file)))
+
+(provide 'enwc-test)
+
+;; Local Variables:
+;; no-byte-compile: t
+;; no-update-autoloads: t
+;; End:
+;;; enwc-test.el ends here
diff --git a/packages/enwc/enwc-wicd.el b/packages/enwc/enwc-wicd.el
index 011f91e..f0e157c 100644
--- a/packages/enwc/enwc-wicd.el
+++ b/packages/enwc/enwc-wicd.el
@@ -218,6 +218,16 @@ property from wireless network with id ID."
   "Get property DET from the wired network with id ID."
   (enwc-wicd-dbus-wired-call-method "GetWiredProperty" id det))
 
+(defun enwc-wicd-get-wired-nw-props (id)
+  ;; TODO Do wicd wired profiles have names?
+  (ignore id)
+  `((name . "Wired Profile")))
+
+(defun enwc-wicd-get-network-props (id &optional wired)
+  (if wired
+      (enwc-wicd-get-wired-nw-props id)
+    (enwc-wicd-get-wireless-network-props id)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;
 ;; Connect Functions ;;
 ;;;;;;;;;;;;;;;;;;;;;;;
@@ -465,7 +475,7 @@ Unregister all of the D-Bus signals set up during load."
   :disconnect #'enwc-wicd-disconnect
   :current-nw-id #'enwc-wicd-get-current-nw-id
   :is-connecting-p #'enwc-wicd-check-connecting
-  :wireless-nw-props #'enwc-wicd-get-wireless-network-props
+  :nw-props #'enwc-wicd-get-network-props
   :is-wired-p #'enwc-wicd-is-wired))
 
 (provide 'enwc-wicd)
diff --git a/packages/enwc/enwc.el b/packages/enwc/enwc.el
index 78a9c33..088f340 100644
--- a/packages/enwc/enwc.el
+++ b/packages/enwc/enwc.el
@@ -4,7 +4,7 @@
 
 ;; Author: Ian Dunn <address@hidden>
 ;; Keywords: external, network, wicd, manager, nm
-;; Version: 2.0beta2
+;; Version: 2.0
 ;; Package-Requires: ((emacs "25.1"))
 ;; Homepage: https://savannah.nongnu.org/p/enwc
 
@@ -49,9 +49,6 @@
 ;;; TODO:
 ;;
 ;; - Add hooks for scan completion, and possibly upon network connection.
-;; - Wired uses profiles, not networks; Refer to them as such
-;; - Is an association list the best idea for scan results?  Perhaps a 
structure
-;;   would work better?
 
 ;;; Code:
 
@@ -71,12 +68,18 @@
   :group 'external)
 
 (defcustom enwc-wireless-device ""
-  "The wireless device to use for ENWC."
+  "The wireless device/interface to use for ENWC.
+
+If this is unset when `enwc-setup' is called, the user will be
+prompted for an interface."
   :group 'enwc
   :type 'string)
 
 (defcustom enwc-wired-device ""
-  "The wired device to use for ENWC."
+  "The wired device/interface to use for ENWC.
+
+If this is unset when `enwc-setup' is called, the user will be
+prompted for an interface."
   :group 'enwc
   :type 'string)
 
@@ -86,44 +89,71 @@ The specific information can be set using 
`enwc-mode-line-format'."
   :group 'enwc
   :type 'boolean)
 
-(defcustom enwc-auto-scan nil
-  "Whether or not to have ENWC automatically scan.
-If non-nil, then ENWC will automatically scan for
-networks every `enwc-auto-scan-interval' seconds."
+(defcustom enwc-enable-auto-scan-on-startup nil
+  "Whether to enable auto-scan during `enwc-setup'.
+
+If non-nil, ENWC will automatically scan for networks every
+`enwc-auto-scan-interval' seconds.
+
+To enable auto-scan after startup, use `enwc-enable-auto-scan'."
   :group 'enwc
   :type 'boolean)
 
+(defvar enwc--auto-scan nil
+  "Current state of auto-scan.
+
+To enable auto-scan, use `enwc-enable-auto-scan'.
+
+To enable auto-scan at startup, set
+`enwc-enable-auto-scan-on-startup'.")
+
 (defcustom enwc-auto-scan-interval 20
   "The interval between automatic scans.
 
-To make any changes to this variable take effect, use
-`enwc-restart-auto-scan'."
+To make any changes to this variable take effect outside of the
+customization interface, use `enwc-restart-auto-scan'."
   :group 'enwc
   :type 'integer)
 
 (defcustom enwc-mode-line-format " [%s%%] "
-  "The format for displaying the mode line.
+  "The format for displaying information in the mode line.
+
+The following format specifiers display information about the
+current wireless connection:
+
+%s = signal strength
 
-%s = The current signal strength.  If wired, then this is set to 100.
+%e = essid
 
-%e = The essid of the current network.  If wired, then this set to 'Wired'
+%b = bssid
 
-%b = The bssid of the current network.  If using a wired connection, then this
-is set to 'Wired'.
+%n = encryption type
 
-%n = The encryption type of the current network, or 'Wired' if using a wired
-connection.
+%c = channel
 
-%c = The channel of the current network, or 'Wired' if using a wired 
connection.
+When a wired connection is active, signal strength is 100, essid
+and bssid are \"Wired\", and encryption and channel are \"None\".
+
+The following format specifiers are also significant:
 
 %% = A Normal '%'"
   :group 'enwc
   :type 'string)
 
+(defcustom enwc-warn-if-already-setup t
+  "Whether to warn the user if ENWC is already setup when calling 
`enwc-setup'."
+  :group 'enwc
+  :type 'boolean)
+
+(defcustom enwc-ask-to-save-interfaces t
+  "Whether to ask about saving changes to the network interfaces during 
`enwc-setup'."
+  :group 'enwc
+  :type 'boolean)
+
 (defvar enwc-display-string " [0%] "
   "The mode line display string.
-This is altered every second to display the current network strength
-in `enwc-update-mode-line'.")
+
+This is updated after every scan using `enwc-update-mode-line'.")
 
 (defun enwc--print-strength (s)
   "Convert signal strength S to a string to dispay."
@@ -141,7 +171,7 @@ in `enwc-update-mode-line'.")
 (cl-defstruct enwc-column-spec ()
   detail display sorter width conv)
 
-(defconst enwc-column-specs
+(defvar enwc-wireless-column-specs
   (list
    (make-enwc-column-spec
     :detail 'strength
@@ -169,6 +199,20 @@ in `enwc-update-mode-line'.")
     :sorter #'enwc--chnl-sorter
     :conv #'number-to-string)))
 
+(defvar enwc-wired-column-specs
+  (list
+   (make-enwc-column-spec
+    :detail 'name
+    :display "Profile"
+    :sorter t
+    :conv #'identity)))
+
+(defvar enwc-column-specs enwc-wireless-column-specs
+  "Specifications for each column in the display.
+
+This should always be set to the value of either
+`enwc-wireless-column-specs' or `enwc-wired-column-specs'.")
+
 (defvar enwc--last-scan-results (make-hash-table :test #'equal)
   "The most recent scan results.
 
@@ -176,6 +220,11 @@ This will be an association list of the form:
 
 ((ID . ((strength . STRENGTH) (essid . ESSID) ...)) ...)
 
+The form will be different when wired is enabled (see
+`enwc-using-wired').  This will have the form:
+
+((ID . ((name . PROFILE-NAME))) ...)
+
 Each ID is a backend-specific network ID.
 
 Each key in the children association lists corresponds to an entry in
@@ -187,8 +236,8 @@ Each key in the children association lists corresponds to 
an entry in
 (defvar enwc-using-wired nil
   "Non-nil means ENWC is using wired connections.
 
-Note that this is NOT the same as `enwc-is-wired'.  This checks
-whether or not ENWC is in wired mode.")
+Note that this is NOT the same as `enwc-is-wired-p'.  This
+variable indicates whether ENWC itself is in wired mode.")
 
 (defvar enwc-edit-id nil
   "This is the network id of the network being edited.")
@@ -201,14 +250,9 @@ This is used so as to avoid multiple updates of the scan 
data.")
   "This is non-nil that a scan was interactively requested.
 This is only used internally.")
 
-(defvar enwc-display-mode-line-timer nil
-  "The timer that updates the mode line display.")
-
 (defvar enwc-scan-timer nil
   "The timer for automatic scanning.")
 
-(defvar enwc-mode-line-timer nil)
-
 (make-local-variable 'enwc-edit-id)
 
 ;; The Faces
@@ -238,7 +282,7 @@ This is only used internally.")
 (defun enwc-get-networks ()
   "Get the identifiers for the access points
 from a previous scan."
-  (enwc--network-ids enwc--current-backend))
+  (enwc--network-ids enwc--current-backend enwc-using-wired))
 
 (defun enwc-request-scan ()
   "Request a backend scan."
@@ -267,13 +311,13 @@ The returned id is specific to the backend."
 Returns `non-nil' if there is one, nil otherwise."
   (enwc--is-connecting-p enwc--current-backend))
 
-(defun enwc-get-wireless-nw-props (id)
+(defun enwc-get-nw-props (id)
   "Get the network properties of the wireless network with id ID.
 This will return an associative list with the keys
 corresponding to `enwc-column-specs'.
 
 ID is specific to the backend."
-  (enwc--wireless-nw-props enwc--current-backend id))
+  (enwc--nw-props enwc--current-backend id enwc-using-wired))
 
 (defun enwc-is-wired-p ()
   "Check whether or not ENWC is connected to a wired network.
@@ -343,8 +387,9 @@ See the documentation for it for more details."
   "Update the mode line display.
 This uses the format specified by `enwc-mode-line-format'.
 This is initiated during setup, and runs once every second."
-  (setq enwc-display-string (enwc-format-mode-line-string))
-  (force-mode-line-update))
+  (when enwc-display-mode-line
+    (setq enwc-display-string (enwc-format-mode-line-string))
+    (force-mode-line-update)))
 
 (defun enwc-enable-display-mode-line ()
   "Enable the mode line display."
@@ -353,9 +398,6 @@ This is initiated during setup, and runs once every second."
   (setq enwc-display-mode-line t)
   (unless (member 'enwc-display-string global-mode-string)
     (setq global-mode-string (append global-mode-string 
'(enwc-display-string))))
-  (unless enwc-display-mode-line-timer
-    (setq enwc-display-mode-line-timer
-          (run-at-time t 1 'enwc-update-mode-line)))
   (message "ENWC mode line enabled"))
 
 (defun enwc-disable-display-mode-line ()
@@ -364,9 +406,6 @@ This is initiated during setup, and runs once every second."
   (or global-mode-string (setq global-mode-string '("")))
   (setq enwc-display-mode-line nil)
   (setq global-mode-string (remove 'enwc-display-string global-mode-string))
-  (when enwc-display-mode-line-timer
-    (cancel-timer enwc-display-mode-line-timer))
-  (setq enwc-display-mode-line-timer nil)
   (message "ENWC mode line disabled"))
 
 (defun enwc-toggle-display-mode-line ()
@@ -379,24 +418,28 @@ This is initiated during setup, and runs once every 
second."
 (defun enwc-enable-auto-scan ()
   "Enable auto scanning."
   (interactive)
-  (unless enwc-scan-timer
-    (setq enwc-scan-timer
-          (run-at-time t enwc-auto-scan-interval 'enwc-scan t)))
-  (setq enwc-auto-scan t)
-  (message "Auto-scan enabled"))
+  (if (or (not (numberp enwc-auto-scan-interval))
+          (< enwc-auto-scan-interval 0))
+      (message "Unable to start ENWC auto-scan with invalid scan \
+interval - Got %s, but need positive number" enwc-auto-scan-interval)
+    (unless enwc-scan-timer
+      (setq enwc-scan-timer
+            (run-at-time t enwc-auto-scan-interval 'enwc-scan t)))
+    (setq enwc--auto-scan t)
+    (message "Auto-scan enabled")))
 
 (defun enwc-disable-auto-scan ()
   "Disable auto scanning."
   (interactive)
   (when enwc-scan-timer (cancel-timer enwc-scan-timer))
-  (setq enwc-auto-scan nil)
+  (setq enwc--auto-scan nil)
   (message "Auto scan disabled"))
 
 (defun enwc-toggle-auto-scan ()
   "Toggles automatic scanning.
 This will use the current value of `enwc-auto-scan-interval'."
   (interactive)
-  (if enwc-auto-scan
+  (if enwc--auto-scan
       (enwc-disable-auto-scan)
     (enwc-enable-auto-scan)))
 
@@ -422,55 +465,44 @@ buffer."
   (with-current-buffer "*ENWC*"
     (enwc-scan-internal)))
 
-(defun enwc-scan-internal-wireless ()
-  "The initial scan routine for wireless networks.
-This initiates a scan using D-Bus, then exits, waiting for the callback.
+(defun enwc-scan-internal ()
+  "The entry point for the internal scan routines.
+This checks whether or not wired is being used, and runs the appropriate
+function.
 
-All back-ends must call `enwc-process-scan' in some way upon completion of a
- scan."
+If wireless is used, a scan is requested.  All back-ends must
+call `enwc-process-scan' in some way upon completion of a
+wireless scan.
+
+A wired scan displays the available wired profiles."
   (when enwc-scan-interactive
     (message "Scanning..."))
-  (enwc-request-scan))
-
-(defun enwc-scan-internal-wired ()
-  "The scanning routine for a wired connection.
-This gets the list of wired network profiles."
-  (message "Updating Profiles...")
-  (let ((profs (enwc-get-networks)))
-    (message "Updating Profiles... Done")
-    (setq enwc-access-points      profs
-          enwc--last-scan-results profs)
-    (enwc-display-wired-networks profs)))
 
-(defun enwc-scan-internal ()
-  "The entry point for the internal scan routines.
-This checks whether or not wired is being used, and runs the appropriate
- function."
   (if enwc-using-wired
-      (enwc-scan-internal-wired)
-    (enwc-scan-internal-wireless)))
+      (enwc-process-scan)
+    (enwc-request-scan)))
 
 (defun enwc--update-scan-results ()
   (setq enwc--last-scan-results (make-hash-table :test #'equal))
   (dolist (ap (enwc-get-networks))
-    (puthash ap (enwc-get-wireless-nw-props ap) enwc--last-scan-results)))
+    (puthash ap (enwc-get-nw-props ap) enwc--last-scan-results)))
 
-(defun enwc-redisplay-wireless-networks ()
+(defun enwc-redisplay-networks ()
   (interactive)
   (enwc--update-scan-results)
-  (enwc-display-wireless-networks enwc--last-scan-results))
+  (enwc-display-networks enwc--last-scan-results)
+  (enwc-update-mode-line))
 
 (defun enwc-process-scan (&rest args)
   "The scanning callback.
 After a scan has been performed, this processes and displays the scan results.
 
 ARGS is only for compatibility with the calling function."
-  (unless (or enwc-using-wired (not enwc-scan-requested))
+  (when enwc-scan-requested
     (setq enwc-scan-requested nil)
     (when enwc-scan-interactive
       (message "Scanning... Done"))
-    (enwc--update-scan-results)
-    (enwc-display-wireless-networks enwc--last-scan-results)
+    (enwc-redisplay-networks)
     (setq enwc-scan-interactive nil)))
 
 ;;;;;;;;;;;;;;;;;;;;;;
@@ -483,27 +515,18 @@ ARGS is only for compatibility with the calling function."
         (mapcar
          (lambda (spec)
            (pcase-let* (((cl-struct enwc-column-spec detail display conv) spec)
-                        (new-max (seq-max
-                                  (map-apply
-                                   (lambda (id nw)
-                                     (length (funcall conv (alist-get detail 
nw))))
-                                   networks)))
+                        (new-max (if (mapp networks)
+                                     (seq-max
+                                      (map-apply
+                                       (lambda (id nw)
+                                         (length (funcall conv (alist-get 
detail nw))))
+                                       networks))
+                                   0))
                         (min-width (+ (length display) 2)))
              (setf (enwc-column-spec-width spec) (max new-max min-width)))
            spec)
          enwc-column-specs)))
 
-(defun enwc-display-wired-networks (networks)
-  "Display the wired networks specified in the list NETWORKS.
-NETWORKS must be in the form returned from
-`enwc-scan-internal-wired'."
-  (let ((inhibit-read-only t))
-    (setq tabulated-list-format (vector '("Profile" . 1)))
-    ;;TODO: actually get names of profiles, if possible.
-    (setq tabulated-list-entries (mapcar (lambda (prop) (cons prop prop)) 
networks))
-    (tabulated-list-init-header)
-    (tabulated-list-print)))
-
 (defun enwc--get-details (network-entry)
   (mapcar
    (lambda (detail)
@@ -530,8 +553,10 @@ NETWORKS must be in the form returned from
              conv)))
        cols)))))
 
-(defun enwc-display-wireless-networks (networks)
-  "Display the networks in the list NETWORKS in the current buffer."
+(defun enwc-display-networks (networks)
+  "Displays the network in NETWORKS.
+This is an entry to the display functions, and checks whether or not ENWC is
+ using wired."
   (enwc-ensure-buffer)
   ;; Update the display widths.
   (enwc-refresh-widths)
@@ -546,20 +571,8 @@ NETWORKS must be in the form returned from
     (setq tabulated-list-printer #'enwc--tabulated-list-printer)
 
     (tabulated-list-init-header)
-
     (tabulated-list-print)))
 
-(defun enwc-display-networks (networks)
-  "Displays the network in NETWORKS.
-This is an entry to the display functions, and checks whether or not ENWC is
- using wired."
-  (unless (eq major-mode 'enwc-mode)
-    (enwc-setup-buffer))
-  (cl-check-type networks list)
-  (if enwc-using-wired
-      (enwc-display-wired-networks networks)
-    (enwc-display-wireless-networks networks)))
-
 (defun enwc-find-network (essid &optional networks)
   "Checks through NETWORKS for the network with essid ESSID,
 and returns the network identifier.  Uses `enwc--last-scan-results' if
@@ -568,6 +581,9 @@ NETWORKS is nil.  If the network is not found, then it 
returns nil.
    When called interactively, this only prints out what it finds.
 Otherwise, it actually returns it."
   (interactive "sNetwork ESSID: ")
+  ;; TODO: Fix this for wired networks
+  (when enwc-using-wired
+    (error "Can't find wireless networks while on wired."))
   (unless (or networks enwc--last-scan-results)
     (setq enwc-scan-interactive nil)
     (enwc-scan-internal))
@@ -594,10 +610,9 @@ Otherwise, it actually returns it."
 This is an entry point for the internal connection functions,
 and checks whether or not ENWC is using wired."
   (enwc-connect id)
-  (if enwc-using-wired
-      id
+  (let ((name-sym (if enwc-using-wired 'name 'essid)))
     (when enwc--last-scan-results
-      (enwc-value-from-scan 'essid id))))
+      (enwc-value-from-scan name-sym id))))
 
 (defun enwc-connect-to-network (net-id)
   "Connect the the network with network id NET-ID.
@@ -624,7 +639,6 @@ Moves to the enwc buffer if necessary."
   (interactive)
   (unless (eq major-mode 'enwc-mode)
     (enwc-setup-buffer))
-  ;;TODO: Fix this for wired (which doesn't have tabulated list)
   (enwc-connect-to-network (tabulated-list-get-id)))
 
 (defun enwc-disconnect-network ()
@@ -633,16 +647,34 @@ Moves to the enwc buffer if necessary."
   (message "Disconnecting")
   (enwc-disconnect))
 
+
+
+(defun enwc-enable-wired ()
+  (setq enwc-using-wired t)
+  (setq enwc-column-specs enwc-wired-column-specs)
+  (setq tabulated-list-sort-key nil))
+
+(defun enwc-enable-wireless ()
+  (setq enwc-using-wired nil)
+  (setq enwc-column-specs enwc-wireless-column-specs)
+  (setq tabulated-list-sort-key nil))
+
 (defun enwc-toggle-wired ()
   "Toggle the display and mode between wireless and wired.
-This has the side-effect of setting the variable `enwc-using-wired', and 
calling
-a scan."
+This has the side-effect of setting the variable
+`enwc-using-wired', and calling a scan.
+
+In lisp code, calling `enwc-enable-wired' or
+`enwc-enable-wireless' will directly set the wired state, rather
+than just toggling it."
   (interactive)
   (unless (eq major-mode 'enwc-mode)
     (enwc-setup-buffer))
   (let ((inhibit-read-only t))
     (erase-buffer)
-    (setq enwc-using-wired (not enwc-using-wired))
+    (if enwc-using-wired
+        (enwc-enable-wireless)
+      (enwc-enable-wired))
     (enwc-scan)))
 
 
@@ -690,11 +722,19 @@ One interface will be used for wireless, and the other 
for wired.
 There is no need to call this function manually; that should be
 left to `enwc-setup'.  Instead, set `enwc-wireless-device' and
 `enwc-wired-device'."
-  (let ((interfaces (funcall enwc-interface-list-function)))
+  (let ((interfaces (funcall enwc-interface-list-function))
+        changed)
     (when (string-empty-p enwc-wired-device)
-      (setq enwc-wired-device (completing-read "Wired Interface: " 
interfaces)))
+      (setq enwc-wired-device (completing-read "Wired Interface: " interfaces))
+      (setq changed t))
     (when (string-empty-p enwc-wireless-device)
-      (setq enwc-wireless-device (completing-read "Wireless Interface: " 
interfaces)))))
+      (setq enwc-wireless-device (completing-read "Wireless Interface: " 
interfaces))
+      (setq changed t))
+    (when (and changed
+               enwc-ask-to-save-interfaces
+               (y-or-n-p "Network Interfaces changed.  Save for future 
sessions? "))
+      (customize-save-variable 'enwc-wired-device enwc-wired-device)
+      (customize-save-variable 'enwc-wireless-device enwc-wireless-device))))
 
 (defvar enwc-mode-map
   (let ((map (make-sparse-keymap)))
@@ -710,7 +750,7 @@ left to `enwc-setup'.  Instead, set `enwc-wireless-device' 
and
 (define-derived-mode enwc-mode tabulated-list-mode "enwc"
   "Mode for working with network connections.
 \\{enwc-mode-map}"
-  (add-hook 'tabulated-list-revert-hook 'enwc-redisplay-wireless-networks nil 
t))
+  (add-hook 'tabulated-list-revert-hook 'enwc-redisplay-networks nil t))
 
 (defun enwc-setup-buffer (&optional nomove)
   "Sets up the ENWC buffer.
@@ -729,6 +769,26 @@ newly created buffer."
   (unless (get-buffer "*ENWC*")
     (enwc-setup-buffer t)))
 
+;; Setup is broken into four functions to ease testing.  This allows developers
+;; to test each one individually without worrying about the side effects of
+;; others
+
+(defun enwc--setup-select-interfaces ()
+  (when (or (string-empty-p enwc-wired-device)
+            (string-empty-p enwc-wireless-device))
+    (enwc--select-interfaces)))
+
+(defun enwc--setup-load-default-backend ()
+  (enwc-load-default-backend enwc-force-backend-loading))
+
+(defun enwc--setup-display-mode-line ()
+  (when enwc-display-mode-line
+    (enwc-enable-display-mode-line)))
+
+(defun enwc--setup-auto-scan ()
+  (when enwc-enable-auto-scan-on-startup
+    (enwc-enable-auto-scan)))
+
 (defvar enwc--setup-done nil
   "Non-nil if enwc has already been set up.")
 
@@ -743,24 +803,19 @@ Load the default backend, forcing it if
 
 If `enwc-display-mode-line' is non-nil, enable the mode line.
 
-If `enwc-auto-scan' is non-nil, start the auto-scan timer."
-  (unless enwc--setup-done
-    (when (or (string-empty-p enwc-wired-device)
-              (string-empty-p enwc-wireless-device))
-      (enwc--select-interfaces))
-
-    (enwc-load-default-backend enwc-force-backend-loading)
-
-    (when enwc-display-mode-line
-      (enwc-enable-display-mode-line))
-
-    (when (and enwc-auto-scan
-               (> enwc-auto-scan-interval 0)
-               (not enwc-scan-timer))
-      (setq enwc-scan-timer
-            (run-at-time t enwc-auto-scan-interval 'enwc-scan t)))
-
-    (setq enwc--setup-done t)))
+If `enwc-enable-auto-scan-on-startup' is non-nil, start the
+auto-scan timer."
+  (cond
+   ((and enwc--setup-done enwc-warn-if-already-setup)
+    (message "ENWC is already setup."))
+   (enwc--setup-done t)
+   (t
+    (enwc--setup-select-interfaces)
+    (enwc--setup-load-default-backend)
+    (enwc--setup-display-mode-line)
+    (enwc--setup-auto-scan)
+
+    (setq enwc--setup-done t))))
 
 ;;;###autoload
 (defun enwc ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]