Ran code maid against this code.
This commit is contained in:
parent
1445945d9c
commit
591528ee91
309 changed files with 139399 additions and 140129 deletions
|
|
@ -3,13 +3,13 @@ Copyright (c) 2014, Lars Brubaker
|
|||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
|
|
@ -23,133 +23,134 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using MatterHackers.Agg.UI;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MatterHackers.MatterControl.FieldValidation
|
||||
{
|
||||
public class ValidationStatus
|
||||
{
|
||||
public bool IsValid { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
public class ValidationStatus
|
||||
{
|
||||
public bool IsValid { get; set; }
|
||||
|
||||
public ValidationStatus()
|
||||
{
|
||||
this.IsValid = true;
|
||||
this.ErrorMessage = "";
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidationMethods
|
||||
{
|
||||
public ValidationMethods() { }
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
public ValidationStatus StringIsNotEmpty(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
if (value.Trim() == "")
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Oops! Field cannot be left blank";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
public ValidationStatus()
|
||||
{
|
||||
this.IsValid = true;
|
||||
this.ErrorMessage = "";
|
||||
}
|
||||
}
|
||||
|
||||
public ValidationStatus StringHasNoSpecialChars(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
|
||||
if(!regexItem.IsMatch(value))
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Oops! Field cannot have special characters";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
public class ValidationMethods
|
||||
{
|
||||
public ValidationMethods()
|
||||
{
|
||||
}
|
||||
|
||||
private static Regex digitsOnly = new Regex(@"[^\d]");
|
||||
public ValidationStatus StringLooksLikePhoneNumber(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
public ValidationStatus StringIsNotEmpty(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
if (value.Trim() == "")
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Oops! Field cannot be left blank";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
value = digitsOnly.Replace(value, "");
|
||||
if (value.Length == 10)
|
||||
{
|
||||
public ValidationStatus StringHasNoSpecialChars(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
|
||||
if (!regexItem.IsMatch(value))
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Oops! Field cannot have special characters";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
status.IsValid = true;
|
||||
}
|
||||
else if (value.Length == 11 && value[0] == '1')
|
||||
{
|
||||
status.IsValid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Sorry! Must be a valid U.S. or Canadian phone number.";
|
||||
}
|
||||
private static Regex digitsOnly = new Regex(@"[^\d]");
|
||||
|
||||
return status;
|
||||
}
|
||||
public ValidationStatus StringLooksLikePhoneNumber(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
|
||||
public ValidationStatus StringLooksLikeEmail(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
int lastAtPos = value.IndexOf("@");
|
||||
int lastDotPos = value.LastIndexOf(".");
|
||||
value = digitsOnly.Replace(value, "");
|
||||
if (value.Length == 10)
|
||||
{
|
||||
status.IsValid = true;
|
||||
}
|
||||
else if (value.Length == 11 && value[0] == '1')
|
||||
{
|
||||
status.IsValid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Sorry! Must be a valid U.S. or Canadian phone number.";
|
||||
}
|
||||
|
||||
if (lastAtPos < lastDotPos && lastAtPos > 0 && value.IndexOf("@@") == -1 && lastDotPos > 2 && (value.Length - lastDotPos) > 2)
|
||||
{
|
||||
status.IsValid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Sorry! Must be a valid email address.";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public class FormField
|
||||
{
|
||||
public delegate ValidationStatus ValidationHandler(string valueToValidate);
|
||||
MHTextEditWidget FieldEditWidget { get; set; }
|
||||
public TextWidget FieldErrorMessageWidget { get; set; }
|
||||
ValidationHandler[] FieldValidationHandlers { get; set; }
|
||||
public ValidationStatus StringLooksLikeEmail(string value)
|
||||
{
|
||||
ValidationStatus status = new ValidationStatus();
|
||||
int lastAtPos = value.IndexOf("@");
|
||||
int lastDotPos = value.LastIndexOf(".");
|
||||
|
||||
public FormField(MHTextEditWidget textEditWidget, TextWidget errorMessageWidget, ValidationHandler[] validationHandlers)
|
||||
{
|
||||
this.FieldEditWidget = textEditWidget;
|
||||
this.FieldErrorMessageWidget = errorMessageWidget;
|
||||
this.FieldValidationHandlers = validationHandlers;
|
||||
}
|
||||
if (lastAtPos < lastDotPos && lastAtPos > 0 && value.IndexOf("@@") == -1 && lastDotPos > 2 && (value.Length - lastDotPos) > 2)
|
||||
{
|
||||
status.IsValid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
status.IsValid = false;
|
||||
status.ErrorMessage = "Sorry! Must be a valid email address.";
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
bool fieldIsValid = true;
|
||||
foreach (ValidationHandler validationHandler in FieldValidationHandlers)
|
||||
{
|
||||
if (fieldIsValid)
|
||||
{
|
||||
ValidationStatus validationStatus = validationHandler(this.FieldEditWidget.Text);
|
||||
if (!validationStatus.IsValid)
|
||||
{
|
||||
fieldIsValid = false;
|
||||
FieldErrorMessageWidget.Text = validationStatus.ErrorMessage;
|
||||
FieldErrorMessageWidget.Visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return fieldIsValid;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class FormField
|
||||
{
|
||||
public delegate ValidationStatus ValidationHandler(string valueToValidate);
|
||||
|
||||
private MHTextEditWidget FieldEditWidget { get; set; }
|
||||
|
||||
public TextWidget FieldErrorMessageWidget { get; set; }
|
||||
|
||||
private ValidationHandler[] FieldValidationHandlers { get; set; }
|
||||
|
||||
public FormField(MHTextEditWidget textEditWidget, TextWidget errorMessageWidget, ValidationHandler[] validationHandlers)
|
||||
{
|
||||
this.FieldEditWidget = textEditWidget;
|
||||
this.FieldErrorMessageWidget = errorMessageWidget;
|
||||
this.FieldValidationHandlers = validationHandlers;
|
||||
}
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
bool fieldIsValid = true;
|
||||
foreach (ValidationHandler validationHandler in FieldValidationHandlers)
|
||||
{
|
||||
if (fieldIsValid)
|
||||
{
|
||||
ValidationStatus validationStatus = validationHandler(this.FieldEditWidget.Text);
|
||||
if (!validationStatus.IsValid)
|
||||
{
|
||||
fieldIsValid = false;
|
||||
FieldErrorMessageWidget.Text = validationStatus.ErrorMessage;
|
||||
FieldErrorMessageWidget.Visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return fieldIsValid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,13 @@ Copyright (c) 2014, Lars Brubaker
|
|||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
|
|
@ -23,103 +23,90 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using MatterHackers.Agg.UI;
|
||||
using MatterHackers.MatterControl.DataStorage;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace MatterHackers.MatterControl
|
||||
{
|
||||
class ManifestFile
|
||||
{
|
||||
List<PrintItem> projectFiles;
|
||||
string projectName = "Test Project";
|
||||
string projectDateCreated;
|
||||
internal class ManifestFile
|
||||
{
|
||||
private List<PrintItem> projectFiles;
|
||||
private string projectName = "Test Project";
|
||||
private string projectDateCreated;
|
||||
|
||||
public ManifestFile()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
projectDateCreated = now.ToString("s");
|
||||
}
|
||||
public ManifestFile()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
projectDateCreated = now.ToString("s");
|
||||
}
|
||||
|
||||
public List<PrintItem> ProjectFiles
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectFiles;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectFiles = value;
|
||||
}
|
||||
}
|
||||
public List<PrintItem> ProjectFiles
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectFiles;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectFiles = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string ProjectName
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectName;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectName = value;
|
||||
}
|
||||
}
|
||||
public string ProjectName
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectName;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectName = value;
|
||||
}
|
||||
}
|
||||
|
||||
public string ProjectDateCreated
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectDateCreated;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectDateCreated = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public string ProjectDateCreated
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectDateCreated;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectDateCreated = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class ManifestFileHandler
|
||||
{
|
||||
private ManifestFile project;
|
||||
|
||||
class ManifestFileHandler
|
||||
{
|
||||
ManifestFile project;
|
||||
public ManifestFileHandler(List<PrintItem> projectFiles)
|
||||
{
|
||||
if (projectFiles != null)
|
||||
{
|
||||
project = new ManifestFile();
|
||||
project.ProjectFiles = projectFiles;
|
||||
}
|
||||
}
|
||||
|
||||
public ManifestFileHandler(List<PrintItem> projectFiles)
|
||||
{
|
||||
if (projectFiles != null)
|
||||
{
|
||||
project = new ManifestFile();
|
||||
project.ProjectFiles = projectFiles;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveAs()
|
||||
//Opens Save file dialog and outputs current queue as a project
|
||||
{
|
||||
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save Project|*.mcp");
|
||||
public void SaveAs()
|
||||
//Opens Save file dialog and outputs current queue as a project
|
||||
{
|
||||
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save Project|*.mcp");
|
||||
|
||||
FileDialog.SaveFileDialog(saveParams, onSaveFileSelected);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void onSaveFileSelected(SaveFileDialogParams saveParams)
|
||||
private void onSaveFileSelected(SaveFileDialogParams saveParams)
|
||||
{
|
||||
if (saveParams.FileName != null)
|
||||
{
|
||||
|
|
@ -127,32 +114,33 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
}
|
||||
|
||||
static string applicationDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
|
||||
static string defaultPathAndFileName = applicationDataPath + "/data/default.mcp";
|
||||
public void ExportToJson(string savedFileName = null)
|
||||
{
|
||||
if (savedFileName == null)
|
||||
{
|
||||
savedFileName = defaultPathAndFileName;
|
||||
}
|
||||
string jsonString = JsonConvert.SerializeObject(this.project, Newtonsoft.Json.Formatting.Indented);
|
||||
if (!Directory.Exists(applicationDataPath + "/data/"))
|
||||
{
|
||||
Directory.CreateDirectory(applicationDataPath + "/data/");
|
||||
}
|
||||
FileStream fs = new FileStream(savedFileName, FileMode.Create);
|
||||
StreamWriter sw = new System.IO.StreamWriter(fs);
|
||||
sw.Write(jsonString);
|
||||
sw.Close();
|
||||
}
|
||||
private static string applicationDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
|
||||
private static string defaultPathAndFileName = applicationDataPath + "/data/default.mcp";
|
||||
|
||||
public void OpenFromDialog()
|
||||
{
|
||||
OpenFileDialogParams openParams = new OpenFileDialogParams("Select a Project file|*.mcp");
|
||||
public void ExportToJson(string savedFileName = null)
|
||||
{
|
||||
if (savedFileName == null)
|
||||
{
|
||||
savedFileName = defaultPathAndFileName;
|
||||
}
|
||||
string jsonString = JsonConvert.SerializeObject(this.project, Newtonsoft.Json.Formatting.Indented);
|
||||
if (!Directory.Exists(applicationDataPath + "/data/"))
|
||||
{
|
||||
Directory.CreateDirectory(applicationDataPath + "/data/");
|
||||
}
|
||||
FileStream fs = new FileStream(savedFileName, FileMode.Create);
|
||||
StreamWriter sw = new System.IO.StreamWriter(fs);
|
||||
sw.Write(jsonString);
|
||||
sw.Close();
|
||||
}
|
||||
|
||||
public void OpenFromDialog()
|
||||
{
|
||||
OpenFileDialogParams openParams = new OpenFileDialogParams("Select a Project file|*.mcp");
|
||||
FileDialog.OpenFileDialog(openParams, onManifestFileLoad);
|
||||
}
|
||||
}
|
||||
|
||||
void onManifestFileLoad(OpenFileDialogParams openParams)
|
||||
private void onManifestFileLoad(OpenFileDialogParams openParams)
|
||||
{
|
||||
if (openParams.FileName != null)
|
||||
{
|
||||
|
|
@ -161,28 +149,28 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
}
|
||||
|
||||
public List<PrintItem> ImportFromJson(string loadedFileName = null)
|
||||
{
|
||||
if (loadedFileName == null)
|
||||
{
|
||||
loadedFileName = defaultPathAndFileName;
|
||||
}
|
||||
public List<PrintItem> ImportFromJson(string loadedFileName = null)
|
||||
{
|
||||
if (loadedFileName == null)
|
||||
{
|
||||
loadedFileName = defaultPathAndFileName;
|
||||
}
|
||||
|
||||
if (System.IO.File.Exists(loadedFileName))
|
||||
{
|
||||
StreamReader sr = new System.IO.StreamReader(loadedFileName);
|
||||
ManifestFile newProject = (ManifestFile)Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), typeof(ManifestFile));
|
||||
sr.Close();
|
||||
if (newProject == null)
|
||||
{
|
||||
return new List<PrintItem>();
|
||||
}
|
||||
return newProject.ProjectFiles;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (System.IO.File.Exists(loadedFileName))
|
||||
{
|
||||
StreamReader sr = new System.IO.StreamReader(loadedFileName);
|
||||
ManifestFile newProject = (ManifestFile)Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), typeof(ManifestFile));
|
||||
sr.Close();
|
||||
if (newProject == null)
|
||||
{
|
||||
return new List<PrintItem>();
|
||||
}
|
||||
return newProject.ProjectFiles;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,13 @@ Copyright (c) 2014, Lars Brubaker
|
|||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
|
|
@ -23,141 +23,132 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using MatterHackers.PolygonMesh.Processors;
|
||||
using MatterHackers.Agg.UI;
|
||||
using MatterHackers.MatterControl.DataStorage;
|
||||
using MatterHackers.MatterControl.PrintQueue;
|
||||
|
||||
using MatterHackers.PolygonMesh.Processors;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
|
||||
namespace MatterHackers.MatterControl
|
||||
{
|
||||
class ManifestItem
|
||||
{
|
||||
public int ItemQuantity { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string FileName { get; set; }
|
||||
}
|
||||
|
||||
class Project
|
||||
{
|
||||
List<ManifestItem> projectFiles;
|
||||
string projectName = "Test Project";
|
||||
string projectDateCreated;
|
||||
internal class ManifestItem
|
||||
{
|
||||
public int ItemQuantity { get; set; }
|
||||
|
||||
public Project()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
projectDateCreated = now.ToString("s");
|
||||
}
|
||||
public string Name { get; set; }
|
||||
|
||||
public List<ManifestItem> ProjectFiles
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectFiles;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectFiles = value;
|
||||
}
|
||||
}
|
||||
public string FileName { get; set; }
|
||||
}
|
||||
|
||||
public string ProjectName
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectName;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectName = value;
|
||||
}
|
||||
}
|
||||
internal class Project
|
||||
{
|
||||
private List<ManifestItem> projectFiles;
|
||||
private string projectName = "Test Project";
|
||||
private string projectDateCreated;
|
||||
|
||||
public string ProjectDateCreated
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectDateCreated;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectDateCreated = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ProjectFileHandler
|
||||
{
|
||||
Project project;
|
||||
Dictionary<string, ManifestItem> sourceFiles = new Dictionary<string, ManifestItem>();
|
||||
HashSet<string> addedFileNames = new HashSet<string>();
|
||||
public Project()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
projectDateCreated = now.ToString("s");
|
||||
}
|
||||
|
||||
public ProjectFileHandler(List<PrintItem> projectFiles)
|
||||
{
|
||||
if (projectFiles != null)
|
||||
{
|
||||
project = new Project();
|
||||
public List<ManifestItem> ProjectFiles
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectFiles;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectFiles = value;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (PrintItem item in projectFiles)
|
||||
{
|
||||
if (sourceFiles.ContainsKey(item.FileLocation))
|
||||
{
|
||||
sourceFiles[item.FileLocation].ItemQuantity = sourceFiles[item.FileLocation].ItemQuantity + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
string fileNameOnly = Path.GetFileName(item.FileLocation);
|
||||
if (addedFileNames.Contains(fileNameOnly))
|
||||
{
|
||||
StyledMessageBox.ShowMessageBox(null, string.Format("Duplicate file name found but in a different folder '{0}'. This part will not be added to the collection.\n\n{1}", fileNameOnly, item.FileLocation), "Duplicate File");
|
||||
continue;
|
||||
}
|
||||
public string ProjectName
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectName;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectName = value;
|
||||
}
|
||||
}
|
||||
|
||||
addedFileNames.Add(fileNameOnly);
|
||||
public string ProjectDateCreated
|
||||
{
|
||||
get
|
||||
{
|
||||
return projectDateCreated;
|
||||
}
|
||||
set
|
||||
{
|
||||
projectDateCreated = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ManifestItem manifestItem = new ManifestItem();
|
||||
manifestItem.ItemQuantity = 1;
|
||||
manifestItem.Name = item.Name;
|
||||
manifestItem.FileName = Path.GetFileName(item.FileLocation);
|
||||
internal class ProjectFileHandler
|
||||
{
|
||||
private Project project;
|
||||
private Dictionary<string, ManifestItem> sourceFiles = new Dictionary<string, ManifestItem>();
|
||||
private HashSet<string> addedFileNames = new HashSet<string>();
|
||||
|
||||
sourceFiles.Add(item.FileLocation, manifestItem);
|
||||
}
|
||||
}
|
||||
List<ManifestItem> manifestFiles = sourceFiles.Values.ToList();
|
||||
project.ProjectFiles = manifestFiles;
|
||||
}
|
||||
}
|
||||
public ProjectFileHandler(List<PrintItem> projectFiles)
|
||||
{
|
||||
if (projectFiles != null)
|
||||
{
|
||||
project = new Project();
|
||||
|
||||
//Opens Save file dialog and outputs current queue as a project
|
||||
public void SaveAs()
|
||||
{
|
||||
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save Project|*.zip");
|
||||
foreach (PrintItem item in projectFiles)
|
||||
{
|
||||
if (sourceFiles.ContainsKey(item.FileLocation))
|
||||
{
|
||||
sourceFiles[item.FileLocation].ItemQuantity = sourceFiles[item.FileLocation].ItemQuantity + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
string fileNameOnly = Path.GetFileName(item.FileLocation);
|
||||
if (addedFileNames.Contains(fileNameOnly))
|
||||
{
|
||||
StyledMessageBox.ShowMessageBox(null, string.Format("Duplicate file name found but in a different folder '{0}'. This part will not be added to the collection.\n\n{1}", fileNameOnly, item.FileLocation), "Duplicate File");
|
||||
continue;
|
||||
}
|
||||
|
||||
addedFileNames.Add(fileNameOnly);
|
||||
|
||||
ManifestItem manifestItem = new ManifestItem();
|
||||
manifestItem.ItemQuantity = 1;
|
||||
manifestItem.Name = item.Name;
|
||||
manifestItem.FileName = Path.GetFileName(item.FileLocation);
|
||||
|
||||
sourceFiles.Add(item.FileLocation, manifestItem);
|
||||
}
|
||||
}
|
||||
List<ManifestItem> manifestFiles = sourceFiles.Values.ToList();
|
||||
project.ProjectFiles = manifestFiles;
|
||||
}
|
||||
}
|
||||
|
||||
//Opens Save file dialog and outputs current queue as a project
|
||||
public void SaveAs()
|
||||
{
|
||||
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save Project|*.zip");
|
||||
|
||||
FileDialog.SaveFileDialog(saveParams, onSaveFileSelected);
|
||||
}
|
||||
}
|
||||
|
||||
void onSaveFileSelected(SaveFileDialogParams saveParams)
|
||||
private void onSaveFileSelected(SaveFileDialogParams saveParams)
|
||||
{
|
||||
if (saveParams.FileName != null)
|
||||
{
|
||||
|
|
@ -165,34 +156,34 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
}
|
||||
|
||||
static string applicationDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
|
||||
static string archiveStagingFolder = Path.Combine(applicationDataPath, "data", "temp", "project-assembly");
|
||||
static string defaultManifestPathAndFileName = Path.Combine(archiveStagingFolder, "manifest.json");
|
||||
static string defaultProjectPathAndFileName = Path.Combine(applicationDataPath, "data", "default.zip");
|
||||
private static string applicationDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
|
||||
private static string archiveStagingFolder = Path.Combine(applicationDataPath, "data", "temp", "project-assembly");
|
||||
private static string defaultManifestPathAndFileName = Path.Combine(archiveStagingFolder, "manifest.json");
|
||||
private static string defaultProjectPathAndFileName = Path.Combine(applicationDataPath, "data", "default.zip");
|
||||
|
||||
public static void EmptyFolder(System.IO.DirectoryInfo directory)
|
||||
{
|
||||
foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
|
||||
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
|
||||
}
|
||||
public static void EmptyFolder(System.IO.DirectoryInfo directory)
|
||||
{
|
||||
foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
|
||||
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
|
||||
}
|
||||
|
||||
public void ExportToProjectArchive(string savedFileName = null)
|
||||
{
|
||||
if (savedFileName == null)
|
||||
{
|
||||
savedFileName = defaultProjectPathAndFileName;
|
||||
}
|
||||
public void ExportToProjectArchive(string savedFileName = null)
|
||||
{
|
||||
if (savedFileName == null)
|
||||
{
|
||||
savedFileName = defaultProjectPathAndFileName;
|
||||
}
|
||||
|
||||
//If the temp folder doesn't exist - create it, otherwise clear it
|
||||
//If the temp folder doesn't exist - create it, otherwise clear it
|
||||
if (!Directory.Exists(archiveStagingFolder))
|
||||
{
|
||||
{
|
||||
Directory.CreateDirectory(archiveStagingFolder);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@archiveStagingFolder);
|
||||
EmptyFolder(directory);
|
||||
}
|
||||
EmptyFolder(directory);
|
||||
}
|
||||
|
||||
//Create and save the project manifest file into the temp directory
|
||||
File.WriteAllText(defaultManifestPathAndFileName, JsonConvert.SerializeObject(this.project, Newtonsoft.Json.Formatting.Indented));
|
||||
|
|
@ -203,23 +194,23 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
|
||||
// Delete or move existing file out of the way as CreateFromDirectory will not overwrite and thows an exception
|
||||
if(File.Exists(savedFileName))
|
||||
if (File.Exists(savedFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(savedFileName);
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
string directory = Path.GetDirectoryName(savedFileName);
|
||||
string fileName = Path.GetFileNameWithoutExtension(savedFileName);
|
||||
string extension = Path.GetExtension(savedFileName);
|
||||
string candidatePath;
|
||||
|
||||
for(int i = 1; i < 20; i++)
|
||||
for (int i = 1; i < 20; i++)
|
||||
{
|
||||
candidatePath = Path.Combine(directory, string.Format("{0}({1}){2}", fileName, i, extension));
|
||||
if(!File.Exists(candidatePath))
|
||||
if (!File.Exists(candidatePath))
|
||||
{
|
||||
File.Move(savedFileName, candidatePath);
|
||||
break;
|
||||
|
|
@ -228,42 +219,42 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
}
|
||||
|
||||
ZipFile.CreateFromDirectory(archiveStagingFolder, savedFileName,CompressionLevel.Optimal, true);
|
||||
ZipFile.CreateFromDirectory(archiveStagingFolder, savedFileName, CompressionLevel.Optimal, true);
|
||||
}
|
||||
|
||||
private static void CopyFileToTempFolder(string sourceFile, string fileName)
|
||||
{
|
||||
if (File.Exists(sourceFile))
|
||||
{
|
||||
{
|
||||
if (File.Exists(sourceFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Will not overwrite if the destination file already exists.
|
||||
File.Copy(sourceFile, Path.Combine(archiveStagingFolder, fileName));
|
||||
}
|
||||
|
||||
// Catch exception if the file was already copied.
|
||||
// Catch exception if the file was already copied.
|
||||
catch (IOException copyError)
|
||||
{
|
||||
Console.WriteLine(copyError.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenFromDialog()
|
||||
{
|
||||
OpenFileDialogParams openParams = new OpenFileDialogParams("Zip file|*.zip");
|
||||
{
|
||||
OpenFileDialogParams openParams = new OpenFileDialogParams("Zip file|*.zip");
|
||||
FileDialog.OpenFileDialog(openParams, onProjectArchiveLoad);
|
||||
}
|
||||
}
|
||||
|
||||
void onProjectArchiveLoad(OpenFileDialogParams openParams)
|
||||
private void onProjectArchiveLoad(OpenFileDialogParams openParams)
|
||||
{
|
||||
List<PrintItem> partFiles;
|
||||
if (openParams.FileNames != null)
|
||||
{
|
||||
string loadedFileName = openParams.FileName;
|
||||
partFiles = ImportFromProjectArchive(loadedFileName);
|
||||
partFiles = ImportFromProjectArchive(loadedFileName);
|
||||
if (partFiles != null)
|
||||
{
|
||||
{
|
||||
foreach (PrintItem part in partFiles)
|
||||
{
|
||||
QueueData.Instance.AddItem(new PrintItemWrapper(new PrintItem(part.Name, part.FileLocation)));
|
||||
|
|
@ -272,107 +263,107 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
}
|
||||
|
||||
public List<PrintItem> ImportFromProjectArchive(string loadedFileName = null)
|
||||
{
|
||||
if (loadedFileName == null)
|
||||
{
|
||||
loadedFileName = defaultProjectPathAndFileName;
|
||||
}
|
||||
public List<PrintItem> ImportFromProjectArchive(string loadedFileName = null)
|
||||
{
|
||||
if (loadedFileName == null)
|
||||
{
|
||||
loadedFileName = defaultProjectPathAndFileName;
|
||||
}
|
||||
|
||||
if (System.IO.File.Exists(loadedFileName))
|
||||
{
|
||||
FileStream fs = File.OpenRead(loadedFileName);
|
||||
ZipArchive zip = new ZipArchive(fs);
|
||||
int projectHashCode = zip.GetHashCode();
|
||||
if (System.IO.File.Exists(loadedFileName))
|
||||
{
|
||||
FileStream fs = File.OpenRead(loadedFileName);
|
||||
ZipArchive zip = new ZipArchive(fs);
|
||||
int projectHashCode = zip.GetHashCode();
|
||||
|
||||
//If the temp folder doesn't exist - create it, otherwise clear it
|
||||
string stagingFolder = Path.Combine(applicationDataPath, "data", "temp", "project-extract", projectHashCode.ToString());
|
||||
if (!Directory.Exists(stagingFolder))
|
||||
{
|
||||
Directory.CreateDirectory(stagingFolder);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@stagingFolder);
|
||||
EmptyFolder(directory);
|
||||
}
|
||||
//If the temp folder doesn't exist - create it, otherwise clear it
|
||||
string stagingFolder = Path.Combine(applicationDataPath, "data", "temp", "project-extract", projectHashCode.ToString());
|
||||
if (!Directory.Exists(stagingFolder))
|
||||
{
|
||||
Directory.CreateDirectory(stagingFolder);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@stagingFolder);
|
||||
EmptyFolder(directory);
|
||||
}
|
||||
|
||||
List<PrintItem> printItemList = new List<PrintItem>();
|
||||
Project projectManifest = null;
|
||||
List<PrintItem> printItemList = new List<PrintItem>();
|
||||
Project projectManifest = null;
|
||||
|
||||
foreach (ZipArchiveEntry zipEntry in zip.Entries)
|
||||
{
|
||||
string sourceExtension = Path.GetExtension(zipEntry.Name).ToUpper();
|
||||
foreach (ZipArchiveEntry zipEntry in zip.Entries)
|
||||
{
|
||||
string sourceExtension = Path.GetExtension(zipEntry.Name).ToUpper();
|
||||
|
||||
// Note: directories have empty Name properties
|
||||
//
|
||||
// Only process ZipEntries that are:
|
||||
// - not directories and
|
||||
// - are in the ValidFileExtension list or
|
||||
// - have a .GCODE extension or
|
||||
// - are named manifest.json
|
||||
if (!string.IsNullOrWhiteSpace(zipEntry.Name) &&
|
||||
(zipEntry.Name == "manifest.json"
|
||||
|| MeshFileIo.ValidFileExtensions().Contains(sourceExtension)
|
||||
|| sourceExtension == ".GCODE"))
|
||||
{
|
||||
string extractedFileName = Path.Combine(stagingFolder, zipEntry.Name);
|
||||
// Note: directories have empty Name properties
|
||||
//
|
||||
// Only process ZipEntries that are:
|
||||
// - not directories and
|
||||
// - are in the ValidFileExtension list or
|
||||
// - have a .GCODE extension or
|
||||
// - are named manifest.json
|
||||
if (!string.IsNullOrWhiteSpace(zipEntry.Name) &&
|
||||
(zipEntry.Name == "manifest.json"
|
||||
|| MeshFileIo.ValidFileExtensions().Contains(sourceExtension)
|
||||
|| sourceExtension == ".GCODE"))
|
||||
{
|
||||
string extractedFileName = Path.Combine(stagingFolder, zipEntry.Name);
|
||||
|
||||
string neededPathForZip = Path.GetDirectoryName(extractedFileName);
|
||||
if (!Directory.Exists(neededPathForZip))
|
||||
{
|
||||
Directory.CreateDirectory(neededPathForZip);
|
||||
}
|
||||
string neededPathForZip = Path.GetDirectoryName(extractedFileName);
|
||||
if (!Directory.Exists(neededPathForZip))
|
||||
{
|
||||
Directory.CreateDirectory(neededPathForZip);
|
||||
}
|
||||
|
||||
using (Stream zipStream = zipEntry.Open())
|
||||
using (FileStream streamWriter = File.Create(extractedFileName))
|
||||
{
|
||||
zipStream.CopyTo(streamWriter);
|
||||
}
|
||||
using (Stream zipStream = zipEntry.Open())
|
||||
using (FileStream streamWriter = File.Create(extractedFileName))
|
||||
{
|
||||
zipStream.CopyTo(streamWriter);
|
||||
}
|
||||
|
||||
if (zipEntry.Name == "manifest.json")
|
||||
{
|
||||
using (StreamReader sr = new System.IO.StreamReader(extractedFileName))
|
||||
{
|
||||
projectManifest = (Project)Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), typeof(Project));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (zipEntry.Name == "manifest.json")
|
||||
{
|
||||
using (StreamReader sr = new System.IO.StreamReader(extractedFileName))
|
||||
{
|
||||
projectManifest = (Project)Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), typeof(Project));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (projectManifest != null)
|
||||
{
|
||||
foreach (ManifestItem item in projectManifest.ProjectFiles)
|
||||
{
|
||||
for (int i = 1; i <= item.ItemQuantity; i++)
|
||||
{
|
||||
printItemList.Add(this.GetPrintItemFromFile(Path.Combine(stagingFolder, item.FileName), item.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] files = Directory.GetFiles(stagingFolder,"*.*", SearchOption.AllDirectories);
|
||||
foreach(string fileName in files)
|
||||
{
|
||||
printItemList.Add(this.GetPrintItemFromFile(fileName, Path.GetFileNameWithoutExtension(fileName)));
|
||||
}
|
||||
}
|
||||
|
||||
return printItemList;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (projectManifest != null)
|
||||
{
|
||||
foreach (ManifestItem item in projectManifest.ProjectFiles)
|
||||
{
|
||||
for (int i = 1; i <= item.ItemQuantity; i++)
|
||||
{
|
||||
printItemList.Add(this.GetPrintItemFromFile(Path.Combine(stagingFolder, item.FileName), item.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] files = Directory.GetFiles(stagingFolder, "*.*", SearchOption.AllDirectories);
|
||||
foreach (string fileName in files)
|
||||
{
|
||||
printItemList.Add(this.GetPrintItemFromFile(fileName, Path.GetFileNameWithoutExtension(fileName)));
|
||||
}
|
||||
}
|
||||
|
||||
private PrintItem GetPrintItemFromFile(string fileName, string displayName)
|
||||
{
|
||||
PrintItem item = new PrintItem();
|
||||
item.FileLocation = fileName;
|
||||
item.Name = displayName;
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return printItemList;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private PrintItem GetPrintItemFromFile(string fileName, string displayName)
|
||||
{
|
||||
PrintItem item = new PrintItem();
|
||||
item.FileLocation = fileName;
|
||||
item.Name = displayName;
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,13 @@ Copyright (c) 2014, Lars Brubaker
|
|||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
|
|
@ -23,22 +23,20 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MatterHackers.MatterControl
|
||||
{
|
||||
public class TupleList<T1, T2> : List<Tuple<T1, T2>>
|
||||
{
|
||||
public void Add(T1 item, T2 item2)
|
||||
{
|
||||
Add(new Tuple<T1, T2>(item, item2));
|
||||
}
|
||||
}
|
||||
}
|
||||
public class TupleList<T1, T2> : List<Tuple<T1, T2>>
|
||||
{
|
||||
public void Add(T1 item, T2 item2)
|
||||
{
|
||||
Add(new Tuple<T1, T2>(item, item2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,13 @@ Copyright (c) 2014, Lars Brubaker
|
|||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
|
|
@ -23,54 +23,50 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MatterHackers.MatterControl
|
||||
{
|
||||
public class JsonResponseDictionary : Dictionary<string, string>
|
||||
{
|
||||
public string get(string key)
|
||||
{
|
||||
string result;
|
||||
if (this.ContainsKey(key))
|
||||
{
|
||||
result = this[key];
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public class JsonResponseDictionary : Dictionary<string, string>
|
||||
{
|
||||
public string get(string key)
|
||||
{
|
||||
string result;
|
||||
if (this.ContainsKey(key))
|
||||
{
|
||||
result = this[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool GetInt(string key, out int result)
|
||||
{
|
||||
if (this.get(key) != null)
|
||||
{
|
||||
bool isInt = Int32.TryParse(this.get(key), out result);
|
||||
if (isInt)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
public bool GetInt(string key, out int result)
|
||||
{
|
||||
if (this.get(key) != null)
|
||||
{
|
||||
bool isInt = Int32.TryParse(this.get(key), out result);
|
||||
if (isInt)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,13 +3,13 @@ Copyright (c) 2014, Lars Brubaker
|
|||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
|
|
@ -23,31 +23,29 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace MatterHackers.MatterControl
|
||||
{
|
||||
public class RequestManager
|
||||
{
|
||||
public string LastResponse { protected set; get; }
|
||||
|
||||
CookieContainer cookies = new CookieContainer();
|
||||
|
||||
internal string GetCookieValue(Uri SiteUri,string name)
|
||||
|
||||
private CookieContainer cookies = new CookieContainer();
|
||||
|
||||
internal string GetCookieValue(Uri SiteUri, string name)
|
||||
{
|
||||
Cookie cookie = cookies.GetCookies(SiteUri)[name];
|
||||
return (cookie == null) ? null : cookie.Value;
|
||||
}
|
||||
|
||||
|
||||
public string GetResponseContent(HttpWebResponse response)
|
||||
{
|
||||
if (response == null)
|
||||
|
|
@ -55,69 +53,69 @@ namespace MatterHackers.MatterControl
|
|||
throw new ArgumentNullException("response");
|
||||
}
|
||||
|
||||
string responseFromServer = null;
|
||||
|
||||
string responseFromServer = null;
|
||||
|
||||
try
|
||||
{
|
||||
// Get the stream containing content returned by the server.
|
||||
using (Stream dataStream = response.GetResponseStream())
|
||||
{
|
||||
// Open the stream using a StreamReader for easy access.
|
||||
using (StreamReader reader = new StreamReader(dataStream))
|
||||
{
|
||||
// Read the content.
|
||||
responseFromServer = reader.ReadToEnd();
|
||||
// Cleanup the streams and the response.
|
||||
}
|
||||
}
|
||||
using (Stream dataStream = response.GetResponseStream())
|
||||
{
|
||||
// Open the stream using a StreamReader for easy access.
|
||||
using (StreamReader reader = new StreamReader(dataStream))
|
||||
{
|
||||
// Read the content.
|
||||
responseFromServer = reader.ReadToEnd();
|
||||
// Cleanup the streams and the response.
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
{
|
||||
response.Close();
|
||||
}
|
||||
LastResponse = responseFromServer;
|
||||
return responseFromServer;
|
||||
}
|
||||
|
||||
|
||||
public HttpWebResponse SendPOSTRequest(string uri, string content, string login, string password, bool allowAutoRedirect)
|
||||
{
|
||||
HttpWebRequest request = GeneratePOSTRequest(uri, content, login, password, allowAutoRedirect);
|
||||
return GetResponse(request);
|
||||
}
|
||||
|
||||
|
||||
public HttpWebResponse SendGETRequest(string uri, string login, string password, bool allowAutoRedirect)
|
||||
{
|
||||
HttpWebRequest request = GenerateGETRequest(uri, login, password, allowAutoRedirect);
|
||||
return GetResponse(request);
|
||||
}
|
||||
|
||||
|
||||
public HttpWebResponse SendRequest(string uri, string content, string method, string login, string password, bool allowAutoRedirect)
|
||||
{
|
||||
HttpWebRequest request = GenerateRequest(uri, content, method, login, password, allowAutoRedirect);
|
||||
return GetResponse(request);
|
||||
}
|
||||
|
||||
|
||||
public HttpWebRequest GenerateGETRequest(string uri, string login, string password, bool allowAutoRedirect)
|
||||
{
|
||||
return GenerateRequest(uri, null, "GET", null, null, allowAutoRedirect);
|
||||
}
|
||||
|
||||
|
||||
public HttpWebRequest GeneratePOSTRequest(string uri, string content, string login, string password, bool allowAutoRedirect)
|
||||
{
|
||||
return GenerateRequest(uri, content, "POST", null, null, allowAutoRedirect);
|
||||
}
|
||||
|
||||
|
||||
internal HttpWebRequest GenerateRequest(string uri, string content, string method, string login, string password, bool allowAutoRedirect)
|
||||
{
|
||||
if (uri == null)
|
||||
{
|
||||
throw new ArgumentNullException("uri");
|
||||
}
|
||||
// Create a request using a URL that can receive a post.
|
||||
// Create a request using a URL that can receive a post.
|
||||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
|
||||
// Set the Method property of the request to POST.
|
||||
request.Method = method;
|
||||
|
|
@ -142,23 +140,23 @@ namespace MatterHackers.MatterControl
|
|||
// Set the ContentLength property of the WebRequest.
|
||||
request.ContentLength = byteArray.Length;
|
||||
// Get the request stream.
|
||||
Stream dataStream = null;
|
||||
try
|
||||
{
|
||||
dataStream = request.GetRequestStream();
|
||||
// Write the data to the request stream.
|
||||
dataStream.Write(byteArray, 0, byteArray.Length);
|
||||
// Close the Stream object.
|
||||
dataStream.Close();
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
Console.WriteLine("Web exception occurred. Status code: {0}", ex.Status);
|
||||
}
|
||||
}
|
||||
Stream dataStream = null;
|
||||
try
|
||||
{
|
||||
dataStream = request.GetRequestStream();
|
||||
// Write the data to the request stream.
|
||||
dataStream.Write(byteArray, 0, byteArray.Length);
|
||||
// Close the Stream object.
|
||||
dataStream.Close();
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
Console.WriteLine("Web exception occurred. Status code: {0}", ex.Status);
|
||||
}
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
internal HttpWebResponse GetResponse(HttpWebRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
|
|
@ -168,8 +166,8 @@ namespace MatterHackers.MatterControl
|
|||
HttpWebResponse response = null;
|
||||
try
|
||||
{
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
cookies.Add(response.Cookies);
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
cookies.Add(response.Cookies);
|
||||
// Print the properties of each cookie.
|
||||
Console.WriteLine("\nCookies: ");
|
||||
foreach (Cookie cook in cookies.GetCookies(request.RequestUri))
|
||||
|
|
@ -189,6 +187,5 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue