OSDN Git Service

更新間隔の最小値を15秒以上とする制限を廃止
[opentween/open-tween.git] / OpenTween / ApplicationEvents.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2012 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2012 Moz (@syo68k)
4 //           (c) 2008-2012 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2012 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2012 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2012      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 //           (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 #nullable enable
29
30 using System;
31 using System.Windows.Forms;
32 using OpenTween.Connection;
33 using OpenTween.Setting;
34
35 namespace OpenTween
36 {
37     internal class ApplicationEvents
38     {
39         /// <summary>
40         /// 起動時に指定されたオプションを取得します
41         /// </summary>
42         public static CommandLineArgs StartupOptions { get; private set; } = null!;
43
44         /// <summary>
45         /// アプリケーションのメイン エントリ ポイントです。
46         /// </summary>
47         [STAThread]
48         public static int Main(string[] args)
49         {
50             Application.EnableVisualStyles();
51             Application.SetCompatibleTextRenderingDefault(false);
52
53             using var errorReportHandler = new ErrorReportHandler();
54
55             StartupOptions = new(args);
56             InitializeTraceFrag();
57
58             if (!ApplicationPreconditions.CheckAll())
59                 return 1;
60
61             var settingsPath = SettingManager.DetermineSettingsPath(StartupOptions);
62             if (MyCommon.IsNullOrEmpty(settingsPath))
63                 return 1;
64
65             var settings = new SettingManager(settingsPath);
66             settings.LoadAll();
67
68             using var container = new ApplicationContainer(settings);
69
70             settings.Common.Validate();
71
72             ThemeManager.ApplyGlobalUIFont(settings.Local);
73             container.CultureService.Initialize();
74
75             Networking.Initialize();
76             settings.ApplySettings();
77
78             // 同じ設定ファイルを使用する OpenTween プロセスの二重起動を防止する
79             using var mutex = new ApplicationInstanceMutex(ApplicationSettings.AssemblyName, settings.SettingsPath);
80
81             if (mutex.InstanceExists)
82             {
83                 var text = string.Format(MyCommon.ReplaceAppName(Properties.Resources.StartupText1), ApplicationSettings.AssemblyName);
84                 MessageBox.Show(text, MyCommon.ReplaceAppName(Properties.Resources.StartupText2), MessageBoxButtons.OK, MessageBoxIcon.Information);
85
86                 mutex.TryActivatePreviousInstance();
87                 return 1;
88             }
89
90             if (settings.IsIncomplete)
91             {
92                 var completed = ShowSettingsDialog(settings, container.IconAssetsManager);
93                 if (!completed)
94                     return 1; // 設定が完了しなかったため終了
95             }
96
97             Application.Run(container.MainForm);
98
99             return 0;
100         }
101
102         private static void InitializeTraceFrag()
103         {
104             var traceFlag = false;
105
106 #if DEBUG
107             traceFlag = true;
108 #endif
109
110             if (StartupOptions.ContainsKey("d"))
111                 traceFlag = true;
112
113             var version = Version.Parse(MyCommon.FileVersion);
114             if (version.Build != 0)
115                 traceFlag = true;
116
117             MyCommon.TraceFlag = traceFlag;
118         }
119
120         private static bool ShowSettingsDialog(SettingManager settings, IconAssetsManager iconAssets)
121         {
122             using var settingDialog = new AppendSettingDialog();
123             settingDialog.Icon = iconAssets.IconMain;
124             settingDialog.ShowInTaskbar = true; // この時点では TweenMain が表示されていないため代わりに表示する
125             settingDialog.LoadConfig(settings.Common, settings.Local);
126
127             var ret = settingDialog.ShowDialog();
128             if (ret != DialogResult.OK)
129                 return false;
130
131             settingDialog.SaveConfig(settings.Common, settings.Local);
132
133             if (settings.IsIncomplete)
134                 return false;
135
136             settings.SaveAll();
137             return true;
138         }
139     }
140 }