Skip to content

Commit

Permalink
Bug fix for read-only project files. Behavior:
Browse files Browse the repository at this point in the history
- Ask for permission to edit a file
- Notify it will be saved
- Save and notify so that the user to prompted to reload the project.
  • Loading branch information
edumunoz committed Jun 18, 2014
1 parent 976135a commit 58c34cb
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion EditProj/EditProjPackage.cs
Expand Up @@ -15,6 +15,7 @@
using EnvDTE;
using System.Collections.Generic;
using System.Linq;
using EnvDTE80;

namespace rdomunozcom.EditProj
{
Expand Down Expand Up @@ -44,6 +45,9 @@ public sealed class EditProjPackage : Package
private DTE dte;
private IDictionary<string, string> tempToProjFiles;

// Calling the QueryEditFiles method within the body pops up a query dialog. While the dialog is waiting for the user, the shell automatically can call the method checking for dirtiness and that will call CanEditFile again. To avoid recursion we use the _GettingCheckOutStatus flag as a guard
private bool gettingCheckoutStatus = false;

/// <summary>
/// Default constructor of the package.
/// Inside this method you can place any initialization code that does not require
Expand Down Expand Up @@ -222,7 +226,22 @@ private void exitCommand_BeforeExecute(string Guid, int ID, object CustomIn, obj
private void UpdateProjFile(string tempFilePath)
{
string contents = ReadFile(tempFilePath);
SetFileContents(this.tempToProjFiles[tempFilePath], contents);
string projFile = this.tempToProjFiles[tempFilePath];
if (CanEditFile(this.tempToProjFiles[tempFilePath]))
{
NotifyForSave(projFile);
SetFileContents(projFile, contents);
NotifyDocChanged(projFile);
}
}

private void NotifyForSave(string p)
{
int hr;
IVsQueryEditQuerySave2 queryEditQuerySave = (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));
uint result;
hr = queryEditQuerySave.QuerySaveFile(p, 0, null, out result);

}

private static void SetFileContents(string filePath, string content)
Expand All @@ -234,5 +253,50 @@ private static string ReadFile(string filePath)
{
return File.ReadAllText(filePath);
}

private bool CanEditFile(string p)
{
if (gettingCheckoutStatus) return false;

try
{
gettingCheckoutStatus = true;

IVsQueryEditQuerySave2 queryEditQuerySave =
(IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));

string[] documents = { p };
uint result;
uint outFlags;

int hr = queryEditQuerySave.QueryEditFiles(
0,
1,
documents,
null,
null,
out result,
out outFlags);
if (ErrorHandler.Succeeded(hr) && (result ==
(uint)tagVSQueryEditResult.QER_EditOK))
{
return true;
}
}
finally
{
gettingCheckoutStatus = false;
}
return false;
}


private void NotifyDocChanged(string p)
{
IVsFileChangeEx fileChangeEx =
(IVsFileChangeEx)GetService(typeof(SVsFileChangeEx));
int hr;
hr = fileChangeEx.SyncFile(p);
}
}
}

0 comments on commit 58c34cb

Please sign in to comment.