Source

perl/keyring/kdewallet / experiments / kwallet_by_dbus.pl

Full commit
#!/usr/bin/perl -w

# See http://stderr.org/doc/kde/HTML/en/kdelibs-apidocs/kwallet/html/classKWallet_1_1Wallet.html

use strict;
use Net::DBus;
use Data::Dumper;
use feature 'say';

my $APP_NAME = "DBUStest";
my $FOLDER_NAME = "DBUStestfolder";

my $dbus = Net::DBus->session()
  or die("KWallet not available (can't access DBus)");
#print "dbus: ", Dumper($dbus);
say "DBus connected"; 

my $kwallet_svc = $dbus->get_service('org.kde.kwalletd')
  or die("KWallet not available (can't access KWallet, likely kwalletd not running)");
#print "kwalletd: ", Dumper($kwallet_svc);
say "org.kde.kwalletd connected";

my $kwallet = $kwallet_svc->get_object('/modules/kwalletd', 'org.kde.KWallet')
  or die("Kwallet not available (can't find wallet)");
#print "kwallet: ", Dumper($kwallet);
say "/modules/kwalletd connected";
# $kwallet = $kwallet->as_interface(...);

# This is text name
my $net_wallet = $kwallet->networkWallet()
  or die "No net wallet";
say "net_wallet id: $net_wallet";

my $handle = $kwallet->open($net_wallet, 0, $APP_NAME); # 0 - window id
$handle > 0 or die "No handle"; # decline means -1
say "Opened, handle = $handle";

my $is_open = $kwallet->isOpen($handle);
say "is open($handle): $is_open";

my $folders = $kwallet->folderList($handle, $APP_NAME);
say "Folders = ", join(", ", @$folders);

unless($kwallet->hasFolder($handle, $FOLDER_NAME, $APP_NAME)) {
    say "Folder $FOLDER_NAME does not exists";
    $kwallet->createFolder($handle, $FOLDER_NAME, $APP_NAME) or die "Failed to create";
    say "Folder $FOLDER_NAME created";
} else {
    say "Folder $FOLDER_NAME already exists";
}

my $password = $kwallet->readPassword($handle, $FOLDER_NAME, "Some key", $APP_NAME);
say "Read password: ", $password || "(undef)";

my $sth = $kwallet->writePassword($handle, $FOLDER_NAME, "Some key", "Some pass 1", $APP_NAME);
say "Wrote password, got $sth";
# NOn-zero output seems to mean failure

my $password = $kwallet->readPassword($handle, $FOLDER_NAME, "Some key", $APP_NAME);
say "Read password: ", $password || "(undef)";

my $sth = $kwallet->writePassword($handle, $FOLDER_NAME, "Some key", "Some pass 2", $APP_NAME);
say "Wrote password, got $sth";

my $password = $kwallet->readPassword($handle, $FOLDER_NAME, "Some key", $APP_NAME);
say "Read password: ", $password || "(undef)";

my $sth = $kwallet->writePassword($handle, $FOLDER_NAME, "Some key", undef, $APP_NAME);
say "Wrote undef password, got $sth";

my $password = $kwallet->readPassword($handle, $FOLDER_NAME, "Some key", $APP_NAME);
say "Read password: ", $password || "(undef)";




$kwallet->removeFolder($handle, $FOLDER_NAME, $APP_NAME);
say "Folder $FOLDER_NAME removed";

# Po close przy ponownym otwieraniu będzie pytanie (cośtam prosi...), bez tego - nie, mimo końca programu
$kwallet->close($handle, 1, $APP_NAME); # drugi parametr: force
say "Closed $handle";

$is_open = $kwallet->isOpen($handle);
say "is open($handle): $is_open";