⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newjobform.cs

📁 一个使用.NET Framework开发的、免费的、开发源码的下载管理程序.
💻 CS
字号:
/************************************************************

NewJobForm.cs

Used to display a form which allows the user to enter details
for a new download.

Revision history:
	November 14, 2000, by Scott Glasgow
		Changed the event handlers to work under .NET Beta 1
	November 16, 2000, by Scott Glasgow
		Made NewJob default to directory application is running from.
		Added directory exists checking. Asks user if they want it created if it doesn't exist.
	November 17, 2000 by Joe Hardy
		- All code was split into modules from downloader.cs (now obsolete)
		- Modified GetFilename(), stopped it parsing the URL if the user was still entering a hostname
		  (checks if the url contains at least two forward slashes)
	November 25, 2000 by Joe Hardy
		- Now appends a backslash to the localPath property when the OK event is fired, if there isn't
		  one already
	November 26, 2000 by Joe Hardy
		- Now remembers the last path the user entered
	Late June - 6 July, 2000 by Joe Hardy
		- Cut all the code across to work with beta 2

************************************************************/

using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Threading;

// the following window is created when the user wants to make a new job. most of it should be
// reasonably self explanatory.
public class NewJobForm : Form {
	Label urlBoxDesc = new Label();
	TextBox urlBox = new TextBox();
	Label localFilePathDesc = new Label();
	TextBox localFilePath = new TextBox();
	Label localFileNameDesc = new Label();
	TextBox localFileName = new TextBox();

	Button cmdGo = new Button();
	Button cmdCancel = new Button();
	Button cmdBrowse = new Button();
	bool dontupdatefilename=false;
	public Directory directory;

	public NewJobForm() {
		this.Size = new Size(417, 150);
		this.Text = "Create New Job";
		this.FormBorderStyle = FormBorderStyle.FixedSingle;
		this.MinimizeBox=false;
		this.MaximizeBox=false;
		this.Font = new Font(new FontFamily("Tahoma"), 8);
		this.AcceptButton = cmdGo;
		this.CancelButton = cmdCancel;

		urlBoxDesc.Text = "&URL:";
		urlBoxDesc.AutoSize = true;
		urlBoxDesc.Location = new Point(8, 8);
		urlBoxDesc.TabIndex=0;

		urlBox.Location = new Point(45, 7);
		urlBox.Size = new Size(350, 20);
		urlBox.TabIndex=1;
		urlBox.TextChanged += new EventHandler(this.urlBox_OnChange);

		localFilePathDesc.Text = "&Dest. Directory:";
		localFilePathDesc.AutoSize = true;
		localFilePathDesc.Location = new Point(8, 35);
		localFilePathDesc.TabIndex=3;

		localFilePath.Location = new Point(96, 34);
		localFilePath.Size = new Size(220, 20);
		localFilePath.TabIndex=4;

		string lastpath = AppSettings.GetSetting("LastNewJobPath");
		if (lastpath!=null)
			localFilePath.Text = lastpath;
		else
			localFilePath.Text = Directory.GetCurrentDirectory();

		cmdBrowse.Location = new Point(320, 34);
		cmdBrowse.Text = "Browse";
		cmdBrowse.Click += new EventHandler(this.cmdBrowse_Clicked);

		localFileNameDesc.Text = "&Filename:";
		localFileNameDesc.AutoSize = true;
		localFileNameDesc.Location = new Point(8, 62);
		localFileNameDesc.TabIndex=3;

		localFileName.Location = new Point(96, 61);
		localFileName.Size = new Size(200, 20);
		localFileName.TabIndex=4;
		localFileName.KeyUp += new KeyEventHandler(this.localFileName_KeyUp);

		cmdCancel.Location = new Point(320, 90);
		cmdCancel.Text = "&Cancel";
		cmdCancel.Click += new EventHandler(this.cmdCancel_Clicked);

		cmdGo.Location = new Point(240, 90);
		cmdGo.Text = "&OK";
		cmdGo.Click += new EventHandler(this.cmdGo_Clicked);

		this.Controls.Add(urlBoxDesc);
		this.Controls.Add(urlBox);
		this.Controls.Add(localFilePathDesc);
		this.Controls.Add(localFilePath);
		this.Controls.Add(localFileNameDesc);
		this.Controls.Add(localFileName);
		this.Controls.Add(cmdGo);
		this.Controls.Add(cmdCancel);
		this.Controls.Add(cmdBrowse);
	}

	private void cmdGo_Clicked(Object Src, EventArgs e) {
		if(!Directory.Exists(localFilePath.Text))
		{
			if(MessageBox.Show("Directory " + localFilePath.Text + " does not exist!\nWould you like to create it?","Directory Does Not Exist!",MessageBoxButtons.YesNo, MessageBoxIcon.Question)==DialogResult.Yes)
				Directory.CreateDirectory(localFilePath.Text);
			else
				return;
		}

		if (urlBox.Text == "" || localFilePath.Text == "" || localFileName.Text == "") {
			MessageBox.Show("All fields must be completed.", "Fill it in dood", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}

		JobData jobData = new JobData();
		jobData.url=urlBox.Text;
		jobData.localPath=localFilePath.Text + (localFilePath.Text.Substring(localFilePath.Text.Length-1, 1)!="\\" ? "\\" : "");
		jobData.localName=localFileName.Text;
		bool notfound=false;

		AppSettings.SaveSetting("LastNewJobPath", localFilePath.Text);

		// create the job if it doesn't already exist. if it does, an exception will be thrown.
		try {
			JobManagement.GetJob(jobData.url);
		}
		catch {
			notfound=true;
		}

		if (!notfound) {
			MessageBox.Show("There is already an active job for this URL!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			this.Dispose();
			return;
		}
		else {
			JobManagement.CreateJob(jobData);
			
			MainForm.LoadJobs(false);

			// spin off a new thread creating the download status window and kicking off the download.
			DownloadForm dlf = new DownloadForm(jobData, true);
			Thread thread = new Thread(new ThreadStart(dlf.StartDownload));
			thread.Start();
		}

		// trash this window
		this.Dispose();
	}

	private void cmdBrowse_Clicked(Object Src, EventArgs e) {
		MessageBox.Show("TBI");
	}

	private void urlBox_OnChange(Object Src, EventArgs e) {
		if (!dontupdatefilename) localFileName.Text = GetFilename(urlBox.Text);
	}

	private void localFileName_KeyUp(Object Src, KeyEventArgs e) {
		dontupdatefilename=(!(localFileName.Text==""));
	}

	// retrieve the file name from a url
	private string GetFilename(string url) {
		int i=url.Length-1;
		int slashcount=0;

		while (true) {
			if (i<1) break;
			i=url.LastIndexOf('/', i-1);
			if (i!=-1)
				slashcount++;
		}

		i=url.LastIndexOf('/');

		if (i != url.Length-1 && slashcount > 2)
			return url.Substring(i+1, url.Length-i-1);
		else
			return "default.htm";
	}

	private void cmdCancel_Clicked(Object Src, EventArgs e) {
		this.Close();
		this.Dispose();
		return;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -