~bookworm-team/bookworm/trunk

« back to all changes in this revision

Viewing changes to src/BookWormWindow.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:
7
7
        class BookWormWindow : Window
8
8
        {
9
9
                private Granite.Widgets.Welcome welcome;
10
 
                private WebKit.WebView text_view;
11
 
                private ScrolledWindow scrolled_window;
12
 
                private Button nextButton;
13
 
                private Button backButton;
14
 
                private int pageno = 0;
 
10
                private Button next_button;
 
11
                private Button back_button;
15
12
                private Grid controls;
16
13
 
17
 
                private Gee.HashMap<string, string> contents;
18
 
                private List<string> toc;
19
 
                private int pageCount = 0;
20
 
                private string booktitle;
21
 
 
22
 
                private List<int> pageCountSection;
23
 
                private int totalPageCount;
24
 
                private int totalPageNumber;
 
14
                private EBookWidget ebook_widget;
25
15
 
26
16
                private Gtk.Label page_counter_label;
27
17
 
31
21
                                "Open a book to start reading.");
32
22
                        welcome.expand = true;
33
23
 
 
24
                        ebook_widget = new EPubWidget();
 
25
 
34
26
                        // Basic UI
35
27
                        var content_grid = new Gtk.Grid ();
36
28
                        content_grid.expand = true;
48
40
                        white_bg.parse("rgb(255,255,255)");
49
41
                        override_background_color (StateFlags.NORMAL, white_bg);
50
42
 
51
 
                        nextButton = new Button ();
 
43
                        next_button = new Button ();
52
44
                        Gtk.Image next_img = new Gtk.Image.from_icon_name ("go-next-symbolic",
53
45
                                                Gtk.IconSize.BUTTON);
54
 
                        nextButton.set_image(next_img);
 
46
                        next_button.set_image(next_img);
55
47
 
56
 
                        backButton = new Button();
 
48
                        back_button = new Button();
57
49
                        Gtk.Image back_img = new Gtk.Image.from_icon_name ("go-previous-symbolic",
58
50
                                                Gtk.IconSize.BUTTON);
59
 
                        backButton.set_image(back_img);
 
51
                        back_button.set_image(back_img);
60
52
 
61
53
                        set_title ("BookWorm");
62
54
                        set_size_request (640, 480);
63
 
                        text_view = new WebKit.WebView ();
64
 
                        text_view.expand = true;
65
 
                        scrolled_window = new ScrolledWindow (null, null);
66
 
                        //scrolled_window.set_policy (PolicyType.NEVER, PolicyType.NEVER);
67
 
                        scrolled_window.add (this.text_view);
68
 
                        scrolled_window.expand = true;
69
 
                        //h_scrollBar = (Scrollbar) scrolled_window.get_hscrollbar();
70
 
                        text_view.notify["load-status"].connect (webview_status_changed);
 
55
 
 
56
                        controls = new Grid ();
71
57
 
72
58
                        controls.hexpand = true;
73
59
                        content_grid.attach (welcome, 0, 1, 1, 1);
74
 
                        content_grid.attach (scrolled_window, 1, 1, 1, 1);
75
 
 
76
 
 
77
 
                        controls = new Grid ();
78
 
                        controls.attach (backButton, 0, 0, 1, 1);
 
60
                        content_grid.attach (ebook_widget, 1, 1, 1, 1);
 
61
 
 
62
 
 
63
 
 
64
                        controls.attach (back_button, 0, 0, 1, 1);
79
65
                        var spacer1 = new Gtk.Grid();
80
66
                        spacer1.hexpand = true;
81
67
                        controls.attach (spacer1, 1, 0, 1, 1);
88
74
                        controls.attach (spacer2, 3, 0, 1, 1);
89
75
 
90
76
 
91
 
                        controls.attach (nextButton, 4, 0, 1, 1);
 
77
                        controls.attach (next_button, 4, 0, 1, 1);
92
78
 
93
79
                        content_grid.attach (controls, 0, 2, 2, 1);
94
80
 
95
81
                        content_grid.show_all ();
96
 
                        scrolled_window.show_all ();
97
 
                        scrolled_window.hide ();
 
82
                        ebook_widget.show_all ();
 
83
                        ebook_widget.hide ();
98
84
                        controls.hide ();
99
85
                        welcome.append( Gtk.Stock.OPEN, "Open Book", "Open a Book");
100
86
                        welcome.set_sensitive (true);
101
87
                        welcome.show_all ();
102
88
 
103
89
                        welcome.activated.connect (on_welcome_activated);
104
 
                        nextButton.clicked.connect (on_next_clicked);
105
 
                        backButton.clicked.connect (on_back_clicked);
106
 
 
 
90
                        next_button.clicked.connect (on_next_clicked);
 
91
                        back_button.clicked.connect (on_back_clicked);
 
92
                        back_button.sensitive = false;
 
93
                        update_page_counter_label ();
107
94
                }
108
95
 
