From 48e5b369369ce32a961b042258a052b3619b318d Mon Sep 17 00:00:00 2001
From: markus101 <markus.mcd5@gmail.com>
Date: Thu, 3 Feb 2011 18:58:02 -0800
Subject: [PATCH] Fixed logging for Settings Controller and QualityProvider

Setup/Update of Default QualityProfiles will occur on start
---
 NzbDrone.Core/CentralDispatch.cs              | 132 ++++++++++++++++++
 NzbDrone.Core/Providers/QualityProvider.cs    |   6 +-
 .../Controllers/SettingsController.cs         |  32 ++++-
 NzbDrone.Web/Views/Settings/Quality.ascx      |   3 +-
 4 files changed, 165 insertions(+), 8 deletions(-)

diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs
index 5fc333a20..8574d75a8 100644
--- a/NzbDrone.Core/CentralDispatch.cs
+++ b/NzbDrone.Core/CentralDispatch.cs
@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Web;
@@ -9,6 +10,7 @@
 using NzbDrone.Core.Providers;
 using NzbDrone.Core.Providers.Fakes;
 using NzbDrone.Core.Repository;
+using NzbDrone.Core.Repository.Quality;
 using SubSonic.DataProviders;
 using SubSonic.Query;
 using SubSonic.Repository;
@@ -76,6 +78,7 @@ public static void BindKernel()
 
                 ForceMigration(_kernel.Get<IRepository>());
                 SetupIndexers(_kernel.Get<IRepository>()); //Setup the default set of indexers on start-up
+                SetupDefaultQualityProfiles(_kernel.Get<IRepository>()); //Setup the default QualityProfiles on start-up
             }
         }
 
@@ -231,5 +234,134 @@ private static void SetupIndexers(IRepository repository)
                 repository.Update(nzbsrusIndexer);
             }
         }
+
+        private static void SetupDefaultQualityProfiles(IRepository repository)
+        {
+            var sdtv = new QualityProfile
+                            {
+                                Name = "SDTV",
+                                Allowed = new List<QualityTypes> {QualityTypes.TV},
+                                Cutoff = QualityTypes.TV
+                            };
+
+            var dvd = new QualityProfile
+                             {
+                                 Name = "DVD SD",
+                                 Allowed = new List<QualityTypes> {QualityTypes.DVD},
+                                 Cutoff = QualityTypes.DVD
+                             };
+
+            var bdrip = new QualityProfile
+                             {
+                                 Name = "BDRip",
+                                 Allowed = new List<QualityTypes> {QualityTypes.BDRip},
+                                 Cutoff = QualityTypes.BDRip
+                             };
+
+            var hdtv = new QualityProfile
+                             {
+                                 Name = "HDTV",
+                                 Allowed = new List<QualityTypes> {QualityTypes.HDTV},
+                                 Cutoff = QualityTypes.HDTV
+                             };
+
+            var webdl = new QualityProfile
+                           {
+                               Name = "WEBDL",
+                               Allowed = new List<QualityTypes> {QualityTypes.WEBDL},
+                               Cutoff = QualityTypes.WEBDL
+                           };
+
+            var bluray = new QualityProfile
+                           {
+                               Name = "Bluray",
+                               Allowed = new List<QualityTypes> {QualityTypes.Bluray},
+                               Cutoff = QualityTypes.Bluray
+                           };
+
+            //Add or Update SDTV
+            Logger.Debug(String.Format("Checking for default QualityProfile: {0}", sdtv.Name));
+            if (!repository.Exists<QualityProfile>(i => i.Name == sdtv.Name))
+            {
+                Logger.Debug(String.Format("Adding new default QualityProfile: {0}", sdtv.Name));
+                repository.Add(sdtv);
+            }
+
+            else
+            {
+                Logger.Debug(String.Format("Updating default QualityProfile: {0}", sdtv.Name));
+                repository.Update(sdtv);
+            }
+
+            //Add or Update DVD
+            Logger.Debug(String.Format("Checking for default QualityProfile: {0}", dvd.Name));
+            if (!repository.Exists<QualityProfile>(i => i.Name == dvd.Name))
+            {
+                Logger.Debug(String.Format("Adding new default QualityProfile: {0}", dvd.Name));
+                repository.Add(dvd);
+            }
+
+            else
+            {
+                Logger.Debug(String.Format("Updating default QualityProfile: {0}", dvd.Name));
+                repository.Update(dvd);
+            }
+
+            //Add or Update BDRip
+            Logger.Debug(String.Format("Checking for default QualityProfile: {0}", bdrip.Name));
+            if (!repository.Exists<QualityProfile>(i => i.Name == bdrip.Name))
+            {
+                Logger.Debug(String.Format("Adding new default QualityProfile: {0}", bdrip.Name));
+                repository.Add(bdrip);
+            }
+
+            else
+            {
+                Logger.Debug(String.Format("Updating default QualityProfile: {0}", bdrip.Name));
+                repository.Update(bdrip);
+            }
+
+            //Add or Update HDTV
+            Logger.Debug(String.Format("Checking for default QualityProfile: {0}", hdtv.Name));
+            if (!repository.Exists<QualityProfile>(i => i.Name == hdtv.Name))
+            {
+                Logger.Debug(String.Format("Adding new default QualityProfile: {0}", hdtv.Name));
+                repository.Add(hdtv);
+            }
+
+            else
+            {
+                Logger.Debug(String.Format("Updating default QualityProfile: {0}", hdtv.Name));
+                repository.Update(hdtv);
+            }
+
+            //Add or Update WEBDL
+            Logger.Debug(String.Format("Checking for default QualityProfile: {0}", webdl.Name));
+            if (!repository.Exists<QualityProfile>(i => i.Name == webdl.Name))
+            {
+                Logger.Debug(String.Format("Adding new default QualityProfile: {0}", webdl.Name));
+                repository.Add(webdl);
+            }
+
+            else
+            {
+                Logger.Debug(String.Format("Updating default QualityProfile: {0}", webdl.Name));
+                repository.Update(webdl);
+            }
+
+            //Add or Update Bluray
+            Logger.Debug(String.Format("Checking for default QualityProfile: {0}", bluray.Name));
+            if (!repository.Exists<QualityProfile>(i => i.Name == bluray.Name))
+            {
+                Logger.Debug(String.Format("Adding new default QualityProfile: {0}", bluray.Name));
+                repository.Add(bluray);
+            }
+
+            else
+            {
+                Logger.Debug(String.Format("Updating default QualityProfile: {0}", bluray.Name));
+                repository.Update(bluray);
+            }
+        }
     }
 }
