summaryrefslogtreecommitdiff
path: root/src/FolderSync/Banshee.FolderSync/FolderSyncController.cs
blob: 9673403201d95d10ed4a2e5ebbb611f2d5b7a930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// Copyright (c) 2011 Timo Dörr <timo.doerr@latecrew.de>
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

using Mono.Addins;
using Mono.Unix;

using Hyena;
using Hyena.Data.Sqlite;
using Banshee.Gui;
using Banshee.ServiceStack;
using Banshee.Sources;
using Banshee.Sources.Gui;

namespace Banshee.FolderSync
{
    public class FolderSyncAction : BansheeActionGroup
    {
        private FolderSyncSource source = null;

        public FolderSyncAction () : base (AddinManager.CurrentLocalizer.GetString ("Sync to Folder"))
        {
            Add (new Gtk.ActionEntry ("FolderSyncAction", Gtk.Stock.Refresh,
                AddinManager.CurrentLocalizer.GetString ("Synchronize to Folder"), null, null, onLoadSync));
            AddUiFromFile ("MenuUI.xml");
        }

        public void onLoadSync (object o, EventArgs args)
        {
            source = new FolderSyncSource ();
            ServiceManager.SourceManager.MusicLibrary.AddChildSource (source);
            ServiceManager.SourceManager.SetActiveSource (source);
        }
    }

    public class FolderSyncController : ISourceContents
    {
        public FolderSyncView View = new FolderSyncView ();
        private Thread syncThread;

#region ISourceContents implementation
        public bool SetSource (ISource source)
        {
            return true;
        }

        public void ResetSource ()
        {
            Reload ();
        }

        public Gtk.Widget Widget {
            get {
                return View;
            }
        }

        public ISource Source { get { return null; } }
#endregion

        public FolderSyncController ()
        {
            //hookup click event
            View.StartSyncButton.Clicked += onStartSync;
            Reload ();
        }

        public void Reload ()
        {
            // fill the database-stored playlist into the treeview
            var playlists = Playlist.GetAllPlaylists ();
            // remove older playlist entries
            View.PlaylistStore.Clear ();
            foreach (var playlist in playlists) {
                View.PlaylistStore.AppendValues (playlist.ID, playlist.Name);
            }
            View.Reload ();
        }

        public void StopSync ()
        {
            if (syncThread != null && syncThread.IsAlive) {
                Hyena.Log.Debug ("aborting sync thread due to user request");
                syncThread.Abort ();
                Reload ();
            }
        }

        private void onStartSync (object o, EventArgs args)
        {
            Hyena.Log.Debug ("Start of Sync triggered!");

            var options = View.GetOptions ();

            // target directory to copy to
            Hyena.Log.DebugFormat ("Target folder is set to: {0}", options.TargetFolder);

            // count all files for progress bar
            int totalFiles = 0;
            foreach (var playlist in options.SelectedPlaylists) {
                totalFiles += playlist.Tracks.Count ();
            }
            View.Progress.Text = AddinManager.CurrentLocalizer.GetString ("Preparing sync");
            var progress_step = 1f / totalFiles;
            var current_progress = 0f;

            // begin sync worker thread
            ThreadStart syncStart = delegate()
            {
                Hyena.Log.Debug ("Sync thread started!");
                // foreach playlist
                foreach (var playlist in options.SelectedPlaylists) {

                    Stream m3u_stream = null;
                    StreamWriter m3u_writer = null;

                    if (options.CreateM3u) {
                        var m3u_fileUri = new StringBuilder ().Append (options.TargetFolder)
                         .Append (Path.DirectorySeparatorChar).Append (playlist.Name)
                         .Append (".m3u").ToString ();

                        m3u_stream = Banshee.IO.File.OpenWrite (new SafeUri (m3u_fileUri), true);
                        Log.DebugFormat ("opened m3u playlist for writing: {0}", m3u_fileUri);
                        m3u_writer = new StreamWriter (m3u_stream, System.Text.Encoding.UTF8);
                    }

                    // for each contained file
                    foreach (var track in playlist.Tracks) {
                        // get filename part of path
                        var dest_path_suffix = track.GetFilepathSuffix (options.SubfolderDepth);

                        // we dont want %20 etc. in the m3u since some android devices delete
                        // playlists with that encoding in it (i.e. galaxy S)
                        Hyena.Log.DebugFormat ("filename for m3u file is {0}", dest_path_suffix);

                        // assemble new Uri of target track
                        var destUri = new SafeUri (new StringBuilder ().Append (options.TargetFolder)
                            .Append (Path.DirectorySeparatorChar)
                            .Append (dest_path_suffix).ToString ());

                        // create subfolders if necessary
                        string dest_path = options.TargetFolder;
                        var folders = track.GetSubdirectories (options.SubfolderDepth);
                        try {
                            for (int i=0; i < folders.Count (); i++) {
                                dest_path += folders [i] + "/";
                                Hyena.Log.DebugFormat ("creating folder {0}", dest_path);
                                if (!Banshee.IO.Directory.Exists (dest_path))
                                    Banshee.IO.Directory.Create (new SafeUri (dest_path));
                            }
                        } catch {
                            // folder creation failed, this is fatal, stop
                            // TODO display a error popup
                            break;
                        }

                        // copy file to selected folder
                        try {
                            var destExists = Banshee.IO.File.Exists (destUri);

                            if (options.OverwriteExisting || !destExists) {
                                if (options.CreateSymLinks) {
                                    var trackFilePath = SafeUri.UriToFilename (track.Uri);
                                    var destFilePath = SafeUri.UriToFilename (destUri);

                                    if (track.Uri.IsLocalPath) {
                                        var target = new UnixFileInfo (trackFilePath);

                                        // symbolic links need manual deletion,
                                        // otherwise an exception is thrown in CreateSymbolicLink()
                                        if (destExists) {
                                            var dest = new UnixFileInfo (destFilePath);
                                            dest.Delete ();
                                        }

                                        target.CreateSymbolicLink (destFilePath);
                                    } else {
                                        Hyena.Log.ErrorFormat ("Cannot create symlink to remote path {0} in {1}, skipping", track.Uri, destFilePath);
                                    }
                                } else {
                                    Banshee.IO.File.Copy (track.Uri, destUri, true);
                                }
                                Hyena.Log.DebugFormat ("Copying {0} to {1}", track.Uri, destUri);
                            } else
                                Hyena.Log.Debug ("Not overwriting existing file {0}", destUri);
                        } catch {
                            Hyena.Log.ErrorFormat ("Error copying file {0} to {1}, skipping", track.Uri, destUri);
                        }

                        // increment the progressbar
                        current_progress += progress_step;
                        if (current_progress > 1.0f)
                            current_progress = 1.0f;

                        Gtk.Application.Invoke (delegate {
                            View.Progress.Fraction = current_progress;
                            View.Progress.Text = AddinManager.CurrentLocalizer.GetString ("Copying") + " " + track.Filepath;
                            Gtk.Main.IterationDo (false);
                        });

                        if (options.CreateM3u) {
                            m3u_writer.Write (track.CreateM3uEntry (options.SubfolderDepth));
                        }
                    }
                    // close the m3u file before processing next playlist
                    if (options.CreateM3u) {
                        Hyena.Log.Debug ("closing m3u filedescriptor");
                        m3u_writer.Close ();
                        m3u_writer.Dispose ();
                    }
                    Hyena.Log.Debug ("sync process finished");
                }

                Gtk.Application.Invoke (delegate {
                    View.Progress.Text = AddinManager.CurrentLocalizer.GetString ("Done!");
                    View.Progress.Fraction = 1f;
                    Gtk.Main.IterationDo (false);
                });
                Hyena.Log.Debug ("sync DONE, returning");
                return;
            };
            // end of sync worker thread

            syncThread = new Thread (syncStart);
            syncThread.Start ();
            return;
        }
    }

