Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.PluginInfrastructure.GUI/PluginSource.cs @ 11553

Last change on this file since 11553 was 118, checked in by gkronber, 17 years ago

fixed #100 by using an absolute path for the plugin-download location

File size: 3.9 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;
29using System.Windows.Forms;
30
31namespace HeuristicLab.PluginInfrastructure.GUI {
32  class PluginSource {
33
34    private string url;
35    private WebClient client;
36    private string cacheDir = Application.StartupPath + "/" + HeuristicLab.PluginInfrastructure.GUI.Properties.Settings.Default.CacheDir;
37
38    private PluginSource(string url) {
39      this.url = url;
40    }
41
42    /// <summary>
43    /// Factory method for new PluginRepositories
44    /// </summary>
45    /// <param name="url"></param>
46    /// <returns></returns>
47    public static PluginSource TryCreate(string url) {
48      PluginSource newSource = new PluginSource(url);
49      if(newSource.VerifyRepositoryLocation()) {
50        return newSource;
51      } else {
52        return null;
53      }
54    }
55
56    private bool VerifyRepositoryLocation() {
57      try {
58        this.client = new WebClient();
59        client.DownloadData(url + "/plugins.xml");
60      } catch(Exception) {
61        return false;
62      }
63      return true;
64    }
65
66    public List<PluginDescription> AvailablePlugins() {
67      List<PluginDescription> availablePlugins = new List<PluginDescription>();
68
69      Stream xmlStream = client.OpenRead(url + "/plugins.xml");
70
71      XmlDocument pluginList = new XmlDocument();
72      pluginList.Load(xmlStream);
73
74      XmlNode list = pluginList.SelectSingleNode("/Plugins");
75      foreach(XmlNode child in list.ChildNodes) {
76       
77        string name = child.Attributes["Name"].Value;
78        string version = child.Attributes["Version"].Value;
79
80        DateTime buildDate;
81        if(child.Attributes["Build"] != null) {
82          string build = child.Attributes["Build"].Value;
83          DateTime.TryParse(build, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat, System.Globalization.DateTimeStyles.AssumeUniversal, out buildDate);
84        } else {
85          buildDate = DateTime.MinValue;
86        }
87        PluginDescription description = new PluginDescription(name, new Version(version), buildDate, this);
88        availablePlugins.Add(description);
89
90        // retrieve the list of dependencies
91        XmlNodeList dependencies = child.SelectNodes("Dependency");
92        foreach(XmlNode dependencyNode in dependencies) {
93          string dependencyName = dependencyNode.Attributes["Name"].Value;
94          description.Dependencies.Add(dependencyName);
95        }
96
97      }
98
99      return availablePlugins;
100    }
101
102    internal long DownloadPlugin(PluginDescription description) {
103      string fileName = description.Name + "-" + description.Version + ".zip";
104      client.DownloadFile(url + "/" + fileName, cacheDir + "/" + fileName);
105
106      // return size of downloaded file
107      FileInfo info = new FileInfo(cacheDir + "/" + fileName);
108      return info.Length;
109    }
110
111    public void CancelAsyncDownload() {
112      client.CancelAsync();
113    }
114
115    public override string ToString() {
116      return url;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.