Readability integration - first draft
[quicknewsreader] / qml / QuickNewsReader / content / view / NewsDetailFromURL.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 QtWebKit 1.0
21
22 Item {
23     id: newsDetailFromURLRect
24     width: window.width; height: window.height
25
26     function getNewsModelItem(prop)
27     {
28         if( typeof currentSource.listModels[componentDepth-1] != "undefined" )
29             if( typeof currentSource.listModels[componentDepth-1][prop] != "undefined" )
30                 return currentSource.listModels[componentDepth-1][prop]
31
32         return ""
33     }
34
35     property string detailURL: getNewsModelItem('urllink')
36     property string title: getNewsModelItem('title')
37     property string image: getNewsModelItem('image')
38
39     VisualItemModel {
40         id: itemModel
41
42         Column {
43             id: column
44             x: 10; y: 10
45             width: newsDetailFromURLRect.width - 20
46 //            height: newsDetailFromURLRect.height
47
48             Row {
49                 id: titleRow
50                 spacing: 5
51
52                 Image {
53                     id: detailImage
54                     source: image
55                 }
56
57                 Text {
58                     anchors.verticalCenter: titleRow.verticalCenter
59                     text: title; width: column.width - detailImage.width - 10; wrapMode: Text.WordWrap
60                     font { bold: true; family: "Helvetica"; pointSize: 16 }
61                 }
62             }
63
64             WebView {
65                 width: column.width
66                 id: newsDetailWebView
67                 url: detailURL
68                 preferredWidth: column.width
69                 settings.javascriptEnabled: true
70                 settings.autoLoadImages: false
71
72                 property bool firstLoad: true;
73
74                 javaScriptWindowObjects: QtObject {
75                     WebView.windowObjectName: "console"
76
77                     function log(message) {
78                         console.log(message);
79                     }
80                 }
81
82                 // on the maemo platform, the background is dark gray by default, so change it to white
83                 onLoadFinished: {
84                     newsDetailWebView.evaluateJavaScript("document.bgColor = '#FFFFFF';")
85
86                     if( firstLoad )
87                     {
88                         newsDetailWebView.evaluateJavaScript("readConvertLinksToFootnotes=false;readStyle='style-newspaper';readSize='size-medium';readMargin='margin-wide';_readability_script=document.createElement('script');_readability_script.type='text/javascript';_readability_script.src='file:///home/chris/dev/projects/QuickNewsReader/qml/QuickNewsReader/content/js/Readability.js?x='+(Math.random());document.documentElement.appendChild(_readability_script);_readability_css=document.createElement('link');_readability_css.rel='stylesheet';_readability_css.href='file:///home/chris/dev/projects/QuickNewsReader/qml/QuickNewsReader/content/css/readability.css';_readability_css.type='text/css';_readability_css.media='all';document.getElementsByTagName('head')[0].appendChild(_readability_css);_readability_print_css=document.createElement('link');_readability_print_css.rel='stylesheet';_readability_print_css.href='file:///home/chris/dev/projects/QuickNewsReader/qml/QuickNewsReader/content/css/readability-print.css';_readability_print_css.media='print';_readability_print_css.type='text/css';document.getElementsByTagName('head')[0].appendChild(_readability_print_css);")
89
90                         var currentHtml = newsDetailWebView.html;
91                         newsDetailWebView.html = currentHtml;
92
93                         firstLoad = false;
94                     }
95                 }
96
97                 onAlert: console.log(message);
98             }
99         }
100     }
101     ListView {
102         id: itemListView
103         anchors.fill: newsDetailFromURLRect
104         model: itemModel
105     }
106     ScrollBar { scrollArea: itemListView; height: itemListView.height; width: 8; anchors.right: itemListView.right }
107 }