forked from amule-project/amule
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPreferences.cpp
More file actions
2833 lines (2531 loc) · 96.3 KB
/
Copy pathPreferences.cpp
File metadata and controls
2833 lines (2531 loc) · 96.3 KB
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
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2026 aMule Team ( https://amule-org.github.io )
// Copyright (c) 2002-2011 Merkur ( [email protected] / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include <wx/wx.h>
#include "Preferences.h"
#include <protocol/ed2k/Constants.h>
#include <common/Constants.h>
#include <common/DataFileVersion.h>
#include <common/Path.h> // Needed for StripSeparators (path-mapping prefixes)
#include <wx/config.h>
#include <wx/dir.h>
#include <wx/regex.h> // Needed for wxRegEx (shared-file exclusion filter)
#include <wx/stdpaths.h>
#include <wx/stopwatch.h>
#include <wx/tokenzr.h>
#include <wx/utils.h> // Needed for wxBusyCursor
#include "amule.h"
#include "FileArea.h" // Needed to push MMapEnabled into CFileArea
#include "config.h" // Needed for PACKAGE_STRING
#include "CFile.h"
#include <common/FileFunctions.h> // CDirIterator for recursive-root walk
#include <common/MD5Sum.h>
#include <set> // reconcile sets in ReloadSharedFolders
#include "Logger.h"
#include <common/Format.h> // Needed for CFormat
#include <common/TextFile.h> // Needed for CTextFile
#include <common/ClientVersion.h>
#include "UserEvents.h"
#ifndef AMULE_DAEMON
#include <wx/valgen.h>
#include "muuli_wdr.h"
#include "StatisticsDlg.h"
#include "MuleColour.h"
#endif
#ifndef CLIENT_GUI
#include "RandomFunctions.h"
#include "PlatformSpecific.h" // Needed for PlatformSpecific::GetMaxConnections()
#include "SharedFileList.h" // Needed for theApp->sharedfiles->Reload()
#endif
// Needed for IP filtering prefs
#include "ClientList.h"
#include "ServerList.h"
#include "GuiEvents.h"
#define DEFAULT_TCP_PORT 4662
#define DEFAULT_UDP_PORT 4672
// Static variables
unsigned long CPreferences::s_colors[cntStatColors];
unsigned long CPreferences::s_colors_ref[cntStatColors];
CPreferences::CFGMap CPreferences::s_CfgList;
CPreferences::CFGList CPreferences::s_MiscList;
wxString CPreferences::s_configDir;
bool CPreferences::s_firstRun = false;
bool CPreferences::s_firstRunWizardDone = false;
/* Proxy */
CProxyData CPreferences::s_ProxyData;
/* The rest, organize it! */
wxString CPreferences::s_nick;
Cfg_Lang_Base *CPreferences::s_cfgLang;
uint32 CPreferences::s_maxupload;
uint32 CPreferences::s_maxdownload;
uint32 CPreferences::s_slotallocation;
wxString CPreferences::s_Addr;
wxString CPreferences::s_NetworkInterface;
uint16 CPreferences::s_port;
uint16 CPreferences::s_udpport;
bool CPreferences::s_UDPEnable;
uint16 CPreferences::s_maxconnections;
bool CPreferences::s_reconnect;
bool CPreferences::s_autoconnect;
bool CPreferences::s_autoconnectstaticonly;
bool CPreferences::s_UPnPEnabled;
bool CPreferences::s_UPnPECEnabled;
bool CPreferences::s_UPnPWebServerEnabled;
uint16 CPreferences::s_UPnPTCPPort;
bool CPreferences::s_UPnPAvailable = false;
bool CPreferences::s_autoserverlist;
bool CPreferences::s_deadserver;
CPath CPreferences::s_incomingdir;
CPath CPreferences::s_tempdir;
bool CPreferences::s_ICH;
uint8 CPreferences::s_depth3D;
bool CPreferences::s_scorsystem;
bool CPreferences::s_hideonclose;
bool CPreferences::s_appimageIntegrationDeclined;
bool CPreferences::s_mintotray;
bool CPreferences::s_notify;
bool CPreferences::s_rememberSearchHistory;
bool CPreferences::s_trayiconenabled;
bool CPreferences::s_addnewfilespaused;
bool CPreferences::s_addserversfromserver;
bool CPreferences::s_addserversfromclient;
uint16 CPreferences::s_maxsourceperfile;
uint16 CPreferences::s_trafficOMeterInterval;
uint16 CPreferences::s_statsInterval;
uint32 CPreferences::s_maxGraphDownloadRate;
uint32 CPreferences::s_maxGraphUploadRate;
bool CPreferences::s_confirmExit;
bool CPreferences::s_filterLanIP;
bool CPreferences::s_paranoidfilter;
bool CPreferences::s_IPFilterSys;
bool CPreferences::s_onlineSig;
uint16 CPreferences::s_OSUpdate;
wxString CPreferences::s_languageID;
uint8 CPreferences::s_iSeeShares;
uint8 CPreferences::s_iToolDelayTime;
uint8 CPreferences::s_splitterbarPosition;
uint16 CPreferences::s_deadserverretries;
uint64 CPreferences::s_dwServerKeepAliveTimeoutMins;
uint8 CPreferences::s_statsMax;
uint8 CPreferences::s_statsAverageMinutes;
bool CPreferences::s_bpreviewprio;
bool CPreferences::s_smartidcheck;
uint8 CPreferences::s_smartidstate;
bool CPreferences::s_safeServerConnect;
bool CPreferences::s_Endgame;
bool CPreferences::s_startMinimized;
uint16 CPreferences::s_MaxConperFive;
uint16 CPreferences::s_kadMaxSourceSearches;
uint64 CPreferences::s_kadSourceReaskMins;
uint64 CPreferences::s_sourceReaskMins;
bool CPreferences::s_checkDiskspace;
uint32 CPreferences::s_uMinFreeDiskSpace;
wxString CPreferences::s_yourHostname;
bool CPreferences::s_bVerbose;
bool CPreferences::s_bVerboseLogfile;
bool CPreferences::s_bmanualhighprio;
bool CPreferences::s_bstartnextfile;
bool CPreferences::s_bstartnextfilesame;
bool CPreferences::s_bstartnextfilealpha;
bool CPreferences::s_bshowoverhead;
bool CPreferences::s_bDAP;
bool CPreferences::s_bUAP;
#ifndef __GIT__
bool CPreferences::s_showVersionOnTitle;
#endif
uint8_t CPreferences::s_showRatesOnTitle;
wxString CPreferences::s_VideoPlayer;
bool CPreferences::s_showAllNotCats;
bool CPreferences::s_msgonlyfriends;
bool CPreferences::s_msgsecure;
uint8 CPreferences::s_filterlevel;
uint8 CPreferences::s_iFileBufferSize;
uint8 CPreferences::s_iQueueSize;
wxString CPreferences::s_datetimeformat;
wxString CPreferences::s_sWebPath;
wxString CPreferences::s_sWebPassword;
wxString CPreferences::s_sWebLowPassword;
uint16 CPreferences::s_nWebPort;
uint16 CPreferences::s_nWebUPnPTCPPort;
bool CPreferences::s_bWebEnabled;
bool CPreferences::s_bWebUseGzip;
bool CPreferences::s_bAmuleApiEnabled;
uint16 CPreferences::s_nAmuleApiPort;
wxString CPreferences::s_sAmuleApiBindAddress;
wxString CPreferences::s_sAmuleApiPassword;
wxString CPreferences::s_sAmuleApiGuestPassword;
bool CPreferences::s_bAmuleApiGuestEnabled;
bool CPreferences::s_bAmuleApiAdminIsSet;
wxString CPreferences::s_sAmuleApiPath;
uint32 CPreferences::s_nWebPageRefresh;
bool CPreferences::s_bWebLowEnabled;
wxString CPreferences::s_WebTemplate;
bool CPreferences::s_showCatTabInfos;
AllCategoryFilter CPreferences::s_allcatFilter;
bool CPreferences::s_AcceptExternalConnections;
bool CPreferences::s_ECRequireEncryption;
wxString CPreferences::s_ECAddr;
wxString CPreferences::s_ECNetworkInterface;
uint32 CPreferences::s_ECPort;
uint32 CPreferences::s_ECAuthFailureWindowSeconds;
uint32 CPreferences::s_ECAuthFailureThreshold;
uint32 CPreferences::s_ECAuthLockoutSeconds;
wxString CPreferences::s_ECPassword;
bool CPreferences::s_TransmitOnlyUploadingClients;
bool CPreferences::s_IPFilterClients;
bool CPreferences::s_IPFilterServers;
bool CPreferences::s_UseSrcSeeds;
bool CPreferences::s_ProgBar;
bool CPreferences::s_Percent;
bool CPreferences::s_SecIdent;
bool CPreferences::s_allocFullFile;
bool CPreferences::s_mmapEnabled;
bool CPreferences::s_mmapSupported;
bool CPreferences::s_createFilesSparse;
wxString CPreferences::s_CustomBrowser;
bool CPreferences::s_BrowserTab;
CPath CPreferences::s_OSDirectory;
wxString CPreferences::s_Skin;
bool CPreferences::s_FastED2KLinksHandler;
bool CPreferences::s_ToolbarOrientation;
bool CPreferences::s_liveListSort;
bool CPreferences::s_AICHTrustEveryHash;
wxString CPreferences::s_CommentFilterString;
bool CPreferences::s_IPFilterAutoLoad;
wxString CPreferences::s_IPFilterURL;
CMD4Hash CPreferences::s_userhash;
bool CPreferences::s_MustFilterMessages;
wxString CPreferences::s_MessageFilterString;
bool CPreferences::s_FilterAllMessages;
bool CPreferences::s_FilterComments;
bool CPreferences::s_FilterSomeMessages;
bool CPreferences::s_ShowMessagesInLog;
bool CPreferences::s_IsAdvancedSpamfilterEnabled;
bool CPreferences::s_IsChatCaptchaEnabled;
bool CPreferences::s_ShareHiddenFiles;
bool CPreferences::s_AutoRescanSharedDirs;
bool CPreferences::s_FollowSymlinksInShares;
wxString CPreferences::s_ExcludeSharePatterns;
bool CPreferences::s_ExcludeSharePatternsUseRegex;
CShareExcludeFilter CPreferences::s_ShareExcludeFilter;
bool CPreferences::s_NewVersionCheck;
// Default true so the monolithic app (which never receives the capability tag
// over EC and doesn't consult this flag) is unaffected; the remote GUI
// overwrites it from each prefs-apply.
bool CPreferences::s_versionCheckAvailable = true;
bool CPreferences::s_MediaMetadataEnabled;
wxString CPreferences::s_MediaMetadataFFProbePath;
bool CPreferences::s_ConnectToKad;
bool CPreferences::s_ConnectToED2K;
unsigned CPreferences::s_maxClientVersions;
bool CPreferences::s_DropSlowSources;
bool CPreferences::s_IsClientCryptLayerSupported;
bool CPreferences::s_bCryptLayerRequested;
bool CPreferences::s_IsClientCryptLayerRequired;
uint32 CPreferences::s_dwKadUDPKey;
uint8 CPreferences::s_byCryptTCPPaddingLength;
wxString CPreferences::s_Ed2kURL;
wxString CPreferences::s_KadURL;
bool CPreferences::s_GeoIPEnabled;
bool CPreferences::s_GeoIPSupported = true;
bool CPreferences::s_GeoIPStatusLoaded = false;
bool CPreferences::s_GeoIPStatusDownloading = false;
wxString CPreferences::s_GeoIPStatusLastResult;
wxString CPreferences::s_GeoIPStatusLoadedSource;
bool CPreferences::s_GeoIPUpdateRequested = false;
wxString CPreferences::s_GeoIPSource;
wxString CPreferences::s_GeoIPLoadedSource;
wxString CPreferences::s_GeoIPMaxMindLicense;
wxString CPreferences::s_GeoIPCustomUrl;
bool CPreferences::s_GeoIPAutoUpdate;
wxString CPreferences::s_GeoIPUpdateUrl;
bool CPreferences::s_preventSleepWhileDownloading;
wxString CPreferences::s_StatsServerName;
wxString CPreferences::s_StatsServerURL;
/**
* Template Cfg class for connecting with widgets.
*
* This template provides the base functionality needed to synchronize a
* variable with a widget. However, please note that wxGenericValidator only
* supports a few types (int, wxString, bool and wxArrayInt), so this template
* can't always be used directly.
*
* Cfg_Str and Cfg_Bool are able to use this template directly, whereas Cfg_Int
* makes use of several workaround to enable it to be used with integers other
* than int.
*/
template <typename TYPE> class Cfg_Tmpl : public Cfg_Base
{
public:
/**
* Constructor.
*
* @param keyname
* @param value
* @param defaultVal
*/
Cfg_Tmpl(const wxString &keyname, TYPE &value, const TYPE &defaultVal)
: Cfg_Base(keyname)
, m_value(value)
, m_default(defaultVal)
, m_widget(NULL)
{
}
#ifndef AMULE_DAEMON
/**
* Connects the Cfg to a widget.
*
* @param id The ID of the widget to be connected.
* @param parent The parent of the widget. Use this to speed up searches.
*
* This function works by setting the wxValidator of the class. This however
* poses some restrictions on which variable types can be used for this
* template, as noted above. It also poses some limits on the widget types,
* refer to the wx documentation for those.
*/
virtual bool ConnectToWidget(int id, wxWindow *parent = NULL)
{
if (id) {
m_widget = wxWindow::FindWindowById(id, parent);
if (m_widget) {
wxGenericValidator validator(&m_value);
m_widget->SetValidator(validator);
return true;
}
} else {
m_widget = NULL;
}
return false;
}
/** Updates the associated variable, returning true on success. */
virtual bool TransferFromWindow()
{
if (m_widget) {
wxValidator *validator = m_widget->GetValidator();
if (validator) {
TYPE temp = m_value;
if (validator->TransferFromWindow()) {
SetChanged(temp != m_value);
return true;
}
}
}
return false;
}
/** Updates the associated widget, returning true on success. */
virtual bool TransferToWindow()
{
if (m_widget) {
wxValidator *validator = m_widget->GetValidator();
if (validator)
return validator->TransferToWindow();
}
return false;
}
/** @see Cfg_Base::ResetToDefault. Shows the default in the widget without
touching the stored variable, so Cancel is a no-op and OK commits it. */
virtual bool ResetToDefault()
{
if (!m_widget) {
return false;
}
TYPE saved = m_value;
m_value = m_default;
bool ok = TransferToWindow();
m_value = saved;
return ok;
}
#endif
/** Sets the default value. */
void SetDefault(const TYPE &defaultVal) { m_default = defaultVal; }
protected:
//! Reference to the associated variable
TYPE &m_value;
//! Default variable value
TYPE m_default;
//! Pointer to the widget assigned to the Cfg instance
wxWindow *m_widget;
};
/** Cfg class for wxStrings. */
class Cfg_Str : public Cfg_Tmpl<wxString>
{
public:
/** Constructor. */
Cfg_Str(const wxString &keyname, wxString &value, const wxString &defaultVal = EmptyString)
: Cfg_Tmpl<wxString>(keyname, value, defaultVal)
{
}
/** Loads the string, using the specified default value. */
virtual void LoadFromFile(wxConfigBase *cfg) { cfg->Read(GetKey(), &m_value, m_default); }
/** Saves the string to the specified wxConfig object. */
virtual void SaveToFile(wxConfigBase *cfg) { cfg->Write(GetKey(), m_value); }
};
/**
* Cfg-class for encrypting strings, for example for passwords.
*/
class Cfg_Str_Encrypted : public Cfg_Str
{
public:
Cfg_Str_Encrypted(const wxString &keyname, wxString &value, const wxString &defaultVal = EmptyString)
: Cfg_Str(keyname, value, defaultVal)
{
}
#ifndef AMULE_DAEMON
virtual bool TransferFromWindow()
{
// Shakraw: when storing value, store it encrypted here (only if changed in prefs)
if (Cfg_Str::TransferFromWindow()) {
// Only recalucate the hash for new, non-empty passwords
if (HasChanged() && !m_value.IsEmpty()) {
m_value = MD5Sum(m_value).GetHash();
}
return true;
}
return false;
}
#endif
};
/** Cfg class for CPath. */
class Cfg_Path : public Cfg_Str
{
public:
/** Constructor. */
Cfg_Path(const wxString &keyname, CPath &value, const wxString &defaultVal = EmptyString)
: Cfg_Str(keyname, m_temp_path, defaultVal)
, m_real_path(value)
{
}
/** @see Cfg_Str::LoadFromFile. */
virtual void LoadFromFile(wxConfigBase *cfg)
{
Cfg_Str::LoadFromFile(cfg);
m_real_path = CPath::FromUniv(m_temp_path);
}
/** @see Cfg_Str::SaveToFile. */
virtual void SaveToFile(wxConfigBase *cfg)
{
m_temp_path = CPath::ToUniv(m_real_path);
Cfg_Str::SaveToFile(cfg);
}
/** @see Cfg_Tmpl::TransferToWindow. */
virtual bool TransferToWindow()
{
m_temp_path = m_real_path.GetRaw();
return Cfg_Str::TransferToWindow();
}
/** @see Cfg_Tmpl::TransferFromWindow. */
virtual bool TransferFromWindow()
{
if (Cfg_Str::TransferFromWindow()) {
m_real_path = CPath(m_temp_path);
return true;
}
return false;
}
private:
wxString m_temp_path;
CPath &m_real_path;
};
/**
* Cfg class that takes care of integer types.
*
* This template is needed since wxValidator only supports normals ints, and
* wxConfig for the matter only supports longs, thus some worksarounds are
* needed.
*
* There are two work-arounds:
* 1) wxValidator only supports int*, so we need a immediate variable to act
* as a storage. Thus we use Cfg_Tmpl<int> as base class. Thus this class
* contains a integer which we use to pass the value back and forth
* between the widgets.
*
* 2) wxConfig uses longs to save and read values, thus we need an immediate
* stage when loading and saving the value.
*/
template <typename TYPE> class Cfg_Int : public Cfg_Tmpl<int>
{
public:
Cfg_Int(const wxString &keyname, TYPE &value, int defaultVal = 0)
: Cfg_Tmpl<int>(keyname, m_temp_value, defaultVal)
, m_real_value(value)
, m_temp_value(value)
{
}
virtual void LoadFromFile(wxConfigBase *cfg)
{
long tmp = 0;
cfg->Read(GetKey(), &tmp, m_default);
// Set the temp value
m_temp_value = (int)tmp;
// Set the actual value
m_real_value = (TYPE)tmp;
}
virtual void SaveToFile(wxConfigBase *cfg) { cfg->Write(GetKey(), (long)m_real_value); }
#ifndef AMULE_DAEMON
virtual bool TransferFromWindow()
{
if (Cfg_Tmpl<int>::TransferFromWindow()) {
m_real_value = (TYPE)m_temp_value;
return true;
}
return false;
}
virtual bool TransferToWindow()
{
m_temp_value = (int)m_real_value;
if (Cfg_Tmpl<int>::TransferToWindow()) {
// In order to let us update labels on slider-changes, we trigger a event
wxSlider *slider = dynamic_cast<wxSlider *>(m_widget);
if (slider) {
int id = m_widget->GetId();
int pos = slider->GetValue();
wxScrollEvent evt(wxEVT_SCROLL_THUMBRELEASE, id, pos);
m_widget->GetEventHandler()->ProcessEvent(evt);
}
return true;
}
return false;
}
/** @see Cfg_Base::ResetToDefault. Cfg_Int's TransferToWindow rebuilds the
widget from m_real_value, so the default is shown by briefly staging it
there and restoring it, leaving the committed value for OK/Cancel. */
virtual bool ResetToDefault()
{
if (!m_widget) {
return false;
}
TYPE saved = m_real_value;
m_real_value = (TYPE)m_default;
bool ok = TransferToWindow();
m_real_value = saved;
return ok;
}
#endif
protected:
TYPE &m_real_value;
int m_temp_value;
};
/**
* Helper function for creating new Cfg_Ints.
*
* @param keyname The cfg-key under which the item should be saved.
* @param value The variable to synchronize. The type of this variable defines the type used to create the
* Cfg_Int.
* @param defaultVal The default value if the key isn't found when loading the value.
* @return A pointer to the new Cfg_Int object. The caller is responsible for deleting it.
*
* This template-function returns a Cfg_Int of the appropriate type for the
* variable used as argument and should be used to avoid having to specify
* the integer type when adding a new Cfg_Int, since that's just increases
* the maintenance burden.
*/
template <class TYPE> Cfg_Base *MkCfg_Int(const wxString &keyname, TYPE &value, int defaultVal)
{
return new Cfg_Int<TYPE>(keyname, value, defaultVal);
}
/**
* Cfg-class for bools.
*/
class Cfg_Bool : public Cfg_Tmpl<bool>
{
public:
Cfg_Bool(const wxString &keyname, bool &value, bool defaultVal)
: Cfg_Tmpl<bool>(keyname, value, defaultVal)
{
}
virtual void LoadFromFile(wxConfigBase *cfg) { cfg->Read(GetKey(), &m_value, m_default); }
virtual void SaveToFile(wxConfigBase *cfg) { cfg->Write(GetKey(), m_value); }
};
/**
* Wraps any Cfg class so its value lives only in memory for this run.
*
* The wrapped preference still binds to a dialog control, still reports
* HasChanged(), and still travels over EC like any other — only the
* amule.conf round trip is dropped.
*
* This is what the amuleapi credential fields need. Those credentials have
* exactly one store, amuleapi-passwords, which amuleapi, amuled and
* monolithic aMule all read and write; a second copy in amule.conf would
* mean two stores that disagree the moment either side changes, with no
* way to tell which is newer. The dialog field is therefore a write-only
* request ("set the password to this"), not a mirror of what is stored.
*
* The key name is kept for readability; nothing reads or writes it.
*/
template <typename BASE> class Cfg_Transient : public BASE
{
public:
using BASE::BASE;
virtual void LoadFromFile(wxConfigBase *) {}
virtual void SaveToFile(wxConfigBase *) {}
};
#ifndef AMULE_DAEMON
class Cfg_Colour : public Cfg_Base
{
public:
Cfg_Colour(const wxString &key, wxColour &colour)
: Cfg_Base(key)
, m_colour(colour)
, m_default(CMuleColour(colour).GetULong())
{
}
virtual void LoadFromFile(wxConfigBase *cfg)
{
long int rgb;
cfg->Read(GetKey(), &rgb, m_default);
m_colour.Set(rgb);
}
virtual void SaveToFile(wxConfigBase *cfg)
{
cfg->Write(GetKey(), static_cast<long int>(CMuleColour(m_colour).GetULong()));
}
private:
wxColour &m_colour;
long int m_default;
};
typedef struct
{
int id;
bool available;
wxString displayname;
wxString name;
} LangInfo;
/**
* The languages aMule has translation for.
*
* Add new languages here.
* Then activate the test code in Cfg_Lang::UpdateChoice below!
*/
static LangInfo aMuleLanguages[] = {
{ wxLANGUAGE_DEFAULT, true, "", wxTRANSLATE("System default") },
{ wxLANGUAGE_ALBANIAN, false, "", wxTRANSLATE("Albanian") },
{ wxLANGUAGE_ARABIC, false, "", wxTRANSLATE("Arabic") },
{ wxLANGUAGE_ASTURIAN, false, "", wxTRANSLATE("Asturian") },
{ wxLANGUAGE_BASQUE, false, "", wxTRANSLATE("Basque") },
{ wxLANGUAGE_BULGARIAN, false, "", wxTRANSLATE("Bulgarian") },
{ wxLANGUAGE_CATALAN, false, "", wxTRANSLATE("Catalan") },
{ wxLANGUAGE_CHINESE_SIMPLIFIED, false, "", wxTRANSLATE("Chinese (Simplified)") },
{ wxLANGUAGE_CHINESE_TRADITIONAL, false, "", wxTRANSLATE("Chinese (Traditional)") },
{ wxLANGUAGE_CROATIAN, false, "", wxTRANSLATE("Croatian") },
{ wxLANGUAGE_CZECH, false, "", wxTRANSLATE("Czech") },
{ wxLANGUAGE_DANISH, false, "", wxTRANSLATE("Danish") },
{ wxLANGUAGE_DUTCH, false, "", wxTRANSLATE("Dutch") },
{ wxLANGUAGE_ENGLISH_UK, false, "", wxTRANSLATE("English (U.K.)") },
{ wxLANGUAGE_ENGLISH_US, false, "", wxTRANSLATE("English (U.S.)") },
{ wxLANGUAGE_ESTONIAN, false, "", wxTRANSLATE("Estonian") },
{ wxLANGUAGE_FINNISH, false, "", wxTRANSLATE("Finnish") },
{ wxLANGUAGE_FRENCH, false, "", wxTRANSLATE("French") },
{ wxLANGUAGE_GALICIAN, false, "", wxTRANSLATE("Galician") },
{ wxLANGUAGE_GERMAN, false, "", wxTRANSLATE("German") },
{ wxLANGUAGE_GREEK, false, "", wxTRANSLATE("Greek") },
{ wxLANGUAGE_HEBREW, false, "", wxTRANSLATE("Hebrew") },
{ wxLANGUAGE_HUNGARIAN, false, "", wxTRANSLATE("Hungarian") },
{ wxLANGUAGE_ITALIAN, false, "", wxTRANSLATE("Italian") },
{ wxLANGUAGE_JAPANESE, false, "", wxTRANSLATE("Japanese") },
{ wxLANGUAGE_KOREAN, false, "", wxTRANSLATE("Korean") },
{ wxLANGUAGE_LATVIAN, false, "", wxTRANSLATE("Latvian") },
{ wxLANGUAGE_LITHUANIAN, false, "", wxTRANSLATE("Lithuanian") },
{ wxLANGUAGE_NORWEGIAN_NYNORSK, false, "", wxTRANSLATE("Norwegian (Nynorsk)") },
{ wxLANGUAGE_POLISH, false, "", wxTRANSLATE("Polish") },
{ wxLANGUAGE_PORTUGUESE, false, "", wxTRANSLATE("Portuguese") },
{ wxLANGUAGE_PORTUGUESE_BRAZILIAN, false, "", wxTRANSLATE("Portuguese (Brazilian)") },
{ wxLANGUAGE_ROMANIAN, false, "", wxTRANSLATE("Romanian") },
{ wxLANGUAGE_RUSSIAN, false, "", wxTRANSLATE("Russian") },
{ wxLANGUAGE_SLOVENIAN, false, "", wxTRANSLATE("Slovenian") },
{ wxLANGUAGE_SPANISH, false, "", wxTRANSLATE("Spanish") },
{ wxLANGUAGE_SWEDISH, false, "", wxTRANSLATE("Swedish") },
{ wxLANGUAGE_TURKISH, false, "", wxTRANSLATE("Turkish") },
{ wxLANGUAGE_UKRAINIAN, false, "", wxTRANSLATE("Ukrainian") },
};
typedef Cfg_Int<int> Cfg_PureInt;
// Returns true if aMule's translation catalog (PACKAGE.mo) exists on
// disk for the given wxWidgets language id, using the same lookup
// prefixes that InitLocale() registers. Used as an additional gate
// in the language-picker probe below: the standard wxLocale::IsAvailable
// / locale_to_check.IsOk() path depends on glibc-locale data being
// present for each candidate language, which is *not* the case inside
// Flatpak sandboxes — the GNOME runtime ships only the user's preferred
// locale, so every other language fails the probe even when our .mo
// files are reachable. Treating "catalog file is on disk" as also-OK
// makes the picker show every language we actually ship a translation
// for, regardless of sandbox glibc state.
static bool HasAMuleCatalogForLanguage(int wxLanguageId)
{
const wxLanguageInfo *info = wxLocale::GetLanguageInfo(wxLanguageId);
if (!info) {
return false;
}
const wxString canonical = info->CanonicalName;
if (canonical.IsEmpty()) {
return false;
}
// Same prefixes as InitLocale (OtherFunctions.cpp) — keep in sync.
wxArrayString prefixes;
#if defined(__WXMAC__) || defined(__WINDOWS__)
prefixes.Add(JoinPaths(wxStandardPaths::Get().GetResourcesDir(), "locale"));
#elif defined(__WXGTK__) || defined(__UNIX__)
prefixes.Add(JoinPaths(JoinPaths(wxStandardPaths::Get().GetInstallPrefix(), "share"), "locale"));
#endif
const wxString catalog = wxString(PACKAGE) + ".mo";
for (size_t i = 0; i < prefixes.GetCount(); ++i) {
const wxString candidate =
JoinPaths(JoinPaths(JoinPaths(prefixes[i], canonical), "LC_MESSAGES"), catalog);
if (wxFileExists(candidate)) {
return true;
}
}
return false;
}
class Cfg_Lang : public Cfg_PureInt, public Cfg_Lang_Base
{
public:
// cppcheck-suppress uninitMemberVar m_selection, m_langSelector
Cfg_Lang()
: Cfg_PureInt("", m_selection, 0)
{
m_languagesReady = false;
m_changePos = 0;
}
virtual void LoadFromFile(wxConfigBase *WXUNUSED(cfg)) {}
virtual void SaveToFile(wxConfigBase *WXUNUSED(cfg)) {}
virtual bool TransferFromWindow()
{
if (!m_languagesReady) {
return true; // nothing changed, no problem
}
if (Cfg_PureInt::TransferFromWindow()) {
// find wx ID of selected language
int i = 0;
while (m_selection > 0) {
i++;
if (aMuleLanguages[i].available) {
m_selection--;
}
}
int id = aMuleLanguages[i].id;
// save language selection
thePrefs::SetLanguageID(wxLang2Str(id));
return true;
}
return false;
}
virtual bool TransferToWindow()
{
m_langSelector = dynamic_cast<wxChoice *>(m_widget); // doesn't work in ctor!
if (m_languagesReady) {
FillChoice();
} else {
int wxId = StrLang2wx(thePrefs::GetLanguageID());
m_langSelector->Clear();
m_selection = 0;
for (uint32 i = 0; i < itemsof(aMuleLanguages); i++) {
if (aMuleLanguages[i].id == wxId) {
m_langSelector->Append(
wxString(wxGetTranslation(aMuleLanguages[i].name)) + " [" +
aMuleLanguages[i].name + "]");
break;
}
}
m_langSelector->Append(_("Change Language"));
m_changePos = m_langSelector->GetCount() - 1;
}
return Cfg_PureInt::TransferToWindow();
}
virtual void UpdateChoice(int pos)
{
if (!m_languagesReady && pos == m_changePos) {
// Find available languages and translate them.
// This is only done when the user selects "Change Language"
// Language is changed rarely, and the go-through-all locales takes a considerable
// time when the settings dialog is opened for the first time.
wxBusyCursor busyCursor;
aMuleLanguages[0].displayname = wxGetTranslation(aMuleLanguages[0].name);
// This suppresses error-messages about invalid locales
for (unsigned int i = 1; i < itemsof(aMuleLanguages); ++i) {
// Outer gate: glibc-locale-data path OR aMule .mo catalog on
// disk. The catalog path is required for Flatpak sandboxes
// where the GNOME runtime ships only the user's preferred
// glibc locale, so wxLocale::IsAvailable returns false for
// every other language despite the .mo being present.
const bool hasCatalog = HasAMuleCatalogForLanguage(aMuleLanguages[i].id);
if ((aMuleLanguages[i].id > wxLANGUAGE_USER_DEFINED) ||
wxLocale::IsAvailable(aMuleLanguages[i].id) || hasCatalog) {
wxLogNull logTarget;
wxLocale locale_to_check;
InitLocale(locale_to_check, aMuleLanguages[i].id);
// English (U.S.) is the source language for the .pot catalog,
// so it is always available even though no en_US.mo is shipped.
const bool isSourceLanguage =
aMuleLanguages[i].id == wxLANGUAGE_ENGLISH_US;
// Inner gate: same Flatpak rationale — InitLocale() can
// fail with IsOk()==false when glibc lacks the locale
// data for the candidate, but our .mo is still on disk
// and will be applied correctly when the user actually
// switches to this language. Accept `hasCatalog` here as
// well so those entries surface in the picker.
if ((locale_to_check.IsOk() && (locale_to_check.IsLoaded(PACKAGE) ||
isSourceLanguage)) ||
hasCatalog) {
aMuleLanguages[i].displayname =
wxString(wxGetTranslation(aMuleLanguages[i].name)) +
" [" + aMuleLanguages[i].name + "]";
aMuleLanguages[i].available = true;
#if 0
// Check for language problems
// Activate this code temporarily after messing with the languages!
int wxid = StrLang2wx(wxLang2Str(aMuleLanguages[i].id));
if (wxid != aMuleLanguages[i].id) {
AddDebugLogLineN(logGeneral, CFormat("Language problem for %s : aMule id %d != wx id %d")
% aMuleLanguages[i].name % aMuleLanguages[i].id % wxid);
}
#endif
}
}
}
// Restore original locale
wxLocale tmpLocale;
InitLocale(tmpLocale, theApp->m_locale.GetLanguage());
FillChoice();
if (m_langSelector->GetCount() == 1) {
wxMessageBox(_("There are no translations installed for aMule"),
_("No languages available"),
wxICON_INFORMATION | wxOK);
}
m_langSelector->SetSelection(m_selection);
m_languagesReady = true;
}
}
protected:
int m_selection;
private:
void FillChoice()
{
int wxId = StrLang2wx(thePrefs::GetLanguageID());
m_langSelector->Clear();
// Add all available languages and find the index of the selected language.
for (unsigned int i = 0, j = 0; i < itemsof(aMuleLanguages); i++) {
if (aMuleLanguages[i].available) {
m_langSelector->Append(aMuleLanguages[i].displayname);
if (aMuleLanguages[i].id == wxId) {
m_selection = j;
}
j++;
}
}
}
bool m_languagesReady; // true: all translations calculated
int m_changePos;
wxChoice *m_langSelector;
};
#endif /* ! AMULE_DAEMON */
void Cfg_Lang_Base::UpdateChoice(int) {} // dummy
class Cfg_Skin : public Cfg_Str
{
public:
Cfg_Skin(const wxString &keyname, wxString &value, const wxString &defaultVal = EmptyString)
: Cfg_Str(keyname, value, defaultVal)
, m_is_skin(false)
{
}
#ifndef AMULE_DAEMON
virtual bool TransferFromWindow()
{
if (Cfg_Str::TransferFromWindow()) {
if (m_is_skin) {
wxChoice *skinSelector = dynamic_cast<wxChoice *>(m_widget);
// "- default -" is always the first
if (skinSelector->GetSelection() == 0) {
m_value.Clear();
}
}
return true;
}
return false;
}
virtual bool TransferToWindow()
{
wxChoice *skinSelector = dynamic_cast<wxChoice *>(m_widget);
skinSelector->Clear();
wxString folder;
int flags = wxDIR_DIRS;
wxString filespec;
wxString defaultSelection = _("- default -");
// #warning there has to be a better way...
if (GetKey() == "/SkinGUIOptions/Skin") {
folder = "skins";
m_is_skin = true;
flags = wxDIR_FILES;
filespec = "*.zip";
skinSelector->Append(defaultSelection);
} else {
folder = "webserver";
}
wxString dirName(JoinPaths(thePrefs::GetConfigDir(), folder));
wxString Filename;
wxDir d;
if (wxDir::Exists(dirName) && d.Open(dirName) && d.GetFirst(&Filename, filespec, flags)) {
do {