~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/AddinStore.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
// AddinStore.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.Collections.Specialized;
 
32
using System.IO;
 
33
using System.Xml;
 
34
using System.Xml.Serialization;
 
35
using System.Reflection;
 
36
using System.Diagnostics;
 
37
using System.Net;
 
38
using System.Runtime.Serialization;
 
39
using System.Runtime.Serialization.Formatters.Binary;
 
40
 
 
41
using ICSharpCode.SharpZipLib.Zip;
 
42
using Mono.Addins;
 
43
using Mono.Addins.Setup.ProgressMonitoring;
 
44
using Mono.Addins.Description;
 
45
using Mono.Addins.Serialization;
 
46
using System.Collections.Generic;
 
47
using System.Linq;
 
48
 
 
49
namespace Mono.Addins.Setup
 
50
{
 
51
        internal class AddinStore
 
52
        {
 
53
                SetupService service;
 
54
                
 
55
                public AddinStore (SetupService service)
 
56
                {
 
57
                        this.service = service;
 
58
                }
 
59
                
 
60
                internal void ResetCachedData ()
 
61
                {
 
62
                }
 
63
                
 
64
                public AddinRegistry Registry {
 
65
                        get { return service.Registry; }
 
66
                }
 
67
                
 
68
                public bool Install (IProgressStatus statusMonitor, params string[] files)
 
69
                {
 
70
                        Package[] packages = new Package [files.Length];
 
71
                        for (int n=0; n<files.Length; n++)
 
72
                                packages [n] = AddinPackage.FromFile (files [n]);
 
73
 
 
74
                        return Install (statusMonitor, packages);
 
75
                }
 
76
                
 
77
                public bool Install (IProgressStatus statusMonitor, params AddinRepositoryEntry[] addins)
 
78
                {
 
79
                        Package[] packages = new Package [addins.Length];
 
80
                        for (int n=0; n<addins.Length; n++)
 
81
                                packages [n] = AddinPackage.FromRepository (addins [n]);
 
82
 
 
83
                        return Install (statusMonitor, packages);
 
84
                }
 
85
                
 
86
                internal bool Install (IProgressStatus monitor, params Package[] packages)
 
87
                {
 
88
                        PackageCollection packs = new PackageCollection ();
 
89
                        packs.AddRange (packages);
 
90
                        return Install (monitor, packs);
 
91
                }
 
92
                
 
93
                internal bool Install (IProgressStatus statusMonitor, PackageCollection packs)
 
94
                {
 
95
                        // Make sure the registry is up to date
 
96
                        service.Registry.Update (statusMonitor);
 
97
                        
 
98
                        IProgressMonitor monitor = ProgressStatusMonitor.GetProgressMonitor (statusMonitor);
 
99
                
 
100
                        PackageCollection toUninstall;
 
101
                        DependencyCollection unresolved;
 
102
                        if (!ResolveDependencies (monitor, packs, out toUninstall, out unresolved)) {
 
103
                                monitor.ReportError ("Not all dependencies could be resolved.", null);
 
104
                                return false;
 
105
                        }
 
106
                        
 
107
                        ArrayList prepared = new ArrayList ();
 
108
                        ArrayList uninstallPrepared = new ArrayList ();
 
109
                        bool rollback = false;
 
110
                        
 
111
                        monitor.BeginTask ("Installing add-ins...", 100);
 
112
                        
 
113
                        // Prepare install
 
114
                        
 
115
                        monitor.BeginStepTask ("Initializing installation", toUninstall.Count + packs.Count + 1, 75);
 
116
                        
 
117
                        foreach (Package mpack in toUninstall) {
 
118
                                try {
 
119
                                        mpack.PrepareUninstall (monitor, this);
 
120
                                        uninstallPrepared.Add (mpack);
 
121
                                        if (monitor.IsCancelRequested)
 
122
                                                throw new InstallException ("Installation cancelled.");
 
123
                                        monitor.Step (1);
 
124
                                } catch (Exception ex) {
 
125
                                        ReportException (monitor, ex);
 
126
                                        rollback = true;
 
127
                                        break;
 
128
                                }
 
129
                        }
 
130
                        
 
131
                        monitor.Step (1);
 
132
 
 
133
                        foreach (Package mpack in packs) {
 
134
                                try {
 
135
                                        mpack.PrepareInstall (monitor, this);
 
136
                                        if (monitor.IsCancelRequested)
 
137
                                                throw new InstallException ("Installation cancelled.");
 
138
                                        prepared.Add (mpack);
 
139
                                        monitor.Step (1);
 
140
                                } catch (Exception ex) {
 
141
                                        ReportException (monitor, ex);
 
142
                                        rollback = true;
 
143
                                        break;
 
144
                                }
 
145
                        }
 
146
                        
 
147
                        monitor.EndTask ();
 
148
                        
 
149
                        monitor.BeginStepTask ("Installing", toUninstall.Count + packs.Count + 1, 20);
 
150
                        
 
151
                        // Commit install
 
152
                        
 
153
                        if (!rollback) {
 
154
                                foreach (Package mpack in toUninstall) {
 
155
                                        try {
 
156
                                                mpack.CommitUninstall (monitor, this);
 
157
                                                if (monitor.IsCancelRequested)
 
158
                                                        throw new InstallException ("Installation cancelled.");
 
159
                                                monitor.Step (1);
 
160
                                        } catch (Exception ex) {
 
161
                                                ReportException (monitor, ex);
 
162
                                                rollback = true;
 
163
                                                break;
 
164
                                        }
 
165
                                }
 
166
                        }
 
167
                        
 
168
                        monitor.Step (1);
 
169
                        
 
170
                        if (!rollback) {
 
171
                                foreach (Package mpack in packs) {
 
172
                                        try {
 
173
                                                mpack.CommitInstall (monitor, this);
 
174
                                                if (monitor.IsCancelRequested)
 
175
                                                        throw new InstallException ("Installation cancelled.");
 
176
                                                monitor.Step (1);
 
177
                                        } catch (Exception ex) {
 
178
                                                ReportException (monitor, ex);
 
179
                                                rollback = true;
 
180
                                                break;
 
181
                                        }
 
182
                                }
 
183
                        }
 
184
                        
 
185
                        monitor.EndTask ();
 
186
                        
 
187
                        // Rollback if failed
 
188
                        
 
189
                        if (monitor.IsCancelRequested)
 
190
                                monitor = new NullProgressMonitor ();
 
191
                        
 
192
                        if (rollback) {
 
193
                                monitor.BeginStepTask ("Finishing installation", (prepared.Count + uninstallPrepared.Count)*2 + 1, 5);
 
194
                        
 
195
                                foreach (Package mpack in prepared) {
 
196
                                        try {
 
197
                                                mpack.RollbackInstall (monitor, this);
 
198
                                                monitor.Step (1);
 
199
                                        } catch (Exception ex) {
 
200
                                                ReportException (monitor, ex);
 
201
                                        }
 
202
                                }
 
203
                        
 
204
                                foreach (Package mpack in uninstallPrepared) {
 
205
                                        try {
 
206
                                                mpack.RollbackUninstall (monitor, this);
 
207
                                                monitor.Step (1);
 
208
                                        } catch (Exception ex) {
 
209
                                                ReportException (monitor, ex);
 
210
                                        }
 
211
                                }
 
212
                        } else
 
213
                                monitor.BeginStepTask ("Finishing installation", prepared.Count + uninstallPrepared.Count + 1, 5);
 
214
                        
 
215
                        // Cleanup
 
216
                        
 
217
                        foreach (Package mpack in prepared) {
 
218
                                try {
 
219
                                        mpack.EndInstall (monitor, this);
 
220
                                        monitor.Step (1);
 
221
                                } catch (Exception ex) {
 
222
                                        monitor.Log.WriteLine (ex);
 
223
                                }
 
224
                        }
 
225
                        
 
226
                        monitor.Step (1);
 
227
 
 
228
                        foreach (Package mpack in uninstallPrepared) {
 
229
                                try {
 
230
                                        mpack.EndUninstall (monitor, this);
 
231
                                        monitor.Step (1);
 
232
                                } catch (Exception ex) {
 
233
                                        monitor.Log.WriteLine (ex);
 
234
                                }
 
235
                        }
 
236
                        
 
237
                        // Update the extension maps
 
238
                        service.Registry.Update (statusMonitor);
 
239
                        
 
240
                        monitor.EndTask ();
 
241
 
 
242
                        monitor.EndTask ();
 
243
                        
 
244
                        service.SaveConfiguration ();
 
245
                        ResetCachedData ();
 
246
                        
 
247
                        return !rollback;
 
248
                }
 
249
                
 
250
                void ReportException (IProgressMonitor statusMonitor, Exception ex)
 
251
                {
 
252
                        if (ex is InstallException)
 
253
                                statusMonitor.ReportError (ex.Message, null);
 
254
                        else
 
255
                                statusMonitor.ReportError (null, ex);
 
256
                }
 
257
                
 
258
                public void Uninstall (IProgressStatus statusMonitor, string id)
 
259
                {
 
260
                        Uninstall (statusMonitor, new string[] { id });
 
261
                }
 
262
                
 
263
                public void Uninstall (IProgressStatus statusMonitor, IEnumerable<string> ids)
 
264
                {
 
265
                        IProgressMonitor monitor = ProgressStatusMonitor.GetProgressMonitor (statusMonitor);
 
266
                        monitor.BeginTask ("Uninstalling add-ins", ids.Count ());
 
267
                        
 
268
                        foreach (string id in ids) {
 
269
                                bool rollback = false;
 
270
                                ArrayList toUninstall = new ArrayList ();
 
271
                                ArrayList uninstallPrepared = new ArrayList ();
 
272
                                
 
273
                                Addin ia = service.Registry.GetAddin (id);
 
274
                                if (ia == null)
 
275
                                        throw new InstallException ("The add-in '" + id + "' is not installed.");
 
276
        
 
277
                                toUninstall.Add (AddinPackage.FromInstalledAddin (ia));
 
278
        
 
279
                                Addin[] deps = GetDependentAddins (id, true);
 
280
                                foreach (Addin dep in deps)
 
281
                                        toUninstall.Add (AddinPackage.FromInstalledAddin (dep));
 
282
                                
 
283
                                monitor.BeginTask ("Deleting files", toUninstall.Count*2 + uninstallPrepared.Count + 1);
 
284
                                
 
285
                                // Prepare install
 
286
                                
 
287
                                foreach (Package mpack in toUninstall) {
 
288
                                        try {
 
289
                                                mpack.PrepareUninstall (monitor, this);
 
290
                                                monitor.Step (1);
 
291
                                                uninstallPrepared.Add (mpack);
 
292
                                        } catch (Exception ex) {
 
293
                                                ReportException (monitor, ex);
 
294
                                                rollback = true;
 
295
                                                break;
 
296
                                        }
 
297
                                }
 
298
                                
 
299
                                // Commit install
 
300
                                
 
301
                                if (!rollback) {
 
302
                                        foreach (Package mpack in toUninstall) {
 
303
                                                try {
 
304
                                                        mpack.CommitUninstall (monitor, this);
 
305
                                                        monitor.Step (1);
 
306
                                                } catch (Exception ex) {
 
307
                                                        ReportException (monitor, ex);
 
308
                                                        rollback = true;
 
309
                                                        break;
 
310
                                                }
 
311
                                        }
 
312
                                }
 
313
                                
 
314
                                // Rollback if failed
 
315
                                
 
316
                                if (rollback) {
 
317
                                        monitor.BeginTask ("Rolling back uninstall", uninstallPrepared.Count);
 
318
                                        foreach (Package mpack in uninstallPrepared) {
 
319
                                                try {
 
320
                                                        mpack.RollbackUninstall (monitor, this);
 
321
                                                } catch (Exception ex) {
 
322
                                                        ReportException (monitor, ex);
 
323
                                                }
 
324
                                        }
 
325
                                        monitor.EndTask ();
 
326
                                }
 
327
                                monitor.Step (1);
 
328
        
 
329
                                // Cleanup
 
330
                                
 
331
                                foreach (Package mpack in uninstallPrepared) {
 
332
                                        try {
 
333
                                                mpack.EndUninstall (monitor, this);
 
334
                                                monitor.Step (1);
 
335
                                        } catch (Exception ex) {
 
336
                                                monitor.Log.WriteLine (ex);
 
337
                                        }
 
338
                                }
 
339
                                
 
340
                                monitor.EndTask ();
 
341
                                monitor.Step (1);
 
342
                        }
 
343
                        
 
344
                        // Update the extension maps
 
345
                        service.Registry.Update (statusMonitor);
 
346
                        
 
347
                        monitor.EndTask ();
 
348
                        
 
349
                        service.SaveConfiguration ();
 
350
                        ResetCachedData ();
 
351
                }
 
352
                
 
353
                public Addin[] GetDependentAddins (string id, bool recursive)
 
354
                {
 
355
                        ArrayList list = new ArrayList ();
 
356
                        FindDependentAddins (list, id, recursive);
 
357
                        return (Addin[]) list.ToArray (typeof (Addin));
 
358
                }
 
359
                
 
360
                void FindDependentAddins (ArrayList list, string id, bool recursive)
 
361
                {
 
362
                        foreach (Addin iaddin in service.Registry.GetAddins ()) {
 
363
                                if (list.Contains (iaddin))
 
364
                                        continue;
 
365
                                foreach (Dependency dep in iaddin.Description.MainModule.Dependencies) {
 
366
                                        AddinDependency adep = dep as AddinDependency;
 
367
                                        if (adep != null && adep.AddinId == id) {
 
368
                                                list.Add (iaddin);
 
369
                                                if (recursive)
 
370
                                                        FindDependentAddins (list, iaddin.Id, true);
 
371
                                        }
 
372
                                }
 
373
                        }
 
374
                }
 
375
                
 
376
                public bool ResolveDependencies (IProgressStatus statusMonitor, AddinRepositoryEntry[] addins, out PackageCollection resolved, out PackageCollection toUninstall, out DependencyCollection unresolved)
 
377
                {
 
378
                        resolved = new PackageCollection ();
 
379
                        for (int n=0; n<addins.Length; n++)
 
380
                                resolved.Add (AddinPackage.FromRepository (addins [n]));
 
381
                        return ResolveDependencies (statusMonitor, resolved, out toUninstall, out unresolved);
 
382
                }
 
383
                
 
384
                public bool ResolveDependencies (IProgressStatus statusMonitor, PackageCollection packages, out PackageCollection toUninstall, out DependencyCollection unresolved)
 
385
                {
 
386
                        IProgressMonitor monitor = ProgressStatusMonitor.GetProgressMonitor (statusMonitor);
 
387
                        return ResolveDependencies (monitor, packages, out toUninstall, out unresolved);
 
388
                }
 
389
                
 
390
                internal bool ResolveDependencies (IProgressMonitor monitor, PackageCollection packages, out PackageCollection toUninstall, out DependencyCollection unresolved)
 
391
                {
 
392
                        PackageCollection requested = new PackageCollection();
 
393
                        requested.AddRange (packages);
 
394
                        
 
395
                        unresolved = new DependencyCollection ();
 
396
                        toUninstall = new PackageCollection ();
 
397
                        PackageCollection installedRequired = new PackageCollection ();
 
398
                        
 
399
                        for (int n=0; n<packages.Count; n++) {
 
400
                                Package p = packages [n];
 
401
                                p.Resolve (monitor, this, packages, toUninstall, installedRequired, unresolved);
 
402
                        }
 
403
                        
 
404
                        if (unresolved.Count != 0) {
 
405
                                foreach (Dependency dep in unresolved)
 
406
                                        monitor.ReportError (string.Format ("The package '{0}' could not be found in any repository", dep.Name), null);
 
407
                                return false;
 
408
                        }
 
409
                        
 
410
                        // Check that we are not uninstalling packages that are required
 
411
                        // by packages being installed.
 
412
 
 
413
                        foreach (Package p in installedRequired) {
 
414
                                if (toUninstall.Contains (p)) {
 
415
                                        // Only accept to uninstall this package if we are
 
416
                                        // going to install a newer version.
 
417
                                        bool foundUpgrade = false;
 
418
                                        foreach (Package tbi in packages)
 
419
                                                if (tbi.Equals (p) || tbi.IsUpgradeOf (p)) {
 
420
                                                        foundUpgrade = true;
 
421
                                                        break;
 
422
                                                }
 
423
                                        if (!foundUpgrade)
 
424
                                                return false;
 
425
                                }
 
426
                        }
 
427
                        
 
428
                        // Check that we are not trying to uninstall from a directory from
 
429
                        // which we don't have write permissions
 
430
                        
 
431
                        foreach (Package p in toUninstall) {
 
432
                                AddinPackage ap = p as AddinPackage;
 
433
                                if (ap != null) {
 
434
                                        Addin ia = service.Registry.GetAddin (ap.Addin.Id);
 
435
                                        if (File.Exists (ia.AddinFile) && !HasWriteAccess (ia.AddinFile) && IsUserAddin (ia.AddinFile)) {
 
436
                                                monitor.ReportError (GetUninstallErrorNoRoot (ap.Addin), null);
 
437
                                                return false;
 
438
                                        }
 
439
                                }
 
440
                        }
 
441
                        
 
442
                        // Check that we are not installing two versions of the same addin
 
443
                        
 
444
                        PackageCollection resolved = new PackageCollection();
 
445
                        resolved.AddRange (packages);
 
446
                        
 
447
                        bool error = false;
 
448
                        
 
449
                        for (int n=0; n<packages.Count; n++) {
 
450
                                AddinPackage ap = packages [n] as AddinPackage;
 
451
                                if (ap == null) continue;
 
452
                                
 
453
                                for (int k=n+1; k<packages.Count; k++) {
 
454
                                        AddinPackage otherap = packages [k] as AddinPackage;
 
455
                                        if (otherap == null) continue;
 
456
                                        
 
457
                                        if (ap.Addin.Id == otherap.Addin.Id) {
 
458
                                                if (ap.IsUpgradeOf (otherap)) {
 
459
                                                        if (requested.Contains (otherap)) {
 
460
                                                                monitor.ReportError ("Can't install two versions of the same add-in: '" + ap.Addin.Name + "'.", null);
 
461
                                                                error = true;
 
462
                                                        } else {
 
463
                                                                packages.RemoveAt (k);
 
464
                                                        }
 
465
                                                } else if (otherap.IsUpgradeOf (ap)) {
 
466
                                                        if (requested.Contains (ap)) {
 
467
                                                                monitor.ReportError ("Can't install two versions of the same add-in: '" + ap.Addin.Name + "'.", null);
 
468
                                                                error = true;
 
469
                                                        } else {
 
470
                                                                packages.RemoveAt (n);
 
471
                                                                n--;
 
472
                                                        }
 
473
                                                } else {
 
474
                                                        error = true;
 
475
                                                        monitor.ReportError ("Can't install two versions of the same add-in: '" + ap.Addin.Name + "'.", null);
 
476
                                                }
 
477
                                                break;
 
478
                                        }
 
479
                                }
 
480
                        }
 
481
                        
 
482
                        // Don't allow installing add-ins which are scheduled for uninstall
 
483
                        
 
484
                        foreach (Package p in packages) {
 
485
                                AddinPackage ap = p as AddinPackage;
 
486
                                if (ap != null && Registry.IsRegisteredForUninstall (ap.Addin.Id)) {
 
487
                                        error = true;
 
488
                                        monitor.ReportError ("The addin " + ap.Addin.Name + " v" + ap.Addin.Version + " is scheduled for uninstallation. Please restart the application before trying to re-install it.", null);
 
489
                                }
 
490
                        }
 
491
                        
 
492
                        return !error;
 
493
                }
 
494
                
 
495
                internal void ResolveDependency (IProgressMonitor monitor, Dependency dep, AddinPackage parentPackage, PackageCollection toInstall, PackageCollection toUninstall, PackageCollection installedRequired, DependencyCollection unresolved)
 
496
                {
 
497
                        AddinDependency adep = dep as AddinDependency;
 
498
                        if (adep == null)
 
499
                                return;
 
500
                        
 
501
                        string nsid = Addin.GetFullId (parentPackage.Addin.Namespace, adep.AddinId, null);
 
502
                        
 
503
                        foreach (Package p in toInstall) {
 
504
                                AddinPackage ap = p as AddinPackage;
 
505
                                if (ap != null) {
 
506
                                        if (Addin.GetIdName (ap.Addin.Id) == nsid && ((AddinInfo)ap.Addin).SupportsVersion (adep.Version))
 
507
                                                return;
 
508
                                } 
 
509
                        }
 
510
                        
 
511
                        ArrayList addins = new ArrayList ();
 
512
                        addins.AddRange (service.Registry.GetAddins ());
 
513
                        addins.AddRange (service.Registry.GetAddinRoots ());
 
514
                        
 
515
                        foreach (Addin addin in addins) {
 
516
                                if (Addin.GetIdName (addin.Id) == nsid && addin.SupportsVersion (adep.Version)) {
 
517
                                        AddinPackage p = AddinPackage.FromInstalledAddin (addin);
 
518
                                        if (!installedRequired.Contains (p))
 
519
                                                installedRequired.Add (p);
 
520
                                        return;
 
521
                                }
 
522
                        }
 
523
                        
 
524
                        AddinRepositoryEntry[] avaddins = service.Repositories.GetAvailableAddins ();
 
525
                        foreach (PackageRepositoryEntry avAddin in avaddins) {
 
526
                                if (Addin.GetIdName (avAddin.Addin.Id) == nsid && ((AddinInfo)avAddin.Addin).SupportsVersion (adep.Version)) {
 
527
                                        toInstall.Add (AddinPackage.FromRepository (avAddin));
 
528
                                        return;
 
529
                                }
 
530
                        }
 
531
                        unresolved.Add (adep);
 
532
                }
 
533
 
 
534
                internal string GetAddinDirectory (AddinInfo info)
 
535
                {
 
536
                        return Path.Combine (service.InstallDirectory, info.Id.Replace (',','.'));
 
537
                }
 
538
                
 
539
                internal void RegisterAddin (IProgressMonitor monitor, AddinInfo info, string sourceDir)
 
540
                {
 
541
                        monitor.Log.WriteLine ("Installing " + info.Name + " v" + info.Version);
 
542
                        string addinDir = GetAddinDirectory (info);
 
543
                        if (!Directory.Exists (addinDir))
 
544
                                Directory.CreateDirectory (addinDir);
 
545
                        CopyDirectory (sourceDir, addinDir);
 
546
 
 
547
                        ResetCachedData ();
 
548
                }
 
549
        
 
550
                void CopyDirectory (string src, string dest)
 
551
                {
 
552
                        CopyDirectory (src, dest, "");
 
553
                }
 
554
                
 
555
                void CopyDirectory (string src, string dest, string subdir)
 
556
                {
 
557
                        string destDir = Path.Combine (dest, subdir);
 
558
        
 
559
                        if (!Directory.Exists (destDir))
 
560
                                Directory.CreateDirectory (destDir);
 
561
        
 
562
                        foreach (string file in Directory.GetFiles (src)) {
 
563
                                if (Path.GetFileName (file) != "addin.info")
 
564
                                        File.Copy (file, Path.Combine (destDir, Path.GetFileName (file)), true);
 
565
                        }
 
566
        
 
567
                        foreach (string dir in Directory.GetDirectories (src))
 
568
                                CopyDirectory (dir, dest, Path.Combine (subdir, Path.GetFileName (dir)));
 
569
                }
 
570
                
 
571
                internal object DownloadObject (IProgressMonitor monitor, string url, Type type)
 
572
                {
 
573
                        string file = null;
 
574
                        try {
 
575
                                file = DownloadFile (monitor, url);
 
576
                                return ReadObject (file, type);
 
577
                        } finally {
 
578
                                if (file != null)
 
579
                                        File.Delete (file);
 
580
                        }
 
581
                }
 
582
                
 
583
                static XmlSerializer GetSerializer (Type type)
 
584
                {
 
585
                        if (type == typeof(AddinSystemConfiguration))
 
586
                                return new AddinSystemConfigurationSerializer ();
 
587
                        else if (type == typeof(Repository))
 
588
                                return new RepositorySerializer ();
 
589
                        else
 
590
                                return new XmlSerializer (type);
 
591
                }
 
592
                
 
593
                internal static object ReadObject (string file, Type type)
 
594
                {
 
595
                        if (!File.Exists (file))
 
596
                                return null;
 
597
 
 
598
                        StreamReader r = new StreamReader (file);
 
599
                        try {
 
600
                                XmlSerializer ser = GetSerializer (type);
 
601
                                return ser.Deserialize (r);
 
602
                        } catch {
 
603
                                return null;
 
604
                        } finally {
 
605
                                r.Close ();
 
606
                        }
 
607
                }
 
608
                
 
609
                internal static void WriteObject (string file, object obj)
 
610
                {
 
611
                        string dir = Path.GetDirectoryName (file);
 
612
                        if (!Directory.Exists (dir))
 
613
                                Directory.CreateDirectory (dir);
 
614
                        StreamWriter s = new StreamWriter (file);
 
615
                        try {
 
616
                                XmlSerializer ser = GetSerializer (obj.GetType());
 
617
                                ser.Serialize (s, obj);
 
618
                                s.Close ();
 
619
                        } catch {
 
620
                                s.Close ();
 
621
                                if (File.Exists (file))
 
622
                                        File.Delete (file);
 
623
                                throw;
 
624
                        }
 
625
                }
 
626
                
 
627
                internal string DownloadFile (IProgressMonitor monitor, string url)
 
628
                {
 
629
                        if (url.StartsWith ("file://")) {
 
630
                                string tmpfile = Path.GetTempFileName ();
 
631
                                string path = new Uri (url).LocalPath;
 
632
                                File.Delete (tmpfile);
 
633
                                File.Copy (path, tmpfile);
 
634
                                return tmpfile;
 
635
                        }
 
636
 
 
637
                        monitor.BeginTask ("Requesting " + url, 2);
 
638
                        HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url);
 
639
                        req.Headers ["Pragma"] = "no-cache";
 
640
                        HttpWebResponse resp = (HttpWebResponse) req.GetResponse ();
 
641
                        monitor.Step (1);
 
642
                        
 
643
                        monitor.BeginTask ("Downloading " + url, (int) resp.ContentLength);
 
644
                        
 
645
                        string file = Path.GetTempFileName ();
 
646
                        FileStream fs = null;
 
647
                        Stream s = null;
 
648
                        try {
 
649
                                fs = new FileStream (file, FileMode.Create, FileAccess.Write);
 
650
                                s = req.GetResponse ().GetResponseStream ();
 
651
                                byte[] buffer = new byte [4096];
 
652
                                
 
653
                                int n;
 
654
                                while ((n = s.Read (buffer, 0, buffer.Length)) != 0) {
 
655
                                        monitor.Step (n);
 
656
                                        fs.Write (buffer, 0, n);
 
657
                                        if (monitor.IsCancelRequested)
 
658
                                                throw new InstallException ("Installation cancelled.");
 
659
                                }
 
660
                                fs.Close ();
 
661
                                s.Close ();
 
662
                                return file;
 
663
                        } catch {
 
664
                                if (fs != null)
 
665
                                        fs.Close ();
 
666
                                if (s != null)
 
667
                                        s.Close ();
 
668
                                File.Delete (file);
 
669
                                throw;
 
670
                        } finally {
 
671
                                monitor.EndTask ();
 
672
                                monitor.EndTask ();
 
673
                        }
 
674
                }
 
675
                        
 
676
                internal bool HasWriteAccess (string file)
 
677
                {
 
678
                        FileInfo f = new FileInfo (file);
 
679
                        return !f.Exists || !f.IsReadOnly;
 
680
                }
 
681
                
 
682
                internal bool IsUserAddin (string addinFile)
 
683
                {
 
684
                        string installPath = service.InstallDirectory;
 
685
                        if (installPath [installPath.Length - 1] != Path.DirectorySeparatorChar)
 
686
                                installPath += Path.DirectorySeparatorChar;
 
687
                        return Path.GetFullPath (addinFile).StartsWith (installPath);
 
688
                }
 
689
                
 
690
                internal static string GetUninstallErrorNoRoot (AddinHeader ainfo)
 
691
                {
 
692
                        return string.Format ("The add-in '{0} v{1}' can't be uninstalled with the current user permissions.", ainfo.Name, ainfo.Version);
 
693
                }
 
694
        }
 
695
}