branch: master
commit 245a82f5f875380f2c0f9a4255aaf2875fb0fa2c
Author: Ian Dunn <du...@gnu.org>
Commit: Ian Dunn <du...@gnu.org>

    Moved Wicd and NetworkManager backends to new system.
---
 lisp/enwc-nm.el   | 108 +++++++++++++++++++++++++++-----------------
 lisp/enwc-wicd.el | 133 ++++++++++++++++++++++++++++++++----------------------
 2 files changed, 146 insertions(+), 95 deletions(-)

diff --git a/lisp/enwc-nm.el b/lisp/enwc-nm.el
index b6499ce..363cb3d 100644
--- a/lisp/enwc-nm.el
+++ b/lisp/enwc-nm.el
@@ -29,7 +29,9 @@
 ;;   This requires NetworkManager >= 0.9.6
 ;;
 
+(require 'enwc-backend)
 (require 'enwc)
+(require 'dbus)
 
 (defgroup enwc-nm nil
   "*NetworkManager variables for ENWC"
@@ -421,31 +423,31 @@ If STATE is 40, then NetworkManager is connecting to a 
new AP."
   (let* ((bytes (split-string addr "\\."))
          (byte-string (mapcar
                        (lambda (n) (lsh (string-to-number (nth n bytes))
-                                        (* 8 n)))
+                                   (* 8 n)))
                        (number-sequence 0 3))))
     (apply 'logior byte-string)))
 
 ;; These next two come from libnm-util/nm-utils.c in NM's source.
 
-(defun enwc-nm-netmask-to-prefix (netmask)
-  "Convert a netmask to a CIDR prefix.
-NETMASK is an ip address in network byte order."
-  (if (and netmask (integerp netmask))
-      (progn
-        (setq netmask (enwc--htonl netmask))
-        (while (cl-evenp netmask)
-          (setq netmask (lsh netmask -1)))
-        (floor (log (1+ netmask) 2)))
-    0))
-
-(defun enwc-nm-prefix-to-netmask (prefix)
-  "Convert a CIDR prefix to a netmask.
-PREFIX is an integer <= 32."
-  (if (and prefix (integerp prefix))
-      (progn
-        (setq prefix (min prefix 32))
-        (enwc--htonl (lsh (1- (expt 2 prefix)) (- 32 prefix))))
-    0))
+;; (defun enwc-nm-netmask-to-prefix (netmask)
+;;   "Convert a netmask to a CIDR prefix.
+;; NETMASK is an ip address in network byte order."
+;;   (if (and netmask (integerp netmask))
+;;       (progn
+;;         (setq netmask (enwc--htonl netmask))
+;;         (while (cl-evenp netmask)
+;;           (setq netmask (lsh netmask -1)))
+;;         (floor (log (1+ netmask) 2)))
+;;     0))
+
+;; (defun enwc-nm-prefix-to-netmask (prefix)
+;;   "Convert a CIDR prefix to a netmask.
+;; PREFIX is an integer <= 32."
+;;   (if (and prefix (integerp prefix))
+;;       (progn
+;;         (setq prefix (min prefix 32))
+;;         (enwc--htonl (lsh (1- (expt 2 prefix)) (- 32 prefix))))
+;;     0))
 
 ;;;;;;;;;;;;;;;;;;;;;;
 ;; D-Bus Conversion ;;
@@ -537,27 +539,27 @@ representing another layer in the dictionary."
       (push `(:string (car ent) ,(enwc-nm-alist-to-dbus-dict (cadr ent))) ret)
       (push :dict-entry ret))))
 
-(defun enwc-nm-get-profile-info (ap &optional wired)
-  "Get the profile info for access point AP."
-  (let ((conn (enwc-nm--ap-to-conn ap))
-        settings)
-    (when conn
-      (setq settings (enwc-nm-get-settings conn)))
-    (when settings
-      (let* ((adr-info (caar (enwc-nm-get-dbus-dict-entry "ipv4/addresses" 
settings)))
-             (ip-addr (enwc-nm-convert-addr (nth 0 adr-info)))
-             (netmask (enwc-nm-convert-addr (enwc-nm-prefix-to-netmask (nth 1 
adr-info))))
-             (gateway (enwc-nm-convert-addr (nth 2 adr-info)))
-             (dns-list (mapcar 'enwc-nm-convert-addr
-                               (car (enwc-nm-get-dbus-dict-entry "ipv4/dns"
-                                                                 settings))))
-             (sec-info (enwc-nm-get-sec-info settings)))
-        `((addr . ,ip-addr)
-          (netmask . ,netmask)
-          (gateway . ,gateway)
-          (dns1    . ,(nth 0 dns-list))
-          (dns2    . ,(nth 1 dns-list))
-          ,@sec-info)))))
+;; (defun enwc-nm-get-profile-info (ap &optional wired)
+;;   "Get the profile info for access point AP."
+;;   (let ((conn (enwc-nm--ap-to-conn ap))
+;;         settings)
+;;     (when conn
+;;       (setq settings (enwc-nm-get-settings conn)))
+;;     (when settings
+;;       (let* ((adr-info (caar (enwc-nm-get-dbus-dict-entry "ipv4/addresses" 
settings)))
+;;              (ip-addr (enwc-nm-convert-addr (nth 0 adr-info)))
+;;              (netmask (enwc-nm-convert-addr (enwc-nm-prefix-to-netmask (nth 
1 adr-info))))
+;;              (gateway (enwc-nm-convert-addr (nth 2 adr-info)))
+;;              (dns-list (mapcar 'enwc-nm-convert-addr
+;;                                (car (enwc-nm-get-dbus-dict-entry "ipv4/dns"
+;;                                                                  
settings))))
+;;              (sec-info (enwc-nm-get-sec-info settings)))
+;;         `((addr . ,ip-addr)
+;;           (netmask . ,netmask)
+;;           (gateway . ,gateway)
+;;           (dns1    . ,(nth 0 dns-list))
+;;           (dns2    . ,(nth 1 dns-list))
+;;           ,@sec-info)))))
 
 (defun enwc-nm-finalize-settings (settings)
   "Set up all of the D-BUS types of a settings list SETTINGS.
@@ -604,6 +606,12 @@ to put it in the form that NetworkManager will recognize."
                                 enwc-nm-dbus-settings-interface
                                 (enwc-nm-setup-settings ap settings wired)))))
 
+
+                                        ; ;;;;;;;;;;;;;;;;; ;
+                                        ; ;; Load/Unload ;; ;
+                                        ; ;;;;;;;;;;;;;;;;; ;
+
+
 (defun enwc-nm-load ()
   "Setup the NetworkManager back-end."
   ;;TODO: Add way of changing these two after load.
@@ -685,6 +693,24 @@ Unregister all of the D-Bus signals set up during load."
   (dbus-unregister-object enwc-nm-wired-state-changed-signal)
   (dbus-unregister-object enwc-nm-state-changed-signal))
 
+(defun enwc-nm-can-load-p ()
+  (dbus-ping :system enwc-nm-dbus-service))
+
+(enwc-register-backend
+ (make-enwc-backend
+  :key 'nm
+  :can-load-p #'enwc-nm-can-load-p
+  :load #'enwc-nm-load
+  :unload #'enwc-nm-unload
+  :network-ids #'enwc-nm-get-networks
+  :scan #'enwc-nm-scan
+  :connect #'enwc-nm-connect
+  :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
+  :is-wired-p #'enwc-nm-is-wired))
+
 (provide 'enwc-nm)
 
 ;;; enwc-nm.el ends here
diff --git a/lisp/enwc-wicd.el b/lisp/enwc-wicd.el
index 3438ee2..cb64fa8 100644
--- a/lisp/enwc-wicd.el
+++ b/lisp/enwc-wicd.el
@@ -30,7 +30,9 @@
 
 ;;; Code:
 
+(require 'enwc-backend)
 (require 'enwc)
+(require 'dbus)
 
 (defgroup enwc-wicd nil
   "*Wicd variables for ENWC."
@@ -289,60 +291,60 @@ so this jut returns the tracked network id."
 ;; Get Profile Info ;;
 ;;;;;;;;;;;;;;;;;;;;;;
 
-(defun enwc-wicd-get-profile-ent (id ent wired)
-  "Get profile entry ENT from the network with id ID.
-WIRED is set to indicate whether this is a wired network.
-This function is a wrapper around the *-get-(wired|wireless)-nw-prop
-functions, allowing for a single function that checks for wired."
-  (if wired
-      (enwc-wicd-get-wired-nw-prop id ent)
-    (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty" id ent)))
-
-(defun enwc-wicd-get-profile-info (id &optional wired)
-  "Get the profile for profile ID.
-WIRED is set to indicate whether this is a wired network."
-  (let ((dns-list (enwc-wicd-get-dns id wired))
-        (sec-info (enwc-wicd-get-profile-sec-info id wired)))
-    `((addr . ,(enwc-wicd-get-ip-addr id wired))
-      (netmask . ,(enwc-wicd-get-netmask id wired))
-      (gateway . ,(enwc-wicd-get-gateway id wired))
-      (dns1 . ,(nth 0 dns-list))
-      (dns2 . ,(nth 1 dns-list))
-      ,@sec-info)))
-
-(defun enwc-wicd-get-profile-sec-info (id &optional wired)
-  "Get the security info for profile with id ID.
-WIRED is set to indicate whether this is a wired network."
-  (remq nil
-   (mapcar
-    (lambda (ent)
-      (let ((info (enwc-wicd-get-profile-ent wired id (symbol-name (car 
ent)))))
-        (if info
-            (cons (car ent) info)
-          nil)))
-    enwc-supplicant-alist)))
-
-(defun enwc-wicd-get-ip-addr (id wired)
-  "Get the IP Address from the network with id ID.
-Wired is set to indicate whether this is a wired network."
-  (or (enwc-wicd-get-profile-ent id "ip" wired) ""))
-
-(defun enwc-wicd-get-netmask (id wired)
-  "Get the Netmask from the network with id ID.
-WIRED is set to indicate whether this is a wired network."
-  (or (enwc-wicd-get-profile-ent id "netmask" wired) ""))
-
-(defun enwc-wicd-get-gateway (id wired)
-  "Get the Gateway from the network with id ID.
-WIRED is set to indicate whether this is a wired network."
-  (or (enwc-wicd-get-profile-ent id "gateway" wired) ""))
-
-(defun enwc-wicd-get-dns (id wired)
-  "Get the list of DNS servers from the network with id ID.
-WIRED is set to indicate whether this is a wired network."
-  (list (or (enwc-wicd-get-profile-ent id "dns1" wired) "")
-        (or (enwc-wicd-get-profile-ent id "dns2" wired) "")
-        (or (enwc-wicd-get-profile-ent id "dns3" wired) "")))
+;; (defun enwc-wicd-get-profile-ent (id ent wired)
+;;   "Get profile entry ENT from the network with id ID.
+;; WIRED is set to indicate whether this is a wired network.
+;; This function is a wrapper around the *-get-(wired|wireless)-nw-prop
+;; functions, allowing for a single function that checks for wired."
+;;   (if wired
+;;       (enwc-wicd-get-wired-nw-prop id ent)
+;;     (enwc-wicd-dbus-wireless-call-method "GetWirelessProperty" id ent)))
+
+;; (defun enwc-wicd-get-profile-info (id &optional wired)
+;;   "Get the profile for profile ID.
+;; WIRED is set to indicate whether this is a wired network."
+;;   (let ((dns-list (enwc-wicd-get-dns id wired))
+;;         (sec-info (enwc-wicd-get-profile-sec-info id wired)))
+;;     `((addr . ,(enwc-wicd-get-ip-addr id wired))
+;;       (netmask . ,(enwc-wicd-get-netmask id wired))
+;;       (gateway . ,(enwc-wicd-get-gateway id wired))
+;;       (dns1 . ,(nth 0 dns-list))
+;;       (dns2 . ,(nth 1 dns-list))
+;;       ,@sec-info)))
+
+;; (defun enwc-wicd-get-profile-sec-info (id &optional wired)
+;;   "Get the security info for profile with id ID.
+;; WIRED is set to indicate whether this is a wired network."
+;;   (remq nil
+;;    (mapcar
+;;     (lambda (ent)
+;;       (let ((info (enwc-wicd-get-profile-ent wired id (symbol-name (car 
ent)))))
+;;         (if info
+;;             (cons (car ent) info)
+;;           nil)))
+;;     enwc-supplicant-alist)))
+
+;; (defun enwc-wicd-get-ip-addr (id wired)
+;;   "Get the IP Address from the network with id ID.
+;; Wired is set to indicate whether this is a wired network."
+;;   (or (enwc-wicd-get-profile-ent id "ip" wired) ""))
+
+;; (defun enwc-wicd-get-netmask (id wired)
+;;   "Get the Netmask from the network with id ID.
+;; WIRED is set to indicate whether this is a wired network."
+;;   (or (enwc-wicd-get-profile-ent id "netmask" wired) ""))
+
+;; (defun enwc-wicd-get-gateway (id wired)
+;;   "Get the Gateway from the network with id ID.
+;; WIRED is set to indicate whether this is a wired network."
+;;   (or (enwc-wicd-get-profile-ent id "gateway" wired) ""))
+
+;; (defun enwc-wicd-get-dns (id wired)
+;;   "Get the list of DNS servers from the network with id ID.
+;; WIRED is set to indicate whether this is a wired network."
+;;   (list (or (enwc-wicd-get-profile-ent id "dns1" wired) "")
+;;         (or (enwc-wicd-get-profile-ent id "dns2" wired) "")
+;;         (or (enwc-wicd-get-profile-ent id "dns3" wired) "")))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Save Network Settings ;;
@@ -414,6 +416,11 @@ WIRED indicates whether or not ID is a wired connection."
                                          (caar (nthcdr 3 info))
                                          (string-to-number (caar (nthcdr 3 
info))))))))
 
+
+; ;;;;;;;;;;;;;;;;; ;
+; ;; Load/Unload ;; ;
+; ;;;;;;;;;;;;;;;;; ;
+
 (defun enwc-wicd-load ()
   "Load the Wicd backend."
   ;; Thanks to Michael Albinus for pointing out this signal.
@@ -440,6 +447,24 @@ Unregister all of the D-Bus signals set up during load."
   (dbus-unregister-object enwc-wicd-end-scan-signal)
   (dbus-unregister-object enwc-wicd-status-changed-signal))
 
+(defun enwc-wicd-can-load-p ()
+  (dbus-ping :system enwc-wicd-dbus-service))
+
+(enwc-register-backend
+ (make-enwc-backend
+  :key 'enwc
+  :can-load-p #'enwc-wicd-can-load-p
+  :load #'enwc-wicd-load
+  :unload #'enwc-wicd-unload
+  :network-ids #'enwc-wicd-get-networks
+  :scan #'enwc-wicd-scan
+  :connect #'enwc-wicd-connect
+  :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
+  :is-wired-p #'enwc-wicd-is-wired))
+
 (provide 'enwc-wicd)
 
 ;;; enwc-wicd.el ends here.

Reply via email to