Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11 was 2, checked in by swagner, 16 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 3.5 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        PluginDescription description = new PluginDescription(name, new Version(version), this);
80        availablePlugins.Add(description);
81
82        // retrieve the list of dependencies
83        XmlNodeList dependencies = child.SelectNodes("Dependency");
84        foreach(XmlNode dependencyNode in dependencies) {
85          string dependencyName = dependencyNode.Attributes["Name"].Value;
86          description.Dependencies.Add(dependencyName);
87        }
88
89      }
90
91      return availablePlugins;
92    }
93
94    internal long DownloadPlugin(PluginDescription description) {
95      string fileName = description.Name + "-" + description.Version + ".zip";
96      client.DownloadFile(url + "/" + fileName, cacheDir + "/" + fileName);
97
98      // return size of downloaded file
99      FileInfo info = new FileInfo(cacheDir + "/" + fileName);
100      return info.Length;
101    }
102
103    public void CancelAsyncDownload() {
104      client.CancelAsync();
105    }
106
107    public override string ToString() {
108      return url;
109    }
110
111  }
112}
Note: See TracBrowser for help on using the repository browser.