Subversion Repositories Kolibri OS

Rev

Rev 9602 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9089 leency 1
//Copyright 2007-2021 by Veliant & Leency
6978 leency 2
//Asper, lev, Lrz, Barsuk, Nable, hidnplayr...
3067 leency 3
 
9259 leency 4
//BUGS
5
//if maximize a window on image load => crash
6
//issues with a long line
7
//add proxy settings
8
 
8332 leency 9
//===================================================//
10
//                                                   //
11
//                       LIB                         //
12
//                                                   //
13
//===================================================//
14
 
9296 leency 15
#define MEMSIZE 1024 * 160
5499 leency 16
#include "..\lib\gui.h"
4508 leency 17
#include "..\lib\draw_buf.h"
18
#include "..\lib\list_box.h"
19
#include "..\lib\cursor.h"
5978 leency 20
#include "..\lib\collection.h"
6795 leency 21
#include "..\lib\random.h"
7037 leency 22
#include "..\lib\clipboard.h"
5981 leency 23
 
5499 leency 24
#include "..\lib\obj\box_lib.h"
7049 leency 25
#include "..\lib\obj\libimg.h"
5499 leency 26
#include "..\lib\obj\http.h"
5690 leency 27
#include "..\lib\obj\iconv.h"
7748 leency 28
#include "..\lib\obj\proc_lib.h"
8330 leency 29
#include "..\lib\obj\netcode.h"
7757 leency 30
 
5978 leency 31
#include "..\lib\patterns\history.h"
7748 leency 32
#include "..\lib\patterns\simple_open_dialog.h"
7759 leency 33
#include "..\lib\patterns\toolbar_button.h"
7914 leency 34
#include "..\lib\patterns\restart_process.h"
5408 leency 35
 
8336 leency 36
#include "const.h"
37
#include "cache.h"
38
#include "show_src.h"
39
 
8332 leency 40
//===================================================//
41
//                                                   //
42
//                       DATA                        //
43
//                                                   //
44
//===================================================//
7757 leency 45
bool debug_mode = false;
8396 leency 46
bool show_images = true;
9293 leency 47
bool source_mode = false;
9348 pavelyakov 48
bool application_mode = false;
7771 leency 49
 
8016 leency 50
_history history;
51
 
7970 leency 52
enum { TARGET_SAME_TAB, TARGET_NEW_WINDOW, TARGET_NEW_TAB };
53
 
8330 leency 54
#include "TWB\TWB.c" //HTML Parser, a core component
7757 leency 55
 
7759 leency 56
TWebBrowser WB1;
7758 leency 57
 
7771 leency 58
#include "history.h"
7752 leency 59
 
9348 pavelyakov 60
dword PADDING = 9;
61
dword TSZE = 25;
62
dword STATUSBAR_H = 15;
63
dword TAB_H = 20;
64
dword TOOLBAR_H = 0;
4677 leency 65
 
7759 leency 66
_http http = 0;
7758 leency 67
 
8330 leency 68
progress_bar prbar;
7750 leency 69
proc_info Form;
7748 leency 70
 
8336 leency 71
#include "tabs.h"
72
 
73
dword cur_img_url;
74
dword shared_url;
8445 leency 75
dword http_get_type=PAGE;
8365 leency 76
dword render_start_time;
7780 leency 77
int menu_id=NULL;
78
 
9585 vitalkrilo 79
char default_dir[] = "/sys";
7788 leency 80
od_filter filter2 = { 22, "TXT\0HTM\0HTML\0DOCX\0\0" };
7748 leency 81
 
