Subject: bug#42977: closed (Re: bug#42977: 28.0.50; New gnus-dbus library providing (optional) D-Bus->Gnus integration)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 27 Aug 2020 21:21:02 +0000

Your bug report

#42977: 28.0.50; New gnus-dbus library providing (optional) D-Bus->Gnus integration

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
42977: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=42977
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#42977: 28.0.50; New gnus-dbus library providing (optional) D-Bus->Gnus integration
From: Eric Abrahamsen <[email-masked]>
To: Lars Ingebrigtsen <[email-masked]>
Cc: [email-masked], [email-masked]
Date: Thu, 27 Aug 2020 14:20:26 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Lars Ingebrigtsen <[email-masked]> writes:

> Eric Abrahamsen <[email-masked]> writes:
>
>> I think it's more a matter of reducing confusion for hackers. If I do
>> `gnus-close-server' followed by `gnus-server-status', it tells me the
>> server is open -- not very helpful.
>>
>> The patch is pretty simple.
>
> Looks good to me.

Cool, in it goes. It turns out the signal needed to be :system after
all, not :session, so I made that change as well, and am closing this
report.

Thanks,
Eric

Subject: 28.0.50; New gnus-dbus library providing (optional) D-Bus->Gnus integration
From: Eric Abrahamsen <[email-masked]>
To: [email-masked]
Date: Fri, 21 Aug 2020 13:43:07 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

The attached patch provides a new gnus-dbus.el library, allowing systems
with dbus support to register a signal that closes all Gnus servers when
the system is going down for sleep. This is kind of a stop-gap solution
for the larger problems discussed in #40748.

It only does anything if Emacs has been compiled with dbus support, and
if the user sets the `gnus-dbus-close-on-sleep' to non-nil (it defaults
to nil).

Right now I've done this by checking the value of that option in
`gnus-1', but I could just as easily have the library add a startup
hook.

I assume this would require a mention in the manual. Is it something
we'd add to NEWS, as well?

Thanks,
Eric

>From 414051f4fb7d60a6674b7267dc7f73025ddb1a3b Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <[email-masked]>
Date: Fri, 21 Aug 2020 13:36:58 -0700
Subject: [PATCH] Add basic D-Bus integration to Gnus

* lisp/gnus/gnus-dbus.el: New library, registering a signal that
closes all Gnus servers when the system is going to sleep.
* lisp/gnus/gnus-start.el: Check new option
`gnus-dbus-close-on-sleep', and register the appropriate D-Bus signal
if it is non-nil.
* lisp/gnus/gnus.el: New gnus-dbus customization group.
---
 lisp/gnus/gnus-dbus.el  | 66 +++++++++++++++++++++++++++++++++++++++++
 lisp/gnus/gnus-start.el |  3 ++
 lisp/gnus/gnus.el       |  4 +++
 3 files changed, 73 insertions(+)
 create mode 100644 lisp/gnus/gnus-dbus.el

diff --git a/lisp/gnus/gnus-dbus.el b/lisp/gnus/gnus-dbus.el
new file mode 100644
index 0000000000..14f0abb6be
--- /dev/null
+++ b/lisp/gnus/gnus-dbus.el
@@ -0,0 +1,66 @@
+;;; gnus-dbus.el --- DBUS integration for Gnus       -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2020  Free Software Foundation, Inc.
+
+;; Author: Eric Abrahamsen <[email-masked]>
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This library contains some Gnus integration for systems using DBUS.
+;; At present it registers a signal to close all Gnus servers before
+;; system sleep or hibernation.
+
+;;; Code:
+
+(require 'dbus)
+
+(defcustom gnus-dbus-close-on-sleep nil
+  "When non-nil, close Gnus servers on system sleep."
+  :group 'gnus-dbus
+  :type 'boolean)
+
+(defvar gnus-dbus-sleep-registration-object nil
+  "Object returned from `dbus-register-signal'.
+Used to unregistering the signal.")
+
+(defun gnus-dbus-register-sleep-signal ()
+  "Use `dbus-register-signal' to close servers on sleep."
+  (when (featurep 'dbusbind)
+    (setq gnus-dbus-sleep-registration-object
+	  (dbus-register-signal :system
+				"org.freedesktop.login1"
+				"/org/freedesktop/login1"
+				"org.freedesktop.login1.Manager"
+				"PrepareForSleep"
+				#'gnus-dbus-sleep-handler))
+    (gnus-add-shutdown #'gnus-dbus-unregister-sleep-signal 'gnus)))
+
+(defun gnus-dbus-sleep-handler (sleep-start)
+  ;; Sleep-start is t before sleeping.
+  (when (and sleep-start
+	     (gnus-alive-p))
+    (condition-case nil
+	(gnus-close-all-servers)
+      (error nil))))
+
+(defun gnus-dbus-unregister-sleep-signal ()
+  (condition-case nil
+      (dbus-unregister-object
+       gnus-dbus-sleep-registration-object)
+    (wrong-type-argument nil)))
+
+(provide 'gnus-dbus)
+;;; gnus-dbus.el ends here
diff --git a/lisp/gnus/gnus-start.el b/lisp/gnus/gnus-start.el
index ba8b91be5c..fe600f107c 100644
--- a/lisp/gnus/gnus-start.el
+++ b/lisp/gnus/gnus-start.el
@@ -31,6 +31,7 @@
 (require 'gnus-range)
 (require 'gnus-util)
 (require 'gnus-cloud)
+(require 'gnus-dbus)
 (autoload 'message-make-date "message")
 (autoload 'gnus-agent-read-servers-validate "gnus-agent")
 (autoload 'gnus-agent-save-local "gnus-agent")
@@ -798,6 +799,8 @@ gnus-1
 	  (gnus-run-hooks 'gnus-setup-news-hook)
 	  (when gnus-agent
 	    (gnus-request-create-group "queue" '(nndraft "")))
+	  (when gnus-dbus-close-on-sleep
+	    (gnus-dbus-register-sleep-signal))
 	  (gnus-start-draft-setup)
 	  ;; Generate the group buffer.
 	  (gnus-group-list-groups level)
diff --git a/lisp/gnus/gnus.el b/lisp/gnus/gnus.el
index cecf4d4fb4..f615d49d27 100644
--- a/lisp/gnus/gnus.el
+++ b/lisp/gnus/gnus.el
@@ -292,6 +292,10 @@ gnus-fun
   :link '(custom-manual "(gnus)Exiting Gnus")
   :group 'gnus)
 
+(defgroup gnus-dbus nil
+  "D-Bus integration for Gnus."
+  :group 'gnus)
+
 (defconst gnus-version-number "5.13"
   "Version number for this version of Gnus.")
 
-- 
2.28.0