Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure.GUI/PluginSource.cs @ 97

Last change on this file since 97 was 91, checked in by gkronber, 17 years ago
  • added an attribute for the build time of an assembly.
  • build-date is automatically updated via SubWCRev in the pre-build action.
  • adapted PluginManager to correctly handle plugins with same version but newer build date

fixes ticket #39

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Net;
26using System.IO;
27using System.Xml;
28using System.ComponentModel;
29
30namespace HeuristicLab.PluginInfrastructure.GUI {
31  class PluginSource {
32
33    private string url;
34    private WebClient client;
35    private string cacheDir = HeuristicLab.PluginInfrastructure.GUI.Properties.Settings.Default.CacheDir;
36
37    private PluginSource(string url) {
38      this.url = url;
39    }
40
41    /// <summary>
42    /// Factory method for new PluginRepositories
43    /// </summary>
44    /// <param name="url"></param>
45    /// <returns></returns>
46    public static PluginSource TryCreate(string url) {
47      PluginSource newSource = new PluginSource(url);
48      if(newSource.VerifyRepositoryLocation()) {
49        return newSource;
50      } else {
51        return null;
52      }
53    }
54
55    private bool VerifyRepositoryLocation() {
56      try {
57        this.client = new WebClient();
58        client.DownloadData(url + "/plugins.xml");
59      } catch(Exception) {
60        return false;
61      }
62      return true;
63    }
64
65    public List<PluginDescription> AvailablePlugins() {
66      List<PluginDescription> availablePlugins = new List<PluginDescription>();
67
68      Stream xmlStream = client.OpenRead(url + "/plugins.xml");
69
70      XmlDocument pluginList = new XmlDocument();
71      pluginList.Load(xmlStream);
72
73      XmlNode list = pluginList.SelectSingleNode("/Plugins");
74      foreach(XmlNode child in list.ChildNodes) {
75       
76        string name = child.Attributes["Name"].Value;
77        string version = child.Attributes["Version"].Value;
78
79        DateTime buildDate;
80        if(child.Attributes["Build"] != null) {
81          string build = child.Attributes["Build"].Value;
82          DateTime.TryParse(build, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat, System.Globalization.DateTimeStyles.AssumeUniversal, out buildDate);
83        } else {
84          buildDate = DateTime.MinValue;
85        }
86        PluginDescription description = new PluginDescription(name, new Version(version), buildDate, this);
87        availablePlugins.Add(description);
88
89        // retrieve the list of dependencies
90        XmlNodeList dependencies = child.SelectNodes("Dependency");
91        foreach(XmlNode dependencyNode in dependencies) {
92          string dependencyName = dependencyNode.Attributes["Name"].Value;
93          description.Dependencies.Add(dependencyName);
94        }
95
96      }
97
98      return availablePlugins;
99    }
100
101    internal long DownloadPlugin(PluginDescription description) {
102      string fileName = description.Name + "-" + description.Version + ".zip";
103      client.DownloadFile(url + "/" + fileName, cacheDir + "/" + fileName);
104
105      // return size of downloaded file
106      FileInfo info = new FileInfo(cacheDir + "/" + fileName);
107      return info.Length;
108    }
109
110    public void CancelAsyncDownload() {
111      client.CancelAsync();
112    }
113
114    public override string ToString() {
115      return url;
116    }
117
118  }
119}
Note: See TracBrowser for help on using the repository browser.