Commit 3ba890c669bba8ab1073f4fae2fc075a6cadb7be

Authored by Perry Werneck
1 parent 47a63a65
Exists in v5.2

Still refactoring windows settings engine.

Showing 1 changed file with 162 additions and 13 deletions   Show diff stats
src/pw3270/windows/config.c
@@ -64,8 +64,10 @@ @@ -64,8 +64,10 @@
64 /// @brief Open registry for read-only access. 64 /// @brief Open registry for read-only access.
65 static enum REG_KEY search_for_registry_key(const gchar *group, const gchar *key, HKEY *hKey) 65 static enum REG_KEY search_for_registry_key(const gchar *group, const gchar *key, HKEY *hKey)
66 { 66 {
67 - g_autofree gchar * path = g_strdup_printf("%s\\%s\\%s",WINDOWS_REGISTRY_PATH,g_get_application_name(),group);  
68 size_t ix; 67 size_t ix;
  68 + g_autofree gchar * path = g_strjoin("\\",WINDOWS_REGISTRY_PATH,g_get_application_name(),group,NULL);
  69 +
  70 + trace("%s(%s)",__FUNCTION__,path);
69 71
70 for(ix=0;ix < G_N_ELEMENTS(predefined);ix++) 72 for(ix=0;ix < G_N_ELEMENTS(predefined);ix++)
71 { 73 {
@@ -88,7 +90,9 @@ @@ -88,7 +90,9 @@
88 gboolean pw3270_win32_registry_open(const gchar *group, HKEY *hKey, REGSAM samDesired) 90 gboolean pw3270_win32_registry_open(const gchar *group, HKEY *hKey, REGSAM samDesired)
89 { 91 {
90 size_t ix; 92 size_t ix;
91 - g_autofree gchar * path = g_strdup_printf("%s\\%s\\%s",WINDOWS_REGISTRY_PATH,g_get_application_name(),group); 93 + g_autofree gchar * path = g_strjoin("\\",WINDOWS_REGISTRY_PATH,g_get_application_name(),group,NULL);
  94 +
  95 + trace("%s(%s)",__FUNCTION__,path);
92 96
93 for(ix=0;ix < G_N_ELEMENTS(predefined);ix++) 97 for(ix=0;ix < G_N_ELEMENTS(predefined);ix++)
94 { 98 {
@@ -308,7 +312,19 @@ void set_string_to_config(const gchar *group, const gchar *key, const gchar *fmt @@ -308,7 +312,19 @@ void set_string_to_config(const gchar *group, const gchar *key, const gchar *fmt
308 } 312 }
309 313
310 } 314 }
  315 + else if(pw3270_win32_registry_open(group, &hKey, KEY_ALL_ACCESS))
  316 + {
  317 + if(str)
  318 + {
  319 + RegSetValueEx(hKey,key,0,REG_SZ,(const BYTE *) str,strlen(str)+1);
  320 + }
  321 + else
  322 + {
  323 + RegDeleteKey(hKey,key);
  324 + }
311 325
  326 + RegCloseKey(hKey);
  327 + }
312 328
313 } 329 }
314 330
@@ -320,7 +336,21 @@ void set_boolean_to_config(const gchar *group, const gchar *key, gboolean val) @@ -320,7 +336,21 @@ void set_boolean_to_config(const gchar *group, const gchar *key, gboolean val)
320 { 336 {
321 g_key_file_set_boolean(keyfile,group,key,val); 337 g_key_file_set_boolean(keyfile,group,key,val);
322 } 338 }
  339 + else if(pw3270_win32_registry_open(group, &hKey, KEY_ALL_ACCESS))
  340 + {
  341 + DWORD value = val ? 1 : 0;
  342 + LONG rc = RegSetValueEx(hKey, key, 0, REG_DWORD,(const BYTE *) &value,sizeof(value));
  343 +
  344 + SetLastError(rc);
323 345
  346 + if(rc != ERROR_SUCCESS)
  347 + {
  348 + g_autofree gchar *msg = g_win32_error_message(GetLastError());
  349 + g_warning("Error \"%s\" when setting key HKCU\\%s\\%s\\%s",msg,WINDOWS_REGISTRY_PATH,g_get_application_name(),group,key);
  350 + }
  351 +
  352 + RegCloseKey(hKey);
  353 + }
324 } 354 }
325 355
326 void set_integer_to_config(const gchar *group, const gchar *key, gint val) 356 void set_integer_to_config(const gchar *group, const gchar *key, gint val)
@@ -331,6 +361,21 @@ void set_integer_to_config(const gchar *group, const gchar *key, gint val) @@ -331,6 +361,21 @@ void set_integer_to_config(const gchar *group, const gchar *key, gint val)
331 { 361 {
332 g_key_file_set_integer(keyfile,group,key,val); 362 g_key_file_set_integer(keyfile,group,key,val);
333 } 363 }
  364 + else if(pw3270_win32_registry_open(group, &hKey, KEY_ALL_ACCESS))
  365 + {
  366 + DWORD value = val;
  367 + LONG rc = RegSetValueEx(hKey, key, 0, REG_DWORD,(const BYTE *) &value,sizeof(value));
  368 +
  369 + SetLastError(rc);
  370 +
  371 + if(rc != ERROR_SUCCESS)
  372 + {
  373 + g_autofree gchar *msg = g_win32_error_message(GetLastError());
  374 + g_warning("Error \"%s\" when setting key HKCU\\%s\\%s\\%s",msg,WINDOWS_REGISTRY_PATH,g_get_application_name(),group,key);
  375 + }
  376 +
  377 + RegCloseKey(hKey);
  378 + }
334 379
335 } 380 }
336 381
@@ -358,7 +403,6 @@ void pw3270_session_config_save() @@ -358,7 +403,6 @@ void pw3270_session_config_save()
358 g_file_set_contents(filename,text,-1,&error); 403 g_file_set_contents(filename,text,-1,&error);
359 } 404 }
360 405
361 -  
362 if(error) { 406 if(error) {
363 g_message( _( "Can't save session settings: %s" ), error->message); 407 g_message( _( "Can't save session settings: %s" ), error->message);
364 g_error_free(error); 408 g_error_free(error);
@@ -440,6 +484,23 @@ GKeyFile * pw3270_session_config_get(gboolean create) @@ -440,6 +484,23 @@ GKeyFile * pw3270_session_config_get(gboolean create)
440 return keyfile; 484 return keyfile;
441 } 485 }
442 486
  487 + void pw3270_session_save_terminal(GtkWidget *terminal)
  488 + {
  489 + HKEY hKey;
  490 +
  491 + if(keyfile)
  492 + {
  493 + v3270_to_key_file(terminal, keyfile, "terminal");
  494 + pw3270_session_config_save();
  495 + }
  496 + else if(pw3270_win32_registry_open(NULL, &hKey, KEY_ALL_ACCESS))
  497 + {
  498 + v3270_to_registry(terminal, hKey, "terminal");
  499 + RegCloseKey(hKey);
  500 + }
  501 +
  502 + }
  503 +
