~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.Setup/Mono.Addins.Setup.ProgressMonitoring/ProgressTracker.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ProgressTracker.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.Collections;
 
32
 
 
33
namespace Mono.Addins.Setup.ProgressMonitoring
 
34
{
 
35
        internal class ProgressTracker
 
36
        {
 
37
                bool done;
 
38
                
 
39
                ArrayList tasks = new ArrayList ();
 
40
                class Task
 
41
                {
 
42
                        public string Name;
 
43
                        public int TotalWork;
 
44
                        public int CurrentWork;
 
45
                        public int StepSize = 1;
 
46
                        public bool IsStep;
 
47
                        
 
48
                        public double GetWorkPercent (double part)
 
49
                        {
 
50
                                if (TotalWork <= 0) return 0;
 
51
                                if (CurrentWork >= TotalWork) return 1.0;
 
52
                                return ((double)CurrentWork + part) / (double)TotalWork;
 
53
                        }
 
54
                }
 
55
                
 
56
                public void Reset ()
 
57
                {
 
58
                        done = false;
 
59
                        tasks.Clear ();
 
60
                }
 
61
                
 
62
                public void BeginTask (string name, int totalWork)
 
63
                {
 
64
                        Task t = new Task (); 
 
65
                        t.Name = name;
 
66
                        t.TotalWork = totalWork;
 
67
                        tasks.Add (t);
 
68
                }
 
69
                
 
70
                public void BeginStepTask (string name, int totalWork, int stepSize)
 
71
                {
 
72
                        Task t = new Task (); 
 
73
                        t.StepSize = stepSize;
 
74
                        t.IsStep = true;
 
75
                        t.Name = name;
 
76
                        t.TotalWork = totalWork;
 
77
                        tasks.Add (t);
 
78
                }
 
79
                
 
80
                public void EndTask ()
 
81
                {
 
82
                        if (tasks.Count > 0) {
 
83
                                Task t = LastTask;
 
84
                                tasks.RemoveAt (tasks.Count - 1);
 
85
                                if (t.IsStep)
 
86
                                        Step (t.StepSize);
 
87
                        }
 
88
                }
 
89
                
 
90
                public void Step (int work)
 
91
                {
 
92
                        if (tasks.Count == 0) return;
 
93
                        Task t = LastTask;
 
94
                        t.CurrentWork += work;
 
95
                        if (t.CurrentWork > t.TotalWork)
 
96
                                t.CurrentWork = t.TotalWork;
 
97
                }
 
98
                
 
99
                Task LastTask {
 
100
                        get { return (Task)tasks [tasks.Count-1]; }
 
101
                }
 
102
                
 
103
                public string CurrentTask {
 
104
                        get {
 
105
                                if (tasks.Count == 0) return null;
 
106
                                return LastTask.Name;
 
107
                        }
 
108
                }
 
109
                
 
110
                public double CurrentTaskWork {
 
111
                        get {
 
112
                                if (tasks.Count == 0) return 0;
 
113
                                return LastTask.GetWorkPercent (0);
 
114
                        }
 
115
                }
 
116
                
 
117
                public bool UnknownWork {
 
118
                        get {
 
119
                                if (tasks.Count == 0) return false;
 
120
                                return LastTask.TotalWork <= 1;
 
121
                        }
 
122
                }
 
123
                
 
124
                public double GlobalWork {
 
125
                        get {
 
126
                                if (done) return 1.0;
 
127
 
 
128
                                double work = 0;
 
129
                                for (int n = tasks.Count - 1; n >= 0; n--) {
 
130
                                        Task t = (Task) tasks [n];
 
131
                                        work = t.GetWorkPercent (work) * (double)t.StepSize;
 
132
                                }
 
133
                                return work;
 
134
                        }
 
135
                }
 
136
                
 
137
                public bool InProgress {
 
138
                        get { return !done; }
 
139
                }
 
140
                
 
141
                public void Done ()
 
142
                {
 
143
                        done = true;
 
144
                        tasks.Clear ();
 
145
                }
 
146
        }
 
147
}