-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathClassWorkspace.cs
119 lines (107 loc) · 4.14 KB
/
ClassWorkspace.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace GitForce
{
/// <summary>
/// This class contains the code to load and save workspaces.
/// It also implements a set of last recently used workspace files.
/// </summary>
static class ClassWorkspace
{
/// <summary>
/// Return a set of last recently used workspace files as a list of absolute file names.
/// </summary>
public static List<string> GetLRU()
{
List<string> lru = Properties.Settings.Default.WorkspaceLRU.
Split(("\t").ToCharArray(),
StringSplitOptions.RemoveEmptyEntries).ToList();
return lru;
}
/// <summary>
/// Set a list of recently used workspace files as a list of absolute file names.
/// Calling this method will overwrite currently stored LRU list!
/// </summary>
public static void SetLRU(List<string> lru)
{
string s = string.Join("\t", lru.ToArray());
Properties.Settings.Default.WorkspaceLRU = s;
}
/// <summary>
/// Add a file name to the list of last recently used files.
/// Place it at the top (LRU), even if it already exists.
/// </summary>
private static void AddLRU(string name)
{
List<string> lru = GetLRU();
// If a name already exists, remove it first since we will be re-adding it at the top
lru.Remove(name); // Remove is safe to call even if the item is not in the list
lru.Insert(0, name); // Insert the new name to the top of LRU
// Keep the number of recently used files down to a reasonable value
if (lru.Count >= 6)
lru.RemoveRange(5, lru.Count - 5);
string s = string.Join("\t", lru.ToArray());
Properties.Settings.Default.WorkspaceLRU = s;
}
/// <summary>
/// Import workspace given the file name.
/// If the file name is null, return false.
/// </summary>
public static bool Import(string name)
{
if (name == null)
return false;
App.PrintStatusMessage("Importing workspace: " + name, MessageType.General);
if (App.Repos.Load(name, true)) // Merge loaded repos with the current set
{
AddLRU(name);
return true;
}
App.PrintStatusMessage("Import cancelled. Current workspace file: " + Properties.Settings.Default.WorkspaceFile, MessageType.General);
return false;
}
/// <summary>
/// Load workspace given the workspace file name.
/// </summary>
public static bool Load(string name)
{
App.PrintStatusMessage("Loading workspace: " + name, MessageType.General);
if (App.Repos.Load(name, false)) // Load operation (not merge)
{
AddLRU(name);
Properties.Settings.Default.WorkspaceFile = name;
return true;
}
App.PrintStatusMessage("Load cancelled. Current workspace file: " + Properties.Settings.Default.WorkspaceFile, MessageType.General);
return false;
}
/// <summary>
/// Save workspace given the file name.
/// If the file name is null, save the current workspace.
/// </summary>
public static bool Save(string name)
{
if (name == null)
name = Properties.Settings.Default.WorkspaceFile;
App.PrintStatusMessage("Saving workspace: " + name, MessageType.General);
if (App.Repos.Save(name))
{
AddLRU(name);
Properties.Settings.Default.WorkspaceFile = name;
return true;
}
return false;
}
/// <summary>
/// Helper function that clears current workspace
/// </summary>
public static void Clear()
{
App.Repos.Repos.Clear();
App.Repos.InitAll();
App.DoRefresh();
}
}
}