Advertisement
Guest User

Untitled

a guest
Feb 27th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  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()' }}}}}";
  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}}}}}";
  90. }
  91. objStr += "}";
  92. var cmpTabs = Qt.createQmlObject(objStr,mainView,"tabsfile");
  93. } else {
  94. res = "Unknown";
  95. }
  96. })
  97. }
  98.  
  99. //Create tabs for each feed
  100. function createTabs() {
  101. var feeds = getFeeds();
  102. for (var i = 0; i < feeds.length; i++){
  103. //Add tab for each feed.
  104. //Cannot be done with existing API
  105.  
  106. }
  107. }
  108.  
  109. //Storage API
  110. function getDatabase() {
  111.  
  112. return LocalStorage.openDatabaseSync("news-feed","1.0","StorageDatabase",10000)
  113. }
  114.  
  115. //Initialise DB tables if not already existing
  116. function initializeDB() {
  117. var db = getDatabase();
  118. db.transaction(function(tx) {
  119. //Create settings table if not existing
  120. tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
  121. tx.executeSql('CREATE TABLE IF NOT EXISTS feeds(feedName TEXT UNIQUE, feedURL TEXT UNIQUE)')
  122. });
  123. }
  124.  
  125. //Write setting to DB
  126. function setSetting(setting,value){
  127. //setting: string - setting name (key)
  128. //value: string - value
  129. var db = getDatabase();
  130. var res = "";
  131. db.transaction(function(tx) {
  132. var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);',[setting,value]);
  133. //console.log(rs.rowsAffected)
  134. if(rs.rowsAffected > 0) {
  135. res = "OK";
  136. } else {
  137. res = "Error";
  138. }
  139. })
  140. return res;
  141. }
  142.  
  143. //Read setting from DB
  144. function getSetting(setting) {
  145. var db = getDatabase();
  146. var res="";
  147. db.transaction(function(tx) {
  148. var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
  149. if (rs.rows.length > 0) {
  150. res = rs.rows.item(0).value;
  151. } else {
  152. res = "Unknown";
  153. }
  154. })
  155. // The function returns “Unknown” if the setting was not found in the database
  156. // For more advanced projects, this should probably be handled through error codes
  157. return res;
  158. }
  159.  
  160. function saveFeed(feedName, feedURL) {
  161. var db = getDatabase();
  162. var res = "";
  163. db.transaction(function(tx){
  164. var rs = tx.executeSql('INSERT OR REPLACE INTO feeds VALUES (?,?)',[feedName,feedURL]);
  165. //console.log(rs.rowsAffected)
  166. if (rs.rowsAffected > 0) {
  167. res = "OK";
  168. } else {
  169. res = "Error";
  170. }
  171. })
  172. return res;
  173. }
  174.  
  175. //Return a single feed
  176. function getFeed(feedName) {
  177. var db = getDatabase();
  178. var res = "";
  179. db.transaction(function(tx) {
  180. var rs = tx.executeSql('SELECT feedURL FROM feeds WHERE feedName=?;', [feedName]);
  181. if (rs.rows.length > 0) {
  182. res = rs.rows.item(0).feedURL;
  183. } else {
  184. res = "Unknown";
  185. }
  186.  
  187. })
  188. return res;
  189. }
  190.  
  191. //Return all feeds and urls
  192. function getFeeds() {
  193. var db = getDatabase();
  194. var res = "";
  195. db.transaction(function(tx) {
  196. var rs = tx.executeSql('SELECT * FROM feeds;');
  197. if (rs.rows.length > 0) {
  198. return rs;
  199. } else {
  200. res = "Unknown";
  201. }
  202. })
  203. return res;
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement