~ubuntu-branches/ubuntu/trusty/ubufox/trusty-updates

« back to all changes in this revision

Viewing changes to res/libs/gio.jsm

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-11-12 13:27:26 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20121112132726-ssrixfmjgibo8f3s
Tags: 2.6-0ubuntu1
* New upstream release.
  - Translation updates, thanks to Adolfo Jayme Barrientos and David Planella

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 *
37
37
 * ***** END LICENSE BLOCK ***** */
38
38
 
 
39
const { utils: Cu } = Components;
 
40
 
39
41
var EXPORTED_SYMBOLS = [ "gio" ];
40
42
 
41
43
const GIO_LIBNAME = "gio-2.0";
42
44
const GIO_ABIS = [ 0 ];
43
45
 
44
 
const Cu = Components.utils;
45
 
 
46
46
Cu.import("resource://gre/modules/ctypes.jsm");
47
 
Cu.import("resource://ubufox/libs/ctypes-utils.jsm");
 
47
Cu.import("resource://ubufox/modules/utils.jsm");
48
48
Cu.import("resource://ubufox/libs/gobject.jsm");
49
49
Cu.import("resource://ubufox/libs/glib.jsm");
50
50
 
51
 
["LOG", "WARN", "ERROR"].forEach(function(aName) {
52
 
  this.__defineGetter__(aName, function() {
53
 
    Components.utils.import("resource://gre/modules/AddonLogging.jsm");
54
 
 
55
 
    LogManager.getLogger("ubufox.gio", this);
56
 
    return this[aName];
57
 
  });
58
 
}, this);
59
 
 
60
 
var asyncCbHandlers = {};
61
 
var asyncCbIdSerial = 0;
62
 
 
63
 
