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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
/*
* 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 Podbird 1.0
import QtMultimedia 5.6
import Ubuntu.Connectivity 1.0
import Qt.labs.settings 1.0
import Ubuntu.Components 1.3
import QtQuick.LocalStorage 2.0
import Ubuntu.DownloadManager 1.2
import "ui"
import "themes" as Themes
import "podcasts.js" as Podcasts
MainView {
id: podbird
objectName: "mainView"
applicationName: "podbird.nik90"
anchorToKeyboard: true
width: units.gu(50)
height: units.gu(75)
backgroundColor: appTheme.background
theme.name: settings.themeName == "Dark.qml" ? "Ubuntu.Components.Themes.SuruDark"
: "Ubuntu.Components.Themes.Ambiance"
Component.onDestruction: {
var db = Podcasts.init()
db.transaction(function (tx) {
tx.executeSql('UPDATE Episode SET queued=0 WHERE queued=1');
})
Podcasts.clearQueue()
}
// RefreshModel function to call refreshModel() function of the tab currently
// visible on application start.
function refreshModels() {
if (tabs.selectedTab === episodesTab) {
episodesTab.refreshModel()
} else if (tabs.selectedTab === podcastTab) {
podcastPage.item.refreshModel()
}
}
Component.onCompleted: {
var db = Podcasts.init()
db.transaction(function (tx) {
tx.executeSql('UPDATE Episode SET queued=0 WHERE queued=1');
})
var today = new Date()
// Only automatically check for podcasts on launch once every 12 hours
if (Math.floor((today - settings.lastUpdate)/86400000) >= 0.5) {
Podcasts.updateEpisodes(refreshModels)
}
loadingIndicator.opacity = 0
// Only perform cleanup of old episodes once a day
if (Math.floor((today - settings.lastCheck)/86400000) >= 1 && settings.retentionDays !== -1) {
Podcasts.cleanUp(today, settings.retentionDays)
settings.lastCheck = today
}
delayStartTimer.start();
}
Timer {
id: delayStartTimer
interval: 500
repeat: false
onTriggered: {
if (!NetworkingStatus.online || podbird.settings.maxEpisodeDownload === -1) {
console.log("[LOG]: Skipped autodownloading of new episodes...")
console.log("[LOG]: Online connectivity: " + NetworkingStatus.online)
console.log("[LOG]: User settings (maxEpisodeDownload): " + podbird.settings.maxEpisodeDownload)
} else {
Podcasts.autoDownloadEpisodes(podbird.settings.maxEpisodeDownload)
}
}
}
property string currentName
property string currentArtist
property string currentImage
property string currentGuid
property url currentUrl: ""
Themes.ThemeManager {
id: themeManager
source: settings.themeName
onSourceChanged: {
podbird.theme.name = settings.themeName == "Dark.qml" ? "Ubuntu.Components.Themes.SuruDark"
: "Ubuntu.Components.Themes.Ambiance"
}
}
property alias appTheme: themeManager.theme
property var themeManager: themeManager
property var settings: Settings {
// Set "Light.qml" as the default theme
property string themeName: "Light.qml"
property int retentionDays: -1
property var lastCheck: new Date()
property var lastUpdate: new Date(0)
property bool firstRun: true
property int maxEpisodeDownload: -1
property bool hideListened: false
property bool showListView: true
property int skipForward: 30
property int skipBack: 10
property bool downloadOverWifiOnly: true
property bool enableBottomEdge: true
}
FileManager {
id: fileManager
}
SingleDownload {
id: imageDownloader
property string feed
property var queue: []
property int queueLength: 0
onFinished: {
var db = Podcasts.init();
var finalLocation = fileManager.saveDownload(path);
db.transaction(function (tx) {
tx.executeSql("UPDATE Podcast SET image=? WHERE feed=?", [finalLocation, imageDownloader.feed]);
queue.shift();
queueLength--
if (queue.length > 0) {
imageDownloader.feed = queue[0][0];
download(queue[0][1]);
} else {
feed = "";
}
});
}
function addDownload(feed, url) {
queue.push([feed, url]);
queueLength++
if (queue.length == 1) {
imageDownloader.feed = feed;
download(url);
}
}
}
Component {
id: singleDownloadComponent
SingleDownload {
id: singleDownloadObject
property string image
property string title
property string guid
metadata: Metadata {
showInIndicator: true
title: singleDownloadObject.title
custom: {"guid": singleDownloadObject.guid, "image" : singleDownloadObject.image}
}
}
}
function downloadEpisode(image, title, guid, url, disableMobileDownload) {
if(downloader.isDownloadInQueue(guid)) {
console.log("[LOG]: Download with GUID of :"+guid+ " is already in the download queue.")
return false;
}
var singleDownload = singleDownloadComponent.createObject(podbird, {"image": image, "title": title, "guid": guid, allowMobileDownload : !disableMobileDownload })
singleDownload.download(url)
}
DownloadManager {
id: downloader
property string downloadingGuid: downloads.length > 0 ? downloads[0].metadata.custom.guid : "NULL"
property int progress: downloads.length > 0 ? downloads[0].progress : 0
cleanDownloads: true
function isDownloadInQueue ( guid ) {
for( var i=0; i < downloads.length; i++) {
if( downloads[i].metadata.custom.guid && guid === downloads[i].metadata.custom.guid) {
return true ;
}
}
return false;
}
onDownloadFinished: {
var db = Podcasts.init();
var finalLocation = fileManager.saveDownload(path);
db.transaction(function (tx) {
tx.executeSql("UPDATE Episode SET downloadedfile=?, queued=0 WHERE guid=?", [finalLocation, download.metadata.custom.guid]);
});
}
onErrorFound: {
console.log("[ERROR]: " + download.errorMessage)
}
}
// This reduces incidences of media-hub getting confused and
// continuing to play the previous file when clearing a playlist
// and starting a new episode
Timer {
id: playStarter
interval: 500
onTriggered: {
// Ideally we'd check the buffer progress here and base it on that
// but media-hub doesn't report it correctly
console.log("Starting playback")
player.play()
player.restorePosition()
}
}
MediaPlayer {
id: player
// Wrapper function around decodeURIComponent() to prevent exceptions
// from bubbling up to the app.
function decodeFileURI(filename)
{
var newFilename = "";
try {
newFilename = decodeURIComponent(filename);
} catch (e) {
newFilename = filename;
console.log("Unicode decoding error:", filename, e.message)
}
return newFilename;
}
function metaForSource(source) {
var blankMeta = {
name: "",
artist: "",
image: "",
guid: "",
position: 0,
}
source = source.toString()
return Podcasts.lookup(decodeFileURI(source)) || blankMeta;
}
function toggle() {
if (playbackState === MediaPlayer.PlayingState) {
pause()
// Save the current position when we pause
savePosition()
} else {
play()
// Clear the last saved position when we start playing again
clearPosition()
}
}
function savePosition() {
if (currentGuid) {
var db = Podcasts.init()
db.transaction(function (tx) {
tx.executeSql("UPDATE Episode SET position=? WHERE guid=?", [player.position, currentGuid])
tx.executeSql("UPDATE Queue SET position=? WHERE guid=?", [player.position, currentGuid])
})
}
}
function restorePosition() {
if (playbackState === MediaPlayer.PlayingState && pendingSeek !== 0) {
player.seek(pendingSeek)
player.clearPosition()
pendingSeek = 0;
}
}
function clearPosition() {
if (currentGuid) {
var db = Podcasts.init()
db.transaction(function (tx) {
tx.executeSql("UPDATE Episode SET position=NULL WHERE guid=?", [currentGuid])
})
}
}
function playEpisode(guid, image, name, artist, url, position) {
player.pause()
// Clear current queue
player.playlist.clear()
Podcasts.clearQueue()
// Add episode to queue
Podcasts.addItemToQueue(guid, image, name, artist, url, position)
player.playlist.addItem(url)
// Play episode
pendingSeek = position
playStarter.restart()
}
function addEpisodeToQueue(guid, image, name, artist, url, position) {
Podcasts.addItemToQueue(guid, image, name, artist, url, position)
player.playlist.addItem(url)
// If added episode is the first one in the queue, then set the current metadata
// so that the bottom player controls will be shown, allowing the user to play
// the episode if he chooses to.
if (player.playlist.itemCount === 0) {
currentGuid = guid
currentName = name
currentArtist = artist
currentImage = image
currentUrl = url
pendingSeek = position
}
}
property bool endOfMedia: false
property double progress: 0
property int pendingSeek: 0
playlist: Playlist {
playbackMode: Playlist.Sequential
readonly property bool canGoPrevious: currentIndex !== 0
readonly property bool canGoNext: currentIndex !== itemCount - 1
onCurrentItemSourceChanged: {
var meta = player.metaForSource(currentItemSource)
currentGuid = "";
currentName = meta.name
currentArtist = meta.artist
currentImage = meta.image
currentGuid = meta.guid
player.pendingSeek = meta.position
}
}
onPlaybackStateChanged: {
restorePosition()
}
onStatusChanged: {
if (status === MediaPlayer.EndOfMedia) {
console.log("[LOG]: End of Media. Stopping.")
endOfMedia = true
stop()
}
}
onStopped: {
if (playlist.itemCount > 0) {
if (endOfMedia) {
// We just ended media, so jump to start of playlist
playlist.currentIndex = 0;
// Play then pause otherwise when we come from EndOfMedia
// it calls next() until EndOfMedia again.
play()
pause()
}
}
// Always reset endOfMedia
endOfMedia = false
}
Component.onDestruction: {
savePosition()
}
}
PageStack {
id: mainStack
Component.onCompleted: {
// Show the welcome wizard only when running the app for the first time
if (settings.firstRun) {
console.log("[LOG]: Detecting first time run by user. Starting welcome wizard.")
push(Qt.resolvedUrl("welcomewizard/WelcomeWizard.qml"))
} else {
push(tabs)
}
}
Tabs {
id: tabs
// Ensure that the last used tab is restored when the app gets killed
// and brought by the system.
StateSaver.properties: "selectedTabIndex"
onSelectedTabChanged: {
// Load the Podcast page only when the user navigates to it. However
// do not unload it when the user switches to another tab.
if (selectedTab === podcastTab) {
podcastPage.source = Qt.resolvedUrl("ui/PodcastsTab.qml")
}
}
EpisodesTab {
id: episodesTab
objectName: "episodesTab"
}
Tab {
id: searchTab
// Dynamically load/unload the search tab as required
page: Loader {
parent: searchTab
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
source: (tabs.selectedTab === searchTab) ? Qt.resolvedUrl("ui/SearchPage.qml") : ""
}
}
Tab {
id: podcastTab
page: Loader {
id: podcastPage
parent: podcastTab
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
}
}
Tab {
id: settingsTab
// Dynamically load/unload the settings tab as required
page: Loader {
parent: settingsTab
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
source: (tabs.selectedTab === settingsTab) ? Qt.resolvedUrl("ui/SettingsPage.qml") : ""
}
}
}
}
Loader {
id: playerControlLoader
anchors.bottom: parent.bottom
height: units.gu(7)
width: parent.width
visible: !Qt.inputMethod.visible
state: "shown"
states: [
State {
name: "shown"
when: player.playlist.itemCount !== 0
PropertyChanges { target: playerControlLoader; anchors.bottomMargin: 0 }
},
State {
name: "hidden"
when: player.playlist.itemCount === 0 || !playerControl.visible
PropertyChanges { target: playerControlLoader; anchors.bottomMargin: -units.gu(7) }
}
]
transitions: [
Transition {
from: "hidden"; to: "shown"
SequentialAnimation {
ScriptAction { script: playerControlLoader.setSource( Qt.resolvedUrl("ui/PlayerControls.qml") ,{ bottomEdge : bottomEdgeLoader.item})}
UbuntuNumberAnimation { target: playerControlLoader; property: "anchors.bottomMargin"; duration: UbuntuAnimation.SlowDuration }
}
},
Transition {
from: "shown"; to: "hidden"
SequentialAnimation {
UbuntuNumberAnimation { target: playerControlLoader; property: "anchors.bottomMargin"; duration: UbuntuAnimation.SlowDuration }
ScriptAction { script: playerControlLoader.setSource( "" ); }
}
}
]
}
Rectangle {
id: loadingIndicator
color: podbird.appTheme.background
anchors.fill: parent
ActivityIndicator {
anchors.centerIn: parent
running: true
}
Behavior on opacity {
UbuntuNumberAnimation {
}
}
}
Loader {
id: bottomEdgeLoader
asynchronous: true
Component.onCompleted: setSource("ui/BottomEdge.qml", {
"objectName": "bottomEdge",
"parent": podbird,
"hint.objectName": "bottomEdgeHint"
})
}
}
|