Clients/Message/FormClientMessage.cs
author Stephane Lenclud
Thu, 18 Aug 2016 20:14:30 +0200
changeset 242 0a956121c273
permissions -rw-r--r--
Weird signing crap.
     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 SharpDisplayClientMessage
    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 FormClientMessage : Form
    41     {
    42         public StartParams Params { get; set; }
    43 
    44         Client iClient;
    45         ContentAlignment iAlignment;
    46         TextField iPrimaryTextField;
    47         TextField iSecondaryTextField;
    48 
    49 
    50         public delegate void CloseDelegate();
    51         public delegate void CloseConnectionDelegate();
    52 
    53 
    54         public FormClientMessage()
    55         {
    56             InitializeComponent();
    57         }
    58 
    59         /// <summary>
    60         /// 
    61         /// </summary>
    62         /// <param name="sender"></param>
    63         /// <param name="e"></param>
    64         private void FormClientMessage_Load(object sender, EventArgs e)
    65         {
    66             //Prevents showing in the Open Task view (Windows Key + Tab)
    67             Visible = false;
    68 
    69             //Display client
    70             iClient = new Client();
    71             iClient.CloseOrderEvent += OnCloseOrder;
    72             iClient.Open();
    73             iClient.SetName("Message");
    74             iClient.SetPriority(Params.Priority);
    75             SetupDisplayClient();
    76 
    77             //Timer
    78             iTimer.Interval = Params.DurationInMs;
    79             iTimer.Start();
    80         }
    81 
    82 
    83         /// <summary>
    84         /// 
    85         /// </summary>
    86         /// <returns></returns>
    87         public bool IsClientReady()
    88         {
    89             return (iClient != null && iClient.IsReady());
    90         }
    91 
    92         /// <summary>
    93         /// 
    94         /// </summary>
    95         public void SetupDisplayClient()
    96         {
    97             //Setup our layout
    98 
    99             //Set one column one line layout
   100 
   101             //Setup our fields
   102             iAlignment = ContentAlignment.MiddleCenter;
   103             iPrimaryTextField = new TextField(Params.PrimaryText, iAlignment, 0, 0);
   104             iSecondaryTextField = new TextField(Params.SecondaryText, iAlignment, 0, 1);
   105 
   106             //Set our fields
   107             if (string.IsNullOrEmpty(Params.SecondaryText))
   108             {
   109                 //One field layout
   110                 TableLayout layout = new TableLayout(1, 1);
   111                 iClient.SetLayout(layout);
   112 
   113                 iClient.CreateFields(new DataField[]
   114                 {
   115                 iPrimaryTextField
   116                 });
   117             }
   118             else
   119             {
   120                 //Two fields layout
   121                 TableLayout layout = new TableLayout(1, 2);
   122                 iClient.SetLayout(layout);
   123 
   124                 iClient.CreateFields(new DataField[]
   125                 {
   126                 iPrimaryTextField,
   127                 iSecondaryTextField
   128                 });
   129             }
   130         }
   131 
   132         public void OnCloseOrder()
   133         {
   134             CloseThreadSafe();
   135         }
   136 
   137         /// <summary>
   138         ///
   139         /// </summary>
   140         public void CloseThreadSafe()
   141         {
   142             if (this.InvokeRequired)
   143             {
   144                 //Not in the proper thread, invoke ourselves
   145                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   146                 this.Invoke(d, new object[] { });
   147             }
   148             else
   149             {
   150                 //We are in the proper thread
   151                 Close();
   152             }
   153         }
   154 
   155         /// <summary>
   156         ///
   157         /// </summary>
   158         public void CloseConnectionThreadSafe()
   159         {
   160             if (this.InvokeRequired)
   161             {
   162                 //Not in the proper thread, invoke ourselves
   163                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   164                 this.Invoke(d, new object[] { });
   165             }
   166             else
   167             {
   168                 //We are in the proper thread
   169                 if (IsClientReady())
   170                 {
   171                     string sessionId = iClient.SessionId;
   172                     Trace.TraceInformation("Closing client: " + sessionId);
   173                     iClient.Close();
   174                     Trace.TraceInformation("Closed client: " + sessionId);
   175                 }
   176 
   177                 iClient = null;
   178             }
   179         }
   180 
   181         /// <summary>
   182         /// 
   183         /// </summary>
   184         /// <param name="sender"></param>
   185         /// <param name="e"></param>
   186         private void FormClientMessage_FormClosing(object sender, FormClosingEventArgs e)
   187         {
   188             CloseConnectionThreadSafe();
   189         }
   190 
   191 
   192         /// <summary>
   193         /// 
   194         /// </summary>
   195         /// <param name="sender"></param>
   196         /// <param name="e"></param>
   197         private void iTimer_Tick(object sender, EventArgs e)
   198         {
   199             Close();
   200         }
   201 
   202 
   203         /// <summary>
   204         /// 
   205         /// </summary>
   206         /// <param name="sender"></param>
   207         /// <param name="e"></param>
   208         private void FormClientMessage_Shown(object sender, EventArgs e)
   209         {
   210             //Visible = false;
   211         }
   212 
   213     }
   214 
   215 }