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
|
/*
* Copyright 2015 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.3
import QtMultimedia 5.0
import QtQuick.Layouts 1.1
import QtQuick.LocalStorage 2.0
import Ubuntu.Components 1.2
import Ubuntu.DownloadManager 0.1
import Ubuntu.Components.ListItems 1.0 as ListItem
import Ubuntu.Components.Popups 1.0
import "../podcasts.js" as Podcasts
import "../components"
Page {
id: podcastPage
/*
#FIXME: The page flickable is to null instead of viewLoader.item since
it otherwise creates bug http://pad.lv/1446162 which can confuse a
new user.
*/
flickable: null
property bool episodesUpdating: false;
/*
#FIXME: The following lines of code is necessary due to a upstream bug
in the SDK http://pad.lv/1400297. This bug is still present in the rtm.
Once it is fixed, this following property and connection can be remvoed.
*/
property Item __oldContents: null
Connections {
target: podcastPage.head
onContentsChanged: {
if (podcastPage.__oldContents) {
podcastPage.__oldContents.parent = null;
}
podcastPage.__oldContents = podcastPage.head.contents;
}
}
state: "default"
states: [
PageHeadState {
name: "default"
head: podcastPage.head
actions: [
Action {
iconName: "search"
text: i18n.tr("Search Podcast")
onTriggered: {
podcastPage.state = "search"
searchField.item.forceActiveFocus()
}
},
Action {
iconName: "add"
text: i18n.tr("Add New Podcasts")
onTriggered: {
mainStack.push(Qt.resolvedUrl("SearchPage.qml"))
}
}
]
},
PageHeadState {
name: "search"
head: podcastPage.head
backAction: Action {
iconName: "back"
text: i18n.tr("Back")
onTriggered: {
viewLoader.item.forceActiveFocus()
podcastPage.state = "default"
}
}
contents: Loader {
id: searchField
sourceComponent: podcastPage.state === "search" ? searchFieldComponent : undefined
anchors.left: parent ? parent.left : undefined
anchors.right: parent ? parent.right : undefined
anchors.rightMargin: units.gu(2)
}
},
PageHeadState {
name: "selection"
head: podcastPage.head
when: podbird.settings.showListView && viewLoader.item.ViewItems.selectMode
backAction: Action {
iconName: "back"
text: i18n.tr("Back")
onTriggered: {
viewLoader.item.ViewItems.selectMode = false
podcastPage.state = "default"
}
}
actions: [
Action {
enabled: viewLoader.item.count !== 0
text: {
if (viewLoader.item.ViewItems.selectedIndices.length === viewLoader.item.count) {
return i18n.tr("Select None")
} else {
return i18n.tr("Select All")
}
}
iconSource: {
if (viewLoader.item.ViewItems.selectedIndices.length === viewLoader.item.count) {
return Qt.resolvedUrl("../graphics/select-none.svg")
} else {
return Qt.resolvedUrl("../graphics/select.svg")
}
}
onTriggered: {
if (viewLoader.item.ViewItems.selectedIndices.length === viewLoader.item.count) {
viewLoader.item.clearSelection()
} else {
viewLoader.item.selectAll()
}
}
},
Action {
iconName: "delete"
text: i18n.tr("Delete")
enabled: viewLoader.item.ViewItems.selectedIndices.length !== 0
onTriggered: {
var items = viewLoader.item.ViewItems.selectedIndices
var db = Podcasts.init();
for (var i=0; i< viewLoader.item.ViewItems.selectedIndices.length; i++) {
var id = podcastModel.get(items[i]).id
db.transaction(function (tx) {
var rs = tx.executeSql("SELECT downloadedfile FROM Episode WHERE downloadedfile NOT NULL AND podcast=?", [id]);
for(var j = 0; j < rs.rows.length; j++) {
fileManager.deleteFile(rs.rows.item(j).downloadedfile);
}
tx.executeSql("DELETE FROM Episode WHERE podcast=?", [id]);
tx.executeSql("DELETE FROM Podcast WHERE rowid=?", [id]);
});
}
viewLoader.item.clearSelection()
podcastPage.refreshModel()
}
}
]
}
]
Component {
id: searchFieldComponent
TextField {
inputMethodHints: Qt.ImhNoPredictiveText
placeholderText: i18n.tr("Search podcast")
}
}
onVisibleChanged: {
if(visible) {
refreshModel();
}
}
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: podcastModel.count === 0 || sortedPodcastModel.count === 0 ? emptyStateComponent : undefined
}
Component {
id: emptyStateComponent
EmptyState {
iconHeight: units.gu(12)
iconWidth: units.gu(22)
iconSource: podcastModel.count === 0 ? Qt.resolvedUrl("../graphics/owlSearch.svg") : Qt.resolvedUrl("../graphics/notFound.svg")
title: podcastModel.count === 0 ? i18n.tr("No Podcast Subscriptions") : i18n.tr("No Podcasts Found")
subTitle: podcastModel.count === 0 ? i18n.tr("You haven't subscribed to any podcasts yet, visit the 'Find New Podcasts' page to add some.")
: i18n.tr("No podcasts found matching the search term.")
}
}
ListModel {
id: podcastModel
}
SortFilterModel {
id: sortedPodcastModel
model: podcastModel
filter.property: "name"
filter.pattern: podcastPage.state === "search" && searchField.status == Loader.Ready ? RegExp(searchField.item.text, "gi")
: RegExp("", "gi")
}
Loader {
id: viewLoader
anchors.fill: parent
sourceComponent: podbird.settings.showListView ? listviewComponent : cardviewComponent
}
Component {
id: cardviewComponent
CardView {
id: cardView
clip: true
model: sortedPodcastModel
delegate: Card {
id: albumCard
coverArt: model.image !== undefined ? model.image : Qt.resolvedUrl("../graphics/podbird.png")
primaryText: model.name !== undefined ? model.name.trim() : "Undefined"
secondaryText: i18n.tr("%1 unheard episode", "%1 unheard episodes", model.episodeCount).arg(model.episodeCount)
onClicked: {
if(podcastPage.state === "search") {
cardView.forceActiveFocus()
podcastPage.state = "default"
}
mainStack.push(Qt.resolvedUrl("EpisodesPage.qml"), {"episodeName": model.name, "episodeId": model.id, "episodeArtist": model.artist, "episodeImage": model.image})
}
}
}
}
Component {
id: listviewComponent
UbuntuListView {
id: listView
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;
}
signal clearSelection()
signal closeSelection()
signal selectAll()
clip: true
model: sortedPodcastModel
anchors.fill: parent
footer: Item {
width: parent.width
height: units.gu(8)
}
delegate: ListDelegate {
id: listItem
height: units.gu(8)
highlightColor: index % 2 === 0 ? "Transparent" : podbird.appTheme.hightlightListView
color: index % 2 === 0 ? podbird.appTheme.hightlightListView : "Transparent"
title: model.name !== undefined ? model.name.trim() : "Undefined"
subtitle: i18n.tr("%1 unheard episode", "%1 unheard episodes", model.episodeCount).arg(model.episodeCount)
coverArt: model.image !== undefined ? model.image : Qt.resolvedUrl("../graphics/podbird.png")
leadingActions: ListItemActions {
actions: [
Action {
iconName: "delete"
onTriggered: {
var db = Podcasts.init();
db.transaction(function (tx) {
var rs = tx.executeSql("SELECT downloadedfile FROM Episode WHERE downloadedfile NOT NULL AND podcast=?", [model.id]);
for(var i = 0; i < rs.rows.length; i++) {
fileManager.deleteFile(rs.rows.item(i).downloadedfile);
}
tx.executeSql("DELETE FROM Episode WHERE podcast=?", [model.id]);
tx.executeSql("DELETE FROM Podcast WHERE rowid=?", [model.id]);
podcastModel.remove(index, 1)
});
}
}
]
}
onClicked: {
if (selectMode) {
selected = !selected
} else {
if(podcastPage.state === "search") {
listView.forceActiveFocus()
podcastPage.state = "default"
}
mainStack.push(Qt.resolvedUrl("EpisodesPage.qml"), {"episodeName": model.name, "episodeId": model.id, "episodeArtist": model.artist, "episodeImage": model.image})
}
}
onPressAndHold: {
ListView.view.ViewItems.selectMode = !ListView.view.ViewItems.selectMode
}
}
onClearSelection: {
ViewItems.selectedIndices = []
}
onSelectAll: {
var tmp = [];
for (var i=0; i < model.count; i++) {
tmp.push(i)
}
ViewItems.selectedIndices = tmp
}
onCloseSelection: {
clearSelection()
ViewItems.selectMode = false
}
// #FIXME: Use SDK Scrollbar when it is themeable
CustomScrollBar {
listview: listView
}
PullToRefresh {
refreshing: episodesUpdating
onRefresh: updateEpisodesDatabase();
}
}
}
function refreshModel() {
var db = Podcasts.init();
db.transaction(function (tx) {
podcastModel.clear();
var rs = tx.executeSql("SELECT rowid, * FROM Podcast ORDER BY name ASC");
for(var i = 0; i < rs.rows.length; i++) {
var podcast = rs.rows.item(i);
var rs2 = tx.executeSql("SELECT Count(*) AS epcount FROM Episode WHERE podcast=? AND NOT listened", [rs.rows.item(i).rowid]);
podcastModel.append({"id" : podcast.rowid, "name" : podcast.name, "artist" : podcast.artist, "image" : podcast.image, "episodeCount" : rs2.rows.item(0).epcount});
if (podcast.lastupdate === null && !episodesUpdating) {
updateEpisodesDatabase();
}
}
});
episodesUpdating = false;
}
function updateEpisodesDatabase() {
episodesUpdating = true;
Podcasts.updateEpisodes(refreshModel)
}
}
|