Filter:   InfoImg
download MainOptions.cs
Language: C#
LOC: 216
Project Info
Sycorax
Server: SourceForge (SVN)
Type: svn
...ax\Sycorax Library\Options\
   MainOptions.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * (c) Sbastien Santoro aka Dereckson, 2006, tous droits rservs
 *
 * Date: 13/07/2006
 * Time: 21:10
 *
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

namespace Sycorax {

    #region enum Id3Version, ReadId3, WriteId3v1, WriteId3v2

    /// <summary>
    /// Gestion des tags id3 en  lecture
    /// </summary>
    public enum ReadId3 {
        //Ne pas lire les tags id3
        No = 0,
        //en tant que source primaire (overwrite path datas)
        AsPrimarySource = 1,
        //en tant que source secondaire (complete path datas)
        AsAlternativeSource = 2,
    }

    /// <summary>
    /// Gestion des tags id3v1 en  criture
    /// </summary>
    public enum WriteId3v1 {
        //Ne pas crire 
        No = 0,
        //Les complter
        Complete = 1,
        //Les craser
        Overwrite = 2
    }

    /// <summary>
    /// Gestion des tags id3v2 en  criture
    /// </summary>
    public enum WriteId3v2 {
        //Ne pas crire 
        No = 0,
        //Les complter
        Complete = 1,
        //Les craser
        Overwrite = 2
    }

    /// <summary>
    /// Tag id3 prioritaire
    /// </summary>
    public enum Id3Version {
        v1 = 1,
        v2 = 2
    }
    #endregion

    /// <summary>
    /// Main Options.
    /// </summary>
    [Serializable()]
    public class MainOptions {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:MainOptions"/> class.
        /// </summary>
        public MainOptions () {
            //Version 0.1 des prfrences
            Version = new Version(0, 1);
        }

        /// <summary>
        /// Sets the defaults options.
        /// </summary>
        public void SetDefaults () {
            connectionString = "Server=localhost;Database=Sycorax;Uid=root;Pwd=;";
            debugMode = true;
            deleteTunesIfOrphans = false;
            extensionsToWatch = new List<string>(new string[14] {
                //Audio files
                ".mp3", ".aac", ".ogg", ".wav", ".wma", ".mpc", ".mp4", ".flac", ".mid", ".m4a",
                //Video files
                ".wmv", ".mpg", ".mpeg", ".avi"
            });
            foldersToWatch = new List<string>();
            mainStorageServer = "mysql";
            priorId3Tag = Id3Version.v2;
            readId3Tags = ReadId3.No;
        }

        #region Srialisation
        /// <summary>
        /// Loads the default options file.
        /// </summary>
        /// <returns>Instance of this options class based on options files</returns>
        public static MainOptions Load () {
            return Load(DefaultOptionsFile);
        }

        /// <summary>
        /// Loads the specified options file.
        /// </summary>
        /// <param name="optionsFile">The options file.</param>
        /// <returns>Instance of this options class based on options files</returns>
        static public MainOptions Load (string optionsFile) {
            if (!File.Exists(optionsFile)) {
                return CreateNewOptionsFile(optionsFile);
            }

            // Create an XmlSerializer to use for retrieving options values
            XmlSerializer mySerializer = new XmlSerializer(typeof(MainOptions));

            // Create a StreamReader to point to the options file
            StreamReader myTextReader = new StreamReader(optionsFile);

            // Create an XmlTextReader to actually read the options.
            XmlTextReader myXmlReader = new XmlTextReader(myTextReader);

            if (mySerializer.CanDeserialize(myXmlReader)) {
                MainOptions options = ((MainOptions)mySerializer.Deserialize(myXmlReader));
                // Close the IO objects we've used.
                myXmlReader.Close();
                myTextReader.Close();
                // Return deserialized result
                return options;
            } else {
                // Close the IO objects we've used.
                myXmlReader.Close();
                myTextReader.Close();
                return CreateNewOptionsFile(optionsFile);
            }
        }

        /// <summary>
        /// Saves the specified options file.
        /// </summary>
        public void Save () {
            Save(DefaultOptionsFile);
        }

        /// <summary>
        /// Creates a new options file.
        /// </summary>
        /// <param name="optionsFile">The filename.</param>
        static public MainOptions CreateNewOptionsFile (string optionsFile) {
            //Le dossier existe-t-il ?
            string folder = Path.GetDirectoryName(MainOptions.DefaultOptionsFile);
            if (!Directory.Exists(folder)) {
                Directory.CreateDirectory(folder);
            }
            //Crons un nouveau fichier 
            //contenant les prfrences par dfaut
            //et enregistrons-le
            MainOptions options = new MainOptions();
            options.SetDefaults();
            options.Save(optionsFile);
            return options;
        }

        /// <summary>
        /// Sauvegarde les prfrences
        /// </summary>
        /// <param name="filename">fichier o les prfrences doivent tre sauvegardes</param>
        public void Save (string filename) {
            // Create a stream writer to overwrite any files currently there,
            // so that the fresh options can be saved.
            StreamWriter myWriter = new StreamWriter(filename);
            // Create an XML Serializer to serialize the object
            XmlSerializer myXmlSerializer = new XmlSerializer(this.GetType());
            // Serialize the current Options object (this) to disk.
            myXmlSerializer.Serialize(myWriter, this);
            // Close the writer.
            myWriter.Flush();
            myWriter.Close();
        }
        #endregion

