~bookworm-team/bookworm/trunk

« back to all changes in this revision

Viewing changes to src/EPubWidget.vala

  • Committer: Raphael Isemann
  • Date: 2013-10-08 14:34:14 UTC
  • Revision ID: teemperor@googlemail.com-20131008143414-mqhijg2yv0nvnrb8
restructured the codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using Gtk;
 
2
using EPub;
 
3
 
 
4
namespace BookWorm
 
5
{
 
6
 
 
7
        class EPubWidget : EBookWidget {
 
8
 
 
9
                private WebKit.WebView text_view;
 
10
                private ScrolledWindow scrolled_window;
 
11
                private Gee.HashMap<string, string> contents;
 
12
                private List<string> toc;
 
13
                private int pageCount = 0;
 
14
                private string booktitle;
 
15
                private int pageno = 0;
 
16
 
 
17
                private int indexNum = 0;
 
18
                private List<int> pageCountSection;
 
19
                private int totalPageCount;
 
20
                private int totalPageNumber;
 
21
 
 
22
                private bool twoPages = false;
 
23
                private bool calculatingTotalPageCount = false;
 
24
                private int calcProgress = 0;
 
25
 
 
26
 
 
27
                public EPubWidget() {
 
28
                        text_view = new WebKit.WebView ();
 
29
                        text_view.expand = true;
 
30
                        scrolled_window = new ScrolledWindow (null, null);
 
31
                        //scrolled_window.set_policy (PolicyType.NEVER, PolicyType.NEVER);
 
32
                        scrolled_window.add (this.text_view);
 
33
                        scrolled_window.expand = true;
 
34
                        //h_scrollBar = (Scrollbar) scrolled_window.get_hscrollbar();
 
35
                        text_view.notify["load-status"].connect (webview_status_changed);
 
36
                        attach(scrolled_window,0,0,1,1);
 
37
                }
 
38
 
 
39
                public override int get_current_page () {
 
40
                        return pageno;
 
41
                }
 
42
 
 
43
                public override int get_total_pages () {
 
44
                        return totalPageCount;
 
45
                }
 
46
 
 
47
                public override bool page_counter_available () {
 
48
                        return true;
 
49
                }
 
50
 
 
51
                public override void next_page () {
 
52
                        pageno++;
 
53
                        totalPageNumber++;
 
54
                        if((pageCount - 1) < pageno) {
 
55
                                pageno = 0;
 
56
                                indexNum++;
 
57
                                text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
 
58
                        }
 
59
 
 
60
                        update_page();
 
61
                }
 
62
 
 
63
                public override void previous_page () {
 
64
                        pageno--;
 
65
                        totalPageNumber--;
 
66
                        if(pageno < 0) {
 
67
                        pageno = 0;
 
68
                        indexNum--;
 
69
                                text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
 
70
                        }
 
71
 
 
72
                        update_page();
 
73
                }
 
74
 
 
75
                private void update_page() {
 
76
 
 
77
                        //set_title(booktitle + " " + (totalPageNumber + 1).to_string() + " of " + totalPageCount.to_string());
 
78
 
 
79
                        string height = (text_view.get_allocated_height() - 10).to_string();
 
80
                        string width = text_view.get_allocated_width().to_string();
 
81
 
 
82
                        int offset = text_view.get_allocated_width() * pageno;
 
83
 
 
84
                        if(twoPages) {
 
85
                        text_view.execute_script(@"
 
86
 
 
87
                                totalHeight = content.offsetHeight;
 
88
                                pageCount = Math.floor(totalHeight/$height) + 1;
 
89
                                pageCount = pageCount * 2;
 
90
                                content.style.width = $width * pageCount / 2;
 
91
                                content.style.height = $height;
 
92
                                content.style.right = $offset;
 
93
                                content.style.WebkitColumnCount =  pageCount;
 
94
                                ");
 
95
                        } else {
 
96
                                text_view.execute_script(@"
 
97
 
 
98
                                totalHeight = content.offsetHeight;
 
99
                                pageCount = Math.floor(totalHeight/$height) + 1;
 
100
                                pageCount = pageCount;
 
101
                                content.style.width = $width * pageCount;
 
102
                                content.style.height = $height;
 
103
                                content.style.right = $offset;
 
104
                                content.style.WebkitColumnCount =  pageCount;
 
105
                                ");
 
106
                        }
 
107
                }
 
108
 
 
109
 
 
110
                private void webview_status_changed (Object? source, ParamSpec pspec) {
 
111
                        int height = (text_view.get_allocated_height() - 10);
 
112
                        string strHeight = height.to_string();
 
113
                        string width = text_view.get_allocated_width().to_string();
 
114
                        int min;
 
115
                        int natural;
 
116
 
 
117
                        text_view.get_preferred_height(out min, out natural);
 
118
                        string totalHeight = min.to_string();
 
119
 
 
120
                         pageCount = (int) Math.floor(min/height) + 1;
 
121
                        string strPageCount = pageCount.to_string();
 
122
                        switch (text_view.load_status) {
 
123
                                case WebKit.LoadStatus.FINISHED:
 
124
 
 
125
                                        double animationSpeed = 0.5;
 
126
                                        if(calculatingTotalPageCount) {
 
127
                                                animationSpeed = 0;
 
128
                                        }
 
129
                                        string js = @"
 
130
                                        var desiredHeight = $height;
 
131
                                        var desiredWidth = $width;
 
132
 
 
133
                                        var bodyID = document.getElementsByTagName('body')[0];
 
134
 
 
135
                                        org_html = bodyID.innerHTML;
 
136
                                        new_html = \"<div id = 'container'><div id='content'>\"  +
 
137
                                        org_html + '</div></div>';
 
138
                                        bodyID.innerHTML = new_html;
 
139
 
 
140
                                        var container = document.getElementsByTagName('div')[0];
 
141
                                        var content = document.getElementsByTagName('div')[1];
 
142
 
 
143
                                        totalHeight = $totalHeight;
 
144
                                        pageCount = Math.floor(totalHeight/desiredHeight) + 1;
 
145
                                        content.style.padding = 0;
 
146
                                        content.style.width = desiredWidth * $strPageCount;
 
147
                                        content.style.height = $strHeight;
 
148
                                        content.style.WebkitColumnCount =  $strPageCount;
 
149
                                        content.style.right = 0;
 
150
                                        content.style.position = 'relative';
 
151
 
 
152
                                        content.style['-webkit-transition-duration'] = '$animationSpeed s';
 
153
                                        content.style.overflow = 'visible';
 
154
 
 
155
 
 
156
                                        container.style.height = '100%';
 
157
                                        container.style.width = '100%';
 
158
 
 
159
                                        container.style.overflow = 'hidden';";
 
160
 
 
161
                                        text_view.execute_script(js);
 
162
                                        update_page();
 
163
                                        if(calculatingTotalPageCount) {
 
164
 
 
165
                                                pageCountSection.append(pageCount);
 
166
                                                totalPageCount += pageCount;
 
167
                                                calcProgress++;
 
168
                                                if(calcProgress < toc.length()) {
 
169
                                                        text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
 
170
                                                } else {
 
171
                                                        calculatingTotalPageCount = false;
 
172
                                                        text_view.load_html_string(contents[toc.nth_data(0)], "file://");
 
173
                                                }
 
174
                                        }
 
175
                                        break;
 
176
 
 
177
                        }
 
178
 
 
179
                }
 
180
 
 
181
                private void calculateTotalPageCount() {
 
182
 
 
183
                        calculatingTotalPageCount = true;
 
184
 
 
185
                        text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
 
186
 
 
187
                }
 
188
 
 
189
                public override void open_book(string filename) {
 
190
                        EPub.Context ebook = new EPub.Context(filename, 3);
 
191
 
 
192
                        booktitle = ebook.get_metadata(EPub.Metadata.TITLE)[0];
 
193
                        //set_title(booktitle);
 
194
 
 
195
                        //show reader view
 
196
                        this.scrolled_window.show ();
 
197
 
 
198
 
 
199
                        //get table of contents from epub
 
200
                        toc = new List<string> ();
 
201
 
 
202
                        EPub.TIterator it = new TIterator (ebook, EPub.TIteratorType.NAVMAP, 3);
 
203
 
 
204
 
 
205
                        if (it == null) {
 
206
                                it = new TIterator(ebook, EPub.TIteratorType.GUIDE, 3);
 
207
                        }
 
208
 
 
209
                        while (it != null && it.next () == 1) {
 
210
 
 
211
                                toc.append (it.get_curr_link ());
 
212
 
 
213
                        }
 
214
 
 
215
                        //if table of contents turns out to be empty, we generate one
 
216
                        bool generatetoc = (toc.length () == 0 );
 
217
 
 
218
 
 
219
                        //get contents of epub
 
220
                        EPub.Iterator I = new Iterator (ebook, EPub.IteratorType.SPINE, 3);
 
221
                        contents = new Gee.HashMap<string, string> ();
 
222
                        string next = "";
 
223
 
 
224
                        do {
 
225
                                if (generatetoc) {
 
226
                                toc.append (I.get_curr_url());
 
227
                                }
 
228
 
 
229
                                contents.set (I.get_curr_url (), next);
 
230
                                next = I.get_next ();
 
231
 
 
232
                        } while (next != null);
 
233
 
 
234
                        //print out table of contents for debugging purposes
 
235
                        foreach (string key in toc) {
 
236
                                                stdout.printf ("keys %s\n", key);
 
237
                                }
 
238
 
 
239
                        calculateTotalPageCount();
 
240
 
 
241
                        //load the first set of pages from table of contents
 
242
                        text_view.load_html_string (contents[toc.nth_data(0)], "file://");
 
243
                }
 
244
 
 
245
                public override string get_title () {
 
246
                        return booktitle;
 
247
                }
 
248
        }
 
249
 
 
250
 
 
251
 
 
252
}