~dangarner/xibo/client-132

« back to all changes in this revision

Viewing changes to client/dotNET/RequiredFiles.cs

  • Committer: Dan Garner
  • Date: 2011-02-28 16:05:59 UTC
  • mfrom: (194.14.7 1.2.2-pre)
  • Revision ID: dan@xibo.org.uk-20110228160559-e02vb9em6vyvfsh2
MergedĀ lp:~dangarner/xibo/1.2.2-pre

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) 2011 Daniel Garner
 
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.Collections.ObjectModel;
 
23
using System.Text;
 
24
using System.IO;
 
25
using System.Security.Cryptography;
 
26
using System.Xml;
 
27
using System.Diagnostics;
 
28
using System.Windows.Forms;
 
29
using System.Xml.Serialization;
 
30
 
 
31
namespace XiboClient
 
32
{
 
33
    public class RequiredFiles
 
34
    {
 
35
        private XmlDocument _requiredFilesXml;
 
36
        public Collection<RequiredFile> _requiredFiles;
 
37
        private xmds.xmds _report;
 
38
 
 
39
        public RequiredFiles()
 
40
        {
 
41
            _requiredFiles = new Collection<RequiredFile>();
 
42
 
 
43
            // Create a webservice call
 
44
            _report = new XiboClient.xmds.xmds();
 
45
 
 
46
            // Start up the Xmds Service Object
 
47
            _report.Credentials = null;
 
48
            _report.Url = Properties.Settings.Default.XiboClient_xmds_xmds;
 
49
            _report.UseDefaultCredentials = false;
 
50
        }
 
51
 
 
52
        /// <summary>
 
53
        /// Set required files from the XML document
 
54
        /// </summary>
 
55
        private void SetRequiredFiles()
 
56
        {
 
57
            // Itterate through the RF XML and populate the RF collection
 
58
            XmlNodeList fileNodes = _requiredFilesXml.SelectNodes("/files/file");
 
59
 
 
60
            foreach (XmlNode file in fileNodes)
 
61
            {
 
62
                RequiredFile rf = new RequiredFile(); 
 
63
                
 
64
                XmlAttributeCollection attributes = file.Attributes;
 
65
 
 
66
                rf.FileType = attributes["type"].Value;
 
67
                rf.Complete = 0;
 
68
                rf.Md5 = "";
 
69
                rf.LastChecked = DateTime.Now;
 
70
 
 
71
                if (rf.FileType == "media")
 
72
                {
 
73
                    string[] filePart = attributes["path"].Value.Split('.');
 
74
                    rf.Id = int.Parse(filePart[0]);
 
75
                    rf.Path = attributes["path"].Value;
 
76
                }
 
77
                else if (rf.FileType == "layout")
 
78
                {
 
79
                    rf.Id = int.Parse(attributes["path"].Value);
 
80
                    rf.Path = attributes["path"].Value + ".xlf";
 
81
                }
 
82
                else
 
83
                {
 
84
                    continue;
 
85
                }
 
86
 
 
87
                _requiredFiles.Add(rf);
 
88
            }
 
89
        }
 
90
 
 
91
        /// <summary>
 
92
        /// Required Files XML
 
93
        /// </summary>
 
94
        public XmlDocument RequiredFilesXml
 
95
        {
 
96
            set
 
97
            {
 
98
                _requiredFilesXml = value;
 
99
                SetRequiredFiles();
 
100
            }
 
101
        }
 
102
 
 
103
        /// <summary>
 
104
        /// Mark a RequiredFile as complete
 
105
        /// </summary>
 
106
        /// <param name="id"></param>
 
107
        /// <param name="md5"></param>
 
108
        public void MarkComplete(int id, string md5)
 
109
        {
 
110
            foreach (RequiredFile rf in _requiredFiles)
 
111
            {
 
112
                if (rf.Id == id)
 
113
                {
 
114
                    RequiredFile newRf = rf;
 
115
 
 
116
                    newRf.Complete = 1;
 
117
                    newRf.Md5 = md5;
 
118
 
 
119
 
 
120
                    _requiredFiles.Add(newRf);
 
121
                    _requiredFiles.Remove(rf);
 
122
 
 
123
                    return;
 
124
                }
 
125
            }
 
126
        }
 
127
 
 
128
        /// <summary>
 
129
        /// Mark a RequiredFile as incomplete
 
130
        /// </summary>
 
131
        /// <param name="id"></param>
 
132
        /// <param name="md5"></param>
 
133
        public void MarkIncomplete(int id, string md5)
 
134
        {
 
135
            foreach (RequiredFile rf in _requiredFiles)
 
136
            {
 
137
                if (rf.Id == id)
 
138
                {
 
139
                    RequiredFile newRf = rf;
 
140
 
 
141
                    newRf.Complete = 0;
 
142
                    newRf.Md5 = md5;
 
143
 
 
144
                    _requiredFiles.Add(newRf);
 
145
                    _requiredFiles.Remove(rf);
 
146
 
 
147
                    return;
 
148
                }
 
149
            }
 
150
        }
 
151
 
 
152
        /// <summary>
 
153
        /// Writes Required Files to disk
 
154
        /// </summary>
 
155
        public void WriteRequiredFiles()
 
156
        {
 
157
            Debug.WriteLine(new LogMessage("RequiredFiles - WriteRequiredFiles", "About to Write RequiredFiles"), LogType.Info.ToString());
 
158
 
 
159
            try
 
160
            {
 
161
                using (StreamWriter streamWriter = new StreamWriter(Application.UserAppDataPath + "\\" + Properties.Settings.Default.RequiredFilesFile))
 
162
                {
 
163
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(RequiredFiles));
 
164
 
 
165
                    xmlSerializer.Serialize(streamWriter, this);
 
166
                }
 
167
            }
 
168
            catch (Exception ex)
 
169
            {
 
170
                System.Diagnostics.Trace.WriteLine(new LogMessage("RequiredFiles - WriteRequiredFiles", "Unable to write RequiredFiles to disk because: " + ex.Message));
 
171
            }
 
172
        }
 
173
 
 
174
        /// <summary>
 
175
        /// Report Required Files to XMDS
 
176
        /// </summary>
 
177
        public void ReportInventory()
 
178
        {
 
179
            HardwareKey hardwareKey = new HardwareKey();
 
180
 
 
181
            // Build the XML required by media file
 
182
            string xml = "";
 
183
            
 
184
            foreach (RequiredFile rf in _requiredFiles)
 
185
            {
 
186
                xml += string.Format("<file type=\"{0}\" id=\"{1}\" complete=\"{2}\" lastChecked=\"{3}\" md5=\"{4}\" />", 
 
187
                    rf.FileType, rf.Id.ToString(), rf.Complete.ToString(), rf.LastChecked.ToString(), rf.Md5);
 
188
            }
 
189
 
 
190
            xml = string.Format("<files>{0}</files>", xml);
 
191
 
 
192
            _report.MediaInventoryAsync(Properties.Settings.Default.Version, Properties.Settings.Default.ServerKey,
 
193
                hardwareKey.Key, xml);
 
194
        }
 
195
    }
 
196
 
 
197
    public struct RequiredFile
 
198
    {
 
199
        public string FileType;
 
200
        public int Id;
 
201
        public int Complete;
 
202
        public DateTime LastChecked;
 
203
        public string Md5;
 
204
        public string Path;
 
205
    }
 
206
}