2
* Xibo - Digitial Signage - http://www.xibo.org.uk
3
* Copyright (C) 2006,2007,2008 Daniel Garner and James Packer
5
* This file is part of Xibo.
7
* Xibo is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU Affero General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
12
* Xibo 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 Affero General Public License for more details.
17
* You should have received a copy of the GNU Affero General Public License
18
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
21
using System.Collections.Generic;
25
using System.Windows.Forms;
26
using System.Diagnostics;
30
class BlackList : IDisposable
32
private xmds.xmds xmds1;
33
private HardwareKey hardwareKey;
35
private string blackListFile;
39
// Check that the black list file is available
40
blackListFile = Application.UserAppDataPath + "//" + Properties.Settings.Default.blackListLocation;
42
// Get the key for this display
43
hardwareKey = new HardwareKey();
47
/// Adds a media item to the Black list. Adds Locally and to the WebService
49
/// <param name="id">The Media ID</param>
50
/// <param name="type">The BlackListType, either All (to blacklist on all displays) or Single (to blacklist only on this display)</param>
51
/// <param name="reason">The reason for the blacklist</param>
52
public void Add(string id, BlackListType type, string reason)
55
if (reason == "") reason = "No reason provided";
58
if (!int.TryParse(id, out mediaId))
60
System.Diagnostics.Trace.WriteLine(String.Format("Currently can only append Integer media types. Id {0}", id), "BlackList - Add");
63
// Send to the webservice
64
xmds1 = new XiboClient.xmds.xmds();
65
xmds1.BlackListCompleted += new XiboClient.xmds.BlackListCompletedEventHandler(xmds1_BlackListCompleted);
67
xmds1.BlackListAsync(Properties.Settings.Default.ServerKey, hardwareKey.Key, mediaId, type.ToString(), reason, Properties.Settings.Default.Version);
69
// Add to the local list
73
private void xmds1_BlackListCompleted(object sender, XiboClient.xmds.BlackListCompletedEventArgs e)
77
System.Diagnostics.Trace.WriteLine("Error sending blacklist", "BlackList - BlackListCompleted");
81
System.Diagnostics.Trace.WriteLine("Blacklist sending complete", "BlackList - BlackListCompleted");
88
/// Adds the Media Items in the XMLNodeList to the Blacklist (will only add these locally)
90
/// <param name="items">The XMLNodeList containing the blacklist items. Each node must have an "id".</param>
91
public void Add(XmlNodeList items)
93
Trace.WriteLine(new LogMessage("Blacklist - Add", "Adding XMLNodeList to Blacklist"), LogType.Info.ToString());
95
foreach (XmlNode node in items)
97
XmlAttributeCollection attributes = node.Attributes;
99
if (attributes["id"].Value != null)
101
AddLocal(attributes["id"].Value);
107
/// Adds the Media ID to the local blacklist
109
/// <param name="id">The ID to be blacklisted.</param>
110
private void AddLocal(string id)
114
StreamWriter tw = new StreamWriter(File.Open(blackListFile, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
116
tw.Write(String.Format("[{0}],", id));
121
System.Diagnostics.Debug.WriteLine(ex.Message, "Blacklist - Add");
122
System.Diagnostics.Trace.WriteLine(String.Format("Cant add {0} to the blacklist", id));
129
/// Truncates the local Blacklist
131
public void Truncate()
135
File.Delete(Application.UserAppDataPath + "//" + Properties.Settings.Default.blackListLocation);
139
System.Diagnostics.Trace.WriteLine("Cannot truncate the BlackList", "Blacklist - Truncate");
140
System.Diagnostics.Trace.WriteLine(ex.Message);
145
/// Checks whether or not a media item is in the blacklist
147
/// <param name="fileId"></param>
148
/// <returns></returns>
149
public Boolean BlackListed(string fileId)
151
StreamReader sr = null;
153
// Store as an XML Fragment
154
if (!File.Exists(blackListFile))
161
// Use an XML Text Reader to grab the shiv from the black list location.
162
sr = new StreamReader(File.Open(blackListFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
164
string listed = sr.ReadToEnd();
166
return listed.Contains(String.Format("[{0}]", fileId));
170
System.Diagnostics.Debug.WriteLine(ex.Message, "BlackList - BlackListed");
174
// Make sure the xr is closed
175
if (sr != null) sr.Close();
181
#region IDisposableMethods
183
private Boolean disposed;
185
protected virtual void Dispose(bool disposing)
191
// Dispose managed resources.
194
// There are no unmanaged resources to release, but
195
// if we add them, they need to be released here.
200
public void Dispose()
203
GC.SuppressFinalize(this);
209
public enum BlackListType { Single, All }