extract common code

Change-Id: I9327f232f1edb9ef052eb7868ac60774f3b6e4cf
Reviewed-on: https://gerrit.libreoffice.org/10072
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>