-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
857 lines (736 loc) · 31.3 KB
/
Copy pathSettings.cs
File metadata and controls
857 lines (736 loc) · 31.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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using Microsoft.Data.Sqlite;
namespace SaemDesk;
/// <summary>
/// 설정 속성 (Fluent API)
/// </summary>
public class SettingProperty<T>
{
private readonly string _key;
private T _value;
private readonly Func<string, T> _parser;
private readonly Func<T, string> _serializer;
internal SettingProperty(string key, T defaultValue, Func<string, T> parser, Func<T, string> serializer)
{
_key = key;
_value = defaultValue;
_parser = parser;
_serializer = serializer;
}
/// <summary>
/// 값 가져오기 (캐시에서 읽기, 초고속)
/// </summary>
public T Value
{
get => _value;
set => _value = value;
}
/// <summary>
/// 값 변경 후 저장
/// </summary>
public void Save()
{
SettingsDb.Set(_key, _serializer(_value));
}
/// <summary>
/// 값 설정 + 저장 (체이닝 가능)
/// </summary>
public SettingProperty<T> Set(T value)
{
_value = value;
Save();
return this;
}
/// <summary>
/// DB에서 다시 로드
/// </summary>
public void Reload()
{
string? strValue = SettingsDb.Get(_key);
if (!string.IsNullOrEmpty(strValue))
{
_value = _parser(strValue);
}
}
// 암시적 변환 (자동으로 값 반환)
public static implicit operator T(SettingProperty<T> prop) => prop.Value;
public override string ToString() => _value?.ToString() ?? "";
}
/// <summary>
/// 메인 설정 클래스 (Fluent API)
/// </summary>
public static class Settings
{
/// <summary>
/// 데이터 경로 (앱 시작 시 1회 결정, 이후 불변)
/// </summary>
public static string UserDataPath { get; } = ResolveDataPath();
/// <summary>
/// 포터블 모드 여부
/// </summary>
public static bool IsPortableMode { get; } = !UserDataPath.Equals(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "SaemDesk"),
StringComparison.OrdinalIgnoreCase);
/// <summary>
/// 데이터 폴더 경로 (UserDataPath와 동일)
/// </summary>
public static string DataDirectory => UserDataPath;
/// <summary>
/// 데이터 경로 결정 로직:
/// 1. exe 옆에 Settings.db → 포터블
/// 2. %USERPROFILE%\NewSchool\Settings.db → 사용자 폴더
/// 3. 최초 실행: 시스템 경로이거나 쓰기 불가 → 사용자 폴더, 그 외 → 포터블
/// </summary>
private static string ResolveDataPath()
{
var exeDir = AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var userPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "SaemDesk");
// 1. exe 옆에 Settings.db 있으면 포터블
if (File.Exists(Path.Combine(exeDir, "Settings.db")))
return exeDir;
// 2. 사용자 폴더에 Settings.db 있으면 사용자 폴더
if (File.Exists(Path.Combine(userPath, "Settings.db")))
return userPath;
// 3. 최초 실행 — 경로 + 쓰기 권한으로 판단
if (IsSystemPath(exeDir) || !IsWritable(exeDir))
return userPath;
return exeDir; // 포터블
}
/// <summary>
/// Program Files, WindowsApps, 개발 빌드 경로 등 포터블 대상이 아닌 경로인지 확인
/// </summary>
private static bool IsSystemPath(string path)
{
var normalized = path.Replace('/', '\\').TrimEnd('\\').ToUpperInvariant();
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).ToUpperInvariant();
var programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86).ToUpperInvariant();
var windowsApps = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Microsoft", "WindowsApps").ToUpperInvariant();
// 시스템 설치 경로
if (normalized.StartsWith(programFiles)
|| normalized.StartsWith(programFilesX86)
|| normalized.StartsWith(windowsApps))
return true;
// 개발 빌드 경로 (bin\Debug, bin\Release, bin\x64\ 등)
if (normalized.Contains(@"\BIN\DEBUG\") || normalized.Contains(@"\BIN\RELEASE\")
|| normalized.Contains(@"\OBJ\"))
return true;
return false;
}
/// <summary>
/// 해당 폴더에 쓰기 가능한지 테스트
/// </summary>
private static bool IsWritable(string path)
{
try
{
var testFile = Path.Combine(path, $".newschool_write_test_{Guid.NewGuid():N}");
File.WriteAllText(testFile, "");
File.Delete(testFile);
return true;
}
catch { return false; }
}
// Scheduler 관련 설정
public static SettingProperty<string> SchedulerDB { get; private set; } = null!;
public static SettingProperty<bool> Scheduler_Inited { get; private set; } = null!;
public static SettingProperty<bool> ShowEvents { get; private set; } = null!;
public static SettingProperty<bool> ShowTasks { get; private set; } = null!;
public static SettingProperty<double> EventFontSize { get; private set; } = null!;
public static SettingProperty<double> TaskFontSize { get; private set; } = null!;
public static SettingProperty<bool> UseGoogle { get; private set; } = null!;
public static SettingProperty<string> GoogleCalendarName { get; private set; } = null!;
public static SettingProperty<string> GoogleCalendarID { get; private set; } = null!;
// Google OAuth 인증
public static SettingProperty<string> GoogleClientId { get; private set; } = null!;
public static SettingProperty<string> GoogleClientSecret { get; private set; } = null!;
public static SettingProperty<string> GoogleAccessToken { get; private set; } = null!;
public static SettingProperty<string> GoogleRefreshToken { get; private set; } = null!;
public static SettingProperty<string> GoogleTokenExpiry { get; private set; } = null!;
public static SettingProperty<bool> GoogleAutoSync { get; private set; } = null!;
public static SettingProperty<int> GoogleSyncIntervalMinutes { get; private set; } = null!;
public static SettingProperty<string> GoogleLastSyncTime { get; private set; } = null!;
// School 관련 설정
public static SettingProperty<string> SchoolDB { get; private set; } = null!;
public static SettingProperty<bool> School_Inited { get; private set; } = null!;
public static SettingProperty<string> User { get; private set; } = null!;
public static SettingProperty<int> WorkYear { get; private set; } = null!;
public static SettingProperty<string> ProvinceCode { get; private set; } = null!;
public static SettingProperty<string> SchoolCode { get; private set; } = null!;
public static SettingProperty<string> SchoolName { get; private set; } = null!;
public static SettingProperty<string> SchoolAddress { get; private set; } = null!;
public static SettingProperty<string> ProvinceName { get; private set; } = null!;
public static SettingProperty<string> NeisApiKey { get; private set; } = null!;
public static SettingProperty<int> WorkSemester { get; private set; } = null!;
public static SettingProperty<bool> TopMost { get; private set; } = null!;
public static SettingProperty<string> UserName { get; private set; } = null!;
public static SettingProperty<bool> IsNeisEventDownloaded { get; private set; } = null!;
// school period 설정
public static SettingProperty<TimeSpan> AssemblyTime { get; private set; } = null!;
public static SettingProperty<TimeSpan> DayStarting { get; private set; } = null!;
public static SettingProperty<TimeSpan> BreakTime { get; private set; } = null!;
public static SettingProperty<TimeSpan> OnePeriod { get; private set; } = null!;
public static SettingProperty<TimeSpan> LunchTime { get; private set; } = null!;
// school homeroom 설정
public static SettingProperty<int> HomeGrade { get; private set; } = null!;
public static SettingProperty<int> HomeRoom { get; private set; } = null!;
//Board 설정
public static SettingProperty<string> Board_DB { get; private set; } = null!;
public static SettingProperty<bool> Board_Inited { get; private set; } = null!;
/// <summary>게시글 Content(HTML)→ardx BLOB 1회성 마이그레이션 완료 여부.</summary>
public static SettingProperty<bool> Board_ArdxMigrated { get; private set; } = null!;
/// <summary>
/// 캐시 활성화 여부
/// </summary>
public static SettingProperty<bool> EnableCache { get; private set; } = null!;
/// <summary>
/// 페이지 크기 (기본값)
/// </summary>
public static SettingProperty<int> DefaultPageSize { get; private set; } = null!;
/// <summary>
/// Windows 시작 시 자동 실행
/// </summary>
public static SettingProperty<bool> StartWithWindows { get; private set; } = null!;
/// <summary>
/// 자동 백업 활성화
/// </summary>
public static SettingProperty<bool> AutoBackup { get; private set; } = null!;
/// <summary>
/// 마지막 백업 시간 (ISO8601)
/// </summary>
public static SettingProperty<string> LastBackupTime { get; private set; } = null!;
/// <summary>
/// 자동 백업 간격 (일)
/// </summary>
public static SettingProperty<int> AutoBackupIntervalDays { get; private set; } = null!;
/// <summary>
/// 백업 보관 개수
/// </summary>
public static SettingProperty<int> BackupRetentionCount { get; private set; } = null!;
/// <summary>
/// 로그 레벨
/// </summary>
public static SettingProperty<string> LogLevel { get; private set; } = null!;
/// <summary>
/// 테마
/// </summary>
public static SettingProperty<string> Theme { get; private set; } = null!;
/// <summary>
/// 언어
/// </summary>
public static SettingProperty<string> Language { get; private set; } = null!;
/// <summary>
/// 창 너비
/// </summary>
public static SettingProperty<int> WindowWidth { get; private set; } = null!;
/// <summary>
/// 창 높이
/// </summary>
public static SettingProperty<int> WindowHeight { get; private set; } = null!;
/// <summary>
/// 창 X 위치
/// </summary>
public static SettingProperty<int> WindowX { get; private set; } = null!;
/// <summary>
/// 창 Y 위치
/// </summary>
public static SettingProperty<int> WindowY { get; private set; } = null!;
/// <summary>
/// 창 최대화 여부
/// </summary>
public static SettingProperty<bool> WindowIsMaximized { get; private set; } = null!;
/// <summary>
/// 마지막으로 열린 페이지
/// </summary>
public static SettingProperty<string> LastPage { get; private set; } = null!;
/// <summary>
/// 설정 초기화 (앱 시작 시 한 번 호출)
/// </summary>
public static void Initialize()
{
System.Diagnostics.Debug.WriteLine("[Settings] 초기화 시작");
// DB 초기화
SettingsDb.Initialize();
// 속성 초기화 (파서와 직렬화기 지정)
SchedulerDB = new SettingProperty<string>("SchedulerDB", "scheduler.db", s => s, s => s);
Scheduler_Inited = new SettingProperty<bool>("Scheduler_Inited", false, bool.Parse, b => b.ToString().ToLower());
ShowEvents = new SettingProperty<bool>("ShowEvents", true, bool.Parse, b => b.ToString().ToLower());
ShowTasks = new SettingProperty<bool>("ShowTasks", true, bool.Parse, b => b.ToString().ToLower());
EventFontSize = new SettingProperty<double>("EventFontSize", 9.0, double.Parse, d => d.ToString());
TaskFontSize = new SettingProperty<double>("TaskFontSize", 10.0, double.Parse, d => d.ToString());
UseGoogle = new SettingProperty<bool>("UseGoogle", false, bool.Parse, b => b.ToString().ToLower());
GoogleCalendarName = new SettingProperty<string>("GoogleCalendarName", "", s => s, s => s);
GoogleCalendarID = new SettingProperty<string>("GoogleCalendarID", "", s => s, s => s);
// Google OAuth
GoogleClientId = new SettingProperty<string>("GoogleClientId", "", s => s, s => s);
GoogleClientSecret = new SettingProperty<string>("GoogleClientSecret", "", s => s, s => s);
GoogleAccessToken = new SettingProperty<string>("GoogleAccessToken", "", s => s, s => s);
GoogleRefreshToken = new SettingProperty<string>("GoogleRefreshToken", "", s => s, s => s);
GoogleTokenExpiry = new SettingProperty<string>("GoogleTokenExpiry", "", s => s, s => s);
GoogleAutoSync = new SettingProperty<bool>("GoogleAutoSync", false, bool.Parse, b => b.ToString().ToLower());
GoogleSyncIntervalMinutes = new SettingProperty<int>("GoogleSyncIntervalMinutes", 15, int.Parse, i => i.ToString());
GoogleLastSyncTime = new SettingProperty<string>("GoogleLastSyncTime", "", s => s, s => s);
User = new SettingProperty<string>("User", "user", s => s, s => s);
WorkYear = new SettingProperty<int>("WorkYear", 0, int.Parse, i => i.ToString());
ProvinceCode = new SettingProperty<string>("ProvinceCode", "", s => s, s => s);
SchoolCode = new SettingProperty<string>("SchoolCode", "", s => s, s => s);
SchoolName = new SettingProperty<string>("SchoolName", "", s => s, s => s);
SchoolAddress = new SettingProperty<string>("SchoolAddress", "", s => s, s => s);
ProvinceName = new SettingProperty<string>("ProvinceName", "", s => s, s => s);
NeisApiKey = new SettingProperty<string>("NeisApiKey", SaemDesk.Services.SecretsService.NeisApiKey, s => s, s => s);
WorkSemester = new SettingProperty<int>("WorkSemester", 0, int.Parse, i => i.ToString());
TopMost = new SettingProperty<bool>("TopMost", false, bool.Parse, b => b.ToString().ToLower());
UserName = new SettingProperty<string>("UserName", "", s => s, s => s);
IsNeisEventDownloaded = new SettingProperty<bool>("IsNeisEventDownloaded", false, bool.Parse, b => b.ToString().ToLower());
AssemblyTime = new SettingProperty<TimeSpan>("AssemblyTime", TimeSpan.FromMinutes(10), TimeSpan.Parse, ts => ts.ToString());
DayStarting = new SettingProperty<TimeSpan>("DayStarting", new TimeSpan(8, 30, 0), TimeSpan.Parse, ts => ts.ToString());
BreakTime = new SettingProperty<TimeSpan>("BreakTime", TimeSpan.FromMinutes(10), TimeSpan.Parse, ts => ts.ToString());
OnePeriod = new SettingProperty<TimeSpan>("OnePeriod", TimeSpan.FromMinutes(45), TimeSpan.Parse, ts => ts.ToString());
LunchTime = new SettingProperty<TimeSpan>("LunchTime", TimeSpan.FromMinutes(50), TimeSpan.Parse, ts => ts.ToString());
HomeGrade = new SettingProperty<int>("HomeGrade", 0, int.Parse, d => d.ToString());
HomeRoom = new SettingProperty<int>("HomeRoom", 0, int.Parse, d => d.ToString());
SchoolDB = new SettingProperty<string>("SchoolDB", "school.db", s => s, s => s);
School_Inited = new SettingProperty<bool>("SchoolDB_Inited", false, bool.Parse, b => b.ToString().ToLower());
Board_DB = new SettingProperty<string>("Board_DB", "board.db", s => s, s => s);
Board_Inited = new SettingProperty<bool>("Board_Init", false, bool.Parse, b => b.ToString().ToLower());
Board_ArdxMigrated = new SettingProperty<bool>("Board_ArdxMigrated", false, bool.Parse, b => b.ToString().ToLower());
/// <summary>
/// 캐시 활성화 여부
/// </summary>
EnableCache = new SettingProperty<bool>("EnableCache", true, bool.Parse, b => b.ToString().ToLower());
/// <summary>
/// 페이지 크기 (기본값)
/// </summary>
DefaultPageSize = new SettingProperty<int>("DefaultPageSize", 20, int.Parse, i => i.ToString());
/// <summary>
/// Windows 시작 시 자동 실행
/// </summary>
StartWithWindows = new SettingProperty<bool>("StartWithWindows", false, bool.Parse, b => b.ToString().ToLower());
/// <summary>
/// 자동 백업 활성화
/// </summary>
AutoBackup = new SettingProperty<bool>("AutoBackup", false, bool.Parse, b => b.ToString().ToLower());
/// <summary>
/// 마지막 백업 시간
/// </summary>
LastBackupTime = new SettingProperty<string>("LastBackupTime", string.Empty, s => s, s => s);
/// <summary>
/// 자동 백업 간격 (일)
/// </summary>
AutoBackupIntervalDays = new SettingProperty<int>("AutoBackupIntervalDays", 7, int.Parse, i => i.ToString());
/// <summary>
/// 백업 보관 개수
/// </summary>
BackupRetentionCount = new SettingProperty<int>("BackupRetentionCount", 20, int.Parse, i => i.ToString());
/// <summary>
/// 로그 레벨
/// </summary>
LogLevel = new SettingProperty<string>("LogLevel", "Info", s => s, s => s);
/// <summary>
/// 테마
/// </summary>
Theme = new SettingProperty<string>("Theme", "Light", s => s, s => s);
/// <summary>
/// 언어
/// </summary>
Language = new SettingProperty<string>("Language", "ko-KR", s => s, s => s);
/// <summary>
/// 창 크기 (기본값: 1400x900)
/// </summary>
WindowWidth = new SettingProperty<int>("WindowWidth", 1400, int.Parse, i => i.ToString());
WindowHeight = new SettingProperty<int>("WindowHeight", 900, int.Parse, i => i.ToString());
WindowX = new SettingProperty<int>("WindowX", -1, int.Parse, i => i.ToString());
WindowY = new SettingProperty<int>("WindowY", -1, int.Parse, i => i.ToString());
WindowIsMaximized = new SettingProperty<bool>("WindowIsMaximized", false, bool.Parse, b => b.ToString().ToLower());
LastPage = new SettingProperty<string>("LastPage", "", s => s, s => s);
// DB에서 모든 값 로드
LoadAll();
System.Diagnostics.Debug.WriteLine("[Settings] 초기화 완료");
}
/// <summary>
/// DB에서 모든 설정 로드
/// </summary>
private static void LoadAll()
{
SchedulerDB.Reload();
Scheduler_Inited.Reload();
ShowEvents.Reload();
ShowTasks.Reload();
EventFontSize.Reload();
TaskFontSize.Reload();
UseGoogle.Reload();
GoogleCalendarName.Reload();
GoogleCalendarID.Reload();
GoogleClientId.Reload();
GoogleClientSecret.Reload();
GoogleAccessToken.Reload();
GoogleRefreshToken.Reload();
GoogleTokenExpiry.Reload();
GoogleAutoSync.Reload();
GoogleSyncIntervalMinutes.Reload();
GoogleLastSyncTime.Reload();
SchoolDB.Reload();
School_Inited.Reload();
User.Reload();
WorkYear.Reload();
ProvinceCode.Reload();
SchoolCode.Reload();
SchoolName.Reload();
SchoolAddress.Reload();
ProvinceName.Reload();
// NeisApiKey 는 빌드 시 secrets.props 로만 주입 — DB persistence 사용 안 함
// (SecretsService.NeisApiKey 가 SettingProperty 의 default 로 주입되어 그대로 유지됨)
WorkSemester.Reload();
TopMost.Reload();
StartWithWindows.Reload();
AutoBackup.Reload();
LastBackupTime.Reload();
UserName.Reload();
IsNeisEventDownloaded.Reload();
AssemblyTime.Reload();
DayStarting.Reload();
BreakTime.Reload();
OnePeriod.Reload();
LunchTime.Reload();
HomeGrade.Reload();
HomeRoom.Reload();
Board_DB.Reload();
Board_Inited.Reload();
Board_ArdxMigrated.Reload();
EnableCache.Reload();
DefaultPageSize.Reload();
AutoBackupIntervalDays.Reload();
BackupRetentionCount.Reload();
LogLevel.Reload();
Theme.Reload();
Language.Reload();
WindowWidth.Reload();
WindowHeight.Reload();
WindowX.Reload();
WindowY.Reload();
WindowIsMaximized.Reload();
LastPage.Reload();
}
/// <summary>
/// 기본값으로 리셋
/// </summary>
public static void ResetToDefaults()
{
SettingsDb.ResetToDefaults();
LoadAll();
}
/// <summary>
/// 전체 데이터 백업 (Settings.db + 모든 DB)
/// </summary>
public static string? Backup()
{
try
{
var backupDir = Path.Combine(UserDataPath, "Backups",
$"backup_{DateTime.Now:yyyyMMdd_HHmmss}");
Directory.CreateDirectory(backupDir);
// UserDataPath의 모든 .db 파일 (Settings.db, school.db, scheduler.db, board.db 등)
foreach (var dbFile in Directory.GetFiles(UserDataPath, "*.db"))
{
var fileName = Path.GetFileName(dbFile);
Helpers.DbFileHelper.Checkpoint(dbFile); // WAL 병합 후 단일 파일 복사
File.Copy(dbFile, Path.Combine(backupDir, fileName), true);
}
LastBackupTime.Set(DateTime.Now.ToString("o"));
CleanupOldBackups();
System.Diagnostics.Debug.WriteLine($"[Settings] 백업 완료: {backupDir}");
return backupDir;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Settings] 백업 오류: {ex.Message}");
return null;
}
}
/// <summary>
/// 백업 폴더에서 전체 복원
/// </summary>
public static bool Restore(string backupDirOrFile)
{
try
{
// 풀에 남은 DB 파일 핸들을 모두 해제 — 덮어쓰기 중 잠김/고아 WAL로 인한 손상 방지
SqliteConnection.ClearAllPools();
// 단일 파일이면 기존 Settings.db 복원 (하위호환)
if (File.Exists(backupDirOrFile) && backupDirOrFile.EndsWith(".db"))
{
bool success = SettingsDb.Restore(backupDirOrFile);
if (success) LoadAll();
return success;
}
// 폴더 복원
if (!Directory.Exists(backupDirOrFile)) return false;
var dbFiles = Directory.GetFiles(backupDirOrFile, "*.db");
foreach (var dbFile in dbFiles)
{
var fileName = Path.GetFileName(dbFile);
if (fileName.Equals("Settings.db", StringComparison.OrdinalIgnoreCase))
{
SettingsDb.Restore(dbFile);
}
else
{
// UserDataPath에 복원
Directory.CreateDirectory(UserDataPath);
var destPath = Path.Combine(UserDataPath, fileName);
File.Copy(dbFile, destPath, true);
Helpers.DbFileHelper.DeleteSidecars(destPath); // 고아 -wal/-shm 제거
}
}
LoadAll();
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Settings] 복원 오류: {ex.Message}");
return false;
}
}
/// <summary>
/// 자동 백업 필요 여부 확인 및 실행
/// </summary>
public static string? RunAutoBackupIfNeeded()
{
if (!AutoBackup.Value) return null;
var lastBackup = LastBackupTime.Value;
if (!string.IsNullOrEmpty(lastBackup) && DateTime.TryParse(lastBackup, out var lastTime))
{
var interval = TimeSpan.FromDays(AutoBackupIntervalDays.Value);
if (DateTime.Now - lastTime < interval) return null;
}
return Backup();
}
/// <summary>
/// 오래된 백업 정리 (BackupRetentionCount 초과분 삭제)
/// </summary>
private static void CleanupOldBackups()
{
try
{
var backupsRoot = Path.Combine(UserDataPath, "Backups");
if (!Directory.Exists(backupsRoot)) return;
var dirs = Directory.GetDirectories(backupsRoot, "backup_*")
.OrderByDescending(d => d)
.ToArray();
var retainCount = BackupRetentionCount.Value;
for (int i = retainCount; i < dirs.Length; i++)
{
Directory.Delete(dirs[i], true);
System.Diagnostics.Debug.WriteLine($"[Settings] 오래된 백업 삭제: {dirs[i]}");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Settings] 백업 정리 오류: {ex.Message}");
}
}
/// <summary>
/// 백업 폴더 경로
/// </summary>
public static string BackupDirectory => Path.Combine(UserDataPath, "Backups");
/// <summary>
/// 디버그 출력
/// </summary>
public static void PrintAll() => SettingsDb.PrintAllSettings();
#region Windows 자동 시작
private const string StartupRegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private const string AppRegistryName = "SaemDesk";
/// <summary>
/// Windows 시작 시 자동 실행 설정/해제
/// </summary>
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void SetStartWithWindows(bool enable)
{
try
{
using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(StartupRegistryKey, true);
if (key == null) return;
if (enable)
{
var exePath = Environment.ProcessPath ?? System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
if (!string.IsNullOrEmpty(exePath))
key.SetValue(AppRegistryName, $"\"{exePath}\"");
}
else
{
key.DeleteValue(AppRegistryName, false);
}
StartWithWindows.Value = enable;
System.Diagnostics.Debug.WriteLine($"[Settings] StartWithWindows = {enable}");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Settings] 자동 시작 설정 오류: {ex.Message}");
}
}
/// <summary>
/// 현재 레지스트리에 자동 시작이 등록되어 있는지 확인
/// </summary>
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static bool IsStartWithWindowsRegistered()
{
try
{
using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(StartupRegistryKey);
return key?.GetValue(AppRegistryName) != null;
}
catch { return false; }
}
#endregion
}
/// <summary>
/// 내부 DB 관리 클래스
/// </summary>
internal static class SettingsDb
{
private static readonly string DbPath;
private static readonly string ConnectionString;
private static readonly Dictionary<string, string> _cache = new();
private static readonly object _lock = new();
private static bool _isInitialized = false;
static SettingsDb()
{
var dataDir = Settings.UserDataPath;
Directory.CreateDirectory(dataDir);
DbPath = Path.Combine(dataDir, "Settings.db");
ConnectionString = $"Data Source={DbPath};Cache=Shared";
}
public static void Initialize()
{
lock (_lock)
{
if (_isInitialized) return;
using var conn = new SqliteConnection(ConnectionString);
conn.Open();
var createSql = @"
CREATE TABLE IF NOT EXISTS Settings (
Key TEXT PRIMARY KEY,
Value TEXT NOT NULL,
Type TEXT NOT NULL,
Description TEXT,
Updated TEXT NOT NULL
)";
using var cmd = new SqliteCommand(createSql, conn);
cmd.ExecuteNonQuery();
// 캐시 로드
LoadCache(conn);
_isInitialized = true;
}
}
private static void LoadCache(SqliteConnection conn)
{
_cache.Clear();
var sql = "SELECT Key, Value FROM Settings";
using var cmd = new SqliteCommand(sql, conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
_cache[reader.GetString(0)] = reader.GetString(1);
}
}
public static string? Get(string key)
{
lock (_lock)
{
return _cache.TryGetValue(key, out var value) ? value : null;
}
}
public static void Set(string key, string value)
{
lock (_lock)
{
using var conn = new SqliteConnection(ConnectionString);
conn.Open();
var sql = @"
INSERT INTO Settings (Key, Value, Type, Description, Updated)
VALUES (@Key, @Value, '', '', @Updated)
ON CONFLICT(Key) DO UPDATE SET
Value = @Value,
Updated = @Updated";
using var cmd = new SqliteCommand(sql, conn);
cmd.Parameters.AddWithValue("@Key", key);
cmd.Parameters.AddWithValue("@Value", value ?? "");
cmd.Parameters.AddWithValue("@Updated", DateTime.UtcNow.ToString("o"));
cmd.ExecuteNonQuery();
_cache[key] = value ?? "";
}
}
public static void ResetToDefaults()
{
lock (_lock)
{
using var conn = new SqliteConnection(ConnectionString);
conn.Open();
var deleteSql = "DELETE FROM Settings";
using var cmd = new SqliteCommand(deleteSql, conn);
cmd.ExecuteNonQuery();
_cache.Clear();
}
}
public static string? Backup()
{
lock (_lock)
{
try
{
string backupFileName = $"appsettings_backup_{DateTime.Now:yyyyMMdd_HHmmss}.db";
string? dir = Path.GetDirectoryName(DbPath);
if (string.IsNullOrEmpty(dir))
throw new InvalidOperationException("DbPath의 디렉터리 경로를 확인할 수 없습니다.");
string backupPath = Path.Combine(dir, backupFileName);
Helpers.DbFileHelper.Checkpoint(DbPath); // WAL 병합 후 단일 파일 복사
File.Copy(DbPath, backupPath, true);
return backupPath;
}
catch
{
return null;
}
}
}
public static bool Restore(string backupPath)
{
lock (_lock)
{
try
{
if (File.Exists(backupPath))
{
SqliteConnection.ClearAllPools(); // 덮어쓰기 전 풀 핸들 해제
File.Copy(backupPath, DbPath, true);
Helpers.DbFileHelper.DeleteSidecars(DbPath); // 고아 -wal/-shm 제거
_isInitialized = false;
Initialize();
return true;
}
return false;
}
catch
{
return false;
}
}
}
public static void PrintAllSettings()
{
lock (_lock)
{
System.Diagnostics.Debug.WriteLine("=== Settings ===");
foreach (var kvp in _cache)
{
System.Diagnostics.Debug.WriteLine($"{kvp.Key}: {kvp.Value}");
}
System.Diagnostics.Debug.WriteLine("================");
}
}
}