[banshee] [Dap] Warn the user if sync deletes more than 10 files (bgo#504492)



commit 05cca6da252dc3d32bc679fcc0c063fdc4b99473
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Fri Mar 5 10:02:04 2010 +1100

    [Dap] Warn the user if sync deletes more than 10 files (bgo#504492)
    
    Signed-off-by: Alexander Kojevnikov <alexander kojevnikov com>

 src/Dap/Banshee.Dap/Banshee.Dap/DapLibrarySync.cs |   20 ++++++++++
 src/Dap/Banshee.Dap/Banshee.Dap/DapSync.cs        |   41 ++++++++++++++++++++-
 2 files changed, 60 insertions(+), 1 deletions(-)
---
diff --git a/src/Dap/Banshee.Dap/Banshee.Dap/DapLibrarySync.cs b/src/Dap/Banshee.Dap/Banshee.Dap/DapLibrarySync.cs
index 15e6305..2930cfc 100644
--- a/src/Dap/Banshee.Dap/Banshee.Dap/DapLibrarySync.cs
+++ b/src/Dap/Banshee.Dap/Banshee.Dap/DapLibrarySync.cs
@@ -54,6 +54,7 @@ namespace Banshee.Dap
         private SchemaEntry<bool> enabled, sync_entire_library;
         private SchemaEntry<string> sync_source;
         private SmartPlaylistSource sync_src, to_add, to_remove;
+        private const int MAX_NOWARN_TRACKS_REMOVAL = 10;
 
         #region Public Properties
 
@@ -196,11 +197,20 @@ namespace Banshee.Dap
 
         internal void Sync ()
         {
+            Sync (false);
+        }
+
+        internal void Sync (bool force)
+        {
             if (Enabled) {
                 ThreadAssist.AssertNotInMainThread ();
 
                 CalculateSync ();
 
+                if (!force && to_remove.Count > MAX_NOWARN_TRACKS_REMOVAL) {
+                    throw new PossibleUserErrorException (to_remove.Count);
+                }
+
                 sync.Dap.DeleteAllTracks (to_remove);
                 sync.Dap.AddAllTracks (to_add);
 
@@ -245,5 +255,15 @@ namespace Banshee.Dap
             // Note to translators: {0}, {1} and {2} will be replaced with numbers.
             Catalog.GetString ("{0} to add, {1} to remove, {2} to update");
         }
+
+        internal class PossibleUserErrorException : ApplicationException {
+
+            internal int TracksToRemove { get; private set; }
+
+            public PossibleUserErrorException (int tracksToRemove) : base ()
+            {
+                TracksToRemove = tracksToRemove;
+            }
+        }
     }
 }
diff --git a/src/Dap/Banshee.Dap/Banshee.Dap/DapSync.cs b/src/Dap/Banshee.Dap/Banshee.Dap/DapSync.cs
index 3d0f95e..6e38d30 100644
--- a/src/Dap/Banshee.Dap/Banshee.Dap/DapSync.cs
+++ b/src/Dap/Banshee.Dap/Banshee.Dap/DapSync.cs
@@ -45,6 +45,10 @@ using Banshee.SmartPlaylist;
 using Banshee.Query;
 using Banshee.Preferences;
 
+using Banshee.Gui;
+using Hyena.Widgets;
+using Gtk;
+
 namespace Banshee.Dap
 {
     public sealed class DapSync : IDisposable
@@ -350,7 +354,42 @@ namespace Banshee.Dap
             }
 
             foreach (DapLibrarySync library_sync in library_syncs) {
-                library_sync.Sync ();
+                try {
+                    library_sync.Sync ();
+                } catch (DapLibrarySync.PossibleUserErrorException e) {
+
+                    string header = String.Format (
+                        Catalog.GetPluralString (
+                           // singular form unused b/c we know it's > 1, but we still need GetPlural
+                           "The sync operation will remove one track from your device.",
+                           "The sync operation will remove {0} tracks from your device.",
+                           e.TracksToRemove),
+                        e.TracksToRemove);
+                    string message = Catalog.GetString ("Are you sure you want to continue?");
+
+                    HigMessageDialog md = new HigMessageDialog (
+                        ServiceManager.Get<GtkElementsService> ("GtkElementsService").PrimaryWindow,
+                        DialogFlags.DestroyWithParent, MessageType.Warning,
+                        ButtonsType.None, header, message
+                    );
+                    md.AddButton ("gtk-cancel", ResponseType.No, true);
+                    md.AddButton (Catalog.GetString ("Remove tracks"), ResponseType.Yes, false);
+
+                    bool remove_tracks = false;
+                    ThreadAssist.BlockingProxyToMain (() => {
+                        try {
+                            if (md.Run () == (int) ResponseType.Yes) {
+                                remove_tracks = true;
+                            }
+                        } finally {
+                            md.Destroy ();
+                        }
+                    });
+
+                    if (remove_tracks) {
+                        library_sync.Sync (true);
+                    }
+                }
             }
 
             if (sync_playlists) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]