2015-06-17 18:15:02 +00:00
|
|
|
|
/* thisaddin.cs
|
|
|
|
|
* part of zaaReloaded2
|
|
|
|
|
*
|
2017-02-23 15:44:07 +00:00
|
|
|
|
* Copyright 2015-2017 Daniel Kraus
|
2015-06-17 18:15:02 +00:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
2015-06-28 07:44:33 +00:00
|
|
|
|
using System.IO;
|
2015-07-27 08:10:18 +00:00
|
|
|
|
using Bovender.Versioning;
|
|
|
|
|
using Bovender.Mvvm.Messaging;
|
2017-03-02 14:27:37 +00:00
|
|
|
|
using Bovender.Extensions;
|
2015-07-27 20:14:18 +00:00
|
|
|
|
using zaaReloaded2.ExceptionHandler;
|
2015-08-31 10:28:50 +00:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Media;
|
2016-09-03 21:18:43 +00:00
|
|
|
|
using System.Configuration;
|
2015-06-15 17:15:52 +00:00
|
|
|
|
|
|
|
|
|
namespace zaaReloaded2
|
|
|
|
|
{
|
|
|
|
|
public partial class ThisAddIn
|
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
#region Static property
|
|
|
|
|
|
2015-06-28 07:44:33 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the subdirectory for addin data in the user profile directory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string Subdir
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(
|
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
|
|
|
|
"zaaReloaded2");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 21:18:43 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Start up and shut down
|
|
|
|
|
|
2015-06-15 17:15:52 +00:00
|
|
|
|
private void ThisAddIn_Startup(object sender, System.EventArgs e)
|
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
Bovender.Logging.LogFile.Default.EnableDebugLogging();
|
|
|
|
|
#endif
|
|
|
|
|
try
|
2015-10-28 07:17:10 +00:00
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
|
|
|
|
|
if (System.IO.File.Exists(config.FilePath))
|
|
|
|
|
{
|
|
|
|
|
System.IO.File.Delete(config.FilePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("ThisAddIn_Startup: Unable to delete user config file");
|
|
|
|
|
Logger.Warn(ex);
|
2015-10-28 07:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Bovender.ExceptionHandler.CentralHandler.ManageExceptionCallback += CentralHandler_ManageExceptionCallback;
|
2016-10-03 14:09:03 +00:00
|
|
|
|
Bovender.Win32Window.MainWindowHandleProvider = () =>
|
|
|
|
|
{
|
2017-03-02 14:27:37 +00:00
|
|
|
|
return System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
|
|
|
|
|
// return IntPtr.Zero;
|
2016-10-03 14:09:03 +00:00
|
|
|
|
};
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Bovender.WpfHelpers.RegisterTextBoxSelectAll();
|
|
|
|
|
UserSettings userSettings = UserSettings.Default;
|
|
|
|
|
|
2015-07-28 11:25:17 +00:00
|
|
|
|
CheckForUpdates();
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Microsoft.Office.Interop.Word.Application word = Globals.ThisAddIn.Application;
|
|
|
|
|
_oldCaption = word.Caption;
|
|
|
|
|
word.Caption =
|
2015-08-18 07:00:26 +00:00
|
|
|
|
String.Format(
|
|
|
|
|
"{0} ({1} {2})",
|
|
|
|
|
_oldCaption,
|
|
|
|
|
Properties.Settings.Default.AddinName,
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Updater.Version.Current.ToString()
|
2015-08-18 07:00:26 +00:00
|
|
|
|
);
|
2016-09-03 21:18:43 +00:00
|
|
|
|
|
2015-09-05 17:47:58 +00:00
|
|
|
|
ViewModels.FirstRunViewModel.InjectIntoIfNeeded<Views.FirstRunView>();
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Logger.Info("ThisAddIn_Startup: Finished startup");
|
2015-06-15 17:15:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
|
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
UserSettings.Default.LastVersionSeen = Updater.Version.Current.ToString();
|
|
|
|
|
UserSettings.Default.Save();
|
|
|
|
|
|
|
|
|
|
if (_updaterVM != null && _updaterVM.InstallCommand.CanExecute(null))
|
2015-07-27 08:10:18 +00:00
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Logger.Info("ThisAddIn_Shutdown: Installing update");
|
|
|
|
|
_updaterVM.InstallCommand.Execute(null);
|
2015-07-27 08:10:18 +00:00
|
|
|
|
}
|
2015-08-18 07:00:26 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Logger.Info("ThisAddIn_Shutdown: Resetting application caption");
|
2015-08-18 07:00:26 +00:00
|
|
|
|
Globals.ThisAddIn.Application.Caption = _oldCaption;
|
|
|
|
|
}
|
2016-09-03 21:18:43 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("ThisAddIn_Shutdown: Failed to reset application caption");
|
|
|
|
|
Logger.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
Logger.Info("ThisAddIn_Shutdown: Finished shutdown");
|
2015-06-15 17:15:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 21:18:43 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2015-07-26 12:50:05 +00:00
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
public Ribbon Ribbon
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_ribbon == null)
|
|
|
|
|
{
|
|
|
|
|
_ribbon = new Ribbon();
|
|
|
|
|
}
|
|
|
|
|
return _ribbon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Ribbon
|
|
|
|
|
|
|
|
|
|
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
|
|
|
|
|
{
|
|
|
|
|
return Ribbon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2015-07-27 08:10:18 +00:00
|
|
|
|
#region Updater
|
|
|
|
|
|
|
|
|
|
void CheckForUpdates()
|
|
|
|
|
{
|
2016-09-03 21:18:43 +00:00
|
|
|
|
// Check only once per day
|
|
|
|
|
if (DateTime.Today == UserSettings.Default.LastUpdateCheck.Date)
|
2015-07-27 08:10:18 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2016-09-03 21:18:43 +00:00
|
|
|
|
Logger.Info("CheckForUpdates");
|
|
|
|
|
UserSettings.Default.LastUpdateCheck = DateTime.Today;
|
|
|
|
|
ReleaseInfo releaseInfo = new ReleaseInfo(new Uri(Properties.Settings.Default.VersionInfoFile));
|
|
|
|
|
ReleaseInfoViewModel releaseInfoVM = new ReleaseInfoViewModel(releaseInfo, Updater.Version.Current);
|
|
|
|
|
releaseInfoVM.UpdateAvailableMessage.Sent += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("CheckForUpdates: Received update-available message");
|
|
|
|
|
Updater.Updater updater = Updater.Updater.CreateDefault(releaseInfo);
|
|
|
|
|
updater.DestinationFolder = System.IO.Path.GetTempPath();
|
|
|
|
|
_updaterVM = new UpdaterViewModel(updater);
|
|
|
|
|
_updaterVM.StartProcess();
|
|
|
|
|
};
|
|
|
|
|
releaseInfoVM.StartProcess();
|
2015-07-27 08:10:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2015-07-26 12:50:05 +00:00
|
|
|
|
|
2015-07-27 20:14:18 +00:00
|
|
|
|
#region Exception handler
|
|
|
|
|
|
|
|
|
|
void CentralHandler_ManageExceptionCallback(object sender, Bovender.ExceptionHandler.ManageExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.IsHandled = true;
|
|
|
|
|
ExceptionViewModel vm = new ExceptionViewModel(e.Exception);
|
2017-03-02 14:27:37 +00:00
|
|
|
|
vm.InjectInto<ExceptionView>().ShowDialogInForm();
|
2015-07-27 20:14:18 +00:00
|
|
|
|
}
|
2015-08-04 00:43:17 +00:00
|
|
|
|
|
2015-07-27 20:14:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2015-07-26 12:50:05 +00:00
|
|
|
|
#region Private fields
|
|
|
|
|
|
2015-07-27 08:10:18 +00:00
|
|
|
|
Ribbon _ribbon;
|
2015-08-18 07:00:26 +00:00
|
|
|
|
string _oldCaption;
|
2016-09-03 21:18:43 +00:00
|
|
|
|
UpdaterViewModel _updaterVM;
|
2015-07-26 12:50:05 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2015-06-15 17:15:52 +00:00
|
|
|
|
#region VSTO generated code
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Required method for Designer support - do not modify
|
|
|
|
|
/// the contents of this method with the code editor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void InternalStartup()
|
|
|
|
|
{
|
|
|
|
|
this.Startup += new System.EventHandler(ThisAddIn_Startup);
|
|
|
|
|
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2016-09-03 21:18:43 +00:00
|
|
|
|
|
|
|
|
|
#region Class logger
|
|
|
|
|
|
|
|
|
|
private static NLog.Logger Logger { get { return _logger.Value; } }
|
|
|
|
|
|
|
|
|
|
private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
|
|
|
|
|
|
|
|
|
|
#endregion
|
2015-06-15 17:15:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|