psa: implemented settings
[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     function addFileToDialog(fullname, filename) {
12         fileDialog.model.append({name: filename,
13                                  fullname: fullname})
14     }
15
16     TabGroup {
17         id: tabGroup
18         anchors.fill: parent
19         currentTab: searchTab
20
21         Item {
22             id: searchTab
23             anchors.fill: parent
24
25             Column {
26                 spacing: 10
27                 width: parent.width
28
29                 Text {
30                     id: searchText
31                     font.pixelSize: 22
32                     color: "white"
33                     text: qsTr("Add a feed by URL, or search by keyword")
34                 }
35
36                 TextInputClear {
37                     id: searchInput
38                     placeHolder: qsTr("Enter a URL or keywords")
39                 }
40
41                 Button {
42                     id: searchButton
43                     text: "Search"
44                     onClicked: {
45                         var key = searchInput.text
46                         if (key.substring(0, 4) === "http") {
47                             categoryDialog.confirmCategory("", urlInput.text);
48                         } else {
49                             FeedJS.keywordSearch(searchInput.text)
50                         }
51                     }
52                 }
53             }
54
55             SelectionDialog {
56                      id: searchDialog
57                      titleText: qsTr("Select feed:")
58                      //model: []
59                      onSelectedIndexChanged: {
60                          if (selectedIndex>=0) {
61                              var feedTitle = searchDialog.model.get(searchDialog.selectedIndex).name;
62                              var feedUrl = searchDialog.model.get(searchDialog.selectedIndex).url;
63                              //controller.addFeed(feedTitle,feedUrl,1);
64                              categoryDialog.confirmCategory(feedTitle, feedUrl);
65                              console.log("Adding feed "+feedTitle+" at " +feedUrl);
66                          }
67                      }
68             }
69
70         } //searchTab
71
72         Item {
73             id: categoryTab
74             anchors.fill: parent
75
76             Column {
77                 width: parent.width
78                 spacing: 10
79                 Text {
80                     id: categoryText
81                     font.pixelSize: 22
82                     color: "white"
83                     text: "Enter the category name"
84                 }
85
86                 TextInputClear {
87                     id: categoryInput
88                     placeHolder: "name"
89                     width: parent.width
90                 }
91
92                 Button {
93                     id: categoryButton
94                     text: "Add Category"
95                     onClicked: {
96                         if (categoryInput.text != "") {
97                             controller.addCategory(categoryInput.text)
98                             categoryDialog.reload()
99                             window.categoryReloadRequest()
100                         }
101                     }
102                     //width: 150
103                 }
104             }
105
106         } //categoryTab
107
108         ToolBarLayout {
109             id: tabTools
110             ToolIcon {
111                 iconId: "toolbar-back"
112                 onClicked: {
113                     if (tabGroup.currentTab.depth > 1) {
114                         tabGroup.currentTab.pop()
115                     } else {
116                         pageStack.pop()
117                     }
118                     window.feedReloadRequest()
119                     window.categoryReloadRequest()
120                 }
121             }
122             ButtonRow {
123                 TabButton {
124                     text: "Search"
125                     tab: searchTab
126                 }
127                 TabButton {
128                     text: "Categories"
129                     tab: categoryTab
130                 }
131             }
132             ToolIcon {
133                 platformIconId: "toolbar-view-menu"
134                 anchors.right: (parent === undefined) ? undefined : parent.right
135                 onClicked: (manageFeedsMenu.status == DialogStatus.Closed) ? manageFeedsMenu.open() : manageFeedsMenu.close()
136             }
137         }
138
139     }
140
141     Menu {
142         id: manageFeedsMenu
143         visualParent: pageStack
144         MenuLayout {
145             MenuItem { text: qsTr("Import Feeds From File"); onClicked: {
146                     fileDialog.model.clear()
147                     controller.populateFileDialog("/home/user/MyDocs/","*.*ml")
148                     fileDialog.open()
149                 } }
150             MenuItem {
151                 text: qsTr("Export Feeds");
152                 onClicked: { var file=controller.exportOpml("/home/user/MyDocs/feedingit-export.opml"); banner.show(qsTr("Feeds exported as %1").arg(file)) }
153             }
154         }
155     }
156
157     SelectionDialog {
158              id: categoryDialog
159              titleText: qsTr("Select category:")
160              property string feedTitle
161              property string feedUrl
162
163              //property variant listModel: ListModel {}
164
165              function createListModel() {
166                  var listModel = Qt.createQmlObject('import QtQuick 1.0; ListModel {}', categoryDialog);
167                  for (var i=0;i<categories.count;i++) {
168                      listModel.append({"name": categories.get(i).title, "catid": categories.get(i).catid });
169                  }
170                  model = listModel
171                  //console.log(listModel.count)
172              }
173
174              function confirmCategory(title, url) {
175                  //categoryDialog.selectedIndex = -1;
176                  createListModel();
177                  feedTitle = title
178                  feedUrl = url
179                  open();
180              }
181
182              function reload() {
183                  //categories.reload()
184                  categories.xml = controller.getCategoryXml()
185              }
186
187              //model: []
188              //model: categories
189
190              XmlListModel {
191                  id: categories
192                  xml: controller.getCategoryXml()
193                  query: "/xml/category"
194                  XmlRole { name: "title"; query: "catname/string()" }
195                  XmlRole { name: "catid"; query: "catid/string()"; isKey: true }
196              }
197
198              onSelectedIndexChanged: {
199                  if (selectedIndex>=0) {
200                      controller.addFeed(feedTitle,feedUrl,categoryDialog.model.get(categoryDialog.selectedIndex).catid);
201                      console.log("Adding feed "+feedTitle+" at " +feedUrl + " in " + categoryDialog.model.get(categoryDialog.selectedIndex).catid);
202                  }
203                  categoryDialog.selectedIndex = -1;
204              }
205     }
206
207     SelectionDialog {
208         id: fileDialog
209         titleText: qsTr("Select a file to import")
210         model: ListModel {
211                 id: fileList
212                }
213
214         onSelectedIndexChanged: {
215             var num = controller.importOpml(fileDialog.model.get(fileDialog.selectedIndex).fullname);
216             console.log("Import "+fileDialog.model.get(fileDialog.selectedIndex).fullname)
217             banner.show(qsTr("Successfully imported %1 feeds.").args(num));
218         }
219     }
220 }