Unity 8
SharingPicker.qml
1 /*
2  * Copyright (C) 2015 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import Ubuntu.Components 1.3
19 import Ubuntu.Content 1.1
20 
21 Item {
22  id: root
23 
24  property var shareData
25  property alias active: peerPicker.active
26  readonly property bool isUrlExternal: url && url.indexOf("file:///") != 0 && url.indexOf("/") != 0
27  readonly property string contentType: shareData ? shareData["content-type"] : ""
28  readonly property var url: shareData ? shareData["uri"] : ""
29  readonly property Item rootItem: QuickUtils.rootItem(root)
30 
31  function showPeerPicker() {
32  peerPicker.visible = true;
33  }
34 
35  function createExportedItems(url) {
36  var items = new Array();
37  if (typeof url === "string") {
38  var exportItem = exportItemComponent.createObject();
39  exportItem.url = url;
40  items.push(exportItem);
41  } else {
42  for (var i = 0; i < url.length; i++) {
43  var exportItem = exportItemComponent.createObject();
44  exportItem.url = url[i];
45  items.push(exportItem);
46  }
47  }
48  return items;
49  }
50 
51  Component {
52  id: exportItemComponent
53  ContentItem {
54  name: i18n.tr("Preview Share Item")
55  }
56  }
57 
58  Component {
59  id: contentPeerComponent
60  ContentPeerPicker {
61  handler: ContentHandler.Share
62  contentType: {
63  // for now, treat all external urls as Links, or it will break contenthub
64  if (root.isUrlExternal) return ContentType.Links;
65 
66  switch(root.contentType) {
67  case "all": return ContentType.All;
68  case "contacts": return ContentType.Contacts;
69  case "documents": return ContentType.Documents;
70  case "links": return ContentType.Links;
71  case "music": return ContentType.Music;
72  case "pictures": return ContentType.Pictures;
73  case "text": return ContentType.Text;
74  default:
75  case "unknown": return ContentType.Unknown;
76  case "videos": return ContentType.Videos;
77  }
78  }
79 
80  onPeerSelected: {
81  var transfer = peer.request();
82  if (transfer.state === ContentTransfer.InProgress) {
83  transfer.items = createExportedItems(url);
84  transfer.state = ContentTransfer.Charged;
85  }
86  peerPicker.visible = false;
87  }
88  onCancelPressed: peerPicker.visible = false;
89  }
90  }
91 
92  Loader {
93  id: peerPicker
94  objectName: "peerPicker"
95  parent: rootItem
96  anchors.fill: parent
97  visible: false
98  active: root.url != ""
99 
100  sourceComponent: contentPeerComponent
101  }
102 }