~qqworini/ubuntu-rssreader-app/uitk-1_3

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
import QtQuick 2.3
import QtQuick.XmlListModel 2.0

import Ubuntu.Components 1.1
import Ubuntu.Components.ListItems 1.0 as ListItems
import Ubuntu.Components.Popups 1.0

import "databasemodule_v2.js" as DB
import "./imgSeparator.js" as ImageUtils
import "./dateutils.js" as DateUtils

Item {
    id: networkManagerRoot

    signal downloadFinished(int tagId)
    signal downloadStarted(int tagId)

    property string operationStatus: "success"

    function updateFeeds(feedsArray, topicId) {
        d.updateFeeds(feedsArray, topicId)
    }

    function cancelDownload() {
        d.cancelDownload()
    }

    /* All private method are inside QtObject.
     */
    QtObject {
        id: d

        property var feedList: []                 // Feed list to update.
        property var currentFeed                  // Current downloading feed.
        property int tagId: 0                     // Tag to update.

        /* Method updates feeds one by another.
         * Input: array of objects, each should include
         * source, link and id (of feed in DB) properties.
         */
        function updateFeeds(feedsArray, topicId) {
            tagId = topicId || 0

            downloadStarted(tagId)

            feedList = feedsArray
            operationStatus = "success"
            updateNextFeed()
        }

        // For inner usage only.
        function updateNextFeed() {
            if (feedList.length == 0) {
                downloadFinished(tagId)
                return
            }

            currentFeed = feedList.shift()
            googleFeedApi.loadFeed(currentFeed.source)
        }

        function cancelDownload() {
            feedList = []
            operationStatus = "abort"
            googleFeedApi.abort()
        }

        function updateFeedInfo(feedId, feedLink, responseData) {
            var entries = responseData.feed.entries
            var f = responseData.feed

            DB.updateFeedByXml(feedId, f.feedUrl === f.link ? feedLink : f.link, // Sometimes google fails and sends site link equal to feed url.
                                                              f.description, f.title)
            console.log(" -------- UPDATE INFO -------- ")
            console.log(f.title, f.link, f.feedUrl, f.description)

            console.time("addArticlesEx")

            var newArticles = []
            for (var i = 0; i < entries.length; i++) {
                var e = entries[i]

                // Grab image from for article.
                var articleImage = ImageUtils.grabArticleImage(e)
                e.content = clearFromBadTags(e.content)

                var temp =
                        {
                    "title": e.title,
                    "content": e.content,
                    "link": e.link,
                    "author": e.author,
                    "description": e.contentSnippet,
                    "pubDate": DateUtils.parseDate(e.publishedDate),
                    "guid": Qt.md5(e.content + e.publishedDate),
                    "image" : articleImage,
                    "media_groups" : e.mediaGroups //== undefined ? "" : JSON.stringify(e.mediaGroups),
                }

                newArticles.push(temp)
            }

//            /* Add new articles to DB and restore 'read' status of some of them.
//             */
//            DB.addArticles(articleModel, feedId, articleProperties);

            /* Add new articles to DB and restore 'read' status of some of them. */
            try {
                DB.addArticlesEx(newArticles, feedId)
            } catch (e) {
                console.log("Exception:", JSON.stringify(e))
            }

            console.timeEnd("addArticlesEx")
        }

        function clearFromBadTags(content) {
            /* Remove non empty too. Useless anyway.
             */
            content = content.replace(/alt=".*?"/g, "")
            content = content.replace(/title=".*?"/g, "")
            return content
        }

        property var googleFeedApi: GoogleFeedApi {
            onLoadResult: {
                if (result.responseStatus !== 200) {
                    console.log("XML NETWORK GFA:", JSON.stringify(result))
                    if (operationStatus == "success")
                        operationStatus = "withErrors"
                } else d.updateFeedInfo(d.currentFeed.id, d.currentFeed.link, result.responseData)

                d.updateNextFeed()
            }
        } // GFA
    } // QtObject
}