5
<title>content-hub.js - Content Hub JS API</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.8.0pr2/build/cssgrids/cssgrids-min.css">
7
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
9
<script src="http://yui.yahooapis.com/combo?3.8.0pr2/build/yui/yui-min.js"></script>
11
<body class="yui3-skin-sam">
14
<div id="hd" class="yui3-g header">
15
<div class="yui3-u-3-4">
16
<h1><a href="../index.html"><img src="../assets/css/logo.png">Content Hub JS API: content-hub.js</a></h1>
18
<div class="yui3-u-1-4 version">
19
<em>API Docs for: 0.1</em>
24
<div id="sidebar" class="yui3-u">
25
<div id="modules" class="sidebox">
27
<h2 class="no-toc">Modules</h2>
31
<li><a href="../modules/ContentHub.html">ContentHub</a>
37
<div id="classes" class="sidebox">
39
<h2 class="no-toc">Classes</h2>
43
<li><a href="../classes/ContentHub.html">ContentHub</a></li>
44
<li><a href="../classes/ContentPeer.html">ContentPeer</a></li>
45
<li><a href="../classes/ContentStore.html">ContentStore</a></li>
46
<li><a href="../classes/ContentTransfer.html">ContentTransfer</a></li>
55
<div id="fileTree" class="sidebox">
57
<h2 class="no-toc">Files</h2>
60
<ul><li>content-hub.js/<ul></ul></li></ul>
66
<div id="main" class="yui3-u">
67
<div class="content"><h4>content-hub.js</h4>
69
<pre class="code prettyprint linenums">
71
* ContentHub is the entry point to resource io transfer
72
from/to remote applications (peers).
77
function createContentHubApi(backendBridge) {
78
var PLUGIN_URI = 'ContentHub';
81
* ContentTransfer is an object created by the ContentHub to
82
and allows one to properly setup and manage a data
83
transfer between two peers.
85
* @class ContentTransfer
89
var api = external.getUnityObject('1.0');
90
var hub = api.ContentHub;
92
var pictureContentType = hub.ContentType.Pictures;
94
hub.defaultSourceForType(
97
hub.importContentForPeer(
101
[setup the transfer options and store]
102
transfer.start(function(state) { [...] });
106
function ContentTransfer(objectid, content) {
107
this._proxy = backendBridge.createRemoteObject(
108
PLUGIN_URI, 'ContentTransfer', objectid);
110
this._store = content && content.store
111
? content.store : null;
112
this._state = content && content.state
113
? content.state : null;
114
this._selectionType = content && content.selectionType
115
? content.selectionType : null;
116
this._direction = content && content.direction
117
? content.direction : null;
119
ContentTransfer.prototype = {
121
serialize: function() {
124
type: 'object-proxy',
125
apiid: 'ContentHub',
126
objecttype: 'ContentTransfer',
127
objectid: self._proxy.id(),
134
* Retrieves the current store.
136
* If the callback parameter is not set, the current "local" value is retrieved.
139
* @param callback (optional) {Function(String)}
141
store: function(callback) {
142
if (callback && typeof(callback) === 'function') {
143
this._proxy.call('store', [], callback);
149
* Sets the current store for the ContentTransfer.
152
* @param store {ContentStore}
153
* @param callback (optional) {Function()} called when the store has been updated
155
setStore: function(store, callback) {
156
this._proxy.call('setStore', [store.serialize(), callback]);
160
* Retrieves the current state.
162
* If the callback parameter is not set, the current "local" value is retrieved.
165
* @param callback (optional) {Function(ContentTransfer.State)}
167
state: function(callback) {
168
if (callback && typeof(callback) === 'function') {
169
this._proxy.call('state', [], callback);
175
* Sets the state of the transfer.
178
* @param state {ContentTransfer.State}
179
* @param callback {Function()} called when the state has been updated
181
setState: function(state, callback) {
182
this._proxy.call('setState', [state, callback]);
185
* Notifies the listener when the state of the transfer changes.
187
* @method onStateChanged
188
* @param callback {Function(ContentTransfer.State)}
190
onStateChanged: function(callback) {
191
this._proxy.call('onStateChanged', [callback]);
195
* Retrieves the current selection type.
197
* @method selectionType
198
* @param callback {Function(ContentTransfer.SelectionType)}
200
selectionType: function(callback) {
201
if (callback && typeof(callback) === 'function') {
202
this._proxy.call('selectionType', [], callback);
205
return this._selectionType;
208
* Sets the selection type (single or multiple).
210
* @method setSelectionType
211
* @param selectionType {ContentTransfer.SelectionType}
212
* @param callback {Function()} called when the state has been updated
214
setSelectionType: function(selectionType, callback) {
215
this._selectionType = selectionType;
216
this._proxy.call('setSelectionType', [selectionType, callback]);
220
* Retrieves the current transfer direction.
222
* If the callback parameter is not set, the current "local" value is retrieved.
225
* @param callback (optional) {Function(ContentTransfer.Direction)}
227
direction: function(callback) {
228
if (callback && typeof(callback) === 'function') {
229
this._proxy.call('direction', [], callback);
232
return this._direction;
235
* Sets the transfer direction (import or export).
237
* @method setDirection
238
* @param direction {ContentTransfer.Direction}
239
* @param callback {Function()} called when the state has been updated
241
setDirection: function(direction, callback) {
242
this._direction = direction;
243
this._proxy.call('setDirection', [direction, callback]);
247
* Retrieves the list of items associated with the ContentTransfer.
250
* @param callback {Function( {Object{name: , url: }} )}
252
items: function(callback) {
253
this._proxy.call('items', [], callback);
256
* Sets the list of items for the associated ContentTransfer (used when exporting).
259
* @param items {Array of Object{name: String, url: String}}
260
* @param callback {Function()} called when the state has been updated
262
setItems: function(items, callback) {
263
this._proxy.call('setItems', [items, callback]);
272
* @param callback {Function(ContentTransfer.State)}
274
start: function(callback) {
275
this._proxy.call('start', [callback]);
279
* Sets State to ContentTransfer.Finalized and cleans up temporary files.
283
finalize: function() {
284
this._proxy.call('finalize', []);
290
* Destroys the remote object. This proxy object is not valid anymore.
294
destroy: function() {
295
this._proxy.call('destroy', []);
300
* ContentPeer is an object returned by the ContentHub.
301
It represents a remote peer that can be used in a request
302
to import, export or share content.
309
var api = external.getUnityObject('1.0');
310
var hub = api.ContentHub;
312
var pictureContentType = hub.ContentType.Pictures;
314
hub.defaultSourceForType(
317
[do something with the peer]
320
function ContentPeer(objectid, content) {
321
this._proxy = backendBridge.createRemoteObject(
322
PLUGIN_URI, 'ContentPeer', objectid);
324
this._appId = content && content.appId
325
? content.appId : null;
326
this._name = content && content.name
327
? content.name : null;
328
this._handler = content && content.handler
329
? content.handler : null;
330
this._contentType = content && content.contentType
331
? content.contentType : null;
332
this._selectionType = content && content.selectionType
333
? content.selectionType : null;
334
this._isDefaultPeer = content && content.isDefaultPeer;
336
ContentPeer.prototype = {
338
serialize: function() {
341
type: 'object-proxy',
342
apiid: 'ContentHub',
343
objecttype: 'ContentPeer',
344
objectid: self._proxy.id(),
351
* Retrieves the app Id of the associated peer.
353
* If the callback parameter is not set, the current "local" value is retrieved.
356
* @return {String} Application Id for this peer
357
* @param callback (optional) {Function(String)}
359
appId: function(callback) {
360
if (callback && typeof(callback) === 'function') {
361
this._proxy.call('appId', [], callback);
367
* Sets the app Id of the associated peer.
370
* @param appId {String}
371
* @param callback {Function()} called when the appId has been updated
373
setAppId: function(appId, callback) {
374
this._proxy.call('setAppId', [appId, callback]);
378
* Retrieves the specific ContentHandler for this peer.
380
* If the callback parameter is not set, the current "local" value is retrieved.
383
* @return {String} ContentHandler for this peer
384
* @param callback (optional) {Function(String)}
386
handler: function(callback) {
387
if (callback && typeof(callback) === 'function') {
388
this._proxy.call('handler', [], callback);
391
return this._handler;
394
* Sets specific ContentHandler for this peer.
397
* @param handler {ContentHandler}
398
* @param callback {Function()} called when the appId has been updated
400
setHandler: function(handler, callback) {
401
this._proxy.call('setHandler', [handler, callback]);
405
* Retrieves the specific ContentType for this peer.
407
* If the callback parameter is not set, the current "local" value is retrieved.
409
* @method contentType
410
* @return {String} ContentType for this peer
411
* @param callback (optional) {Function(String)}
413
contentType: function(callback) {
414
if (callback && typeof(callback) === 'function') {
415
this._proxy.call('contentType', [], callback);
418
return this._contentType;
421
* Sets specific ContentType for this peer.
423
* @method setContentType
424
* @param contentType {ContentType}
425
* @param callback {Function()} called when the content type has been updated
427
setContentType: function(contentType, callback) {
428
this._proxy.call('setContentType', [contentType, callback]);
432
* Retrieves the specific SelectionType for this peer.
434
* If the callback parameter is not set, the current "local" value is retrieved.
436
* @method selectionType
437
* @return {String} ContentTransfer.SelectionType for this peer
438
* @param callback (optional) {Function(String)}
440
selectionType: function(callback) {
441
if (callback && typeof(callback) === 'function') {
442
this._proxy.call('selectionType', [], callback);
445
return this._selectionType;
448
* Sets specific SelectionType for this peer.
450
* @method setSelectionType
451
* @param selectionType {ContentTransfer.SelectionType}
452
* @param callback {Function()} called when the content type has been updated
454
setSelectionType: function(selectionType, callback) {
455
this._proxy.call('setSelectionType', [selectionType, callback]);
459
* Retrieves the name of the associated peer.
461
* If the callback parameter is not set, the current "local" value is retrieved.
464
* @param callback (optional) {Function(String)}
466
name: function(callback) {
467
if (callback && typeof(callback) === 'function') {
468
this._proxy.call('name', [], callback);
475
* Returns true if the peer is a default one, false otherwise.
477
* If the callback parameter is not set, the current "local" value is retrieved.
479
* @method isDefaultPeer
480
* @param callback (optional) {Function(Bool)}
482
isDefaultPeer: function(callback) {
483
if (callback && typeof(callback) === 'function') {
484
this._proxy.call('isDefaultPeer', [], callback);
487
return this._isDefaultPeer;
493
* Request to exchange content with this ContentPeer.
496
* @param callback {Function(ContentTransfer)} Called with the resulting content transfer
498
request: function(callback) {
499
this._proxy.call('request', [], callback);
503
* Request to import content from this ContentPeer and use a ContentStore for permanent storage.
505
* @method requestForStore
506
* @param store {ContentStore} Store used as a permanent storage
507
* @param callback {Function(ContentTransfer)} Called with the resulting content transfer
509
requestForStore: function(store, callback) {
510
this._proxy.call('requestForStore', [store.serialize()], callback);
516
* Destroys the remote object. This proxy object is not valid anymore.
520
destroy: function() {
521
this._proxy.call('destroy', []);
526
* ContentStore is an object returned by the ContentHub.
528
It represents a location where the resources imported or
529
exported from a peer during a transfer operation are to be
530
either saved or found.
532
* @class ContentStore
537
var api = external.getUnityObject('1.0');
538
var hub = api.ContentHub;
540
var pictureContentType = hub.ContentType.Pictures;
542
hub.defaultStoreForType(pictureContentType, function(store) {
543
[do something with the store]
546
function ContentStore(objectid, content) {
547
this._proxy = backendBridge.createRemoteObject(
548
PLUGIN_URI, 'ContentStore', objectid);
550
this._uri = content && content.uri
551
? content.uri : null;
552
this._scope = content && content.scope
553
? content.scope : null;
555
ContentStore.prototype = {
557
serialize: function() {
559
type: 'object-proxy',
560
apiid: 'ContentHub',
561
objecttype: 'ContentStore',
562
objectid: this._proxy.id(),
571
* Retrieves the uri of the associated store.
573
* If the callback parameter is not set, the current "local" value is retrieved.
576
* @return {String} current uri
577
* @param callback (optional) {Function(String)}
579
uri: function(callback) {
580
if (callback && typeof(callback) === 'function') {
581
this._proxy.call('uri', [], callback);
588
* Retrieves the current scope.
590
* If the callback parameter is not set, the current "local" value is retrieved.
593
* @return {ContentScope} current scope
594
* @param callback (optional) {Function(ContentScope)}
596
scope: function(callback) {
597
if (callback && typeof(callback) === 'function') {
598
this._proxy.call('scope', [], callback);
604
* Sets the current scope.
607
* @param scope {ContentScope}
608
* @param callback {Function()} called when the scope has been updated
610
setScope: function(scope, callback) {
611
this._proxy.call('setScope', [scope, callback]);
617
* Destroys the remote object. This proxy object is not valid anymore.
621
destroy: function() {
622
this._proxy.call('destroy', []);
626
function _constructorFromName(className) {
627
var constructorPerName = {
628
"ContentPeer": ContentPeer,
629
"ContentStore": ContentStore,
630
"ContentTransfer": ContentTransfer,
632
return className in constructorPerName
633
? constructorPerName[className]
638
* The ContentHub object.
646
ContentType is an enumeration of well known content types.
663
@property ContentType {String}
667
var api = external.getUnityObject('1.0');
668
var hub = api.ContentHub;
670
var pictureContentType = hub.ContentType.Pictures;
673
All: "All",
674
Unknown: "Unknown",
675
Pictures: "Pictures",
676
Documents: "Documents",
677
Music: "Music",
678
Contacts: "Contacts",
679
Videos: "Videos",
680
Links: "Links",
684
ContentHandler is an enumeration of well known content handlers.
695
@property ContentHandler {String}
698
Source: "Source",
699
Destination: "Destination",
700
Share: "Share",
704
ContentScope is an enumeration of well known scope types.
715
@property ContentScope {String}
718
System: "System",
719
User: "User",
720
App: "App",
726
ContentTransfer.State is an enumeration of the state of a given ongoing ContentTransfer.
730
Created: Transfer created, waiting to be initiated.
732
Initiated: Transfer has been initiated.
734
InProgress: Transfer is in progress.
736
Charged: Transfer is charged with items and ready to be collected.
738
Collected: Items in the transfer have been collected.
740
Aborted: Transfer has been aborted.
742
Finalized: Transfer has been finished and cleaned up.
744
Downloaded: Download specified by downloadId has completed.
746
Downloading: Transfer is downloading item specified by downloadId.
749
@property ContentTransfer.State {String}
753
var api = external.getUnityObject('1.0');
754
var hub = api.ContentHub;
756
var transferState = hub.ContentTransfer.State;
757
var pictureContentType = hub.ContentType.Pictures;
759
hub.importContentForPeer(
763
hub.defaultStoreForType(pictureContentType, function(store) {
764
transfer.setStore(store, function() {
765
transfer.start(function(state) {
766
if (transferState.Aborted === state) {
777
// Transfer created, waiting to be initiated.
778
Created: "Created",
780
// Transfer has been initiated.
781
Initiated: "Initiated",
783
// Transfer is in progress.
784
InProgress: "InProgress",
786
// Transfer is charged with items and ready to be collected.
787
Charged: "Charged",
789
// Items in the transfer have been collected.
790
Collected: "Collected",
792
// Transfer has been aborted.
793
Aborted: "Aborted",
795
// Transfer has been finished and cleaned up.
796
Finalized: "Finalized",
798
// Transfer has finished downloading.
799
Downloaded: "Downloaded",
801
// Transfer is downloading.
802
Downloading: "Downloading",
806
ContentTransfer.Direction is an enumeration of the directions of a given ContentTransfer.
817
@property ContentTransfer.Direction {String}
820
// Transfer is a request to import content
821
Import: "Import",
823
// Transfer is a request to export content
824
Export: "Export",
826
// Transfer is a request to share content
827
Share: "Share",
831
ContentTransfer.SelectionType is an enumeration of the directions of a given ContentTransfer.
835
Single: Transfer should contain a single item
837
Multiple: Transfer can contain multiple items
840
@property ContentTransfer.SelectionType {String}
843
// Transfer should contain a single item
844
Single: "Single",
846
// Transfer can contain multiple items
847
Multiple: "Multiple",
852
* Creates a ContentPeer object for the given source type.
855
* @param filters {Object} A dictionary of parameters to filter the result. The filtering keys are:
856
* - contentType: desired ContentType
857
* - handler: desired ContentHandler
859
* @param callback {Function(List of ContentPeer objects)} Callback that receives the result or null
861
getPeers: function(filter, callback) {
862
backendBridge.call('ContentHub.getPeers',
868
* Creates a ContentStore object for the given scope type.
871
* @param scope {ContentScope} The content scope for the store
872
* @param callback {Function(ContentStore)} Callback that receives the result or null
874
getStore: function(scope, callback) {
875
backendBridge.call('ContentHub.getStore',
881
* Launches the content peer picker ui that allows the user to select a peer.
883
* @method launchContentPeerPicker
884
* @param filters {Object} A dictionary of parameters to filter the result. The filtering keys are:
885
* - contentType: desired ContentType
886
* - handler: desired ContentHandler
887
* - showTitle: boolean value indicating if the title should be visible
888
* @param onPeerSelected {Function(ContentPeer)} Called when the user has selected a peer
889
* @param onCancelPressed {Function()} Called when the user has pressed cancel
891
launchContentPeerPicker: function(filters, onPeerSelected, onCancelPressed) {
892
backendBridge.call('ContentHub.launchContentPeerPicker',
893
[filters, onPeerSelected, onCancelPressed]);
897
* Sets a handler that is to be called when the current application is the
898
* target of an export request.
900
* @method onExportRequested
901
* @param callback {Function(ContentTransfer)} Function when one requests a resource to be exported.
902
* The corresponding ContentTransfer is provided as a parameter.
906
var api = external.getUnityObject(1.0);
907
var hub = api.ContentHub;
909
var transferState = hub.ContentTransfer.State;
911
function _exportRequested(transfer) {
912
var url = window.location.href;
913
url = url.substr(0, url.lastIndexOf('/')+1) + 'img/ubuntuone-music.png';
915
transfer.setItems([{name: 'Ubuntu One', url: url}],
917
transfer.setState(hub.ContentTransfer.State.Charged);
921
hub.onExportRequested(_exportRequested);
924
onExportRequested: function(callback) {
925
backendBridge.call('ContentHub.onExportRequested',
930
* Sets a handler that is to be called when the current application is the
931
* target of an share request.
933
* @method onShareRequested
934
* @param callback {Function(ContentTransfer)} Function when one requests a resource to be shared.
935
* The corresponding ContentTransfer is provided as a parameter.
939
var api = external.getUnityObject(1.0);
940
var hub = api.ContentHub;
942
var transferState = hub.ContentTransfer.State;
944
function _shareRequested(transfer) {
947
hub.onShareRequested(_shareRequested);
950
onShareRequested: function(callback) {
951
backendBridge.call('ContentHub.onShareRequested',
956
* Sets a handler that is to be called when the current application is the
957
* target of an import request.
959
* @method onImportRequested
960
* @param callback {Function(ContentTransfer)} Function when one requests a resource to be imported.
961
* The corresponding ContentTransfer is provided as a parameter.
965
var api = external.getUnityObject(1.0);
966
var hub = api.ContentHub;
968
var transferState = hub.ContentTransfer.State;
970
function _importRequested(transfer) {
973
hub.onImportRequested(_importRequested);
976
onImportRequested: function(callback) {
977
backendBridge.call('ContentHub.onImportRequested',
984
* Creates a ContentStore object for the given ContentPeer.
986
* @method api.importContent
987
* @param type {ContentType} type of the content to import
988
* @param peer {ContentPeer} peer who's content should be imported
989
* @param transferOptions {Object} a dictionary of transfer options. The options are the following:
990
* - multipleFiles {Bool}: specified if a transfer should involve multiple files or not
991
* - scope {ContentScope}: specifies the location where the transferred files should be copied to
992
* @param onError {Function(reason:)} called when the transfer has failed
993
* @param onSuccess {Function(Array of {ContentItem})} called when the transfer has been a success and items are available
995
importContent: function(type, peer, transferOptions, onSuccess, onError) {
996
backendBridge.call('ContentHub.apiImportContent',
997
[type, peer.serialize(), transferOptions, onSuccess, onError]);
1007
createObjectWrapper: function(objectType, objectId, content) {
1008
var Constructor = _constructorFromName(objectType);
1009
return new Constructor(objectId, content);
1020
<script src="../assets/vendor/prettify/prettify-min.js"></script>
1021
<script>prettyPrint();</script>
1022
<script src="../assets/js/yui-prettify.js"></script>
1023
<script src="../assets/js/tabs.js"></script>