109
96
                /**
115
102
                        }
116
103
                }
117
104
 
118
 
                int indexNum = 0;
119
 
                private void on_next_clicked() {
120
 
                        pageno++;
121
 
                        totalPageNumber++;
122
 
                        if((pageCount - 1) < pageno) {
123
 
                                pageno = 0;
124
 
                                indexNum++;
125
 
                                text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
126
 
                        }
127
 
 
128
 
                        update_page();
129
 
                }
130
 
 
131
 
                private void on_back_clicked() {
132
 
                        pageno--;
133
 
                        totalPageNumber--;
134
 
                        if(pageno < 0) {
135
 
                        pageno = 0;
136
 
                        indexNum--;
137
 
                                text_view.load_html_string(contents[toc.nth_data(indexNum)], "file://");
138
 
                        }
139
 
 
140
 
                        update_page();
141
 
                }
142
 
 
143
 
                bool twoPages = false;
144
 
 
145
 
                private void update_page() {
146
 
 
147
 
                        set_title(booktitle + " " + (totalPageNumber + 1).to_string() + " of " + totalPageCount.to_string());
148
 
 
149
 
                        string height = (text_view.get_allocated_height() - 10).to_string();
150
 
                        string width = text_view.get_allocated_width().to_string();
151
 
 
152
 
                        int offset = text_view.get_allocated_width() * pageno;
153
 
 
154
 
                        if(twoPages) {
155
 
                        text_view.execute_script(@"
156
 
 
157
 
                                totalHeight = content.offsetHeight;
158
 
                                pageCount = Math.floor(totalHeight/$height) + 1;
159
 
                                pageCount = pageCount * 2;
160
 
                                content.style.width = $width * pageCount / 2;
161
 
                                content.style.height = $height;
162
 
                                content.style.right = $offset;
163
 
                                content.style.WebkitColumnCount =  pageCount;
164
 
                                ");
165
 
                        } else {
166
 
                                text_view.execute_script(@"
167
 
 
168
 
                                totalHeight = content.offsetHeight;
169
 
                                pageCount = Math.floor(totalHeight/$height) + 1;
170
 
                                pageCount = pageCount;
171
 
                                content.style.width = $width * pageCount;
172
 
                                content.style.height = $height;
173
 
                                content.style.right = $offset;
174
 
                                content.style.WebkitColumnCount =  pageCount;
175
 
                                ");
176
 
                        }
177
 
 
178
 
                        page_counter_label.set_label (pageno.to_string () +
179
 
                                                "/" + totalPageCount.to_string ());
180
 
                }
181
 
 
182
 
 
183
 
                private void webview_status_changed (Object? source, ParamSpec pspec) {
184
 
                        int height = (text_view.get_allocated_height() - 10);
185
 
                        string strHeight = height.to_string();
186
 
                        string width = text_view.get_allocated_width().to_string();
187
 
                        int min;
188
 
                        int natural;
189
 
 
190
 
                        text_view.get_preferred_height(out min, out natural);
191
 
                        string totalHeight = min.to_string();
192
 
 
193
 
                         pageCount = (int) Math.floor(min/height) + 1;
194
 
                        string strPageCount = pageCount.to_string();
195
 
                        switch (text_view.load_status) {
196
 
                                case WebKit.LoadStatus.FINISHED:
197
 
 
198
 
                                        double animationSpeed = 0.5;
199
 
                                        if(calculatingTotalPageCount) {
200
 
                                                animationSpeed = 0;
201
 
                                        }
202
 
                                        string js = @"
203
 
                                        var desiredHeight = $height;
204
 
                                        var desiredWidth = $width;
205
 
 
206
 
                                        var bodyID = document.getElementsByTagName('body')[0];
207
 
 
208
 
                                        org_html = bodyID.innerHTML;
209
 
                                        new_html = \"<div id = 'container'><div id='content'>\"  +
210
 
                                        org_html + '</div></div>';
211
 
                                        bodyID.innerHTML = new_html;
212
 
 
213
 
                                        var container = document.getElementsByTagName('div')[0];
214
 
                                        var content = document.getElementsByTagName('div')[1];
215
 
 
216
 
                                        totalHeight = $totalHeight;
217
 
                                        pageCount = Math.floor(totalHeight/desiredHeight) + 1;
218
 
                                        content.style.padding = 0;
219
 
                                        content.style.width = desiredWidth * $strPageCount;
220
 
                                        content.style.height = $strHeight;
221
 
                                        content.style.WebkitColumnCount =  $strPageCount;
222
 
                                        content.style.right = 0;
223
 
                                        content.style.position = 'relative';
224
 
 
225
 
                                        content.style['-webkit-transition-duration'] = '$animationSpeed s';
226
 
                                        content.style.overflow = 'visible';
227
 
 
228
 
 
229
 
                                        container.style.height = '100%';
230
 
                                        container.style.width = '100%';
231
 
 
232
 
                                        container.style.overflow = 'hidden';";
233
 
 
234
 
                                        text_view.execute_script(js);
235
 
                                        update_page();
236
 
                                        if(calculatingTotalPageCount) {
237
 
 
238
 
                                                pageCountSection.append(pageCount);
239
 
                                                totalPageCount += pageCount;
240
 
                                                calcProgress++;
241
 
                                                if(calcProgress < toc.length()) {
242
 
                                                        text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
243
 
                                                } else {
244
 
                                                        calculatingTotalPageCount = false;
245
 
                                                        text_view.load_html_string(contents[toc.nth_data(0)], "file://");
246
 
                                                }
247
 
                                        }
248
 
                                        break;
249
 
 
250
 
                        }
251
 
 
252
 
                }
253
 
 
254
 
 
 
105
                private void on_next_clicked () {
 
106
                        ebook_widget.next_page ();
 
107
                        check_nav_buttons ();
 
108
                        update_page_counter_label ();
 
109
                }
 
110
 
 
111
                private void on_back_clicked () {
 
112
                        ebook_widget.previous_page ();
 
113
                        check_nav_buttons ();
 
114
                        update_page_counter_label ();
 
115
                }
 
116
 
 
117
                private void check_nav_buttons () {
 
118
                        if(ebook_widget.page_counter_available ()) {
 
119
                                back_button.sensitive = (ebook_widget.get_current_page () != 0);
 
120
                                next_button.sensitive = (ebook_widget.get_current_page ()
 
121
                                                != ebook_widget.get_total_pages ());
 
122
                                }
 
123
                }
 
124
 
 
125
                private void update_page_counter_label () {
 
126
                        if(ebook_widget.page_counter_available ())
 
127
                                page_counter_label.set_label (
 
128
                                                (ebook_widget.get_current_page ()+1).to_string () +
 
129
                                                "/" + ebook_widget.get_total_pages ().to_string ());
 
130
                }
255
131
 
256
132
                /**
257
133
                 * Set and call the file chooser dialog.
270
146
                        filechooser.destroy ();
271
147
 
272
148
                        if (filename != "") {
273
 
                                open_epub (filename);
274
 
                        }
275
 
 
276
 
 
277
 
                }
278
 
 
279
 
                bool calculatingTotalPageCount = false;
280
 
                int calcProgress = 0;
281
 
 
282
 
                private void calculateTotalPageCount() {
283
 
 
284
 
                        calculatingTotalPageCount = true;
285
 
 
286
 
                        text_view.load_html_string(contents[toc.nth_data(calcProgress)], "file://");
287
 
 
288
 
                }
289
 
 
290
 
                private void open_epub(string filename) {
291
 
 
292
 
                        EPub.Context ebook = new EPub.Context(filename, 3);
293
 
 
294
 
                        booktitle = ebook.get_metadata(EPub.Metadata.TITLE)[0];
295
 
                        set_title(booktitle);
296
 
 
297
 
                        //show reader view
298
 
                        this.scrolled_window.show ();
299
 
                        this.welcome.hide ();
300
 
                        controls.show ();
301
 
 
302
 
 
303
 
                        //get table of contents from epub
304
 
                        toc = new List<string> ();
305
 
 
306
 
                        EPub.TIterator it = new TIterator (ebook, EPub.TIteratorType.NAVMAP, 3);
307
 
 
308
 
 
309
 
                        if (it == null) {
310
 
                                it = new TIterator(ebook, EPub.TIteratorType.GUIDE, 3);
311
 
                        }
312
 
 
313
 
                        while (it != null && it.next () == 1) {
314
 
 
315
 
                                toc.append (it.get_curr_link ());
316
 
 
317
 
                        }
318
 
 
319
 
                        //if table of contents turns out to be empty, we generate one
320
 
                        bool generatetoc = (toc.length () == 0 );
321
 
 
322
 
 
323
 
                        //get contents of epub
324
 
                        EPub.Iterator I = new Iterator (ebook, EPub.IteratorType.SPINE, 3);
325
 
                        contents = new Gee.HashMap<string, string> ();
326
 
                        string next = "";
327
 
 
328
 
                        do {
329
 
 
330
 
                                if (generatetoc) {
331
 
                                toc.append (I.get_curr_url());
332
 
                                }
333
 
 
334
 
                                contents.set (I.get_curr_url (), next);
335
 
                                next = I.get_next ();
336
 
 
337
 
                        }
338
 
                        while (next != null);
339
 
 
340
 
                        //print out table of contents for debugging purposes
341
 
                        foreach (string key in toc) {
342
 
                                                stdout.printf ("keys %s\n", key);
343
 
                                }
344
 
 
345
 
 
346
 
                        calculateTotalPageCount();
347
 
 
348
 
                        //load the first set of pages from table of contents
349
 
                        text_view.load_html_string (contents[toc.nth_data(0)], "file://");
 
149
                                ebook_widget.open_book (filename);
 
150
                                ebook_widget.show_all();
 
151
                                welcome.hide ();
 
152
                                controls.show ();
 
153
                                update_page_counter_label ();
 
154
                        }
 
155
 
350
156
 
351
157
                }
352
158