Licensing (suite)
[quicknewsreader] / qml / QuickNewsReader / content / modelimpl / GoogleReaderSourceModel.qml
1 /***
2 ** Copyright (C) 2012 Christophe CHAPUIS <chris.chapuis _at_ gmail _dot_ com>
3 **
4 ** This package is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This package is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this package; if not, write to the Free Software
16 ** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17 **
18 ***/
19 import QtQuick 1.0
20 import "../modelitf"
21 import "../js/SettingsStorage.js" as Storage
22
23 SourceModel {
24     id: googleReaderModel
25     name: "Google Reader"
26
27     listViews: [
28         { viewComponent: 'content/view/Categories.qml' },
29         { viewComponent: 'content/view/News.qml' }
30     ]
31     listModels: [
32         categoriesModel,
33         categoryContentModel
34     ]
35
36     property variant categoriesModel: GoogleReaderCategories { }
37     property variant categoryContentModel: GoogleReaderNews { sourceDepth: 2 }
38     property variant categorySubContentModel: GoogleReaderNews { sourceDepth: 3 }
39     property variant newsDetailModel: QtObject {
40         property int sourceDepth: 3
41
42         property string htmlcontent: ""
43         property string title: ""
44         property string image: ""
45     }
46     property variant sid;
47     property variant sidToken;
48
49     loading: false
50     hasSettings: true
51     settingsComponent: "GoogleReaderConfig.qml"
52     function storeConfiguration(configUI) {
53         // save the values in the database
54         Storage.setSetting("GoogleReader.login", configUI.loginValue)
55         Storage.setSetting("GoogleReader.password", configUI.passwordValue)
56
57         tryLogin()
58     }
59     function loadConfiguration(configUI) {
60         // retrieve the values from the database
61         configUI.loginValue = Storage.getSetting("GoogleReader.login")
62         configUI.passwordValue = Storage.getSetting("GoogleReader.password")
63     }
64
65     property variant googleReaderLoginWorker: WorkerScript {
66              id: googleReaderLoginWorker
67              source: "../js/GoogleReaderAPI.js"
68
69              onMessage: {
70                  sid = messageObject.sid
71                  sidToken = messageObject.sidToken
72
73                  loading = false
74              }
75          }
76
77     property variant googleReaderLoadCategoryWorker: WorkerScript {
78              id: googleReaderLoadCategoryWorker
79              source: "../js/GoogleReaderAPI.js"
80
81              onMessage: {
82                  categoryContentModel.append({ 'title': messageObject.title, 'description': messageObject.published, 'image': '', 'id': messageObject.id })
83
84                  loading = false
85              }
86          }
87     property variant googleReaderLoadSubscriptionOrTagWorker: WorkerScript {
88              id: googleReaderLoadSubscriptionOrTagWorker
89              source: "../js/GoogleReaderAPI.js"
90
91              onMessage: {
92                  categorySubContentModel.append({ 'title': messageObject.title, 'description': messageObject.published, 'image': '', 'content': messageObject.description })
93
94                  loading = false
95              }
96          }
97     property variant googleReaderLoadItemWorker: WorkerScript {
98              id: googleReaderLoadItemWorker
99              source: "../js/GoogleReaderAPI.js"
100
101              onMessage: {
102                  newsDetailModel.htmlcontent = messageObject.newsContent
103
104                  loading = false
105              }
106          }
107
108     function tryLogin() {
109         var loginValue = Storage.getSetting("GoogleReader.login")
110         var passwordValue = Storage.getSetting("GoogleReader.password")
111
112         loading = true
113
114         googleReaderLoginWorker.sendMessage({
115                                            'action': 'login',
116                                            'email': loginValue,
117                                            'password': passwordValue
118                                        })
119     }
120     onCurrentPathChanged: {
121         // build the right model. currentPath[1] => category
122         var selectionDepth = 0;
123         while(typeof currentPath[selectionDepth+1] !== "undefined")
124             selectionDepth++;
125
126         if( typeof currentPath[1] !== "undefined" ) {
127             if( selectionDepth == 1 ) {
128                 // the category has been selected, so fill in the right content
129                 categoryContentModel.clear()
130
131                 // reshape the views and the models to fit the chosen path
132                 var newsDetailIndex = 2;
133                 var tmpListModels = listModels
134                 var tmpListViews = listViews
135                 if( currentPath[1] === 3 || currentPath[1] === 4 )
136                 {
137                     tmpListModels[2] = categorySubContentModel;
138                     tmpListViews[2] = { viewComponent: 'content/view/News.qml' }
139                     newsDetailIndex = 3;
140                 }
141                 tmpListModels[newsDetailIndex] = newsDetailModel;
142                 tmpListViews[newsDetailIndex] = { viewComponent: 'content/view/NewsDetail.qml' };
143                 tmpListModels[newsDetailIndex+1] = null
144                 tmpListViews[newsDetailIndex+1] = null
145                 listModels = tmpListModels
146                 listViews = tmpListViews
147
148                 googleReaderLoadCategoryWorker.sendMessage({
149                                                             'action': 'getCategoryContent',
150                                                             'sid': sid, 'sidToken': sidToken,
151                                                             'category': currentPath[1]
152                                                         })
153             }
154             else if( selectionDepth == 2 && currentPath[1] === 3 ) {
155                 // subscriptions selected
156                 categorySubContentModel.clear()
157                 googleReaderLoadSubscriptionOrTagWorker.sendMessage({
158                                                             'action': 'getSubscriptionItems',
159                                                             'sid': sid, 'sidToken': sidToken,
160                                                             'subscription': categoryContentModel.get(currentPath[2]).id
161                                                         })
162             }
163             else if( selectionDepth == 2 && currentPath[1] === 4 ) {
164                 // tags selected
165                 categorySubContentModel.clear()
166                 googleReaderLoadSubscriptionOrTagWorker.sendMessage({
167                                                             'action': 'getTaggedItems',
168                                                             'sid': sid, 'sidToken': sidToken,
169                                                             'tag': categoryContentModel.get(currentPath[2]).id
170                                                         })
171             }
172             else if( selectionDepth == 3 ) {
173                 // subscription or tagged item selected
174                 newsDetailModel.htmlcontent = categorySubContentModel.get(currentPath[3]).content
175             }
176             else if( selectionDepth == 2 ) {
177                 // simply get the chosen news
178                 newsDetailModel.htmlcontent = categoryContentModel.get(currentPath[2]).content
179             }
180         }
181     }
182
183     Component.onCompleted: {
184         Storage.initialize()
185         tryLogin()
186     }
187 }