PWAPP/PWAPPv2/MainWindow.xaml.cs

435 lines
15 KiB
C#

using PWAPPv2.Source;
using PWAPPv2.Source.Attachments;
using PWAPPv2.Source.Database;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
/**
* TODO:
* * ADD API SUPPORT
*/
namespace PWAPPv2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
static Source.APIConfig apiconfig;// = new Source.APIConfig();
static Source.Database.DatabaseConfig DataConfig;
static Source.API.APIConnection apiConnection;
Source.DataObjects.APICredentials apiCreds;
Source.API.PWApiConnection pwapiConnection;
Source.DataObjects.ComboBoxData TypeBox;
Source.DataObjects.ComboBoxData ToBox;
Source.DataObjects.ComboBoxData FromBox;
Source.Config.Configuration practiceConfig;
Source.Config.Configuration universalConfig;
string[] args;
Source.Patient patient;
List<Source.DataObjects.Attachment> attachments;
List<string> SupportedImageTypes = new List<string>();
//string ConfigPath = "C:\\PWAPP\\Config\\Config.xml";
string ConfigPath;
FileHandler fileHandler;
public MainWindow()
{
SupportedImageTypes.Clear();
SupportedImageTypes.Add(".jpeg");
SupportedImageTypes.Add(".jpg");
SupportedImageTypes.Add(".png");
try
{
args = App.Args;
}
catch (Exception)
{ }
ConfigPath = "C:\\PWAPP\\Config\\";
try
{
practiceConfig = new Source.Config.Configuration("C:\\PWAPP\\Config\\PracticeConfig.xml");
universalConfig = new Source.Config.Configuration("C:\\PWAPP\\App\\Config\\UniversalConfig.xml");
}
catch
(Exception ex)
{
System.Windows.MessageBox.Show("An error has occured in the configurations files.\n(" + ex.Message + ")");
}
pwapiConnection = new Source.API.PWApiConnection(practiceConfig, universalConfig);
try
{
/*if (pwapiConnection.CheckForUpdate() == true)
{
string message = "An update is available! Would you like to install it?";
string title = "Update available!";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = System.Windows.Forms.MessageBox.Show(message, title, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//System.Windows.MessageBox.Show("HAHA NO UPDATE FOR YOU!");
Process p = new Process();
p.StartInfo.FileName = "C:\\PWAPP\\Updater\\PWAppUpdaterForm.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
try
{
p.StartInfo.Arguments = args[0];
}
catch { }
if (System.Environment.OSVersion.Version.Major >= 6)
{
p.StartInfo.Verb = "runas";
}
p.Start();
Environment.Exit(0);
}
}*/
pwapiConnection.Update(args);
}
catch (Exception)
{
System.Windows.MessageBox.Show("An error has occured while checking for updates");
}
try
{
attachments = new List<Source.DataObjects.Attachment>();
Source.Database.DatabaseConnection dbcon = new Source.Database.DatabaseConnection(practiceConfig);
apiconfig = new Source.APIConfig(practiceConfig, universalConfig);
apiCreds = new Source.DataObjects.APICredentials(apiconfig);
apiConnection = new Source.API.APIConnection(universalConfig.Get("PWBaseURI"), apiCreds);
fileHandler = new FileHandler(apiCreds);
InitializeComponent();
PopulateComboBoxes();
//patient = new Source.Patient();
//patient.BuildFromDatabase(dbcon, args[0]);
patient = Patient.GetPatient(dbcon, args[0]);
this.DataContext = new Source.PatientGUIAdapter(patient);
}
catch (NullReferenceException)
{
}
catch (CouldNotOpenConnectionException)
{
System.Windows.MessageBox.Show("An problem occurred trying to connect to the OpenDental database.\nThe program will now exit.");
Environment.Exit(-1);
}
catch (Exception e)
{
System.Windows.MessageBox.Show("An unknow error has occurred.\n" + e.Message);
System.Environment.Exit(-1);
}
}
private void PopulateComboBoxes()
{
PopulateReferTypesBox();
PopulateReferToBox();
PopulateReferFromBox();
}
private void PopulateReferTypesBox()
{
//string ReferTypeString = apiConnection.SendPostRequestAsync("api/PWReferralTypes");
string ReferTypeString = pwapiConnection.GetReferalTypes();
TypeBox = new Source.DataObjects.ReferralTypeBox(boxReferType, ReferTypeString);
}
private void PopulateReferToBox()
{
string ReferToString = pwapiConnection.GetReferTo(); //apiConnection.SendPostRequestAsync("api/PWReferTo");
ToBox = new Source.DataObjects.ReferToBox(boxReferTo, ReferToString);
}
private void PopulateReferFromBox()
{
string ReferFromString = pwapiConnection.GetReferFrom(); //apiConnection.SendPostRequestAsync("api/PWReferFrom");
FromBox = new Source.DataObjects.ReferFromBox(boxReferFrom, ReferFromString);
}
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
}
private void boxReferType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
TypeBox.GetSelectedID();
}
catch (Source.DataObjects.ReferralTypeDefaultException)
{
}
catch (Source.DataObjects.InvalidReferralTypeException)
{
}
catch (NullReferenceException)
{
}
}
//Cancel button
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
//OK Button
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Source.DataObjects.Referral referral;
if (fileHandler.Attachments.Count == 0)
{
referral = new Source.DataObjects.Referral(apiCreds, patient,
(Source.DataObjects.ReferralTypeBox)TypeBox,
(Source.DataObjects.ReferToBox)ToBox,
(Source.DataObjects.ReferFromBox)FromBox,
fieldRemakrs, contact, false);
}
else
{
referral = new Source.DataObjects.Referral(apiCreds, patient,
(Source.DataObjects.ReferralTypeBox)TypeBox,
(Source.DataObjects.ReferToBox)ToBox,
(Source.DataObjects.ReferFromBox)FromBox,
fieldRemakrs, contact, true);
}
try
{
string referralString = referral.ToJsonString();
string result = pwapiConnection.SendReferral(referralString);
fileHandler.SendFilesAsAttachments(apiConnection, result);
System.Windows.MessageBox.Show("Referral added successfully!");
this.Close();
}
catch (Source.DataObjects.Referral.InvalidReferalDataException)
{
}
}
//AddImage Button
private void Button_Click_2(object sender, RoutedEventArgs e)
{
/*//System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
//openFileDialog.Multiselect = true;
//openFileDialog.Filter = "Attachment files (*.jpg,*.jpeg,*.pdf,*.png)|*.jpg;*.jpeg;*.pdf;*.png;";
//openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
//if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
//{
// foreach (string filename in openFileDialog.FileNames)
// {
// //if (Path.GetExtension(filename) == ".jpg" || Path.GetExtension(filename) == ".jpeg")
// if(SupportedImageTypes.Contains(Path.GetExtension(filename)))
// {
// try
// {
// attachments.Add(new Source.DataObjects.Attachment(apiCreds, new Source.DataObjects.PWImage(filename)));
// }
// catch (NullReferenceException)
// {
// attachments = new List<Source.DataObjects.Attachment>
// {
// new Source.DataObjects.Attachment(apiCreds, new Source.DataObjects.PWImage(filename))
// };
// }
// }
// if (Path.GetExtension(filename) == ".pdf")
// {
// try
// {
// attachments.Add(new Source.DataObjects.Attachment(apiCreds, new Source.DataObjects.PWPdf(filename)));
// }
// catch (NullReferenceException)
// {
// attachments = new List<Source.DataObjects.Attachment>()
// {
// new Source.DataObjects.Attachment(apiCreds, new Source.DataObjects.PWPdf(filename))
// };
// }
// }
// }
//}*/
fileHandler.AddFile();
try
{
ImageList.Items.Clear();
foreach (var attachment in fileHandler.Attachments)
{
if (attachment.pdf != null)
{
ImageList.Items.Add(CreateImageGridItem(new Source.DataObjects.PWImage("C:\\PWAPP\\App\\App\\pdf.jpg"), attachment.pdf.path));
}
else
{
ImageList.Items.Add(CreateImageGridItem(attachment.image));
}
}
}
catch (NullReferenceException)
{ }
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
int index = ImageList.SelectedIndex;
if (index == -1)
{
return;
}
attachments.RemoveAt(index);
try
{
ImageList.Items.Clear();
foreach (var attachment in fileHandler.Attachments)
{
if (attachment.pdf != null)
{
ImageList.Items.Add(CreateImageGridItem(new Source.DataObjects.PWImage("C:\\PWAPP\\App\\App\\pdf.jpg"), attachment.pdf.path));
}
else
{
ImageList.Items.Add(CreateImageGridItem(attachment.image));
}
}
}
catch (NullReferenceException)
{ }
}
public Grid CreateImageGridItem(Source.DataObjects.PWImage image)
{
Grid imageGrid = new Grid();
imageGrid.Width = 477;
imageGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
imageGrid.VerticalAlignment = VerticalAlignment.Top;
imageGrid.ShowGridLines = true;
ColumnDefinition imageColumn = new ColumnDefinition();
imageColumn.Width = new GridLength(50);
ColumnDefinition textColumn = new ColumnDefinition();
textColumn.Width = new GridLength(427);
imageGrid.ColumnDefinitions.Add(imageColumn);
imageGrid.ColumnDefinitions.Add(textColumn);
Image addedImage = new Image();
addedImage.Source = image.GetBitmapImage();
TextBlock text = new TextBlock();
text.Text = image.GetPath();
text.FontSize = 12;
Grid.SetColumn(addedImage, 0);
Grid.SetColumn(text, 1);
imageGrid.Children.Add(addedImage);
imageGrid.Children.Add(text);
return imageGrid;
}
public Grid CreateImageGridItem(Source.DataObjects.PWImage image, string path)
{
Grid imageGrid = new Grid();
imageGrid.Width = 477;
imageGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
imageGrid.VerticalAlignment = VerticalAlignment.Top;
imageGrid.ShowGridLines = true;
ColumnDefinition imageColumn = new ColumnDefinition();
imageColumn.Width = new GridLength(50);
ColumnDefinition textColumn = new ColumnDefinition();
textColumn.Width = new GridLength(427);
imageGrid.ColumnDefinitions.Add(imageColumn);
imageGrid.ColumnDefinitions.Add(textColumn);
Image addedImage = new Image();
addedImage.Source = image.GetBitmapImage();
TextBlock text = new TextBlock();
text.Text = path;
text.FontSize = 12;
Grid.SetColumn(addedImage, 0);
Grid.SetColumn(text, 1);
imageGrid.Children.Add(addedImage);
imageGrid.Children.Add(text);
return imageGrid;
}
}
}