Пример #1
0
        private void tox_OnFriendConnectionStatusChanged(object sender, ToxEventArgs.FriendConnectionStatusEventArgs e)
        {
            var friend = Dispatcher.Invoke(() => ViewModel.GetFriendObjectByNumber(e.FriendNumber));
            if (friend == null)
                return;

            if (e.Status == ToxConnectionStatus.None)
            {
                Dispatcher.BeginInvoke(((Action)(() =>
                {
                    DateTime lastOnline = TimeZoneInfo.ConvertTime(tox.GetFriendLastOnline(e.FriendNumber), TimeZoneInfo.Utc, TimeZoneInfo.Local);

                    if (lastOnline.Year == 1970) //quick and dirty way to check if we're dealing with epoch 0
                        friend.StatusMessage = "Friend request sent";
                    else
                        friend.StatusMessage = string.Format("Last seen: {0} {1}", lastOnline.ToShortDateString(), lastOnline.ToLongTimeString());

                    friend.ToxStatus = ToxStatus.Invalid; //not the proper way to do it, I know...

                    if (friend.Selected)
                    {
                        CallButton.Visibility = Visibility.Collapsed;
                        FileButton.Visibility = Visibility.Collapsed;
                        TypingStatusLabel.Content = "";
                    }
                })));

                var receivers = transfers.Where(t => t.GetType() == typeof(FileReceiver) && t.FriendNumber == e.FriendNumber && !t.Finished);
                if (receivers.Count() > 0)
                {
                    foreach (var transfer in receivers)
                        transfer.Broken = true;
                }

                var senders = transfers.Where(t => t.GetType() == typeof(FileSender) && t.FriendNumber == e.FriendNumber && !t.Finished);
                if (senders.Count() > 0)
                {
                    foreach (var transfer in senders)
                        transfer.Broken = true;
                }
            }
            else if (e.Status != ToxConnectionStatus.None)
            {
                Dispatcher.BeginInvoke(((Action)(() =>
                {
                    friend.StatusMessage = getFriendStatusMessage(friend.ChatNumber);

                    if (friend.Selected)
                    {
                        CallButton.Visibility = Visibility.Visible;
                        FileButton.Visibility = Visibility.Visible;
                    }

                    if (ViewModel.MainToxyUser.AvatarBytes != null)
                    {
                        string avatarsDir = Path.Combine(toxDataDir, "avatars");
                        string selfAvatarFile = Path.Combine(avatarsDir, tox.Id.PublicKey.GetString() + ".png");

                        var fileInfo = tox.FileSend(e.FriendNumber, ToxFileKind.Avatar, ViewModel.MainToxyUser.AvatarBytes.Length, "avatar.png", ToxTools.Hash(ViewModel.MainToxyUser.AvatarBytes));
                        var transfer = new FileSender(tox, fileInfo.Number, friend.ChatNumber, ToxFileKind.Avatar, ViewModel.MainToxyUser.AvatarBytes.Length, "", selfAvatarFile);

                        transfers.Add(transfer);
                    }
                })));
            }

            Dispatcher.BeginInvoke(((Action)(() => RearrangeChatList())));
        }
Пример #2
0
        private void SendFile(int chatNumber, string filename)
        {
            FileInfo info = new FileInfo(filename);
            var fileInfo = tox.FileSend(chatNumber, ToxFileKind.Data, info.Length, filename.Split('\\').Last<string>());

            if (fileInfo.Number == -1)
                return;

            var transfer = new FileSender(tox, fileInfo.Number, chatNumber, ToxFileKind.Data, info.Length, filename.Split('\\').Last<string>(), filename);
            var control = convdic[chatNumber].AddNewFileTransfer(tox, transfer);
            transfer.Tag = control;

            control.SetStatus(string.Format("Waiting for {0} to accept...", getFriendName(chatNumber)));
            control.AcceptButton.Visibility = Visibility.Collapsed;
            control.DeclineButton.Visibility = Visibility.Visible;

            control.OnDecline += delegate(FileTransfer ft)
            {
                ft.Kill(false);

                if (transfers.Contains(ft))
                    transfers.Remove(ft);
            };

            control.OnPause += delegate(FileTransfer ft)
            {
                if (ft.Paused)
                    tox.FileControl(ft.FriendNumber, ft.FileNumber, ToxFileControl.Pause);
                else
                    tox.FileControl(ft.FriendNumber, ft.FileNumber, ToxFileControl.Resume);
            };

            transfers.Add(transfer);
            ScrollChatBox();
        }
