-
Notifications
You must be signed in to change notification settings - Fork 10
/
rc_gui.c
2282 lines (1929 loc) · 86.9 KB
/
rc_gui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2018 Raspberry Pi (Trading) Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* NOTE raspi-config nonint functions obey sh return codes - 0 is in general success / yes / selected, 1 is failed / no / not selected */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <libintl.h>
#include <crypt.h>
/* Command strings */
#define GET_PREFIX "raspi-config nonint "
#define SET_PREFIX "SUDO_ASKPASS=/usr/lib/rc-gui/pwdrcg.sh sudo -A " GET_PREFIX
#define GET_HOSTNAME GET_PREFIX "get_hostname"
#define SET_HOSTNAME SET_PREFIX "do_hostname %s"
#define GET_BOOT_CLI GET_PREFIX "get_boot_cli"
#define GET_AUTOLOGIN GET_PREFIX "get_autologin"
#define SET_BOOT_CLI SET_PREFIX "do_boot_behaviour B1"
#define SET_BOOT_CLIA SET_PREFIX "do_boot_behaviour B2"
#define SET_BOOT_GUI SET_PREFIX "do_boot_behaviour B3"
#define SET_BOOT_GUIA SET_PREFIX "do_boot_behaviour B4"
#define GET_SPLASH GET_PREFIX "get_boot_splash"
#define SET_SPLASH SET_PREFIX "do_boot_splash %d"
#define GET_OVERSCAN GET_PREFIX "get_overscan_kms 1"
#define GET_OVERSCAN2 GET_PREFIX "get_overscan_kms 2"
#define SET_OVERSCAN SET_PREFIX "do_overscan_kms 1 %d"
#define SET_OVERSCAN2 SET_PREFIX "do_overscan_kms 2 %d"
#define GET_CAMERA GET_PREFIX "get_camera"
#define SET_CAMERA SET_PREFIX "do_camera %d"
#define GET_SSH GET_PREFIX "get_ssh"
#define SET_SSH SET_PREFIX "do_ssh %d"
#define GET_RPC GET_PREFIX "get_rpi_connect"
#define SET_RPC SET_PREFIX "do_rpi_connect %d"
#define GET_VNC GET_PREFIX "get_vnc"
#define SET_VNC SET_PREFIX "do_vnc %d"
#define GET_SPI GET_PREFIX "get_spi"
#define SET_SPI SET_PREFIX "do_spi %d"
#define GET_I2C GET_PREFIX "get_i2c"
#define SET_I2C SET_PREFIX "do_i2c %d"
#define GET_SERIALCON GET_PREFIX "get_serial_cons"
#define GET_SERIALHW GET_PREFIX "get_serial_hw"
#define SET_SERIALCON SET_PREFIX "do_serial_cons %d"
#define SET_SERIALHW SET_PREFIX "do_serial_hw %d"
#define GET_1WIRE GET_PREFIX "get_onewire"
#define SET_1WIRE SET_PREFIX "do_onewire %d"
#define GET_RGPIO GET_PREFIX "get_rgpio"
#define SET_RGPIO SET_PREFIX "do_rgpio %d"
#define GET_BLANK GET_PREFIX "get_blanking"
#define SET_BLANK SET_PREFIX "do_blanking %d"
#define GET_LEDS GET_PREFIX "get_leds"
#define SET_LEDS SET_PREFIX "do_leds %d"
#define GET_USBI GET_PREFIX "get_usb_current"
#define SET_USBI SET_PREFIX "do_usb_current %d"
#define GET_PI_TYPE GET_PREFIX "get_pi_type"
#define IS_PI GET_PREFIX "is_pi"
#define IS_PI4 GET_PREFIX "is_pifour"
#define IS_PI5 GET_PREFIX "is_pifive"
#define IS_64BIT GET_PREFIX "is_64bit"
#define HAS_ANALOG GET_PREFIX "has_analog"
#define GET_OVERCLOCK GET_PREFIX "get_config_var arm_freq /boot/config.txt"
#define SET_OVERCLOCK SET_PREFIX "do_overclock %s"
#define GET_FAN GET_PREFIX "get_fan"
#define GET_FAN_GPIO GET_PREFIX "get_fan_gpio"
#define GET_FAN_TEMP GET_PREFIX "get_fan_temp"
#define SET_FAN SET_PREFIX "do_fan %d %d %d"
#define GET_HDMI_GROUP GET_PREFIX "get_config_var hdmi_group /boot/config.txt"
#define GET_HDMI_MODE GET_PREFIX "get_config_var hdmi_mode /boot/config.txt"
#define SET_HDMI_GP_MOD SET_PREFIX "do_resolution %d %d"
#define GET_WIFI_CTRY GET_PREFIX "get_wifi_country"
#define SET_WIFI_CTRY SET_PREFIX "do_wifi_country %s"
#define SET_PI4_4KH SET_PREFIX "do_pi4video V1"
#define SET_PI4_ATV SET_PREFIX "do_pi4video V2"
#define SET_PI4_NONE SET_PREFIX "do_pi4video V3"
#define GET_PI4_VID GET_PREFIX "get_pi4video"
#define GET_OVERLAYNOW GET_PREFIX "get_overlay_now"
#define GET_OVERLAY GET_PREFIX "get_overlay_conf"
#define GET_BOOTRO GET_PREFIX "get_bootro_conf"
#define SET_OFS_ON SET_PREFIX "enable_overlayfs"
#define SET_OFS_OFF SET_PREFIX "disable_overlayfs"
#define SET_BOOTP_RO SET_PREFIX "enable_bootro"
#define SET_BOOTP_RW SET_PREFIX "disable_bootro"
#define CHECK_UNAME GET_PREFIX "is_uname_current"
#define WLAN_INTERFACES GET_PREFIX "list_wlan_interfaces"
#define RVNC_INSTALLED GET_PREFIX "is_installed realvnc-vnc-server"
#define WVNC_INSTALLED GET_PREFIX "is_installed wayvnc"
#define XSCR_INSTALLED GET_PREFIX "is_installed xscreensaver"
#define GET_VNC_RES GET_PREFIX "get_vnc_resolution"
#define SET_VNC_RES SET_PREFIX "do_vnc_resolution %s"
#define CAN_CONFIGURE GET_PREFIX "can_configure"
#define SET_LOCALE SET_PREFIX "do_change_locale_rc_gui %s"
#define SET_TIMEZONE SET_PREFIX "do_change_timezone_rc_gui %s"
#define SET_KEYBOARD SET_PREFIX "do_change_keyboard_rc_gui %s"
#define GET_BROWSER GET_PREFIX "get_browser"
#define SET_BROWSER SET_PREFIX "do_browser %s"
#define FF_INSTALLED GET_PREFIX "is_installed firefox"
#define CR_INSTALLED GET_PREFIX "is_installed chromium"
#define RPC_INSTALLED GET_PREFIX "is_installed rpi-connect"
#define VKBD_INSTALLED GET_PREFIX "is_installed squeekboard"
#define GET_SQUEEK GET_PREFIX "get_squeekboard"
#define SET_SQUEEK SET_PREFIX "do_squeekboard S%d"
#define DEFAULT_GPU_MEM "vcgencmd get_mem gpu | cut -d = -f 2 | cut -d M -f 1"
#define CHANGE_PASSWD "echo $USER:'%s' | SUDO_ASKPASS=/usr/lib/rc-gui/pwdrcg.sh sudo -A chpasswd -e"
#define CONFIG_SWITCH(wid,name,var,cmd) wid = gtk_builder_get_object (builder, name); \
gtk_switch_set_active (GTK_SWITCH (wid), !(var = get_status (cmd)));
#define READ_SWITCH(wid,var,cmd,reb) if (var == gtk_switch_get_active (GTK_SWITCH (wid))) \
{ \
vsystem (cmd, (1 - var)); \
if (reb) reboot = 1; \
}
/* Controls */
static GObject *passwd_btn, *hostname_btn, *locale_btn, *timezone_btn, *keyboard_btn, *wifi_btn, *ofs_btn;
static GObject *boot_desktop_rb, *boot_cli_rb, *chromium_rb, *firefox_rb;
static GObject *overscan_sw, *overscan2_sw, *ssh_sw, *rgpio_sw, *vnc_sw;
static GObject *spi_sw, *i2c_sw, *serial_sw, *onewire_sw, *usb_sw, *squeek_cb;
static GObject *alogin_sw, *splash_sw, *scons_sw;
static GObject *blank_sw, *led_actpwr_sw, *fan_sw;
static GObject *overclock_cb, *hostname_tb, *ofs_en_sw, *bp_ro_sw, *ofs_lbl;
static GObject *fan_gpio_sb, *fan_temp_sb, *vnc_res_cb;
static GObject *pwentry1_tb, *pwentry2_tb, *pwok_btn;
static GObject *hostname_tb;
static GObject *tzarea_cb, *tzloc_cb, *wccountry_cb;
static GObject *loclang_cb, *loccount_cb, *locchar_cb;
static GObject *keymodel_cb, *keylayout_cb, *keyvar_cb, *keyalayout_cb, *keyavar_cb, *keyshort_cb, *keyled_cb, *keyalt_btn;
static GObject *keybox5, *keybox6, *keybox7, *keybox8;
static GtkWidget *main_dlg, *msg_dlg;
/* Initial values */
static int orig_boot, orig_overscan, orig_overscan2, orig_ssh, orig_spi, orig_i2c, orig_serial, orig_scons, orig_splash;
static int orig_clock, orig_autolog, orig_onewire, orig_rgpio, orig_vnc, orig_usbi, orig_squeek;
static int orig_ofs, orig_bpro, orig_blank, orig_leds, orig_fan, orig_fan_gpio, orig_fan_temp, orig_vnc_res;
static char *vres, *orig_browser;
/* Reboot flag set after locale change */
static int needs_reboot, ovfs_rb;
static int alt_keys;
/* Window manager in use */
typedef enum {
WM_OPENBOX,
WM_WAYFIRE,
WM_LABWC } wm_type;
static wm_type wm;
/* Globals accessed from multiple threads */
static char gbuffer[512];
GThread *pthread;
gulong draw_id;
/* Lists for keyboard setting */
GtkListStore *model_list, *layout_list, *variant_list, *avariant_list, *toggle_list, *led_list;
/* List for locale setting */
GtkListStore *locale_list, *country_list, *charset_list;
#define LOC_NAME 0
#define LOC_LCODE 1
#define LOC_CCODE 2
#define LOC_LCCODE 3
GtkListStore *timezone_list, *tzcity_list;
#define TZ_NAME 0
#define TZ_PATH 1
#define TZ_AREA 2
/* Helpers */
static int vsystem (const char *fmt, ...)
{
char *cmdline;
int res;
va_list arg;
va_start (arg, fmt);
g_vasprintf (&cmdline, fmt, arg);
va_end (arg);
res = system (cmdline);
g_free (cmdline);
return res;
}
static int get_status (char *cmd)
{
FILE *fp = popen (cmd, "r");
char *buf = NULL;
size_t res = 0;
int val = 0;
if (fp == NULL) return 0;
if (getline (&buf, &res, fp) > 0)
{
if (sscanf (buf, "%zu", &res) == 1)
{
val = res;
}
}
pclose (fp);
g_free (buf);
return val;
}
static char *get_string (char *cmd)
{
char *line = NULL, *res = NULL;
size_t len = 0;
FILE *fp = popen (cmd, "r");
if (fp == NULL) return NULL;
if (getline (&line, &len, fp) > 0)
{
res = line;
while (*res)
{
if (g_ascii_isspace (*res)) *res = 0;
res++;
}
res = g_strdup (line);
}
pclose (fp);
g_free (line);
return res;
}
static char *get_quoted_param (char *path, char *fname, char *toseek)
{
char *pathname, *linebuf, *cptr, *dptr, *res;
size_t len;
pathname = g_strdup_printf ("%s/%s", path, fname);
FILE *fp = fopen (pathname, "rb");
g_free (pathname);
if (!fp) return NULL;
linebuf = NULL;
len = 0;
while (getline (&linebuf, &len, fp) > 0)
{
// skip whitespace at line start
cptr = linebuf;
while (*cptr == ' ' || *cptr == '\t') cptr++;
// compare against string to find
if (!strncmp (cptr, toseek, strlen (toseek)))
{
// find string in quotes
strtok (cptr, "\"");
dptr = strtok (NULL, "\"\n\r");
// copy to dest
if (dptr) res = g_strdup (dptr);
else res = NULL;
// done
g_free (linebuf);
fclose (fp);
return res;
}
}
// end of file with no match
g_free (linebuf);
fclose (fp);
return NULL;
}
static void message (char *msg)
{
GtkWidget *wid;
GtkBuilder *builder = gtk_builder_new_from_file (PACKAGE_DATA_DIR "/rc_gui.ui");
msg_dlg = (GtkWidget *) gtk_builder_get_object (builder, "modal");
if (main_dlg) gtk_window_set_transient_for (GTK_WINDOW (msg_dlg), GTK_WINDOW (main_dlg));
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_msg");
gtk_label_set_text (GTK_LABEL (wid), msg);
gtk_widget_show (msg_dlg);
g_object_unref (builder);
}
static gboolean ok_clicked (GtkButton *button, gpointer data)
{
gtk_widget_destroy (msg_dlg);
if (!main_dlg) gtk_main_quit ();
return FALSE;
}
static void info (char *msg)
{
GtkWidget *wid;
GtkBuilder *builder = gtk_builder_new_from_file (PACKAGE_DATA_DIR "/rc_gui.ui");
msg_dlg = (GtkWidget *) gtk_builder_get_object (builder, "modal");
if (main_dlg) gtk_window_set_transient_for (GTK_WINDOW (msg_dlg), GTK_WINDOW (main_dlg));
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_msg");
gtk_label_set_text (GTK_LABEL (wid), msg);
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_ok");
g_signal_connect (wid, "clicked", G_CALLBACK (ok_clicked), NULL);
gtk_widget_show (wid);
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_buttons");
gtk_widget_show (wid);
gtk_widget_show (msg_dlg);
g_object_unref (builder);
}
static gboolean close_app (GtkButton *button, gpointer data)
{
gtk_widget_destroy (msg_dlg);
gtk_main_quit ();
return FALSE;
}
static gboolean close_app_reboot (GtkButton *button, gpointer data)
{
gtk_widget_destroy (msg_dlg);
gtk_main_quit ();
vsystem ("reboot");
return FALSE;
}
static gboolean reboot_prompt (gpointer data)
{
GtkWidget *wid;
GtkBuilder *builder = gtk_builder_new_from_file (PACKAGE_DATA_DIR "/rc_gui.ui");
msg_dlg = (GtkWidget *) gtk_builder_get_object (builder, "modal");
gtk_window_set_transient_for (GTK_WINDOW (msg_dlg), GTK_WINDOW (main_dlg));
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_msg");
gtk_label_set_text (GTK_LABEL (wid), _("The changes you have made require the Raspberry Pi to be rebooted to take effect.\n\nWould you like to reboot now? "));
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_cancel");
gtk_button_set_label (GTK_BUTTON (wid), _("_No"));
g_signal_connect (wid, "clicked", G_CALLBACK (close_app), NULL);
gtk_widget_show (wid);
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_ok");
gtk_button_set_label (GTK_BUTTON (wid), _("_Yes"));
g_signal_connect (wid, "clicked", G_CALLBACK (close_app_reboot), NULL);
gtk_widget_show (wid);
wid = (GtkWidget *) gtk_builder_get_object (builder, "modal_buttons");
gtk_widget_show (wid);
gtk_widget_show (msg_dlg);
g_object_unref (builder);
return FALSE;
}
/* Password setting */
static void set_passwd (GtkEntry *entry, gpointer ptr)
{
if (strlen (gtk_entry_get_text (GTK_ENTRY (pwentry1_tb))) && g_strcmp0 (gtk_entry_get_text (GTK_ENTRY (pwentry1_tb)),
gtk_entry_get_text (GTK_ENTRY(pwentry2_tb))))
gtk_widget_set_sensitive (GTK_WIDGET (pwok_btn), FALSE);
else
gtk_widget_set_sensitive (GTK_WIDGET (pwok_btn), TRUE);
}
static void on_change_passwd (GtkButton* btn, gpointer ptr)
{
GtkBuilder *builder;
GtkWidget *dlg;
int res;
builder = gtk_builder_new_from_file (PACKAGE_DATA_DIR "/rc_gui.ui");
dlg = (GtkWidget *) gtk_builder_get_object (builder, "passwddlg");
gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (main_dlg));
pwentry1_tb = gtk_builder_get_object (builder, "pwentry1");
pwentry2_tb = gtk_builder_get_object (builder, "pwentry2");
gtk_entry_set_visibility (GTK_ENTRY (pwentry1_tb), FALSE);
gtk_entry_set_visibility (GTK_ENTRY (pwentry2_tb), FALSE);
g_signal_connect (pwentry1_tb, "changed", G_CALLBACK (set_passwd), NULL);
g_signal_connect (pwentry2_tb, "changed", G_CALLBACK (set_passwd), NULL);
pwok_btn = gtk_builder_get_object (builder, "passwdok");
gtk_widget_set_sensitive (GTK_WIDGET (pwok_btn), FALSE);
if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_OK)
{
res = vsystem (CHANGE_PASSWD, crypt (gtk_entry_get_text (GTK_ENTRY (pwentry1_tb)), crypt_gensalt (NULL, 0, NULL, 0)));
gtk_widget_destroy (dlg);
if (res)
info (_("The password change failed.\n\nThis could be because the current password was incorrect, or because the new password was not sufficiently complex or was too similar to the current password."));
else
info (_("The password has been changed successfully."));
}
else gtk_widget_destroy (dlg);
g_object_unref (builder);
}
/* Hostname setting */
static void on_change_hostname (GtkButton* btn, gpointer ptr)
{
GtkBuilder *builder;
GtkWidget *dlg;
int res;
const char *new_hn, *cptr;
char *orig_hn;
builder = gtk_builder_new_from_file (PACKAGE_DATA_DIR "/rc_gui.ui");
dlg = (GtkWidget *) gtk_builder_get_object (builder, "hostnamedlg");
gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (main_dlg));
hostname_tb = gtk_builder_get_object (builder, "hnentry1");
orig_hn = get_string (GET_HOSTNAME);
gtk_entry_set_text (GTK_ENTRY (hostname_tb), orig_hn);
if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_OK)
{
new_hn = gtk_entry_get_text (GTK_ENTRY (hostname_tb));
if (g_strcmp0 (orig_hn, new_hn))
{
res = 1;
cptr = new_hn;
if (*cptr == 0 || *cptr == '-') res = 0;
else while (*cptr)
{
if (!(*cptr >= '0' && *cptr <= '9') && !(*cptr >= 'a' && *cptr <= 'z') && !(*cptr >= 'A' && *cptr <= 'Z') && *cptr != '-')
{
res = 0;
break;
}
cptr++;
}
if (res && *(cptr - 1) == '-') res = 0;
if (res)
{
vsystem (SET_HOSTNAME, new_hn);
gtk_widget_destroy (dlg);
info (_("The hostname has been changed successfully and will take effect on the next reboot."));
needs_reboot = 1;
}
else
{
gtk_widget_destroy (dlg);
info (_("The hostname change failed.\n\nThe hostname must only contain the characters A-Z, a-z, 0-9 and hyphen.\nThe first and last character may not be the hyphen."));
}
}
else gtk_widget_destroy (dlg);
}
else gtk_widget_destroy (dlg);
g_free (orig_hn);
g_object_unref (builder);
}
/* Locale setting */
static gboolean unique_rows (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
{
GtkTreeIter next = *iter;
char *str1, *str2;
gboolean res;
if (!gtk_tree_model_iter_next (model, &next)) return TRUE;
gtk_tree_model_get (model, iter, (int) data, &str1, -1);
gtk_tree_model_get (model, &next, (int) data, &str2, -1);
if (!g_strcmp0 (str1, str2)) res = FALSE;
else res = TRUE;
g_free (str1);
g_free (str2);
return res;
}
static gboolean match_lang (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
{
char *str;
gboolean res;
gtk_tree_model_get (model, iter, LOC_LCODE, &str, -1);
if (!g_strcmp0 (str, (char *) data)) res = TRUE;
else res = FALSE;
g_free (str);
return res;
}
static gboolean match_country (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
{
char *str;
gboolean res;
gtk_tree_model_get (model, iter, LOC_CCODE, &str, -1);
if (!g_strcmp0 (str, (char *) data)) res = TRUE;
else res = FALSE;
g_free (str);
return res;
}
static void set_init (GtkTreeModel *model, GObject *cb, int pos, char *init)
{
GtkTreeIter iter;
char *val;
gtk_tree_model_get_iter_first (model, &iter);
if (!init) gtk_combo_box_set_active_iter (GTK_COMBO_BOX (cb), &iter);
else
{
while (1)
{
gtk_tree_model_get (model, &iter, pos, &val, -1);
if (!g_strcmp0 (init, val))
{
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (cb), &iter);
g_free (val);
return;
}
g_free (val);
if (!gtk_tree_model_iter_next (model, &iter)) break;
}
}
// couldn't match - just choose the first option - should never happen, but...
gtk_tree_model_get_iter_first (model, &iter);
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (cb), &iter);
}
static void set_init_sub (GtkTreeModel *model, GObject *cb, int pos, char *init)
{
GtkTreeIter iter;
char *val;
gtk_tree_model_get_iter_first (model, &iter);
if (!init || *init == 0) gtk_combo_box_set_active_iter (GTK_COMBO_BOX (cb), &iter);
else
{
while (1)
{
if (!gtk_tree_model_iter_next (model, &iter)) break;
gtk_tree_model_get (model, &iter, pos, &val, -1);
if (strstr (init, val))
{
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (cb), &iter);
g_free (val);
return;
}
g_free (val);
}
}
// couldn't match - just choose the first option - should never happen, but...
gtk_tree_model_get_iter_first (model, &iter);
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (cb), &iter);
}
static void deunicode (char **str)
{
if (*str && strchr (*str, '<'))
{
int val;
char *tmp = g_strdup (*str);
char *pos = strchr (tmp, '<');
if (sscanf (pos, "<U00%X>", &val) == 1)
{
*pos++ = val >= 0xC0 ? 0xC3 : 0xC2;
*pos++ = val >= 0xC0 ? val - 0x40 : val;
sprintf (pos, "%s", strchr (*str, '>') + 1);
g_free (*str);
*str = tmp;
}
}
}
static void read_locales (void)
{
char *cname, *lname, *buffer, *lang, *country, *charset, *loccode, *flname, *fcname;
GtkTreeIter iter;
FILE *fp;
size_t len;
// populate the locale database
buffer = NULL;
len = 0;
fp = fopen ("/usr/share/i18n/SUPPORTED", "rb");
while (getline (&buffer, &len, fp) > 0)
{
// split into l/c pair and charset
loccode = strtok (buffer, " ");
charset = strtok (NULL, " \t\n\r");
if (loccode && charset)
{
// strip any extension
lang = g_strdup (loccode);
strtok (lang, ".");
// lang now holds locale file name - read names from locale file
cname = get_quoted_param ("/usr/share/i18n/locales", lang, "territory");
lname = get_quoted_param ("/usr/share/i18n/locales", lang, "language");
if (!lname && !cname)
{
g_free (lang);
continue;
}
// deal with the likes of "malta"...
if (cname) cname[0] = g_ascii_toupper (cname[0]);
if (lname) lname[0] = g_ascii_toupper (lname[0]);
// deal with Curacao and Bokmal
deunicode (&cname);
deunicode (&lname);
// now split to language and country codes
strtok (lang, "_");
country = strtok (NULL, " \t\n\r");
flname = g_strdup_printf ("%s (%s)", lang, lname);
// purely to deal with esperanto - a language without a country, which is a clue as to just how pointless it is...
if (country)
fcname = g_strdup_printf ("%s (%s)", country, cname);
else
fcname = NULL;
gtk_list_store_append (locale_list, &iter);
gtk_list_store_set (locale_list, &iter, LOC_NAME, flname, LOC_LCODE, lang, -1);
gtk_list_store_append (country_list, &iter);
gtk_list_store_set (country_list, &iter, LOC_NAME, fcname, LOC_LCODE, lang, LOC_CCODE, country, -1);
gtk_list_store_append (charset_list, &iter);
gtk_list_store_set (charset_list, &iter, LOC_NAME, charset, LOC_LCODE, lang, LOC_CCODE, country, LOC_LCCODE, loccode, -1);
g_free (cname);
g_free (lname);
g_free (lang);
g_free (flname);
g_free (fcname);
}
}
fclose (fp);
g_free (buffer);
}
static void country_changed (GtkComboBox *cb, char *ptr)
{
GtkTreeModel *model;
GtkTreeModelFilter *f1, *f2;
GtkTreeModelSort *schar;
GtkTreeIter iter;
char *lstr = NULL, *cstr = NULL;
// get the current language code from the combo box
model = gtk_combo_box_get_model (GTK_COMBO_BOX (loclang_cb));
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (loclang_cb), &iter))
gtk_tree_model_get (model, &iter, LOC_LCODE, &lstr, -1);
// get the current country code from the combo box
model = gtk_combo_box_get_model (GTK_COMBO_BOX (loccount_cb));
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (loccount_cb), &iter))
gtk_tree_model_get (model, &iter, LOC_CCODE, &cstr, -1);
// filter and sort the master database for entries matching this code
f1 = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (charset_list), NULL));
gtk_tree_model_filter_set_visible_func (f1, (GtkTreeModelFilterVisibleFunc) match_lang, lstr, NULL);
f2 = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (f1), NULL));
gtk_tree_model_filter_set_visible_func (f2, (GtkTreeModelFilterVisibleFunc) match_country, cstr, NULL);
schar = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (f2)));
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (schar), LOC_NAME, GTK_SORT_ASCENDING);
// set up the combo box from the sorted and filtered list
gtk_combo_box_set_model (GTK_COMBO_BOX (locchar_cb), GTK_TREE_MODEL (schar));
if (ptr == NULL) gtk_combo_box_set_active (GTK_COMBO_BOX (locchar_cb), 0);
else set_init (GTK_TREE_MODEL (schar), locchar_cb, LOC_LCCODE, ptr);
g_object_unref (f1);
g_object_unref (f2);
g_object_unref (schar);
g_free (lstr);
g_free (cstr);
}
static void language_changed (GtkComboBox *cb, char *ptr)
{
GtkTreeModel *model;
GtkTreeModelFilter *f1, *f2;
GtkTreeModelSort *scount;
GtkTreeIter iter;
char *lstr = NULL, *init, *init_count;
// get the current language code from the combo box
model = gtk_combo_box_get_model (GTK_COMBO_BOX (loclang_cb));
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (loclang_cb), &iter))
gtk_tree_model_get (model, &iter, LOC_LCODE, &lstr, -1);
// filter and sort the master database for entries matching this code
f1 = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (country_list), NULL));
gtk_tree_model_filter_set_visible_func (f1, (GtkTreeModelFilterVisibleFunc) match_lang, lstr, NULL);
f2 = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (f1), NULL));
gtk_tree_model_filter_set_visible_func (f2, (GtkTreeModelFilterVisibleFunc) unique_rows, (void *) LOC_CCODE, NULL);
scount = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (f2)));
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (scount), LOC_CCODE, GTK_SORT_ASCENDING);
// set up the combo box from the sorted and filtered list
gtk_combo_box_set_model (GTK_COMBO_BOX (loccount_cb), GTK_TREE_MODEL (scount));
if (ptr == NULL) gtk_combo_box_set_active (GTK_COMBO_BOX (loccount_cb), 0);
else
{
// parse the initial locale for a country code
init = g_strdup (ptr);
strtok (init, "_");
init_count = strtok (NULL, " .");
set_init (GTK_TREE_MODEL (scount), loccount_cb, LOC_CCODE, init_count);
g_free (init);
}
// disable the combo box if it has no entries
gtk_widget_set_sensitive (GTK_WIDGET (loccount_cb), gtk_tree_model_iter_n_children (GTK_TREE_MODEL (scount), NULL) > 1);
g_object_unref (f1);
g_object_unref (f2);
g_object_unref (scount);
g_free (lstr);
country_changed (GTK_COMBO_BOX (loccount_cb), ptr);
}
static gboolean close_msg (gpointer data)
{
gtk_widget_destroy (GTK_WIDGET (msg_dlg));
return FALSE;
}
static gpointer locale_thread (gpointer data)
{
vsystem (SET_LOCALE, gbuffer);
g_idle_add (close_msg, NULL);
return NULL;
}
static void on_set_locale (GtkButton* btn, gpointer ptr)
{
GtkBuilder *builder;
GtkWidget *dlg;
GtkCellRenderer *col;
GtkTreeModel *model;
GtkTreeModelSort *slang;
GtkTreeModelFilter *flang;
GtkTreeIter iter;
char *buffer, *init_locale = NULL;
// create and populate the locale database
locale_list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
country_list = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
charset_list = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
read_locales ();
// create the dialog
builder = gtk_builder_new_from_file (PACKAGE_DATA_DIR "/rc_gui.ui");
dlg = (GtkWidget *) gtk_builder_get_object (builder, "localedlg");
gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (main_dlg));
// create the combo boxes
loclang_cb = (GObject *) gtk_builder_get_object (builder, "loccblang");
loccount_cb = (GObject *) gtk_builder_get_object (builder, "loccbcountry");
locchar_cb = (GObject *) gtk_builder_get_object (builder, "loccbchar");
col = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (loclang_cb), col, FALSE);
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (loclang_cb), col, "text", LOC_NAME);
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (loccount_cb), col, FALSE);
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (loccount_cb), col, "text", LOC_NAME);
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (locchar_cb), col, FALSE);
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (locchar_cb), col, "text", LOC_NAME);
// get the current locale setting and save as init_locale
init_locale = get_string ("grep LANG= /etc/default/locale | cut -d = -f 2");
if (init_locale == NULL) init_locale = g_strdup ("en_GB.UTF-8");
// filter and sort the master database
slang = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (locale_list)));
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (slang), LOC_LCODE, GTK_SORT_ASCENDING);
flang = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (slang), NULL));
gtk_tree_model_filter_set_visible_func (flang, (GtkTreeModelFilterVisibleFunc) unique_rows, (void *) LOC_LCODE, NULL);
// set up the language combo box from the sorted and filtered language list
gtk_combo_box_set_model (GTK_COMBO_BOX (loclang_cb), GTK_TREE_MODEL (flang));
buffer = g_strdup (init_locale);
strtok (buffer, "_");
set_init (GTK_TREE_MODEL (flang), loclang_cb, LOC_LCODE, buffer);
g_free (buffer);
// set the other combo boxes accordingly
language_changed (GTK_COMBO_BOX (loclang_cb), init_locale);
g_signal_connect (loccount_cb, "changed", G_CALLBACK (country_changed), NULL);
g_signal_connect (loclang_cb, "changed", G_CALLBACK (language_changed), NULL);
g_object_unref (builder);
if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_OK)
{
// get the current charset code from the combo box
model = gtk_combo_box_get_model (GTK_COMBO_BOX (locchar_cb));
gtk_combo_box_get_active_iter (GTK_COMBO_BOX (locchar_cb), &iter);
gtk_tree_model_get (model, &iter, LOC_LCCODE, &buffer, -1);
gtk_widget_destroy (dlg);
if (g_strcmp0 (buffer, init_locale))
{
// warn about a short delay...
message (_("Setting locale - please wait..."));
strcpy (gbuffer, buffer);
// launch a thread with the system call to update the generated locales
g_thread_new (NULL, locale_thread, NULL);
// set reboot flag
needs_reboot = 1;
}
g_free (buffer);
}
else gtk_widget_destroy (dlg);
g_free (init_locale);
g_object_unref (locale_list);
g_object_unref (country_list);
g_object_unref (charset_list);
g_object_unref (flang);
g_object_unref (slang);
}
/* Timezone setting */
int tzfilter (const struct dirent *entry)
{
if (entry->d_name[0] >= 'A' && entry->d_name[0] <= 'Z') return 1;
return 0;
}
static gboolean match_area (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
{
char *str;
gboolean res;
gtk_tree_model_get (model, iter, TZ_AREA, &str, -1);
if (!g_strcmp0 (str, (char *) data)) res = TRUE;
else res = FALSE;
g_free (str);
return res;
}
static void area_changed (GtkComboBox *cb, gpointer ptr)
{
GtkTreeModel *model;
GtkTreeModelFilter *far;
GtkTreeModelSort *sar;
GtkTreeIter iter;
char *str = NULL;
// get the current area from the combo box
model = gtk_combo_box_get_model (GTK_COMBO_BOX (tzarea_cb));
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (tzarea_cb), &iter))
gtk_tree_model_get (model, &iter, TZ_NAME, &str, -1);
// filter and sort the master database for entries matching this code
far = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (GTK_TREE_MODEL (tzcity_list), NULL));
gtk_tree_model_filter_set_visible_func (far, (GtkTreeModelFilterVisibleFunc) match_area, str, NULL);
sar = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (far)));
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sar), TZ_AREA, GTK_SORT_ASCENDING);
// set up the combo box from the sorted and filtered list
gtk_combo_box_set_model (GTK_COMBO_BOX (tzloc_cb), GTK_TREE_MODEL (sar));
if (!ptr) gtk_combo_box_set_active (GTK_COMBO_BOX (tzloc_cb), 0);
else set_init (GTK_TREE_MODEL (sar), tzloc_cb, TZ_PATH, ptr);
// disable the combo box if it has no entries
gtk_widget_set_sensitive (GTK_WIDGET (tzloc_cb), gtk_tree_model_iter_n_children (GTK_TREE_MODEL (sar), NULL) > 1);
g_object_unref (far);
g_object_unref (sar);
g_free (str);
}
static gpointer timezone_thread (gpointer data)
{
vsystem (SET_TIMEZONE, gbuffer);
g_idle_add (close_msg, NULL);
return NULL;
}
static void read_timezones (void)
{
char *buffer, *path, *area, *zone, *cptr;
GtkTreeIter iter;
struct dirent **filelist, *dp, **sfilelist, *sdp, **ssfilelist, *ssdp;
int entries, entry, sentries, sentry, ssentries, ssentry;
entries = scandir ("/usr/share/zoneinfo", &filelist, tzfilter, alphasort);
for (entry = 0; entry < entries; entry++)
{
dp = filelist[entry];
if (dp->d_type == DT_DIR)
{
buffer = g_strdup_printf ("/usr/share/zoneinfo/%s", dp->d_name);
sentries = scandir (buffer, &sfilelist, tzfilter, alphasort);
g_free (buffer);
for (sentry = 0; sentry < sentries; sentry++)
{
sdp = sfilelist[sentry];
if (sdp->d_type == DT_DIR)
{
buffer = g_strdup_printf ("/usr/share/zoneinfo/%s/%s", dp->d_name, sdp->d_name);
ssentries = scandir (buffer, &ssfilelist, tzfilter, alphasort);
g_free (buffer);
for (ssentry = 0; ssentry < ssentries; ssentry++)
{
ssdp = ssfilelist[ssentry];
path = g_strdup_printf ("%s/%s/%s", dp->d_name, sdp->d_name, ssdp->d_name);
area = g_strdup_printf ("%s/%s", dp->d_name, sdp->d_name);
zone = g_strdup_printf ("%s", ssdp->d_name);
cptr = zone;
while (*cptr++) if (*cptr == '_') *cptr = ' ';
cptr = area;
while (*cptr++) if (*cptr == '_') *cptr = ' ';
gtk_list_store_append (timezone_list, &iter);
gtk_list_store_set (timezone_list, &iter, TZ_PATH, path, TZ_NAME, area, -1);
gtk_list_store_append (tzcity_list, &iter);
gtk_list_store_set (tzcity_list, &iter, TZ_PATH, path, TZ_AREA, area, TZ_NAME, zone, -1);
g_free (path);
g_free (area);
g_free (zone);
}
}
else
{
path = g_strdup_printf ("%s/%s", dp->d_name, sdp->d_name);
zone = g_strdup_printf ("%s", sdp->d_name);
cptr = zone;
while (*cptr++) if (*cptr == '_') *cptr = ' ';