Debian Codegraph @

plugins/ubuntu-plugin/qml/workers/ContentWorker.qml

raw
1 /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 import QtQuick 2.4
19 import QtQuick.Window 2.1
20 import QuickFlux 1.0
21 import Lomiri.Components.Popups 1.3
22 import Lomiri.Content 1.1
23 import Dekko.Mail.API 1.0
24 import Dekko.Mail.Stores.Views 1.0
25 import Dekko.Mail.Stores.Composer 1.0
26 import Dekko.Lomiri.Dialogs 1.0
27
28 /*!
29 *
30 * ContentWorker
31 *
32 * The ContentWorker is responsible for the import and export of content.
33 * Whether that's by ContentHub on unity8 or using the native platform pickers
34 *
35 * Users of the ContentActions API should not be concerned with the platform they
36 * are on as that is all handled here.
37 *
38 * @ingroup qml_worker
39 */
40 AppListener {
41
42 waitFor: [ViewStore.listenerId, ComposerStore.listenerId]
43 property string pickerUrl: ""
44
45 Connections {
46 target: ContentHub
47 onImportRequested: {
48 Log.logStatus("[ContentHub::importRequested]", "import requested, running import...")
49 ContentActions.importFromContentHub(transfer)
50 }
51 onShareRequested: {
52 Log.logStatus("[ContentHub::shareRequested]", "share requested. importing share")
53 ContentActions.importFromContentHub(transfer)
54 }
55 onExportRequested: {
56 Log.logStatus("ContentHub::exportRequested", "NotImplementedYet")
57 }
58 }
59
60 AppScript {
61 runWhen: ContentKeys.pickFile
62 script: {
63 // First open the file picker for the platform
64 // This will either be the ContentHub picker
65 // or Qt's own FileDialog for non unity8 platforms
66 ContentActions.openFilePicker(message.root)
67 // Listen on selected files and add them to the composer
68 once(ContentKeys.filesSelected, function(message) {
69 console.log("FILES: ", message.files)
70 var files = message.files
71 for (var i in message.files) {
72 console.log("File: ", message.files[i])
73 var file = message.files[i]
74 ComposerActions.addFileAttachment(file.toString().replace("file://", ""))
75 }
76 })
77 // Drop all callbacks if the picker is closed with no selections
78 once(ContentKeys.pickerClosed, exit.bind(this, 0))
79 }
80 }
81
82 Component {
83 id: chp
84 ContentPickerDialog{}
85 }
86
87 // Component {
88 // id: fp
89 // FilePickerDialog{}
90 // }
91
92 Filter {
93 type: ContentKeys.openFilePicker
94 onDispatched: {
95 if (true) {
96 var chPicker = PopupUtils.open(chp, message.root, {isExport: false})
97 chPicker.filesImported.connect(function(files){
98 var imports = new Array()
99 for (var i in files) {
100 Log.logInfo("ContentManager::filesPicked", "File selected: %1".arg(files[i].url))
101 imports.push(files[i].url)
102 }
103 ContentActions.filesSelected(imports)
104 })
105 } else {
106 var c = Qt.createComponent("")
107 var filePicker = c.createObject(message.root)
108 filePicker.accepted.connect(function(){
109 var files = new Array()
110 for (var i in filePicker.fileUrls) {
111 Log.logInfo("ContentManager::filesPicked", "File selected: %1".arg(filePicker.fileUrls[i]))
112 files.push(filePicker.fileUrls[i])
113 }
114 ContentActions.filesSelected(files)
115 filePicker.close()
116 filePicker.destroy()
117 })
118 filePicker.rejected.connect(function() {
119 Log.logInfo("ContentManager::filePickerRejected", "No attachments selected")
120 ContentActions.pickerClosed()
121 })
122 }
123 }
124 }
125
126 Filter {
127 type: ContentKeys.importFromContentHub
128 onDispatched: {
129 if (message.transfer.length === 0) {
130 Log.logWarning("ContentManager::importFromContentHub", "Not items to transfer :-/")
131 return
132 }
133 Log.logInfo("ContentManager::importFromContentHub", "Number of transferred items: " + message.transfer.items.length)
134 // open the composer
135 ViewActions.delayCallWithArgs(ViewKeys.openComposer, {})
136 for (var item in message.transfer.items) {
137 var url = message.transfer.items[item].url
138 Log.logInfo("ContentManager::importFromContentHub", "Item url: " + url)
139 var text = message.transfer.items[item].text
140 Log.logInfo("ContentManager::importFromContentHub", "Item text: " + text)
141 switch (message.transfer.contentType) {
142 case ContentType.Links:
143 Log.logInfo("ContentManager::importFromContentHub", "ContentType is Link: " + url.toString())
144 ViewActions.delayCallWithArgs(ComposerKeys.appendTextToSubject, {text: text})
145 ViewActions.delayCallWithArgs(ComposerKeys.appendTextToBody, {text: url.toString()})
146 break;
147 case ContentType.Text:
148 Log.logInfo("ContentManager::importFromContentHub", "ContentType is Text: " + text)
149 ViewActions.delayCallWithArgs(ComposerKeys.appendTextToBody, {text: text})
150 break;
151 default:
152 var other = url.toString().replace("file://", "")
153 Log.logInfo("ContentManager::importFromContentHub", "ContentType is Other, most likely a file: " + other)
154 ComposerActions.addFileAttachment(other)
155 break;
156 }
157 }
158 }
159 }
160
161 Filter {
162 type: ContentKeys.exportFile
163 onDispatched: {
164 PopupUtils.open(chp, message.root, {fileUrl: message.file})
165 }
166 }
167 }
168