Exemple #1
0
        private void Tox_OnFileSendRequestReceived(object sender, ToxEventArgs.FileSendRequestEventArgs e)
        {
            var transfer = new FileTransfer(e.FileNumber, e.FriendNumber, e.FileKind, FileTransferDirection.Incoming);

            transfer.Name = e.FileName;
            transfer.Size = e.FileSize;

            switch (e.FileKind)
            {
            case ToxFileKind.Avatar:
            {
                if (e.FileSize == 0)
                {
                    //friend removed avatar, remove it from our store too
                    ProfileManager.Instance.AvatarManager.RemoveAvatar(e.FriendNumber);
                    _tox.FileControl(e.FriendNumber, e.FileNumber, ToxFileControl.Cancel);

                    Debugging.Write("Friend removed avatar");
                    break;
                }

                if (e.FileSize > 1 << 16)
                {
                    //we don't like this avatar, it's too big
                    _tox.FileControl(e.FriendNumber, e.FileNumber, ToxFileControl.Cancel);

                    Debugging.Write("Avatar too big, ignoring");
                    break;
                }

                if (ProfileManager.Instance.AvatarManager.Contains(e.FriendNumber))
                {
                    //compare hashes to see if we already have this avatar
                    byte[] hash = _tox.FileGetId(e.FriendNumber, e.FileNumber);
                    if (hash != null && ProfileManager.Instance.AvatarManager.HashMatches(e.FriendNumber, hash))
                    {
                        //we already have this avatar, cancel the transfer
                        _tox.FileControl(e.FriendNumber, e.FileNumber, ToxFileControl.Cancel);
                        break;
                    }
                }

                var error = ToxErrorFileControl.Ok;
                if (!_tox.FileControl(e.FriendNumber, e.FileNumber, ToxFileControl.Resume, out error))
                {
                    Debugging.Write("Failed to accept avatar transfer request: " + error);
                    break;
                }

                var avatarPath = ProfileManager.Instance.AvatarManager.GetAvatarFilename(e.FriendNumber);
                if (avatarPath == null)
                {
                    Debugging.Write("Could not find public key for friend");
                    break;
                }

                if (!_transfers.ContainsKey(transfer))
                {
                    //catch any scary exceptions (no access, file already in use and whatnot)
                    try { _transfers.Add(transfer, new FileStream(avatarPath, FileMode.Create)); }
                    catch (Exception ex)
                    {
                        Debugging.Write("Can't open filestream: " + ex.ToString());
                    }
                }
                else
                {
                    Debugging.Write("We already have a transfer with that number!");
                }

                break;
            }

            case ToxFileKind.Data:
            {
                if (!_transfers.ContainsKey(transfer))
                {
                    _transfers.Add(transfer, null);
                }
                else
                {
                    Debugging.Write("We already have a transfer with that number!");
                    return;
                }

                MainWindow.Instance.UInvoke(() =>
                    {
                        var friend = MainWindow.Instance.ViewModel.CurrentFriendListView.FindFriend(e.FriendNumber);
                        if (friend == null)
                        {
                            return;
                        }

                        (friend.ConversationView as ConversationViewModel).AddTransfer(transfer);
                    });

                break;
            }
            }

            Debugging.Write("New file send request: " + e.FileKind.ToString());
        }