/// <summary> /// New connection found /// </summary> /// <param name="client">The client</param> private void NewConnection(TcpClient client) { // If I am private, I am going to reject automatically, // acting like a don't even exist if (((Settings)Application.Current.Properties["Settings"]).Ghost) { client.Close(); return; } // If I have to automatically accept all transfers, I automatically start the download. // So I won't even ask the user to accept it. if (((Settings)Application.Current.Properties["Settings"]).AutoAccept) { List <Task> Transfers = (List <Task>)Application.Current.Properties["Transfers"]; Transfers.Add(Task.Run(() => { Receive Receiver = new Receive(client); if (!Receiver.ParseHello()) { return; } Receiver.Start(); Receiver.Wait(); })); return; } //------------------------------------ // Show the request //------------------------------------ MainWindowDispatcher.InvokeAsync(() => { ConnectionWindow w = new ConnectionWindow(client); // UPDATE: show the window later, so we can first see if request is valid. /*w.Show(); * w.Activate();*/ }); }
/// <summary> /// Constructor /// </summary> /// <param name="w">The window</param> /// <param name="Client">The client</param> public IncomingTransferViewModel(ConnectionWindow w, TcpClient Client) { //------------------------------------ // Init //------------------------------------ PrivateSender = false; TheWindow = w; SelectFolder = new ChooseFolder(this); //------------------------------------ // Add the transfer but don't start it yet //------------------------------------ List <Task> Transfers = (List <Task>)Application.Current.Properties["Transfers"]; Transfers.Add(Task.Run(() => { Receive Receiver = new Receive(Client); if (!Receiver.ParseHello()) { // Not a valid request? then just close the window (NOTE: it is still hidden at this point) TheWindow.Dispatcher.InvokeAsync(() => { TheWindow.Close(); }); return; } else { // Open the window to ask user to accept or reject TheWindow.Dispatcher.InvokeAsync(() => { TheWindow.InitializeComponent(); TheWindow.DataContext = this; TheWindow.Topmost = true; TheWindow.Show(); TheWindow.Activate(); TheWindow.Topmost = false; }); } //------------------------------------ // Render data //------------------------------------ if (Receiver.SenderUser != null) { UserRequestText = Receiver.SenderUser.Name + " wants to send you"; UserProfilePicture = Receiver.SenderUser.PicBytes; } else { UserRequestText = "A private user wants to send you"; PrivateSender = true; } if (!Receiver.IsFolder) { FileName = Receiver.FileName + "." + Receiver.FileExtension; UserRequestText += " a file:"; _Size = Receiver.FileSize; Extension = Receiver.FileExtension; } else { string[] _name = Receiver.FileName.Split('/'); FileName = _name[0]; UserRequestText += " a folder:"; } //------------------------------------ // Wait for user's confirmation //------------------------------------ lock (this) { if (Status == Reply.UNANSWERED) { Monitor.Wait(this); } } //------------------------------------ // Deal with user's decision //------------------------------------ if (!SelectedPath.Equals(String.Empty)) { Receiver.FolderLocation = SelectedPath; } if (Status == Reply.ACCEPTED) { Receiver.Start(); } else { Receiver.Reject(true); } })); Accept = new AcceptTransfer(this); Reject = new RejectTransfer(this); }