File: ThreadPoolImportSource.cs

package info (click to toggle)
banshee 2.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,208 kB
  • sloc: xml: 163,694; cs: 137,409; sh: 11,650; ansic: 4,790; makefile: 2,922; python: 38
file content (170 lines) | stat: -rw-r--r-- 5,129 bytes parent folder | download | duplicates (5)
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
//
// ThreadPoolImportSource.cs
//
// Author:
//   Scott Peterson <lunchtimemama@gmail.com>
//
// Copyright (C) 2008 Scott Peterson
//
// 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.IO;
using System.Threading;

using Mono.Unix;

using Hyena;
using Hyena.Jobs;

using Banshee.ServiceStack;
using Banshee.Sources;

namespace Banshee.Library
{
    public abstract class ThreadPoolImportSource : IImportSource
    {
        private bool importing;
        private UserJob user_job;
        private readonly object user_job_mutex = new object ();

        private void CreateUserJob ()
        {
            lock (user_job_mutex) {
                if(user_job != null) {
                    return;
                }

                user_job = new UserJob (UserJobTitle, UserJobTitle, Catalog.GetString ("Importing Songs"));
                user_job.SetResources (Resource.Cpu, Resource.Disk, Resource.Database);
                user_job.PriorityHints = PriorityHints.SpeedSensitive | PriorityHints.DataLossIfStopped;
                user_job.IconNames = IconNames;
                user_job.CancelMessage = CancelMessage;
                user_job.CanCancel = CanCancel;
                user_job.Register ();
            }
        }

        private void DestroyUserJob ()
        {
            lock(user_job_mutex) {
                if(user_job == null) {
                    return;
                }

                user_job.Finish ();
                user_job = null;
            }
        }

        protected void UpdateUserJob (int processed, int count, string artist, string title)
        {
            user_job.Title = String.Format(
                Catalog.GetString("Importing {0} of {1}"),
                processed, count);
            user_job.Status = String.Format("{0} - {1}", artist, title);
            user_job.Progress = processed / (double)count;
        }

        protected void LogError (string path, Exception e)
        {
            LogError (path, e.Message);
        }

        protected void LogError (string path, string msg)
        {
            ThreadAssist.ProxyToMain (delegate {
                ErrorSource error_source = ServiceManager.SourceManager.MusicLibrary.ErrorSource;
                error_source.AddMessage (path, msg);

                Log.Error (path, msg, false);
            });
        }

        protected bool CheckForCanceled ()
        {
            lock(user_job_mutex) {
                return user_job != null && user_job.IsCancelRequested;
            }
        }

        private string user_job_title;
        protected virtual string UserJobTitle {
            get {
                if (user_job_title == null) {
                    user_job_title = String.Format (Catalog.GetString ("Importing From {0}"), Name);
                }
                return user_job_title;
            }
        }

        private string cancel_message = Catalog.GetString ("The import process is currently running. Would you like to stop it?");
        protected virtual string CancelMessage {
            get { return cancel_message; }
        }

        protected virtual bool CanCancel {
            get { return true; }
        }

#region IImportSource

        public abstract string Name { get; }

        public virtual string ImportLabel { get { return null; } }

        public abstract string[] IconNames { get; }

        public abstract int SortOrder { get; }

        public virtual bool CanImport {
            get { return true; }
        }

        public void Import ()
        {
            if (importing || !ConfirmImport ()) {
                return;
            }

            importing = true;
            CreateUserJob ();
            ThreadPool.QueueUserWorkItem (ImportCore);
            importing = false;
        }

#endregion

        private void ImportCore (object o)
        {
            ImportCore ();
            DestroyUserJob ();
        }

        protected virtual bool ConfirmImport ()
        {
            return true;
        }

        protected abstract void ImportCore ();

    }
}