7755 leency 82
char editURL[URL_SIZE+1];
9348 pavelyakov 83
edit_box omnibox_edit = {, 0, 0, 0xffffff,
7758 leency 84
	0x94AECE, 0xffffff, 0xffffff,0x10000000,URL_SIZE-2,#editURL,0,,19,19};
4534 leency 85
 
8332 leency 86
//===================================================//
87
//                                                   //
88
//                       CODE                        //
89
//                                                   //
90
//===================================================//
91
 
7748 leency 92
void LoadLibraries()
93
{
8330 leency 94
	load_dll(boxlib,      #box_lib_init,0);
95
	load_dll(libimg,      #libimg_init,1);
96
	load_dll(libHTTP,     #http_lib_init,1);
97
	load_dll(iconv_lib,   #iconv_open,0);
98
	load_dll(netcode_lib, #base64_encode,0);
99
	load_dll(Proc_lib,    #OpenDialog_init,0);
7748 leency 100
	OpenDialog_init stdcall (#o_dialog);
101
}
102
 
103
void HandleParam()
104
{
8278 leency 105
	if (!param) {
8336 leency 106
		history.add(DEFAULT_URL);
8278 leency 107
	} else {
108
		if (!strncmp(#param, "-source ", 8)) {
7750 leency 109
			source_mode = true;
7765 leency 110
			history.add(#param + 8);
7936 leency 111
		} else if (!strncmp(#param, "-new ", 5)) {
112
			history.add(#param + 5);
9348 pavelyakov 113
		} else if (!strncmp(#param, "-app ", 5)) {
114
          	history.add(#param + 5);
115
          	application_mode = true;
116
        } else {
7914 leency 117
			if (GetProcessesCount("WEBVIEW") == 1) {
118
				history.add(#param);
119
			} else {
120
				shared_url = memopen(#webview_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
121
				strncpy(shared_url, #param, URL_SIZE);
122
				ExitProcess();
123
			}
7748 leency 124
		}
125
	}
7914 leency 126
	shared_url = memopen(#webview_shared, URL_SIZE+1, SHM_CREATE + SHM_WRITE);
7920 leency 127
	ESDWORD[shared_url] = '\0';
7748 leency 128
}
129
 
3067 leency 130
void main()
131
{
8332 leency 132
	int redirect_count=0;
9348 pavelyakov 133
 
134
	TOOLBAR_H = PADDING+TSZE+PADDING+2;
135
 
7748 leency 136
	LoadLibraries();
9348 pavelyakov 137
    HandleParam();
138
 
139
	if (application_mode) {
140
	    TOOLBAR_H = 0;
141
	    PADDING = 0;
142
        TSZE = 0;
143
        STATUSBAR_H = 0;
144
        TAB_H = 0;
145
	}
146
 
147
	omnibox_edit.left = PADDING+TSZE*2+PADDING+6;
148
	omnibox_edit.top = PADDING+3;
149
 
6045 leency 150
	WB1.list.SetFont(8, 14, 10011000b);
5779 leency 151
	WB1.list.no_selection = true;
7759 leency 152
	WB1.custom_encoding = -1;
8332 leency 153
	@SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
7914 leency 154
	loop() switch(@WaitEventTimeout(30))
6978 leency 155
	{
156
		case evMouse:
8332 leency 157
			edit_box_mouse stdcall (#omnibox_edit);
6978 leency 158
			mouse.get();
7970 leency 159
 
7282 leency 160
			if (WB1.list.MouseScroll(mouse.vert)) WB1.DrawPage();
7970 leency 161
 
8490 leency 162
			if (WB1.list.count > WB1.list.visible) {
163
				scrollbar_v_mouse (#scroll_wv);
9259 leency 164
				if (WB1.list.first != scroll_wv.position) {
8490 leency 165
					WB1.list.first = scroll_wv.position;
166
					WB1.DrawPage();
167
					break;
168
				}
6978 leency 169
			}
7970 leency 170
 
8500 leency 171
			if (WB1.list.MouseOver(mouse.x, mouse.y)) && (links.hover(WB1.list.y, WB1.list.first))
7758 leency 172
			{
8330 leency 173
				if (mouse.key&MOUSE_MIDDLE) && (mouse.up) {
8500 leency 174
					GetKeyModifier();
7970 leency 175
					if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
176
						EventClickLink(TARGET_NEW_WINDOW);
177
					} else {
178
						EventClickLink(TARGET_NEW_TAB);
179
					}
180
				}
8330 leency 181
				if (mouse.key&MOUSE_LEFT) && (mouse.up) {
7970 leency 182
					CursorPointer.Restore();
183
					EventClickLink(TARGET_SAME_TAB);
184
				}
8330 leency 185
				if (mouse.key&MOUSE_RIGHT) && (mouse.up) {
7970 leency 186
					CursorPointer.Restore();
187
					EventShowLinkMenu();
188
				}
189
			} else {
190
				CursorPointer.Restore();
8330 leency 191
				if (mouse.key&MOUSE_RIGHT) && (mouse.up) && (WB1.list.MouseOver(mouse.x, mouse.y)) {
7970 leency 192
					EventShowPageMenu();
193
				}
7758 leency 194
			}
6978 leency 195
			break;
5711 leency 196
 
6978 leency 197
		case evButton:
8332 leency 198
			ProcessButtonClick( @GetButtonID() );
6978 leency 199
			break;
5711 leency 200
 
6978 leency 201
		case evKey:
8332 leency 202
			@GetKeys();
203
			edit_box_key stdcall(#omnibox_edit);
204
			ProcessKeyEvent();
6978 leency 205
			break;
206
 
207
		case evReDraw:
8490 leency 208
			DefineAndDrawWindow(GetScreenWidth()-WIN_W/2-random(80),GetScreenHeight()-WIN_H/2-random(80),
209
			//DefineAndDrawWindow(0,0,
210
				WIN_W,WIN_H,0x73,0,0,0);
6978 leency 211
			GetProcessInfo(#Form, SelfInfo);
7780 leency 212
			ProcessMenuClick();
7806 leency 213
			sc.get();
7759 leency 214
			if (Form.status_window>2) break;
6978 leency 215
			if (Form.height<120) { MoveSize(OLD,OLD,OLD,120); break; }
216
			if (Form.width<280) { MoveSize(OLD,OLD,280,OLD); break; }
7748 leency 217
			draw_window();
6978 leency 218
			break;
219
 
220
		case evNetwork:
7758 leency 221
			if (http.transfer <= 0) break;
222
			http.receive();
8336 leency 223
 
224
			if (http_get_type==PAGE) {
225
				CheckContentType();
226
				prbar.max = http.content_length;
227
				if (prbar.value != http.content_received) {
228
					prbar.value = http.content_received;
229
					DrawProgress();
230
				}
231
			}
232
 
7758 leency 233
			if (http.receive_result != 0) break;
8499 leency 234
			if (debug_mode) {
235
				EAX = http.transfer;
236
				debugln(#EAX.http_msg.http_header);
237
			}
7758 leency 238
			if (http.status_code >= 300) && (http.status_code < 400)
239
			{
240
				// Handle redirects
241
				if (redirect_count<=5) {
242
					redirect_count++;
8305 leency 243
					HandleRedirect();
7758 leency 244
				} else {
245
					StopLoading();
7791 leency 246
					redirect_count = 0;
8440 leency 247
					if (http_get_type==IMG) goto _IMG_RES;
8457 leency 248
					notify("'Too many redirects.' -E");
4536 leency 249
				}
7758 leency 250
			} else {
251
				// Loading the page is complete, free resources
252
				redirect_count = 0;
8016 leency 253
				if (http_get_type==PAGE) {
8413 leency 254
					history.add(http.cur_url);
8462 leency 255
					if (!strchr(http.cur_url, '?')) {
8492 leency 256
						cache.add(http.cur_url, http.content_pointer, http.content_received, PAGE, WB1.custom_encoding);
8462 leency 257
					}
258
					LoadInternalPage(http.content_pointer, http.content_received);
8425 leency 259
					free(http.content_pointer);
9089 leency 260
					DrawOmnibox();
8016 leency 261
				}
262
				else if (http_get_type==IMG) {
8440 leency 263
					_IMG_RES:
8454 leency 264
					if (http.status_code >= 200) && (http.status_code < 300) {
8492 leency 265
						cache.add(cur_img_url, http.content_pointer, http.content_received, IMG, NULL);
8454 leency 266
					} else {
8492 leency 267
						cache.add(cur_img_url, 0, 0, IMG, NULL);
8454 leency 268
					}
8698 leency 269
					http.hfree();
8425 leency 270
					free(http.content_pointer);
8336 leency 271
					GetImg(false);
8016 leency 272
				}
6978 leency 273
			}
7914 leency 274
			break;
275
		default:
276
			if (ESDWORD[shared_url] != '\0') {
277
				EventOpenNewTab(shared_url);
278
				ESDWORD[shared_url] = '\0';
279
				ActivateWindow(GetProcessSlot(Form.ID));
280
			}
6978 leency 281
	}
3067 leency 282
}
283
 
8332 leency 284
 
285
//===================================================//
286
//                                                   //
287
//                      EVENTS                       //
288
//                                                   //
289
//===================================================//
290
 
291
void ProcessButtonClick(dword id__)
7772 leency 292
{
8332 leency 293
	switch (id__)
294
	{
295
		case 1: ExitProcess();
296
		case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX: EventTabClose(id__ - TAB_CLOSE_ID); return;
297
		case TAB_ID...TAB_ID+TABS_MAX: EventAllTabsClick(id__ - TAB_ID); return;
8492 leency 298
		case ENCODINGS...ENCODINGS+6: EventManuallyChangeEncoding(id__-ENCODINGS); return;
8332 leency 299
		case NEW_WINDOW:       RunProgram(#program_path, NULL); return;
300
		case NEW_TAB:          if (!http.transfer) EventOpenNewTab(URL_SERVICE_HOMEPAGE); return;
301
		case SCAN_CODE_BS:
302
		case BACK_BUTTON:      if (history.back()) OpenPage(history.current()); return;
303
		case FORWARD_BUTTON:   if (history.forward()) OpenPage(history.current()); return;
304
		case GOTOURL_BUTTON:   EventSubmitOmnibox();	return;
305
		case REFRESH_BUTTON:   EventRefreshPage(); return;
306
		case CHANGE_ENCODING:  EventShowEncodingsList(); return;
307
		case SANDWICH_BUTTON:  EventShowMainMenu(); return;
308
		case VIEW_SOURCE:      EventViewSource(); return;
309
		case EDIT_SOURCE:      EventEditSource(); return;
310
		case VIEW_HISTORY:     OpenPage(URL_SERVICE_HISTORY); return;
311
		case DOWNLOAD_MANAGER: EventOpenDownloader(""); return;
312
		case UPDATE_BROWSER:   EventUpdateBrowser(); return;
313
		case CLEAR_CACHE:      EventClearCache(); return;
314
		case IN_NEW_TAB:       EventClickLink(TARGET_NEW_TAB); return;
315
		case IN_NEW_WINDOW:    EventClickLink(TARGET_NEW_WINDOW); return;
316
		case COPY_LINK_URL:    EventCopyLinkToClipboard(); return;
317
		case DOWNLOAD_LINK_CT: EventOpenDownloader( GetAbsoluteActiveURL() ); return;
318
		case OPEN_FILE:        EventOpenDialog(); return;
319
	}
320
}
321
 
8500 leency 322
void ProcessKeyEvent()
8332 leency 323
{
324
	if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT)
325
	{
326
		if (key_scancode == SCAN_CODE_TAB) {EventActivatePreviousTab();return;}
8500 leency 327
		if (key_scancode == SCAN_CODE_KEY_T) {EventOpenNewTab(URL_SERVICE_TEST);return;}
8332 leency 328
	}
329
 
7772 leency 330
	if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) switch(key_scancode)
331
	{
8500 leency 332
		case SCAN_CODE_KEY_O: EventOpenDialog(); return;
333
		case SCAN_CODE_KEY_H: ProcessButtonClick(VIEW_HISTORY); return;
334
		case SCAN_CODE_KEY_U: EventViewSource(); return;
335
		case SCAN_CODE_KEY_T: EventOpenNewTab(URL_SERVICE_HOMEPAGE); return;
336
		case SCAN_CODE_KEY_N: RunProgram(#program_path, NULL); return;
337
		case SCAN_CODE_KEY_J: ProcessButtonClick(DOWNLOAD_MANAGER); return;
338
		case SCAN_CODE_KEY_R: ProcessButtonClick(REFRESH_BUTTON); return;
339
		case SCAN_CODE_ENTER: EventSeachWeb(); return;
340
		case SCAN_CODE_LEFT:  ProcessButtonClick(BACK_BUTTON); return;
341
		case SCAN_CODE_RIGHT: ProcessButtonClick(FORWARD_BUTTON); return;
342
		case SCAN_CODE_KEY_W: EventCloseActiveTab(); return;
343
		case SCAN_CODE_TAB:   EventActivateNextTab(); return;
8336 leency 344
		case SCAN_CODE_F5:    EventClearCache(); return;
8500 leency 345
		default: return;
7772 leency 346
	}
8332 leency 347
 
348
	switch(key_scancode)
349
	{
350
		case SCAN_CODE_UP:    EventScrollUpAndDown(SCAN_CODE_UP); return;
351
		case SCAN_CODE_DOWN:  EventScrollUpAndDown(SCAN_CODE_DOWN); return;
352
		case SCAN_CODE_F6:    {omnibox_edit.flags=ed_focus; DrawOmnibox();} return;
353
		case SCAN_CODE_F5:    EventRefreshPage(); return;
354
		case SCAN_CODE_ENTER: if (omnibox_edit.flags & ed_focus) EventSubmitOmnibox(); return;
355
		case SCAN_CODE_F12:   EventToggleDebugMode(); return;
356
		case SCAN_CODE_F11:   show_images^=1; EventClearCache(); return;
357
		default:              if (WB1.list.ProcessKey(key_scancode)) WB1.DrawPage(); return;
358
	}
7772 leency 359
}
360
 
3067 leency 361
void SetElementSizes()
362
{
8332 leency 363
	omnibox_edit.width = Form.cwidth - omnibox_edit.left - 52 - 16;
9309 leency 364
	if (Form.cwidth - scroll_wv.size_x != WB1.list.w) {
365
		//temporary fix for crash
366
		//related to 'cur_img_url' var read
367
		//http://board.kolibrios.org/viewtopic.php?f=1&t=1712&start=60#p77523
368
		StopLoading();
369
	}
8457 leency 370
	WB1.list.SetSizes(0, TOOLBAR_H+TAB_H, Form.cwidth - scroll_wv.size_x,
7771 leency 371
		Form.cheight - TOOLBAR_H - STATUSBAR_H - TAB_H, BASIC_LINE_H);
7759 leency 372
	WB1.list.wheel_size = 7 * BASIC_LINE_H;
7746 leency 373
	WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w + 1;
6794 leency 374
	WB1.list.visible = WB1.list.h;
3067 leency 375
}
376
 
5981 leency 377
 
7748 leency 378
void draw_window()
3067 leency 379
{
7910 leency 380
	bool burger_active = false;
381
	if (menu_id == OPEN_FILE) burger_active = true;
382
 
3067 leency 383
	SetElementSizes();
7758 leency 384
 
9348 pavelyakov 385
    if (!application_mode) {
386
        DrawBar(0,0, Form.cwidth,PADDING, sc.work);
387
        DrawBar(0,PADDING+TSZE+1, Form.cwidth,PADDING-1, sc.work);
9602 leency 388
        DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, MixColors(sc.dark, sc.work, 180));
389
        DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, sc.line);
9348 pavelyakov 390
        DrawBar(0, PADDING, omnibox_edit.left-2, TSZE+1, sc.work);
391
        DrawBar(omnibox_edit.left+omnibox_edit.width+18, PADDING, Form.cwidth-omnibox_edit.left-omnibox_edit.width-18, TSZE+1, sc.work);
7758 leency 392
 
9348 pavelyakov 393
        DrawTopPanelButton(BACK_BUTTON, PADDING-1, PADDING, 30, false);
394
        DrawTopPanelButton(FORWARD_BUTTON, PADDING+TSZE+PADDING-2, PADDING, 31, false);
395
        DrawTopPanelButton(SANDWICH_BUTTON, Form.cwidth-PADDING-TSZE-3, PADDING, -1, burger_active); //burger menu
7758 leency 396
 
9602 leency 397
        DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, sc.line);
7758 leency 398
 
9348 pavelyakov 399
        DrawRectangle(WB1.list.x + WB1.list.w, WB1.list.y, scroll_wv.size_x,
7758 leency 400
		WB1.list.h-1, scroll_wv.bckg_col);
9348 pavelyakov 401
	}
7758 leency 402
 
8457 leency 403
	if (!canvas.bufw) {
8500 leency 404
		EventOpenFirstPage();
8457 leency 405
	} else {
7771 leency 406
		WB1.DrawPage();
8457 leency 407
		DrawOmnibox();
7771 leency 408
	}
9348 pavelyakov 409
	if (!application_mode) {
410
	    DrawProgress();
411
	    DrawStatusBar(NULL);
412
        DrawTabsBar();
413
	}
414
 
7771 leency 415
}
416
 
8500 leency 417
void EventOpenFirstPage()
418
{
419
	OpenPage(history.current());
420
}
421
 
8492 leency 422
void EventManuallyChangeEncoding(int _new_encoding)
7759 leency 423
{
424
	dword newbuf, newsize;
425
	WB1.custom_encoding = _new_encoding;
426
	newsize = strlen(WB1.o_bufpointer);
427
	newbuf = malloc(newsize);
428
	memmov(newbuf, WB1.o_bufpointer, newsize);
429
	LoadInternalPage(newbuf, newsize);
430
	free(newbuf);
431
}
3067 leency 432
 
7759 leency 433
 
8332 leency 434
void EventScrollUpAndDown(int _direction)
4413 leency 435
{
8332 leency 436
	int i;
437
	for (i=0;i
438
		if (_direction == SCAN_CODE_UP) WB1.list.KeyUp();
439
		if (_direction == SCAN_CODE_DOWN) WB1.list.KeyDown();
440
	}
441
	WB1.DrawPage();
4413 leency 442
}
443
 
8305 leency 444
void EventToggleDebugMode()
445
{
446
	debug_mode ^= 1;
447
	if (debug_mode) notify("'Debug mode ON'-I");
448
	else notify("'Debug mode OFF'-I");
449
}
450
 
451
void EventAllTabsClick(dword _n)
452
{
453
	if (mouse.mkm) {
8490 leency 454
		StopLoading();
8305 leency 455
		EventTabClose(_n);
456
	} else {
8490 leency 457
		if (!http.transfer) EventTabClick(_n);
8355 leency 458
	}
8305 leency 459
}
460
 
461
void EventEditSource()
462
{
463
	if (check_is_the_adress_local(history.current())) {
9585 vitalkrilo 464
		RunProgram("/sys/develop/cedit", history.current());
8305 leency 465
	} else {
466
		CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm");
9585 vitalkrilo 467
		if (!EAX) RunProgram("/sys/develop/cedit", "/tmp0/1/WebView_tmp.htm");
8305 leency 468
	}
469
}
470
 
471
void EventClearCache()
472
{
473
	cache.clear();
474
	notify(#clear_cache_ok);
475
	EventRefreshPage();
476
}
477
 
478
void EventCopyLinkToClipboard()
479
{
480
	Clipboard__CopyText(GetAbsoluteActiveURL());
481
	notify("'URL copied to clipboard'O");
482
}
483
 
4636 leency 484
void StopLoading()
485
{
8305 leency 486
	if (http.stop()) pause(10);
8330 leency 487
	prbar.value = 0;
4645 leency 488
}
489
 
7756 leency 490
//rewrite into
7757 leency 491
//bool strrpl(dword dst, from, into, dst_len);
492
bool ReplaceSpaceInUrl(dword url, size) {
7756 leency 493
	unsigned int i, j;
7757 leency 494
	bool was_changed=false;
495
	for (i=url+size-3; i>url; i--)
7720 leency 496
	{
7756 leency 497
		if (ESBYTE[i]!=' ') continue;
7757 leency 498
		for (j=url+size-3; j>=i; j--) {
499
			ESBYTE[j+3]=ESBYTE[j+2];
7756 leency 500
			ESBYTE[j+2]=ESBYTE[j+1];
501
			ESBYTE[j+1]=ESBYTE[j];
502
		}
503
		ESBYTE[i] = '%';
504
		ESBYTE[i+1] = '2';
505
		ESBYTE[i+2] = '0';
7757 leency 506
		was_changed = true;
7720 leency 507
	}
7757 leency 508
	return was_changed;
7720 leency 509
}
510
 
7801 leency 511
bool HandleUrlFiles(dword _path, _data)
512
{
513
	dword url_from_file;
514
	if (!UrlExtIs(_path, "url")) return false;
7945 leency 515
	if (! url_from_file = strstri(_data, "URL=")) return false;
7801 leency 516
	replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
7945 leency 517
	OpenPage(url_from_file);
518
	return true;
7801 leency 519
}
520
 
7755 leency 521
bool GetLocalFileData(dword _path)
4416 leency 522
{
7801 leency 523
	dword data, size;
8392 leency 524
	read_file(_path, #data, #size);
7801 leency 525
	if (!HandleUrlFiles(_path, data)) {
526
		LoadInternalPage(data, size);
4692 leency 527
	}
7801 leency 528
	free(data);
529
	return true;
7755 leency 530
}
7282 leency 531
 
8336 leency 532
bool GetUrl(dword _http_url)
8330 leency 533
{
534
	char new_url_full[URL_SIZE+1];
535
 
536
	if (!strncmp(_http_url,"http:",5)) {
537
		http.get(_http_url);
8336 leency 538
		return true;
8330 leency 539
	} else if (!strncmp(_http_url,"https://",8)) {
540
		strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
541
		strncat(#new_url_full, _http_url, URL_SIZE);
542
		http.get(#new_url_full);
8336 leency 543
		return true;
8330 leency 544
	}
8336 leency 545
	return false;
8330 leency 546
}
547
 
7755 leency 548
void OpenPage(dword _open_URL)
549
{
550
	char new_url[URL_SIZE+1];
7788 leency 551
	int unz_id;
7755 leency 552
 
553
	StopLoading();
554
 
7771 leency 555
	SetOmniboxText(_open_URL);
556
 
7755 leency 557
	strncpy(#new_url, _open_URL, URL_SIZE);
558
 
559
	//Exclude # from the URL to the load page
560
	//We will bring it back when we get the buffer
7970 leency 561
	if (strrchr(#new_url, '#')) {
562
		anchors.take_anchor_from(#new_url);
563
	}
7755 leency 564
 
7976 leency 565
	/*
566
	There could be several possible types of addresses:
567
	- cached page (only http/https)
568
	- internal page
569
	- web page
570
	- local file
571
	So we need to detect what incoming address is
572
	and then halndle it in the propper way.
573
	*/
574
 
8016 leency 575
	if (cache.has(#new_url)) {
7764 leency 576
		//CACHED PAGE
8413 leency 577
		if (cache.current_type==PAGE) {
578
			history.add(#new_url);
8492 leency 579
			WB1.custom_encoding = cache.current_charset;
8413 leency 580
			LoadInternalPage(cache.current_buf, cache.current_size);
581
		}
582
		else {
583
			EventDownloadAndOpenImage(#new_url);
584
		}
7764 leency 585
 
586
	} else if (!strncmp(#new_url,"WebView:",8)) {
7755 leency 587
		//INTERNAL PAGE
8413 leency 588
		history.add(#new_url);
8492 leency 589
		WB1.custom_encoding = -1;
8278 leency 590
		if (streq(#new_url, URL_SERVICE_HOMEPAGE)) LoadInternalPage(#buildin_page_home, sizeof(buildin_page_home));
591
		else if (streq(#new_url, URL_SERVICE_HELP)) LoadInternalPage(#buildin_page_help, sizeof(buildin_page_help));
8457 leency 592
		else if (streq(#new_url, URL_SERVICE_TEST)) LoadInternalPage(#buildin_page_test, sizeof(buildin_page_test));
7976 leency 593
		else if (streq(#new_url, URL_SERVICE_HISTORY)) ShowHistory();
8278 leency 594
		else LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
7800 leency 595
 
7755 leency 596
	} else if (!strncmp(#new_url,"http:",5)) || (!strncmp(#new_url,"https:",6)) {
597
		//WEB PAGE
7757 leency 598
		if (ReplaceSpaceInUrl(#new_url, URL_SIZE)) {
599
			strcpy(#editURL, #new_url);
600
		}
601
 
8016 leency 602
		http_get_type = PAGE;
8330 leency 603
		GetUrl(#new_url);
7758 leency 604
 
605
		DrawOmnibox();
606
 
7755 leency 607
		if (!http.transfer) {
8413 leency 608
			history.add(#new_url);
8278 leency 609
			LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
4646 leency 610
		}
7755 leency 611
	} else {
612
		//LOCAL PAGE
8413 leency 613
		history.add(#new_url);
7788 leency 614
		if (UrlExtIs(#new_url,".docx")) {
615
			DeleteFile("/tmp0/1/temp/word/document.xml");
616
			CreateDir("/tmp0/1/temp");
617
			unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
618
			while (GetProcessSlot(unz_id)) pause(2);
619
			strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
620
		}
7755 leency 621
		if (!GetLocalFileData(#new_url)) {
8278 leency 622
			LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
4650 leency 623
		}
4636 leency 624
	}
4416 leency 625
}
4415 leency 626
 
8278 leency 627
dword EventOpenDownloader(dword _url)
628
{
629
	//char download_params[URL_SIZE+50];
630
	return RunProgram("/sys/network/dl", _url);
631
}
7970 leency 632
 
633
bool EventClickAnchor()
4508 leency 634
{
7970 leency 635
	dword aURL = links.active_url;
636
 
637
	if (anchors.get_pos_by_name(aURL+1)!=-1) {
638
		WB1.list.first = anchors.get_pos_by_name(aURL+1);
8417 leency 639
		WB1.list.CheckDoesValuesOkey();
7970 leency 640
		strcpy(#editURL, history.current());
641
		strcat(#editURL, aURL);
642
		DrawOmnibox();
643
		WB1.DrawPage();
644
		return true;
645
	}
646
	return false;
647
}
648
 
649
void EventClickLink(dword _target)
650
{
7756 leency 651
	char new_url[URL_SIZE+1];
7936 leency 652
	char new_url_full[URL_SIZE+1];
7970 leency 653
	dword aURL = GetAbsoluteActiveURL();
654
	if (!aURL) return;
4415 leency 655
 
7970 leency 656
	strcpy(#new_url, aURL);
657
 
658
	if (ESBYTE[aURL]=='#') {
659
		if (_target == TARGET_SAME_TAB) {
660
			EventClickAnchor();
661
			return;
662
		} else {
663
			strcpy(#new_url, history.current());
664
			strcat(#new_url, aURL);
665
		}
666
	}
667
 
668
	if (_target == TARGET_NEW_TAB) {
669
		EventOpenNewTab(#new_url);
7758 leency 670
		return;
671
	}
672
 
7970 leency 673
	if (_target == TARGET_NEW_WINDOW) {
674
		strcpy(#new_url_full, "-new ");
675
		strncat(#new_url_full, #new_url, URL_SIZE);
676
		RunProgram(#program_path, #new_url_full);
7755 leency 677
		return;
5768 leency 678
	}
7742 leency 679
 
7970 leency 680
	if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
681
		notify(#new_url);
7755 leency 682
		return;
7742 leency 683
	}
4508 leency 684
 
7771 leency 685
	if (http.transfer) {
5774 leency 686
		StopLoading();
687
	}
6730 leency 688
 
7755 leency 689
	if (strrchr(#new_url, '#')!=0) {
690
		anchors.take_anchor_from(#new_url);
691
		OpenPage(#new_url);
6730 leency 692
		return;
693
	}
694
 
7755 leency 695
	if (!strncmp(#new_url,"WebView:",8)) {
696
		OpenPage(#new_url);
7742 leency 697
		return;
698
	}
6730 leency 699
 
7755 leency 700
	if (strncmp(#new_url,"http://",7)!=0) && (strncmp(#new_url,"https://",8)!=0)
5774 leency 701
	{
7755 leency 702
		if (UrlExtIs(#new_url,".htm")!=true) && (UrlExtIs(#new_url,".html")!=true)
6730 leency 703
		{
7755 leency 704
			if (strchr(#new_url, '|')) {
705
				ESBYTE[strchr(#new_url, '|')] = NULL;
706
				RunProgram(#new_url, strlen(#new_url)+1+#new_url);
707
			} else {
708
				RunProgram("/sys/@open", #new_url);
7422 leency 709
			}
6730 leency 710
			return;
711
		}
5774 leency 712
	}
7755 leency 713
	OpenPage(#new_url);
714
}
715
 
716
void EventSubmitOmnibox()
717
{
718
	char new_url[URL_SIZE+1];
719
	if (!editURL[0]) return;
720
	if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/')
721
	|| (!strncmp(#editURL,"https:",6)) || (!strncmp(#editURL,"WebView:",8)) {
722
		OpenPage(#editURL);
723
	} else {
724
		strcpy(#new_url, "http://");
7970 leency 725
		strncat(#new_url, #editURL, URL_SIZE-1);
7755 leency 726
		OpenPage(#new_url);
6931 leency 727
	}
5774 leency 728
}
729
 
7755 leency 730
void LoadInternalPage(dword _bufdata, _in_bufsize){
731
	if (!_bufdata) || (!_in_bufsize) {
8278 leency 732
		LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
7755 leency 733
	} else {
734
		WB1.list.first = 0; //scroll page to the top
735
		DrawOmnibox();
736
		if(!strrchr(#editURL, '#')) {
737
			strcat(#editURL, #anchors.current);
738
			DrawOmnibox();
739
		}
8365 leency 740
		render_start_time = GetStartTime();
7759 leency 741
		WB1.ParseHtml(_bufdata, _in_bufsize);
8302 leency 742
		// REJECTED. Reason: infinite redirect at Google Results.
743
		/*
744
		if (WB1.redirect) { //
745
			get_absolute_url(#WB1.redirect, history.current());
746
			history.back();
747
			OpenPage(#WB1.redirect);
748
		}
749
		*/
8413 leency 750
		DrawStatusBar(NULL);
7771 leency 751
		DrawActiveTab();
7756 leency 752
		if (source_mode) {
753
			source_mode = false;
7799 leency 754
			WB1.custom_encoding = CH_CP866;
7759 leency 755
			ShowSource(WB1.bufpointer, _in_bufsize);
7757 leency 756
		} else {
8016 leency 757
			WB1.DrawPage();
7756 leency 758
		}
8698 leency 759
		http.hfree();
8445 leency 760
		if (WB1.img_url.count) { GetImg(true); DrawOmnibox(); }
7742 leency 761
	}
762
}
763
 
7799 leency 764
bool UrlExtIs(dword base, ext)
7742 leency 765
{
7755 leency 766
	if (!strcmpi(base + strlen(base) - strlen(ext), ext)) return true;
7742 leency 767
	return false;
768
}
769
 
770
void DrawProgress()
771
{
8330 leency 772
	dword pct;
9348 pavelyakov 773
	if (application_mode) return;
8330 leency 774
	if (!http.transfer) return;
775
	if (http_get_type==PAGE) && (prbar.max) pct = prbar.value*30/prbar.max; else pct = 10;
8336 leency 776
	if (http_get_type==IMG) pct = prbar.value * 70 / prbar.max + 30;
8332 leency 777
	DrawBar(omnibox_edit.left-1, omnibox_edit.top+20, pct*omnibox_edit.width+16/100, 2, 0x72B7EB);
7742 leency 778
}
779
 
7770 leency 780
void EventShowPageMenu()
6782 leency 781
{
9348 pavelyakov 782
    if (application_mode) return;
8020 leency 783
	open_lmenu(mouse.x, mouse.y, MENU_TOP_LEFT, NULL, #rmb_menu);
7780 leency 784
	menu_id = VIEW_SOURCE;
6782 leency 785
}
786
 
7770 leency 787
void EventShowLinkMenu()
7759 leency 788
{
9348 pavelyakov 789
    if (application_mode) return;
8020 leency 790
	open_lmenu(mouse.x, mouse.y, MENU_TOP_LEFT, NULL, #link_menu);
7789 simsanutiy 791
	menu_id = IN_NEW_TAB;
7759 leency 792
}
793
 
7780 leency 794
void EventShowMainMenu()
795
{
9348 pavelyakov 796
    if (application_mode) return;
8020 leency 797
	open_lmenu(Form.cwidth - PADDING -4, PADDING + TSZE + 3,
798
		MENU_TOP_RIGHT, NULL, #main_menu);
7780 leency 799
	menu_id = OPEN_FILE;
800
}
801
 
7770 leency 802
void EventShowEncodingsList()
7758 leency 803
{
9348 pavelyakov 804
    if (application_mode) return;
8020 leency 805
	open_lmenu(Form.cwidth-4, Form.cheight - STATUSBAR_H + 12,
806
		MENU_BOT_RIGHT, WB1.cur_encoding + 1,
807
		"UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
7780 leency 808
	menu_id = ENCODINGS;
7758 leency 809
}
810
 
7780 leency 811
void ProcessMenuClick()
6795 leency 812
{
7780 leency 813
	int click_id;
7910 leency 814
	if (menu_id) {
815
		if (click_id = get_menu_click()) {
816
			click_id += menu_id - 1;
8332 leency 817
			ProcessButtonClick(click_id);
7910 leency 818
		}
819
		if (!menu_process_id) menu_id = NULL;
7780 leency 820
	}
6795 leency 821
}
822
 
7743 leency 823
void EventSeachWeb()
824
{
7755 leency 825
	char new_url[URL_SIZE+1];
8492 leency 826
	replace_char(#editURL, ' ', '+', URL_SIZE);
7755 leency 827
	strcpy(#new_url, "https://www.google.com/search?q=");
828
	strncat(#new_url, #editURL, URL_SIZE);
829
	OpenPage(#new_url);
7743 leency 830
}
831
 
7748 leency 832
void EventOpenDialog()
833
{
834
	OpenDialog_start stdcall (#o_dialog);
835
	if (o_dialog.status) {
7755 leency 836
		OpenPage(#openfile_path);
7748 leency 837
	}
838
}
839
 
7750 leency 840
void EventViewSource()
841
{
7771 leency 842
	source_mode = true;
843
	EventOpenNewTab(history.current());
7750 leency 844
}
845
 
7893 leency 846
void EventRefreshPage()
847
{
848
	if (http.transfer) {
849
		StopLoading();
850
		draw_window();
851
	} else {
852
		OpenPage(history.current());
853
	}
854
}
855
 
7765 leency 856
void EventUpdateBrowser()
857
{
858
	dword downloader_id, slot_n;
859
	dword current_size;
860
	dword new_size;
861
 
862
	draw_window();
863
 
8278 leency 864
	downloader_id = EventOpenDownloader(#update_param);
7765 leency 865
	do {
866
		slot_n = GetProcessSlot(downloader_id);
867
		pause(10);
868
	} while (slot_n!=0);
869
 
8392 leency 870
	current_size = get_file_size(#program_path);
871
	new_size = get_file_size("/tmp0/1/Downloads/WebView.com");
7765 leency 872
 
8413 leency 873
	if (!new_size) || (new_size<5000) { notify(#update_download_error); return; }
874
	if (current_size == new_size) { notify(#update_is_current);	return; }
7765 leency 875
 
8332 leency 876
	if (CopyFileAtOnce(new_size, "/tmp0/1/Downloads/WebView.com", #program_path)) {
7765 leency 877
		notify(#update_can_not_copy);
878
	} else {
879
		notify(#update_ok);
880
		RunProgram(#program_path, history.current());
881
		ExitProcess();
882
	}
883
}
884
 
8413 leency 885
void DrawStatusBar(dword _msg)
6795 leency 886
{
7970 leency 887
	dword status_y = Form.cheight - STATUSBAR_H + 4;
888
	dword status_w = Form.cwidth - 90;
9348 pavelyakov 889
	if (application_mode) return;
8417 leency 890
	if (Form.status_window>2) return;
7806 leency 891
	DrawBar(0,Form.cheight - STATUSBAR_H+1, Form.cwidth,STATUSBAR_H-1, sc.work);
8413 leency 892
	if (_msg) {
893
		ESI = math.min(status_w/6, strlen(_msg));
894
		WriteText(10, status_y, 0, sc.work_text, _msg);
7758 leency 895
	}
7970 leency 896
	DefineHiddenButton(status_w+20, status_y-3, 60, 12, CHANGE_ENCODING);
897
	WriteTextCenter(status_w+20, status_y, 60, sc.work_text, WB1.cur_encoding*10+#charsets);
7758 leency 898
}
899
 
900
void DrawOmnibox()
901
{
7784 leency 902
	int imgxoff;
9348 pavelyakov 903
	if (application_mode) return;
9602 leency 904
	DrawOvalBorder(omnibox_edit.left-2, omnibox_edit.top-3, omnibox_edit.width+18, 24, sc.line,
905
		sc.line, sc.line, sc.dark);
8332 leency 906
	DrawBar(omnibox_edit.left-1, omnibox_edit.top-2, omnibox_edit.width+18, 1, 0xD8DCD8);
9644 leency 907
	DrawBar(omnibox_edit.left-1, omnibox_edit.top-1, omnibox_edit.width+18, 1, omnibox_edit.bg_color);
908
	DrawBar(omnibox_edit.left-1, omnibox_edit.top, 1, 22, omnibox_edit.bg_color);
7758 leency 909
 
8332 leency 910
	if (omnibox_edit.flags & ed_focus) omnibox_edit.flags = ed_focus; else omnibox_edit.flags = 0;
911
	EditBox_UpdateText(#omnibox_edit, omnibox_edit.flags);
912
	edit_box_draw stdcall(#omnibox_edit);
7784 leency 913
	if (http.transfer) imgxoff = 16*23*3; else imgxoff = 0;
9602 leency 914
	PutImage(omnibox_edit.left+omnibox_edit.width+1, omnibox_edit.top-1, 16, 23, imgxoff + #editbox_icons);
8332 leency 915
	DefineHiddenButton(omnibox_edit.left+omnibox_edit.width-1, omnibox_edit.top-2, 17, 23, REFRESH_BUTTON);
7758 leency 916
 
917
	DrawProgress();
918
}
919
 
7771 leency 920
void SetOmniboxText(dword _text)
921
{
9348 pavelyakov 922
    if (application_mode) return;
923
    edit_box_set_text stdcall (#omnibox_edit, _text);
924
    omnibox_edit.pos = omnibox_edit.flags = 0;
925
    DrawOmnibox();
7771 leency 926
}
7758 leency 927
 
7970 leency 928
dword GetAbsoluteActiveURL()
929
{
930
	char abs_url[URL_SIZE];
931
	if (links.active_url) {
932
		strncpy(#abs_url, links.active_url, URL_SIZE);
8291 leency 933
		get_absolute_url(#abs_url, history.current());
7970 leency 934
		return #abs_url;
935
	}
936
	return 0;
937
}
7771 leency 938
 
8305 leency 939
void CheckContentType()
940
{
941
	char content_type[64];
8319 leency 942
	if (http.header_field("content-type", #content_type, sizeof(content_type))) // application || image
8413 leency 943
 
944
	if (content_type[0] == 'i') {
945
		EventDownloadAndOpenImage(http.cur_url);
946
		StopLoading();
8499 leency 947
	}
948
	else if (content_type[0] == 'a') {
8444 leency 949
		EventOpenDownloader(http.cur_url);
8305 leency 950
		StopLoading();
951
	}
8499 leency 952
	else {
953
		WB1.custom_encoding = -1;
954
		if (EAX = strchr(#content_type, '=')) {
955
			WB1.custom_encoding = get_encoding_type_by_name(EAX+1);
956
		}
957
	}
8305 leency 958
}
959
 
8413 leency 960
void EventDownloadAndOpenImage(dword _url)
961
{
962
	char image_download_url[URL_SIZE];
963
	strcpy(#image_download_url, "-eo ");
964
	strncat(#image_download_url, _url, URL_SIZE);
965
	EventOpenDownloader(#image_download_url);
966
}
967
 
8305 leency 968
void HandleRedirect()
969
{
8500 leency 970
	dword redirect_url = malloc(URL_SIZE);
971
	http.header_field("location", redirect_url, URL_SIZE);
972
	get_absolute_url(redirect_url, http.cur_url);
8305 leency 973
	http.hfree();
8500 leency 974
	if (http_get_type==PAGE) OpenPage(redirect_url);
975
	else if (http_get_type==IMG) GetUrl(redirect_url);
976
	free(redirect_url);
8305 leency 977
}
978
 
8336 leency 979
dword GetImg(bool _new)
8016 leency 980
{
8336 leency 981
	int i;
8330 leency 982
	if (!show_images) return;
8336 leency 983
	http_get_type = IMG;
984
 
8396 leency 985
	for (i = 0; i < WB1.img_url.count; i++)
8336 leency 986
	{
8396 leency 987
		cur_img_url = WB1.img_url.get(i);
9436 leency 988
		if (debug_mode)
9422 leency 989
			{debug("get img: ");debugln(cur_img_url);}
8336 leency 990
		if (cache.has(cur_img_url)==false) {
8396 leency 991
			prbar.max = WB1.img_url.count;
8336 leency 992
			prbar.value = i;
8413 leency 993
			if (GetUrl(cur_img_url)) {DrawStatusBar(cur_img_url); DrawProgress(); return;}
8336 leency 994
		}
8016 leency 995
	}
8336 leency 996
	if (_new) return;
8016 leency 997
	DrawOmnibox();
8413 leency 998
	DrawStatusBar(T_RENDERING);
8500 leency 999
	WB1.Reparse();
8016 leency 1000
	WB1.DrawPage();
8413 leency 1001
	debugln(sprintf(#param, T_DONE_IN_SEC, GetStartTime()-render_start_time/100));
1002
	DrawStatusBar(NULL);
8016 leency 1003
}
1004
 
9585 vitalkrilo 1005
stop: