b0f0433b824fa26c5ae7f7af1dcc3478d86b91c2
[marketstoday] / src / qml / MarketsToday.qml
1 /*
2 @version: 0.1
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5 */
6
7 import Qt 4.7
8
9 import "Library" as Library
10 import "Library/js/ISODate.js" as DateLib
11 import "Library/js/DBUtility.js" as DBUtility
12 import "Library/js/Common.js" as Common
13
14 Item {
15     id: screen; width: 400; height: 325
16
17     signal showConfigInNewWindow
18     signal quoteRefreshStarted
19     signal quoteRefreshCompleted
20
21     property int autoUpdateInterval: 300000
22     property bool updateWeekDaysOnly: false
23
24     function getQuery(){
25         var query;
26         var symbolsArray = DBUtility.getAllSymbols();
27         if (symbolsArray && symbolsArray.length > 0){
28             var i = 0;
29             for (i = 0; i< symbolsArray.length; i++) {
30                 logUtility.logMessage("Appending "+symbolsArray[i]+ " to Query");
31
32                 if (!query){
33                     query = '"'+symbolsArray[i]+'"';
34                 }
35                 else{
36                     query = query + ',"' + symbolsArray[i]+'"';
37                 }
38             }
39         }
40
41         return query;        
42     }
43
44
45     function reloadQuotes(){
46         var query = getQuery();
47         if (query){
48             screen.quoteRefreshStarted();
49             logUtility.logMessage("Reloading Data..");
50
51             //var queryURL = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol in ("INDU","^IXIC","^GSPC","CLJ11.NYM","YHOO","AAPL","GOOG","MSFT")&env=store://datatables.org/alltableswithkeys';
52             var queryURL = 'http://query.yahooapis.com/v1/public/yql?q=select Symbol,Name,LastTradePriceOnly,Change,ChangeinPercent from yahoo.finance.quotes where symbol in ('+query+')&env=store://datatables.org/alltableswithkeys';
53             logUtility.logMessage(queryURL);
54
55             var response = new XMLHttpRequest();
56             response.onreadystatechange = function() {
57                 if (response.readyState == XMLHttpRequest.DONE) {
58                     stockQuoteDataModel.xml = response.responseText;
59                     lastUpdatedDateModel.xml = response.responseText;
60                     logUtility.logMessage("Data Reload Completed..");
61                     screen.quoteRefreshCompleted();
62                 }
63             }
64
65             response.open("GET", queryURL);
66             response.send();
67         }
68         else{
69             logUtility.logMessage("No stock symbols found in configuration.");
70         }
71     }
72
73     function loadSettings(){
74         var value;
75         value  = DBUtility.getSetting("UpdateFreqency");
76         if (!value || value == "0.0" || value == ""){
77             autoUpdateInterval = 0;
78         }
79         else{
80             autoUpdateInterval = parseInt(value)*60*1000; //Convert minutes to milliseconds
81         }
82         value  = DBUtility.getSetting("UpdateWeekdaysOnly");
83         if (!value || value == "0.0" || value == ""){
84             updateWeekDaysOnly = false;
85         }
86         else{
87             updateWeekDaysOnly = true;
88         }
89     }
90
91     function initialize(){
92         if (autoUpdateTimer.running) autoUpdateTimer.stop();
93         loadSettings();
94         reloadQuotes();
95         if (autoUpdateInterval != 0) {
96             logUtility.logMessage("Starting Timer..");
97             autoUpdateTimer.start();
98         }
99     }
100
101     Component.onCompleted: {
102         initialize();
103     }
104
105     Timer {
106         id: autoUpdateTimer
107         interval: autoUpdateInterval
108         //running: (autoUpdateInterval == 0? false:true)
109         repeat: true
110         onTriggered: {
111             if (!updateWeekDaysOnly){
112                 logUtility.logMessage("Update triggered: Allowed to update all days of the week");
113                 reloadQuotes();
114             }
115             else if (Common.isTodayAWeekDay()){
116                 logUtility.logMessage("Update triggered: Today is a weekday");
117                 reloadQuotes();
118             }
119             else{
120                 logUtility.logMessage("Update not triggered: Today is not a weekday");
121             }
122         }
123     }
124
125     XmlListModel{
126         id: stockQuoteDataModel
127         query:  "/query/results/quote"
128
129         XmlRole { name: "symbol"; query: "Symbol/string()" }
130         XmlRole { name: "stockName"; query: "Name/string()" }
131         XmlRole { name: "lastTradedPrice"; query: "LastTradePriceOnly/string()" }
132         XmlRole { name: "change"; query: "Change/string()" }
133         XmlRole { name: "changePercentage"; query: "ChangeinPercent/string()" }
134
135         onStatusChanged: {
136             if (status == XmlListModel.Ready){
137                 logUtility.logMessage("No. of tickers: "+stockQuoteDataModel.count);
138             }
139         }
140     }
141
142     XmlListModel {
143         id: lastUpdatedDateModel
144         query: "/query"
145         namespaceDeclarations: "declare namespace yahoo='http://www.yahooapis.com/v1/base.rng';"
146         XmlRole { name: "timestamp"; query: '@yahoo:created/string()'}
147
148         onStatusChanged: {
149             if (status == XmlListModel.Ready && lastUpdatedDateModel.get(0)){
150                 logUtility.logMessage("Updated: "+DateLib.ISODate.format(lastUpdatedDateModel.get(0).timestamp));
151             }
152         }
153     }            
154
155     Rectangle {
156         id: background        
157         anchors.fill: parent;
158         color: "#343434"
159         clip: true
160         property int itemHeight: 50
161
162         Library.TitleBar {
163             id: titleBar;
164             width: parent.width; height: 60;
165             anchors.top: parent.top
166             title: "Markets Today";
167             buttonType: "Config";
168             onSettingsClicked: {
169                 screen.showConfigInNewWindow();
170             }
171
172             onCloseClicked: {
173                 Qt.quit();
174             }
175         }
176
177         Loader {
178             id: marketsTodayLoader
179             anchors.top: titleBar.bottom
180             anchors.bottom: parent.bottom
181             width: parent.width
182             sourceComponent: stockQuotesParentComponent
183         }
184
185         Component {
186             id: stockQuotesParentComponent
187             StockQuotesComponent{
188                 id:stockQuotesComponent
189                 componentWidth: background.width
190                 stockQuotesListModel: stockQuoteDataModel
191                 lastUpdatedModel: lastUpdatedDateModel
192
193                 Connections {
194                     target: screen
195
196                     onQuoteRefreshStarted: {
197                         stockQuotesComponent.updateStarted();
198                     }
199
200                     onQuoteRefreshCompleted: {
201                         stockQuotesComponent.updateCompleted();
202                     }
203                 }
204
205                 Component.onCompleted: {
206                     titleBar.title = "Markets Today";
207                     titleBar.buttonType = "Config";
208                 }
209             }
210         }
211     }
212 }