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,12 @@ 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 ();
27+ }
28+
29+ @ Override
30+ public String displayName () {
31+ return "KDE Wallet" ;
4332 }
4433
4534 @ Override
@@ -85,6 +74,28 @@ public ConnectedWallet(DBusConnection connection) {
8574 this .wallet = new KDEWallet (connection );
8675 }
8776
77+ static Optional <ConnectedWallet > connect () {
78+ try {
79+ return Optional .of (new ConnectedWallet (getConnection ()));
80+ } catch (DBusException e ) {
81+ LOG .warn ("Connecting to D-Bus failed." , e );
82+ return Optional .empty ();
83+ }
84+ }
85+
86+ private static DBusConnection getConnection () throws DBusException {
87+ try {
88+ return DBusConnection .getConnection (DBusConnection .DBusBusType .SESSION );
89+ } catch (DBusConnectionException ce ) {
90+ LOG .warn ("SESSION DBus not found, falling back to SYSTEM DBus" );
91+ try {
92+ return DBusConnection .getConnection (DBusConnection .DBusBusType .SYSTEM );
93+ } catch (DBusException e ) {
94+ throw e ;
95+ }
96+ }
97+ }
98+
8899 public boolean isSupported () {
89100 return wallet .isEnabled ();
90101 }
0 commit comments