1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
/*
* Copyright 2015-2016 Michael Sheldon <mike@mikeasoft.com>
*
* This file is part of Podbird.
*
* Podbird is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* Podbird is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.4
import Ubuntu.Components 1.3
import QtQuick.LocalStorage 2.0
import Ubuntu.Components.Popups 1.3
import "../podcasts.js" as Podcasts
import "../components"
Page {
id: searchPage
property var xhr: new XMLHttpRequest;
TabsList {
id: tabsList
}
header: standardHeader
PageHeader {
id: standardHeader
visible: searchPage.header === standardHeader
title: i18n.tr("Add New Podcasts")
leadingActionBar {
numberOfSlots: 0
actions: tabsList.actions
}
trailingActionBar.actions: [
Action {
iconName: "search"
text: i18n.tr("Search Podcast")
onTriggered: {
searchPage.header = searchHeader
searchField.item.forceActiveFocus()
}
},
Action {
text: i18n.tr("Add Podcast")
iconName: "add"
onTriggered: {
searchPage.header = addHeader
feedUrlField.item.forceActiveFocus()
}
}
]
}
PageHeader {
id: searchHeader
visible: searchPage.header === searchHeader
contents: Loader {
id: searchField
sourceComponent: searchPage.header === searchHeader ? searchFieldComponent : undefined
anchors.left: parent ? parent.left : undefined
anchors.right: parent ? parent.right : undefined
anchors.verticalCenter: parent.verticalCenter
}
leadingActionBar.actions: [
Action {
iconName: "back"
onTriggered: {
resultsView.forceActiveFocus()
searchResults.clear()
searchPage.header = standardHeader
}
}
]
}
PageHeader {
id: addHeader
visible: searchPage.header === addHeader
contents: Loader {
id: feedUrlField
sourceComponent: searchPage.header === addHeader ? feedUrlComponent : undefined
anchors.left: parent ? parent.left : undefined
anchors.right: parent ? parent.right : undefined
anchors.verticalCenter: parent.verticalCenter
}
trailingActionBar.actions: [
Action {
iconName: "ok"
text: i18n.tr("Save Podcast")
onTriggered: {
resultsView.forceActiveFocus()
subscribeFromFeed(feedUrlField.item.text);
}
},
Action {
iconName: "edit-clear"
text: i18n.tr("Cancel")
onTriggered: {
resultsView.forceActiveFocus()
searchPage.header = standardHeader
}
}
]
}
onVisibleChanged: {
if(!visible) {
searchPage.header = standardHeader;
}
}
Component {
id: feedUrlComponent
TextField {
inputMethodHints: Qt.ImhUrlCharactersOnly
placeholderText: i18n.tr("Feed URL")
onAccepted: {
resultsView.forceActiveFocus()
subscribeFromFeed(feedUrlField.text);
}
}
}
Component {
id: searchFieldComponent
TextField {
inputMethodHints: Qt.ImhNoPredictiveText
placeholderText: i18n.tr("Search Podcast")
onTextChanged: {
if (text.length > 2) {
search(text)
} else {
searchResults.clear();
}
}
}
}
Component {
id: subscribeFailedDialog
Dialog {
id: dialogInternal
title: i18n.tr("Unable to subscribe")
text: i18n.tr("Please check the URL and try again")
Button {
text: i18n.tr("Close")
onClicked: {
PopupUtils.close(dialogInternal)
}
}
}
}
Loader {
id: emptyState
anchors {
left: parent.left
right: parent.right
margins: units.gu(2)
verticalCenter: parent.verticalCenter
verticalCenterOffset: Qt.inputMethod.visible ? units.gu(4) : 0
}
sourceComponent: searchPage.header === standardHeader ? emptyStateComponent : searchResults.count === 0 && searchPage.header === searchHeader ? emptyStateComponent
: undefined
}
Component {
id: emptyStateComponent
EmptyState {
icon.source: searchPage.header !== searchHeader ? Qt.resolvedUrl("../graphics/owlSearch.svg") : Qt.resolvedUrl("../graphics/notFound.svg")
title: searchPage.header !== searchHeader ? i18n.tr("Looking to add a new podcast?") : i18n.tr("No Podcasts Found")
subTitle: searchPage.header !== searchHeader ? i18n.tr("Click the 'magnifier' at the top to search or the 'plus' button to add by URL")
: i18n.tr("No podcasts found matching the search term.")
}
}
UbuntuListView {
id: resultsView
Component.onCompleted: {
// FIXME: workaround for qtubuntu not returning values depending on the grid unit definition
// for Flickable.maximumFlickVelocity and Flickable.flickDeceleration
var scaleFactor = units.gridUnit / 8;
maximumFlickVelocity = maximumFlickVelocity * scaleFactor;
flickDeceleration = flickDeceleration * scaleFactor;
}
model: searchResults
currentIndex: -1
anchors {
top: searchPage.header.bottom
left: parent.left
right: parent.right
bottom: parent.bottom
}
clip: true
visible: searchPage.header !== addHeader
footer: Item {
width: parent.width
height: units.gu(7)
}
delegate: ListItem {
id: listItem
property bool fetchedDescription: false
property bool expanded: false
divider.visible: false
highlightColor: "Transparent"
height: expanded ? listItemLayout.height + descriptionLoader.height + units.gu(1) : listItemLayout.height + units.gu(0.5)
color: index % 2 === 0 ? theme.palette.selected.background : "Transparent"
ListItemLayout {
id: listItemLayout
title.text: model.name
subtitle.text: model.artist
padding.top: units.gu(1)
padding.bottom: units.gu(0.5)
Image {
height: width
width: units.gu(6)
source: model.image
SlotsLayout.position: SlotsLayout.Leading
sourceSize { width: width; height: height }
}
Button {
SlotsLayout.position: SlotsLayout.Trailing
color: !model.subscribed ? theme.palette.normal.positive : theme.palette.normal.negative
text: !model.subscribed ? i18n.tr("Subscribe") : i18n.tr("Unsubscribe")
onClicked: {
if (!model.subscribed) {
Podcasts.subscribe(model.artist, model.name, model.feed, model.image);
imageDownloader.addDownload(model.feed, model.image)
} else {
var db = Podcasts.init();
db.transaction(function (tx) {
var rs = tx.executeSql("SELECT rowid FROM Podcast WHERE feed = ?", model.feed);
if (rs.rows.length !== 0) {
var podcast = rs.rows.item(0)
var rs2 = tx.executeSql("SELECT downloadedfile FROM Episode WHERE downloadedfile NOT NULL AND podcast=?", [podcast.rowid]);
for(var i = 0; i < rs2.rows.length; i++) {
fileManager.deleteFile(rs2.rows.item(i).downloadedfile);
}
tx.executeSql("DELETE FROM Episode WHERE podcast=?", [podcast.rowid]);
tx.executeSql("DELETE FROM Podcast WHERE rowid=?", [podcast.rowid]);
}
});
}
tabs.selectedTabIndex = 2;
}
}
}
Loader {
id: descriptionLoader
anchors { top: listItemLayout.bottom; left: parent.left; right: parent.right; leftMargin: units.gu(2); rightMargin: units.gu(2) }
visible: sourceComponent !== undefined
sourceComponent: expanded ? _description : undefined
}
Component {
id: _description
Label {
clip: true
// TRANSLATORS: The first argument here is the date of when the podcast was last updated followed by
// the podcast description.
text: i18n.tr("Last Updated: %1\n%2").arg(model.releaseDate.split("T")[0]).arg(model.description)
wrapMode: Text.WordWrap
textSize: Label.Small
color: theme.palette.normal.backgroundTertiaryText
linkColor: podbird.appTheme.linkText
height: expanded ? contentHeight : 0
onLinkActivated: Qt.openUrlExternally(link)
Behavior on height {
UbuntuNumberAnimation {
duration: UbuntuAnimation.BriskDuration
}
}
}
}
onClicked: {
expanded = !expanded
if (expanded && !fetchedDescription) {
getPodcastDescription(model.feed, index)
fetchedDescription = true
}
}
}
}
ListModel {
id: searchResults
}
function search(term) {
var url = "https://itunes.apple.com/search?term=" + term + "&media=podcast&entity=podcast"
xhr.open("GET", url);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
searchResults.clear();
var json = JSON.parse(xhr.responseText);
var db = Podcasts.init();
for(var i in json.results) {
var subscribed = false
db.transaction(function (tx) {
var rs = tx.executeSql("SELECT rowid, * FROM Podcast ORDER BY name ASC");
for(var j = 0; j < rs.rows.length; j++) {
var podcast = rs.rows.item(j);
if (podcast.name == json.results[i].trackName) {
subscribed = true
break
}
}
});
searchResults.append({"name" : json.results[i].trackName,
"artist" : json.results[i].artistName,
"feed" : json.results[i].feedUrl,
"image" : json.results[i].artworkUrl600,
"releaseDate": json.results[i].releaseDate,
"description": i18n.tr("Not Available"),
"subscribed": subscribed});
}
}
}
xhr.send();
}
function getPodcastDescription(feedUrl, index) {
var description = ""
var xhr = new XMLHttpRequest;
xhr.open("GET", feedUrl);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
var e = xhr.responseXML.documentElement;
for(var h = 0; h < e.childNodes.length; h++) {
if(e.childNodes[h].nodeName === "channel") {
var c = e.childNodes[h];
for(var j = 0; j < c.childNodes.length; j++) {
if (c.childNodes[j].nodeName === "description") {
description = c.childNodes[j].childNodes[0].nodeValue
if (description != undefined) {
searchResults.setProperty(index, "description", description)
return
}
}
}
}
}
}
}
xhr.send();
}
function subscribeFromFeed(feed) {
var xhr = new XMLHttpRequest;
if (feed.indexOf("://") === -1) {
feed = "http://" + feed;
}
xhr.open("GET", feed);
xhr.onreadystatechange = function() {
var name = "";
var artist = "";
var image = "";
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status < 200 || xhr.status > 299 || xhr.responseXML === null) {
PopupUtils.open(subscribeFailedDialog);
searchPage.header = addHeader
feedUrlField.item.text = feed
return;
}
var e = xhr.responseXML.documentElement;
for(var h = 0; h < e.childNodes.length; h++) {
if(e.childNodes[h].nodeName === "channel") {
var c = e.childNodes[h];
for(var j = 0; j < c.childNodes.length; j++) {
var nodeName = c.childNodes[j].nodeName;
if (nodeName === "title") name = c.childNodes[j].childNodes[0].nodeValue;
else if (nodeName === "author") artist = c.childNodes[j].childNodes[0].nodeValue;
else if (nodeName === "image") {
var el = c.childNodes[j];
for (var l = 0; l < el.attributes.length; l++) {
if(el.attributes[l].nodeName === "href") image = el.attributes[l].nodeValue;
}
}
}
}
}
if(name != "") {
Podcasts.subscribe(artist, name, feed, image);
imageDownloader.addDownload(feed, image)
tabs.selectedTabIndex = 2;
} else {
PopupUtils.open(subscribeFailedDialog);
searchPage.header = addHeader
feedUrlField.item.text = feed
return;
}
}
}
xhr.send();
}
}
|