-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from fernandreu/feature/context-menu
Add context menu entry for known extensions
- Loading branch information
Showing
4 changed files
with
259 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Reflection; | ||
using Microsoft.Win32; | ||
|
||
namespace OfficeRibbonXEditor.Models | ||
{ | ||
public class FileAssociationHelper | ||
{ | ||
private const string MenuEntryName = "OfficeXRibbonEdit"; | ||
|
||
private readonly string extension; | ||
|
||
public FileAssociationHelper(string extension) | ||
{ | ||
this.extension = extension; | ||
} | ||
|
||
public bool CheckAssociation() | ||
{ | ||
var type = this.GetFileType(); | ||
if (string.IsNullOrEmpty(type)) | ||
{ | ||
return false; | ||
} | ||
|
||
var menuKey = this.GetMenuKey(type, false); | ||
return menuKey != null; | ||
} | ||
|
||
public void RemoveAssociation() | ||
{ | ||
var type = this.GetFileType(); | ||
if (string.IsNullOrEmpty(type)) | ||
{ | ||
return; | ||
} | ||
|
||
Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{type}\shell\{MenuEntryName}"); | ||
} | ||
|
||
public void AddAssociation() | ||
{ | ||
var type = this.GetFileType(); | ||
if (string.IsNullOrEmpty(type)) | ||
{ | ||
return; | ||
} | ||
|
||
var key = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{type}\shell\{MenuEntryName}"); | ||
if (key == null) | ||
{ | ||
// TODO: Throw | ||
return; | ||
} | ||
key.SetValue(null, "Edit with OfficeRibbonXEditor"); | ||
var exePath = Assembly.GetExecutingAssembly().Location; | ||
key.SetValue("Icon", exePath); | ||
var subKey = key.CreateSubKey("command"); | ||
subKey?.SetValue(null, $"\"{exePath}\" \"%1\""); | ||
} | ||
|
||
private string GetFileType() | ||
{ | ||
var key = Registry.ClassesRoot.OpenSubKey(this.extension)?.GetValue(null)?.ToString(); | ||
return key; | ||
} | ||
|
||
private RegistryKey GetMenuKey(string fileType, bool writable = true) | ||
{ | ||
var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{fileType}\shell\{MenuEntryName}", writable); | ||
return key; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/OfficeRibbonXEditor/ViewModels/Shell/FileAssociationViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using GalaSoft.MvvmLight; | ||
using OfficeRibbonXEditor.Models; | ||
|
||
namespace OfficeRibbonXEditor.ViewModels.Shell | ||
{ | ||
public class FileAssociationViewModel : ViewModelBase | ||
{ | ||
public FileAssociationViewModel(string extension) | ||
{ | ||
this.Extension = extension; | ||
this.PreviousValue = GetCurrentValue(); | ||
this.NewValue = this.PreviousValue; | ||
} | ||
|
||
public event EventHandler ValueChanged; | ||
|
||
public string Extension { get; } | ||
|
||
private bool PreviousValue { get; set; } | ||
|
||
private bool newValue; | ||
|
||
public bool NewValue | ||
{ | ||
get => this.newValue; | ||
set | ||
{ | ||
if (!this.Set(ref this.newValue, value)) | ||
{ | ||
return; | ||
} | ||
|
||
this.ValueChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
} | ||
|
||
public bool GetCurrentValue() | ||
{ | ||
return new FileAssociationHelper(this.Extension).CheckAssociation(); | ||
} | ||
|
||
public void Apply() | ||
{ | ||
if (this.PreviousValue == this.NewValue) | ||
{ | ||
return; | ||
} | ||
|
||
if (this.NewValue) | ||
{ | ||
new FileAssociationHelper(this.Extension).AddAssociation(); | ||
} | ||
else | ||
{ | ||
new FileAssociationHelper(this.Extension).RemoveAssociation(); | ||
} | ||
|
||
this.PreviousValue = this.NewValue; | ||
} | ||
|
||
public void ResetToCurrent() | ||
{ | ||
this.NewValue = this.PreviousValue; | ||
} | ||
|
||
public void ResetToDefault() | ||
{ | ||
this.NewValue = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters