Subversion Repositories Kolibri OS

Rev

Rev 7987 | Rev 8020 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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