From 8f8abc26ff74c1d25a85b38b04a5f9051834f8fd Mon Sep 17 00:00:00 2001
From: regeszter <regeszter@freemail.hu>
Date: Wed, 21 Aug 2013 15:06:27 +0200
Subject: [PATCH] Rework of quota management to support unc path

---
 .../SetupTv/Sections/TvRecording.Designer.cs       |  1 +
 .../TVLibrary/SetupTv/Sections/TvRecording.cs      | 40 ++++++++++++++---
 TvEngine3/TVLibrary/SetupTv/Util.cs                | 52 +++++++++++++++++++++-
 .../TvService/DiskManagement/DiskManagement.cs     | 25 +++++++++--
 4 files changed, 108 insertions(+), 10 deletions(-)

diff --git a/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.Designer.cs b/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.Designer.cs
index 8185b5f..a854b27 100644
--- a/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.Designer.cs
+++ b/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.Designer.cs
@@ -895,6 +895,7 @@ namespace SetupTv.Sections
       this.enableDiskQuota.TabIndex = 21;
       this.enableDiskQuota.Text = "Enable disk quota";
       this.enableDiskQuota.UseVisualStyleBackColor = true;
+      this.enableDiskQuota.CheckedChanged += new System.EventHandler(this.enableDiskQuota_CheckedChanged);
       // 
       // mpNumericTextBoxDiskQuota
       // 
diff --git a/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.cs b/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.cs
index 78f20fe..fe6cec4 100644
--- a/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.cs
+++ b/TvEngine3/TVLibrary/SetupTv/Sections/TvRecording.cs
@@ -657,26 +657,48 @@ namespace SetupTv.Sections
         return;
       string drive = (string)comboBoxDrive.SelectedItem;
       ulong freeSpace = Utils.GetFreeDiskSpace(drive);
-      long totalSpace = Utils.GetDiskSize(drive);
+      ulong totalSpace = Utils.GetDiskSpace(drive);
 
       labelFreeDiskspace.Text = Utils.GetSize((long)freeSpace);
-      labelTotalDiskSpace.Text = Utils.GetSize(totalSpace);
+      labelTotalDiskSpace.Text = Utils.GetSize((long)totalSpace);
       if (labelTotalDiskSpace.Text == "0")
         labelTotalDiskSpace.Text = "Not available - WMI service not available";
