~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Services/File/RecentOpen.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Collections.ObjectModel;
 
7
using System.IO;
 
8
using System.Linq;
 
9
using System.Windows.Shell;
 
10
using ICSharpCode.Core;
 
11
 
 
12
namespace ICSharpCode.SharpDevelop
 
13
{
 
14
        /// <summary>
 
15
        /// This class handles the recent open files and the recent open project files of SharpDevelop
 
16
        /// it checks, if the files exists at every creation, and if not it doesn't list them in the
 
17
        /// recent files, and they'll not be saved during the next option save.
 
18
        /// </summary>
 
19
        public sealed class RecentOpen
 
20
        {
 
21
                /// <summary>
 
22
                /// This variable is the maximal length of lastfile/lastopen entries
 
23
                /// must be > 0
 
24
                /// </summary>
 
25
                int MAX_LENGTH = 10;
 
26
                
 
27
                readonly ObservableCollection<string> lastfile    = new ObservableCollection<string>();
 
28
                readonly ObservableCollection<string> lastproject = new ObservableCollection<string>();
 
29
                
 
30
                public IList<string> RecentFile {
 
31
                        get {
 
32
                                return lastfile;
 
33
                        }
 
34
                }
 
35
 
 
36
                public IList<string> RecentProject {
 
37
                        get {
 
38
                                return lastproject;
 
39
                        }
 
40
                }
 
41
                
 
42
                public RecentOpen()
 
43
                {
 
44
                }
 
45
                
 
46
                public RecentOpen(Properties p)
 
47
                {
 
48
                        // don't check whether files exist because that might be slow (e.g. if file is on network
 
49
                        // drive that's unavailable)
 
50
                        
 
51
                        // if one of these entries is a string, then it's from a previous SharpDevelop version - don't try loading it
 
52
                        if (p.Contains("Files") && !(p.Get("Files") is string)) {
 
53
                                lastfile.AddRange(p.Get("Files", new string[0]));
 
54
                        }
 
55
                        
 
56
                        if (p.Contains("Projects") && !(p.Get("Files") is string)) {
 
57
                                lastproject.AddRange(p.Get("Projects", new string[0]));
 
58
                        }
 
59
                }
 
60
                
 
61
                public void AddLastFile(string name)
 
62
                {
 
63
                        for (int i = 0; i < lastfile.Count; ++i) {
 
64
                                if (lastfile[i].Equals(name, StringComparison.OrdinalIgnoreCase)) {
 
65
                                        lastfile.RemoveAt(i);
 
66
                                }
 
67
                        }
 
68
                        
 
69
                        while (lastfile.Count >= MAX_LENGTH) {
 
70
                                lastfile.RemoveAt(lastfile.Count - 1);
 
71
                        }
 
72
                        
 
73
                        lastfile.Insert(0, name);
 
74
                }
 
75
                
 
76
                public void ClearRecentFiles()
 
77
                {
 
78
                        lastfile.Clear();
 
79
                }
 
80
                
 
81
                public void ClearRecentProjects()
 
82
                {
 
83
                        lastproject.Clear();
 
84
                }
 
85
                
 
86
                public void AddLastProject(string name)
 
87
                {
 
88
                        for (int i = 0; i < lastproject.Count; ++i) {
 
89
                                if (lastproject[i].ToString().Equals(name, StringComparison.OrdinalIgnoreCase)) {
 
90
                                        lastproject.RemoveAt(i);
 
91
                                }
 
92
                        }
 
93
                        
 
94
                        while (lastproject.Count >= MAX_LENGTH) {
 
95
                                lastproject.RemoveAt(lastproject.Count - 1);
 
96
                        }
 
97
                        
 
98
                        lastproject.Insert(0, name);
 
99
                        JumpList.AddToRecentCategory(name);
 
100
                }
 
101
                
 
102
                public static RecentOpen FromXmlElement(Properties properties)
 
103
                {
 
104
                        return new RecentOpen(properties);
 
105
                }
 
106
                
 
107
                public Properties ToProperties()
 
108
                {
 
109
                        Properties p = new Properties();
 
110
                        p.Set("Files", lastfile.ToArray());
 
111
                        p.Set("Projects", lastproject.ToArray());
 
112
                        return p;
 
113
                }
 
114
                
 
115
                internal void FileRemoved(object sender, FileEventArgs e)
 
116
                {
 
117
                        for (int i = 0; i < lastfile.Count; ++i) {
 
118
                                string file = lastfile[i].ToString();
 
119
                                if (e.FileName == file) {
 
120
                                        lastfile.RemoveAt(i);
 
121
                                        break;
 
122
                                }
 
123
                        }
 
124
                }
 
125
                
 
126
                internal void FileRenamed(object sender, FileRenameEventArgs e)
 
127
                {
 
128
                        for (int i = 0; i < lastfile.Count; ++i) {
 
129
                                string file = lastfile[i].ToString();
 
130
                                if (e.SourceFile == file) {
 
131
                                        lastfile.RemoveAt(i);
 
132
                                        lastfile.Insert(i, e.TargetFile);
 
133
                                        break;
 
134
                                }
 
135
                        }
 
136
                }
 
137
        }
 
138
}