dbusUtils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const Gio = imports.gi.Gio;
  2. const GLib = imports.gi.GLib;
  3. var NautilusFileOperationsProxy;
  4. var FreeDesktopFileManagerProxy;
  5. var GnomeNautilusPreviewerProxy;
  6. const NautilusFileOperationsInterface = `<node>
  7. <interface name='org.gnome.Nautilus.FileOperations'>
  8. <method name='CopyURIs'>
  9. <arg name='URIs' type='as' direction='in'/>
  10. <arg name='Destination' type='s' direction='in'/>
  11. </method>
  12. <method name='MoveURIs'>
  13. <arg name='URIs' type='as' direction='in'/>
  14. <arg name='Destination' type='s' direction='in'/>
  15. </method>
  16. <method name='EmptyTrash'>
  17. </method>
  18. <method name='TrashFiles'>
  19. <arg name='URIs' type='as' direction='in'/>
  20. </method>
  21. <method name='CreateFolder'>
  22. <arg name='URI' type='s' direction='in'/>
  23. </method>
  24. <method name='RenameFile'>
  25. <arg name='URI' type='s' direction='in'/>
  26. <arg name='NewName' type='s' direction='in'/>
  27. </method>
  28. <method name='Undo'>
  29. </method>
  30. <method name='Redo'>
  31. </method>
  32. <property name='UndoStatus' type='i' access='read'/>
  33. </interface>
  34. </node>`;
  35. const NautilusFileOperationsProxyInterface = Gio.DBusProxy.makeProxyWrapper(NautilusFileOperationsInterface);
  36. const FreeDesktopFileManagerInterface = `<node>
  37. <interface name='org.freedesktop.FileManager1'>
  38. <method name='ShowItems'>
  39. <arg name='URIs' type='as' direction='in'/>
  40. <arg name='StartupId' type='s' direction='in'/>
  41. </method>
  42. <method name='ShowItemProperties'>
  43. <arg name='URIs' type='as' direction='in'/>
  44. <arg name='StartupId' type='s' direction='in'/>
  45. </method>
  46. </interface>
  47. </node>`;
  48. const FreeDesktopFileManagerProxyInterface = Gio.DBusProxy.makeProxyWrapper(FreeDesktopFileManagerInterface);
  49. const GnomeNautilusPreviewerInterface = `<node>
  50. <interface name='org.gnome.NautilusPreviewer'>
  51. <method name='ShowFile'>
  52. <arg name='FileUri' type='s' direction='in'/>
  53. <arg name='ParentXid' type='i' direction='in'/>
  54. <arg name='CloseIfShown' type='b' direction='in'/>
  55. </method>
  56. </interface>
  57. </node>`;
  58. const GnomeNautilusPreviewerProxyInterface = Gio.DBusProxy.makeProxyWrapper(GnomeNautilusPreviewerInterface);
  59. function init() {
  60. NautilusFileOperationsProxy = new NautilusFileOperationsProxyInterface(
  61. Gio.DBus.session,
  62. 'org.gnome.Nautilus',
  63. '/org/gnome/Nautilus',
  64. (proxy, error) => {
  65. if (error) {
  66. log('Error connecting to Nautilus');
  67. }
  68. }
  69. );
  70. FreeDesktopFileManagerProxy = new FreeDesktopFileManagerProxyInterface(
  71. Gio.DBus.session,
  72. 'org.freedesktop.FileManager1',
  73. '/org/freedesktop/FileManager1',
  74. (proxy, error) => {
  75. if (error) {
  76. log('Error connecting to Nautilus');
  77. }
  78. }
  79. );
  80. GnomeNautilusPreviewerProxy = new GnomeNautilusPreviewerProxyInterface(
  81. Gio.DBus.session,
  82. 'org.gnome.NautilusPreviewer',
  83. '/org/gnome/NautilusPreviewer',
  84. (proxy, error) => {
  85. if (error) {
  86. log('Error connecting to Nautilus Previewer');
  87. }
  88. }
  89. );
  90. }
  91. function openFileWithOtherApplication(filePath) {
  92. let fdList = new Gio.UnixFDList();
  93. let channel = GLib.IOChannel.new_file(filePath, "r");
  94. fdList.append(channel.unix_get_fd());
  95. channel.set_close_on_unref(true);
  96. let builder = GLib.VariantBuilder.new(GLib.VariantType.new("a{sv}"));
  97. let options = builder.end();
  98. let parameters = GLib.Variant.new_tuple([GLib.Variant.new_string("0"),
  99. GLib.Variant.new_handle(0),
  100. options]);
  101. Gio.bus_get(Gio.BusType.SESSION, null,
  102. (source, result) => {
  103. let dbus_connection = Gio.bus_get_finish(result);
  104. dbus_connection.call_with_unix_fd_list("org.freedesktop.portal.Desktop",
  105. "/org/freedesktop/portal/desktop",
  106. "org.freedesktop.portal.OpenURI",
  107. "OpenFile",
  108. parameters,
  109. GLib.VariantType.new("o"),
  110. Gio.DBusCallFlags.NONE,
  111. -1,
  112. fdList,
  113. null,
  114. null);
  115. }
  116. );
  117. }