1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Generic;
33 using System.Xml.Serialization;
34 using System.Windows.Forms;
35 using System.Threading;
39 abstract public class SettingBase<T> where T : class, new()
42 /// XML に含まれる追加データを保持するフィールド
45 /// 他バージョンから設定をコピーした場合など、クラスに存在しないフィールドが
46 /// XML に含まれていた場合に破棄せず保持するため必要となる。
49 public XmlElement[] ExtraElements;
51 private static object lockObj = new object();
53 protected static T LoadSettings(string FileId)
57 var settingFilePath = GetSettingFilePath(FileId);
58 if (!File.Exists(settingFilePath))
65 using (FileStream fs = new FileStream(settingFilePath, FileMode.Open, FileAccess.Read))
68 XmlSerializer xs = new XmlSerializer(typeof(T));
69 T instance = (T)xs.Deserialize(fs);
74 catch (FileNotFoundException)
80 string backupFile = Path.Combine(
82 Application.StartupPath,
83 ApplicationSettings.AssemblyName + "Backup1st"),
84 typeof(T).Name + FileId + ".xml");
85 if (File.Exists(backupFile))
91 using (FileStream fs = new FileStream(backupFile, FileMode.Open, FileAccess.Read))
94 XmlSerializer xs = new XmlSerializer(typeof(T));
95 T instance = (T)xs.Deserialize(fs);
96 MessageBox.Show("File: " + GetSettingFilePath(FileId) + Environment.NewLine + "Use old setting file, because application can't read this setting file.");
105 MessageBox.Show("File: " + GetSettingFilePath(FileId) + Environment.NewLine + "Use default setting, because application can't read this setting file.");
107 //ex.Data.Add("FilePath", GetSettingFilePath(FileId));
108 //FileInfo fi = new IO.FileInfo(GetSettingFilePath(FileId));
109 //ex.Data.Add("FileSize", fi.Length.ToString());
110 //ex.Data.Add("FileData", File.ReadAllText(GetSettingFilePath(FileId)));
115 protected static T LoadSettings()
117 return LoadSettings("");
120 protected static void SaveSettings(T instance, string fileId)
122 const int SaveRetryMax = 3;
124 if (instance == null)
128 Exception lastException = null;
130 var filePath = GetSettingFilePath(fileId);
133 var tmpfilePath = GetSettingFilePath("_" + Path.GetRandomFileName());
138 using (var stream = new FileStream(tmpfilePath, FileMode.Create, FileAccess.Write))
140 var serializer = new XmlSerializer(typeof(T));
141 serializer.Serialize(stream, instance);
145 var fileInfo = new FileInfo(tmpfilePath);
146 if (fileInfo.Length != 0)
149 File.Copy(tmpfilePath, filePath, true);
162 if (File.Exists(tmpfilePath))
163 File.Delete(tmpfilePath);
165 catch (Exception) { }
172 } while (retryCount <= SaveRetryMax);
175 if (lastException != null)
176 MyCommon.ExceptionOut(lastException);
178 MessageBox.Show("Can't write setting XML.(" + filePath + ")", "Save Settings", MessageBoxButtons.OK);
181 protected static void SaveSettings(T Instance)
183 SaveSettings(Instance, "");
186 public static string GetSettingFilePath(string FileId)
188 return Path.Combine(MyCommon.settingPath, typeof(T).Name + FileId + ".xml");