/* thisaddin.cs * part of zaaReloaded2 * * Copyright 2015-2018 Daniel Kraus * * 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; using System.IO; using Bovender.Versioning; using Bovender.Mvvm.Messaging; using Bovender.Extensions; using zaaReloaded2.ExceptionHandler; using System.Windows; using System.Windows.Input; using System.Windows.Controls; using System.Windows.Media; using System.Configuration; using System.Windows.Threading; namespace zaaReloaded2 { public partial class ThisAddIn { #region Static property /// /// Gets the subdirectory for addin data in the user profile directory. /// public static string Subdir { get { return Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "zaaReloaded2"); } } #endregion #region Start up and shut down private void ThisAddIn_Startup(object sender, System.EventArgs e) { #if DEBUG Bovender.Logging.LogFile.Default.EnableDebugLogging(); #endif try { 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); } Bovender.ExceptionHandler.CentralHandler.ManageExceptionCallback += CentralHandler_ManageExceptionCallback; Bovender.Win32Window.MainWindowHandleProvider = () => { return System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; // return IntPtr.Zero; }; Bovender.WpfHelpers.RegisterTextBoxSelectAll(); UserSettings userSettings = UserSettings.Default; CheckForUpdates(); Microsoft.Office.Interop.Word.Application word = Globals.ThisAddIn.Application; _oldCaption = word.Caption; word.Caption = String.Format( "{0} ({1} {2})", _oldCaption, Properties.Settings.Default.AddinName, Updater.Version.Current.ToString() ); _dispatcher = Dispatcher.CurrentDispatcher; System.Threading.Timer timer = new System.Threading.Timer((obj) => { _dispatcher.Invoke((Action)(() => ViewModels.FirstRunViewModel.InjectIntoIfNeeded()), null); }, null, 500, System.Threading.Timeout.Infinite); Logger.Info("ThisAddIn_Startup: Finished startup"); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { UserSettings.Default.LastVersionSeen = Updater.Version.Current.ToString(); UserSettings.Default.Save(); if (_updaterVM != null && _updaterVM.InstallCommand.CanExecute(null)) { Logger.Info("ThisAddIn_Shutdown: Installing update"); _updaterVM.InstallCommand.Execute(null); } try { Logger.Info("ThisAddIn_Shutdown: Resetting application caption"); Globals.ThisAddIn.Application.Caption = _oldCaption; } catch (Exception ex) { Logger.Warn("ThisAddIn_Shutdown: Failed to reset application caption"); Logger.Warn(ex); } Logger.Info("ThisAddIn_Shutdown: Finished shutdown"); } #endregion #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 #region Updater void CheckForUpdates() { // Check only once per day if (DateTime.Today == UserSettings.Default.LastUpdateCheck.Date) return; 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(); } #endregion #region Exception handler void CentralHandler_ManageExceptionCallback(object sender, Bovender.ExceptionHandler.ManageExceptionEventArgs e) { Logger.Fatal("*** EXCEPTION ***"); Logger.Fatal(e.Exception); e.IsHandled = true; ExceptionViewModel vm = new ExceptionViewModel(e.Exception); vm.InjectInto().ShowDialogInForm(); } #endregion #region Private fields Ribbon _ribbon; string _oldCaption; UpdaterViewModel _updaterVM; Dispatcher _dispatcher; #endregion #region VSTO generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion #region Class logger private static NLog.Logger Logger { get { return _logger.Value; } } private static readonly Lazy _logger = new Lazy(() => NLog.LogManager.GetCurrentClassLogger()); #endregion } }