context: /****************************************************************************
  **
  ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
  ** Contact: http://www.qt-project.org/legal
  **
  ** This file is part of the Qt VS Add-in.
  **
  ** $QT_BEGIN_LICENSE:LGPL$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and Digia.  For licensing terms and
  ** conditions see http://qt.digia.com/licensing.  For further information
  ** use the contact form at http://qt.digia.com/contact-us.
  **
  ** GNU Lesser General Public License Usage
  ** Alternatively, this file may be used under the terms of the GNU Lesser
  ** General Public License version 2.1 as published by the Free Software
  ** Foundation and appearing in the file LICENSE.LGPL included in the
  ** packaging of this file.  Please review the following information to
  ** ensure the GNU Lesser General Public License version 2.1 requirements
  ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  **
  ** In addition, as a special exception, Digia gives you certain additional
  ** rights.  These rights are described in the Digia Qt LGPL Exception
  ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  **
  ** GNU General Public License Usage
  ** Alternatively, this file may be used under the terms of the GNU
  ** General Public License version 3.0 as published by the Free Software
  ** Foundation and appearing in the file LICENSE.GPL included in the
  ** packaging of this file.  Please review the following information to
  ** ensure the GNU General Public License version 3.0 requirements will be
  ** met: http://www.gnu.org/copyleft/gpl.html.
  **
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/
  
  using System;
  using Extensibility;
  using EnvDTE;
  using Microsoft.VisualStudio.VCProjectEngine;
  using Microsoft.Win32;
  using System.IO;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  using Digia.Qt5ProjectLib;
  using EnvDTE80;
  
  namespace Qt5VSAddin
  {
      [GuidAttribute("C80C78C8-F64B-43df-9A53-96F7C44A1EB6"), ProgId("Qt5VSAddin")]
      /// <summary>The object for implementing an Add-in.</summary>
      /// <seealso class='IDTExtensibility2' />
      public class Connect : IDTExtensibility2, IDTCommandTarget
      {
          public static DTE _applicationObject = null;
          public static AddIn _addInInstance = null;
          public static ExtLoader extLoader = null;
          public static bool menuVisible = false;
  
          private AddinInit initializer = null;
          private AddInEventHandler eventHandler = null;
          private FormChangeQtVersion formChangeQtVersion = null;
          private FormVSQtSettings formQtVersions = null;
          private FormProjectQtSettings formProjectQtSettings = null;
          private string installationDir = null;
          private string appWrapperPath = null;
          private string qmakeFileReaderPath = null;
          private bool commandLine = false;
  
          public static Connect Instance()
          {
              return _addInInstance.Object as Connect;
          }
  
          /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
          public Connect()
          {
          }
  
          public string InstallationDir
          {
              get
              {
                  return installationDir;
              }
          }
  
          public string AppWrapperPath
          {
              get
              {
                  if (appWrapperPath == null)
                      appWrapperPath = locateHelperExecutable("qt5appwrapper.exe");
                  return appWrapperPath;
              }
          }
  
          public string QMakeFileReaderPath
          {
              get
              {
                  if (qmakeFileReaderPath == null)
                      qmakeFileReaderPath = locateHelperExecutable("qmakefilereader.exe");
                  return qmakeFileReaderPath;
              }
          }
  
          private string locateHelperExecutable(string exeName)
          {
              string filePath = installationDir + exeName;
              if (!File.Exists(filePath))
              {
                  filePath = installationDir;
                  if (filePath.EndsWith("\\"))
                      filePath = filePath.Remove(filePath.Length - 1);
                  int idx = filePath.LastIndexOf('\\');
                  if (idx >= 0 && idx < filePath.Length - 1)
                      filePath = filePath.Remove(idx + 1);
                  filePath += exeName;
                  if (!File.Exists(filePath))
                      filePath = null;
              }
              return filePath;
          }
  
          /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
          /// <param term='application'>Root object of the host application.</param>
          /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
          /// <param term='addInInst'>Object representing this Add-in.</param>
          /// <seealso class='IDTExtensibility2' />
          public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
          {
              _applicationObject = (DTE)application;
              _addInInstance = (AddIn)addInInst;
  
              // determine the installation directory
              System.Uri uri = new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase);
              installationDir = Path.GetDirectoryName(System.Uri.UnescapeDataString(uri.AbsolutePath));
              installationDir += "\\";
  
              // General startup code
              if ((ext_ConnectMode.ext_cm_AfterStartup == connectMode) ||
                  (ext_ConnectMode.ext_cm_Startup == connectMode) ||
                  (ext_ConnectMode.ext_cm_CommandLine == connectMode))
              {
                  QtVersionManager vm = QtVersionManager.The();
                  string error = null;
                  if (vm.HasInvalidVersions(out error))
                      Messages.DisplayErrorMessage(error);
                  eventHandler = new AddInEventHandler(_applicationObject);
                  extLoader = new ExtLoader();
                  if (ext_ConnectMode.ext_cm_CommandLine != connectMode)
                  {
                      try
                      {
                          initializer = new AddinInit(_applicationObject);
                          initializer.removeCommands();
                          initializer.registerCommands();
                          UpdateDefaultEditors(true);
                      }
                      catch (System.Exception e)
                      {
                          MessageBox.Show(e.Message + "\r\n\r\nStacktrace:\r\n" + e.StackTrace);
                      }
                  }
                  else
                      commandLine = true;
              }
          }
  
          public void QueryStatus(string commandName,
                   EnvDTE.vsCommandStatusTextWanted neededText,
                   ref EnvDTE.vsCommandStatus status,
                   ref object commandText)
          {
              try
              {
                  if (neededText == EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
                  {
                      if ((commandName == Res.LaunchDesignerFullCommand) ||
                          (commandName == Res.LaunchLinguistFullCommand) ||
                          (commandName == Res.LaunchAssistantFullCommand) ||
                          (commandName == Res.VSQtOptionsFullCommand) ||
                          (commandName == Res.ImportProFileFullCommand))
                      {
                          status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                              | vsCommandStatus.vsCommandStatusEnabled;
                      }
                      else if ((commandName == Res.ImportPriFileFullCommand) ||
                          (commandName == Res.ExportPriFileFullCommand) ||
                          (commandName == Res.ExportProFileFullCommand) ||
                          (commandName == Res.CreateNewTranslationFileFullCommand) ||
                          (commandName == Res.lupdateProjectFullCommand) ||
                          (commandName == Res.lreleaseProjectFullCommand))
                      {
                          Project prj = HelperFunctions.GetSelectedProject(_applicationObject);
                          if (prj != null && HelperFunctions.IsQt5Project(prj))
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                                      | vsCommandStatus.vsCommandStatusEnabled;
                          else
                              status = vsCommandStatus.vsCommandStatusSupported;
                      }
                      else if (commandName == Res.ProjectQtSettingsFullCommand)
                      {
                          Project prj = HelperFunctions.GetSelectedProject(_applicationObject);
                          if (prj == null)
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          else if (HelperFunctions.IsQt5Project(prj))
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                                      | vsCommandStatus.vsCommandStatusEnabled;
                          else if (HelperFunctions.IsQMakeProject(prj))
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          else
                              status = vsCommandStatus.vsCommandStatusSupported;
                      }
                      else if (commandName == Res.ChangeProjectQtVersionFullCommand)
                      {
                          Project prj = HelperFunctions.GetSelectedProject(_applicationObject);
                          if (prj == null || HelperFunctions.IsQt5Project(prj))
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          else if (HelperFunctions.IsQMakeProject(prj))
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                                      | vsCommandStatus.vsCommandStatusEnabled;
                          else
                              status = vsCommandStatus.vsCommandStatusInvisible;
                      }
                      else if (commandName == Res.ConvertToQMakeFullCommand)
                      {
                          Project prj = HelperFunctions.GetSelectedProject(_applicationObject);
                          if (prj == null)
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          else if (HelperFunctions.IsQt5Project(prj))
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                                      | vsCommandStatus.vsCommandStatusEnabled;
                          else if (HelperFunctions.IsQMakeProject(prj))
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          else
                              status = vsCommandStatus.vsCommandStatusInvisible;
                      }
                      else if (commandName == Res.ConvertToQtFullCommand)
                      {
                          Project prj = HelperFunctions.GetSelectedProject(_applicationObject);
                          if (prj == null || HelperFunctions.IsQt5Project(prj))
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          else if (HelperFunctions.IsQMakeProject(prj))
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                                      | vsCommandStatus.vsCommandStatusEnabled;
                          else if (HelperFunctions.IsVcProject(prj))
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                                      | vsCommandStatus.vsCommandStatusEnabled;
                          else
                              status = vsCommandStatus.vsCommandStatusInvisible;
                      }
                      else if ((commandName == Res.ChangeSolutionQtVersionFullCommand) ||
                          (commandName == Res.lupdateSolutionFullCommand) ||
                          (commandName == Res.lreleaseSolutionFullCommand))
                      {
                          if (_applicationObject.Solution.IsOpen)
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                              | vsCommandStatus.vsCommandStatusEnabled;
                          else
                              status = vsCommandStatus.vsCommandStatusSupported;
                      }
                      else if (commandName == Res.CommandBarName + ".Connect.lupdate" ||
                          commandName == Res.CommandBarName + ".Connect.lrelease")
                      {
                          Project prj = HelperFunctions.GetSelectedProject(_applicationObject);
                          if (prj == null || !HelperFunctions.IsQt5Project(prj) ||
                              _applicationObject.SelectedItems.Count == 0)
                          {
                              status = vsCommandStatus.vsCommandStatusInvisible;
                          }
                          else
                          {
                              status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported
                                  | vsCommandStatus.vsCommandStatusEnabled;
                          }
  
                          if (status != vsCommandStatus.vsCommandStatusInvisible)
                          {
                              // Don't display commands if one of the selected files is not a .ts file.
                              foreach (SelectedItem si in _applicationObject.SelectedItems)
                              {
                                  if (!si.Name.ToLower().EndsWith(".ts"))
                                  {
                                      status = vsCommandStatus.vsCommandStatusInvisible;
                                      break;
                                  }
                              }
                          }
                      }
                  }
              }
              catch (System.Exception e)
              {
                  MessageBox.Show(e.Message + "\r\n\r\nStacktrace:\r\n" + e.StackTrace);
              }
          }
  
          public void Exec(string commandName,
                    EnvDTE.vsCommandExecOption executeOption,
                    ref object varIn,
                    ref object varOut,
                    ref bool handled)
          {
              try
              {
                  handled = false;
                  if (executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
                  {
                      switch (commandName)
                      {
                          case Res.LaunchDesignerFullCommand:
                              handled = true;
                              extLoader.loadDesigner(null);
                              break;
                          case Res.LaunchLinguistFullCommand:
                              handled = true;
                              ExtLoader.loadLinguist(null);
                              break;
                          case Res.LaunchAssistantFullCommand:
                              handled = true;
                              ExtLoader.loadAssistant();
                              break;
                          case Res.ImportProFileFullCommand:
                              handled = true;
                              ExtLoader.ImportProFile();
                              break;
                          case Res.ImportPriFileFullCommand:
                              handled = true;
                              ExtLoader.ImportPriFile(HelperFunctions.GetSelectedQtProject(_applicationObject));
                              break;
                          case Res.ExportPriFileFullCommand:
                              handled = true;
                              ExtLoader.ExportPriFile();
                              break;
                          case Res.ExportProFileFullCommand:
                              handled = true;
                              ExtLoader.ExportProFile();
                              break;
                          case Res.ChangeSolutionQtVersionFullCommand:
                              QtVersionManager vManager = QtVersionManager.The();
                              if (formChangeQtVersion == null)
                                  formChangeQtVersion = new FormChangeQtVersion();
                              formChangeQtVersion.UpdateContent(ChangeFor.Solution);
                              if (formChangeQtVersion.ShowDialog() == DialogResult.OK)
                              {
                                  string newQtVersion = formChangeQtVersion.GetSelectedQtVersion();
                                  if (newQtVersion != null)
                                  {
                                      string currentPlatform = null;
                                      try
                                      {
                                          SolutionConfiguration config = _applicationObject.Solution.SolutionBuild.ActiveConfiguration;
                                          SolutionConfiguration2 config2 = config as SolutionConfiguration2;
                                          currentPlatform = config2.PlatformName;
                                      }
                                      catch
                                      {
                                      }
                                      if (string.IsNullOrEmpty(currentPlatform))
                                          return;
  
                                      vManager.SetPlatform(currentPlatform);
  
                                      foreach (Project project in HelperFunctions.ProjectsInSolution(_applicationObject))
                                      {
                                          if (HelperFunctions.IsQt5Project(project))
                                          {
                                              string OldQtVersion = vManager.GetProjectQtVersion(project, currentPlatform);
                                              if (OldQtVersion == null)
                                                  OldQtVersion = vManager.GetDefaultVersion();
  
                                              QtProject qtProject = QtProject.Create(project);
                                              bool newProjectCreated = false;
                                              qtProject.ChangeQtVersion(OldQtVersion, newQtVersion, ref newProjectCreated);
                                          }
                                      }
                                      vManager.SaveSolutionQtVersion(_applicationObject.Solution, newQtVersion);
                                  }
                              }
                              break;
                          case Res.ProjectQtSettingsFullCommand:
                              handled = true;
                              EnvDTE.DTE dte = _applicationObject;
                              Project pro = HelperFunctions.GetSelectedQtProject(dte);
                              if (pro != null)
                              {
                                  if (formProjectQtSettings == null)
  
gt:                                     formProjectQtSettings = new FormProjectQtSettings();