[longomatch] Check for invalid path chars in template names
- From: Andoni Morales Alastruey <amorales src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [longomatch] Check for invalid path chars in template names
- Date: Tue, 28 Oct 2014 09:50:42 +0000 (UTC)
commit aecf83b4ff3d5dfc4f70086904f29f27b5251ae9
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date: Tue Oct 21 23:49:31 2014 +0200
Check for invalid path chars in template names
LongoMatch.Core/Common/Exceptions.cs | 10 ++++
LongoMatch.GUI/Gui/Component/TeamTemplateEditor.cs | 9 +++-
LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs | 47 +++++++++++++++-----
LongoMatch.GUI/Gui/Panel/TeamsTemplatesPanel.cs | 30 ++++++++++--
LongoMatch.Services/Services/TemplatesService.cs | 31 +++++++++----
5 files changed, 100 insertions(+), 27 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Exceptions.cs b/LongoMatch.Core/Common/Exceptions.cs
index d581200..979fbc4 100644
--- a/LongoMatch.Core/Common/Exceptions.cs
+++ b/LongoMatch.Core/Common/Exceptions.cs
@@ -17,6 +17,7 @@
//
using System;
using Mono.Unix;
+using System.Collections.Generic;
namespace LongoMatch.Core.Common
{
@@ -60,5 +61,14 @@ namespace LongoMatch.Core.Common
{
}
}
+
+ public class InvalidTemplateFilenameException: Exception
+ {
+ public InvalidTemplateFilenameException (List<char> invalidChars):
+ base (Catalog.GetString("The name contains invalid characters: ") + String.Join (" ",
invalidChars))
+ {
+ }
+ }
+
}
diff --git a/LongoMatch.GUI/Gui/Component/TeamTemplateEditor.cs
b/LongoMatch.GUI/Gui/Component/TeamTemplateEditor.cs
index bb43701..eab2ebe 100644
--- a/LongoMatch.GUI/Gui/Component/TeamTemplateEditor.cs
+++ b/LongoMatch.GUI/Gui/Component/TeamTemplateEditor.cs
@@ -278,8 +278,13 @@ namespace LongoMatch.Gui.Component
void HandleSaveTemplateClicked (object sender, EventArgs e)
{
if (template != null) {
- Config.TeamTemplatesProvider.Update (template);
- Edited = false;
+ try {
+ Config.TeamTemplatesProvider.Update (template);
+ Edited = false;
+ } catch (InvalidTemplateFilenameException ex) {
+ Config.GUIToolkit.ErrorMessage (ex.ToString (), this);
+ return;
+ }
}
if (TemplateSaved != null)
TemplateSaved (this, null);
diff --git a/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
b/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
index 7c35f34..41f042c 100644
--- a/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
+++ b/LongoMatch.GUI/Gui/Panel/SportsTemplatesPanel.cs
@@ -165,6 +165,17 @@ namespace LongoMatch.Gui.Panel
}
}
+ bool SaveTemplate (Dashboard dashboard)
+ {
+ try {
+ provider.Update (dashboard);
+ return true;
+ } catch (InvalidTemplateFilenameException ex) {
+ Config.GUIToolkit.ErrorMessage (ex.Message, this);
+ return false;
+ }
+ }
+
void SaveStatic ()
{
string msg = Catalog.GetString ("System templates can't be edited, do you want to
create a copy?");
@@ -178,8 +189,7 @@ namespace LongoMatch.Gui.Panel
if (provider.TemplatesNames.Contains (newName)) {
msg = Catalog.GetString ("A template with the same name
already exists");
Config.GUIToolkit.ErrorMessage (msg, this);
- }
- else {
+ } else {
break;
}
}
@@ -189,8 +199,9 @@ namespace LongoMatch.Gui.Panel
Dashboard newtemplate = loadedTemplate.Clone ();
newtemplate.Name = newName;
newtemplate.Static = false;
- provider.Save (newtemplate);
- Load (newtemplate.Name);
+ if (SaveTemplate (newtemplate)) {
+ Load (newtemplate.Name);
+ }
}
}
@@ -205,8 +216,9 @@ namespace LongoMatch.Gui.Panel
} else {
string msg = Catalog.GetString ("Do you want to save the current
template");
if (!prompt || Config.GUIToolkit.QuestionMessage (msg, null, this)) {
- provider.Update (loadedTemplate);
- buttonswidget.Edited = false;
+ if (SaveTemplate (loadedTemplate)) {
+ buttonswidget.Edited = false;
+ }
}
}
}
@@ -314,12 +326,21 @@ namespace LongoMatch.Gui.Panel
}
}
if (dialog.SelectedTemplate != null) {
- provider.Copy (dialog.SelectedTemplate, dialog.Text);
+ try {
+ provider.Copy (dialog.SelectedTemplate, dialog.Text);
+ } catch (InvalidTemplateFilenameException ex) {
+ Config.GUIToolkit.ErrorMessage (ex.Message, this);
+ dialog.Destroy ();
+ return;
+ }
} else {
Dashboard template;
template = Dashboard.DefaultTemplate (dialog.Count);
template.Name = dialog.Text;
- provider.Save (template);
+ if (!SaveTemplate (template)) {
+ dialog.Destroy ();
+ return;
+ }
}
Load (dialog.Text);
}
@@ -337,10 +358,14 @@ namespace LongoMatch.Gui.Panel
Config.GUIToolkit.ErrorMessage (Catalog.GetString ("A template with
the same name already exists"), this);
args.RetVal = false;
} else {
- provider.Delete (template.Name);
+ string prevName = template.Name;
template.Name = args.NewText;
- provider.Save (template);
- templates.SetValue (iter, 1, template.Name);
+ if (!SaveTemplate (template)) {
+ template.Name = prevName;
+ } else {
+ provider.Delete (template.Name);
+ templates.SetValue (iter, 1, template.Name);
+ }
}
}
}
diff --git a/LongoMatch.GUI/Gui/Panel/TeamsTemplatesPanel.cs b/LongoMatch.GUI/Gui/Panel/TeamsTemplatesPanel.cs
index 98d9e7a..39b38a3 100644
--- a/LongoMatch.GUI/Gui/Panel/TeamsTemplatesPanel.cs
+++ b/LongoMatch.GUI/Gui/Panel/TeamsTemplatesPanel.cs
@@ -149,6 +149,17 @@ namespace LongoMatch.Gui.Panel
HandleSelectionChanged (null, null);
}
}
+
+ bool SaveTemplate (TeamTemplate template)
+ {
+ try {
+ provider.Update (template);
+ return true;
+ } catch (InvalidTemplateFilenameException ex) {
+ Config.GUIToolkit.ErrorMessage (ex.Message, this);
+ return false;
+ }
+ }
void HandleEnterTeamButton (object sender, EventArgs e)
{
@@ -185,7 +196,9 @@ namespace LongoMatch.Gui.Panel
if (loadedTeam == null)
return;
- provider.Update (loadedTeam);
+ if (!SaveTemplate (loadedTeam)) {
+ return;
+ }
/* The shield might have changed, update it just in case */
if (loadedTeam.Shield != null) {
teamseditortreeview.Model.SetValue (itersDict [loadedTeam.Name], 0,
@@ -281,7 +294,10 @@ namespace LongoMatch.Gui.Panel
team = TeamTemplate.DefaultTemplate (dialog.Count);
team.TeamName = dialog.Text;
team.Name = dialog.Text;
- provider.Update (team);
+ if (!SaveTemplate (team)) {
+ dialog.Destroy ();
+ return;
+ }
}
Load (dialog.Text);
}
@@ -299,10 +315,14 @@ namespace LongoMatch.Gui.Panel
Config.GUIToolkit.ErrorMessage (Catalog.GetString ("A team with the
same name already exists"), this);
args.RetVal = false;
} else {
- provider.Delete (team.Name);
+ string prevName = team.Name;
team.Name = args.NewText;
- provider.Save (team);
- teams.SetValue (iter, 1, team.Name);
+ if (!SaveTemplate (team)) {
+ team.Name = prevName;
+ } else {
+ provider.Delete (team.Name);
+ teams.SetValue (iter, 1, team.Name);
+ }
}
}
}
diff --git a/LongoMatch.Services/Services/TemplatesService.cs
b/LongoMatch.Services/Services/TemplatesService.cs
index 2441fd7..41373cd 100644
--- a/LongoMatch.Services/Services/TemplatesService.cs
+++ b/LongoMatch.Services/Services/TemplatesService.cs
@@ -36,7 +36,7 @@ namespace LongoMatch.Services
{
dict = new Dictionary<Type, ITemplateProvider> ();
dict.Add (typeof(TeamTemplate),
- new TeamTemplatesProvider (Config.TeamsDir));
+ new TeamTemplatesProvider (Config.TeamsDir));
dict.Add (typeof(Dashboard), new CategoriesTemplatesProvider (Config.AnalysisDir));
CheckDefaultTemplates ();
}
@@ -81,12 +81,7 @@ namespace LongoMatch.Services
this.extension = extension;
methodLoad = typeof(T).GetMethod ("Load");
methodDefaultTemplate = typeof(T).GetMethod ("DefaultTemplate");
- systemTemplates = new List<T>();
- }
-
- private string GetPath (string templateName)
- {
- return System.IO.Path.Combine (basePath, templateName) + extension;
+ systemTemplates = new List<T> ();
}
public virtual void CheckDefaultTemplate ()
@@ -149,6 +144,7 @@ namespace LongoMatch.Services
public void Save (ITemplate template)
{
+ CheckInvalidChars (template.Name);
string filename = GetPath (template.Name);
Log.Information ("Saving template " + filename);
@@ -168,14 +164,15 @@ namespace LongoMatch.Services
public void Update (ITemplate template)
{
+ CheckInvalidChars (template.Name);
string filename = GetPath (template.Name);
-
Log.Information ("Updating template " + filename);
/* Don't cach the Exception here to chain it up */
template.Save (filename);
}
- public void Register (T template) {
+ public void Register (T template)
+ {
systemTemplates.Add (template);
}
@@ -186,6 +183,7 @@ namespace LongoMatch.Services
"the name: ") + copy);
}
+ CheckInvalidChars (copy);
Log.Information (String.Format ("Copying template {0} to {1}", orig, copy));
File.Copy (GetPath (orig), GetPath (copy));
}
@@ -211,6 +209,21 @@ namespace LongoMatch.Services
t.Name = templateName;
Save (t);
}
+
+ void CheckInvalidChars (string name)
+ {
+ List<char> invalidChars;
+
+ invalidChars = name.Intersect (Path.GetInvalidFileNameChars ()).ToList ();
+ if (invalidChars.Count > 0) {
+ throw new InvalidTemplateFilenameException (invalidChars);
+ }
+ }
+
+ string GetPath (string templateName)
+ {
+ return System.IO.Path.Combine (basePath, templateName) + extension;
+ }
}
public class TeamTemplatesProvider: TemplatesProvider<TeamTemplate>, ITeamTemplatesProvider
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]