context: /* 
   * Copyright (C) 2006,2007 David Riseley 
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   */
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using uk.org.riseley.puttySessionManager.model;
  using uk.org.riseley.puttySessionManager.model.eventargs;
  using uk.org.riseley.puttySessionManager.controller;
  using uk.org.riseley.puttySessionManager.control;
  using System.Runtime.InteropServices;
  using System.IO;
  
  namespace uk.org.riseley.puttySessionManager.form
  {
      public partial class SessionManagerForm : SessionManagementForm, IMessageFilter 
      {
          private Options optionsDialog;
          private AboutBox aboutDialog;
          private SessionEditorForm sessionEditor;
          private HotkeyChooser hotKeyChooser;
          private SynchronizeForm synchronizeForm;
  
          private SessionControl currentSessionControl;
          private SessionControl hiddenSessionControl;
  
          private HotkeyController hkc;
  
          private class User32
          {
              [DllImport("user32.dll")]
              private static extern bool SetForegroundWindow(IntPtr hWnd);
  
              public static void SetForegroundWindow()
              {
                  SetForegroundWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
              }
  
          }
  
          public SessionManagerForm()
              : base()
          {
              this.Visible = false;
              InitializeComponent();
              hkc = HotkeyController.getInstance();
              LoadLayout();
              SessionController.SessionsRefreshedEventHandler scHandler = new SessionController.SessionsRefreshedEventHandler(this.SessionsRefreshed);
              sc.SessionsRefreshed += scHandler;
              Application.AddMessageFilter(this);
              EventHandler dialogFontHandler = new EventHandler(this.dialogFontChanged);
              optionsDialog.DialogFontChanged += dialogFontHandler;
          }
  
          /// <summary>
          /// Check PuTTY is accessible on startup
          /// and launch pageant if required
          /// </summary>
          public void doStartupActions()
          {
              // Check to see if PuTTY is accessible
              if (sc.isPuTTYExecutableAccessible() == false)
              {
                  if (MessageBox.Show("PuTTY location is not accessible\n.Would you like to specify this now?"
                      , "Warning"
                      , MessageBoxButtons.YesNo
                      , MessageBoxIcon.Warning
                      , MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                  {
                      optionsDialog.ShowDialog();
                  }
              }
  
              // Launch Pageant if required
              if (Properties.Settings.Default.LaunchPageantOnStart)
                  sc.launchPageant();
          }
  
          private void LoadLayout()
          {
              optionsDialog = new Options(this);
              aboutDialog   = new AboutBox();
              sessionEditor = new SessionEditorForm();
              hotKeyChooser = new HotkeyChooser(this);
              synchronizeForm = new SynchronizeForm();
  
              // Attempt to override the system and taskbar tray icons
              string iconFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\psm.ico";
              if (File.Exists(iconFile))
              {
                  try
                  {
                      Icon psmIcon = new Icon(iconFile);
                      systrayIcon.Icon = psmIcon;
                      this.Icon = psmIcon;
                  }
                  catch (Exception)
                  {
                    // Do nothing
                  }
              }
              systrayIcon.Visible = true;
  
              // Restore the size of the application
              this.ClientSize = Properties.Settings.Default.WindowSize;
  
              // Restore the location of the application
              Point savedLocation = Properties.Settings.Default.Location;
  
              // Get the screen that will display the point
              Screen display = Screen.FromPoint(savedLocation);
  
              // Check that the selected display contains the point
              // if not - restore the application in the top left hand corner
              if (display != null && display.Bounds.Contains(savedLocation))
                  this.DesktopLocation = Properties.Settings.Default.Location;
              else
                  this.DesktopLocation = new Point(0, 0);
  
              // Reset the display to either the tree or the list
              displayTreeToolStripMenuItem.Checked = Properties.Settings.Default.DisplayTree;
  
              // Register the new session hotkey if enabled
              if (Properties.Settings.Default.HotkeyNewEnabled)
                  hkc.RegisterHotkey(this, HotkeyController.HotKeyId.HKID_NEW);
  
              // Register the minimize hotkey if enabled
              if (Properties.Settings.Default.HotkeyMinimizeEnabled)
                  hkc.RegisterHotkey(this, HotkeyController.HotKeyId.HKID_MINIMIZE);
  
              // Invalidate the session list to force a refresh of all forms
              sc.invalidateSessionList(this, true); 
  
              // Expand the tree if requested on startup
              if (Properties.Settings.Default.ExpandTreeOnStartup)
                  sessionTreeControl1.expandFullTree();
  
              // Setup the display
              setDisplay();
          }
  
          private void SaveSize()
          {
              Properties.Settings.Default.WindowSize = this.ClientSize;
              Properties.Settings.Default.Location = this.DesktopLocation;
          }
  
          private void exitToolStripMenuItem_Click(object sender, EventArgs e)
          {
              confirmExit();
          }
  
          /// <summary>
          /// Comfirm exit if required
          /// </summary>
          private void confirmExit()
          {
              bool doExit = true;
  
              if (Properties.Settings.Default.ConfirmExit == true &&
                  MessageBox.Show(this, "Exit application?"
                                     , "Exit"
                                     , MessageBoxButtons.YesNo
                                     , MessageBoxIcon.Question) == DialogResult.No)
              {
                  doExit = false;
              }
  
              if (doExit)
                  Exit();
          }
          
          public void Exit()
          {
              systrayIcon.Visible = false;
  	        optionsDialog.Close();
  	        aboutDialog.Close();
  	        sessionEditor.Close();
  	        hotKeyChooser.Close();
  	        synchronizeForm.Close();
  	        hkc.UnregisterAllHotKeys(this);
  	        SaveSize();
  	        Application.Exit();        
          }
  
          /// <summary>
          /// Event handler for double click event on th
          /// system tray icon
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
          {
              showApplication(!this.Visible);            
          }
  
          /// <summary>
          /// Helper to display the main window
          /// </summary>
          /// <param name="visible"></param>
          private void showApplication(bool visible)
          {
              Visible = visible;          
              if (Visible)
              {
                  // If the window has been minimized
                  // bring it back to it's normal size
                  if (WindowState == FormWindowState.Minimized)
                      WindowState = FormWindowState.Normal;
  
                  // Request focus in this window
                  Activate();
  
                  // Bring the window to the front
                  User32.SetForegroundWindow();
              }
          }
  
          /// <summary>
          /// Allow other parts of the code to request display of the main window
          /// </summary>
          public void showApplication()
          {
              showApplication(true);
          }
  
          private void sessionControl_LaunchSession(object sender, LaunchSessionEventArgs se)
          {
              if (se != null)
              {
  
gt:                 if (se.program == LaunchSessionEventArgs.PROGRAM.PUTTY)