public void ExecuteDeleteValue(object obj)
        {
            LogEntryViewModel itemToDelete = (LogEntryViewModel)obj;
            bool check = log.Logs.Remove(itemToDelete);

            Request.Log.Remove(itemToDelete);
            if (itemToDelete.IsFavorite)
            {
                Request.FavoriteLog.Remove(itemToDelete);
            }
            SaveHistory();
        }
        public async void ClearHistory(object obj)
        {
            var dialSettings = new MetroDialogSettings();

            dialSettings.NegativeButtonText       = "Delete everything but favorite entries";
            dialSettings.AffirmativeButtonText    = "Delete everything";
            dialSettings.FirstAuxiliaryButtonText = "Cancel";
            dialSettings.MaximumBodyHeight        = 50;
            dialSettings.DefaultButtonFocus       = MessageDialogResult.Affirmative;

            var res = await _dialogCordinator.ShowMessageAsync(this, "Delete History", "Choose what you want to delete:", style : MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, settings : dialSettings);

            if (res == MessageDialogResult.Affirmative)
            {
                log.Logs.Clear();
                Request.Log         = new ObservableCollection <LogEntryViewModel>(log.Logs);
                Request.FavoriteLog = new ObservableCollection <LogEntryViewModel>(log.Logs.Where(x => x.IsFavorite));
                if (File.Exists(Path.Combine(GetHistorySavePath() + historyFileName)))
                {
                    File.Delete(Path.Combine(GetHistorySavePath() + historyFileName));
                }
            }
            else
            {
                List <LogEntryViewModel> logsToRemove = new List <LogEntryViewModel>();
                foreach (var item in log.Logs)
                {
                    if (item.IsFavorite == false)
                    {
                        logsToRemove.Add(item);
                    }
                }
                foreach (var item in logsToRemove)
                {
                    log.Logs.Remove(item);
                }
                SelectedLogEntryViewModelItem = log.Logs.FirstOrDefault();
                Request.Log         = new ObservableCollection <LogEntryViewModel>(log.Logs);
                Request.FavoriteLog = new ObservableCollection <LogEntryViewModel>(log.Logs.Where(x => x.IsFavorite));
                SaveHistory();
            }
        }
 /// <summary>
 /// Loads the Values from the selected History Log Item.
 /// </summary>
 /// <param name="value"></param>
 public void LoadValues(object value)
 {
     if (value != null)
     {
         LogEntryViewModel oldRequest = (LogEntryViewModel)value;
         if (oldRequest.Action == "Server")
         {
             Request.SelectedIndex = 0;
             Request.Url           = oldRequest.RequestEntry.Uri;
             Request.XmlString     = oldRequest.RequestEntry.Content;
             Request.Username      = oldRequest.RequestEntry.Username;
             Request.Password      = oldRequest.RequestEntry.Password;
         }
         else if (oldRequest.Action == "Client")
         {
             Request.SelectedIndex = 1;
             Request.Directory     = oldRequest.RequestEntry.Uri;
             Request.XmlString     = oldRequest.RequestEntry.Content;
         }
         SelectedLogEntryViewModelItem = oldRequest;
     }
 }
        public void ExecuteChangeIsFavorite(object obj)
        {
            LogEntryViewModel item = (LogEntryViewModel)obj;

            if (item.IsFavorite)
            {
                viewmodel.Request.Log.Where(x => x.Equals(item)).FirstOrDefault().IsFavorite = false;
                viewmodel.Request.FavoriteLog.Remove(item);
                item.IsFavorite = false;
                RaisePropertyChanged(nameof(FavoriseIcon));
                RaisePropertyChanged(nameof(IsFavorite));
            }
            else
            {
                viewmodel.Request.Log.Where(x => x.Equals(item)).FirstOrDefault().IsFavorite = true;
                this.IsFavorite = true;
                viewmodel.Request.FavoriteLog.Add(item);
                viewmodel.Request.FavoriteLog = new System.Collections.ObjectModel.ObservableCollection <LogEntryViewModel>(viewmodel.Request.FavoriteLog.OrderByDescending(x => x.RequestEntry.Date));
                RaisePropertyChanged(nameof(FavoriseIcon));
                RaisePropertyChanged(nameof(IsFavorite));
            }
            viewmodel.SaveHistory();
        }
        /// <summary>
        /// Sends the Server Request.
        /// Posts given input fields to the OneOffixx Server.
        /// </summary>
        /// <param name="obj"></param>
        public async void SendRequest(object obj)
        {
            double length;

            isErrorAppeared    = false;
            Request.CanExecute = false;
            LogEntryViewModel values = new LogEntryViewModel(this);
            var date = System.DateTime.Now;

            values.Id           = Guid.NewGuid();
            values.Name         = date.ToString();
            values.Action       = "Server";
            values.RequestEntry = new Request()
            {
                Uri = Request.Url, Content = Request.XmlString, Username = Request.Username, Password = Request.Password, Date = date
            };
            values.ResponseEntry = new Response();
            values.IsFavorite    = false;
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    var inBytes = Encoding.ASCII.GetBytes($"{Request.Username}:{Request.Password}");
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(inBytes));
                    HttpContent content = new StringContent(Request.XmlString);
                    Stopwatch   sw      = Stopwatch.StartNew();
                    var         result  = await client.PostAsync(Request.Url, content);

                    sw.Stop();
                    values.ResponseEntry.TimeUsed = Math.Round((decimal)sw.ElapsedMilliseconds / 1000, 3).ToString();
                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        using (content = result.Content)
                        {
                            length     = (double)content.Headers.ContentLength;
                            filename   = content.Headers.ContentDisposition.FileName;
                            filename   = filename.Replace("\"", "");
                            byteResult = await content.ReadAsByteArrayAsync();
                        }
                        values.ResponseEntry.StatusCode = ((int)result.StatusCode).ToString();
                        SuccessView success = new SuccessView(this);
                        dial                  = new ResponseWindow();
                        length                = Math.Round((length / 1048576), 2);
                        success.Size.Text     = length.ToString() + " MB";
                        success.Filename.Text = filename;
                        success.Time.Text     = values.ResponseEntry.TimeUsed + " Seconds";
                        dial.Content          = success;
                        dial.Height           = 300;
                        dial.Width            = 300;
                        await((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
                    }
                    else
                    {
                        FailView fail = new FailView(this);
                        dial = new ResponseWindow();
                        values.ResponseEntry.StatusCode = ((int)result.StatusCode).ToString();
                        fail.ServerStatus.Text          = result.StatusCode.ToString() + " " + (int)result.StatusCode;
                        fail.Details.Text = result.ReasonPhrase;
                        dial.Content      = fail;
                        dial.Height       = 300;
                        dial.Width        = 300;
                        await((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
                    }
                }
                catch (Exception)
                {
                    FailView fail = new FailView(this);
                    dial = new ResponseWindow();
                    fail.ServerStatus.Text = "Uri not found";
                    values.ResponseEntry   = new Response()
                    {
                        StatusCode = "Uri not found"
                    };
                    fail.Details.Text = "Your Server input may be wrong as we could not find the Uri you requested.";
                    dial.Content      = fail;
                    dial.Height       = 300;
                    dial.Width        = 300;
                    await((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
                }
                finally
                {
                    values.ResponseEntry.Filename = filename;
                    Request.CanExecute            = true;
                    log.Logs.Add(values);
                    Request.Log         = new ObservableCollection <LogEntryViewModel>(log.Logs.OrderByDescending(x => x.RequestEntry.Date).ToList());
                    Request.FavoriteLog = new ObservableCollection <LogEntryViewModel>(log.Logs.Where(x => x.IsFavorite));
                    SaveHistory();
                }
            }
        }
        public void LoadHistoryFromFile()
        {
            string filepath = Path.Combine(GetHistorySavePath() + historyFileName);

            if (File.Exists(filepath))
            {
                try
                {
                    XDocument xdoc     = XDocument.Load(filepath);
                    string    xml      = xdoc.ToString();
                    var       elements = XmlSerializer.Deserialize <HistoryEntry>(xml);
                    foreach (var item in elements.Logs)
                    {
                        log.Logs.Add(new LogEntryViewModel(this)
                        {
                            Id           = item.Id,
                            Name         = item.Name,
                            Action       = item.Action,
                            RequestEntry = new Request()
                            {
                                Uri = item.RequestEntry.Uri, Username = item.RequestEntry.Username, Password = item.RequestEntry.Password, Content = item.RequestEntry.Content, Date = item.RequestEntry.Date
                            },
                            ResponseEntry = new Response()
                            {
                                StatusCode = item.ResponseEntry.StatusCode, Filename = item.ResponseEntry.Filename, TimeUsed = item.ResponseEntry.TimeUsed
                            },
                            IsFavorite = item.IsFavorite
                        });
                    }
                }
                catch (Exception ex)
                {
                    string newFilepath = GetHistorySavePath() + Guid.NewGuid().ToString() + "\\" + historyFileName;
                    System.IO.File.Move("filepath", newFilepath);
                    FailView fail = new FailView(this);
                    dial = new ResponseWindow();
                    fail.ServerStatus.Text = "History Error";
                    fail.Details.Text      = "An Error occured while trying to read your History. It seems that the xml structure was changed or is from an older version. Your History file was renamed to: " + newFilepath;
                    dial.Content           = fail;
                    dial.Height            = 300;
                    dial.Width             = 300;
                    ((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
                }

                RequestModel request = new RequestModel();
                if (log.Logs.FirstOrDefault()?.Action == "Server")
                {
                    request.WarningVisibility = Visibility.Hidden;
                    request.Url       = log.Logs[0].RequestEntry.Uri;
                    request.Username  = log.Logs[0].RequestEntry.Username;
                    request.Password  = log.Logs[0].RequestEntry.Password;
                    request.XmlString = log.Logs[0].RequestEntry.Content;
                    Request           = request;
                }
                else if (log.Logs.FirstOrDefault()?.Action == "Client")
                {
                    request.WarningVisibility = Visibility.Hidden;
                    request.Directory         = log.Logs[0].RequestEntry.Uri;
                    request.XmlString         = log.Logs[0].RequestEntry.Content;
                    Request = request;
                    Request.SelectedIndex = 1;
                }
                else
                {
                    Request = new RequestModel();
                }
                SelectedLogEntryViewModelItem = log.Logs.FirstOrDefault();
                Request.Log         = new ObservableCollection <LogEntryViewModel>(log.Logs);
                Request.FavoriteLog = new ObservableCollection <LogEntryViewModel>(log.Logs.Where(x => x.IsFavorite));
            }
            else
            {
                RequestModel request = new RequestModel()
                {
                    WarningVisibility = Visibility.Hidden
                };
                Request = request;
            }
        }
        /// <summary>
        /// Sends the Client Request.
        /// Uses the system Process which is used for OneConnect.
        /// </summary>
        /// <param name="obj"></param>
        public async void ClientConnect(object obj)
        {
            LogEntryViewModel values = new LogEntryViewModel(this);

            values.Action       = "Client";
            values.Name         = System.DateTime.Now.ToString();
            values.RequestEntry = new Request()
            {
                Date = System.DateTime.Now, Uri = Request.Directory, Content = Request.XmlString
            };
            values.ResponseEntry = new Response();
            SaveHistory();
            Request.CanExecuteClient = false;
            filename = Path.GetTempPath() + Guid.NewGuid().ToString() + ".oocx";
            try
            {
                XDocument xdoc = XDocument.Parse(Request.XmlString);
                xdoc.Save(filename);
                try
                {
                    if (File.Exists(Request.Directory))
                    {
                        var command = Process.Start($"{Request.Directory}", $"/connector \"{filename}\"");
                        values.ResponseEntry.StatusCode = "200";
                    }
                    else
                    {
                        FailView fail = new FailView(this);
                        dial = new ResponseWindow();
                        fail.ServerStatus.Text = "failed";
                        values.ResponseEntry   = new Response()
                        {
                            StatusCode = "File not found"
                        };
                        fail.Details.Text = "Since we could not find the file please check your input directory and confirm if there has crept in an error. ";
                        dial.Content      = fail;
                        dial.Height       = 300;
                        dial.Width        = 300;
                        await((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
                    }
                }
                catch (Exception ex)
                {
                    FailView fail = new FailView(this);
                    dial = new ResponseWindow();
                    fail.ServerStatus.Text = "Uri not found";
                    values.ResponseEntry   = new Response()
                    {
                        StatusCode = "Unexpected Error"
                    };
                    fail.Details.Text = "There has been an unexpected Error while trying to execute the Connector. Error: " + ex.Message;
                    dial.Content      = fail;
                    dial.Height       = 300;
                    dial.Width        = 300;
                    await((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
                }
            }
            catch (System.Xml.XmlException)
            {
                FailView fail = new FailView(this);
                dial = new ResponseWindow();
                fail.ServerStatus.Text = "Uri not found";
                values.ResponseEntry   = new Response()
                {
                    StatusCode = "Unexpected Error"
                };
                fail.Details.Text = "An unexpected error has occured while trying to create the temp .oocx file. Please check the XML syntax of your content input.";
                dial.Content      = fail;
                dial.Height       = 300;
                dial.Width        = 300;
                await((MetroWindow)Application.Current.MainWindow).ShowMetroDialogAsync(dial);
            }
            log.Logs.Add(values);
            Request.Log              = new ObservableCollection <LogEntryViewModel>(log.Logs.OrderByDescending(x => x.RequestEntry.Date));
            Request.FavoriteLog      = new ObservableCollection <LogEntryViewModel>(log.Logs.Where(x => x.IsFavorite).OrderByDescending(x => x.RequestEntry.Date));
            Request.CanExecuteClient = true;
        }
 public void ExecuteEditHistoryName(object obj)
 {
     editNameItem           = (LogEntryViewModel)obj;
     editNameItem.IsEditing = true;
 }