Esempio n. 1
0
        private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                if (!(e.UserState is TraceEvent userState))
                {
                    throw new ArgumentException("No TraceEvent received.");
                }

                // show all events
                if (CheckBoxShowTraceEvents.IsChecked == true)
                {
                    // filter a bit otherwise too much noise...
                    if (!string.Equals(userState.EventName, "FunctionEntry",
                                       StringComparison.CurrentCultureIgnoreCase) &&
                        !string.Equals(userState.EventName, "FunctionExit",
                                       StringComparison.CurrentCultureIgnoreCase) &&
                        !string.Equals(userState.EventName, "GenericLogEvent",
                                       StringComparison.CurrentCultureIgnoreCase))
                    {
                        TextEditorStream.AppendText(userState.EventName + " ");
                    }
                }

                // we are interested in just a few events with relevant data
                if (string.Equals(userState.EventName, "OmaDmClientExeStart",
                                  StringComparison.CurrentCultureIgnoreCase) ||
                    string.Equals(userState.EventName, "OmaDmSyncmlVerboseTrace",
                                  StringComparison.CurrentCultureIgnoreCase))
                {
                    SyncMlProgress.NotInProgress = false;
                    LabelStatus.Visibility       = Visibility.Visible;

                    string eventDataText = null;
                    try
                    {
                        eventDataText = Encoding.UTF8.GetString(userState.EventData());
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    if (eventDataText == null)
                    {
                        return;
                    }

                    var startIndex = eventDataText.IndexOf("<SyncML", StringComparison.CurrentCultureIgnoreCase);
                    if (startIndex == -1)
                    {
                        return;
                    }

                    var valueSyncMl =
                        TryFormatXml(eventDataText.Substring(startIndex, eventDataText.Length - startIndex - 1));

                    if (TextEditorStream.Text.Length == 0)
                    {
                        TextEditorStream.AppendText(valueSyncMl + Environment.NewLine);
                    }
                    else
                    {
                        TextEditorStream.AppendText(Environment.NewLine + valueSyncMl + Environment.NewLine);
                    }

                    TextEditorMessages.Text = valueSyncMl;
                    _foldingStrategy.UpdateFoldings(_foldingManager, TextEditorMessages.Document);

                    var valueSessionId = "0";
                    var matchSessionId = new Regex("<SessionID>([0-9a-zA-Z]+)</SessionID>").Match(valueSyncMl);
                    if (matchSessionId.Success)
                    {
                        valueSessionId = matchSessionId.Groups[1].Value;
                    }

                    if (!SyncMlSessions.Any(item => item.SessionId == valueSessionId))
                    {
                        var syncMlSession = new SyncMlSession(valueSessionId);
                        SyncMlSessions.Add(syncMlSession);
                        SyncMlMlMessages.Clear();
                    }

                    var valueMsgId = "0";
                    var matchMsgId = new Regex("<MsgID>([0-9]+)</MsgID>").Match(valueSyncMl);
                    if (matchMsgId.Success)
                    {
                        valueMsgId = matchMsgId.Groups[1].Value;
                    }

                    var syncMlMessage = new SyncMlMessage(valueSessionId, valueMsgId, valueSyncMl);
                    SyncMlSessions.FirstOrDefault(item => item.SessionId == valueSessionId)?.Messages
                    .Add(syncMlMessage);
                    SyncMlMlMessages.Add(syncMlMessage);
                }
                else if (string.Equals(userState.EventName, "OmaDmSessionStart",
                                       StringComparison.CurrentCultureIgnoreCase))
                {
                    TextEditorStream.AppendText("<!--- OmaDmSessionStart --->" + Environment.NewLine);
                }
                else if (string.Equals(userState.EventName, "OmaDmSessionComplete",
                                       StringComparison.CurrentCultureIgnoreCase))
                {
                    TextEditorStream.AppendText(Environment.NewLine + "<!--- OmaDmSessionComplete --->" +
                                                Environment.NewLine);
                    SyncMlProgress.NotInProgress = true;
                    LabelStatus.Visibility       = Visibility.Hidden;
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }
        private void MenuItemOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                Filter           = "Xml files|*.xml|All files|*.*",
                FilterIndex      = 0,
                RestoreDirectory = true,
                Title            = "Open SyncML stream",
            };

            if (openFileDialog.ShowDialog() == true)
            {
                TextEditorStream.Clear();
                TextEditorMessages.Clear();

                SyncMlSessions.Clear();

                var fileStream = openFileDialog.OpenFile();
                using (StreamReader reader = new StreamReader(fileStream))
                {
                    TextEditorStream.Text = reader.ReadToEnd();
                }

                if (TextEditorStream.Text.Length > 0)
                {
                    var syncMlMessages = Regex.Matches(TextEditorStream.Text, @"<SyncML[\s\S]*?</SyncML>", RegexOptions.IgnoreCase);
                    foreach (Match message in syncMlMessages)
                    {
                        var valueSyncMl = TryFormatXml(message.Value);

                        TextEditorMessages.Text = valueSyncMl;
                        _foldingStrategy.UpdateFoldings(_foldingManager, TextEditorMessages.Document);

                        var valueSessionId = "0";
                        var matchSessionId = new Regex("<SessionID>([0-9a-zA-Z]+)</SessionID>", RegexOptions.IgnoreCase).Match(valueSyncMl);
                        if (matchSessionId.Success)
                        {
                            valueSessionId = matchSessionId.Groups[1].Value;
                        }

                        if (!SyncMlSessions.Any(item => item.SessionId == valueSessionId))
                        {
                            var syncMlSession = new SyncMlSession(valueSessionId);
                            SyncMlSessions.Add(syncMlSession);
                        }

                        var valueMsgId = "0";
                        var matchMsgId = new Regex("<MsgID>([0-9]+)</MsgID>", RegexOptions.IgnoreCase).Match(valueSyncMl);
                        if (matchMsgId.Success)
                        {
                            valueMsgId = matchMsgId.Groups[1].Value;
                        }

                        var syncMlMessage = new SyncMlMessage(valueSessionId, valueMsgId, valueSyncMl);
                        SyncMlSessions.FirstOrDefault(item => item.SessionId == valueSessionId)?.Messages
                        .Add(syncMlMessage);
                    }
                }
            }
        }