443 static const struct _WindowState 504 static const struct _WindowState
444 { 505 {
445 const char *name; 506 const char *name;
@@ -452,33 +513,121 @@ GKeyFile * pw3270_session_config_get(gboolean create) @@ -452,33 +513,121 @@ GKeyFile * pw3270_session_config_get(gboolean create)
452 { "Sticky", GDK_WINDOW_STATE_STICKY, gtk_window_stick } 513 { "Sticky", GDK_WINDOW_STATE_STICKY, gtk_window_stick }
453 }; 514 };
454 515
455 - void pw3270_session_save_terminal(GtkWidget *terminal) 516 + void save_window_state_to_config(const gchar *group, const gchar *key, GdkWindowState CurrentState)
456 { 517 {
  518 + size_t f;
457 HKEY hKey; 519 HKEY hKey;
458 520
459 - GKeyFile * keyfile = pw3270_session_config_get(FALSE);  
460 -  
461 - if(keyfile) 521 + if(keyfile)
462 { 522 {
463 - v3270_to_key_file(terminal, keyfile, "terminal");  
464 - pw3270_session_config_save();  
465 - } 523 + g_autofree gchar * id = g_strconcat(group,".",key,NULL);
466 524
  525 + for(f=0;f<G_N_ELEMENTS(WindowState);f++)
  526 + g_key_file_set_boolean(keyfile,id,WindowState[f].name,CurrentState & WindowState[f].flag);
467 527
468 - } 528 + }
  529 + else if(pw3270_win32_registry_open(group, &hKey, KEY_ALL_ACCESS))
  530 + {
  531 + for(f=0;f<G_N_ELEMENTS(WindowState);f++)
  532 + {
  533 + DWORD value = (CurrentState & WindowState[f].flag) ? 1 : 0;
  534 + RegSetValueEx(hKey, WindowState[f].name, 0, REG_DWORD,(const BYTE *) &value,sizeof(value));
  535 + }
469 536
470 - void save_window_state_to_config(const gchar *group, const gchar *key, GdkWindowState CurrentState)  
471 - { 537 + RegCloseKey(hKey);
  538 + }
472 539
473 } 540 }
474 541
475 void save_window_size_to_config(const gchar *group, const gchar *key, GtkWidget *hwnd) 542 void save_window_size_to_config(const gchar *group, const gchar *key, GtkWidget *hwnd)
476 { 543 {
  544 + HKEY hKey;
  545 +
  546 + int pos[2];
  547 + gtk_window_get_size(GTK_WINDOW(hwnd),&pos[0],&pos[1]);
  548 +
  549 + if(keyfile)
  550 + {
  551 + g_autofree gchar * id = g_strconcat(group,".",key,NULL);
  552 + g_key_file_set_integer_list(keyfile,id,"size",pos,2);
  553 + }
  554 + else if(pw3270_win32_registry_open(group, &hKey, KEY_ALL_ACCESS))
  555 + {
  556 + RegSetValueEx(hKey, "Size", 0, REG_BINARY,(const BYTE *) pos,sizeof(pos));
  557 + RegCloseKey(hKey);
  558 + }
477 559
478 } 560 }
479 561
480 void restore_window_from_config(const gchar *group, const gchar *key, GtkWidget *hwnd) 562 void restore_window_from_config(const gchar *group, const gchar *key, GtkWidget *hwnd)
481 { 563 {
  564 + HKEY hKey;
  565 + int f;
  566 +
  567 + if(keyfile)
  568 + {
  569 + g_autofree gchar * id = g_strconcat(group,".",key,NULL);
  570 +
  571 + if(g_key_file_has_key(keyfile,id,"size",NULL))
  572 + {
  573 + gsize sz = 2;
  574 + gint * vlr = g_key_file_get_integer_list(keyfile,id,"size",&sz,NULL);
  575 +
  576 + if(vlr)
  577 + {
  578 + gtk_window_resize(GTK_WINDOW(hwnd),vlr[0],vlr[1]);
  579 + g_free(vlr);
  580 + }
  581 +
  582 + }
  583 +
  584 + for(f=0;f<G_N_ELEMENTS(WindowState);f++)
  585 + {
  586 + if(g_key_file_get_boolean(keyfile,id,WindowState[f].name,NULL))
  587 + WindowState[f].activate(GTK_WINDOW(hwnd));
  588 + }
  589 +
  590 + }
  591 + else if(search_for_registry_key(group,key,&hKey) != REG_KEY_INEXISTENT)
  592 + {
  593 + int pos[2];
  594 + unsigned long datalen = sizeof(pos);
  595 + unsigned long datatype;
  596 +
  597 + if(RegQueryValueExA(hKey,"Size",NULL,&datatype,(BYTE *) pos,&datalen) == ERROR_SUCCESS)
  598 + {
  599 + if(datatype == REG_BINARY && datalen == sizeof(pos))
  600 + {
  601 + gtk_window_resize(GTK_WINDOW(hwnd),pos[0],pos[1]);
  602 + }
  603 + else
  604 + {
  605 + g_warning("Unexpected registry data in %s\\%s\\Size",group,key);
  606 + }
  607 + }
  608 +
  609 + for(f=0;f<G_N_ELEMENTS(WindowState);f++)
  610 + {
  611 + DWORD data;
  612 +
  613 + datalen = sizeof(data);
  614 +
  615 + if(RegQueryValueExA(hKey,WindowState[f].name,NULL,&datatype,(BYTE *) &data,&datalen) == ERROR_SUCCESS)
  616 + {
  617 + if(datatype == REG_DWORD)
  618 + {
  619 + if(data)
  620 + WindowState[f].activate(GTK_WINDOW(hwnd));
  621 + }
  622 + else
  623 + {
  624 + g_warning("Unexpected registry data type in %s\\%s\\%s",group,key,WindowState[f].name);
  625 + }
  626 + }
  627 + }
  628 +
  629 + RegCloseKey(hKey);
  630 + }
482 631
483 } 632 }
484 633