private async Task ExecuteSafe(Func <CancellationToken, Task> stuff, string statusBarOkText, string statusBarFailText)
        {
            IsLoadingStarted = true;
            try
            {
                _cancellationTokenSource = new CancellationTokenSource();
                await stuff(_cancellationTokenSource.Token).ConfigureAwait(false);

                SetStatusBarText(statusBarOkText);
            }
            catch (Exception ex)
            {
                SetStatusBarText(statusBarFailText);
                _dispatcher.Invoke(() =>
                {
                    var msg = $"Error while executing operation on server '{_selectedConnection.Name}': {ex.Message}";
                    _statusReports.Clear();
                    _statusReports.Add(StatusReport.CreateOperation(msg, MessageLevel.Error, OperationType.None));
                    StatusReports.Refresh();
                });
            }
            finally
            {
                IsLoadingStarted = false;
            }
        }
        public static StatusReport ToReport(this string data)
        {
            var parts = data.Split('|');

            if (parts.Count() != 3)
            {
                throw new MalformedPackageReceivedException($"There should be 3 parts of a packet but found {parts.Count()}.");
            }

            var reportType     = (ReportType)Enum.Parse(typeof(ReportType), parts[0]);
            var messageLevel   = (MessageLevel)Enum.Parse(typeof(MessageLevel), parts[1]);
            var encodedMessage = Convert.FromBase64String(parts[2]);
            var message        = Encoding.UTF8.GetString(encodedMessage);

            if (reportType == ReportType.Progress)
            {
                return(StatusReport.CreateProgress(message));
            }

            var operationType = OperationType.None;

            message = message.Trim();
            if (message.Contains(addedTag))
            {
                message       = message.Replace(addedTag, "").Trim();
                operationType = OperationType.Added;
            }
            else if (message.Contains(updatedTag))
            {
                message       = message.Replace(updatedTag, "").Trim();
                operationType = OperationType.Updated;
            }
            else if (message.Contains(deletedTag))
            {
                message       = message.Replace(deletedTag, "").Trim();
                operationType = OperationType.Deleted;
            }
            else if (message.Contains(templateChangedTag))
            {
                message       = message.Replace(templateChangedTag, "").Trim();
                operationType = OperationType.TemplateChanged;
            }
            else if (message.Contains(renamedTag))
            {
                message       = message.Replace(renamedTag, "").Trim();
                operationType = OperationType.Renamed;
            }
            else if (message.Contains(movedTag))
            {
                message       = message.Replace(movedTag, "").Trim();
                operationType = OperationType.Moved;
            }
            else if (message.Contains(serializedTag))
            {
                message       = message.Replace(serializedTag, "").Trim();
                operationType = OperationType.Serialized;
            }

            return(StatusReport.CreateOperation(message, messageLevel, operationType));
        }
 private void RefreshStatus(StatusReport report, ProgressContext progressContext)
 {
     try
     {
         if (report.IsProgressReport())
         {
             Progress = int.Parse(report.Message);
             progressContext.SetProgress((uint)Progress);
         }
         else
         {
             _statusReports.Add(report);
         }
     }
     catch (Exception ex)
     {
         _statusReports.Add(StatusReport.CreateOperation("Malformated package. " + ex.Message,
                                                         MessageLevel.Error, OperationType.None));
     }
 }