44import org .cryptomator .integrations .keychain .KeychainAccessException ;
55import org .cryptomator .integrations .keychain .KeychainAccessProvider ;
66import org .freedesktop .dbus .connections .impl .DBusConnection ;
7+ import org .freedesktop .dbus .exceptions .DBusConnectionException ;
78import org .freedesktop .dbus .exceptions .DBusException ;
89import org .kde .KWallet ;
910import org .kde .Static ;
@@ -22,24 +23,7 @@ public class KDEWalletKeychainAccess implements KeychainAccessProvider {
2223 private final Optional <ConnectedWallet > wallet ;
2324
2425 public KDEWalletKeychainAccess () {
25- ConnectedWallet wallet = null ;
26- try {
27- DBusConnection conn = null ;
28- try {
29- conn = DBusConnection .getConnection (DBusConnection .DBusBusType .SESSION );
30- } catch (RuntimeException e ) {
31- if (e .getMessage () == "Cannot Resolve Session Bus Address" ) {
32- LOG .warn ("SESSION DBus not found." );
33- }
34- }
35- if (conn == null ) {
36- conn = DBusConnection .getConnection (DBusConnection .DBusBusType .SYSTEM );
37- }
38- wallet = new ConnectedWallet (conn );
39- } catch (DBusException e ) {
40- LOG .warn ("Connecting to D-Bus failed." , e );
41- }
42- this .wallet = Optional .ofNullable (wallet );
26+ this .wallet = ConnectedWallet .connect ();
4327 }
4428
4529 @ Override
@@ -85,6 +69,28 @@ public ConnectedWallet(DBusConnection connection) {
8569 this .wallet = new KDEWallet (connection );
8670 }
8771
72+ static Optional <ConnectedWallet > connect () {
73+ try {
74+ return Optional .of (new ConnectedWallet (getConnection ()));
75+ } catch (DBusException e ) {
76+ LOG .warn ("Connecting to D-Bus failed." , e );
77+ return Optional .empty ();
78+ }
79+ }
80+
81+ private static DBusConnection getConnection () throws DBusException {
82+ try {
83+ return DBusConnection .getConnection (DBusConnection .DBusBusType .SESSION );
84+ } catch (DBusConnectionException ce ) {
85+ LOG .warn ("SESSION DBus not found, falling back to SYSTEM DBus" );
86+ try {
87+ return DBusConnection .getConnection (DBusConnection .DBusBusType .SYSTEM );
88+ } catch (DBusException e ) {
89+ throw e ;
90+ }
91+ }
92+ }
93+
8894 public boolean isSupported () {
8995 return wallet .isEnabled ();
9096 }
0 commit comments