Beispiel #1
0
        private void SendButton_Click(object sender, EventArgs e)
        {
            if (MessageText.Text.Length == 0) return;

            ShowProgressBar(AppResources.Chat_ProgressSendingMessage);

            sendButton.IsEnabled = false;

            try {
                gtalk.SendMessage(to, MessageText.Text, data => Dispatcher.BeginInvoke(() => {
                    HideProgressBar();

                    var bubble = new SentChatBubble();
                    bubble.Text = MessageText.Text;
                    bubble.TimeStamp = DateTime.Now;

                    App.Current.GtalkHelper.AddRecentContact(currentContact);

                    MessageList.Children.Add(bubble);

                    sendButton.IsEnabled = true;

                    MessageList.UpdateLayout();
                    Scroller.UpdateLayout();
                    Scroller.ScrollToVerticalOffset(Scroller.ExtentHeight);

                    lock (chatLog) {
                        while (chatLog.Count >= GoogleTalkHelper.MaximumChatLogSize) {
                            chatLog.RemoveAt(0);
                        }
                        chatLog.Add(new Message {
                            Body = MessageText.Text,
                            Outbound = true,
                            Time = DateTime.Now,
                            OTR = otr
                        });
                    }

                    MessageText.Text = "";
                }), error => {
                    HideProgressBar();
                    if (error.StartsWith("403")) {
                        settings.Remove("token");
                        settings.Remove("rootUrl");
                        gtalkHelper.LoginIfNeeded();
                    } else {
                        Dispatcher.BeginInvoke(
                            () => {
                                sendButton.IsEnabled = true;
                                gtalkHelper.ShowToast(AppResources.Chat_ErrorMessageNotSent);
                            }
                        );
                    }
                });
            } catch (InvalidOperationException) {
                Dispatcher.BeginInvoke(
                    () => {
                        HideProgressBar();
                        MessageBox.Show(
                            AppResources.Chat_ErrorAuthExpiredBody,
                            AppResources.Chat_ErrorAuthExpiredTitle,
                            MessageBoxButton.OK
                        );
                        App.Current.RootFrame.Navigate(new Uri("/Pages/Login.xaml", UriKind.Relative));
                    }
                );
            }
        }
Beispiel #2
0
        private void Initialize()
        {
            gtalkHelper.RosterUpdated -= Initialize;

            Dispatcher.BeginInvoke(() => {
                string displayName = email;
                string status = string.Empty;
                if (App.Current.Roster.Contains(to)) {
                    Contact t = App.Current.Roster[to];
                    displayName = t.NameOrEmail;
                    status = t.Status;
                }

                PageTitle.Text = displayName.ToUpper();
                if (status != string.Empty) {
                    PageTitle.Text += ", " + char.ToUpper(status[0]) + status.Substring(1);
                }

                TypingStatus.Text = String.Format(AppResources.Chat_NoticeTyping, displayName);

                if (gtalkHelper.IsContactPinned(email)) {
                    pinButton.IsEnabled = false;
                }

                chatLog = gtalkHelper.ChatLog(to);

                MessageList.Visibility = System.Windows.Visibility.Collapsed;

                MessageList.Children.Clear();

                lock (chatLog) {
                    var otr = false;

                    foreach (var message in chatLog) {
                        UserControl bubble;

                        if (message.OTR != otr) {
                            if (message.OTR) {
                                ShowStartOtr();
                            } else {
                                ShowEndOtr();
                            }

                            otr = message.OTR;
                        }

                        if (message.Body == null) continue;

                        if (message.Outbound) {
                            bubble = new SentChatBubble();

                            (bubble as SentChatBubble).Text = message.Body;
                            (bubble as SentChatBubble).TimeStamp = message.Time;
                        } else {
                            bubble = new ReceivedChatBubble();

                            (bubble as ReceivedChatBubble).Text = message.Body;
                            (bubble as ReceivedChatBubble).TimeStamp = message.Time;
                        }

                        MessageList.Children.Add(bubble);
                    }
                }

                MessageList.Visibility = System.Windows.Visibility.Visible;
                MessageList.UpdateLayout();
                Scroller.UpdateLayout();
                Scroller.ScrollToVerticalOffset(Scroller.ExtentHeight);

                var unread = settings["unread"] as Dictionary<string, int>;
                lock (unread) {
                    unread[email] = 0;
                }
                if (App.Current.Roster.Contains(email)) {
                    App.Current.Roster[email].UnreadCount = 0;
                }

                Uri url = gtalkHelper.GetPinUri(email);
                ShellTile existing = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri == url);

                if (existing != null) {
                    existing.Update(
                        new StandardTileData {
                            Count = 0
                        }
                    );
                }

                var contact = App.Current.Roster[email];

                if (contact != null) {
                    contact.UnreadCount = 0;
                }

                // Sets to broadcast the first message in a conversation
                to = email;
            });
        }