OSDN Git Service

設定ファイルの読み込み・保存に関するコードをSettingManagerクラスに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 15 Feb 2017 17:07:40 +0000 (02:07 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 16 Feb 2017 14:37:10 +0000 (23:37 +0900)
OpenTween.Tests/TestUtils.cs
OpenTween.Tests/TwitterTest.cs
OpenTween/OpenTween.csproj
OpenTween/Setting/SettingCommon.cs
OpenTween/Setting/SettingManager.cs [new file with mode: 0644]
OpenTween/Tween.cs

index 1bdd046..d776369 100644 (file)
@@ -91,3 +91,33 @@ namespace OpenTween
         }
     }
 }
+
+namespace OpenTween.Setting
+{
+    public class SettingManagerTest
+    {
+        public static SettingCommon Common
+        {
+            get { return SettingManager.Common; }
+            set { SettingManager.Common = value; }
+        }
+
+        public static SettingLocal Local
+        {
+            get { return SettingManager.Local; }
+            set { SettingManager.Local = value; }
+        }
+
+        public static SettingTabs Tabs
+        {
+            get { return SettingManager.Tabs; }
+            set { SettingManager.Tabs = value; }
+        }
+
+        public static SettingAtIdList AtIdList
+        {
+            get { return SettingManager.AtIdList; }
+            set { SettingManager.AtIdList = value; }
+        }
+    }
+}
index 172bfc3..4ca8985 100644 (file)
@@ -26,6 +26,7 @@ using System.Text;
 using System.Text.RegularExpressions;
 using OpenTween.Api.DataModel;
 using OpenTween.Models;
+using OpenTween.Setting;
 using Xunit;
 using Xunit.Extensions;
 
@@ -208,8 +209,8 @@ namespace OpenTween
         [Fact]
         public void GetApiResultCount_DefaultTest()
         {
-            var oldInstance = SettingCommon.Instance;
-            SettingCommon.Instance = new SettingCommon();
+            var oldInstance = SettingManagerTest.Common;
+            SettingManagerTest.Common = new SettingCommon();
 
             var timeline = SettingCommon.Instance.CountApi;
             var reply = SettingCommon.Instance.CountApiReply;
@@ -246,14 +247,14 @@ namespace OpenTween
             Assert.Equal(timeline, Twitter.GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, false, false));
             Assert.Equal(timeline, Twitter.GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, false, false));
 
-            SettingCommon.Instance = oldInstance;
+            SettingManagerTest.Common = oldInstance;
         }
 
         [Fact]
         public void GetApiResultCount_AdditionalCountTest()
         {
-            var oldInstance = SettingCommon.Instance;
-            SettingCommon.Instance = new SettingCommon();
+            var oldInstance = SettingManagerTest.Common;
+            SettingManagerTest.Common = new SettingCommon();
 
             var timeline = SettingCommon.Instance.CountApi;
             var reply = SettingCommon.Instance.CountApiReply;
@@ -325,7 +326,7 @@ namespace OpenTween
             Assert.Equal(more, Twitter.GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, true, false));
             Assert.Equal(startup, Twitter.GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, false, true));
 
-            SettingCommon.Instance = oldInstance;
+            SettingManagerTest.Common = oldInstance;
         }
 
         [Fact]
index c46d139..2810c59 100644 (file)
     <Compile Include="SendErrorReportForm.Designer.cs">
       <DependentUpon>SendErrorReportForm.cs</DependentUpon>
     </Compile>
+    <Compile Include="Setting\SettingManager.cs" />
     <Compile Include="ShortcutCommand.cs" />
     <Compile Include="Thumbnail\Services\PbsTwimgCom.cs" />
     <Compile Include="TweetDetailsView.cs">
index 3343199..ac7d1cf 100644 (file)
@@ -29,17 +29,13 @@ using System.Xml.Serialization;
 using System.Collections.Generic;
 using System.Windows.Forms;
 using OpenTween.Thumbnail;
