Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. my $action = '';
  6. my $filename = '-';
  7.  
  8. for my $arg (@ARGV){
  9. if ($arg eq "-e" or $arg eq "--export"){
  10. $action = 'export';
  11. } elsif ($arg eq "-i" or $arg eq "--import"){
  12. $action = 'import';
  13. } elsif ($arg eq "-h" or $arg eq "--help"){
  14. print "Import and export keybindingsn";
  15. print " -e, --export <filename>n";
  16. print " -i, --import <filename>n";
  17. print " -h, --helpn";
  18. exit;
  19. } elsif ($arg =~ /^-/){
  20. die "Unknown argument $arg";
  21. } else {
  22. $filename = $arg;
  23. if (!$action){
  24. if ( -e $filename){
  25. $action='import';
  26. } else {
  27. $action='export';
  28. }
  29. }
  30. }
  31. }
  32.  
  33. $action='export' if (!$action);
  34. if ($action eq 'export'){
  35. &export();
  36. } else {
  37. &import();
  38. }
  39.  
  40. sub export(){
  41. my $gsettingsFolders = [
  42. ['org.gnome.desktop.wm.keybindings','.'],
  43. ['org.gnome.settings-daemon.plugins.power','button'],
  44. ['org.gnome.settings-daemon.plugins.media-keys','.'],
  45. ];
  46.  
  47. my $customBindings = [
  48. ];
  49.  
  50. $filename = ">$filename";
  51. open (my $fh, $filename) || die "Can't open file $filename: $!";
  52.  
  53. for my $folder (@$gsettingsFolders){
  54. my @keylist = split(/n/, `gsettings list-recursively $folder->[0]`);
  55. foreach my $line (@keylist){
  56. if ($line =~ /^([^ ]+) ([^ ]+)(?: @[a-z]+)? (.*)/){
  57. my ($path, $name, $value) = ($1,$2,$3);
  58. if ($name eq "custom-keybindings"){
  59. $value =~ s/[[]' ]//g;
  60. my @c = split(/,/, $value);
  61. $customBindings = @c;
  62. } elsif ($name =~ /$folder->[1]/){
  63. if ($value =~ /^[|'/){
  64. if ($value =~ /^['(?:disabled)?']$/){
  65. $value = '[]';
  66. }
  67. print $fh "$patht$namet$valuen";
  68. }
  69. }
  70. } else {
  71. die "Could note parse $line";
  72. }
  73. }
  74. }
  75.  
  76. for my $folder (@$customBindings){
  77. my $gs = `gsettings list-recursively org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$folder`;
  78. my ($binding) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding binding ('[^n]+')/g;
  79. my ($command) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding command ('[^n]+')/g;
  80. my ($name) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding name ('[^n]+')/g;
  81. $command =~ s/"/\"/g;
  82. $command =~ s/^'(.*)'$/$1/g;
  83. $command =~ s/'/'\''/g;
  84. $command = "'$command'";
  85. print $fh "customt$namet$commandt$bindingn"
  86. }
  87.  
  88. close($fh);
  89. }
  90.  
  91. sub import(){
  92.  
  93. $filename = "<$filename";
  94. open (my $fh, $filename) || die "Can't open file $filename: $!";
  95.  
  96. my $customcount=0;
  97.  
  98. while (my $line = <$fh>){
  99. chomp $line;
  100. if ($line){
  101. my @v = split(/t/, $line);
  102. if (@v[0] eq 'custom'){
  103. my ($custom, $name, $command, $binding) = @v;
  104. print "Installing custom keybinding: $namen";
  105. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ name "$name"`;
  106. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ command "$command"`;
  107. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ binding "$binding"`;
  108. $customcount++;
  109. } else {
  110. my ($path, $name, $value) = @v;
  111. print "Importing $path $namen";
  112. print `gsettings set "$path" "$name" "$value"`;
  113. }
  114. }
  115. }
  116. if ($customcount > 0){
  117. my $customlist = "";
  118. for (my $i=0; $i<$customcount; $i++){
  119. $customlist .= "," if ($customlist);
  120. $customlist .= "'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'";
  121. }
  122. $customlist = "[$customlist]";
  123. print "Importing list of custom keybindings.n";
  124. print `gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "$customlist"`;
  125. }
  126.  
  127. close($fh);
  128. }
  129.  
  130. dconf dump / >~/.config/dconf/user.conf
  131.  
  132. editor ~/.config/dconf/user.conf
  133.  
  134. dconf watch /
  135.  
  136. dconf load / <~/.config/dconf/user.conf
  137.  
  138. $HOME/.gconf/desktop/gnome/keybindings
  139.  
  140. #!/usr/bin/perl
  141.  
  142. use strict;
  143.  
  144. my $action = '';
  145. my $filename = '-';
  146.  
  147. for my $arg (@ARGV){
  148. if ($arg eq "-e" or $arg eq "--export"){
  149. $action = 'export';
  150. } elsif ($arg eq "-i" or $arg eq "--import"){
  151. $action = 'import';
  152. } elsif ($arg eq "-h" or $arg eq "--help"){
  153. print "Import and export keybindingsn";
  154. print " -e, --export <filename>n";
  155. print " -i, --import <filename>n";
  156. print " -h, --helpn";
  157. exit;
  158. } elsif ($arg =~ /^-/){
  159. die "Unknown argument $arg";
  160. } else {
  161. $filename = $arg;
  162. if (!$action){
  163. if ( -e $filename){
  164. $action='import';
  165. } else {
  166. $action='export';
  167. }
  168. }
  169. }
  170. }
  171.  
  172. $action='export' if (!$action);
  173. if ($action eq 'export'){
  174. &export();
  175. } else {
  176. &import();
  177. }
  178.  
  179. sub export(){
  180. my $gsettingsFolders = [
  181. ['org.gnome.desktop.wm.keybindings','.'],
  182. ['org.gnome.settings-daemon.plugins.power','button'],
  183. ['org.gnome.settings-daemon.plugins.media-keys','.'],
  184. ];
  185.  
  186. my $customBindings = [
  187. ];
  188.  
  189. $filename = ">$filename";
  190. open (my $fh, $filename) || die "Can't open file $filename: $!";
  191.  
  192. for my $folder (@$gsettingsFolders){
  193. my @keylist = split(/n/, `gsettings list-recursively $folder->[0]`);
  194. foreach my $line (@keylist){
  195. if ($line =~ /^([^ ]+) ([^ ]+)(?: @[a-z]+)? (.*)/){
  196. my ($path, $name, $value) = ($1,$2,$3);
  197. if ($name eq "custom-keybindings"){
  198. $value =~ s/[[]' ]//g;
  199. my @c = split(/,/, $value);
  200. $customBindings = @c;
  201. } elsif ($name =~ /$folder->[1]/){
  202. if ($value =~ /^[|'/){
  203. if ($value =~ /^['(?:disabled)?']$/){
  204. $value = '[]';
  205. }
  206. print $fh "$patht$namet$valuen";
  207. }
  208. }
  209. } else {
  210. die "Could note parse $line";
  211. }
  212. }
  213. }
  214.  
  215. for my $folder (@$customBindings){
  216. my $gs = `gsettings list-recursively org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$folder`;
  217. my ($binding) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding binding ('[^n]+')/g;
  218. my ($command) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding command ('[^n]+')/g;
  219. my ($name) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding name ('[^n]+')/g;
  220. $command =~ s/"/\"/g;
  221. $command =~ s/^'(.*)'$/$1/g;
  222. $command =~ s/'/'\''/g;
  223. $command = "'$command'";
  224. print $fh "customt$namet$commandt$bindingn"
  225. }
  226.  
  227. close($fh);
  228. }
  229.  
  230. sub import(){
  231.  
  232. $filename = "<$filename";
  233. open (my $fh, $filename) || die "Can't open file $filename: $!";
  234.  
  235. my $customcount=0;
  236.  
  237. while (my $line = <$fh>){
  238. chomp $line;
  239. if ($line){
  240. my @v = split(/t/, $line);
  241. if (@v[0] eq 'custom'){
  242. my ($custom, $name, $command, $binding) = @v;
  243. print "Installing custom keybinding: $namen";
  244. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ name "$name"`;
  245. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ command "$command"`;
  246. print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ binding "$binding"`;
  247. $customcount++;
  248. } else {
  249. my ($path, $name, $value) = @v;
  250. print "Importing $path $namen";
  251. print `gsettings set "$path" "$name" "$value"`;
  252. }
  253. }
  254. }
  255. if ($customcount > 0){
  256. my $customlist = "";
  257. for (my $i=0; $i<$customcount; $i++){
  258. $customlist .= "," if ($customlist);
  259. $customlist .= "'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'";
  260. }
  261. $customlist = "[$customlist]";
  262. print "Importing list of custom keybindings.n";
  263. print `gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "$customlist"`;
  264. }
  265.  
  266. close($fh);
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement