~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/Repository.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
// Repository.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
using System;
 
30
using System.Collections;
 
31
using System.Xml;
 
32
using System.Xml.Serialization;
 
33
using System.IO;
 
34
using System.Net;
 
35
using System.Threading;
 
36
 
 
37
namespace Mono.Addins.Setup
 
38
{
 
39
        internal class Repository
 
40
        {
 
41
                RepositoryEntryCollection repositories;
 
42
                RepositoryEntryCollection addins;
 
43
                string name;
 
44
                internal string url;
 
45
                
 
46
                public string Name {
 
47
                        get { return name; }
 
48
                        set { name = value; }
 
49
                }
 
50
                
 
51
                public string Url {
 
52
                        get { return url; }
 
53
                        set { url = value; }
 
54
                }
 
55
                
 
56
                internal string CachedFilesDir { get; set; }
 
57
        
 
58
                [XmlElement ("Repository", Type = typeof(ReferenceRepositoryEntry))]
 
59
                public RepositoryEntryCollection Repositories {
 
60
                        get {
 
61
                                if (repositories == null)       
 
62
                                        repositories = new RepositoryEntryCollection (this);
 
63
                                return repositories;
 
64
                        }
 
65
                }
 
66
        
 
67
                [XmlElement ("Addin", Type = typeof(PackageRepositoryEntry))]
 
68
                public RepositoryEntryCollection Addins {
 
69
                        get {
 
70
                                if (addins == null)
 
71
                                        addins = new RepositoryEntryCollection (this);
 
72
                                return addins;
 
73
                        }
 
74
                }
 
75
                
 
76
                public RepositoryEntry FindEntry (string url)
 
77
                {
 
78
                        if (Repositories != null) {
 
79
                                foreach (RepositoryEntry e in Repositories)
 
80
                                        if (e.Url == url) return e;
 
81
                        }
 
82
                        if (Addins != null) {
 
83
                                foreach (RepositoryEntry e in Addins)
 
84
                                        if (e.Url == url) return e;
 
85
                        }
 
86
                        return null;
 
87
                }
 
88
                
 
89
                public void AddEntry (RepositoryEntry entry)
 
90
                {
 
91
                        entry.owner = this;
 
92
                        if (entry is ReferenceRepositoryEntry) {
 
93
                                Repositories.Add (entry);
 
94
                        } else {
 
95
                                Addins.Add (entry);
 
96
                        }
 
97
                }
 
98
                
 
99
                public void RemoveEntry (RepositoryEntry entry)
 
100
                {
 
101
                        if (entry is PackageRepositoryEntry)
 
102
                                Addins.Remove (entry);
 
103
                        else
 
104
                                Repositories.Remove (entry);
 
105
                }
 
106
                
 
107
                public IAsyncResult BeginDownloadSupportFile (string name, AsyncCallback cb, object state)
 
108
                {
 
109
                        FileAsyncResult res = new FileAsyncResult ();
 
110
                        res.AsyncState = state;
 
111
                        res.Callback = cb;
 
112
                        
 
113
                        string cachedFile = Path.Combine (CachedFilesDir, Path.GetFileName (name));
 
114
                        if (File.Exists (cachedFile)) {
 
115
                                res.FilePath = cachedFile;
 
116
                                res.CompletedSynchronously = true;
 
117
                                res.SetDone ();
 
118
                                return res;
 
119
                        }
 
120
                        
 
121
                        Uri u = new Uri (new Uri (Url), name);
 
122
                        if (u.Scheme == "file") {
 
123
                                res.FilePath = u.AbsolutePath;
 
124
                                res.CompletedSynchronously = true;
 
125
                                res.SetDone ();
 
126
                                return res;
 
127
                        }
 
128
                        else {
 
129
                                HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create (u);
 
130
                                res.FilePath = cachedFile;
 
131
                                res.Request = req;
 
132
                                req.BeginGetResponse (OnGotResponse, res);
 
133
                        }
 
134
                        return res;
 
135
                }
 
136
                
 
137
                void OnGotResponse (IAsyncResult ares)
 
138
                {
 
139
                        FileAsyncResult res = (FileAsyncResult) ares.AsyncState;
 
140
                        try {
 
141
                                WebResponse resp = res.Request.EndGetResponse (ares);
 
142
                                string dir = Path.GetDirectoryName (res.FilePath);
 
143
                                lock (this) {
 
144
                                        if (!Directory.Exists (dir))
 
145
                                                Directory.CreateDirectory (dir);
 
146
                                }
 
147
                                byte[] buffer = new byte [8092];
 
148
                                using (var s = resp.GetResponseStream ()) {
 
149
                                        using (var f = File.OpenWrite (res.FilePath)) {
 
150
                                                int nr = 0;
 
151
                                                while ((nr = s.Read (buffer, 0, buffer.Length)) > 0)
 
152
                                                        f.Write (buffer, 0, nr);
 
153
                                        }
 
154
                                }
 
155
                        } catch (Exception ex) {
 
156
                                res.Error = ex;
 
157
                        }
 
158
                        res.SetDone ();
 
159
                }
 
160
                
 
161
                public Stream EndDownloadSupportFile (IAsyncResult ares)
 
162
                {
 
163
                        FileAsyncResult res = ares as FileAsyncResult;
 
164
                        if (res == null)
 
165
                                throw new InvalidOperationException ("Invalid IAsyncResult instance");
 
166
                        if (res.Error != null)
 
167
                                throw res.Error;
 
168
                        return File.OpenRead (res.FilePath);
 
169
                }
 
170
        }
 
171
 
 
172
        class FileAsyncResult: IAsyncResult
 
173
        {
 
174
                ManualResetEvent done;
 
175
                
 
176
                public string FilePath;
 
177
                public HttpWebRequest Request;
 
178
                public AsyncCallback Callback;
 
179
                public Exception Error;
 
180
                
 
181
                public void SetDone ()
 
182
                {
 
183
                        lock (this) {
 
184
                                IsCompleted = true;
 
185
                                if (done != null)
 
186
                                        done.Set ();
 
187
                        }
 
188
                        if (Callback != null)
 
189
                                Callback (this);
 
190
                }
 
191
                
 
192
                public object AsyncState { get; set; }
 
193
        
 
194
                public WaitHandle AsyncWaitHandle {
 
195
                        get {
 
196
                                lock (this) {
 
197
                                        if (done == null)
 
198
                                                done = new ManualResetEvent (IsCompleted);
 
199
                                }
 
200
                                return done;
 
201
                        }
 
202
                }
 
203
        
 
204
                public bool CompletedSynchronously { get; set; }
 
205
        
 
206
                public bool IsCompleted { get; set; }
 
207
        }
 
208
        
 
209
}