OSDN Git Service

AccountCollectionクラスを追加
[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 using OpenTween.SocialProtocol;
35
36 namespace OpenTween
37 {
38     internal class ApplicationEvents
39     {
40         /// <summary>
41         /// 起動時に指定されたオプションを取得します
42         /// </summary>
43         public static CommandLineArgs StartupOptions { get; private set; } = null!;
44
45         /// <summary>
46         /// アプリケーションのメイン エントリ ポイントです。
47         /// </summary>
48         [STAThread]
49         public static int Main(string[] args)
50         {
51             Application.EnableVisualStyles();
52             Application.SetCompatibleTextRenderingDefault(false);
53
54             using var errorReportHandler = new ErrorReportHandler();
55
56             StartupOptions = new(args);
57             InitializeTraceFrag();
58
59             if (!ApplicationPreconditions.CheckAll())
60                 return 1;
61
62             var settingsPath = SettingManager.DetermineSettingsPath(StartupOptions);
63             if (MyCommon.IsNullOrEmpty(settingsPath))
64                 return 1;
65
66             var settings = new SettingManager(settingsPath);
67             settings.LoadAll();
68
69             using var container = new ApplicationContainer(settings);
70
71             settings.Common.Validate();
72
73             ThemeManager.ApplyGlobalUIFont(settings.Local);
74             container.CultureService.Initialize();
75
76             Networking.Initialize();
77             settings.ApplySettings();
78
79             // 同じ設定ファイルを使用する OpenTween プロセスの二重起動を防止する
80             using var mutex = new ApplicationInstanceMutex(ApplicationSettings.AssemblyName, settings.SettingsPath);
81
82             if (mutex.InstanceExists)
83             {
84                 var text = string.Format(MyCommon.ReplaceAppName(Properties.Resources.StartupText1), ApplicationSettings.AssemblyName);
85                 MessageBox.Show(text, MyCommon.ReplaceAppName(Properties.Resources.StartupText2), MessageBoxButtons.OK, MessageBoxIcon.Information);
86
87                 mutex.TryActivatePreviousInstance();
88                 return 1;
89             }
90
91             if (settings.IsIncomplete)
92             {
93                 var completed = ShowSettingsDialog(settings, container.IconAssetsManager);
94                 if (!completed)
95                     return 1; // 設定が完了しなかったため終了
96             }
97
98             SetupAccounts(container.AccountCollection, settings);
99
100             Application.Run(container.MainForm);
101
102             return 0;
103         }
104
105         private static void InitializeTraceFrag()
106         {
107             var traceFlag = false;
108
109 #if DEBUG
110             traceFlag = true;
111 #endif
112
113             if (StartupOptions.ContainsKey("d"))
114                 traceFlag = true;
115
116             var version = Version.Parse(MyCommon.FileVersion);
117             if (version.Build != 0)
118                 traceFlag = true;
119
120             MyCommon.TraceFlag = traceFlag;
121         }
122
123         private static bool ShowSettingsDialog(SettingManager settings, IconAssetsManager iconAssets)
124         {
125             using var settingDialog = new AppendSettingDialog();
126             settingDialog.Icon = iconAssets.IconMain;
127             settingDialog.ShowInTaskbar = true; // この時点では TweenMain が表示されていないため代わりに表示する
128             settingDialog.LoadConfig(settings.Common, settings.Local);
129
130             var ret = settingDialog.ShowDialog();
131             if (ret != DialogResult.OK)
132                 return false;
133
134             settingDialog.SaveConfig(settings.Common, settings.Local);
135
136             if (settings.IsIncomplete)
137                 return false;
138
139             settings.SaveAll();
140             return true;
141         }
142
143         private static void SetupAccounts(AccountCollection accounts, SettingManager settings)
144         {
145             accounts.LoadFromSettings(settings.Common);
146
147             // アクセストークンが有効であるか確認する
148             // ここが Twitter API への最初のアクセスになるようにすること
149             try
150             {
151                 accounts.Primary.VerifyCredentials();
152             }
153             catch (WebApiException ex)
154             {
155                 MessageBox.Show(
156                     string.Format(Properties.Resources.StartupAuthError_Text, ex.Message),
157                     ApplicationSettings.ApplicationName,
158                     MessageBoxButtons.OK,
159                     MessageBoxIcon.Warning
160                 );
161             }
162         }
163     }
164 }