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

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

[elpa] master b7529b1 01/13: Fixed wired connection and interface.


From: Ian Dunn
Subject: [elpa] master b7529b1 01/13: Fixed wired connection and interface.
Date: Sat, 15 Apr 2017 16:02:40 -0400 (EDT)

branch: master
commit b7529b12826f926da1e9b608a6cad428e12bd57c
Author: Ian Dunn <address@hidden>
Commit: Ian Dunn <address@hidden>

    Fixed wired connection and interface.
    
    * enwc-backend.el (enwc--network-ids): Added wired argument
      (enwc-backend): Changed wireless-nw-props to nw-props
      (enwc--wireless-nw-props): Changed to enwc--nw-props
    
    * enwc-nm.el (enwc-nm--ap-to-conn): Replaced with logic from
      enwc-nm-wireless-connect
      (enwc-nm-wireless-connect): Use it
      (enwc-nm--profile-wired-p): New function
      (enwc-nm-get-wired-profiles): Use it and return ids instead of names.
      (enwc-nm-get-wired-nw-props): New function
      (enwc-nm-get-nw-props): Use it
    
    * enwc-wicd.el: Updated to use new backend property.
    
    * enwc.el (enwc-wireless-column-specs, enwc-wired-column-specs): New 
variables
      (enwc-column-specs): Set to value of one or the other.
      (enwc-get-networks): Pass enwc-using-wired.
      (enwc-get-nw-props): Renamed from enwc-get-wireless-nw-props, and call
      enwc--nw-props
      (enwc--update-scan-results): Use it.
      (enwc-scan-internal-wireless, enwc-scan-internal-wired): Removed.
      (enwc-scan-internal): Handle wired or wireless.
      (enwc-display-wired-networks): Removed.
      (enwc-display-networks): Renamed from enwc-display-wireless-networks
      (enwc-redisplay-networks): Call it.
      (enwc-connect-network): No special treatment for wired.
      (enwc-enable-wired, enwc-enable-wireless): New functions.
      (enwc-toggle-wired): Use them.
      (enwc-mode): call enwc-display-networks on revert
---
 enwc-backend.el |  10 ++--
 enwc-nm.el      |  87 +++++++++++++++--------------------
 enwc-wicd.el    |  12 ++++-
 enwc.el         | 140 ++++++++++++++++++++++++++++----------------------------
 4 files changed, 123 insertions(+), 126 deletions(-)

diff --git a/enwc-backend.el b/enwc-backend.el
index 076907f..a8b8f92 100644
--- a/enwc-backend.el
+++ b/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/enwc-nm.el b/enwc-nm.el
index b3275e0..391f4ee 100644
--- a/enwc-nm.el
+++ b/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/enwc-wicd.el b/enwc-wicd.el
index 011f91e..f0e157c 100644
--- a/enwc-wicd.el
+++ b/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/enwc.el b/enwc.el
index 78a9c33..6ed962e 100644
--- a/enwc.el
+++ b/enwc.el
@@ -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:
 
@@ -141,7 +138,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 +166,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 +187,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 +203,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.")
@@ -238,7 +254,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 +283,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.
@@ -422,55 +438,43 @@ 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.
+
+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.
 
-All back-ends must call `enwc-process-scan' in some way upon completion of a
- 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))
 
 (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)))
 
 ;;;;;;;;;;;;;;;;;;;;;;
@@ -493,17 +497,6 @@ ARGS is only for compatibility with the calling function."
            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 +523,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 +541,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 +551,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 +580,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 +609,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 +617,32 @@ 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))
+
+(defun enwc-enable-wireless ()
+  (setq enwc-using-wired nil)
+  (setq enwc-column-specs enwc-wireless-column-specs))
+
 (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)))
 
 
@@ -710,7 +710,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.



reply via email to

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