OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Diagnostics; | |
5 using Microsoft.VisualStudio; | |
6 using Microsoft.VisualStudio.Shell.Interop; | |
7 | |
8 namespace Microsoft.VisualStudio.Project | |
9 { | |
10 /// <summary> | |
11 /// Used by a project to query the environment for permission to add, re
move, or rename a file or directory in a solution | |
12 /// </summary> | |
13 internal class TrackDocumentsHelper | |
14 { | |
15 #region fields | |
16 private ProjectNode projectMgr; | |
17 #endregion | |
18 | |
19 #region properties | |
20 | |
21 #endregion | |
22 | |
23 #region ctors | |
24 internal TrackDocumentsHelper(ProjectNode project) | |
25 { | |
26 this.projectMgr = project; | |
27 } | |
28 #endregion | |
29 | |
30 #region helper methods | |
31 /// <summary> | |
32 /// Gets the IVsTrackProjectDocuments2 object by asking the serv
ice provider for it. | |
33 /// </summary> | |
34 /// <returns>the IVsTrackProjectDocuments2 object</returns> | |
35 private IVsTrackProjectDocuments2 GetIVsTrackProjectDocuments2() | |
36 { | |
37 Debug.Assert(this.projectMgr != null && !this.projectMgr
.IsClosed && this.projectMgr.Site != null); | |
38 | |
39 IVsTrackProjectDocuments2 documentTracker = this.project
Mgr.Site.GetService(typeof(SVsTrackProjectDocuments)) as IVsTrackProjectDocument
s2; | |
40 if(documentTracker == null) | |
41 { | |
42 throw new InvalidOperationException(); | |
43 } | |
44 | |
45 return documentTracker; | |
46 } | |
47 | |
48 /// <summary> | |
49 /// Asks the environment for permission to add files. | |
50 /// </summary> | |
51 /// <param name="files">The files to add.</param> | |
52 /// <param name="flags">The VSQUERYADDFILEFLAGS flags associated
to the files added</param> | |
53 /// <returns>true if the file can be added, false if not.</retur
ns> | |
54 internal bool CanAddItems(string[] files, VSQUERYADDFILEFLAGS[]
flags) | |
55 { | |
56 // If we are silent then we assume that the file can be
added, since we do not want to trigger this event. | |
57 if((this.projectMgr.EventTriggeringFlag & ProjectNode.Ev
entTriggering.DoNotTriggerTrackerEvents) != 0) | |
58 { | |
59 return true; | |
60 } | |
61 | |
62 if(files == null || files.Length == 0) | |
63 { | |
64 return false; | |
65 } | |
66 | |
67 int len = files.Length; | |
68 VSQUERYADDFILERESULTS[] summary = new VSQUERYADDFILERESU
LTS[1]; | |
69 ErrorHandler.ThrowOnFailure(this.GetIVsTrackProjectDocum
ents2().OnQueryAddFiles(this.projectMgr, len, files, flags, summary, null)); | |
70 if(summary[0] == VSQUERYADDFILERESULTS.VSQUERYADDFILERES
ULTS_AddNotOK) | |
71 { | |
72 return false; | |
73 } | |
74 | |
75 return true; | |
76 } | |
77 | |
78 /// <summary> | |
79 /// Notify the environment about a file just added | |
80 /// </summary> | |
81 internal void OnItemAdded(string file, VSADDFILEFLAGS flag) | |
82 { | |
83 if((this.projectMgr.EventTriggeringFlag & ProjectNode.Ev
entTriggering.DoNotTriggerTrackerEvents) == 0) | |
84 { | |
85 ErrorHandler.ThrowOnFailure(this.GetIVsTrackProj
ectDocuments2().OnAfterAddFilesEx(this.projectMgr, 1, new string[1] { file }, ne
w VSADDFILEFLAGS[1] { flag })); | |
86 } | |
87 } | |
88 | |
89 /// <summary> | |
90 /// Asks the environment for permission to remove files. | |
91 /// </summary> | |
92 /// <param name="files">an array of files to remove</param> | |
93 /// <param name="flags">The VSQUERYREMOVEFILEFLAGS associated to
the files to be removed.</param> | |
94 /// <returns>true if the files can be removed, false if not.</re
turns> | |
95 internal bool CanRemoveItems(string[] files, VSQUERYREMOVEFILEFL
AGS[] flags) | |
96 { | |
97 // If we are silent then we assume that the file can be
removed, since we do not want to trigger this event. | |
98 if((this.projectMgr.EventTriggeringFlag & ProjectNode.Ev
entTriggering.DoNotTriggerTrackerEvents) != 0) | |
99 { | |
100 return true; | |
101 } | |
102 | |
103 if(files == null || files.Length == 0) | |
104 { | |
105 return false; | |
106 } | |
107 int length = files.Length; | |
108 | |
109 VSQUERYREMOVEFILERESULTS[] summary = new VSQUERYREMOVEFI
LERESULTS[1]; | |
110 | |
111 ErrorHandler.ThrowOnFailure(this.GetIVsTrackProjectDocum
ents2().OnQueryRemoveFiles(this.projectMgr, length, files, flags, summary, null)
); | |
112 if(summary[0] == VSQUERYREMOVEFILERESULTS.VSQUERYREMOVEF
ILERESULTS_RemoveNotOK) | |
113 { | |
114 return false; | |
115 } | |
116 | |
117 return true; | |
118 } | |
119 | |
120 /// <summary> | |
121 /// Notify the environment about a file just removed | |
122 /// </summary> | |
123 internal void OnItemRemoved(string file, VSREMOVEFILEFLAGS flag) | |
124 { | |
125 if((this.projectMgr.EventTriggeringFlag & ProjectNode.Ev
entTriggering.DoNotTriggerTrackerEvents) == 0) | |
126 { | |
127 ErrorHandler.ThrowOnFailure(this.GetIVsTrackProj
ectDocuments2().OnAfterRemoveFiles(this.projectMgr, 1, new string[1] { file }, n
ew VSREMOVEFILEFLAGS[1] { flag })); | |
128 } | |
129 } | |
130 | |
131 /// <summary> | |
132 /// Asks the environment for permission to rename files. | |
133 /// </summary> | |
134 /// <param name="oldFileName">Path to the file to be renamed.</p
aram> | |
135 /// <param name="newFileName">Path to the new file.</param> | |
136 /// <param name="flag">The VSRENAMEFILEFLAGS associated with the
file to be renamed.</param> | |
137 /// <returns>true if the file can be renamed. Otherwise false.</
returns> | |
138 internal bool CanRenameItem(string oldFileName, string newFileNa
me, VSRENAMEFILEFLAGS flag) | |
139 { | |
140 // If we are silent then we assume that the file can be
renamed, since we do not want to trigger this event. | |
141 if((this.projectMgr.EventTriggeringFlag & ProjectNode.Ev
entTriggering.DoNotTriggerTrackerEvents) != 0) | |
142 { | |
143 return true; | |
144 } | |
145 | |
146 int iCanContinue = 0; | |
147 ErrorHandler.ThrowOnFailure(this.GetIVsTrackProjectDocum
ents2().OnQueryRenameFile(this.projectMgr, oldFileName, newFileName, flag, out i
CanContinue)); | |
148 return (iCanContinue != 0); | |
149 } | |
150 | |
151 /// <summary> | |
152 /// Get's called to tell the env that a file was renamed | |
153 /// </summary> | |
154 /// | |
155 internal void OnItemRenamed(string strOldName, string strNewName
, VSRENAMEFILEFLAGS flag) | |
156 { | |
157 if((this.projectMgr.EventTriggeringFlag & ProjectNode.Ev
entTriggering.DoNotTriggerTrackerEvents) == 0) | |
158 { | |
159 ErrorHandler.ThrowOnFailure(this.GetIVsTrackProj
ectDocuments2().OnAfterRenameFile(this.projectMgr, strOldName, strNewName, flag)
); | |
160 } | |
161 } | |
162 #endregion | |
163 } | |
164 } | |
165 | |
OLD | NEW |