~carla-sella/ubuntu-rssreader-app/edit-topic-test

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

import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItems
import Ubuntu.Components.Popups 0.1

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

Item {
    id: xmlNetworkItem

    property var feedList: []               // feed list from database
    property var currentFeed                // current downloading feed

    property int tagId: 0

    signal downloadFinished(int tagId) // TODO RENAME
    signal downloadStarted()

    /* 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()

        feedList = feedsArray
        updateNextFeed()
    }

    // For inner usage only.
    function updateNextFeed() {
        if (feedList.length == 0) {
            downloadFinished(tagId)
            return
        }
        //var feedArray = feedList
        //var oneFeed = feedList.shift()
        // console.log(JSON.stringify(oneFeed))
        //feedList = feedArray
        currentFeed = feedList.shift()
        googleFeedApi.loadFeed(currentFeed.source)
    }

    function cancelDownload() {
        feedList = []
        googleFeedApi.abort()
        downloadFinished(tagId)
    }

    /* Temporary here - TODO move to separate file.
     */
    function updateFeedInfo(feedId, feedLink, responseData) {
        var entries = responseData.feed.entries
        var f = responseData.feed

        // TODO MB NO NEED
        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)

        // save some needed properties before clear articles, currently save status
        var articleProperties = DB.preloadArticlesProperties(feedId)

        DB.clearArticles(feedId)
        // articleModel.clear()
        var articleModel = []

        for (var i = 0; i < entries.length; i++)
        {
            var e = entries[i]
            // Grab image from for article.
            // Joey, please, don't comment out my working code.
            var articleImage = grabArticleImage(e)
            e.content = clearFromBadTags(e.content)

            var temp =
                    {
                "title": e.title,
                "content": e.content,
                "link": e.link,
                "description": e.contentSnippet,
                "pubDate": DateUtils.parseDate(e.publishedDate),
                "guid": Qt.md5(e.content + e.publishedDate),
                "image" : articleImage
            }
            //articleModel.append(temp);
            articleModel.push(temp)
        }

        // Temporary, for code compatibility.
        articleModel.count = articleModel.length
        articleModel.get = function(index) {
            return this[index]
        }

        DB.addArticles(articleModel, feedId, articleProperties); // restore properties when add articles
    }

    // Not naive anymore :)
    function grabArticleImage(e) {
        if (e.mediaGroups) {
            var medias = e.mediaGroups
            for (var i = 0; i < medias.length; i++) {
                var media = medias[i]

                for (var j = 0; j < media.contents.length; j++) {
                    var cont = media.contents[j]

                    if (cont.type === "image/jpeg" || cont.type === "image/png" ||
                            cont.type === "image/jpeg" || cont.type === "image/pjpeg" ||
                            cont.type === "image/svg+xml" || cont.medium === "image") {
                        return cont.url
                    }
                }
            }
        }

        var image = ImageUtils.getFirstImage(e.content)
        if (image != null) {
            return image
        }

        return null
    }

    function clearFromBadTags(content) {
        content = content.replace(/alt=""/, "")
        content = content.replace(/title=""/, "")
        return content
    }

    // -------------------------------------------------------

    // -------------------------------------       GOOGLE API

    Connections {
        target: googleFeedApi

        onLoadResult: {
            if (result.responseStatus !== 200) {
                // TODO Handle error
                console.log("XML NETWORK GFA:", JSON.stringify(result))
                // TODO ERROR MB
                updateNextFeed()
                return
            }

            updateFeedInfo(currentFeed.id, currentFeed.link, result.responseData)
            updateNextFeed()
        }
    }

    GoogleFeedApi {
        id: googleFeedApi
    }

    // -------------------------------------       GOOGLE API
}