2df208e0453c766200894e576d80a6d4bb79418f
[feedingit] / src / qml / FeedingIt.qml
1 import Qt 4.7
2 import "common" as Common
3 // Depends on qt4-declarative-qmlviewer
4
5 Item {
6     width: 480
7     height: 640
8     id: screen
9
10     Rectangle {
11         id: container
12         /*anchors.fill: parent;*/ color: "#343434";
13         anchors.centerIn: parent
14         transformOrigin: Item.Center
15         property bool editMode: false
16
17         function categoryClicked(catid) {
18             feedsItem.catid = catid;
19             categoriesItem.isShown = false;
20             feedsItem.visible = true;
21         }
22
23         function feedClicked(feedid, updating) {
24             flipper.feedid = feedid;
25             toolBar.feedUpdating = updating;
26             flipper.visible = true;
27         }
28
29 //        function articleClicked(articleid, index) {
30 //            // Assign the articleId for the current, next and previous article to the associated variables
31 //            // Note the modulo, so it goes around
32 //            //articleDisplay.articleindex = modulo(index,articlesItem.count)
33 //            //articleDisplay.nextArticle = articlesItem.getArticleid(modulo(index+1,articlesItem.count))
34 //            //articleDisplay.prevArticle = articlesItem.getArticleid(modulo(index-1,articlesItem.count))
35 //            //articleDisplay.articleid = articleid
36 //            //flipper.model = articlesItem.articles
37 //            flipper.visible = true;
38 //        }
39
40         function backClicked() {
41             if (flipper.visible && flipper.articleShown) {
42                 // We're viewing an article, and going back to article listing
43                 flipper.articleShown = false;
44                 flipper.reload()
45                 //flipper.articleid = "";
46                 //flipper.value = 1;
47                 //articlesItem.reload()
48                 return;
49             }
50             if (flipper.visible) {
51                 feedsItem.reload();
52                 toolBar.feedUpdating = false;
53                 flipper.visible = false;
54                 flipper.feedid = "";
55                 return;
56             }
57
58 //            if (articlesItem.visible) {
59 //                // Viewing articles, going back to feeds
60 //                //articlesItem.feedid = "";
61 //                feedsItem.reload();
62 //                articlesItem.visible = false;
63 //                //articlesItem.reload();
64 //                return;
65 //            }
66             if (feedsItem.visible) {
67                 // Viewing feeds, going back to categories
68                 //feedsItem.catid = "";
69                 feedsItem.visible = false;
70                 //feedsItem.reload();
71                 categoriesItem.isShown = true;
72                 return;
73             }
74             if (!feedsItem.visible) {
75                 // Viewing categories, quitting
76                 Qt.quit();
77             }
78         }
79
80         function categoryDeleted(catid) {
81             confirmationMessage.catid=catid;
82             confirmationMessage.state="deleteCat";
83         }
84
85         function feedDeleted(catid, feedid) {
86             confirmationMessage.catid=catid;
87             confirmationMessage.feedid=feedid;
88             confirmationMessage.state="deleteFeed";
89         }
90
91         function addCategory(categoryName) {
92             var doc = new XMLHttpRequest();
93             var url = "http://localhost:8000/addCat/"+categoryName
94             doc.open("GET", url);
95             doc.send();
96             categoriesItem.reload();
97             addCat.visible=false;
98         }
99
100         function addFeed(catid, feedName, feedURL) {
101             var doc = new XMLHttpRequest();
102             var url = "http://localhost:8000/addFeed/"+catid + "/" + feedName + "?url=" + feedURL
103             doc.open("GET", url);
104             doc.send();
105             feedsItem.reload();
106             console.log(addFeedDialog.visible)
107             addFeedDialog.visible=false;
108             console.log(addFeedDialog.visible)
109         }
110
111         function updateClicked(feedid) {
112             var doc = new XMLHttpRequest();
113             var url = "http://localhost:8000/updateFeed/" + feedid
114             doc.open("GET", url);
115             doc.send();
116         }
117
118         function updateAllClicked() {
119             var doc = new XMLHttpRequest();
120             var url = "http://localhost:8000/updateAll"
121             doc.open("GET", url);
122             doc.send();
123         }
124
125         Common.Menu {
126             id: config
127             z: 5
128             property string hideReadFeeds : "False"
129             property string hideReadArticles : "False"
130
131             property bool isShown: false;
132
133             //width: parent.width; height: parent.height;
134
135             //height: 0
136             states: State {
137                 name: "shown"; when: config.isShown == true
138                 PropertyChanges { target: config; y: 66 }
139             }
140
141             transitions: Transition {
142                 NumberAnimation { properties: "y"; duration: 300; easing.type: "InOutQuad" }
143             }
144
145         }
146
147         Common.ConfirmationMessage {
148             id: confirmationMessage;
149             property string catid: "";
150             property string feedid: "";
151
152             function action() {
153                 if (state=="markAll") {
154                     flipper.markAllAsRead();
155                     state="hidden"
156                     feedsItem.reload()
157                     return;
158                 }
159                 if (state=="deleteCat") {
160                     var doc = new XMLHttpRequest();
161                     var url = "http://localhost:8000/deleteCat/"+catid
162                     doc.open("GET", url);
163                     doc.send();
164                     categoriesItem.reload();
165                     state="hidden";
166                     return;
167                 }
168                 if (state=="deleteFeed") {
169                     var doc = new XMLHttpRequest();
170                     var url = "http://localhost:8000/deleteFeed/"+catid+"/"+feedid
171                     doc.open("GET", url);
172                     doc.send();
173                     feedsItem.reload();
174                     state="hidden";
175                     return;
176                 }
177             }
178             visible: false
179             onOkClicked: action()
180             onCancelClicked: visible=false
181             state: "hidden"
182             states: [ State {name: "markAll";
183                     PropertyChanges { target: confirmationMessage; text: qsTr("Do you want to mark all items as read?") }
184                     PropertyChanges { target: confirmationMessage; visible: true; }
185
186                 }, State {name: "deleteCat";
187                     PropertyChanges { target: confirmationMessage; text: qsTr("Do you want to delete this category?") }
188                     PropertyChanges { target: confirmationMessage; visible: true; }
189                 }, State {name: "deleteFeed";
190                     PropertyChanges { target: confirmationMessage; text: qsTr("Do you want to delete this feed and all its articles?") }
191                     PropertyChanges { target: confirmationMessage; visible: true; }
192                 }, State {name: "hidden";
193                     PropertyChanges { target: confirmationMessage; visible: false; }
194                 }
195             ]
196
197         }
198
199         Common.ToolBar {
200             id: toolBar; z: 7
201             height: 66; anchors.top: parent.top; width: parent.width; opacity: 0.9
202             menuLabel: qsTr("Config"); backLabel: qsTr("Back")
203             nextLabel: qsTr("Next"); prevLabel: qsTr("Previous")
204             markAllLabel: qsTr("Mark All As Read"); zoomLabel: qsTr("Zoom")
205             taskSwitcherLabel: qsTr("Task Switch")
206             onMenuClicked: config.isShown = !config.isShown;
207             onBackClicked: container.backClicked()
208             onPrevClicked: flipper.prev();
209             onNextClicked: flipper.next();
210             onMarkAllClicked: {
211                 confirmationMessage.state = "markAll";
212             }
213             onZoomClicked: { flipper.zoomEnabled = !flipper.zoomEnabled; }
214             onTaskSwitcherClicked: {
215                 var doc = new XMLHttpRequest();
216                 var url = "http://localhost:8000/task"
217                 doc.open("GET", url);
218                 doc.send();
219             }
220             onAddClicked: {
221                 if (feedsItem.visible) {
222                     addFeedDialog.feedName="";
223                     addFeedDialog.catid = feedsItem.catid;
224                     addFeedDialog.visible = true;
225                     return;
226                 }
227                 if (categoriesItem.visible) {
228                     addCat.catName="";
229                     addCat.visible=true;
230                     return;
231                 }
232             }
233             onUpdateClicked: {
234                 if (flipper.visible) {
235                     toolBar.feedUpdating = true
236                     container.updateClicked(flipper.feedid);
237                 } else {
238                     container.updateAllClicked();
239                 }
240             }
241
242             states: [ State {
243                 name: "navButtons"; when: flipper.articleShown
244                 PropertyChanges { target: toolBar; nextVisible: !container.inPortrait; }
245                 PropertyChanges { target: toolBar; prevVisible: !container.inPortrait; }
246                 PropertyChanges { target: toolBar; zoomVisible: true; }
247                 PropertyChanges { target: toolBar; addVisible: false; }
248             },
249                 State {
250                     name: "feedButtons"; when: (flipper.visible)&&(!flipper.articleShown)
251                     PropertyChanges { target: toolBar; markAllVisible: true; }
252                     PropertyChanges { target: toolBar; addVisible: false; }
253                     PropertyChanges { target: toolBar; updateVisible: true; }
254                 },
255                 State {
256                     name: "quitButton"; when: (!feedsItem.visible)
257                     PropertyChanges { target: toolBar; quitVisible: true;}
258                     PropertyChanges { target: toolBar; backVisible: false; }
259                     PropertyChanges { target: toolBar; updateVisible: true; }
260                     //PropertyChanges { target: toolBar; addVisible: true; }
261                 }
262             ]
263         }
264
265         Item {
266             id: views
267             //x: 2;
268             //y:66;
269             width: parent.width // - 4
270             height: parent.height-toolBar.height;
271             anchors.top: toolBar.bottom; anchors.bottom: parent.bottom
272             y: toolBar.height;
273
274             Common.AddCat {
275                 visible: false;
276                 id: addCat
277                 width: parent.width;
278                 height: parent.height;
279                 z: 10;
280             }
281
282             Common.AddFeed {
283                 visible: false;
284                 id: addFeedDialog
285                 width: parent.width;
286                 height: parent.height;
287                 z: 10;
288             }
289
290             Timer {
291                 function checkUpdates() {
292                         if (categoriesItem.visible && !feedsItem.visible) {
293                             var doc = new XMLHttpRequest();
294                             var url = "http://localhost:8000/isUpdating/"
295                             doc.onreadystatechange = function() {
296                                 if (doc.readyState == XMLHttpRequest.DONE) {
297                                     var xmlDoc = doc.responseXML.documentElement;
298                                     //var els = xmlDoc.getElementsByTagName("updating");
299                                     var isUpdating = xmlDoc.firstChild.firstChild.nodeValue;
300
301                                     console.log(isUpdating);
302                                     if (isUpdating=="True") {
303                                             toolBar.feedUpdating = true;
304                                     } else {
305                                         if (toolBar.feedUpdating) {
306                                             // We changed from updating to not updating, so we reload the listing
307                                             toolBar.feedUpdating = false;
308                                             categoriesItem.reload();
309                                         }
310                                     }
311                                     var commands = xmlDoc.lastChild.childNodes;
312                                     for (var ii = 0; ii < commands.length; ++ii) {
313                                         // process the commands
314                                         var command = commands[ii].attributes[0].value; //("c")
315                                         console.log(command)
316                                         if (command=="openFeed") {
317                                             // Open feed feed
318                                             var catid = commands[ii].attributes[1].value;
319                                             var feedid = commands[ii].firstChild.nodeValue;
320                                             if (!flipper.visible) {
321                                                 container.categoryClicked(catid);
322                                                 container.feedClicked(feedid,false);
323                                                 console.log("feedid: " + feedid);
324                                             }
325                                         }
326                                         if (command=="openArticle") {
327                                             // Open feed and article
328                                             var feedid = commands[ii].attributes[2].value; //("key");
329                                             var articleid = commands[ii].firstChild.nodeValue;
330                                             if (!flipper.visible) {
331                                                 container.categoryClicked(catid);
332                                                 container.feedClicked(feedid,false);
333                                                 container.articleClicked(articleid, index)
334                                                 console.log("art: "+feedid+"/"+articleid);
335                                             }
336                                         }
337                                         if (command=="addFeed") {
338                                             // Open the addFeed dialog
339                                             var url = commands[ii].firstChild.nodeValue;
340                                             console.log("add: "+url)
341                                         }
342                                     }
343
344                                 }
345                             }
346                             doc.open("GET", url);
347                             doc.send();
348                             //categoriesItem.reload()
349                         }
350                         if (feedsItem.visible && !flipper.visible) {
351                             //feedsItem.reload()
352                         }
353                         if (flipper.visible) {
354                             var doc = new XMLHttpRequest();
355                             var url = "http://localhost:8000/isUpdating/" + flipper.feedid
356                             doc.onreadystatechange = function() {
357                                 if (doc.readyState == XMLHttpRequest.DONE) {
358                                     var a = doc.responseXML.documentElement;
359                                     console.log(a.firstChild.nodeValue);
360                                     if (a.firstChild.nodeValue=="True") {
361                                             toolBar.feedUpdating = true;
362                                     } else {
363                                         if (toolBar.feedUpdating) {
364                                             // We changed from updating to not updating, so we reload the listing
365                                             toolBar.feedUpdating = false;
366                                             flipper.reload();
367                                         }
368                                     }
369                                 }
370                             }
371                             doc.open("GET", url);
372                             doc.send();
373
374                             //flipper.reload()
375                         }
376                     }
377                 interval: 2000; running: true; repeat: true
378                 onTriggered: checkUpdates();
379             }
380
381             Categories {
382                 // Loads the categoryList view and delegate
383                 id: categoriesItem
384                 property bool isShown: true;
385                 inEditMode: container.editMode;
386
387                 states: State {
388                     name: "shown"; when: categoriesItem.isShown == false
389                     PropertyChanges { target: categoriesItem; x: -screen.width }
390                 }
391
392                 transitions: Transition {
393                     NumberAnimation { properties: "x"; duration: 300; easing.type: "InOutQuad" }
394                 }
395
396             }
397
398             Feeds {
399
400                 // Loads the feedList view and delegate
401                 id: feedsItem;
402                 property string hideReadFeeds: config.hideReadFeeds
403                 visible: false;
404                 inEditMode: container.editMode;
405
406                 states: [
407                     State { name: "articlesShown"; when: flipper.visible; PropertyChanges { target: feedsItem; x: -parent.width } },
408                     State { name: "shown"; when: feedsItem.visible; PropertyChanges { target: feedsItem; x: 0 } }
409                 ]
410
411                 transitions: Transition {
412                     NumberAnimation { properties: "x"; duration: 300; easing.type: "InOutQuad" }
413                 }
414
415             }
416
417             ArticleViewer {
418                 id: flipper
419                 visible: false;
420                 property string hideReadArticles: config.hideReadArticles
421                 property string feedid: ""
422                 x: parent.width
423
424                 states: State { name: "shown"; when: flipper.visible; PropertyChanges { target: flipper; x: 0 }
425                     }
426
427                 transitions: Transition {
428                     NumberAnimation { properties: "x"; duration: 300; easing.type: "InOutQuad" }
429                 }
430             }
431         }
432
433         property bool lockRotation: false
434         property variant selectedOrientation: Orientation.UnknownOrientation
435         property variant activeOrientation: selectedOrientation == Orientation.UnknownOrientation ? runtime.orientation : selectedOrientation
436         state: "orientation " + activeOrientation
437         property bool inPortrait: (activeOrientation == Orientation.Portrait || activeOrientation == Orientation.PortraitInverted);
438
439         // rotation correction for landscape devices like N900
440         property bool landscapeWindow: screen.width > screen.height
441         property variant rotationDelta: landscapeWindow ? -90 : 0
442         rotation: rotationDelta
443
444         // initial state is portrait
445         property real baseWidth: landscapeWindow ? screen.height : screen.width
446         property real baseHeight: landscapeWindow ? screen.width : screen.height
447
448         width: baseWidth
449         height: baseHeight
450
451         function getAngle(orientation) {
452             var angle;
453             if (orientation == Orientation.Portrait) {
454                 angle = 0;
455             } else if (orientation == Orientation.Landscape) {
456                 angle = 90;
457             } else if (orientation == Orientation.PortraitInverted) {
458                 angle = 180;
459             } else if (orientation == Orientation.LandscapeInverted) {
460                 angle = 270;
461             } else {
462                 angle = 0;
463             }
464             return angle;
465         }
466
467         states: [
468             State {
469                 name: "orientation " + Orientation.Landscape
470                 PropertyChanges {
471                     target: container
472                     rotation: getAngle(Orientation.Landscape)+rotationDelta
473                     width: baseHeight
474                     height: baseWidth
475                 }
476                 StateChangeScript { script: console.log(container.width +"/"+container.height) }
477             },
478             State {
479                 name: "orientation " + Orientation.PortraitInverted
480                 PropertyChanges {
481                     target: container
482                     rotation: getAngle(Orientation.PortraitInverted)+rotationDelta
483                     width: baseWidth
484                     height: baseHeight
485                 }
486                 StateChangeScript { script: console.log(container.width +"/"+container.height) }
487             },
488             State {
489                 name: "orientation " + Orientation.LandscapeInverted
490                 PropertyChanges {
491                     target: container
492                     rotation: getAngle(Orientation.LandscapeInverted)+rotationDelta
493                     width: baseHeight
494                     height: baseWidth
495                 }
496                 StateChangeScript { script: console.log(container.width +"/"+container.height) }
497             }
498         ]
499         transitions: Transition {
500             ParallelAnimation {
501                 RotationAnimation {
502                     direction: RotationAnimation.Shortest
503                     duration: 300
504                     easing.type: Easing.InOutQuint
505                 }
506                 NumberAnimation {
507                     properties: "x,y,width,height"
508                     duration: 300
509                     easing.type: Easing.InOutQuint
510                 }
511             }
512         }
513
514     }
515 }