function AsyncCbData(cb, ccb) {
64
 
  this.cb = cb,
65
 
  this.ccb = ccb;
 
51
function getString(aWrappee) {
 
52
  let res = aWrappee.apply(null, Array.prototype.slice.call(arguments, 1));
 
53
  return res.isNull() ? null : res.readString();
66
54
}
67
55
 
68
 
function newAsyncCbId() {
69
 
  while (asyncCbHandlers[++asyncCbIdSerial]) {}
70
 
  return asyncCbIdSerial;
 
56
function getOwnedString(aWrappee) {
 
57
  let res;
 
58
  try {
 
59
    res = aWrappee.apply(null, Array.prototype.slice.call(arguments, 1));
 
60
    return res.isNull() ? null : res.readString();
 
61
  } finally { glib.g_free(res); }
71
62
}
72
63
 
73
64
function gio_defines(lib) {
74
65
  // Enums
75
 
  // GBusType
76
 
  this.GBusType = ctypes.int;
77
 
  this.G_BUS_TYPE_STARTER = -1;
78
 
  this.G_BUS_TYPE_NONE = 0;
79
 
  this.G_BUS_TYPE_SYSTEM = 1;
80
 
  this.G_BUS_TYPE_SESSION = 2;
81
 
 
82
 
  // GDBusProxyFlags
83
 
  this.GDBusProxyFlags = ctypes.int;
84
 
  this.G_DBUS_PROXY_FLAGS_NONE = 0;
85
 
  this.G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES = (1<<0);
86
 
  this.G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS = (1<<1);
87
 
  this.G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START = (1<<2);
88
 
 
89
 
  // GDBusCallFlags
90
 
  this.GDBusCallFlags = ctypes.int;
91
 
  this.G_DBUS_CALL_FLAGS_NONE = 0;
92
 
  this.G_DBUS_CALL_FLAGS_NO_AUTO_START = (1<<0);
93
 
 
94
 
  // GFileMonitorFlags
95
 
  this.GFileMonitorFlags = ctypes.int;
96
 
  this.G_FILE_MONITOR_NONE = 0;
97
 
  this.G_FILE_MONITOR_WATCH_MOUNTS = (1<<0);
98
 
  this.G_FILE_MONITOR_SEND_MOVED = (1<<1);
99
 
 
100
 
  // GFileMonitorEvent
101
 
  this.GFileMonitorEvent = ctypes.int;
102
 
  this.G_FILE_MONITOR_EVENT_CHANGED = 0;
103
 
  this.G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT = 1;
104
 
  this.G_FILE_MONITOR_EVENT_DELETED = 2;
105
 
  this.G_FILE_MONITOR_EVENT_CREATED = 3;
106
 
  this.G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED = 4;
107
 
  this.G_FILE_MONITOR_EVENT_PRE_UNMOUNT = 5;
108
 
  this.G_FILE_MONITOR_EVENT_UNMOUNTED = 6;
109
 
  this.G_FILE_MONITOR_EVENT_MOVED = 7;
 
66
  CTypesUtils.defineEnums(this, "GBusType", -1, [
 
67
    "G_BUS_TYPE_STARTER",
 
68
    "G_BUS_TYPE_NONE",
 
69
    "G_BUS_TYPE_SYSTEM",
 
70
    "G_BUS_TYPE_SESSION"
 
71
  ]);
 
72
 
 
73
  CTypesUtils.defineFlags(this, "GDBusProxyFlags", 0, [
 
74
    "G_DBUS_PROXY_FLAGS_NONE",
 
75
    "G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES",
 
76
    "G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS",
 
77
    "G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START"
 
78
  ]);
 
79
 
 
80
  CTypesUtils.defineFlags(this, "GDBusCallFlags", 0, [
 
81
    "G_DBUS_CALL_FLAGS_NONE",
 
82
    "G_DBUS_CALL_FLAGS_NO_AUTO_START"
 
83
  ]);
 
84
 
 
85
  CTypesUtils.defineFlags(this, "GFileMonitorFlags", 0, [
 
86
    "G_FILE_MONITOR_NONE",
 
87
    "G_FILE_MONITOR_WATCH_MOUNTS",
 
88
    "G_FILE_MONITOR_SEND_MOVED"
 
89
  ]);
 
90
 
 
91
  CTypesUtils.defineEnums(this, "GFileMonitorEvent", 0, [
 
92
    "G_FILE_MONITOR_EVENT_CHANGED",
 
93
    "G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT",
 
94
    "G_FILE_MONITOR_EVENT_DELETED",
 
95
    "G_FILE_MONITOR_EVENT_CREATED",
 
96
    "G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED",
 
97
    "G_FILE_MONITOR_EVENT_PRE_UNMOUNT",
 
98
    "G_FILE_MONITOR_EVENT_UNMOUNTED",
 
99
    "G_FILE_MONITOR_EVENT_MOVED"
 
100
  ]);
110
101
 
111
102
  // GIOErrorEnum
112
 
  this.G_IO_ERROR_DBUS_ERROR = 36;
 
103
  CTypesUtils.defineSimple(this, "G_IO_ERROR_DBUS_ERROR", 36);
113
104
 
114
105
  // Types
115
 
  this.GDBusInterfaceInfo = ctypes.StructType("GDBusInterfaceInfo");
116
 
  this.GCancellable = ctypes.StructType("GCancellable");
117
 
  this.GAsyncResult = ctypes.StructType("GAsyncResult");
118
 
  this.GDBusProxy = ctypes.StructType("GDBusProxy");
119
 
  this.GFile = ctypes.StructType("GFile");
120
 
  this.GFileMonitor = ctypes.StructType("GFileMonitor");
 
106
  CTypesUtils.defineSimple(this, "GDBusInterfaceInfo",
 
107
                           ctypes.StructType("GDBusInterfaceInfo"));
 
108
  CTypesUtils.defineSimple(this, "GCancellable",
 
109
                           ctypes.StructType("GCancellable"));
 
110
  CTypesUtils.defineSimple(this, "GAsyncResult",
 
111
                           ctypes.StructType("GAsyncResult"));
 
112
  CTypesUtils.defineSimple(this, "GDBusProxy",
 
113
                           ctypes.StructType("GDBusProxy"));
 
114
  CTypesUtils.defineSimple(this, "GFile",
 
115
                           ctypes.StructType("GFile"));
 
116
  CTypesUtils.defineSimple(this, "GFileMonitor",
 
117
                           ctypes.StructType("GFileMonitor"));
121
118
 
122
119
  // Templates
123
 
  this.GAsyncReadyCallback = ctypes.FunctionType(ctypes.default_abi,
124
 
                                                 ctypes.void_t,
125
 
                                                 [gobject.GObject.ptr,
126
 
                                                  this.GAsyncResult.ptr,
127
 
                                                  glib.gpointer]).ptr;
 
120
  CTypesUtils.defineSimple(this, "GAsyncReadyCallback",
 
121
                           ctypes.FunctionType(ctypes.default_abi,
 
122
                                               ctypes.void_t,
 
123
                                               [gobject.GObject.ptr,
 
124
                                                this.GAsyncResult.ptr,
 
125
                                                glib.gpointer]).ptr);
128
126
 
129
127
  // Functions
130
 
  lib.lazy_bind("g_dbus_proxy_new_for_bus", ctypes.void_t, this.GBusType,
131
 
                this.GDBusProxyFlags, this.GDBusInterfaceInfo.ptr,
132
 
                glib.gchar.ptr, glib.gchar.ptr, glib.gchar.ptr,
133
 
                this.GCancellable.ptr, this.GAsyncReadyCallback, glib.gpointer);
 
128
  lib.lazy_bind_with_wrapper("g_dbus_proxy_new_for_bus", function(aWrappee,
 
129
                                                                  aBusType,
 
130
                                                                  aFlags,
 
131
                                                                  aInterfaceInfo,
 
132
                                                                  aName, aPath,
 
133
                                                                  aInterface,
 
134
                                                                  aCancellable,
 
135
                                                                  aCallback) {
 
136
    var ccw = CTypesUtils.wrapCallback(aCallback,
 
137
                                       {type: gio.GAsyncReadyCallback,
 
138
                                        root: true, singleshot: true});
 
139
 
 
140
    try {
 
141
      aWrappee(aBusType, aFlags, aInterfaceInfo, aName, aPath, aInterface,
 
142
               aCancellable, ccw, null);
 
143
    } catch(e) {
 
144
      CTypesUtils.unrootCallback(aCallback);
 
145
      throw e;
 
146
    }
 
147
  }, ctypes.void_t, [this.GBusType, this.GDBusProxyFlags,
 
148
                     this.GDBusInterfaceInfo.ptr, glib.gchar.ptr,
 
149
                     glib.gchar.ptr, glib.gchar.ptr, this.GCancellable.ptr,
 
150
                     this.GAsyncReadyCallback, glib.gpointer]);
134
151
  lib.lazy_bind("g_dbus_proxy_new_for_bus_finish", this.GDBusProxy.ptr,
135
 
                this.GAsyncResult.ptr, glib.GError.ptr.ptr);
136
 
  lib.lazy_bind("g_dbus_proxy_call", ctypes.void_t, this.GDBusProxy.ptr,
137
 
                glib.gchar.ptr, glib.GVariant.ptr, this.GDBusCallFlags,
138
 
                glib.gint, this.GCancellable.ptr, this.GAsyncReadyCallback,
139
 
                glib.gpointer);
 
152
                [this.GAsyncResult.ptr, glib.GError.ptr.ptr]);
 
153
  lib.lazy_bind_with_wrapper("g_dbus_proxy_call", function(aWrappee, aProxy,
 
154
                                                           aMethod, aParams,
 
155
                                                           aFlags, aTimeout,
 
156
                                                           aCancellable,
 
157
                                                           aCallback) {
 
158
    var ccw = CTypesUtils.wrapCallback(aCallback,
 
159
                                       {type: gio.GAsyncReadyCallback,
 
160
                                        root: true, singleshot: true});
 
161
 
 
162
    try {
 
163
      aWrappee(aProxy, aMethod, aParams, aFlags, aTimeout, aCancellable,
 
164
               ccw, null);
 
165
    } catch(e) {
 
166
      CTypesUtils.unrootCallback(aCallback);
 
167
      throw e;
 
168
    }
 
169
  }, ctypes.void_t, [this.GDBusProxy.ptr, glib.gchar.ptr, glib.GVariant.ptr,
 
170
                     this.GDBusCallFlags, glib.gint, this.GCancellable.ptr,
 
171
                     this.GAsyncReadyCallback, glib.gpointer]);
140
172
  lib.lazy_bind("g_dbus_proxy_call_finish", glib.GVariant.ptr,
141
 
                this.GDBusProxy.ptr, this.GAsyncResult.ptr, glib.GError.ptr.ptr);
142
 
  lib.lazy_bind("g_dbus_proxy_get_name", glib.gchar.ptr, this.GDBusProxy.ptr);
143
 
  lib.lazy_bind("g_dbus_proxy_get_interface_name", glib.gchar.ptr,
144
 
                this.GDBusProxy.ptr);
145
 
  lib.lazy_bind("g_dbus_proxy_get_object_path", glib.gchar.ptr, this.GDBusProxy.ptr);
 
173
                [this.GDBusProxy.ptr, this.GAsyncResult.ptr,
 
174
                 glib.GError.ptr.ptr]);
 
175
  lib.lazy_bind_with_wrapper("g_dbus_proxy_get_name", getString,
 
176
                             glib.gchar.ptr, [this.GDBusProxy.ptr]);
 
177
  lib.lazy_bind_with_wrapper("g_dbus_proxy_get_interface_name", getString,
 
178
                             glib.gchar.ptr, [this.GDBusProxy.ptr]);
 
179
  lib.lazy_bind_with_wrapper("g_dbus_proxy_get_object_path", getString,
 
180
                             glib.gchar.ptr, [this.GDBusProxy.ptr]);
146
181
  lib.lazy_bind("g_io_error_quark", glib.GQuark);
147
 
  lib.lazy_bind("g_dbus_error_get_remote_error", glib.gchar.ptr, glib.GError.ptr);
148
 
  lib.lazy_bind("g_dbus_proxy_get_name_owner", glib.gchar.ptr, this.GDBusProxy.ptr);
149
 
  lib.lazy_bind("g_file_new_for_path", this.GFile.ptr, ctypes.char.ptr);
150
 
  lib.lazy_bind("g_file_monitor_file", this.GFileMonitor.ptr, this.GFile.ptr,
151
 
                this.GFileMonitorFlags, this.GCancellable.ptr, glib.GError.ptr);
152
 
  lib.lazy_bind("g_file_monitor_directory", this.GFileMonitor.ptr, this.GFile.ptr,
153
 
                this.GFileMonitorFlags, this.GCancellable.ptr, glib.GError.ptr);
154
 
  lib.lazy_bind("g_file_get_path", ctypes.char.ptr, this.GFile.ptr);
155
 
 
156
 
  // Helpers to work around jsctypes limitations
157
 
  this.GDbusProxyNewForBus = function(aType, aFlags, aInfo, aName,
158
 
                                      aPath, aInterface, aCancellable,
159
 
                                      aCallback) {
160
 
    let cb = function(aObject, aResult, aData) {
161
 
      try {
162
 
        aCallback(aObject, aResult);
163
 
      } catch(e) {
164
 
        Cu.reportError(e);
165
 
      }
166
 
      delete asyncCbHandlers[id];
167
 
    };
168
 
    let ccb = gio.GAsyncReadyCallback(cb);
169
 
 
170
 
    var id = newAsyncCbId();
171
 
    asyncCbHandlers[id] = new AsyncCbData(cb, ccb);
172
 
 
173
 
    gio.g_dbus_proxy_new_for_bus(aType, aFlags, aInfo, aName, aPath,
174
 
                                 aInterface, aCancellable, ccb, null);
175
 
  };
176
 
 
177
 
  this.GDbusProxyCall = function(aProxy, aMethod, aParams, aFlags, aTimeout,
178
 
                                 aCancellable, aCallback) {
179
 
    let cb = function(aObject, aResult, aData) {
180
 
      try {
181
 
        aCallback(aObject, aResult);
182
 
      } catch(e) {
183
 
        Cu.reportError(e);
184
 
      }
185
 
      delete asyncCbHandlers[id];
186
 
    };
187
 
    let ccb = gio.GAsyncReadyCallback(cb);
188
 
 
189
 
    var id = newAsyncCbId();
190
 
    asyncCbHandlers[id] = new AsyncCbData(cb, ccb);
191
 
 
192
 
    gio.g_dbus_proxy_call(aProxy, aMethod, aParams, aFlags, aTimeout,
193
 
                          aCancellable, ccb, null);
194
 
  };
195
 
 
196
 
  this.G_IO_ERROR = this.g_io_error_quark();
 
182
  lib.lazy_bind_with_wrapper("g_dbus_error_get_remote_error", getOwnedString,
 
183
                             glib.gchar.ptr, [glib.GError.ptr]);
 
184
  lib.lazy_bind_with_wrapper("g_dbus_proxy_get_name_owner", getOwnedString,
 
185
                             glib.gchar.ptr, [this.GDBusProxy.ptr]);
 
186
  lib.lazy_bind("g_file_new_for_path", this.GFile.ptr, [glib.gchar.ptr]);
 
187
  lib.lazy_bind("g_file_monitor_file", this.GFileMonitor.ptr, [this.GFile.ptr,
 
188
                this.GFileMonitorFlags, this.GCancellable.ptr,
 
189
                glib.GError.ptr]);
 
190
  lib.lazy_bind("g_file_monitor_directory", this.GFileMonitor.ptr,
 
191
                [this.GFile.ptr, this.GFileMonitorFlags,
 
192
                 this.GCancellable.ptr, glib.GError.ptr]);
 
193
  lib.lazy_bind_with_wrapper("g_file_get_path", getOwnedString,
 
194
                             glib.gchar.ptr, [this.GFile.ptr]);
 
195
 
 
196
  CTypesUtils.defineSimple(this, "G_IO_ERROR", this.g_io_error_quark());
 
197
 
 
198
  gobject.createSignal(this.GDBusProxy, "g-signal", ctypes.void_t,
 
199
                       [this.GDBusProxy.ptr, glib.gchar.ptr,
 
200
                        glib.gchar.ptr, glib.GVariant.ptr,
 
201
                        glib.gpointer]);
 
202
  gobject.createSignal(this.GFileMonitor, "changed", ctypes.void_t,
 
203
                       [this.GFileMonitor.ptr, this.GFile.ptr,
 
204
                        this.GFile.ptr, this.GFileMonitorEvent,
 
205
                        glib.gpointer]);
197
206
}
198
207
 
199
 
new ctypes_library(GIO_LIBNAME, GIO_ABIS, gio_defines, this);
 
208
var gio = new CTypesUtils.newLibrary(GIO_LIBNAME, GIO_ABIS, gio_defines);