Clients/Idle/FormClientIdle.cs
author StephaneLenclud
Sun, 07 Feb 2016 13:29:05 +0100
changeset 196 0fb548a75849
parent 194 b0c0aa5aea05
child 197 c66ec88ed19d
permissions -rw-r--r--
Idle Client not showing in Open Task view any more.
     1 //
     2 // Copyright (C) 2014-2016 Stéphane Lenclud.
     3 //
     4 // This file is part of SharpDisplayManager.
     5 //
     6 // SharpDisplayManager is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // SharpDisplayManager is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 using System;
    21 using System.Collections.Generic;
    22 using System.ComponentModel;
    23 using System.Data;
    24 using System.Drawing;
    25 using System.Linq;
    26 using System.Text;
    27 using System.Threading.Tasks;
    28 using System.Windows.Forms;
    29 using System.Diagnostics;
    30 using SharpLib.Display;
    31 
    32 
    33 namespace SharpDisplayIdleClient
    34 {
    35 
    36     /// <summary>
    37     /// Sharp Display Client designed to act as an idle client.
    38     /// It should take care of screen saving and other such concerns.
    39     /// </summary>
    40     public partial class FormClientIdle : Form
    41     {
    42         public StartParams Params { get; set; }
    43 
    44         Client iClient;
    45         ContentAlignment iAlignment;
    46         TextField iTextField;
    47 
    48         public delegate void CloseDelegate();
    49         public delegate void CloseConnectionDelegate();
    50 
    51 
    52         public FormClientIdle()
    53         {
    54             InitializeComponent();
    55         }
    56 
    57         /// <summary>
    58         /// 
    59         /// </summary>
    60         /// <param name="sender"></param>
    61         /// <param name="e"></param>
    62         private void FormClientIdle_Load(object sender, EventArgs e)
    63         {
    64             //Prevents showing in the Open Task view (Windows Key + Tab)
    65             Visible = false;
    66 
    67             //Display client
    68             iClient = new Client();
    69             iClient.CloseOrderEvent += OnCloseOrder;
    70             iClient.Open();
    71             iClient.SetName("Idle");
    72             iClient.SetPriority(Priorities.Background);
    73             SetupDisplayClient();
    74 
    75             //Timer
    76             iTimer.Interval = IntervalToNextMinute();
    77             iTimer.Start();
    78         }
    79 
    80         /// <summary>
    81         /// 
    82         /// </summary>
    83         /// <returns></returns>
    84         public static int IntervalToNextMinute()
    85         {
    86             DateTime now = DateTime.Now;
    87             return 60000 - now.Millisecond;
    88         }
    89 
    90 
    91         /// <summary>
    92         /// 
    93         /// </summary>
    94         /// <returns></returns>
    95         public bool IsClientReady()
    96         {
    97             return (iClient != null && iClient.IsReady());
    98         }
    99 
   100         /// <summary>
   101         /// 
   102         /// </summary>
   103         public void SetupDisplayClient()
   104         {
   105             //Setup our layout
   106 
   107             //Set one column one line layout
   108             TableLayout layout = new TableLayout(1, 1);
   109             iClient.SetLayout(layout);
   110 
   111             //Setup our fields
   112             iAlignment = ContentAlignment.MiddleCenter;
   113             iTextField = new TextField(DateTime.Now.ToString("t"), iAlignment, 0, 0);
   114 
   115             //Set our fields
   116             iClient.CreateFields(new DataField[]
   117             {
   118                 iTextField
   119             });
   120         }
   121 
   122         public void OnCloseOrder()
   123         {
   124             CloseThreadSafe();
   125         }
   126 
   127         /// <summary>
   128         ///
   129         /// </summary>
   130         public void CloseThreadSafe()
   131         {
   132             if (this.InvokeRequired)
   133             {
   134                 //Not in the proper thread, invoke ourselves
   135                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   136                 this.Invoke(d, new object[] { });
   137             }
   138             else
   139             {
   140                 //We are in the proper thread
   141                 Close();
   142             }
   143         }
   144 
   145         /// <summary>
   146         ///
   147         /// </summary>
   148         public void CloseConnectionThreadSafe()
   149         {
   150             if (this.InvokeRequired)
   151             {
   152                 //Not in the proper thread, invoke ourselves
   153                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   154                 this.Invoke(d, new object[] { });
   155             }
   156             else
   157             {
   158                 //We are in the proper thread
   159                 if (IsClientReady())
   160                 {
   161                     string sessionId = iClient.SessionId;
   162                     Trace.TraceInformation("Closing client: " + sessionId);
   163                     iClient.Close();
   164                     Trace.TraceInformation("Closed client: " + sessionId);
   165                 }
   166 
   167                 iClient = null;
   168             }
   169         }
   170 
   171         /// <summary>
   172         /// 
   173         /// </summary>
   174         /// <param name="sender"></param>
   175         /// <param name="e"></param>
   176         private void FormClientIdle_FormClosing(object sender, FormClosingEventArgs e)
   177         {
   178             CloseConnectionThreadSafe();
   179         }
   180 
   181 
   182         /// <summary>
   183         /// 
   184         /// </summary>
   185         /// <param name="sender"></param>
   186         /// <param name="e"></param>
   187         private void iTimer_Tick(object sender, EventArgs e)
   188         {
   189             //Timer
   190             iTimer.Interval = IntervalToNextMinute();
   191             iTimer.Start();
   192 
   193             //
   194             if (String.IsNullOrEmpty(iTextField.Text))
   195             {
   196                 //Time to show our time
   197                 iTextField.Text = DateTime.Now.ToString("t");
   198             }
   199             else
   200             {
   201                 //Do some screen saving                
   202                 iTextField.Text = "";
   203             }
   204 
   205             // Update our field
   206             iClient.SetField(iTextField);
   207 
   208             //Now make sure we save our screen from any running system monitor
   209             if (String.IsNullOrEmpty(iTextField.Text))
   210             {
   211                 //If text it empty it means we need to cool down our display even if system monitor is running
   212                 iClient.SetPriority(Priorities.SystemMonitor + 1);
   213             }
   214             else
   215             {
   216                 //Switch back to background then, leaving system monitor if any doing its magic
   217                 iClient.SetPriority(Priorities.Background);
   218             }
   219         }
   220 
   221         private void FormClientIdle_Shown(object sender, EventArgs e)
   222         {
   223             //Visible = false;
   224         }
   225     }
   226 
   227 }