summaryrefslogtreecommitdiffstats
path: root/MainWindow.xaml.cs
blob: 380b3cc0985d5d964c570971ab8566d24323cb0e (plain)
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
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ftpmpv {
	public partial class MainWindow : Window {
		string currentPath;

		public MainWindow() {
			InitializeComponent();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e) {
			TreeViewItem tvi = new TreeViewItem() {Header = "/"};
			dirs.Items.Add(tvi);
			listDir("/", tvi);
		}

		void listDir(string path, TreeViewItem tree) {
			FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xis" + path);
			request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
			FtpWebResponse response = (FtpWebResponse)request.GetResponse();
			StreamReader sr = new StreamReader(response.GetResponseStream());

			files.Items.Clear();
			tree.Items.Clear();

			while (true) {
				string line = sr.ReadLine();
				if (line == null || line.Length == 0)
					break;
				string[] split = line.Split(new char[]{' '}, 9, StringSplitOptions.RemoveEmptyEntries);

				string mode = split[0];
				string name = split[8];
				if (mode[0] == 'd') {
					TreeViewItem tvi = new TreeViewItem() {Header = name};
					tree.Items.Add(tvi);
				} else
					files.Items.Add(name);
			}

			tree.ExpandSubtree();
			currentPath = path;
		}

		private void dirs_KeyUp(object sender, KeyEventArgs e) {
			if (e.Key == Key.Enter)
				dirsActivate();
		}

		private void dirs_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
			dirsActivate();
		}

		private void dirsActivate() {
			TreeViewItem tvi = (TreeViewItem)dirs.SelectedItem;

			string path = "";
			TreeViewItem cur = tvi;
			while (true) {
				path = "/" + (string)cur.Header + path;
				if (cur.Parent != null && cur.Parent.GetType() == typeof(TreeViewItem))
					cur = (TreeViewItem)cur.Parent;
				else
					break;
			}
			path += "/";

			listDir(path, tvi);
		}

		private void files_KeyUp(object sender, KeyEventArgs e) {
			if (e.Key == Key.Enter)
				filesActivate();
		}

		private void files_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
			filesActivate();
		}

		private void filesActivate() {
			string filename = (string)files.SelectedItem;
			Console.WriteLine("ftp://xis" + currentPath + filename);
			System.Diagnostics.Process.Start(@"D:\PRGMs\mpv\mpv.exe", "\"ftp://xis" + currentPath + filename + "\"");
		}
	}
}