-
Notifications
You must be signed in to change notification settings - Fork 23
/
browser.cpp
1387 lines (1129 loc) · 43.5 KB
/
browser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "browser.h"
int TITLE_LENGTH = 20;
int LIMIT_TAB = 8;
ustring app_name = "DBT Browser";
ustring remove_http_string(ustring address) {
string r = address;
//~ cout << "full address: " << r << endl;
if (address.find("http://www.") == 0) {
r.erase(0, 11);
//~ cout << "after trim: " << r << endl;
}
else if (address.find("https://www.") == 0) {
r.erase(0, 12);
//~ cout << "after trim: " << r << endl;
}
else if (address.find("http://") == 0) {
r.erase(0, 7);
//~ cout << "after trim: " << r << endl;
}
else if (address.find("https://") == 0) {
r.erase(0, 8);
//~ cout << "after trim: " << r << endl;
}
// xoa dau / cuoi string
if (r.back() == '/') {
r.pop_back();
//~ cout << "after trim: " << r << endl;
}
return r;
}
void find_and_replace(ustring& content, const string& search, const string& replace) {
auto pos = content.find(search);
while(pos != std::string::npos) {
content.replace(pos, search.length(), replace);
pos = content.find(search, pos);
}
}
Browser::~Browser() {
}
static gboolean webview_decide_policy(WebKitWebView *webview, WebKitPolicyDecision *decision,
WebKitPolicyDecisionType type, void* thisclass) {
//~ Browser* browser = (Browser*)thisclass;
switch (type) {
case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION: {
//~ cout << "WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION" << endl;
auto navigation_decision = WEBKIT_NAVIGATION_POLICY_DECISION(decision);
auto navigation_action = webkit_navigation_policy_decision_get_navigation_action(navigation_decision);
int modifiers = webkit_navigation_action_get_modifiers(navigation_action);
// get request uri va tao new tab
auto request = webkit_navigation_action_get_request(navigation_action);
auto address = webkit_uri_request_get_uri(request);
if (address) {
Browser* browser = (Browser*)thisclass;
// control click
if (modifiers == GDK_CONTROL_MASK) {
webkit_policy_decision_ignore(decision); // bo request link hien tai
browser->create_new_tab(address, false); // ko switch to last tab
}
}
break;
}
case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
//~ webkit_policy_decision_ignore(decision);
//~ cout << "WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION" << endl;
break;
case WEBKIT_POLICY_DECISION_TYPE_RESPONSE: {
//~ cout << "WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION" << endl;
//~ WebKitResponsePolicyDecision* response = WEBKIT_RESPONSE_POLICY_DECISION(decision);
//~ auto request = webkit_response_policy_decision_get_request(response);
//~ auto address = webkit_uri_request_get_uri(request);
//~ if (address) {
//~ // cout << "uri response: " << address << "\n";
//~ Browser* browser = (Browser*)thisclass;
//~ if (browser->checkBlackListAddress(address)) {
//~ // cout << "is in black list" << "\n";
//~ webkit_policy_decision_ignore(decision);
//~ }
//~ }
// not use
//~ if(!webkit_response_policy_decision_is_mime_type_supported(response)) {
//~ webkit_policy_decision_ignore(decision);
//~ }
break;
}
default:
//~ cout << "WEBKIT_POLICY_DEFAULT" << endl;
//~ cout << "DEFAULT WEBKIT POLICY" << endl;
return false;
}
return true;
}
bool Browser::checkBlackListAddress(std::string address) {
return blacklist.isInBlackList(address);
}
static GtkWidget* webview_create(WebKitWebView* webview, WebKitNavigationAction *navigation_action, void* thisclass) {
//~ auto type = webkit_navigation_action_get_navigation_type(navigation_action);
//~ cout << "create type ==== ::: " << type << endl;
auto request = webkit_navigation_action_get_request(navigation_action);
const gchar* address = webkit_uri_request_get_uri(request);
if (address) {
Browser* browser = (Browser*)thisclass;
// CHEAT: tam thoi cheat cho nay ko cho link vao mot so site gay crash va ads
if (browser->checkBlackListAddress(address)) {
return NULL;
}
browser->create_new_tab(address, false); // false: dont switch to last tab
}
return NULL;
}
static void webview_load_change (WebKitWebView* webview, WebKitLoadEvent load_event, void* thisclass) {
switch (load_event) {
case WEBKIT_LOAD_STARTED: {
//~ cout << "WEBKIT_LOAD_STARTED" << endl;
// load address
const gchar* address = webkit_web_view_get_uri(webview);
if (address) {
// cout << "address: " << address << endl;
Browser* browser = (Browser*)thisclass;
// CHEAT: tam thoi cheat cho nay ko cho link vao mot so site gay crash va ads
if (browser->checkBlackListAddress(address)) {
return;
}
auto current_tab_webview = browser->get_webview_from_current_tab();
// set address bar va header title neu dang o tab do
if (webview == current_tab_webview) {
browser->set_title(app_name + " - " + address);
browser->set_address(address);
browser->check_address_bookmark(address);
}
// set duong dan address cho title tab vi luc nay title chua co hoac con giu title cua webview hien tai
ustring tab_label_text = "↻ " + remove_http_string(address);
browser->set_tab_label(webview, tab_label_text);
}
break;
}
case WEBKIT_LOAD_REDIRECTED:
// cout << "WEBKIT_LOAD_REDIRECTED" << endl;
break;
case WEBKIT_LOAD_COMMITTED: {
// cout << "WEBKIT_LOAD_COMMITTED" << endl;
break;
}
case WEBKIT_LOAD_FINISHED: {
//~ cout << "WEBKIT_LOAD_FINISHED" << endl;
// phai gan gchar* cho nay vi neu de ustring crash
const gchar* title = webkit_web_view_get_title(webview);
const gchar* address = webkit_web_view_get_uri(webview);
if (title && *title && address) {
//~ cout << "title: " << title << " -- " << address << endl;
Browser* browser = (Browser*)thisclass;
browser->set_tab_label(webview, title);
browser->save_history(title, address);
}
break;
}
}
}
static void webview_load_fail(WebKitWebView* webview, WebKitLoadEvent load_event, void* thisclass) {
switch (load_event) {
case WEBKIT_LOAD_STARTED: {
// cout << "FAILED WEBKIT_LOAD_STARTED" << endl;
break;
}
case WEBKIT_LOAD_REDIRECTED:
// cout << "FAILED WEBKIT_LOAD_REDIRECTED" << endl;
break;
case WEBKIT_LOAD_COMMITTED: {
// cout << "FAILED WEBKIT_LOAD_COMMITTED" << endl;
//~ webkit_web_view_stop_loading(webview);
break;
}
case WEBKIT_LOAD_FINISHED: {
// cout << "FAILED WEBKIT_LOAD_FINISHED" << endl;
//~ webkit_web_view_stop_loading(webview);
break;
}
}
}
static gboolean webview_enter_fullscreen (WebKitWebView* web_view, void* thisclass) {
Browser* browser = (Browser*)thisclass;
browser->make_fullscreen();
return false;
}
static gboolean webview_leave_fullscreen (WebKitWebView* web_view, void* thisclass) {
Browser* browser = (Browser*)thisclass;
browser->make_unfullscreen();
return false;
}
void Browser::make_fullscreen() {
fullscreen();
menu_box.hide();
notebook.set_show_tabs(false);
browser_box_side_bar.hide();
showing_side_bar = false;
is_fullscreen = true;
}
void Browser::make_unfullscreen() {
//~ cout << "make_unfullscreen" << endl;
unfullscreen();
menu_box.show();
notebook.set_show_tabs(true);
is_fullscreen = false;
}
WebKitWebView* Browser::get_webview_from_current_tab() {
int tab_index = notebook.get_current_page();
Widget* widget = notebook.get_nth_page(tab_index);
WebKitWebView* webview = (WebKitWebView*)Glib::unwrap(widget);
return webview;
}
WebKitWebContext* Browser::get_web_context(bool is_private) {
if (is_private) {
//~ cout << "return private " << endl;
//~ return web_context_private;
return web_context; // tam thoi chua xu ly
}
else {
// tam thoi bo khong su nhieu web context
//~ cout << "return context " << switch_context << endl;
if (switch_context == 0) {
switch_context++;
return web_context;
}
else {
switch_context = 0;
//~ return web_context_second;
return web_context; // tam thoi chi co 1 context
}
}
}
WebKitWebView* Browser::create_new_webview() {
//~ bool is_private = false;
auto web_context = get_web_context();
WebKitWebView* webview = WEBKIT_WEB_VIEW(webkit_web_view_new_with_context(web_context));
g_signal_connect(webview, "load-changed", G_CALLBACK(webview_load_change), this);
g_signal_connect(webview, "load-failed", G_CALLBACK(webview_load_fail), this);
g_signal_connect(webview, "create", G_CALLBACK(webview_create), this);
g_signal_connect(webview, "decide-policy", G_CALLBACK(webview_decide_policy), this);
g_signal_connect(webview, "enter-fullscreen", G_CALLBACK(webview_enter_fullscreen), this);
g_signal_connect(webview, "leave-fullscreen", G_CALLBACK(webview_leave_fullscreen), this);
return webview;
}
void Browser::create_new_tab(ustring address="", bool switch_to_last_tab = true) {
if (notebook.get_n_pages() > LIMIT_TAB) return;
set_title(app_name); // luon set header title cho app
auto webview = create_new_webview();
auto widget = manage(Glib::wrap(GTK_WIDGET(webview)));
notebook.append_page(*widget, "New Tab");
notebook.set_tab_reorderable(*widget, true);
notebook.show_all();
if (switch_to_last_tab)
jump_to_last_tab();
// load site va set address bar
if (!address.empty()) {
address_bar.set_text(address);
webkit_web_view_load_uri(webview, address.c_str());
}
// note:
// Widget* widget2 = notebook.get_nth_page(0);
// webview == (WebKitWebView*)Glib::unwrap(widget2)
}
void Browser::clone_current_tab(bool switch_to_last_tab = true) {
if (notebook.get_n_pages() > LIMIT_TAB) return;
auto current_webview = get_webview_from_current_tab();
auto webview = create_new_webview();
auto widget = manage(Glib::wrap(GTK_WIDGET(webview)));
//~ webviews.push_back(webview);
// nhu tao tab
const gchar* title = webkit_web_view_get_title(current_webview); // neu ko de gtitle ma ustring, se crash
if (title) {
notebook.append_page(*widget, title);
notebook.set_tab_reorderable(*widget, true);
notebook.show_all();
set_tab_label(*widget, title);
if(switch_to_last_tab) jump_to_last_tab();
}
const gchar* address = webkit_web_view_get_uri(current_webview);
if (address) {
address_bar.set_text(address); // set lai location bar
webkit_web_view_load_uri(webview, address);
}
}
void Browser::on_search_next() {
auto webview = get_webview_from_current_tab();
auto controller = webkit_web_view_get_find_controller(webview);
if(webkit_find_controller_get_search_text(controller))
webkit_find_controller_search_next(controller);
}
void Browser::on_search_previous() {
auto webview = get_webview_from_current_tab();
auto controller = webkit_web_view_get_find_controller(webview);
if(webkit_find_controller_get_search_text(controller))
webkit_find_controller_search_previous(controller);
}
void Browser::on_search_finish() {
auto webview = get_webview_from_current_tab();
auto controller = webkit_web_view_get_find_controller(webview);
webkit_find_controller_search_finish(controller);
}
void Browser::on_search_text() {
auto text = search_box_text.get_text();
if (text.empty()) return;
auto webview = get_webview_from_current_tab();
auto controller = webkit_web_view_get_find_controller(webview);
int match_count = 200;
webkit_find_controller_search (controller, text.c_str(), WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE, match_count);
//WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE WEBKIT_FIND_OPTIONS_NONE
}
/* BUTTON FUNCTION */
void Browser::on_next() {
//~ cout << "on_next" << endl;
// lay web hien cua tab hien tai va stop loading neu web view dang chay
auto webview = get_webview_from_current_tab();
webkit_web_view_stop_loading(webview);
webkit_web_view_go_forward(webview);
auto title = webkit_web_view_get_title(webview);
if(title)
set_title(app_name + " - " + title);
}
void Browser::on_back() {
//~ cout << "on_previous" << endl;
// lay web hien cua tab hien tai va stop loading neu web view dang chay
auto webview = get_webview_from_current_tab();
webkit_web_view_stop_loading(webview);
webkit_web_view_go_back(webview);
auto title = webkit_web_view_get_title(webview);
if(title)
set_title(app_name + " - " + title);
}
// tam thoi khong su dung
bool Browser::load_search_website(ustring address, WebKitWebView* webview) {
if (address == "") return false;
is_none_tab = false;
// thay + = %2b
find_and_replace(address, "+", "%2B");
find_and_replace(address, "#", "%23");
find_and_replace(address, " ", "+");
find_and_replace(address, ":", "%3A");
// load google search
ustring uri = "https://www.google.com/search?q=";
//~ ustring uri = "https://www.bing.com/search?q=";
uri.append(address);
webkit_web_view_load_uri(webview, uri.c_str());
return true;
}
bool Browser::load_website(ustring address, WebKitWebView* webview) {
if (address == "") return false;
is_none_tab = false;
// them http:// neu khong tim thay
//~ if (address.find("http") == string::npos) { address = "http://" + address; }
if (address.find("http") != 0) { address = "http://" + address; }
webkit_web_view_load_uri(webview, address.c_str());
return true;
}
void Browser::on_notebook_switch_page(Gtk::Widget* widget, guint tab_index)
{
//~ cout << " switch to tab : " << tab_index << endl;
WebKitWebView* webview = (WebKitWebView*)Glib::unwrap(widget);
const gchar* title = webkit_web_view_get_title(webview);
const gchar* address = webkit_web_view_get_uri(webview);
if (address) {
// kiem tra va hien bookmark
check_address_bookmark(address);
address_bar.set_text(address);
}
else {
save_bookmark_button.set_label("♡");
address_bar.set_text("");
}
if (title && *title) {
//~ cout << "switch page get title: " << title << endl;
set_title(app_name + " - " + title); // set lai title
set_tab_label(*widget, title);
}
else {
// title ko co thi set address thanh tab label
if (address) {
remove_http_string(address);
set_title(app_name + " - " + address);
set_tab_label(*widget, address);
}
}
}
void Browser::on_sidebook_switch_page(Gtk::Widget* widget, guint tab_index)
{
//~ cout << " switch to tab : " << tab_index << endl;
if(tab_index == 2) {
// close
showing_side_bar = false;
browser_box_side_bar.hide();
}
}
void Browser::jump_to_last_tab() {
int last_tab = notebook.get_n_pages() - 1;
//~ cout << "jump to tab: " << last_tab << endl;
notebook.set_current_page(last_tab);
address_bar.grab_focus(); // focus to address bar
}
void Browser::toogle_search_box() {
if (showing_search_box) {
search_box.hide();
showing_search_box = false;
on_search_finish(); // ket thuc tim kiem
}
else {
search_box.show();
showing_search_box = true;
}
}
enum { HISTORY, BOOKMARK };
void Browser::toogle_side_bar(int tab_index) {
if (showing_side_bar) {
int current_tab = sidebook.get_current_page();
// neu dang mo tab hien tai thi dong lai
if(current_tab == tab_index) {
browser_box_side_bar.hide();
showing_side_bar = false;
}
//con neu dang mo tab khac thi switch tro lai tab_index nay
else {
sidebook.set_current_page(tab_index);
}
}
else {
browser_box_side_bar.show();
sidebook.set_current_page(tab_index);
showing_side_bar = true;
// chi rieng history moi scroll len top vi nguoi dung bao gio cung muon xem history moi nhat
if (tab_index == 0) {
history_tree.get_selection()->unselect_all(); // unselect tree
history_scroll.get_vadjustment()->set_value(0); // scroll to top
}
else {
// bookmark tree thi van phai unselect
bookmark_tree.get_selection()->unselect_all(); // unselect tree
}
}
// xoa bo list address da chon trong history/bookmark tree
list_address_selected.clear();
}
void Browser::delete_current_tab() {
auto webview = get_webview_from_current_tab();
// web context
auto context = get_web_context(webview);
if (context == web_context) { switch_context = 0; }
else { switch_context = 1; }
//
webkit_web_view_stop_loading(webview); // stop loading webview hien tai neu dang chay
int tab_index = notebook.get_current_page();
notebook.remove_page(tab_index);
//~ webviews.erase(webviews.begin()+tab_index);
// khong con tab nao thi tao tab moi
if(notebook.get_n_pages() == 0) { create_new_tab(); is_none_tab = true; }
}
bool Browser::on_key_press(GdkEventKey* event)
{
if(is_fullscreen) return false;
switch(event->keyval) {
case GDK_KEY_t:
if (event->state & GDK_CONTROL_MASK ) {
create_new_tab();
}
break;
case GDK_KEY_f:
if (event->state & GDK_CONTROL_MASK) {
toogle_search_box();
search_box_text.grab_focus();
}
break;
case GDK_KEY_w:
if (event->state & GDK_CONTROL_MASK) {
delete_current_tab();
}
break;
default:
return false;
}
return true;
}
bool Browser::on_key_release(GdkEventKey* event)
{
//~ GdkModifierType modifiers = gtk_accelerator_get_default_mod_mask();
//~ else if((event->state & modifiers) == GDK_CONTROL_MASK) { }
if (!is_fullscreen) {
if (event->keyval == GDK_KEY_F2) {
create_new_tab();
}
// delete tab
else if (event->keyval == GDK_KEY_F3) {
delete_current_tab();
}
else if (event->keyval == GDK_KEY_F5) {
on_reload_site();
}
else if (event->keyval == GDK_KEY_F6) {
clone_current_tab();
}
else if (event->keyval == GDK_KEY_F7) {
toogle_side_bar(HISTORY);
}
else if (event->keyval == GDK_KEY_F8) {
// open bookmark
toogle_side_bar(BOOKMARK);
}
else if (event->keyval == GDK_KEY_Delete) {
// delete history, bookmark
if (history_tree.is_focus()) delete_history();
else if (bookmark_tree.is_focus()) delete_bookmark();
}
}
else {
if (event->keyval == GDK_KEY_F11) {
if (is_fullscreen) make_unfullscreen();
else make_fullscreen();
}
}
return true;
}
// press enter on entry
void Browser::on_enter_address() {
ustring address = address_bar.get_text();
if (address == "") return;
set_title(app_name);
// lay web hien cua tab hien tai va stop loading neu web view dang chay
auto webview = get_webview_from_current_tab();
webkit_web_view_stop_loading(webview); // stop webview hien tai neu dang loading
// check address is valid url using regex
std::string regex_str = R"(^((http[s]?|ftp)://)?\/?([^/\.]+\.)*?([^/\.]+\.[^:/\s\.]{2,3}(\.[^:/\s\.]{2,3})?)(:\d+)?($|/)([^#?\s]+)?(.*?)?(#[\w\-]+)?$)";
std::string regex_address = address;
regex regex_url(regex_str);
smatch base_match;
// kiem tra day co phai la website ko
if(regex_match(regex_address, base_match, regex_url)) {
//~ cout << "load valid address: " << address << endl;
// CHEAT: tam thoi cheat cho nay ko cho link vao mot so site gay crash va ads
if (checkBlackListAddress(address)) {
return;
}
load_website(address, webview);
}
// ko phai website: chuyen sang tim kiem hoac kiem tra localhost, local file
else {
// check day co phai la local host khong, cho phep load truc tiep
if (address.find("localhost") == 0 || address.find("http://localhost") == 0 || address.find("https://localhost") == 0
|| address.find("127.0.0.1") == 0 || address.find("http://127.0.0.1") == 0 || address.find("https://127.0.0.1") == 0)
{
load_website(address, webview); // valid url
}
// check day co phai la local file ko
else if(address.find("file://") == 0) {
webkit_web_view_load_uri(webview, address.c_str());
}
// check day co phai la local file ko
else if(address.at(0) == '/') {
string file = "file://" + address;
webkit_web_view_load_uri(webview, file.c_str());
}
// co khoang trang, tuc la tim kiem
else if(address.find(" ") == 0) {
load_search_website(address, webview); // invalid url : chuyen qua tim kiem
}
else {
//~ cout << "search invalid address: " << address << endl;
load_search_website(address, webview); // invalid url : chuyen qua tim kiem
}
}
}
void Browser::set_address(ustring name) {
address_bar.set_text(""); // clear text
address_bar.set_text(name);
}
void Browser::set_tab_label(Widget& widget, ustring name) {
ustring resize_name = name;
resize_name.resize(TITLE_LENGTH);
//~ cout << "set tab label: " << name << endl;
notebook.set_tab_label_text(widget, resize_name);
}
void Browser::set_tab_label(WebKitWebView* webview, ustring name) {
ustring resize_name = name;
resize_name.resize(TITLE_LENGTH);
//~ cout << "set tab label: " << name << endl;
Widget* widget = manage(Glib::wrap(GTK_WIDGET(webview)));
notebook.set_tab_label_text(*widget, resize_name);
}
int Browser::get_current_tab() {
int result = notebook.get_current_page();
if (result < 0) result = 0;
return result;
}
/* HISTORY, BOOKMARK */
void Browser::save_history(ustring title, ustring address) {
//~ cout << "save history: " << title << " - " << address << endl;
// neu co history trong list thi cap nhat so lan su dung
// nguoc lai insert vao database va list
char* query = sqlite3_mprintf("INSERT INTO history(address, title, up) values ('%q', '%q', '%d');",
address.c_str(), title.c_str(), 0);
int error = run_sql(ustring(query), NULL);
if (error == SQLITE_OK) {
//~ cout << "insert history success " << address << endl;
// them address vao list
load_history(title, address);
// them vao history tree
add_history_tree(title, address, true);
add_address_store(title, address); // them vao address store de search khi go text
}
else {
add_history_tree(title, address, true); // them len top khoi can xoa trong tree
// cap nhat so lan su dung va title lan nua, tam thoi chua can su dung
//~ query = sqlite3_mprintf( "UPDATE history SET up = up + 1, title='%q' WHERE address = '%q' ;", title.c_str(), address.c_str());
//~ error = run_sql(ustring(query), NULL);
//~ if (error == SQLITE_OK) {
//~ // cout << "update success history " << endl;
//~ }
//~ // cap nhat lai history tree
//~ auto children = history_store->children();
//~ for(auto& iter : children) {
//~ auto row = *iter;
//~ if(row[tree_model.column_address] == address) {
//~ history_store->erase(iter); // remove khoi dong hien tai
//~ add_history_tree(title, address); // them len top
//~ break;
//~ }
//~ }
}
}
void Browser::save_bookmark(ustring title, ustring address) {
if (title.empty() && address.empty()) return;
//~ cout << "save book: " << title << " - " << address << endl;
// save to db
// insert address
char* query = sqlite3_mprintf("INSERT INTO bookmark(address, title) values ('%q', '%q');",
address.c_str(), title.c_str());
int error = run_sql(ustring(query), NULL);
sqlite3_free(query);
// insert thanh cong
if (error == SQLITE_OK) {
// add to list
//~ tuple<ustring, ustring> tmp(title, address);
//~ list_bookmark.push_front(tmp);
// std::map, address lam key chinh nen add address truoc
list_bookmark.insert(std::pair<ustring, ustring>(address, title));
// them vao bookmark tree
add_bookmark_tree(title, address, true);
save_bookmark_button.set_label("♥");
add_address_store(title, address); // them vao address store de search khi go text
}
}
void Browser::on_save_bookmark() {
int tab_index = notebook.get_current_page();
Widget* widget = notebook.get_nth_page(tab_index);
WebKitWebView* webview = (WebKitWebView*)Glib::unwrap(widget);
const gchar* title = webkit_web_view_get_title(webview);
const gchar* address = webkit_web_view_get_uri(webview);
if (title && address) {
save_bookmark(title, address);
}
}
void Browser::on_show_bookmark() {
toogle_side_bar(BOOKMARK);
}
void Browser::check_address_bookmark(ustring address) {
if (address.empty()) {
save_bookmark_button.set_label("♡");
return;
}
//~ cout << "check address bookmark" << address << endl;
//~ for(auto& x: list_bookmark) {
//~ ustring tuple_address = get<1>(x);
//~ if (address == tuple_address) {
//~ // cout << "we bookmarked this address " << address << endl;
//~ save_bookmark_button.set_label("♥");
//~ return;
//~ }
//~ }
for(auto it = list_bookmark.cbegin(); it != list_bookmark.cend();) {
if (it->first.compare(address) == 0) {
save_bookmark_button.set_label("♥");
return;
}
else
++it;
}
//~ cout << "still not bookmark: " << address << endl;
save_bookmark_button.set_label("♡");
}
void Browser::on_show_history() {
toogle_side_bar(HISTORY);
}
/* DB */
void Browser::load_history(ustring title, ustring address) {
//~ tuple<ustring, ustring> tmp(title, address);
//~ list_history.push_front(tmp);
}
void Browser::load_bookmark(ustring title, ustring address) {
list_bookmark.insert(std::pair<ustring, ustring>(address, title));
}
void Browser::add_history_tree(ustring title, ustring address, bool prepend = false) {
TreeModel::iterator iter;
if(prepend) iter = history_store->prepend();
else iter = history_store->append();
TreeModel::Row row = *iter;
row[tree_model.column_title] = title;
row[tree_model.column_address] = address;
}
void Browser::add_bookmark_tree(ustring title, ustring address, bool prepend = false) {
//~ TreeModel::iterator iter = bookmark_store->append();
TreeModel::iterator iter;
if(prepend) iter = bookmark_store->prepend();
else iter = bookmark_store->append();
TreeModel::Row row = *iter;
row[tree_model.column_title] = title;
row[tree_model.column_address] = address;
}
void Browser::add_address_store(ustring title, ustring address) {
//~ cout << " add addresss store : " << title << " - " << address << endl;
//~ TreeModel::iterator iter = address_store->prepend();
//~ TreeModel::Row row = *iter;
//~ row[address_model_column.column_name] = title;
//~ iter = address_store->prepend(row.children());
//~ TreeModel::Row childrow = *iter;
//~ childrow[address_model_column.column_name] = address;
//~ TreeModel::iterator iter = address_store->prepend();
//~ TreeModel::Row row = *iter;
//~ row[address_model_column.column_name] = title;
//~ row[address_model_column.column_address] = address;
}
static int load_all_bookmark(void* thisclass, int argc, char** argv, char** col) {
Browser* browser = (Browser*)thisclass;
ustring title, address;
// iterator through all data, index = 0: address, index = 1: title
for(int i = 0; i < argc; i++) {
if (i == 0) address = ustring(argv[i]);
else if (i == 1) title = ustring(argv[i]);
}
// them vao danh sach bookmark de su dung
if (title != "" && address != "") {
// cout << "load history: " << title << " ::: " << address << endl;
browser->load_bookmark(title, address);
browser->add_bookmark_tree(title, address);
browser->add_address_store(title, address); // them vao address store de search khi go text
}
return 0;
}
static int load_all_history(void* thisclass, int argc, char** argv, char** col) {
Browser* browser = (Browser*)thisclass;
ustring title, address;
// iterator through all data, index = 0: address, index = 1: title
for(int i = 0; i < argc; i++) {
if (i == 0) address = ustring(argv[i]);
else if (i == 1) title = ustring(argv[i]);
}
// them vao danh sach history de su dung
if (title != "" && address != "") {
// cout << "load history: " << title << " ::: " << address << endl;
browser->load_history(title, address);
browser->add_history_tree(title, address);
browser->add_address_store(title, address); // them vao address store de search khi go text
}
return 0;
}
int Browser::run_sql(ustring sql, int(*func)(void*, int, char**, char**)) {
ustring db_path = exe_path + "/data.db";
sqlite3 *db;
int error = sqlite3_open(db_path.c_str(), &db);
if (error) {
//~ cout << "Can't open database: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return error;
}
char* errmsg = 0;
error = sqlite3_exec(db, sql.c_str(), func, this, &errmsg);
if (error != SQLITE_OK) {
//~ cout << "SQL Error: " << errmsg << endl;
sqlite3_free(errmsg);
}
sqlite3_close(db);
return error;
}
void Browser::on_bookmark_tree_double_click(const TreeModel::Path& path, TreeViewColumn* column) {
TreeModel::iterator iter = bookmark_store->get_iter(path);
if (iter) {
TreeModel::Row row = *iter;
ustring address = row[tree_model.column_address];
// neu chi moi chi co 1 tab thi load len tab do luon
if (is_none_tab) {
is_none_tab = false;
if(address.find("file://") == 0) {
webkit_web_view_load_uri(get_webview_from_current_tab(), address.c_str());
}
// check day co phai la local file ko
else if(address.at(0) == '/') {
string file = "file://" + address;
webkit_web_view_load_uri(get_webview_from_current_tab(), file.c_str());
}
else {
load_website(address, get_webview_from_current_tab());
}
}
else create_new_tab(address);
}
}
void Browser::on_history_tree_double_click(const TreeModel::Path& path, TreeViewColumn* column) {
TreeModel::iterator iter = history_store->get_iter(path);
if (iter) {
TreeModel::Row row = *iter;
ustring address = row[tree_model.column_address];
// neu chi moi chi co 1 tab thi load len tab do luon
if (is_none_tab) {
is_none_tab = false;
if(address.find("file://") == 0) {
webkit_web_view_load_uri(get_webview_from_current_tab(), address.c_str());
}
// check day co phai la local file ko
else if(address.at(0) == '/') {
string file = "file://" + address;
webkit_web_view_load_uri(get_webview_from_current_tab(), file.c_str());
}
else {
load_website(address, get_webview_from_current_tab());