1d9f7c1bb467c1b4a8b254f55bbafadda188b67d
[quicknewsreader] / qml / QuickNewsReader / content / modelimpl / GoogleReaderSourceModel.qml
1 import QtQuick 1.0
2 import "../modelitf"
3 import "../js/SettingsStorage.js" as Storage
4
5 SourceModel {
6     id: googleReaderModel
7     name: "Google Reader"
8
9     listViews: [
10         { viewComponent: 'content/view/Categories.qml' },
11         { viewComponent: 'content/view/News.qml' }
12     ]
13     listModels: [
14         categoriesModel,
15         categoryContentModel
16     ]
17
18     property variant categoriesModel: GoogleReaderCategories { }
19     property variant categoryContentModel: GoogleReaderNews { sourceDepth: 2 }
20     property variant categorySubContentModel: GoogleReaderNews { sourceDepth: 3 }
21     property variant newsDetailModel: QtObject {
22         property int sourceDepth: 3
23
24         property string htmlcontent: ""
25         property string title: ""
26         property string image: ""
27     }
28     property variant sid;
29     property variant sidToken;
30
31     loading: false
32     hasSettings: true
33     settingsComponent: "GoogleReaderConfig.qml"
34     function storeConfiguration(configUI) {
35         // save the values in the database
36         Storage.setSetting("GoogleReader.login", configUI.loginValue)
37         Storage.setSetting("GoogleReader.password", configUI.passwordValue)
38
39         tryLogin()
40     }
41     function loadConfiguration(configUI) {
42         // retrieve the values from the database
43         configUI.loginValue = Storage.getSetting("GoogleReader.login")
44         configUI.passwordValue = Storage.getSetting("GoogleReader.password")
45     }
46
47     property variant googleReaderLoginWorker: WorkerScript {
48              id: googleReaderLoginWorker
49              source: "../js/GoogleReaderAPI.js"
50
51              onMessage: {
52                  sid = messageObject.sid
53                  sidToken = messageObject.sidToken
54
55                  loading = false
56              }
57          }
58
59     property variant googleReaderLoadCategoryWorker: WorkerScript {
60              id: googleReaderLoadCategoryWorker
61              source: "../js/GoogleReaderAPI.js"
62
63              onMessage: {
64                  categoryContentModel.append({ 'title': messageObject.title, 'description': messageObject.published, 'image': '', 'id': messageObject.id })
65
66                  loading = false
67              }
68          }
69     property variant googleReaderLoadSubscriptionOrTagWorker: WorkerScript {
70              id: googleReaderLoadSubscriptionOrTagWorker
71              source: "../js/GoogleReaderAPI.js"
72
73              onMessage: {
74                  categorySubContentModel.append({ 'title': messageObject.title, 'description': messageObject.published, 'image': '', 'content': messageObject.description })
75
76                  loading = false
77              }
78          }
79     property variant googleReaderLoadItemWorker: WorkerScript {
80              id: googleReaderLoadItemWorker
81              source: "../js/GoogleReaderAPI.js"
82
83              onMessage: {
84                  newsDetailModel.htmlcontent = messageObject.newsContent
85
86                  loading = false
87              }
88          }
89
90     function tryLogin() {
91         var loginValue = Storage.getSetting("GoogleReader.login")
92         var passwordValue = Storage.getSetting("GoogleReader.password")
93
94         loading = true
95
96         googleReaderLoginWorker.sendMessage({
97                                            'action': 'login',
98                                            'email': loginValue,
99                                            'password': passwordValue
100                                        })
101     }
102     onCurrentPathChanged: {
103         // build the right model. currentPath[1] => category
104         var selectionDepth = 0;
105         while(typeof currentPath[selectionDepth+1] !== "undefined")
106             selectionDepth++;
107
108         if( typeof currentPath[1] !== "undefined" ) {
109             if( selectionDepth == 1 ) {
110                 // the category has been selected, so fill in the right content
111                 categoryContentModel.clear()
112
113                 // reshape the views and the models to fit the chosen path
114                 var newsDetailIndex = 2;
115                 var tmpListModels = listModels
116                 var tmpListViews = listViews
117                 if( currentPath[1] === 3 || currentPath[1] === 4 )
118                 {
119                     tmpListModels[2] = categorySubContentModel;
120                     tmpListViews[2] = { viewComponent: 'content/view/News.qml' }
121                     newsDetailIndex = 3;
122                 }
123                 tmpListModels[newsDetailIndex] = newsDetailModel;
124                 tmpListViews[newsDetailIndex] = { viewComponent: 'content/view/NewsDetail.qml' };
125                 tmpListModels[newsDetailIndex+1] = null
126                 tmpListViews[newsDetailIndex+1] = null
127                 listModels = tmpListModels
128                 listViews = tmpListViews
129
130                 googleReaderLoadCategoryWorker.sendMessage({
131                                                             'action': 'getCategoryContent',
132                                                             'sid': sid, 'sidToken': sidToken,
133                                                             'category': currentPath[1]
134                                                         })
135             }
136             else if( selectionDepth == 2 && currentPath[1] === 3 ) {
137                 // subscriptions selected
138                 categorySubContentModel.clear()
139                 googleReaderLoadSubscriptionOrTagWorker.sendMessage({
140                                                             'action': 'getSubscriptionItems',
141                                                             'sid': sid, 'sidToken': sidToken,
142                                                             'subscription': categoryContentModel.get(currentPath[2]).id
143                                                         })
144             }
145             else if( selectionDepth == 2 && currentPath[1] === 4 ) {
146                 // tags selected
147                 categorySubContentModel.clear()
148                 googleReaderLoadSubscriptionOrTagWorker.sendMessage({
149                                                             'action': 'getTaggedItems',
150                                                             'sid': sid, 'sidToken': sidToken,
151                                                             'tag': categoryContentModel.get(currentPath[2]).id
152                                                         })
153             }
154             else if( selectionDepth == 3 ) {
155                 // subscription or tagged item selected
156                 newsDetailModel.htmlcontent = categorySubContentModel.get(currentPath[3]).content
157             }
158             else if( selectionDepth == 2 ) {
159                 // simply get the chosen news
160                 newsDetailModel.htmlcontent = categoryContentModel.get(currentPath[2]).content
161             }
162         }
163     }
164
165     Component.onCompleted: {
166         Storage.initialize()
167         tryLogin()
168     }
169 }