+using OpenTween.Setting;
 
 namespace OpenTween
 {
     public class SettingCommon : SettingBase<SettingCommon>
     {
-        public static SettingCommon Instance { get; internal set; }
-
-        static SettingCommon()
-        {
-            Instance = new SettingCommon();
-        }
+        public static SettingCommon Instance => SettingManager.Common;
 
         #region "Settingクラス基本"
         public static SettingCommon Load()
diff --git a/OpenTween/Setting/SettingManager.cs b/OpenTween/Setting/SettingManager.cs
new file mode 100644 (file)
index 0000000..9f2077c
--- /dev/null
@@ -0,0 +1,91 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// 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 3 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, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OpenTween.Setting
+{
+    public static class SettingManager
+    {
+        public static SettingCommon Common { get; internal set; } = new SettingCommon();
+
+        public static SettingLocal Local { get; internal set; } = new SettingLocal();
+
+        public static SettingTabs Tabs { get; internal set; } = new SettingTabs();
+
+        public static SettingAtIdList AtIdList { get; internal set; } = new SettingAtIdList();
+
+        public static void LoadAll()
+        {
+            LoadCommon();
+            LoadLocal();
+            LoadTabs();
+            LoadAtIdList();
+        }
+
+        public static void LoadCommon()
+        {
+            var settings = SettingCommon.Load();
+
+            if (settings.UserAccounts == null || settings.UserAccounts.Count == 0)
+            {
+                settings.UserAccounts = new List<UserAccount>();
+                if (!string.IsNullOrEmpty(settings.UserName))
+                {
+                    UserAccount account = new UserAccount();
+                    account.Username = settings.UserName;
+                    account.UserId = settings.UserId;
+                    account.Token = settings.Token;
+                    account.TokenSecret = settings.TokenSecret;
+
+                    settings.UserAccounts.Add(account);
+                }
+            }
+
+            SettingManager.Common = settings;
+        }
+
+        public static void LoadLocal()
+            => SettingManager.Local = SettingLocal.Load();
+
+        public static void LoadTabs()
+            => SettingManager.Tabs = SettingTabs.Load();
+
+        public static void LoadAtIdList()
+            => SettingManager.AtIdList = SettingAtIdList.Load();
+
+        public static void SaveCommon()
+            => SettingManager.Common.Save();
+
+        public static void SaveLocal()
+            => SettingManager.Local.Save();
+
+        public static void SaveTabs()
+            => SettingManager.Tabs.Save();
+
+        public static void SaveAtIdList()
+            => SettingManager.AtIdList.Save();
+    }
+}
index a77407a..f508b6a 100644 (file)
@@ -52,6 +52,7 @@ using OpenTween.Api.DataModel;
 using OpenTween.Connection;
 using OpenTween.Models;
 using OpenTween.OpenTweenCustomControl;
+using OpenTween.Setting;
 using OpenTween.Thumbnail;
 
 namespace OpenTween
@@ -844,7 +845,7 @@ namespace OpenTween
             ImageSelector.Initialize(tw, this.tw.Configuration, _cfgCommon.UseImageServiceName, _cfgCommon.UseImageService);
 
             //ハッシュタグ/@id関連
-            AtIdSupl = new AtIdSupplement(SettingAtIdList.Load().AtIdList, "@");
+            AtIdSupl = new AtIdSupplement(SettingManager.AtIdList.AtIdList, "@");
             HashSupl = new AtIdSupplement(_cfgCommon.HashTags, "#");
             HashMgr = new HashtagManage(HashSupl,
                                     _cfgCommon.HashTags.ToArray(),
@@ -1197,31 +1198,17 @@ namespace OpenTween
 
         private void LoadConfig()
         {
-            _cfgCommon = SettingCommon.Load();
-            SettingCommon.Instance = this._cfgCommon;
-            if (_cfgCommon.UserAccounts == null || _cfgCommon.UserAccounts.Count == 0)
-            {
-                _cfgCommon.UserAccounts = new List<UserAccount>();
-                if (!string.IsNullOrEmpty(_cfgCommon.UserName))
-                {
-                    UserAccount account = new UserAccount();
-                    account.Username = _cfgCommon.UserName;
-                    account.UserId = _cfgCommon.UserId;
-                    account.Token = _cfgCommon.Token;
-                    account.TokenSecret = _cfgCommon.TokenSecret;
-
-                    _cfgCommon.UserAccounts.Add(account);
-                }
-            }
+            SettingManager.LoadAll();
 
-            _cfgLocal = SettingLocal.Load();
+            this._cfgCommon = SettingManager.Common;
+            this._cfgLocal = SettingManager.Local;
 
             // v1.2.4 以前の設定には ScaleDimension の項目がないため、現在の DPI と同じとして扱う
             if (_cfgLocal.ScaleDimension.IsEmpty)
                 _cfgLocal.ScaleDimension = this.CurrentAutoScaleDimensions;
 
-            var tabsSetting = SettingTabs.Load().Tabs;
-            foreach (var tabSetting in tabsSetting)
+            var tabSettings = SettingManager.Tabs;
+            foreach (var tabSetting in tabSettings.Tabs)
             {
                 TabModel tab;
                 switch (tabSetting.TabType)
@@ -7211,8 +7198,8 @@ namespace OpenTween
             if (_ignoreConfigSave || !this._cfgCommon.UseAtIdSupplement && AtIdSupl == null) return;
 
             ModifySettingAtId = false;
-            SettingAtIdList cfgAtId = new SettingAtIdList(AtIdSupl.GetItemList());
-            cfgAtId.Save();
+            SettingManager.AtIdList.AtIdList = this.AtIdSupl.GetItemList();
+            SettingManager.SaveAtIdList();
         }
 
         private void SaveConfigsCommon()
@@ -7275,7 +7262,7 @@ namespace OpenTween
                 _cfgCommon.UseImageService = ImageSelector.ServiceIndex;
                 _cfgCommon.UseImageServiceName = ImageSelector.ServiceName;
 
-                _cfgCommon.Save();
+                SettingManager.SaveCommon();
             }
         }
 
@@ -7316,13 +7303,13 @@ namespace OpenTween
                 _cfgLocal.FontInputFont = _fntInputFont;
 
                 if (_ignoreConfigSave) return;
-                _cfgLocal.Save();
+                SettingManager.SaveLocal();
             }
         }
 
         private void SaveConfigsTabs()
         {
-            var tabsSetting = new SettingTabs();
+            var tabSettingList = new List<SettingTabs.SettingTabItem>();
 
             var tabs = this.ListTab.TabPages.Cast<TabPage>()
                 .Select(x => this._statuses.Tabs[x.Text])
@@ -7362,10 +7349,11 @@ namespace OpenTween
                 if (listTab != null)
                     tabSetting.ListInfo = listTab.ListInfo;
 
-                tabsSetting.Tabs.Add(tabSetting);
+                tabSettingList.Add(tabSetting);
             }
 
-            tabsSetting.Save();
+            SettingManager.Tabs.Tabs = tabSettingList;
+            SettingManager.SaveTabs();
         }
 
         private async void OpenURLFileMenuItem_Click(object sender, EventArgs e)