    // small helper classes
    public class Track
    {
        public uint ID;
        public SafeUri Uri;

        public string Filepath {
            get {
                return SafeUri.UriToFilename (Uri);
            }
        }

        public string Filename {
            get {
                return Filepath.Split (Path.DirectorySeparatorChar).Last ();
            }
        }

        public Track ()
        { }

        public string CreateM3uEntry (uint subfolder_depth)
        {
            // TODO add #EXTIF additional metadata
            return GetFilepathSuffix (subfolder_depth) + "\n";
        }

        public string GetFilepathSuffix (uint subfolder_depth)
        {
            if (subfolder_depth == 0)
                return Filename;

            string path = "";
            foreach (var folder in GetSubdirectories (subfolder_depth))
                path += folder + "/";

            return path + Filename;
        }

        public List<string> GetSubdirectories (uint level)
        {
            var folders = this.Filepath.Split (Path.DirectorySeparatorChar).ToList ();

            // drop last segment as its not a folder but the filename
            folders.RemoveAt (folders.Count - 1);
            int skip = (int)(folders.Count - level);
            if (skip < 0)
                skip = 0;

            return folders.Skip (skip).ToList ();
        }
    }

    public class Playlist
    {
        public uint ID;
        public string Name;

        public Playlist ()
        { }

        public Playlist (uint id)
        {
            HyenaDataReader reader = new HyenaDataReader (ServiceManager.DbConnection.Query
                ("SELECT PlaylistID, Name from CorePlaylists WHERE PlaylistID = ?", id));

            if (!reader.Read ())
                throw new Exception ("No such playlist!");

            ID = reader.Get<uint> (0);
            Name = reader.Get<string> (1);
        }

        public List<Track> Tracks {
            get {
                return GetAllTracks (this.ID);
            }
        }

        public static List<Playlist> GetAllPlaylists ()
        {
            // get all the Uri from database
            HyenaDataReader reader = new HyenaDataReader (ServiceManager.DbConnection.Query
                ("SELECT PlaylistID, Name from CorePlaylists"));

            var playlists = new List<Playlist> ();

            while (reader.Read()) {
                playlists.Add (new Playlist {
                    ID = reader.Get<uint> (0),
                    Name = reader.Get<string> (1)
                });
            }
            return playlists;
        }

        // returns filenames of all included files in playlist
        public static List<Track> GetAllTracks (uint playlist_id)
        {
            HyenaDataReader reader = new HyenaDataReader (ServiceManager.DbConnection.Query (
                "SELECT t.TrackID, t.Uri from CorePlaylistEntries e JOIN CoreTracks t ON(e.TrackID = t.TrackID) " +
                "WHERE e.PlaylistID = ?", playlist_id));

            // package the Uris into a List for convenience
            List<Track > tracks = new List<Track> ();
            while (reader.Read()) {
                tracks.Add (new Track () {
                    ID = reader.Get<uint> (0),
                    Uri = new SafeUri(reader.Get<string> (1))
                });
            }
            return tracks;
        }
    }
}