Subversion Repositories Kolibri OS

Rev

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