~mrqtros/ubuntu-rssreader-app/ubuntu-rssreader-app-to-tabs

« back to all changes in this revision

Viewing changes to shorts/qml/pages/ChooseTopicPage.qml

  • Committer: Roman Shchekin
  • Date: 2015-07-04 08:38:18 UTC
  • Revision ID: mrqtros@gmail.com-20150704083818-ecojm3nmy5bpkxrb
Merge with the Reboot project.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.4
 
2
import QtQuick.XmlListModel 2.0
 
3
import Ubuntu.Components 1.2
 
4
import Ubuntu.Components.ListItems 1.0 as ListItem
 
5
import Ubuntu.Components.Popups 1.0
 
6
 
 
7
import "../utils/databasemodule_v2.js" as DB
 
8
 
 
9
Page {
 
10
    id: chooseTopicPage
 
11
 
 
12
    objectName: "choosetopicpage"
 
13
    title: i18n.tr("Choose topic")
 
14
    flickable: null
 
15
 
 
16
    property var feedsToAdd: null
 
17
    signal topicChoosen(int topicId, var addedFeeds)
 
18
 
 
19
    function reloadPageContent() {
 
20
        suggestionTopicsModel.clear()
 
21
 
 
22
        var tags = DB.loadTags()
 
23
 
 
24
        for (var i = 0; i < tags.rows.length; i++) {
 
25
            var curTag = tags.rows.item(i)
 
26
 
 
27
            suggestionTopicsModel.append({"tagName" : curTag.name, "tagId" : curTag.id})
 
28
        }
 
29
    }
 
30
 
 
31
    function appendFeedsToTopic(topicId) {
 
32
        var tagId = topicId
 
33
 
 
34
        var updateList = []
 
35
 
 
36
        for(var i = 0; i < feedsToAdd.length; i++) {
 
37
 
 
38
            var f = feedsToAdd[i]
 
39
 
 
40
            // TODO should I remove html tags or not? :)
 
41
            var feedTitle = f.title
 
42
            feedTitle = feedTitle.replace(/<[^>]*>/g, '')
 
43
 
 
44
            var dbResult = DB.addFeed(feedTitle, f.url)
 
45
 
 
46
            if (dbResult.error) {
 
47
                // TODO Exist
 
48
                continue
 
49
            }
 
50
 
 
51
            DB.updateFeedByXml(dbResult.feedId, f.link, f.description, feedTitle)
 
52
            DB.addFeedTag(dbResult.feedId, tagId)
 
53
 
 
54
            updateList.push({"source" : f.url, "link" : f.link, "id" : dbResult.feedId})
 
55
        }
 
56
 
 
57
        pageStack.pop()
 
58
        pageStack.pop()
 
59
 
 
60
        topicChoosen(tagId, updateList)
 
61
    }
 
62
 
 
63
    ListView {
 
64
        id: suggestionTopics
 
65
 
 
66
        width: parent.width
 
67
        clip: true
 
68
        anchors {
 
69
            left: parent.left; right: parent.right
 
70
            top: parent.top; bottom: parent.bottom
 
71
        }
 
72
 
 
73
        model: suggestionTopicsModel
 
74
 
 
75
        header: ListItem.Header {
 
76
            ListItem.ThinDivider { }
 
77
            text: i18n.tr("Add your new feeds to a topic")
 
78
        }
 
79
 
 
80
        footer: Item {
 
81
                    width: parent.width
 
82
                    height: tfNewTopicName.height + units.gu(2)
 
83
 
 
84
                    TextField {
 
85
                        objectName: "newTopic"
 
86
                        id: tfNewTopicName
 
87
 
 
88
                        placeholderText: i18n.tr(" + New topic")
 
89
 
 
90
                        width: parent.width - units.gu(4)
 
91
                        anchors {
 
92
                            horizontalCenter: parent.horizontalCenter
 
93
                            verticalCenter: parent.verticalCenter
 
94
                        }
 
95
 
 
96
                        onAccepted: {
 
97
                            var tagName = text.replace(/^\s+|\s+$/g, '')  // Trimming whitespaces.
 
98
                            if (tagName != "") { // Check that tagName contains only spaces.
 
99
 
 
100
                                /* Make first letter capital.
 
101
                                 */
 
102
                                tagName = tagName.charAt(0).toUpperCase() + tagName.slice(1)
 
103
 
 
104
                                var dbResult = DB.addTag(tagName)
 
105
 
 
106
                                if (dbResult.error) {
 
107
                                    PopupUtils.open(errorDialogComponent, chooseTopicPage,
 
108
                                                    {"text" : i18n.tr("A topic with this name already exists"),
 
109
                                                        "title" : i18n.tr("Warning")})
 
110
                                    return
 
111
                                }
 
112
 
 
113
                                suggestionTopicsModel.append({"tagName" : tagName,
 
114
                                                                 "tagId" : dbResult.tagId})
 
115
 
 
116
                                text = ""
 
117
 
 
118
                                appendFeedsToTopic(dbResult.tagId)
 
119
                            } else {
 
120
                                PopupUtils.open(errorDialogComponent, chooseTopicPage,
 
121
                                                {"text" : i18n.tr("Topic name can't contain only whitespaces"),
 
122
                                                    "title" : i18n.tr("Warning")})
 
123
                            }
 
124
                        }
 
125
                    }
 
126
                }
 
127
 
 
128
        delegate: ListItem.Standard {
 
129
            objectName: "topicItem"
 
130
            text: model.tagName
 
131
 
 
132
            onClicked: {
 
133
                console.log("Topic selected", model.tagId, model.tagName)
 
134
 
 
135
                appendFeedsToTopic(model.tagId)
 
136
            }
 
137
        }
 
138
    }
 
139
 
 
140
    ListModel {
 
141
        id: suggestionTopicsModel
 
142
    }
 
143
}