Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RecentOpen.cs « MonoDevelop.Ide.Desktop « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ad57a0cfaf9f61af004fe3b0e47d66be7ee79ab (plain)
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
//
// RecentOpen.cs
//
// Author:
//   Mike Krüger <mkrueger@novell.com>
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

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

using MonoDevelop.Core;
using System.Linq;

namespace MonoDevelop.Ide.Desktop
{
	public class FdoRecentFiles : RecentFiles, IDisposable
	{
		RecentFileStorage recentFiles;
		
		const string projGroup = "MonoDevelop Projects";
		const string fileGroup = "MonoDevelop Files";
		
		const int ItemLimit = 25;
		
		public FdoRecentFiles () : this (RecentFileStorage.DefaultPath)
		{
		}
		
		public FdoRecentFiles (string storageFile)
		{
			recentFiles = new RecentFileStorage (storageFile);
			recentFiles.RemoveMissingFiles (projGroup, fileGroup);
		}
		
		public override event EventHandler Changed {
			add { recentFiles.RecentFilesChanged += value; }
			remove { recentFiles.RecentFilesChanged -= value; }
		}
		
		protected override IList<RecentFile> OnGetProjects ()
		{
			try {
				return Get (projGroup);
			} catch (Exception e) {
				LoggingService.LogError ("Can't get recent projects list.", e);
				return new List <RecentFile> ();
			}
		}
		
		protected override IList<RecentFile> OnGetFiles ()
		{
			try {
				return Get (fileGroup);
			} catch (Exception e) {
				LoggingService.LogError ("Can't get recent files list.", e);
				return new List <RecentFile> ();
			}
		}
		
		IList<RecentFile> Get (string grp)
		{
			var gp = recentFiles.GetItemsInGroup (grp);
			return gp.Select (i => new RecentFile (i.LocalPath, i.Private, i.Timestamp)).ToList ();
		}
		
		public override void ClearProjects ()
		{
			try {
				recentFiles.ClearGroup (projGroup);
			} catch (Exception e) {
				LoggingService.LogError ("Can't clear recent projects list.", e);
			}
		}
		
		public override void ClearFiles ()
		{
			try {
				recentFiles.ClearGroup (fileGroup);
			} catch (Exception e) {
				LoggingService.LogError ("Can't get recent files list.", e);
			}
		}
		
		public override void AddFile (string fileName, string displayName)
		{
			Add (fileGroup, fileName, displayName);
		}
		
		public override void AddProject (string fileName, string displayName)
		{
			Add (projGroup, fileName, displayName);
		}
		
		void Add (string grp, string fileName, string displayName)
		{
			var mime = DesktopService.GetMimeTypeForUri (fileName);
			System.Threading.ThreadPool.QueueUserWorkItem (_ => {
				try {
					var uri = RecentFileStorage.ToUri (fileName);
					var recentItem = new RecentItem (uri, mime, grp) { Private = displayName };
					recentFiles.AddWithLimit (recentItem, grp, ItemLimit);
				} catch (Exception e) {
					LoggingService.LogError ("Failed to add item to recent files list.", e);
				}
			});
		}
		
		public override void NotifyFileRemoved (string fileName)
		{
			System.Threading.ThreadPool.QueueUserWorkItem (_ => {
				try {
					recentFiles.RemoveItem (RecentFileStorage.ToUri (fileName));
				} catch (Exception e) {
					LoggingService.LogError ("Can't remove from recent files list.", e);
				}
			});
		}
		
		public override void NotifyFileRenamed (string oldName, string newName)
		{
			System.Threading.ThreadPool.QueueUserWorkItem (_ => {
				try {
					recentFiles.RenameItem (RecentFileStorage.ToUri (oldName), RecentFileStorage.ToUri (newName));
				} catch (Exception e) {
					LoggingService.LogError ("Can't rename file in recent files list.", e);
				}
			});
		}
		
		public void Dispose ()
		{
			recentFiles.Dispose ();
			recentFiles = null;
		}
	}
		
	public abstract class RecentFiles
	{
		List<string> favoriteFiles;
		const string FavoritesConfigKey = "FavoriteRecentFiles";

		public RecentFiles ()
		{
			favoriteFiles = PropertyService.Get (FavoritesConfigKey, new List<string> ());
		}

		public IList<RecentFile> GetProjects ()
		{
			var projects = OnGetProjects ();
			List<RecentFile> result = new List<RecentFile> ();
			List<string> toRemove = null;
			foreach (var f in favoriteFiles) {
				if (!File.Exists (f)) {
					if (toRemove == null)
						toRemove = new List<string> ();
					toRemove.Add (f);
					continue;
				}
				var entry = projects.FirstOrDefault (p => f == p.FileName);
				if (entry != null)
					result.Add (entry);
				else
					result.Add (new RecentFile (f, Path.GetFileNameWithoutExtension (f), DateTime.Now));
			}
			if (toRemove != null) {
				foreach (var f in toRemove)
					favoriteFiles.Remove (f);
			}
			foreach (var e in projects)
				if (!result.Contains (e))
					result.Add (e);
			return result;
		}

		public IList<RecentFile> GetFiles ()
		{
			return OnGetFiles ();
		}

		public abstract event EventHandler Changed;
		public abstract void ClearProjects ();
		public abstract void ClearFiles ();
		public abstract void AddFile (string fileName, string displayName);
		public abstract void AddProject (string fileName, string displayName);
		public abstract void NotifyFileRemoved (string filename);
		public abstract void NotifyFileRenamed (string oldName, string newName);
		
		protected abstract IList<RecentFile> OnGetProjects ();
		protected abstract IList<RecentFile> OnGetFiles ();

		public void AddFile (string fileName, MonoDevelop.Projects.Project project)
		{
			var projectName = project != null? project.Name : null;
			var displayName = projectName != null?
				string.Format ("{0} [{1}]", Path.GetFileName (fileName), projectName) 
				: Path.GetFileName (fileName);
			AddFile (fileName, displayName);
		}

		internal void SetFavoriteFile (FilePath file, bool favorite)
		{
			if (favorite)
				favoriteFiles.Add (file);
			else
				favoriteFiles.Remove (file);

			PropertyService.Set (FavoritesConfigKey, favoriteFiles);
			PropertyService.SaveProperties ();
		}

		internal bool IsFavoriteFile (FilePath file)
		{
			return favoriteFiles.Contains (file);
		}
	}
	
	public class RecentFile
	{
		string displayName, fileName;
		DateTime timestamp;

		public RecentFile (string fileName, string displayName, DateTime timestamp)
		{
			this.fileName = fileName;
			this.displayName = displayName;
			this.timestamp = timestamp;
		}

		public bool IsFavorite {
			get {
				return DesktopService.RecentFiles.IsFavoriteFile (fileName);
			}
			set {
				DesktopService.RecentFiles.SetFavoriteFile (fileName, value);
			}
		}

		public string FileName { get { return fileName; } }
		public string DisplayName {
			get {
				return string.IsNullOrEmpty (displayName)? Path.GetFileName (fileName) : displayName;
			}
		}
		
		public DateTime TimeStamp { get { return timestamp; } }
		
		public override string ToString ()
		{
			return FileName;
		}
	}
}