Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve signal handling for kdewallet #3

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there better alternatives? If at any point in the future the JPMS is introduced in this library, this leads to a dependency to the java.desktop module, which imho should not be present in this "system" library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot get the interconnection between the JPMS and the PropertyChangeListener pattern I introduced here.
Can you elaborate you point further?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern fine!

The classes from the java.beans package are part of the java.desktop module. So if this library will be migrated to the module system, it needs a dependeny to this module.

From my perspective, this is a "system" library and should not depend in java.desktop. But I'm also a little nitpicky here 😄

import java.util.Optional;

@Priority(900)
Expand Down Expand Up @@ -69,13 +71,25 @@ public void changePassphrase(String key, String displayName, CharSequence passph
wallet.get().changePassphrase(key, passphrase);
}

private static class ConnectedWallet {
private static class ConnectedWallet implements PropertyChangeListener {

private final KDEWallet wallet;
private int handle = -1;

public ConnectedWallet(DBusConnection connection) {
this.wallet = new KDEWallet(connection);
this.wallet.getSignalHandler().addPropertyChangeListener(this);
}

@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("KWallet.walletAsyncOpened")
&& event.getNewValue() instanceof Integer i) {
handle = i;
if (handle != -1) {
LOG.info("Wallet successfully opened.");
}
}
}

static Optional<ConnectedWallet> connect() {
Expand Down Expand Up @@ -185,15 +199,10 @@ private boolean walletIsOpen() throws KeychainAccessException {
return true;
}
wallet.openAsync(Static.DEFAULT_WALLET, 0, APP_NAME, false);
wallet.getSignalHandler().await(KWallet.walletAsyncOpened.class, Static.ObjectPaths.KWALLETD5, () -> null);
handle = wallet.getSignalHandler().getLastHandledSignal(KWallet.walletAsyncOpened.class, Static.ObjectPaths.KWALLETD5).handle;
LOG.debug("Wallet successfully initialized.");
return handle != -1;
} catch (RuntimeException e) {
throw new KeychainAccessException("Asynchronous opening the wallet failed.", e);
}
}


}
}