File: DownloadUserJob.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 (144 lines) | stat: -rw-r--r-- 4,891 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
/***************************************************************************
 *  DownloadUserJob.cs
 *
 *  Copyright (C) 2007 Michael C. Urbanski
 *  Written by Mike Urbanski <michael.c.urbanski@gmail.com>
 ****************************************************************************/

/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
 *
 *  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 Gtk;
using Mono.Unix;

using Banshee.ServiceStack;

namespace Banshee.Podcasting.Gui
{
    public class DownloadUserJob : UserJob
    {
        private bool disposed = false;
        private bool canceled = false;
        private bool cancelRequested = false;

        private readonly object sync = new object ();

        public DownloadUserJob () : base (Catalog.GetString ("Downloads"), String.Empty, String.Empty)
        {
            CancelRequested += OnCancelRequested;

            Title = Catalog.GetString ("Downloading Podcast(s)");
            Status = Catalog.GetString ("Initializing...");
            CancelMessage = Catalog.GetString ("Cancel all podcast downloads?");

            this.IconNames = new string[1] {
                Stock.Network
            };

            CanCancel = true;
        }

        public void Dispose ()
        {
            lock (sync) {
                if (disposed) {
                    throw new ObjectDisposedException (GetType ().FullName);
                } else if (cancelRequested) {
                    throw new InvalidOperationException ("Cannot dispose object while canceling.");
                } else {
                    disposed = true;
                }

                CancelRequested -= OnCancelRequested;
            }
        }

        private bool SetCanceled ()
        {
            bool ret = false;

            lock (sync) {
                if (!cancelRequested && !canceled && !disposed) {
                    CanCancel = false;
                    ret = cancelRequested = true;
                }
            }

            return ret;
        }

        public void UpdateProgress (int progress)
        {
            if (progress < 0 || progress > 100) {
                throw new ArgumentException ("progress:  Must be between 0 and 100.");
            }

            lock (sync) {
                if (canceled || cancelRequested || disposed) {
                    return;
                }

                Progress = (double) progress / 100;
            }
        }

        public void UpdateStatus (int downloading, int remaining, int completed, long bytesPerSecond)
        {
            if (downloading < 0) {
                throw new ArgumentException ("downloading:  Must be positive.");
            } else if (bytesPerSecond < 0) {
                bytesPerSecond = 0;
            }

            lock (sync) {
                if (canceled || cancelRequested || disposed) {
                    return;
                }

                int total = remaining + completed;
                string fmt = Catalog.GetPluralString (
                        "Transferring {0} file at {1} KB/s",
                        "Transferring {0} of {2} files at {1} KB/s", total
                );

                Status = String.Format (fmt, downloading, (bytesPerSecond / 1024), total);
            }
        }

        private void OnCancelRequested (object sender, EventArgs e)
        {
            if (SetCanceled ()) {
                lock (sync)  {
                    Progress = 0.0;
                    Title = Catalog.GetString ("Canceling Downloads");
                    Status = Catalog.GetString (
                        "Waiting for downloads to terminate..."
                    );

                    cancelRequested = false;
                    canceled = true;
                }
            }
        }
    }
}