~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Database/AddinScanFolderInfo.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// AddinScanFolderInfo.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.IO;
 
32
using System.Collections;
 
33
using System.Collections.Specialized;
 
34
using Mono.Addins.Serialization;
 
35
 
 
36
namespace Mono.Addins.Database
 
37
{
 
38
        class AddinScanFolderInfo: IBinaryXmlElement
 
39
        {
 
40
                Hashtable files = new Hashtable ();
 
41
                string folder;
 
42
                string fileName;
 
43
                string domain;
 
44
                bool sharedFolder = true;
 
45
                
 
46
                static BinaryXmlTypeMap typeMap = new BinaryXmlTypeMap (
 
47
                        typeof(AddinScanFolderInfo),
 
48
                        typeof(AddinFileInfo)
 
49
                );
 
50
                
 
51
                internal AddinScanFolderInfo ()
 
52
                {
 
53
                }
 
54
                
 
55
                public AddinScanFolderInfo (string folder)
 
56
                {
 
57
                        this.folder = folder;
 
58
                }
 
59
                
 
60
                public string FileName {
 
61
                        get { return fileName; }
 
62
                }
 
63
                
 
64
                public static AddinScanFolderInfo Read (FileDatabase filedb, string file)
 
65
                {
 
66
                        AddinScanFolderInfo finfo = (AddinScanFolderInfo) filedb.ReadSharedObject (file, typeMap);
 
67
                        if (finfo != null)
 
68
                                finfo.fileName = file;
 
69
                        return finfo;
 
70
                }
 
71
                
 
72
                public static AddinScanFolderInfo Read (FileDatabase filedb, string basePath, string folderPath)
 
73
                {
 
74
                        string fileName;
 
75
                        AddinScanFolderInfo finfo = (AddinScanFolderInfo) filedb.ReadSharedObject (basePath, GetDomain (folderPath), ".data", Path.GetFullPath (folderPath), typeMap, out fileName);
 
76
                        if (finfo != null)
 
77
                                finfo.fileName = fileName;
 
78
                        return finfo;
 
79
                }
 
80
                
 
81
                static string GetDomain (string path)
 
82
                {
 
83
                        path = Path.GetFullPath (path);
 
84
                        string s = path.Replace (Path.DirectorySeparatorChar, '_');
 
85
                        s = s.Replace (Path.AltDirectorySeparatorChar, '_');
 
86
                        s = s.Replace (Path.VolumeSeparatorChar, '_');
 
87
                        s = s.Trim ('_');
 
88
                        return s;
 
89
                }
 
90
                
 
91
                public void Write (FileDatabase filedb, string basePath)
 
92
                {
 
93
                        filedb.WriteSharedObject (basePath, GetDomain (folder), ".data", Path.GetFullPath (folder), fileName, typeMap, this);
 
94
                }
 
95
                
 
96
                public string GetExistingLocalDomain ()
 
97
                {
 
98
                        foreach (AddinFileInfo info in files.Values) {
 
99
                                if (info.Domain != null && info.Domain != AddinDatabase.GlobalDomain)
 
100
                                        return info.Domain;
 
101
                        }
 
102
                        return AddinDatabase.GlobalDomain;
 
103
                }
 
104
                
 
105
                public string Folder {
 
106
                        get { return folder; }
 
107
                }
 
108
 
 
109
                public string Domain {
 
110
                        get {
 
111
                                if (sharedFolder)
 
112
                                        return AddinDatabase.GlobalDomain;
 
113
                                else
 
114
                                        return domain;
 
115
                        }
 
116
                        set {
 
117
                                domain = value;
 
118
                                sharedFolder = true;
 
119
                        }
 
120
                }
 
121
                
 
122
                public string RootsDomain {
 
123
                        get {
 
124
                                return domain;
 
125
                        }
 
126
                        set {
 
127
                                domain = value;
 
128
                        }
 
129
                }
 
130
                
 
131
                public string GetDomain (bool isRoot)
 
132
                {
 
133
                        if (isRoot)
 
134
                                return RootsDomain;
 
135
                        else
 
136
                                return Domain;
 
137
                }
 
138
                
 
139
                public bool SharedFolder {
 
140
                        get {
 
141
                                return sharedFolder;
 
142
                        }
 
143
                        set {
 
144
                                sharedFolder = value;
 
145
                        }
 
146
                }
 
147
                
 
148
                public DateTime GetLastScanTime (string file)
 
149
                {
 
150
                        AddinFileInfo info = (AddinFileInfo) files [file];
 
151
                        if (info == null)
 
152
                                return DateTime.MinValue;
 
153
                        else
 
154
                                return info.LastScan;
 
155
                }
 
156
                
 
157
                public AddinFileInfo GetAddinFileInfo (string file)
 
158
                {
 
159
                        return (AddinFileInfo) files [file];
 
160
                }
 
161
                
 
162
                public AddinFileInfo SetLastScanTime (string file, string addinId, bool isRoot, DateTime time, bool scanError)
 
163
                {
 
164
                        AddinFileInfo info = (AddinFileInfo) files [file];
 
165
                        if (info == null) {
 
166
                                info = new AddinFileInfo ();
 
167
                                info.File = file;
 
168
                                files [file] = info;
 
169
                        }
 
170
                        info.LastScan = time;
 
171
                        info.AddinId = addinId;
 
172
                        info.IsRoot = isRoot;
 
173
                        info.ScanError = scanError;
 
174
                        if (addinId != null)
 
175
                                info.Domain = GetDomain (isRoot);
 
176
                        else
 
177
                                info.Domain = null;
 
178
                        return info;
 
179
                }
 
180
                
 
181
                public ArrayList GetMissingAddins (AddinFileSystemExtension fs)
 
182
                {
 
183
                        ArrayList missing = new ArrayList ();
 
184
                        
 
185
                        if (!fs.DirectoryExists (folder)) {
 
186
                                // All deleted
 
187
                                foreach (AddinFileInfo info in files.Values) {
 
188
                                        if (info.IsAddin)
 
189
                                                missing.Add (info);
 
190
                                }
 
191
                                files.Clear ();
 
192
                                return missing;
 
193
                        }
 
194
                        ArrayList toDelete = new ArrayList ();
 
195
                        foreach (AddinFileInfo info in files.Values) {
 
196
                                if (!fs.FileExists (info.File)) {
 
197
                                        if (info.IsAddin)
 
198
                                                missing.Add (info);
 
199
                                        toDelete.Add (info.File);
 
200
                                }
 
201
                                else if (info.IsAddin && info.Domain != GetDomain (info.IsRoot)) {
 
202
                                        missing.Add (info);
 
203
                                }
 
204
                        }
 
205
                        foreach (string file in toDelete)
 
206
                                files.Remove (file);
 
207
                                
 
208
                        return missing;
 
209
                }
 
210
                
 
211
                void IBinaryXmlElement.Write (BinaryXmlWriter writer)
 
212
                {
 
213
                        if (files.Count == 0) {
 
214
                                domain = null;
 
215
                                sharedFolder = true;
 
216
                        }
 
217
                        writer.WriteValue ("folder", folder);
 
218
                        writer.WriteValue ("files", files);
 
219
                        writer.WriteValue ("domain", domain);
 
220
                        writer.WriteValue ("sharedFolder", sharedFolder);
 
221
                }
 
222
                
 
223
                void IBinaryXmlElement.Read (BinaryXmlReader reader)
 
224
                {
 
225
                        folder = reader.ReadStringValue ("folder");
 
226
                        reader.ReadValue ("files", files);
 
227
                        domain = reader.ReadStringValue ("domain");
 
228
                        sharedFolder = reader.ReadBooleanValue ("sharedFolder");
 
229
                }
 
230
        }
 
231
        
 
232
        
 
233
        class AddinFileInfo: IBinaryXmlElement
 
234
        {
 
235
                public string File;
 
236
                public DateTime LastScan;
 
237
                public string AddinId;
 
238
                public bool IsRoot;
 
239
                public bool ScanError;
 
240
                public string Domain;
 
241
                public StringCollection IgnorePaths;
 
242
                
 
243
                public bool IsAddin {
 
244
                        get { return AddinId != null && AddinId.Length != 0; }
 
245
                }
 
246
                
 
247
                public void AddPathToIgnore (string path)
 
248
                {
 
249
                        if (IgnorePaths == null)
 
250
                                IgnorePaths = new StringCollection ();
 
251
                        IgnorePaths.Add (path);
 
252
                }
 
253
                
 
254
                void IBinaryXmlElement.Write (BinaryXmlWriter writer)
 
255
                {
 
256
                        writer.WriteValue ("File", File);
 
257
                        writer.WriteValue ("LastScan", LastScan);
 
258
                        writer.WriteValue ("AddinId", AddinId);
 
259
                        writer.WriteValue ("IsRoot", IsRoot);
 
260
                        writer.WriteValue ("ScanError", ScanError);
 
261
                        writer.WriteValue ("Domain", Domain);
 
262
                        if (IgnorePaths != null && IgnorePaths.Count > 0)
 
263
                                writer.WriteValue ("IgnorePaths", IgnorePaths);
 
264
                }
 
265
                
 
266
                void IBinaryXmlElement.Read (BinaryXmlReader reader)
 
267
                {
 
268
                        File = reader.ReadStringValue ("File");
 
269
                        LastScan = reader.ReadDateTimeValue ("LastScan");
 
270
                        AddinId = reader.ReadStringValue ("AddinId");
 
271
                        IsRoot = reader.ReadBooleanValue ("IsRoot");
 
272
                        ScanError = reader.ReadBooleanValue ("ScanError");
 
273
                        Domain = reader.ReadStringValue ("Domain");
 
274
                        IgnorePaths = (StringCollection) reader.ReadValue ("IgnorePaths", new StringCollection ());
 
275
                }
 
276
        }
 
277
}