        private bool debugMode;
        /// <summary>
        /// Gets or sets a value indicating whether debug mode is enabled.
        /// </summary>
        /// <value><c>true</c> if debug mode is enabled; otherwise, <c>false</c>.</value>
        public bool DebugMode {
            get { return debugMode; }
            set { debugMode = value; }
        }

        private bool deleteTunesIfOrphans;
        /// <summary>
        /// Gets or sets a value indicating whether tunes has to be deleted if orphans.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if we've to delete orphans tunes ; otherwise, <c>false</c>.
        /// </value>
        public bool DeleteTunesIfOrphans {
            get { return deleteTunesIfOrphans; }
            set { deleteTunesIfOrphans = value; }
        }

        

        #region Databases stuff
        /// <summary>
        /// Storage Engine
        /// </summary>
        /// <remarks>Not an enum to be more evolutive</remarks>
        private string mainStorageServer;
        public string  MainStorageServer {
            get {
                return mainStorageServer;
            }
            set {
                mainStorageServer = value;
            }
        }

        private string connectionString;
        /// <summary>
        /// Gets or sets the database connection string.
        /// </summary>
        /// <value>The database connection string.</value>
        public string ConnectionString {
            get {
                return connectionString;
            }
            set {
                connectionString = value;
            }
        }
        #endregion

        #region Proprits Id3
        private ReadId3 readId3Tags;
        /// <summary>
        /// Ecrire les tag id3v2 ?
        /// </summary>
        public ReadId3 ReadId3Tags {
            get {
                return readId3Tags;
            }
            set {
                readId3Tags = value;
            }
        }


        private WriteId3v1 writeId3v1Tags;
        /// <summary>
        /// Ecrire les tag id3v2 ?
        /// </summary>
        public WriteId3v1 WriteId3v1Tags {
            get {
                return writeId3v1Tags;
            }
            set {
                writeId3v1Tags = value;
            }
        }


        private WriteId3v2 writeId3v2Tags;
        /// <summary>
        /// Ecrire les tag id3v2 ?
        /// </summary>
        public WriteId3v2 WriteId3v2Tags {
            get {
                return writeId3v2Tags;
            }
            set {
                writeId3v2Tags = value;
            }
        }

        private Id3Version priorId3Tag;
        /// <summary>
        /// Lequel des deux tags a la priorit ?
        /// </summary>
        public Id3Version PriorId3Tag {
            get {
                return priorId3Tag;
            }
            set {
                priorId3Tag = value;
            }
        }
        #endregion

        #region Listes des dossiers sous surveillance (FoldersToWatch, AddFolder, DelFolder)
        private List<string> foldersToWatch;
        /// <summary>
        /// Gets or sets the folders to watch.
        /// </summary>
        /// <value>The folders to watch.</value>
        public string[] FoldersToWatch {
            get {
                return foldersToWatch.ToArray();
            }
            set {
                foldersToWatch = new List<string>(value);
            }
        }

        
        /// <summary>
        /// Adds a folder to the watch list.
        /// </summary>
        /// <param name="path">The folder path.</param>
        public void AddFolder (string path) {
            if (!foldersToWatch.Contains(path)) {
                foldersToWatch.Add(path);
            }
        }

        /// <summary>
        /// Dels the folder from the watch list
        /// </summary>
        /// <param name="path">The folder path.</param>
        public void DelFolder (string path) {
            if (foldersToWatch.Contains(path)) {
                foldersToWatch.Remove(path);
            }
        }
        #endregion

        #region Extensions to watch
        private List<string> extensionsToWatch;
        /// <summary>
        /// Gets or sets the extensions to watch.
        /// </summary>
        /// <value>The extensions to watch.</value>
        public string[] ExtensionsToWatch {
            get {
                return extensionsToWatch.ToArray();
            }
            set {
                extensionsToWatch = new List<string>(value);
            }
        }

        /// <summary>
        /// Determines whether the specified extension is watched.
        /// </summary>
        /// <param name="extension">The extension to check.</param>
        /// <returns>
        /// 	<c>true</c> if the specified extension is watched ; otherwise, <c>false</c>.
        /// </returns>
        public bool isExtensionWatched (string extension) {
            try {
                return extensionsToWatch.Contains(extension);
            } catch {
                return false;
            }           
        }

        /// <summary>
        /// Adds a folder to the watch list.
        /// </summary>
        /// <param name="path">The folder path.</param>
        public void AddExtension (string path) {
            if (!foldersToWatch.Contains(path)) {
                extensionsToWatch.Add(path);
            }
        }

        /// <summary>
        /// Dels the folder from the watch list
        /// </summary>
        /// <param name="path">The folder path.</param>
        public void DelExtension (string path) {
            if (extensionsToWatch.Contains(path)) {
                extensionsToWatch.Remove(path);
            }
        }
        #endregion
 
        /// <summary>
        /// Gets the default options file.
        /// </summary>
        /// <value>The default options file.</value>
        static public string DefaultOptionsFile {
            get {
                return Path.Combine(
                    //document and settings > <user profile> > local settings > application data > Sycorax
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    @"Sycorax\SycoraxAutoUpdate.config"
                );
            }
        }

        /// <summary>
        /// Version des options
        /// (utilis pour complter la dsralisation par les nouvelles valeurs par dfaut)
        /// </summary>
        private Version Version;
    }
}