psa: improved add feed/categories dialog
[feedingit] / psa_harmattan / feedingit / qml / AddFeed.qml
1 import QtQuick 1.0
2 import com.nokia.meego 1.0
3 import "feedSearch.js" as FeedJS
4
5 Page {
6     id: addFeedPage
7     tools: tabTools
8     anchors.margins: UiConstants.DefaultMargin
9     anchors.fill: parent
10
11     TabGroup {
12         id: tabGroup
13         anchors.fill: parent
14         currentTab: searchTab
15
16         Item {
17             id: searchTab
18             anchors.fill: parent
19
20             Column {
21                 spacing: 10
22
23                 Text {
24                     id: searchText
25                     font.pixelSize: 22
26                     color: "white"
27                     text: "Search for feeds"
28                 }
29
30                 TextInputClear {
31                     id: searchInput
32                     placeHolder: "Enter a search term"
33                 }
34
35                 Button {
36                     id: searchButton
37                     text: "Search"
38                     onClicked: FeedJS.keywordSearch(searchInput.text)
39                 }
40             }
41
42             SelectionDialog {
43                      id: searchDialog
44                      titleText: "Select feed:"
45                      //model: []
46                      onSelectedIndexChanged: {
47                          if (selectedIndex>=0) {
48                              var feedTitle = searchDialog.model.get(searchDialog.selectedIndex).name;
49                              var feedUrl = searchDialog.model.get(searchDialog.selectedIndex).url;
50                              //controller.addFeed(feedTitle,feedUrl,1);
51                              categoryDialog.confirmCategory(feedTitle, feedUrl);
52                              console.log("Adding feed "+feedTitle+" at " +feedUrl);
53                          }
54                      }
55             }
56
57         } //searchTab
58
59         Item {
60             id: urlTab
61
62             Column {
63                 Text {
64                     id: urlText
65                     font.pixelSize: 22
66                     color: "white"
67                     text: "Enter the feed URL"
68                 }
69
70                 TextInputClear {
71                     id: urlInput
72                     placeHolder: "Feed URL"
73                     width: parent.width
74                 }
75
76                 Button {
77                     id: urlButton
78                     text: "Add"
79                     onClicked: {
80                         console.log("Adding "+urlInput.text)
81                         //controller.addFeed("",urlInput.text,1);
82                         categoryDialog.confirmCategory("", urlInput.text);
83                     }
84                     width: 150
85                 }
86             }
87
88         } //urlTab
89
90         Item {
91             id: categoryTab
92
93             Column {
94                 Text {
95                     id: categoryText
96                     font.pixelSize: 22
97                     color: "white"
98                     text: "Enter the category name"
99                 }
100
101                 TextInputClear {
102                     id: categoryInput
103                     placeHolder: "name"
104                     width: parent.width
105                 }
106
107                 Button {
108                     id: categoryButton
109                     text: "Add"
110                     onClicked: {
111                         if (categoryInput.text != "") {
112                             controller.addCategory(categoryInput.text)
113                             categoryDialog.reload()
114                             window.categoryReloadRequest()
115                         }
116                     }
117                     width: 150
118                 }
119             }
120
121         } //categoryTab
122
123         ToolBarLayout {
124             id: tabTools
125             ToolIcon {
126                 iconId: "toolbar-back"
127                 onClicked: {
128                     if (tabGroup.currentTab.depth > 1) {
129                         tabGroup.currentTab.pop()
130                     } else {
131                         pageStack.pop()
132                     }
133                     window.feedReloadRequest()
134                     window.categoryReloadRequest()
135                 }
136             }
137             ButtonRow {
138                 TabButton {
139                     text: "Search"
140                     tab: searchTab
141                 }
142                 TabButton {
143                     text: "URL"
144                     tab: urlTab
145                 }
146                 TabButton {
147                     text: "Categories"
148                     tab: categoryTab
149                 }
150             }
151
152         }
153     }
154
155     SelectionDialog {
156              id: categoryDialog
157              titleText: "Select category:"
158              property string feedTitle
159              property string feedUrl
160
161              //property variant listModel: ListModel {}
162
163              function createListModel() {
164                  var listModel = Qt.createQmlObject('import QtQuick 1.0; ListModel {}', categoryDialog);
165                  for (var i=0;i<categories.count;i++) {
166                      listModel.append({"name": categories.get(i).title, "catid": categories.get(i).catid });
167                  }
168                  model = listModel
169                  //console.log(listModel.count)
170              }
171
172              function confirmCategory(title, url) {
173                  //categoryDialog.selectedIndex = -1;
174                  createListModel();
175                  feedTitle = title
176                  feedUrl = url
177                  open();
178              }
179
180              function reload() {
181                  //categories.reload()
182                  categories.xml = controller.getCategoryXml()
183              }
184
185              //model: []
186              //model: categories
187
188              XmlListModel {
189                  id: categories
190                  xml: controller.getCategoryXml()
191                  query: "/xml/category"
192                  XmlRole { name: "title"; query: "catname/string()" }
193                  XmlRole { name: "catid"; query: "catid/string()"; isKey: true }
194              }
195
196              onSelectedIndexChanged: {
197                  if (selectedIndex>=0) {
198                      controller.addFeed(feedTitle,feedUrl,categoryDialog.model.get(categoryDialog.selectedIndex).catid);
199                      console.log("Adding feed "+feedTitle+" at " +feedUrl + " in " + categoryDialog.model.get(categoryDialog.selectedIndex).catid);
200                  }
201                  categoryDialog.selectedIndex = -1;
202              }
203     }
204 }