Unity 8
PreviewSharing.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  implicitHeight: button.height
24  implicitWidth: button.width
25 
26  property var shareData
27  property alias active: peerPicker.active
28  readonly property bool isUrlExternal: url && url.indexOf("file:///") != 0 && url.indexOf("/") != 0
29  readonly property string contentType: shareData ? shareData["content-type"] : ""
30  readonly property var url: shareData ? shareData["uri"] : ""
31  readonly property Item rootItem: QuickUtils.rootItem(root)
32 
33  AbstractButton {
34  id: button
35  height: units.gu(4)
36  width: units.gu(4)
37  onClicked: peerPicker.visible = true
38 
39  Icon {
40  anchors.centerIn: parent
41  height: units.gu(3)
42  width: units.gu(3)
43  source: "image://theme/share"
44  }
45  }
46 
47  function createExportedItems(url) {
48  var items = new Array();
49  if (typeof url === "string") {
50  var exportItem = exportItemComponent.createObject();
51  exportItem.url = url;
52  items.push(exportItem);
53  } else {
54  for (var i = 0; i < url.length; i++) {
55  var exportItem = exportItemComponent.createObject();
56  exportItem.url = url[i];
57  items.push(exportItem);
58  }
59  }
60  return items;
61  }
62 
63  Component {
64  id: exportItemComponent
65  ContentItem {
66  name: i18n.tr("Preview Share Item")
67  }
68  }
69 
70  Component {
71  id: contentPeerComponent
72  ContentPeerPicker {
73  handler: ContentHandler.Share
74  contentType: {
75  // for now, treat all external urls as Links, or it will break contenthub
76  if (root.isUrlExternal) return ContentType.Links;
77 
78  switch(root.contentType) {
79  case "all": return ContentType.All;
80  case "contacts": return ContentType.Contacts;
81  case "documents": return ContentType.Documents;
82  case "links": return ContentType.Links;
83  case "music": return ContentType.Music;
84  case "pictures": return ContentType.Pictures;
85  case "text": return ContentType.Text;
86  default:
87  case "unknown": return ContentType.Unknown;
88  case "videos": return ContentType.Videos;
89  }
90  }
91 
92  onPeerSelected: {
93  var transfer = peer.request();
94  if (transfer.state === ContentTransfer.InProgress) {
95  transfer.items = createExportedItems(url);
96  transfer.state = ContentTransfer.Charged;
97  }
98  peerPicker.visible = false;
99  }
100  onCancelPressed: peerPicker.visible = false;
101  }
102  }
103 
104  Loader {
105  id: peerPicker
106  objectName: "peerPicker"
107  parent: rootItem
108  anchors.fill: parent
109  visible: false
110  active: root.url != ""
111 
112  sourceComponent: contentPeerComponent
113  }
114 }