4
4
import org .cryptomator .integrations .keychain .KeychainAccessException ;
5
5
import org .cryptomator .integrations .keychain .KeychainAccessProvider ;
6
6
import org .freedesktop .dbus .connections .impl .DBusConnection ;
7
+ import org .freedesktop .dbus .exceptions .DBusConnectionException ;
7
8
import org .freedesktop .dbus .exceptions .DBusException ;
8
9
import org .kde .KWallet ;
9
10
import org .kde .Static ;
@@ -22,24 +23,7 @@ public class KDEWalletKeychainAccess implements KeychainAccessProvider {
22
23
private final Optional <ConnectedWallet > wallet ;
23
24
24
25
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 ();
43
27
}
44
28
45
29
@ Override
@@ -85,6 +69,28 @@ public ConnectedWallet(DBusConnection connection) {
85
69
this .wallet = new KDEWallet (connection );
86
70
}
87
71
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
+
88
94
public boolean isSupported () {
89
95
return wallet .isEnabled ();
90
96
}
0 commit comments