+      
+      Setting setting;
+      
       if (save)
       {
         TvBusinessLayer layer = new TvBusinessLayer();
-        Setting setting = layer.GetSetting("freediskspace" + drive[0]);
+        
+        if (drive.StartsWith(@"\"))
+        {
+          setting = layer.GetSetting("freediskspace" + drive);
+        }
+        else
+        {
+          setting = layer.GetSetting("freediskspace" + drive[0]);
+        }
+                
         if (mpNumericTextBoxDiskQuota.Value < 500)
           mpNumericTextBoxDiskQuota.Value = 500;
         long quota = mpNumericTextBoxDiskQuota.Value * 1024;
         setting.Value = quota.ToString();
         setting.Persist();
+        Log.Debug("SetupTV: saved quota  for {0} is {1}", drive, setting.Value);
       }
       else
       {
         TvBusinessLayer layer = new TvBusinessLayer();
-        Setting setting = layer.GetSetting("freediskspace" + drive[0]);
+
+        if (drive.StartsWith(@"\"))
+        {
+          setting = layer.GetSetting("freediskspace" + drive);
+        }
+        else
+        {
+          setting = layer.GetSetting("freediskspace" + drive[0]);
+        }
+
         try
         {
           long quota = Int64.Parse(setting.Value);
@@ -730,7 +752,15 @@ namespace SetupTv.Sections
         if (card.RecordingFolder.Length > 0)
         {
           string driveLetter = String.Format("{0}:", card.RecordingFolder[0]);
-          if (Utils.getDriveType(driveLetter) == 3)
+          
+          if (card.RecordingFolder.StartsWith(@"\"))
+          {
+            if (!comboBoxDrive.Items.Contains(driveLetter))
+            {
+              comboBoxDrive.Items.Add(card.RecordingFolder);
+            }
+          }
+          else if (Utils.getDriveType(driveLetter) == 3)
           {
             if (!comboBoxDrive.Items.Contains(driveLetter))
             {
diff --git a/TvEngine3/TVLibrary/SetupTv/Util.cs b/TvEngine3/TVLibrary/SetupTv/Util.cs
index 48d14eb..6b360be 100644
--- a/TvEngine3/TVLibrary/SetupTv/Util.cs
+++ b/TvEngine3/TVLibrary/SetupTv/Util.cs
@@ -748,10 +748,30 @@ namespace SetupTv
       return ReplaceTag(line, tag, value, string.Empty);
     }
 
+    public static ulong GetDiskSpace(string drive)
+    {
+      if (drive.StartsWith(@"\"))
+      {
+        return GetShareSpace(drive);
+      }
+      ulong freeBytesAvailable = 0;
+      ulong totalNumberOfBytes = 0;
+      ulong totalNumberOfFreeBytes = 0;
+
+      GetDiskFreeSpaceEx(
+        drive[0] + @":\",
+        out freeBytesAvailable,
+        out totalNumberOfBytes,
+        out totalNumberOfFreeBytes);
+      return totalNumberOfBytes;
+    }
+    
     public static ulong GetFreeDiskSpace(string drive)
     {
-      if (drive == null)
-        return 0;
+      if (drive.StartsWith(@"\"))
+      {
+        return GetFreeShareSpace(drive);
+      }
       ulong freeBytesAvailable;
       ulong totalNumberOfBytes;
       ulong totalNumberOfFreeBytes;
@@ -764,6 +784,34 @@ namespace SetupTv
       return freeBytesAvailable;
     }
 
+    public static ulong GetFreeShareSpace(string UNCPath)
+    {
+      ulong freeBytesAvailable = 0;
+      ulong totalNumberOfBytes = 0;
+      ulong totalNumberOfFreeBytes = 0;
+
+      GetDiskFreeSpaceEx(
+        System.IO.Path.GetPathRoot(UNCPath),
+        out freeBytesAvailable,
+        out totalNumberOfBytes,
+        out totalNumberOfFreeBytes);
+      return freeBytesAvailable;
+    }
+
+    public static ulong GetShareSpace(string UNCPath)
+    {
+      ulong freeBytesAvailable = 0;
+      ulong totalNumberOfBytes = 0;
+      ulong totalNumberOfFreeBytes = 0;
+
+      GetDiskFreeSpaceEx(
+        System.IO.Path.GetPathRoot(UNCPath),
+        out freeBytesAvailable,
+        out totalNumberOfBytes,
+        out totalNumberOfFreeBytes);
+      return totalNumberOfBytes;
+    }
+
     public static bool HibernateSystem(bool forceShutDown)
     {
       return (SetSuspendState(PowerState.Hibernate, forceShutDown));
diff --git a/TvEngine3/TVLibrary/TvService/DiskManagement/DiskManagement.cs b/TvEngine3/TVLibrary/TvService/DiskManagement/DiskManagement.cs
index 2ae47a9..d6f9051 100644
--- a/TvEngine3/TVLibrary/TvService/DiskManagement/DiskManagement.cs
+++ b/TvEngine3/TVLibrary/TvService/DiskManagement/DiskManagement.cs
@@ -53,11 +53,21 @@ namespace TvService
         if (card.RecordingFolder.Length > 0)
         {
           string driveLetter = String.Format("{0}:", card.RecordingFolder[0]);
-          if (Utils.getDriveType(driveLetter) == 3)
+
+          if (card.RecordingFolder.StartsWith(@"\"))
+          {
+            if (!drives.Contains(driveLetter))
+            {
+              drives.Add(card.RecordingFolder);
+              Log.Debug("Quota GetDisks: {0}", card.RecordingFolder);
+            }
+          } 
+          else if (Utils.getDriveType(driveLetter) == 3)
           {
             if (!drives.Contains(driveLetter))
             {
               drives.Add(driveLetter);
+              Log.Debug("Quota GetDisks: {0}", driveLetter);
             }
           }
         }
@@ -104,11 +114,20 @@ namespace TvService
       TvBusinessLayer layer = new TvBusinessLayer();
 
       ulong minimiumFreeDiskSpace;
-
-      string quotaText = layer.GetSetting("freediskspace" + drive[0], "51200").Value;
+      string quotaText;
+
+      if (drive.StartsWith(@"\"))
+       {
+         quotaText = layer.GetSetting("freediskspace" + drive, "51200").Value;
+       }
+      else
+       {
+         quotaText = layer.GetSetting("freediskspace" + drive[0], "51200").Value;
+       }
       try
       {
         minimiumFreeDiskSpace = (ulong)Int32.Parse(quotaText);
+        Log.Debug("DiskManagement: minimiumFreeDiskSpace for {0} is {1}", drive, quotaText);
       }
       catch (Exception e)
       {
-- 
1.8.1.msysgit.1

