~dholbach/ubuntu-rssreader-app/fix-paths

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
/*
  license GPL v3 ...........

  description of this file:
  a page for viewing a user selected RSS feed ;

*/

import QtQuick 2.0
import QtQuick.XmlListModel 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem
import "./dateutils.js" as DateUtils
import "databasemodule_v2.js" as DB

Page {
    id: page_feed

    property string rssModelUrl: ""
    property string rssTitle: ""
    property int rssIndex: 0
    property var rssModel
    property var rssItem: null
    property bool preventIndexChangeHandler: false

    onRssIndexChanged: {
        rssListview.currentIndex = rssIndex
        rssListview.positionViewAtIndex(rssListview.currentIndex, ListView.Center)
    }

    function setFeed(model, index) {
        /* Setting new model and not-null index will cause two change events instead of one.
         * Settings "preventIndexChangeHandler" to true helps to avoid it.
         */
        if (rssModel != model && index !== 0)
            preventIndexChangeHandler = true
        rssModel = model
        rssIndex = index
    }

    function reloadPageContent() {

    }

    tools: ToolbarItems {
        id: toolbar

        ToolbarButton {
            action: Action {
                text:  rssItem == null ? "" : (rssItem.favourite == "0" ? i18n.tr("Save") : i18n.tr("Unsave"))
                iconSource: {
                    if (rssItem == null || rssItem.favourite == "0")
                        return Qt.resolvedUrl("./icons_tmp/favorite-unselected.svg")
                    else return Qt.resolvedUrl("./icons_tmp/favorite-selected.svg")
                }
                onTriggered: {
                    var fav = (rssItem.favourite == "0" ? "1" : "0")
                    var dbResult = DB.updateArticleFavourite(rssItem.id, fav)
                    if (dbResult.rowsAffected == 1) {
                        rssItem.favourite = fav
                        var dbResult1 = DB.updateArticleStatus(rssItem.id, "0")
                        if (dbResult1.rowsAffected == 1) {
                            rssItem.status = "0"
                        }
                        mainView.refreshSavedTab()
                    }
                }
            }
        }

        ToolbarButton {
            action: Action {
                text:  i18n.tr("Open site")
                iconSource: Qt.resolvedUrl("./icons_tmp/go-to.svg")
                onTriggered:
                {
                    console.log("Open site", rssListview.model.get(rssListview.currentIndex).link)
                    Qt.openUrlExternally(rssListview.model.get(rssListview.currentIndex).link)
                }
            }
        }
    }

    //////////////////////////////////////////////      a listview to show the RSS content
    ListView {
        id: rssListview

        width: parent.width
        anchors.bottom: parent.bottom
        anchors.bottomMargin: units.gu(2)
        anchors.top: parent.top
        anchors.topMargin: units.gu(2)
        snapMode: ListView.SnapOneItem
        cacheBuffer: 90
        boundsBehavior: Flickable.StopAtBounds
        orientation: ListView.Horizontal
        contentHeight: parent.width * count
        model: rssModel
        delegate: xmlDelegate
        highlightFollowsCurrentItem: true
        highlightRangeMode: ListView.StrictlyEnforceRange

        onCurrentIndexChanged: {
            console.log("ListView onCurrentIndexChanged", currentIndex, preventIndexChangeHandler)
            if (preventIndexChangeHandler) {
                preventIndexChangeHandler = false
                return
            }
            rssItem = rssModel.get(currentIndex)
            rssTitle = rssItem.feed_name

            if (rssItem.status != "1") {
                var dbResult = DB.updateArticleStatus(rssItem.id, "1")
                if (dbResult.rowsAffected == 1) {
                    rssItem.status = "1"
                }
            }
        }
    }

    //////////////////////////////////////////////      delegate for ListView
    Component {
        id: xmlDelegate

        Flickable {
            id: scrollArea

            clip: true

            width: rssListview.width
            height: rssListview.height

            contentWidth: width
            contentHeight: innerAreaColumn.height

            Column {
                id: innerAreaColumn

                spacing: units.gu(2)
                width: parent.width

                Label {
                    id: label_time
                    text: DateUtils.formatRelativeTime(i18n, pubdate)
                    fontSize: "small"
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: parent.width - units.gu(4)
                }

                Label {
                    fontSize: "large"
                    text: title
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: parent.width - units.gu(4)
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                }

                Label {
                    text: content
                    linkColor: UbuntuColors.orange // Temporary. Mb bad color, but better than dark blue.
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: parent.width - units.gu(4)
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                    clip: true
                }

                Label {
                    id: label_feedname
                    text: rssTitle
                    fontSize: "small"
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: parent.width - units.gu(4)
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                }
            }
        } // Flickable
    } // Component
}