7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
30
using System.Collections;
32
using System.Xml.Serialization;
35
using System.Threading;
37
namespace Mono.Addins.Setup
39
internal class Repository
41
RepositoryEntryCollection repositories;
42
RepositoryEntryCollection addins;
56
internal string CachedFilesDir { get; set; }
58
[XmlElement ("Repository", Type = typeof(ReferenceRepositoryEntry))]
59
public RepositoryEntryCollection Repositories {
61
if (repositories == null)
62
repositories = new RepositoryEntryCollection (this);
67
[XmlElement ("Addin", Type = typeof(PackageRepositoryEntry))]
68
public RepositoryEntryCollection Addins {
71
addins = new RepositoryEntryCollection (this);
76
public RepositoryEntry FindEntry (string url)
78
if (Repositories != null) {
79
foreach (RepositoryEntry e in Repositories)
80
if (e.Url == url) return e;
83
foreach (RepositoryEntry e in Addins)
84
if (e.Url == url) return e;
89
public void AddEntry (RepositoryEntry entry)
92
if (entry is ReferenceRepositoryEntry) {
93
Repositories.Add (entry);
99
public void RemoveEntry (RepositoryEntry entry)
101
if (entry is PackageRepositoryEntry)
102
Addins.Remove (entry);
104
Repositories.Remove (entry);
107
public IAsyncResult BeginDownloadSupportFile (string name, AsyncCallback cb, object state)
109
FileAsyncResult res = new FileAsyncResult ();
110
res.AsyncState = state;
113
string cachedFile = Path.Combine (CachedFilesDir, Path.GetFileName (name));
114
if (File.Exists (cachedFile)) {
115
res.FilePath = cachedFile;
116
res.CompletedSynchronously = true;
121
Uri u = new Uri (new Uri (Url), name);
122
if (u.Scheme == "file") {
123
res.FilePath = u.AbsolutePath;
124
res.CompletedSynchronously = true;
129
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create (u);
130
res.FilePath = cachedFile;
132
req.BeginGetResponse (OnGotResponse, res);
137
void OnGotResponse (IAsyncResult ares)
139
FileAsyncResult res = (FileAsyncResult) ares.AsyncState;
141
WebResponse resp = res.Request.EndGetResponse (ares);
142
string dir = Path.GetDirectoryName (res.FilePath);
144
if (!Directory.Exists (dir))
145
Directory.CreateDirectory (dir);
147
byte[] buffer = new byte [8092];
148
using (var s = resp.GetResponseStream ()) {
149
using (var f = File.OpenWrite (res.FilePath)) {
151
while ((nr = s.Read (buffer, 0, buffer.Length)) > 0)
152
f.Write (buffer, 0, nr);
155
} catch (Exception ex) {
161
public Stream EndDownloadSupportFile (IAsyncResult ares)
163
FileAsyncResult res = ares as FileAsyncResult;
165
throw new InvalidOperationException ("Invalid IAsyncResult instance");
166
if (res.Error != null)
168
return File.OpenRead (res.FilePath);
172
class FileAsyncResult: IAsyncResult
174
ManualResetEvent done;
176
public string FilePath;
177
public HttpWebRequest Request;
178
public AsyncCallback Callback;
179
public Exception Error;
181
public void SetDone ()
188
if (Callback != null)
192
public object AsyncState { get; set; }
194
public WaitHandle AsyncWaitHandle {
198
done = new ManualResetEvent (IsCompleted);
204
public bool CompletedSynchronously { get; set; }
206
public bool IsCompleted { get; set; }