\ No newline at end of file
diff --git a/NzbDrone.Core/Providers/QualityProvider.cs b/NzbDrone.Core/Providers/QualityProvider.cs
index 0141e1da7..6deac32fc 100644
--- a/NzbDrone.Core/Providers/QualityProvider.cs
+++ b/NzbDrone.Core/Providers/QualityProvider.cs
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using NLog;
 using NzbDrone.Core.Repository.Quality;
 using SubSonic.Repository;
 
@@ -10,6 +11,7 @@ namespace NzbDrone.Core.Providers
     public class QualityProvider : IQualityProvider
     {
         private IRepository _sonicRepo;
+        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
 
         public QualityProvider(IRepository sonicRepo)
         {
@@ -27,8 +29,8 @@ public void Update(QualityProfile profile)
         {
             if (!_sonicRepo.Exists<QualityProfile>(q => q.ProfileId == profile.ProfileId))
             {
-                //Log Error
-                throw new InvalidOperationException("Unable to update none existing profile");
+                Logger.Error("Unable to update non-existing profile");
+                throw new InvalidOperationException("Unable to update non-existing profile");
             }
 
             _sonicRepo.Update(profile);
diff --git a/NzbDrone.Web/Controllers/SettingsController.cs b/NzbDrone.Web/Controllers/SettingsController.cs
index c2ab4bf92..e999cb5f6 100644
--- a/NzbDrone.Web/Controllers/SettingsController.cs
+++ b/NzbDrone.Web/Controllers/SettingsController.cs
@@ -123,11 +123,12 @@ public ActionResult Index(SettingsModel data)
             }
             catch (Exception)
             {
-                if (Request.IsAjaxRequest())
-                    return Content("Settings Saved.");
+                Logger.Error("Error saving settings.");
 
-                return Content("Settings Saved.");
-                Logger.Error("");
+                if (Request.IsAjaxRequest())
+                    return Content("Error saving settings.");
+
+                return Content("Error saving settings.");
             }
 
 
@@ -258,6 +259,29 @@ public ActionResult SaveDownloads(SettingsModel data)
             return Content("Settings Saved.");
         }
 
+        [HttpPost]
+        public ActionResult SaveQuality(QualityModel data)
+        {
+            try
+            {
+                
+
+            }
+            catch (Exception e)
+            {
+                Logger.ErrorException(e.Message, e);
+                if (Request.IsAjaxRequest())
+                    return Content("Error Saving Settings, please fix any errors");
+
+                return Content("Error Saving Settings, please fix any errors");
+            }
+
+            if (Request.IsAjaxRequest())
+                return Content("Settings Saved.");
+
+            return Content("Settings Saved.");
+        }
+
         [HttpPost]
         public ActionResult SortedList(List<object > items)
         {
diff --git a/NzbDrone.Web/Views/Settings/Quality.ascx b/NzbDrone.Web/Views/Settings/Quality.ascx
index a9d61eda6..ce613ee65 100644
--- a/NzbDrone.Web/Views/Settings/Quality.ascx
+++ b/NzbDrone.Web/Views/Settings/Quality.ascx
@@ -49,7 +49,6 @@
 	.ui-state-highlight { height: 1.5em; line-height: 1.2em; }
 	</style>
 
-
     <script type="text/javascript">
         $(function () {
             $("#sortable").sortable({
@@ -59,7 +58,7 @@
         });
 	</script>
 
-    <% using (Html.BeginForm("SaveIndexers", "Settings", FormMethod.Post, new { id = "form", name = "form" }))
+    <% using (Html.BeginForm("SaveQuality", "Settings", FormMethod.Post, new { id = "form", name = "form" }))
        {%>
 <%: Html.ValidationSummary(true, "Unable to save your settings. Please correct the errors and try again.") %>