~iwarford/do-plugins/fart-plugin-fwiw

« back to all changes in this revision

Viewing changes to SqueezeCenter/src/RadioItem.cs

  • Committer: Jason Jones
  • Date: 2008-12-24 04:45:02 UTC
  • mfrom: (335.1.9 do-plugins)
  • Revision ID: jasonedwardjones@gmail.com-20081224044502-ra56ym06cp1iqs7t
Merged from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  This program is free software: you can redistribute it and/or modify
 
2
//  it under the terms of the GNU General Public License as published by
 
3
//  the Free Software Foundation, either version 3 of the License, or
 
4
//  (at your option) any later version.
 
5
//
 
6
//  This program is distributed in the hope that it will be useful,
 
7
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
//  GNU General Public License for more details.
 
10
//
 
11
//  You should have received a copy of the GNU General Public License
 
12
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
13
 
 
14
using System;
 
15
using System.Collections.Generic;
 
16
 
 
17
 
 
18
using Do.Universe;
 
19
 
 
20
namespace SqueezeCenter
 
21
{
 
22
 
 
23
        public abstract class RadioItem : Item
 
24
        {
 
25
                protected RadioSubItem[] children;
 
26
                                
 
27
                public virtual RadioSubItem[] Children
 
28
                {
 
29
                        get { return this.children; }
 
30
                        set 
 
31
                        {
 
32
                                System.Threading.Interlocked.Exchange<RadioSubItem[]> (ref this.children, value);                               
 
33
                        }
 
34
                }
 
35
                
 
36
                public abstract RadioItem Parent {get;}
 
37
                
 
38
                public override string Icon
 
39
                {
 
40
                        get { return "radio.png@" + this.GetType ().Assembly.FullName; }                        
 
41
                }               
 
42
        }
 
43
                
 
44
        public class RadioSuperItem : RadioItem
 
45
        {               
 
46
                readonly string cmd, name;
 
47
                bool childrenSet = false;
 
48
                
 
49
                public RadioSuperItem (string cmd, string name)
 
50
                {
 
51
                        this.cmd = cmd;
 
52
                        this.name = name;
 
53
                        this.children = new RadioSubItem[0];
 
54
                }
 
55
                
 
56
                public string Command {
 
57
                        get {                           
 
58
                                return this.cmd;
 
59
                        }
 
60
                }
 
61
                                                
 
62
                public override string Name {
 
63
                        get {
 
64
                                return this.name;
 
65
                        }
 
66
                }
 
67
                        
 
68
                public override string Description {
 
69
                        get {
 
70
                                return "Radio" ;
 
71
                        }
 
72
                }
 
73
                
 
74
                public override RadioSubItem[] Children {
 
75
                        get { return base.Children; }
 
76
                        set 
 
77
                        { 
 
78
                                base.Children = value;
 
79
                                this.childrenSet = true;
 
80
                        }
 
81
                }
 
82
 
 
83
                public bool IsLoadedRecursive
 
84
                {
 
85
                        get
 
86
                        {
 
87
                                if (!this.childrenSet)
 
88
                                        return false;
 
89
                                foreach (RadioSubItem rmi in this.children) {
 
90
                                        if (!rmi.IsLoadedRecursive)
 
91
                                                return false;
 
92
                                }
 
93
                                return true;
 
94
                        }
 
95
                }
 
96
 
 
97
                public override RadioItem Parent
 
98
                {
 
99
                        get { return null; }
 
100
                }
 
101
 
 
102
                public RadioSubItem[] GetChildrenRecursive () 
 
103
                {
 
104
                        List<RadioSubItem> result = new List<RadioSubItem> ();
 
105
                        foreach (RadioSubItem rmi in children) {
 
106
                                result.Add (rmi);
 
107
                                rmi.CopyChildrenRecursive (result);
 
108
                        }
 
109
                        return result.ToArray ();
 
110
                }
 
111
        }
 
112
        
 
113
        public class RadioSubItem : RadioItem
 
114
        {
 
115
                readonly int id;
 
116
                readonly RadioItem parent;
 
117
                readonly string name;
 
118
                readonly bool hasItems;
 
119
                                
 
120
                public RadioSubItem (RadioItem parent, int id, string name, bool hasItems)
 
121
                {
 
122
                        this.parent = parent;
 
123
                        this.id = id;
 
124
                        this.name = name;
 
125
                        this.hasItems = hasItems;
 
126
                        this.children = new RadioSubItem[0];
 
127
                }
 
128
                                                
 
129
                public override string Name {
 
130
                        get {
 
131
                                return this.name;
 
132
                        }
 
133
                }
 
134
                        
 
135
                public override string Description {
 
136
                        get {
 
137
                                RadioItem parent = this.parent;
 
138
                                string result = string.Empty;
 
139
                                while (parent != null) {
 
140
                                        result = parent.Name + (result.Length == 0 ? string.Empty : " → " + result);
 
141
                                        parent = parent.Parent;
 
142
                                }
 
143
                                return result;
 
144
                        }
 
145
                }
 
146
 
 
147
                public override RadioItem Parent
 
148
                {
 
149
                        get { return this.parent; }
 
150
                }
 
151
                
 
152
                public int Id
 
153
                {
 
154
                        get { return this.id; }
 
155
                }
 
156
 
 
157
                public bool HasItems
 
158
                {
 
159
                        get { return this.hasItems; }
 
160
                }
 
161
                
 
162
                public RadioSuperItem GetSuper ()
 
163
                {
 
164
                        RadioItem r = this;
 
165
                        while (r != null) {
 
166
                                if (r is RadioSuperItem)
 
167
                                        return r as RadioSuperItem;
 
168
                                r = r.Parent;
 
169
                        }
 
170
                        return null;
 
171
                }
 
172
 
 
173
                public string IdPath
 
174
                {
 
175
                        get {
 
176
                                if (! (this.parent is RadioSubItem)) {                                  
 
177
                                        return this.Id.ToString ();
 
178
                                }
 
179
                                return string.Format ("{0}.{1}", (this.parent as RadioSubItem).IdPath, this.Id);                                
 
180
                        }
 
181
                }
 
182
                
 
183
                public void CopyChildrenRecursive (List<RadioSubItem> target)
 
184
                {
 
185
                        foreach (RadioSubItem rmi in children) {
 
186
                                target.Add (rmi);
 
187
                                rmi.CopyChildrenRecursive (target);
 
188
                        }
 
189
                }
 
190
                
 
191
                public bool IsLoadedRecursive
 
192
                {
 
193
                        get
 
194
                        {
 
195
                                if (this.hasItems) {                                    
 
196
                                        foreach (RadioSubItem rmi in this.children) {
 
197
                                                if (!rmi.IsLoadedRecursive) {
 
198
                                                        return false;
 
199
                                                }
 
200
                                        }
 
201
                                }
 
202
                                return true;
 
203
                        }
 
204
                }
 
205
        }
 
206
}