private void LeaveSessionToolStripMenuItemClick(object sender, EventArgs e) { if (_networkSession != null) { _networkSession.TimerStop(); _networkSession.Leave(); } if (_networkChat != null && !_networkChat.IsDisposed) { _networkChat.Close(); _networkChat = null; } _networkSession = null; EnableDisableControlsNotWorkingInNetworkMode(true); toolStripStatusNetworking.Visible = false; SubtitleListview1.HideExtraColumn(); _networkChat = null; var format = GetCurrentSubtitleFormat(); if (format.HasStyleSupport && _networkSession == null) { if (format.GetType() == typeof(Sami) || format.GetType() == typeof(SamiModern)) SubtitleListview1.ShowExtraColumn(_languageGeneral.Class); else SubtitleListview1.ShowExtraColumn(_languageGeneral.Style); SubtitleListview1.DisplayExtraFromExtra = true; } }
private void chatToolStripMenuItem_Click(object sender, EventArgs e) { if (_networkSession != null) { if (_networkChat == null || _networkChat.IsDisposed) { _networkChat = new NetworkChat(); _networkChat.Initialize(_networkSession); _networkChat.Show(this); } else { _networkChat.WindowState = FormWindowState.Normal; } } }
private void NetworkGetSendUpdates(List<int> deleteIndices, int insertIndex, Paragraph insertParagraph) { _networkSession.TimerStop(); bool doReFill = false; bool updateListViewStatus = false; SubtitleListview1.SelectedIndexChanged -= SubtitleListview1_SelectedIndexChanged; string message = string.Empty; int numberOfLines = 0; List<SeNetworkService.SeUpdate> updates = null; int numberOfRetries = 10; while (numberOfRetries > 0) { numberOfRetries--; try { updates = _networkSession.GetUpdates(out message, out numberOfLines); numberOfRetries = 0; } catch (Exception exception) { if (numberOfRetries <= 0) { if (exception.InnerException != null) MessageBox.Show(string.Format(_language.NetworkUnableToConnectToServer, exception.InnerException.Message + Environment.NewLine + exception.InnerException.StackTrace)); else MessageBox.Show(string.Format(_language.NetworkUnableToConnectToServer, exception.Message + Environment.NewLine + exception.StackTrace)); _networkSession.TimerStop(); if (_networkChat != null && !_networkChat.IsDisposed) { _networkChat.Close(); _networkChat = null; } _networkSession = null; EnableDisableControlsNotWorkingInNetworkMode(true); toolStripStatusNetworking.Visible = false; SubtitleListview1.HideExtraColumn(); _networkChat = null; return; } Application.DoEvents(); System.Threading.Thread.Sleep(250); } } int currentSelectedIndex = -1; if (SubtitleListview1.SelectedItems.Count > 0) currentSelectedIndex = SubtitleListview1.SelectedItems[0].Index; int oldCurrentSelectedIndex = currentSelectedIndex; if (message == "OK") { foreach (var update in updates) { if (!string.IsNullOrEmpty(update.Text)) { if (!update.Text.Contains(Environment.NewLine)) update.Text = update.Text.Replace("\n", Environment.NewLine); update.Text = WebUtility.HtmlDecode(update.Text).Replace("<br />", Environment.NewLine); } if (update.User.Ip != _networkSession.CurrentUser.Ip || update.User.UserName != _networkSession.CurrentUser.UserName) { if (update.Action == "USR") { _networkSession.Users.Add(update.User); if (_networkChat != null && !_networkChat.IsDisposed) { _networkChat.AddUser(update.User); } _networkSession.AppendToLog(string.Format(_language.NetworkNewUser, update.User.UserName, update.User.Ip)); } else if (update.Action == "MSG") { _networkSession.ChatLog.Add(new NikseWebServiceSession.ChatEntry { User = update.User, Message = update.Text }); if (_networkChat == null || _networkChat.IsDisposed) { _networkChat = new NetworkChat(); _networkChat.Initialize(_networkSession); _networkChat.Show(this); } else { _networkChat.AddChatMessage(update.User, update.Text); } if (!string.IsNullOrEmpty(Configuration.Settings.NetworkSettings.NewMessageSound) && File.Exists(Configuration.Settings.NetworkSettings.NewMessageSound)) { try { using (var soundPlayer = new System.Media.SoundPlayer(Configuration.Settings.NetworkSettings.NewMessageSound)) { soundPlayer.Play(); } } catch { } } _networkSession.AppendToLog(string.Format(_language.NetworkMessage, update.User.UserName, update.User.Ip, update.Text)); } else if (update.Action == "DEL") { doReFill = true; _subtitle.Paragraphs.RemoveAt(update.Index); if (_networkSession.LastSubtitle != null) _networkSession.LastSubtitle.Paragraphs.RemoveAt(update.Index); _networkSession.AppendToLog(string.Format(_language.NetworkDelete, update.User.UserName, update.User.Ip, update.Index)); _networkSession.AdjustUpdateLogToDelete(update.Index); if (deleteIndices.Count > 0) { for (int i = deleteIndices.Count - 1; i >= 0; i--) { int index = deleteIndices[i]; if (index == update.Index) deleteIndices.RemoveAt(i); else if (index > update.Index) deleteIndices[i] = index - 1; } } if (insertIndex > update.Index) insertIndex--; if (currentSelectedIndex >= 0 && currentSelectedIndex > update.Index) currentSelectedIndex--; } else if (update.Action == "INS") { doReFill = true; var p = new Paragraph(update.Text, update.StartMilliseconds, update.EndMilliseconds); _subtitle.Paragraphs.Insert(update.Index, p); if (_networkSession.LastSubtitle != null) _networkSession.LastSubtitle.Paragraphs.Insert(update.Index, new Paragraph(p)); _networkSession.AppendToLog(string.Format(_language.NetworkInsert, update.User.UserName, update.User.Ip, update.Index, update.Text.Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString))); _networkSession.AddToWsUserLog(update.User, update.Index, update.Action, false); updateListViewStatus = true; _networkSession.AdjustUpdateLogToInsert(update.Index); if (deleteIndices.Count > 0) { for (int i = deleteIndices.Count - 1; i >= 0; i--) { int index = deleteIndices[i]; if (index > update.Index) deleteIndices[i] = index + 1; } } if (insertIndex > update.Index) insertIndex++; if (currentSelectedIndex >= 0 && currentSelectedIndex > update.Index) currentSelectedIndex++; } else if (update.Action == "UPD") { updateListViewStatus = true; var p = _subtitle.GetParagraphOrDefault(update.Index); if (p != null) { p.StartTime.TotalMilliseconds = update.StartMilliseconds; p.EndTime.TotalMilliseconds = update.EndMilliseconds; p.Text = update.Text; SubtitleListview1.SetTimeAndText(update.Index, p); _networkSession.AppendToLog(string.Format(_language.NetworkUpdate, update.User.UserName, update.User.Ip, update.Index, update.Text.Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString))); _networkSession.AddToWsUserLog(update.User, update.Index, update.Action, true); updateListViewStatus = true; } if (_networkSession.LastSubtitle != null) { p = _networkSession.LastSubtitle.GetParagraphOrDefault(update.Index); if (p != null) { p.StartTime.TotalMilliseconds = update.StartMilliseconds; p.EndTime.TotalMilliseconds = update.EndMilliseconds; p.Text = update.Text; } } } else if (update.Action == "BYE") { if (_networkChat != null && !_networkChat.IsDisposed) _networkChat.RemoveUser(update.User); SeNetworkService.SeUser removeUser = null; foreach (var user in _networkSession.Users) { if (user.UserName == update.User.UserName) { removeUser = user; break; } } if (removeUser != null) _networkSession.Users.Remove(removeUser); _networkSession.AppendToLog(string.Format(_language.NetworkByeUser, update.User.UserName, update.User.Ip)); } else { _networkSession.AppendToLog("UNKNOWN ACTION: " + update.Action + " by " + update.User.UserName + " (" + update.User.Ip + ")"); } } } if (numberOfLines != _subtitle.Paragraphs.Count) { _subtitle = _networkSession.ReloadSubtitle(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); UpdateListviewWithUserLogEntries(); _networkSession.LastSubtitle = new Subtitle(_subtitle); _oldSelectedParagraph = null; SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; _networkSession.TimerStart(); RefreshSelectedParagraph(); return; } if (deleteIndices.Count > 0) { deleteIndices.Sort(); deleteIndices.Reverse(); foreach (int i in deleteIndices) { _subtitle.Paragraphs.RemoveAt(i); if (_networkSession.LastSubtitle != null && i < _networkSession.LastSubtitle.Paragraphs.Count) _networkSession.LastSubtitle.Paragraphs.RemoveAt(i); } _networkSession.DeleteLines(deleteIndices); doReFill = true; } if (insertIndex >= 0 && insertParagraph != null) { _subtitle.Paragraphs.Insert(insertIndex, insertParagraph); if (_networkSession.LastSubtitle != null && insertIndex < _networkSession.LastSubtitle.Paragraphs.Count) _networkSession.LastSubtitle.Paragraphs.Insert(insertIndex, insertParagraph); _networkSession.InsertLine(insertIndex, insertParagraph); doReFill = true; } _networkSession.CheckForAndSubmitUpdates(); // updates only (no inserts/deletes) } else { if (message == "Session not found!") { message = _networkSession.Restart(); if (message == "Reload") { _subtitle = _networkSession.ReloadSubtitle(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); UpdateListviewWithUserLogEntries(); _networkSession.LastSubtitle = new Subtitle(_subtitle); _oldSelectedParagraph = null; SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; _networkSession.TimerStart(); RefreshSelectedParagraph(); return; } if (message == "OK") { _networkSession.TimerStart(); RefreshSelectedParagraph(); return; } } else if (message == "User not found!") { message = _networkSession.ReJoin(); if (message == "Reload") { _subtitle = _networkSession.ReloadSubtitle(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); UpdateListviewWithUserLogEntries(); _networkSession.LastSubtitle = new Subtitle(_subtitle); _oldSelectedParagraph = null; SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; _networkSession.TimerStart(); RefreshSelectedParagraph(); return; } } MessageBox.Show(message); LeaveSessionToolStripMenuItemClick(null, null); SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; return; } if (doReFill) { _subtitle.Renumber(); SubtitleListview1.Fill(_subtitle, _subtitleAlternate); UpdateListviewWithUserLogEntries(); if (oldCurrentSelectedIndex != currentSelectedIndex) { _oldSelectedParagraph = null; _subtitleListViewIndex = currentSelectedIndex; SubtitleListview1.SelectIndexAndEnsureVisible(_subtitleListViewIndex); } else if (_oldSelectedParagraph != null) { var p = _subtitle.GetFirstAlike(_oldSelectedParagraph); if (p == null) { var tmp = new Paragraph(_oldSelectedParagraph); tmp.Text = textBoxListViewText.Text; p = _subtitle.GetFirstAlike(tmp); } if (p == null) { int idx = oldCurrentSelectedIndex; if (idx >= _subtitle.Paragraphs.Count) idx = _subtitle.Paragraphs.Count - 1; if (idx >= 0 && idx < _subtitle.Paragraphs.Count) { SubtitleListview1.SelectIndexAndEnsureVisible(idx); _listViewTextUndoIndex = -1; SubtitleListView1SelectedIndexChange(); textBoxListViewText.Text = _subtitle.Paragraphs[idx].Text; } } else { _subtitleListViewIndex = _subtitle.GetIndex(p); SubtitleListview1.SelectIndexAndEnsureVisible(_subtitleListViewIndex); _listViewTextUndoIndex = -1; SubtitleListView1SelectedIndexChange(); } } } else if (updateListViewStatus) { UpdateListviewWithUserLogEntries(); } _networkSession.LastSubtitle = new Subtitle(_subtitle); SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; _networkSession.TimerStart(); }