-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
164 lines (144 loc) · 4.97 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class MainForm : Form
{
private BindingList<BlogItem> bindingList;
private bool readingRss;
private string currentFileName;
public MainForm()
{
InitializeComponent();
splitContainer2.SplitterDistance = splitContainer2.Height - textBox2.Height;
openFileDialog1.Filter = "RSSリスト|*.txt";
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFileDialog1.FileName = "rsslist.txt";
saveFileDialog1.Filter = "RSSリスト|*.txt";
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveFileDialog1.FileName = "rsslist.txt";
bindingList = new BindingList<BlogItem>();
dgvBlogList.DataSource = bindingList;
readingRss = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
}
private async void ReadRssFromFile(string path)
{
if (readingRss)
{
MessageBox.Show("RSSリストの読み込みが完了していません。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
readingRss = true;
SetCurrentFile(path);
bindingList.Clear();
foreach (var line in File.ReadLines(path))
{
toolStripStatusLabel1.Text = "RSS取得中: " + line;
var blog = await Task.Run(() => Blog.FromRssUriOrEmpty(line));
bindingList.Add(new BlogItem(blog));
}
dgvBlogList.AutoResizeColumns();
toolStripStatusLabel1.Text = "RSSリスト読み込み完了";
readingRss = false;
}
private bool CheckFilePath(string path)
{
if (File.Exists(path))
{
return true;
}
else
{
MessageBox.Show("指定されたファイルは存在しません。\r\nパス: " + path, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private void SetCurrentFile(string path)
{
currentFileName = path;
Text = Path.GetFileName(path) + " - " + Settings.Title;
}
private void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
var paths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
if (CheckFilePath(paths[0]))
{
ReadRssFromFile(paths[0]);
}
}
private void dgvBlogList_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (bindingList.Count == 0) return;
var blogItem = bindingList[e.RowIndex];
textBox1.Clear();
var sb = new StringBuilder();
int i = 1;
foreach (var article in blogItem.GetBlog().Articles)
{
sb.AppendLine(i.ToString("00") + " " + article.Title);
i++;
}
textBox1.Text = sb.ToString();
}
private void openStripMenuItem_Click(object sender, EventArgs e)
{
var result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
if (CheckFilePath(openFileDialog1.FileName))
{
ReadRssFromFile(openFileDialog1.FileName);
}
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
File.WriteAllLines(saveFileDialog1.FileName, bindingList.Select(item => item.RssUri).OrderBy(line => line));
SetCurrentFile(saveFileDialog1.FileName);
toolStripStatusLabel1.Text = "保存完了: " + currentFileName;
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (currentFileName == null)
{
saveAsToolStripMenuItem_Click(sender, e);
}
else
{
File.WriteAllLines(currentFileName, bindingList.Select(item => item.RssUri).OrderBy(line => line));
toolStripStatusLabel1.Text = "保存完了: " + currentFileName;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private async void button1_Click(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "RSS取得中: " + textBox2.Text;
var blog = await Task.Run(() => Blog.FromRssUriOrEmpty(textBox2.Text));
bindingList.Add(new BlogItem(blog));
toolStripStatusLabel1.Text = "RSS取得完了";
}
}