using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
namespace MyNewsReader.Models
{
public enum UILanguage {
SimplifiedChinese,
TraditionalChinese,
English
}
class ProfileManager
{
public Profile Profile = new Profile();
public ProfileManager()
{
//Init();
}
//默认频道设置
public void ChannelReset()
{
Profile.Feeds.Clear();
//教学时 局域网内可以采用以下初始值
//AtomFeed gnews = new AtomFeed("Google新闻", "http://localhost/mynewsreader/gnews.xml", "Google 内地新闻");
//RssFeed sinanews = new RssFeed("新浪新闻", "http://localhost/mynewsreader/sinanews.xml", "新浪新闻");
//RssFeed sasu = new RssFeed("萨苏", "http://localhost/mynewsreader/sasu.xml", "萨苏的 Blog");
//RssFeed lvqiu = new RssFeed("闾丘露薇", "http://localhost/mynewsreader/lvqiu.xml", "闾丘露薇的 Blog");
//RssFeed liang = new RssFeed("张靓颖", "http://localhost/mynewsreader/liang.xml", "张靓颖的 Blog");
//RssFeed fbb = new RssFeed("范冰冰", "http://localhost/mynewsreader/fbb.xml", "范冰冰的 Blog");
//AtomFeed gnews = new AtomFeed("Google新闻", "http://news.google.com/nwshp?tab=wn&hl=zh-CN®ion=cn&topic=n&output=atom&ned=:ePkh8BM9EwLbwQe0Q4q1KkPX2Q9oAJHWAQAM3wlj", "Google 内地新闻");
RssFeed sinanews = new RssFeed("新浪新闻", "http://rss.sina.com.cn/news/marquee/ddt.xml", "新浪新闻",0);
RssFeed sasu = new RssFeed("萨苏", "http://blog.sina.com.cn/myblog/index_rss.php?uid=1197950454", "萨苏的 Blog",0);
RssFeed lvqiu = new RssFeed("闾丘露薇", "http://blog.phoenixtv.com/index.php/uid_674832_action_rss_type_blog", "闾丘露薇的 Blog",0);
RssFeed liang = new RssFeed("张靓颖", "http://blog.sina.com.cn/myblog/index_rss.php?uid=1173538795", "张靓颖的 Blog",0);
RssFeed fbb = new RssFeed("范冰冰", "http://blog.sina.com.cn/myblog/index_rss.php?uid=1192147255", "范冰冰的 Blog",0);
//Profile.Feeds.Add(gnews);
Profile.Feeds.Add(sinanews);
Profile.Feeds.Add(sasu);
Profile.Feeds.Add(lvqiu);
Profile.Feeds.Add(liang);
Profile.Feeds.Add(fbb);
}
//保存用户配置信息
public void Save()
{
FileStream fileStream = null;
try
{
fileStream = new FileStream("profile.bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fileStream, Profile);
/*FileStream fs = new FileStream("user.profile", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//写入频道总数
sw.WriteLine(Profile.Feeds.Count);
foreach (FeedBase feed in Profile.Feeds)
{
if (feed is RssFeed) { sw.WriteLine("RSS"); }
if (feed is AtomFeed) { sw.WriteLine("Atom"); }
//Feed 的三个基本信息每行存一个
sw.WriteLine(feed.DisplayName);
sw.WriteLine(feed.Url);
sw.WriteLine(feed.Description);
}
sw.WriteLine(Profile.EnableFeedsAutoUpdate);
sw.WriteLine(Profile.IntervalToRefresh.ToString());
sw.WriteLine(Profile.EnableNewVersionAutoDetection.ToString());
sw.WriteLine(Profile.EnableProxy.ToString());
sw.WriteLine(Profile.ProxyName.ToString());
sw.WriteLine(Profile.ProxyPort.ToString());
sw.WriteLine(Profile.ProxyUserId.ToString());
sw.WriteLine(Profile.ProxyUserPwd.ToString());
sw.WriteLine(Profile.uiLanguage.ToString());
sw.Close();
fs.Close();
}*/
}
catch (Exception ex)
{
throw;
}
finally {
fileStream.Close();
}
}
//读取用户配置信息
public void Load()
{
string type;
string display; //频道标题
string url; //频道Url
string desc; //频道描述
int feedsCount; //频道个数
FileStream fileStream=null;
try
{
fileStream = new FileStream("profile.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
Profile = (Profile)bf.Deserialize(fileStream);
/*FileStream fs = new FileStream("user.profile", FileMode.Open);
StreamReader sr = new StreamReader(fs);
feedsCount = int.Parse(sr.ReadLine());
//feedsCount = Convert.ToInt32(sr.ReadLine());
Profile.Feeds.Clear();
for (int i = 0; i < feedsCount; i++)
{
type = sr.ReadLine();
display = sr.ReadLine();
url = sr.ReadLine();
desc = sr.ReadLine();
if (type == "RSS")
{
Profile.Feeds.Add(new RssFeed(display, url, desc, 0));
}
if (type == "Atom")
{
Profile.Feeds.Add(new AtomFeed(display, url, desc, 0));
}
Profile.EnableFeedsAutoUpdate = Boolean.Parse(sr.ReadLine());
Profile.IntervalToRefresh = int.Parse(sr.ReadLine());
Profile.EnableNewVersionAutoDetection = Boolean.Parse(sr.ReadLine());
Profile.EnableProxy = Boolean.Parse(sr.ReadLine());
Profile.ProxyName = sr.ReadLine();
Profile.ProxyPort = sr.ReadLine();
Profile.ProxyUserId = sr.ReadLine();
Profile.ProxyUserPwd = sr.ReadLine();
Profile.uiLanguage = (UILanguage)(Enum.Parse(typeof(UILanguage), sr.ReadLine()));*/
}
//sr.Close();
//fs.Close();
catch (Exception ex)
{
this.ChannelReset();
}
finally {
fileStream.Close();
}
}
}
}