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.
11 // This file is part of OpenTween.
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)
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
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.
31 using System.Windows.Forms;
32 using OpenTween.Connection;
33 using OpenTween.Setting;
37 internal class ApplicationEvents
40 /// 起動時に指定されたオプションを取得します
42 public static CommandLineArgs StartupOptions { get; private set; } = null!;
45 /// アプリケーションのメイン エントリ ポイントです。
48 public static int Main(string[] args)
50 Application.EnableVisualStyles();
51 Application.SetCompatibleTextRenderingDefault(false);
53 using var errorReportHandler = new ErrorReportHandler();
55 StartupOptions = new(args);
56 InitializeTraceFrag();
58 if (!ApplicationPreconditions.CheckAll())
61 var settingsPath = SettingManager.DetermineSettingsPath(StartupOptions);
62 if (MyCommon.IsNullOrEmpty(settingsPath))
65 var settings = new SettingManager(settingsPath);
68 using var container = new ApplicationContainer(settings);
70 settings.Common.Validate();
72 ThemeManager.ApplyGlobalUIFont(settings.Local);
73 container.CultureService.Initialize();
75 Networking.Initialize();
76 settings.ApplySettings();
78 // 同じ設定ファイルを使用する OpenTween プロセスの二重起動を防止する
79 using var mutex = new ApplicationInstanceMutex(ApplicationSettings.AssemblyName, settings.SettingsPath);
81 if (mutex.InstanceExists)
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);
86 mutex.TryActivatePreviousInstance();
90 if (settings.IsIncomplete)
92 var completed = ShowSettingsDialog(settings, container.IconAssetsManager);
94 return 1; // 設定が完了しなかったため終了
97 SetupTwitter(container.Twitter, settings);
99 Application.Run(container.MainForm);
104 private static void InitializeTraceFrag()
106 var traceFlag = false;
112 if (StartupOptions.ContainsKey("d"))
115 var version = Version.Parse(MyCommon.FileVersion);
116 if (version.Build != 0)
119 MyCommon.TraceFlag = traceFlag;
122 private static bool ShowSettingsDialog(SettingManager settings, IconAssetsManager iconAssets)
124 using var settingDialog = new AppendSettingDialog();
125 settingDialog.Icon = iconAssets.IconMain;
126 settingDialog.ShowInTaskbar = true; // この時点では TweenMain が表示されていないため代わりに表示する
127 settingDialog.LoadConfig(settings.Common, settings.Local);
129 var ret = settingDialog.ShowDialog();
130 if (ret != DialogResult.OK)
133 settingDialog.SaveConfig(settings.Common, settings.Local);
135 if (settings.IsIncomplete)
142 private static void SetupTwitter(Twitter tw, SettingManager settings)
144 var account = settings.Common.SelectedAccount;
146 tw.Initialize(account.GetTwitterCredential(), account.Username, account.UserId);
148 tw.Initialize(new TwitterCredentialNone(), "", 0L);
150 tw.RestrictFavCheck = settings.Common.RestrictFavCheck;
151 tw.ReadOwnPost = settings.Common.ReadOwnPost;
153 // アクセストークンが有効であるか確認する
154 // ここが Twitter API への最初のアクセスになるようにすること
157 tw.VerifyCredentials();
159 catch (WebApiException ex)
162 string.Format(Properties.Resources.StartupAuthError_Text, ex.Message),
163 ApplicationSettings.ApplicationName,
164 MessageBoxButtons.OK,
165 MessageBoxIcon.Warning