~alexharrington/xibo/pyclient-1.1.0a22

« back to all changes in this revision

Viewing changes to client/dotNET/BlackList.cs

  • Committer: Dan Garner
  • Date: 2008-12-14 14:42:52 UTC
  • mto: (1.1.80 Xibo)
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: mail@dangarner.co.uk-20081214144252-8dosaegtfwvv0dsl
Moved 3rd Party libraries to their own folder.
Updated jQuery to the latest revision and now use jQuery UI instead of individual plugins.

Tabs are not currently working

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Xibo - Digitial Signage - http://www.xibo.org.uk
3
 
 * Copyright (C) 2006,2007,2008 Daniel Garner and James Packer
4
 
 *
5
 
 * This file is part of Xibo.
6
 
 *
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
10
 
 * any later version. 
11
 
 *
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.
16
 
 *
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/>.
19
 
 */
20
 
using System;
21
 
using System.Collections.Generic;
22
 
using System.IO;
23
 
using System.Text;
24
 
using System.Xml;
25
 
using System.Windows.Forms;
26
 
using System.Diagnostics;
27
 
 
28
 
namespace XiboClient
29
 
{
30
 
    class BlackList : IDisposable
31
 
    {
32
 
        private xmds.xmds xmds1;
33
 
        private HardwareKey hardwareKey;
34
 
 
35
 
        private string blackListFile;
36
 
 
37
 
        public BlackList()
38
 
        {
39
 
            // Check that the black list file is available
40
 
            blackListFile = Application.UserAppDataPath + "//" + Properties.Settings.Default.blackListLocation;
41
 
 
42
 
            // Get the key for this display
43
 
            hardwareKey = new HardwareKey();
44
 
        }
45
 
 
46
 
        /// <summary>
47
 
        /// Adds a media item to the Black list. Adds Locally and to the WebService
48
 
        /// </summary>
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)
53
 
        {
54
 
            // Do some validation
55
 
            if (reason == "") reason = "No reason provided";
56
 
            
57
 
            int mediaId;
58
 
            if (!int.TryParse(id, out mediaId))
59
 
            {
60
 
                System.Diagnostics.Trace.WriteLine(String.Format("Currently can only append Integer media types. Id {0}", id), "BlackList - Add");
61
 
            }
62
 
 
63
 
            // Send to the webservice
64
 
            xmds1 = new XiboClient.xmds.xmds();
65
 
            xmds1.BlackListCompleted += new XiboClient.xmds.BlackListCompletedEventHandler(xmds1_BlackListCompleted);
66
 
 
67
 
            xmds1.BlackListAsync(Properties.Settings.Default.ServerKey, hardwareKey.Key, mediaId, type.ToString(), reason, Properties.Settings.Default.Version);
68
 
 
69
 
            // Add to the local list
70
 
            AddLocal(id);
71
 
        }
72
 
 
73
 
        private void xmds1_BlackListCompleted(object sender, XiboClient.xmds.BlackListCompletedEventArgs e)
74
 
        {
75
 
            if (e.Error != null)
76
 
            {
77
 
                System.Diagnostics.Trace.WriteLine("Error sending blacklist", "BlackList - BlackListCompleted");
78
 
            }
79
 
            else
80
 
            {
81
 
                System.Diagnostics.Trace.WriteLine("Blacklist sending complete", "BlackList - BlackListCompleted");
82
 
            }
83
 
 
84
 
            return;
85
 
        }
86
 
 
87
 
        /// <summary>
88
 
        /// Adds the Media Items in the XMLNodeList to the Blacklist (will only add these locally)
89
 
        /// </summary>
90
 
        /// <param name="items">The XMLNodeList containing the blacklist items. Each node must have an "id".</param>
91
 
        public void Add(XmlNodeList items)
92
 
        {
93
 
            Trace.WriteLine(new LogMessage("Blacklist - Add", "Adding XMLNodeList to Blacklist"), LogType.Info.ToString());
94
 
 
95
 
            foreach (XmlNode node in items)
96
 
            {
97
 
                XmlAttributeCollection attributes = node.Attributes;
98
 
 
99
 
                if (attributes["id"].Value != null)
100
 
                {
101
 
                    AddLocal(attributes["id"].Value);
102
 
                }
103
 
            }
104
 
        }
105
 
 
106
 
        /// <summary>
107
 
        /// Adds the Media ID to the local blacklist
108
 
        /// </summary>
109
 
        /// <param name="id">The ID to be blacklisted.</param>
110
 
        private void AddLocal(string id)
111
 
        {
112
 
            try
113
 
            {
114
 
                StreamWriter tw = new StreamWriter(File.Open(blackListFile, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
115
 
 
116
 
                tw.Write(String.Format("[{0}],", id));
117
 
                tw.Close();
118
 
            }
119
 
            catch (Exception ex)
120
 
            {
121
 
                System.Diagnostics.Debug.WriteLine(ex.Message, "Blacklist - Add");
122
 
                System.Diagnostics.Trace.WriteLine(String.Format("Cant add {0} to the blacklist", id));
123
 
            }
124
 
 
125
 
            return;
126
 
        }
127
 
 
128
 
        /// <summary>
129
 
        /// Truncates the local Blacklist
130
 
        /// </summary>
131
 
        public void Truncate()
132
 
        {
133
 
            try
134
 
            {
135
 
                File.Delete(Application.UserAppDataPath + "//" + Properties.Settings.Default.blackListLocation);
136
 
            }
137
 
            catch (Exception ex)
138
 
            {
139
 
                System.Diagnostics.Trace.WriteLine("Cannot truncate the BlackList", "Blacklist - Truncate");
140
 
                System.Diagnostics.Trace.WriteLine(ex.Message);
141
 
            }
142
 
        }
143
 
 
144
 
        /// <summary>
145
 
        /// Checks whether or not a media item is in the blacklist
146
 
        /// </summary>
147
 
        /// <param name="fileId"></param>
148
 
        /// <returns></returns>
149
 
        public Boolean BlackListed(string fileId)
150
 
        {
151
 
            StreamReader sr = null;
152
 
 
153
 
            // Store as an XML Fragment
154
 
            if (!File.Exists(blackListFile))
155
 
            {
156
 
                return false;
157
 
            }
158
 
 
159
 
            try
160
 
            {
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));
163
 
                
164
 
                string listed = sr.ReadToEnd();
165
 
 
166
 
                return listed.Contains(String.Format("[{0}]", fileId));
167
 
            }
168
 
            catch (Exception ex)
169
 
            {
170
 
                System.Diagnostics.Debug.WriteLine(ex.Message, "BlackList - BlackListed");
171
 
            }
172
 
            finally
173
 
            {
174
 
                // Make sure the xr is closed
175
 
                if (sr != null) sr.Close();
176
 
            }
177
 
 
178
 
            return false;
179
 
        }
180
 
 
181
 
        #region IDisposableMethods
182
 
 
183
 
        private Boolean disposed;
184
 
 
185
 
        protected virtual void Dispose(bool disposing)
186
 
        {
187
 
            if (!disposed)
188
 
            {
189
 
                if (disposing)
190
 
                {
191
 
                    // Dispose managed resources.
192
 
                }
193
 
 
194
 
                // There are no unmanaged resources to release, but
195
 
                // if we add them, they need to be released here.
196
 
            }
197
 
            disposed = true;
198
 
        }
199
 
 
200
 
        public void Dispose()
201
 
        {
202
 
            Dispose(true);
203
 
            GC.SuppressFinalize(this);
204
 
        }
205
 
 
206
 
        #endregion
207
 
    }
208
 
 
209
 
    public enum BlackListType { Single, All }
210
 
}