Пример #3
0
        private void changeAvatar()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image files (*.png, *.gif, *.jpeg, *.jpg) | *.png;*.gif;*.jpeg;*.jpg";
            dialog.InitialDirectory = Environment.CurrentDirectory;
            dialog.Multiselect = false;

            if (dialog.ShowDialog() != true)
                return;

            string filename = dialog.FileName;
            FileInfo info = new FileInfo(filename);
          
            byte[] avatarBytes = File.ReadAllBytes(filename);
            MemoryStream stream = new MemoryStream(avatarBytes);
            Bitmap bmp = new Bitmap(stream);

            if(bmp.RawFormat != ImageFormat.Png)
            {
                var memStream = new MemoryStream();
                bmp.Save(memStream, ImageFormat.Png);
                bmp.Dispose();

                bmp = new Bitmap(memStream);
                avatarBytes = memStream.ToArray();
            }
            
            if (avatarBytes.Length > 0x4000)
            {
                double width = 64, height = 64;
                Bitmap newBmp = new Bitmap((int)width, (int)height);

                using (Graphics g = Graphics.FromImage(newBmp))
                {
                    double ratioX = width / (double)bmp.Width;
                    double ratioY = height / (double)bmp.Height;
                    double ratio = ratioX < ratioY ? ratioX : ratioY;

                    int newWidth = (int)(bmp.Width * ratio);
                    int newHeight = (int)(bmp.Height * ratio);

                    int posX = (int)((width - (bmp.Width * ratio)) / 2);
                    int posY = (int)((height - (bmp.Height * ratio)) / 2);
                    
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(bmp, posX, posY, newWidth, newHeight);
                }

                bmp.Dispose();

                bmp = newBmp;
                avatarBytes = avatarBitmapToBytes(bmp);

                if (avatarBytes.Length > 0x4000)
                {
                    this.ShowMessageAsync("Error", "This image is bigger than 16 KB and Toxy could not resize the image.");
                    return;
                }
            }

            ViewModel.MainToxyUser.AvatarBytes = avatarBytes;
            ViewModel.MainToxyUser.Avatar = bmp.ToBitmapImage(ImageFormat.Png);
            bmp.Dispose();

            string avatarsDir = Path.Combine(toxDataDir, "avatars");
            string selfAvatarFile = Path.Combine(avatarsDir, tox.Id.PublicKey.GetString() + ".png");

            if (!Directory.Exists(avatarsDir))
                Directory.CreateDirectory(avatarsDir);

            File.WriteAllBytes(selfAvatarFile, avatarBytes);

            //let's announce our new avatar
            byte[] hash = ToxTools.Hash(avatarBytes);
            foreach (int friend in tox.Friends)
            {
                if (!tox.IsFriendOnline(friend))
                    continue;

                var fileInfo = tox.FileSend(friend, ToxFileKind.Avatar, avatarBytes.Length, "avatar.png", hash);
                var transfer = new FileSender(tox, fileInfo.Number, friend, ToxFileKind.Avatar, avatarBytes.Length, "", selfAvatarFile);
                transfers.Add(transfer);
            }
        }
Пример #4
0
        private void TextToSend_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (ViewModel.IsGroupSelected)
                    return;

                if (Clipboard.ContainsImage())
                {
                    var bmp = (Bitmap)System.Windows.Forms.Clipboard.GetImage();
                    byte[] bytes = bmp.GetBytes();

                    if (!convdic.ContainsKey(ViewModel.SelectedChatNumber))
                        convdic.Add(ViewModel.SelectedChatNumber, FlowDocumentExtensions.CreateNewDocument());

                    var fileInfo = tox.FileSend(ViewModel.SelectedChatNumber, ToxFileKind.Data, bytes.Length, "image.bmp");

                    if (fileInfo.Number == -1)
                        return;

                    var transfer = new FileSender(tox, fileInfo.Number, ViewModel.SelectedChatNumber, ToxFileKind.Data, bytes.Length, "image.bmp", new MemoryStream(bytes));
                    var control = convdic[ViewModel.SelectedChatNumber].AddNewFileTransfer(tox, transfer);
                    transfer.Tag = control;

                    transfer.Tag.SetStatus(string.Format("Waiting for {0} to accept...", getFriendName(ViewModel.SelectedChatNumber)));
                    transfer.Tag.AcceptButton.Visibility = Visibility.Collapsed;
                    transfer.Tag.DeclineButton.Visibility = Visibility.Visible;

                    control.OnDecline += delegate(FileTransfer ft)
                    {
                        ft.Kill(false);

                        if (transfers.Contains(ft))
                            transfers.Remove(ft);
                    };

                    control.OnPause += delegate(FileTransfer ft)
                    {
                        if (ft.Paused)
                            tox.FileControl(ft.FriendNumber, ft.FileNumber, ToxFileControl.Pause);
                        else
                            tox.FileControl(ft.FriendNumber, ft.FileNumber, ToxFileControl.Resume);
                    };

                    transfers.Add(transfer);

                    ScrollChatBox();
                }
            }
        }