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,12 @@ 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 ();
27
+ }
28
+
29
+ @ Override
30
+ public String displayName () {
31
+ return "KDE Wallet" ;
43
32
}
44
33
45
34
@ Override
@@ -85,6 +74,28 @@ public ConnectedWallet(DBusConnection connection) {
85
74
this .wallet = new KDEWallet (connection );
86
75
}
87
76
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
+
88
99
public boolean isSupported () {
89
100
return wallet .isEnabled ();
90
101
}
0 commit comments