fix height checkbox
[kolibrios.git] / programs / cmm / misc / osupdate.c
blobc391a4ec7a1723c5aa3df68849a8f929f61934e2
1 #define MEMSIZE 1024*50
3 #include "../lib/gui.h"
4 #include "../lib/copyf.h"
6 #include "../lib/obj/libini.h"
7 #include "../lib/obj/libimg.h"
8 #include "../lib/obj/http.h"
9 #include "../lib/obj/network.h"
11 #include "../lib/patterns/restart_process.h"
13 bool install_complete = false;
14 _http http;
16 #define WINW 460
17 #define WINH 380
19 //#define LANG_RUS 1
21 #ifdef LANG_RUS
22 #define T_WINDOW_TITLE "Ž­« ©­ ®¡­®¢«¥­¨¥ KolibriOS"
23 #define T_TITLE_H1 "Ž�‹€‰� Ž��Ž‚‹…�ˆ…"
24 #define T_INTRO "„ ­­®¥ ¯à¨«®¦¥­¨¥ ᪠砥⠯®á«¥¤­îî ¢¥àá¨î KolibriOS ¨ à á¯ ªã¥â ¥¥ ­  RAM-¤¨áª. �ਠí⮬ ï¤à® ­¥ ¡ã¤¥â ¯¥à¥§ ¯ã饭®, ¤«ï í⮣® ­¥®¡å®¤¨¬® á®åà ­¨âì ®¡à § ¨ ¯¥à¥§ £à㧨âáï. �®¦ «ã©áâ , § ªà®©â¥ ¢á¥ ®âªàëâë¥ ¯à¨«®¦¥­¨ï ¯¥à¥¤ ­ ç «®¬ ®¡­®¢«¥­¨ï.
25 ‚�ˆŒ€�ˆ…: ‚ᥠ¨§¬¥­­ë¥ ä ©«ë ­  RAM-¤¨áª¥ ¡ã¤ãâ ¯¥à¥§ ¯¨á ­ë!";
26 #define T_INSTALL "Ž¡­®¢¨âì"
27 #define T_COMPLETE "Ž¡­®¢«¥­¨¥ § ¢¥à襭®"
28 #define T_EXIT "‚ë室"
29 #define IMG_URL "http://builds.kolibrios.org/rus/data/data/kolibri.img"
30 #define KS "‘®åà ­¨âì ­ áâனª¨"
31 #define checkbox_h 190
32 #else
33 #define T_WINDOW_TITLE "KolibriOS Online Updater"
34 #define T_TITLE_H1 "ONLINE UPDATE"
35 #define T_INTRO "This app will download the latest KolibriOS dirsto and update your RAM-disk with it. Kernel won't be restarted.
36 Please close all opened apps before start.
37 Note that all changes on RAM-disk will be lost.";
38 #define T_INSTALL "Update"
39 #define T_COMPLETE "Update complete"
40 #define T_EXIT "Exit"
41 #define IMG_URL "http://builds.kolibrios.org/eng/data/data/kolibri.img"
42 #define KS "Keep settings folder"
43 #define checkbox_h 210
44 #endif
45 char accept_language[]="en"; //not used, necessary for http.get()
46 void Operation_Draw_Progress(dword f) {} //not used, necessary for copyf()
48 checkbox keep_settings = { KS, true };
50 void main()
52 int btn;
53 sensor progress;
54 load_dll(libimg, #libimg_init,1);
55 load_dll(libHTTP, #http_lib_init,1);
56 @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_STACK);
57 loop() switch(@WaitEventTimeout(300))
59 case evButton:
60 btn = @GetButtonID();
61 if (btn<=2) ExitProcess();
62 if (btn==9) goto _INSTALL;
63 keep_settings.click(btn);
64 break;
66 case evKey:
67 switch (@GetKeyScancode()) {
68 case SCAN_CODE_ESC: ExitProcess();
69 case SCAN_CODE_ENTER:
70 if (install_complete) ExitProcess();
71 else {
72 _INSTALL:
73 http.get(IMG_URL);
74 goto _DRAW_WINDOW;
77 break;
79 case evReDraw:
80 _DRAW_WINDOW:
81 sc.get();
82 DefineAndDrawWindow(screen.width-WINW/2,screen.height-WINH/2,
83 WINW+9,WINH+skin_height,0x34,sc.work,T_WINDOW_TITLE,0);
84 WriteText(30, 20, 0x81, 0xEC008C, T_TITLE_H1);
85 if (!install_complete) {
86 DrawTextViewArea(30, 50, WINW-60, WINH-80, T_INTRO, -1, sc.work_text);
87 progress.set_size(30, WINH-130, WINW-60, 20);
88 if (http.transfer<=0) {
89 DrawCaptButton(WINW-110/2, WINH-70, 110, 28, 9, 0x0092D8, 0xFFFfff, T_INSTALL);
90 keep_settings.draw(30, WINH - checkbox_h);
92 } else {
93 DrawIcon32(WINW-32/2, 140, sc.work, 49);
94 WriteTextCenter(0,185, WINW, sc.work_text, T_COMPLETE);
95 DrawCaptButton(WINW-110/2, WINH-70, 110, 28, 2,
96 0x0092D8, 0xFFFfff, T_EXIT);
99 case evNetwork:
100 if (http.transfer <= 0) break;
101 http.receive();
102 if (http.content_length) {
103 progress.draw_progress(http.content_length - http.content_received
104 * progress.w / http.content_length);
106 if (!http.receive_result) {
107 CreateFile(http.content_received,
108 http.content_pointer, "/tmp0/1/latest.img");
109 http.stop();
110 EventDownloadComplete();
115 dword GetFreeSpaceOfRamdisk()
117 dword rdempty = malloc(1440*1024);
118 CreateFile(0, 1440*1024, rdempty, "/sys/rdempty");
119 free(rdempty);
120 rdempty = get_file_size("/sys/rdempty");
121 DeleteFile("/sys/rdempty");
122 return rdempty;
125 signed CheckFreeSpace(dword _latest, _combined)
127 dword cur_size, new_size, empty;
128 DIR_SIZE dir_size;
130 dir_size.get("/sys");
131 cur_size = dir_size.bytes;
133 copyf("/sys", _combined);
134 copyf(_latest, _combined);
135 dir_size.get(_combined);
136 new_size = dir_size.bytes;
138 empty = GetFreeSpaceOfRamdisk();
140 return cur_size + empty - new_size / 1024;
143 void EventDownloadComplete()
145 dword unimg_id, slot_n;
146 signed space_delta;
147 int i=0;
149 char osupdate[32];
150 char latest[40];
151 char backup[40];
152 char combined[40];
153 char exract_param[64];
154 char backup_settings[64];
156 do { sprintf(#osupdate, "/tmp0/1/osupdate%d", i); i++;
157 } while (dir_exists(#osupdate));
158 CreateDir(#osupdate);
160 sprintf(#latest, "%s/latest", #osupdate);
161 sprintf(#backup, "%s/rdbackup", #osupdate);
162 sprintf(#combined, "%s/combined", #osupdate);
163 sprintf(#backup_settings, "%s/settings", #backup);
164 sprintf(#exract_param, "/tmp0/1/latest.img %s -e", #latest);
167 unimg_id = RunProgram("/sys/unimg", #exract_param);
168 do {
169 slot_n = GetProcessSlot(unimg_id);
170 pause(10);
171 } while (slot_n!=0);
173 space_delta = CheckFreeSpace(#latest, #combined);
174 if (space_delta<0) {
175 sprintf(#param, "'Not enought free space! You need %d Kb more.'E", -space_delta);
176 notify(#param);
177 } else {
178 copyf("/sys", #backup);
179 copyf(#latest, "/sys");
180 if (keep_settings.checked) copyf(#backup_settings, "/sys/settings");
181 install_complete = true;