Repository: goldendict/goldendict (language: cpp)

Issue:
Goldendict cancels KDE logout 
**Your system information**
+ Version: 1.5.0-RC2+git
+ Distribution: OpenSUSE Tumbleweed with KDE

**Please describe your issue in as much detail as possible**
+ Trying to logout or otherwise end the session in KDE will cause Plasma Workspace to show a notification saying "Logout was canceled by ''usr/bin/goldendict".

**Steps for reproducing this issue:**
+ Log in to a Plasma session
+ Open Goldendict
+ Try to end the session through logging out, restarting, or shutting down.


Discussion / hints:
Which GoldenDict git commit exactly? If older than d65a1f7869e83eba5c01617253c46ac7f63e3514, then it's likely fixed in master already.
@vedgy @Abs62 

I am having the same issue on Manjaro KDE (latest edition) with GoldenDict installed from the AUR Repository:
https://aur.archlinux.org/packages/goldendict-git

![image](https://user-images.githubusercontent.com/58692066/142712956-5f6c4c25-daea-4cbc-a768-b681325be74e.png)


>     * Trying to logout or otherwise end the session in KDE will cause Plasma Workspace to show a notification saying "Logout was canceled by ''usr/bin/goldendict".
> 

@marreromarco, please don't ping GoldenDict developers so often, unless you want to annoy them into ignoring you. I have already commented in this issue, so I would have received the notification about your comment anyway. The other developer you just pinged is a Windows user. There is little chance that he would try to reproduce and fix this desktop-environment-specific issue (unless someone figured out and indicated where the current GoldenDict code is wrong according to Qt documentation).

Back to this issue: is this a recent regression? Did it first appear after the latest huge Manjaro stable system upgrade?
I am very sorry. I thought that pinning was the only way of being notified. I apologize. I am not a programmer.

I was using the AppImage and there was no issue. Only this month I changed to the AUR version and began to notice the problem.
1. Does this issue happen on every logout or only occasionally?
2. Could you try running the AppImage version again and see if the issue disappears?
I had exactly same problem:
 "Logout was canceled by ''usr/bin/goldendict".

**This only happen if "Scan Popup" is active**

Your system information
Version: 1.5.0-RC2+git
Distribution: OpenSUSE Leap 15.3 with KDE
Based on Qt 5.12.7 (GCC 7.5.0, 64 bit)
1. The issue happens on every occasion with "[GoldenDict-git](https://aur.archlinux.org/packages/goldendict-git)" version from AUR Repository on Manjaro KDE (QT 5.15.2  / KDE Plasma Version 5.23.3 / Kerneal 5.10 .79-1 / Graphics Platform X11).
2. Latest AppImage also has the same problem on every occasion. 

* Previosly, I had the AppImage but  _didn't notice_ the issue. It is because I was used to manually close the software before shutting down the PC (fearing index database corruption). 

_In my case,  there seems to be no relation with the "Scan Popup" activation._

![image](https://user-images.githubusercontent.com/58692066/143501717-ee2e0eb4-6765-485b-a5cc-e52ffd47d600.png)
![image](https://user-images.githubusercontent.com/58692066/143501727-0573f7b1-22b2-493e-a816-cdd4f726852b.png)


> **This only happen if "Scan Popup" is active**

@Alexqwesa, what do you mean by **"Scan Popup" is active**?
1. Scan popup checkbox is checked and the system tray icon looks like on the screenshot in the previous post by @FyTpodQUuWvQNTh?
2. Or the Scan Popup window is visible and is the active window?
> > **This only happen if "Scan Popup" is active**
> 
> @Alexqwesa, what do you mean by **"Scan Popup" is active**?

sorry for unclear message, i did mean: if checkbox **"Scan Popup"** is set - goldendict block logout process, if I unset this checkbox - everything work

**upd:**
and now goldendict block logout anyway  - not sure what did i change?... 

**upd2**
FOUND:  It is the option **Close to system tray** - this block the logout!!!

BTW: qbittorent has the same problem - it block logout if option "minimaze to system tray" set   https://github.com/qbittorrent/qBittorrent/issues/13613
I wonder why this issue started appearing only recently. I mean, it was created 23 days ago, and already two more people confirmed it. The relevant GoldenDict code is at least 4 years old: https://github.com/goldendict/goldendict/blame/b10cdf685c8c02d2be2f1ae4f1e03a4e02db48d8/mainwindow.cc#L1211-L1227. The issue seems to be limited to Plasma desktop. I have never seen this in Xfce, even though I have *Close to system tray* option enabled for years.

Has each commenter in this thread updated Plasma to a new version recently, and then first noticed the bug? If yes, which exactly old Plasma version was updated to which new one?

Or has everyone first enabled the *Close to system tray* option only recently?
> Has each commenter in this thread updated Plasma to a new version recently, and then first noticed the bug? If yes, which exactly old Plasma version was updated to which new one?
> 
> Or has everyone first enabled the _Close to system tray_ option only recently?

didn't update KDE recently, BUT I did delete goldendict config and reconfigure it recently

**ALSO**
I did several tests and this is that i found:

if option "**Close to system tray"** active and window of goldendict  is **open** - it says: **Logout canceled** by '/usr/bin/goldendict **AND** close goldendict window to the system tray

if option "**Close to system tray"** active **but** goldendict window is already closed(in system tray) - it logout normally

BTW, this is also apply to qbittorent




@Alexqwesa, thank you for testing. This last finding makes sense: Plasma tries to close all visible windows first; then asks all running applications with no visible windows to quit. When any app rejects the window-close or the quit request, the logout is canceled. GoldenDict never rejects a quit request (see https://github.com/goldendict/goldendict/blame/b10cdf685c8c02d2be2f1ae4f1e03a4e02db48d8/mainwindow.cc#L1064-L1067). But GoldenDict ignores close events if *Close to system tray* option is enabled, and Plasma considers this a cancellation. I wonder if GoldenDict should handle close events differently or Plasma should be smarter and ask apps to quit if they reject close events. I'll check if Xfce behaves like Plasma in this scenario.

Fix patch:
diff --git a/mainwindow.cc b/mainwindow.cc
index b5980f7c1..807b2ee3c 100644
--- a/mainwindow.cc
+++ b/mainwindow.cc
@@ -1212,12 +1212,21 @@ void MainWindow::closeEvent( QCloseEvent * ev )
 {
   if ( cfg.preferences.enableTrayIcon && cfg.preferences.closeToTray )
   {
-    ev->ignore();
-
     if( !cfg.preferences.searchInDock )
       translateBox->setPopupEnabled( false );
 
+#ifdef HAVE_X11
+    // Don't ignore the close event, because doing so cancels session logout if
+    // the main window is visible when the user attempts to log out.
+    // The main window will be only hidden, because QApplication::quitOnLastWindowClosed
+    // property is false and Qt::WA_DeleteOnClose widget attribute is not set.
+    Q_ASSERT(!QApplication::quitOnLastWindowClosed());
+    Q_ASSERT(!testAttribute(Qt::WA_DeleteOnClose));
+#else
+    // Ignore the close event because closing the main window breaks global hotkeys on Windows.
+    ev->ignore();
     hide();
+#endif
   }
   else
   {


Test patch: