~and471/+junk/do-with-docky

« back to all changes in this revision

Viewing changes to Do.Interface.Linux.Docky/src/Docky.Core/DockServices.cs

  • Committer: rugby471 at gmail
  • Date: 2010-10-15 16:08:38 UTC
  • Revision ID: rugby471@gmail.com-20101015160838-z9m3utbf7bxzb5ty
reverted to before docky removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// DockServices.cs
 
2
// 
 
3
// Copyright (C) 2009 GNOME Do
 
4
//
 
5
// This program is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
 
 
23
namespace Docky.Core
 
24
{
 
25
        
 
26
        
 
27
        public static class DockServices
 
28
        {
 
29
                static List<IDockService> services = new List<IDockService> ();
 
30
                
 
31
                static IItemsService items_service;
 
32
                static IDrawingService drawing_service;
 
33
                static IDoInteropService do_interop_service;
 
34
                static IPainterService painter_service;
 
35
                static IDockletService docklet_service;
 
36
                
 
37
                public static IDockletService DockletService {
 
38
                        get {
 
39
                                if (docklet_service == null)
 
40
                                        docklet_service = new Default.DockletService () as IDockletService;
 
41
                                return docklet_service;
 
42
                        }
 
43
                }
 
44
                
 
45
                public static IItemsService ItemsService {
 
46
                        get { 
 
47
                                if (items_service == null)
 
48
                                        items_service = new Default.ItemsService () as IItemsService;
 
49
                                return items_service;
 
50
                        }
 
51
                }
 
52
 
 
53
                public static IDrawingService DrawingService {
 
54
                        get { 
 
55
                                if (drawing_service == null)
 
56
                                        drawing_service = LoadService<IDrawingService, Default.DrawingService> ();
 
57
                                return drawing_service; 
 
58
                        }
 
59
                }
 
60
 
 
61
                public static IDoInteropService DoInteropService {
 
62
                        get { 
 
63
                                if (do_interop_service == null)
 
64
                                        do_interop_service = LoadService<IDoInteropService, Default.DoInteropService> ();
 
65
                                return do_interop_service;
 
66
                        }
 
67
                }
 
68
 
 
69
                public static IPainterService PainterService {
 
70
                        get {
 
71
                                if (painter_service == null)
 
72
                                        painter_service = LoadService<IPainterService, Default.PainterService> ();
 
73
                                return painter_service; 
 
74
                        }
 
75
                }
 
76
                
 
77
                public static void Clean ()
 
78
                {
 
79
                        if (items_service != null) {
 
80
                                items_service.Dispose ();
 
81
                                items_service = null;
 
82
                        }
 
83
                        
 
84
                        if (drawing_service != null) {
 
85
                                drawing_service.Dispose ();
 
86
                                drawing_service = null;
 
87
                        }
 
88
                        
 
89
                        if (painter_service != null) {
 
90
                                painter_service.Dispose ();
 
91
                                painter_service = null;
 
92
                        }
 
93
                        
 
94
                        if (docklet_service != null) {
 
95
                                docklet_service.Dispose ();
 
96
                                docklet_service = null;
 
97
                        }
 
98
                        
 
99
                        if (do_interop_service != null) {
 
100
                                do_interop_service.Dispose ();
 
101
                                do_interop_service = null;
 
102
                        }
 
103
                        
 
104
                        services.Clear ();
 
105
                }
 
106
 
 
107
                public static void RegisterService (IDockService service)
 
108
                {
 
109
                        services.Add (service);
 
110
                }
 
111
                
 
112
                public static IDockService UnregisterService (IDockService service)
 
113
                {
 
114
                        if (!services.Contains (service))
 
115
                                return service;
 
116
                        
 
117
                        if (do_interop_service == service)
 
118
                                do_interop_service = null;
 
119
                        
 
120
                        if (painter_service == service)
 
121
                                painter_service = null;
 
122
                        
 
123
                        if (drawing_service == service)
 
124
                                drawing_service = null;
 
125
                        
 
126
                        if (items_service == service)
 
127
                                items_service = null;
 
128
                        
 
129
                        if (docklet_service == service)
 
130
                                docklet_service = null;
 
131
                        
 
132
                        services.Remove (service);
 
133
                        
 
134
                        return service;
 
135
                }
 
136
 
 
137
                static TService LoadService<TService, TElse> ()
 
138
                        where TService : class, IDockService
 
139
                        where TElse : TService
 
140
                {
 
141
                        if (services.OfType<TService> ().Any ()) {
 
142
                                return services.OfType<TService> ().First () as TService;
 
143
                        } else {
 
144
                                return Activator.CreateInstance<TElse> () as TService;
 
145
                        }
 
146
                }
 
147
        }
 
148
}