/plugin API/Plugin.cs

http://yet-another-music-application.googlecode.com/ · C# · 559 lines · 278 code · 83 blank · 198 comment · 34 complexity · 8a4303111950c7e1d375f6dd1ef6c098 MD5 · raw file

  1. /**
  2. * Plugin.cs
  3. *
  4. * All the functionality for a plugin.
  5. *
  6. * * * * * * * * *
  7. *
  8. * Copyright 2012 Simplare
  9. *
  10. * This code is part of the Stoffi Music Player Project.
  11. * Visit our website at: stoffiplayer.com
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 3 of the License, or (at your option) any later version.
  17. *
  18. * See stoffiplayer.com/license for more information.
  19. **/
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Collections.Specialized;
  23. using System.Collections.ObjectModel;
  24. using System.ComponentModel;
  25. using System.Drawing;
  26. using System.Globalization;
  27. using System.Linq;
  28. using System.Text;
  29. using System.Xml.Serialization;
  30. namespace Stoffi.Plugins
  31. {
  32. /// <summary>
  33. /// The plugin interface which will make available stoffi functionality for the plugin programmer.
  34. /// </summary>
  35. public class Plugin
  36. {
  37. #region Members
  38. protected PluginType pluginType = PluginType.Unknown;
  39. #endregion
  40. #region Properties
  41. /// <summary>
  42. /// Gets the type of the plugin.
  43. /// </summary>
  44. public PluginType Type { get { return pluginType; } }
  45. /// <summary>
  46. /// Gets the name of the author of the plugin.
  47. /// </summary>
  48. public virtual string Author { get; protected set; }
  49. /// <summary>
  50. /// Gets the website of the plugin.
  51. /// </summary>
  52. public virtual string Website { get; protected set; }
  53. /// <summary>
  54. /// Gets or sets the version of the plugin.
  55. /// </summary>
  56. public virtual Version Version { get; protected set; }
  57. /// <summary>
  58. /// Gets the version of the plugin platform that the
  59. /// plugin was built for.
  60. /// </summary>
  61. public Version PlatformVersion { get; private set; }
  62. /// <summary>
  63. /// Gets the identifier of the plugin.
  64. /// </summary>
  65. public string ID { get; private set; }
  66. /// <summary>
  67. /// Gets or sets the list of supported languages.
  68. /// </summary>
  69. public List<Language> Languages { get; set; }
  70. /// <summary>
  71. /// Gets or sets the IETF tag of the currently active culture.
  72. /// </summary>
  73. public string CurrentCulture { get; set; }
  74. /// <summary>
  75. /// Gets the currently active language.
  76. /// </summary>
  77. public Language CurrentLanguage
  78. {
  79. get
  80. {
  81. foreach (Language l in Languages)
  82. if (l.Culture == CurrentCulture)
  83. return l;
  84. foreach (Language l in Languages)
  85. if (l.Culture == "en-US")
  86. return l;
  87. return null;
  88. }
  89. }
  90. /// <summary>
  91. /// The default language (English if found, otherwise first in list).
  92. /// </summary>
  93. public Language DefaultLanguage
  94. {
  95. get
  96. {
  97. foreach (Language l in Languages)
  98. if (l.Culture == "en-US")
  99. return l;
  100. if (Languages.Count > 0)
  101. return Languages[0];
  102. return null;
  103. }
  104. }
  105. /// <summary>
  106. /// Gets the list of settings used to configure the plugin.
  107. /// </summary>
  108. public ObservableCollection<Setting> Settings { get; private set; }
  109. /// <summary>
  110. /// Gets the list of status labels of the plugin.
  111. /// </summary>
  112. public List<StatusLabel> StatusLabels { get; private set; }
  113. #endregion
  114. #region Constructor
  115. /// <summary>
  116. /// Creates an instance of a plugin.
  117. /// </summary>
  118. /// <param name="id">A string identifying the plugin</param>
  119. /// <param name="version">The assembly version</param>
  120. /// <param name="platformVersion">The minimum version required of the plugin platform</param>
  121. public Plugin(string id, Version version, Version platformVersion)
  122. {
  123. this.ID = id;
  124. this.Version = version;
  125. this.PlatformVersion = platformVersion;
  126. PlatformVersion = new Version(0, 4);
  127. Languages = new List<Language>();
  128. Settings = new ObservableCollection<Setting>();
  129. StatusLabels = new List<StatusLabel>();
  130. }
  131. #endregion
  132. #region Methods
  133. #region Public
  134. /// <summary>
  135. /// Translates a string.
  136. ///
  137. /// It first tries to find the string in the current language,
  138. /// if not found it will search in the default language,
  139. /// if still not found it will return the ID of the string.
  140. /// </summary>
  141. /// <param name="id">The ID of the translation string</param>
  142. /// <returns>The string localized according to the current culture</returns>
  143. public string T(string id)
  144. {
  145. Language l = CurrentLanguage;
  146. if (l != null)
  147. foreach (Translation t in l.Translations)
  148. if (t.ID == id)
  149. return t.Text.Replace("\\n", "\n");
  150. l = DefaultLanguage;
  151. if (l != null)
  152. foreach (Translation t in l.Translations)
  153. if (t.ID == id)
  154. return t.Text.Replace("\\n", "\n");
  155. return id;
  156. }
  157. /// <summary>
  158. /// Called when the plugin is installed.
  159. /// </summary>
  160. /// <returns>True if set up was successful, otherwise false</returns>
  161. public virtual bool OnInstall()
  162. {
  163. return true;
  164. }
  165. /// <summary>
  166. /// Called when the plugin is activated
  167. /// </summary>
  168. /// <returns>True if set up was successful, otherwise false</returns>
  169. public virtual bool OnStart()
  170. {
  171. return true;
  172. }
  173. /// <summary>
  174. /// Called when the plugin is deactivated
  175. /// </summary>
  176. /// <returns>True if tear down was successful, otherwise false</returns>
  177. public virtual bool OnStop()
  178. {
  179. return true;
  180. }
  181. /// <summary>
  182. /// Called when the plugin is uninstalled.
  183. /// </summary>
  184. /// <returns>True if tear down was successful, otherwise false</returns>
  185. public virtual bool OnUninstall()
  186. {
  187. return true;
  188. }
  189. /// <summary>
  190. /// Updates the plugin.
  191. /// </summary>
  192. public virtual void Refresh()
  193. {
  194. }
  195. /// <summary>
  196. /// Updates the plugin.
  197. /// </summary>
  198. /// /// <param name="deltaTime">Time elapsed since last tick</param>
  199. public virtual void Refresh(float deltaTime)
  200. {
  201. }
  202. #endregion
  203. #region Protected
  204. #endregion
  205. #endregion
  206. }
  207. #region Enums
  208. /// <summary>
  209. /// A type of a plugin.
  210. /// </summary>
  211. public enum PluginType
  212. {
  213. /// <summary>
  214. /// A plugin providing a source of music.
  215. /// </summary>
  216. Source,
  217. /// <summary>
  218. /// A plugin manipulating the sound.
  219. /// </summary>
  220. Filter,
  221. /// <summary>
  222. /// A plugin providing a visualization of the sound.
  223. /// </summary>
  224. Visualizer,
  225. /// <summary>
  226. /// A plugin of unknown type.
  227. /// </summary>
  228. Unknown
  229. }
  230. #endregion
  231. #region Data structures
  232. /// <summary>
  233. /// Describes a language containing a collection of localized strings.
  234. /// </summary>
  235. public class Language
  236. {
  237. /// <summary>
  238. /// Gets or sets the IETF culture tag of the language.
  239. /// </summary>
  240. public string Culture { get; set; }
  241. /// <summary>
  242. /// Gets or sets the collection of localized strings.
  243. /// </summary>
  244. public List<Translation> Translations { get; set; }
  245. }
  246. /// <summary>
  247. /// Describes a single localized string.
  248. /// </summary>
  249. public class Translation
  250. {
  251. /// <summary>
  252. /// Gets or sets the ID of the translation string.
  253. /// </summary>
  254. [XmlAttribute("ID")]
  255. public string ID { get; set; }
  256. /// <summary>
  257. /// Gets or sets the localized string.
  258. /// </summary>
  259. [XmlAttribute("Text")]
  260. public string Text { get; set; }
  261. }
  262. /// <summary>
  263. /// Describes a status of the plugin.
  264. /// </summary>
  265. public class StatusLabel : INotifyPropertyChanged
  266. {
  267. #region Members
  268. private String label;
  269. private String status;
  270. #endregion
  271. #region Properties
  272. /// <summary>
  273. /// Gets or sets the string of the label.
  274. /// </summary>
  275. public String Label
  276. {
  277. get { return label; }
  278. set { label = value; OnPropertyChanged("Label"); }
  279. }
  280. /// <summary>
  281. /// Gets or sets the string describing the current status.
  282. /// </summary>
  283. public String Status
  284. {
  285. get { return status; }
  286. set { status = value; OnPropertyChanged("Status"); }
  287. }
  288. #endregion
  289. #region Methods
  290. /// <summary>
  291. /// Dispatches the PropertyChanged event
  292. /// </summary>
  293. /// <param name="name">The name of the property that was changed</param>
  294. public void OnPropertyChanged(string name)
  295. {
  296. if (PropertyChanged != null)
  297. PropertyChanged(this, new PropertyChangedEventArgs(name));
  298. }
  299. #endregion
  300. #region Events
  301. /// <summary>
  302. /// Occurs when the property of the item is changed
  303. /// </summary>
  304. public event PropertyChangedEventHandler PropertyChanged;
  305. #endregion
  306. }
  307. /// <summary>
  308. /// A plugin setting.
  309. /// </summary>
  310. public class Setting : INotifyPropertyChanged
  311. {
  312. #region Members
  313. private String id;
  314. private Object value;
  315. private Object maximum;
  316. private Object minimum;
  317. private List<Object> possibleValues = new List<object>();
  318. private Type type;
  319. private Boolean isVisible;
  320. #endregion
  321. #region Properties
  322. /// <summary>
  323. /// Gets or sets the ID of the setting.
  324. /// </summary>
  325. public String ID
  326. {
  327. get { return id; }
  328. set { id = value; OnPropertyChanged("ID"); }
  329. }
  330. /// <summary>
  331. /// Gets or sets the type of the setting's value.
  332. /// </summary>
  333. [XmlIgnore()]
  334. public Type Type
  335. {
  336. get { return type; }
  337. set { type = value; OnPropertyChanged("Type"); }
  338. }
  339. /// <summary>
  340. /// Gets or sets the value of the setting.
  341. /// </summary>
  342. [XmlIgnore()]
  343. public Object Value
  344. {
  345. get { return value; }
  346. set { this.value = value; OnPropertyChanged("Value"); }
  347. }
  348. /// <summary>
  349. /// Gets or sets the maximum value possible.
  350. /// </summary>
  351. /// <remarks>
  352. /// Used with numerical types to create a slider.
  353. /// </remarks>
  354. public Object Maximum
  355. {
  356. get { return maximum; }
  357. set { maximum = value; OnPropertyChanged("Maximum"); }
  358. }
  359. /// <summary>
  360. /// Gets or sets the minimum value possible.
  361. /// </summary>
  362. /// <remarks>
  363. /// Used with numerical types to create a slider along with the Maximum property.
  364. /// </remarks>
  365. public Object Minimum
  366. {
  367. get { return minimum; }
  368. set { minimum = value; OnPropertyChanged("Minimum"); }
  369. }
  370. /// <summary>
  371. /// Gets or sets the list of possible values for the setting.
  372. /// </summary>
  373. /// <remarks>
  374. /// Used to create a dropdown menu.
  375. /// </remarks>
  376. public List<Object> PossibleValues
  377. {
  378. get { return possibleValues; }
  379. set { possibleValues = value; OnPropertyChanged("PossibleValues"); }
  380. }
  381. /// <summary>
  382. /// Gets or sets whether or not the setting should be visible to the user.
  383. /// </summary>
  384. public Boolean IsVisible
  385. {
  386. get { return isVisible; }
  387. set { isVisible = value; OnPropertyChanged("IsVisible"); }
  388. }
  389. /// <summary>
  390. /// Gets or sets a serialized representation of the Type property.
  391. /// </summary>
  392. /// <remarks>
  393. /// Enables the class to be serialized for saving in XML file.
  394. /// </remarks>
  395. [XmlElement(ElementName = "Type")]
  396. public String SerializedType
  397. {
  398. get
  399. {
  400. return type == null ? null : type.AssemblyQualifiedName;
  401. }
  402. set
  403. {
  404. type = Type.GetType(value);
  405. }
  406. }
  407. /// <summary>
  408. /// Gets or sets a serialized representation of the Value property.
  409. /// </summary>
  410. /// <remarks>
  411. /// Enables the class to be serialized for saving in XML file.
  412. /// </remarks>
  413. [XmlElement(ElementName = "Value")]
  414. public String SerializedValue
  415. {
  416. get
  417. {
  418. if (SerializedType == typeof(Color).AssemblyQualifiedName)
  419. {
  420. try
  421. {
  422. Color c = (Color)value;
  423. return c.IsNamedColor ? c.Name : "#" + c.Name.ToUpper();
  424. }
  425. catch
  426. {
  427. return null;
  428. }
  429. }
  430. else
  431. return value == null ? null : value.ToString();
  432. }
  433. set
  434. {
  435. string t = SerializedType;
  436. if (t == typeof(Color).AssemblyQualifiedName)
  437. {
  438. try
  439. {
  440. this.value = ColorTranslator.FromHtml(value);
  441. }
  442. catch
  443. {
  444. this.value = value;
  445. }
  446. }
  447. else if (t == typeof(Boolean).AssemblyQualifiedName)
  448. this.value = Boolean.Parse(value);
  449. else if (t == typeof(Int32).AssemblyQualifiedName)
  450. this.value = Int32.Parse(value);
  451. else if (t == typeof(Double).AssemblyQualifiedName)
  452. this.value = Double.Parse(value);
  453. else
  454. this.value = value;
  455. }
  456. }
  457. #endregion
  458. #region Methods
  459. /// <summary>
  460. /// Dispatches the PropertyChanged event
  461. /// </summary>
  462. /// <param name="name">The name of the property that was changed</param>
  463. public void OnPropertyChanged(string name)
  464. {
  465. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
  466. }
  467. #endregion
  468. #region Events
  469. /// <summary>
  470. /// Occurs when the property of the item is changed
  471. /// </summary>
  472. public event PropertyChangedEventHandler PropertyChanged;
  473. #endregion
  474. }
  475. #endregion
  476. }