SHOW:
|
|
- or go back to the newest paste.
| 1 | import QtQuick 2.0 | |
| 2 | import QtQuick.LocalStorage 2.0 | |
| 3 | import Ubuntu.Components 0.1 | |
| 4 | ||
| 5 | /*! | |
| 6 | \brief MainView with Tabs element. | |
| 7 | First Tab has a single Label and | |
| 8 | second Tab has a single ToolbarAction. | |
| 9 | */ | |
| 10 | ||
| 11 | MainView {
| |
| 12 | // objectName for functional testing purposes (autopilot-qt5) | |
| 13 | id: mainView | |
| 14 | objectName: "mainView" | |
| 15 | applicationName: "news-feed" | |
| 16 | ||
| 17 | width: units.gu(100) | |
| 18 | height: units.gu(75) | |
| 19 | ||
| 20 | tools: ToolbarActions {
| |
| 21 | Action {
| |
| 22 | objectName: "action" | |
| 23 | ||
| 24 | iconSource: Qt.resolvedUrl("avatar.png")
| |
| 25 | text: i18n.tr("Tap me!")
| |
| 26 | ||
| 27 | onTriggered: {
| |
| 28 | label.text = i18n.tr("Toolbar tapped")
| |
| 29 | } | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | Component.onCompleted: {
| |
| 34 | mainView.initializeDB(); | |
| 35 | mainView.saveFeed("BBC News","http://feeds.bbci.co.uk/news/rss.xml");
| |
| 36 | mainView.saveFeed("Jono Bacon","http://www.jonobacon.org/?feed=rss2");
| |
| 37 | mainView.saveFeed("The Register", "http://www.theregister.co.uk/headlines.atom");
| |
| 38 | fillTabs(); | |
| 39 | } | |
| 40 | ||
| 41 | Tabs {
| |
| 42 | id: initialtabs | |
| 43 | anchors.fill: parent | |
| 44 | ||
| 45 | tools: ToolbarActions {
| |
| 46 | Action {
| |
| 47 | objectName: "action" | |
| 48 | ||
| 49 | iconSource: Qt.resolvedUrl("avatar.png")
| |
| 50 | text: i18n.tr("Tap me!")
| |
| 51 | ||
| 52 | onTriggered: {
| |
| 53 | label.text = i18n.tr("Toolbar tapped")
| |
| 54 | } | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | // First tab begins here | |
| 59 | Tab {
| |
| 60 | id: tabFrontPage | |
| 61 | objectName: "tabFrontPage" | |
| 62 | ||
| 63 | title: i18n.tr("Front Page")
| |
| 64 | ||
| 65 | // Tab content begins here | |
| 66 | page: Page {
| |
| 67 | Column {
| |
| 68 | anchors.centerIn: parent | |
| 69 | Label {
| |
| 70 | id: labelFrontPage | |
| 71 | text: i18n.tr("This will be the front page \n An aggregation of the top stories from each feed")
| |
| 72 | } | |
| 73 | } | |
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | ||
| 79 | function fillTabs() {
| |
| 80 | initialtabs.destroy(); | |
| 81 | var objStr = "import QtQuick 2.0;import Ubuntu.Components 0.1;import QtQuick.XmlListModel 2.0;Tabs{id:tabs;anchors.fill:parent;"
| |
| 82 | var db = getDatabase(); | |
| 83 | db.transaction(function(tx) {
| |
| 84 | var rs = tx.executeSql('SELECT * FROM feeds;');
| |
| 85 | if (rs.rows.length > 0) {
| |
| 86 | for(var i = 0; i < rs.rows.length; i++) {
| |
| 87 | objStr += "Tab {id:tab" + i + ";anchors.fill:parent;title:'" + rs.rows.item(i).feedName + "';property string source: '" + rs.rows.item(i).feedURL + "';page: Page {anchors.margins: units.gu(2);Column {anchors.centerIn: parent;Label{text:tab" + i + ".source;}}}}";
| |
| 88 | - | ////objStr += "Tab {id:tab" + i + ";anchors.fill:parent;title:'" + rs.rows.item(i).feedName + "';property string source: '" + rs.rows.item(i).feedURL + "';page: Page {anchors.margins: units.gu(2);Column {anchors.centerIn: parent;XmlListModel {id: xmlModel" + i + ";source: '" + rs.rows.item(i).feedUrl +"';query: '/rss/channel/item';XmlRole { name: 'title'; query: 'title/string()' }XmlRole { name: 'pubDate'; query: 'pubDate/string()' }}}}}";
|
| 88 | + | |
| 89 | - | //objStr += "Tab {id:tab" + i + ";anchors.fill:parent;title:'" + rs.rows.item(i).feedName + "';property string source: '" + rs.rows.item(i).feedURL + "';page: Page {anchors.margins: units.gu(2);Column {anchors.centerIn: parent;XmlListModel {id: xmlModel" + i + ";source: '" + rs.rows.item(i).feedUrl +"';query: '/rss/channel/item';XmlRole { name: 'title'; query: 'title/string()' }XmlRole { name: 'pubDate'; query: 'pubDate/string()' }}ListView {anchors.fill: parent;model: xmlModel" + i + ";delegate: Text { text: title}}}}}";
|
| 89 | + | |
| 90 | var cmpTabs = Qt.createQmlObject(objStr,mainView,"tabsfile"); | |
| 91 | } else {
| |
| 92 | res = "Unknown"; | |
| 93 | } | |
| 94 | }) | |
| 95 | } | |
| 96 | ||
| 97 | //Create tabs for each feed | |
| 98 | function createTabs() {
| |
| 99 | var feeds = getFeeds(); | |
| 100 | for (var i = 0; i < feeds.length; i++){
| |
| 101 | //Add tab for each feed. | |
| 102 | //Cannot be done with existing API | |
| 103 | ||
| 104 | } | |
| 105 | } | |
| 106 | ||
| 107 | //Storage API | |
| 108 | function getDatabase() {
| |
| 109 | ||
| 110 | return LocalStorage.openDatabaseSync("news-feed","1.0","StorageDatabase",10000)
| |
| 111 | } | |
| 112 | ||
| 113 | //Initialise DB tables if not already existing | |
| 114 | function initializeDB() {
| |
| 115 | var db = getDatabase(); | |
| 116 | db.transaction(function(tx) {
| |
| 117 | //Create settings table if not existing | |
| 118 | tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
| |
| 119 | tx.executeSql('CREATE TABLE IF NOT EXISTS feeds(feedName TEXT UNIQUE, feedURL TEXT UNIQUE)')
| |
| 120 | }); | |
| 121 | } | |
| 122 | ||
| 123 | //Write setting to DB | |
| 124 | function setSetting(setting,value){
| |
| 125 | //setting: string - setting name (key) | |
| 126 | //value: string - value | |
| 127 | var db = getDatabase(); | |
| 128 | var res = ""; | |
| 129 | db.transaction(function(tx) {
| |
| 130 | var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);',[setting,value]);
| |
| 131 | //console.log(rs.rowsAffected) | |
| 132 | if(rs.rowsAffected > 0) {
| |
| 133 | res = "OK"; | |
| 134 | } else {
| |
| 135 | res = "Error"; | |
| 136 | } | |
| 137 | }) | |
| 138 | return res; | |
| 139 | } | |
| 140 | ||
| 141 | //Read setting from DB | |
| 142 | function getSetting(setting) {
| |
| 143 | var db = getDatabase(); | |
| 144 | var res=""; | |
| 145 | db.transaction(function(tx) {
| |
| 146 | var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
| |
| 147 | if (rs.rows.length > 0) {
| |
| 148 | res = rs.rows.item(0).value; | |
| 149 | } else {
| |
| 150 | res = "Unknown"; | |
| 151 | } | |
| 152 | }) | |
| 153 | // The function returns “Unknown” if the setting was not found in the database | |
| 154 | // For more advanced projects, this should probably be handled through error codes | |
| 155 | return res; | |
| 156 | } | |
| 157 | ||
| 158 | function saveFeed(feedName, feedURL) {
| |
| 159 | var db = getDatabase(); | |
| 160 | var res = ""; | |
| 161 | db.transaction(function(tx){
| |
| 162 | var rs = tx.executeSql('INSERT OR REPLACE INTO feeds VALUES (?,?)',[feedName,feedURL]);
| |
| 163 | //console.log(rs.rowsAffected) | |
| 164 | if (rs.rowsAffected > 0) {
| |
| 165 | res = "OK"; | |
| 166 | } else {
| |
| 167 | res = "Error"; | |
| 168 | } | |
| 169 | }) | |
| 170 | return res; | |
| 171 | } | |
| 172 | ||
| 173 | //Return a single feed | |
| 174 | function getFeed(feedName) {
| |
| 175 | var db = getDatabase(); | |
| 176 | var res = ""; | |
| 177 | db.transaction(function(tx) {
| |
| 178 | var rs = tx.executeSql('SELECT feedURL FROM feeds WHERE feedName=?;', [feedName]);
| |
| 179 | if (rs.rows.length > 0) {
| |
| 180 | res = rs.rows.item(0).feedURL; | |
| 181 | } else {
| |
| 182 | res = "Unknown"; | |
| 183 | } | |
| 184 | ||
| 185 | }) | |
| 186 | return res; | |
| 187 | } | |
| 188 | ||
| 189 | //Return all feeds and urls | |
| 190 | function getFeeds() {
| |
| 191 | var db = getDatabase(); | |
| 192 | var res = ""; | |
| 193 | db.transaction(function(tx) {
| |
| 194 | var rs = tx.executeSql('SELECT * FROM feeds;');
| |
| 195 | if (rs.rows.length > 0) {
| |
| 196 | return rs; | |
| 197 | } else {
| |
| 198 | res = "Unknown"; | |
| 199 | } | |
| 200 | }) | |
| 201 | return res; | |
| 202 | } | |
| 203 | } |