Clients/Message/FormClientMessage.cs
changeset 225 6ccaa430aa23
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Clients/Message/FormClientMessage.cs	Thu Jul 28 19:32:40 2016 +0200
     1.3 @@ -0,0 +1,215 @@
     1.4 +//
     1.5 +// Copyright (C) 2014-2016 Stéphane Lenclud.
     1.6 +//
     1.7 +// This file is part of SharpDisplayManager.
     1.8 +//
     1.9 +// SharpDisplayManager is free software: you can redistribute it and/or modify
    1.10 +// it under the terms of the GNU General Public License as published by
    1.11 +// the Free Software Foundation, either version 3 of the License, or
    1.12 +// (at your option) any later version.
    1.13 +//
    1.14 +// SharpDisplayManager is distributed in the hope that it will be useful,
    1.15 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +// GNU General Public License for more details.
    1.18 +//
    1.19 +// You should have received a copy of the GNU General Public License
    1.20 +// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    1.21 +//
    1.22 +
    1.23 +using System;
    1.24 +using System.Collections.Generic;
    1.25 +using System.ComponentModel;
    1.26 +using System.Data;
    1.27 +using System.Drawing;
    1.28 +using System.Linq;
    1.29 +using System.Text;
    1.30 +using System.Threading.Tasks;
    1.31 +using System.Windows.Forms;
    1.32 +using System.Diagnostics;
    1.33 +using SharpLib.Display;
    1.34 +
    1.35 +
    1.36 +namespace SharpDisplayClientMessage
    1.37 +{
    1.38 +
    1.39 +    /// <summary>
    1.40 +    /// Sharp Display Client designed to act as an idle client.
    1.41 +    /// It should take care of screen saving and other such concerns.
    1.42 +    /// </summary>
    1.43 +    public partial class FormClientMessage : Form
    1.44 +    {
    1.45 +        public StartParams Params { get; set; }
    1.46 +
    1.47 +        Client iClient;
    1.48 +        ContentAlignment iAlignment;
    1.49 +        TextField iPrimaryTextField;
    1.50 +        TextField iSecondaryTextField;
    1.51 +
    1.52 +
    1.53 +        public delegate void CloseDelegate();
    1.54 +        public delegate void CloseConnectionDelegate();
    1.55 +
    1.56 +
    1.57 +        public FormClientMessage()
    1.58 +        {
    1.59 +            InitializeComponent();
    1.60 +        }
    1.61 +
    1.62 +        /// <summary>
    1.63 +        /// 
    1.64 +        /// </summary>
    1.65 +        /// <param name="sender"></param>
    1.66 +        /// <param name="e"></param>
    1.67 +        private void FormClientMessage_Load(object sender, EventArgs e)
    1.68 +        {
    1.69 +            //Prevents showing in the Open Task view (Windows Key + Tab)
    1.70 +            Visible = false;
    1.71 +
    1.72 +            //Display client
    1.73 +            iClient = new Client();
    1.74 +            iClient.CloseOrderEvent += OnCloseOrder;
    1.75 +            iClient.Open();
    1.76 +            iClient.SetName("Message");
    1.77 +            iClient.SetPriority(Params.Priority);
    1.78 +            SetupDisplayClient();
    1.79 +
    1.80 +            //Timer
    1.81 +            iTimer.Interval = Params.DurationInMs;
    1.82 +            iTimer.Start();
    1.83 +        }
    1.84 +
    1.85 +
    1.86 +        /// <summary>
    1.87 +        /// 
    1.88 +        /// </summary>
    1.89 +        /// <returns></returns>
    1.90 +        public bool IsClientReady()
    1.91 +        {
    1.92 +            return (iClient != null && iClient.IsReady());
    1.93 +        }
    1.94 +
    1.95 +        /// <summary>
    1.96 +        /// 
    1.97 +        /// </summary>
    1.98 +        public void SetupDisplayClient()
    1.99 +        {
   1.100 +            //Setup our layout
   1.101 +
   1.102 +            //Set one column one line layout
   1.103 +
   1.104 +            //Setup our fields
   1.105 +            iAlignment = ContentAlignment.MiddleCenter;
   1.106 +            iPrimaryTextField = new TextField(Params.PrimaryText, iAlignment, 0, 0);
   1.107 +            iSecondaryTextField = new TextField(Params.SecondaryText, iAlignment, 0, 1);
   1.108 +
   1.109 +            //Set our fields
   1.110 +            if (string.IsNullOrEmpty(Params.SecondaryText))
   1.111 +            {
   1.112 +                //One field layout
   1.113 +                TableLayout layout = new TableLayout(1, 1);
   1.114 +                iClient.SetLayout(layout);
   1.115 +
   1.116 +                iClient.CreateFields(new DataField[]
   1.117 +                {
   1.118 +                iPrimaryTextField
   1.119 +                });
   1.120 +            }
   1.121 +            else
   1.122 +            {
   1.123 +                //Two fields layout
   1.124 +                TableLayout layout = new TableLayout(1, 2);
   1.125 +                iClient.SetLayout(layout);
   1.126 +
   1.127 +                iClient.CreateFields(new DataField[]
   1.128 +                {
   1.129 +                iPrimaryTextField,
   1.130 +                iSecondaryTextField
   1.131 +                });
   1.132 +            }
   1.133 +        }
   1.134 +
   1.135 +        public void OnCloseOrder()
   1.136 +        {
   1.137 +            CloseThreadSafe();
   1.138 +        }
   1.139 +
   1.140 +        /// <summary>
   1.141 +        ///
   1.142 +        /// </summary>
   1.143 +        public void CloseThreadSafe()
   1.144 +        {
   1.145 +            if (this.InvokeRequired)
   1.146 +            {
   1.147 +                //Not in the proper thread, invoke ourselves
   1.148 +                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   1.149 +                this.Invoke(d, new object[] { });
   1.150 +            }
   1.151 +            else
   1.152 +            {
   1.153 +                //We are in the proper thread
   1.154 +                Close();
   1.155 +            }
   1.156 +        }
   1.157 +
   1.158 +        /// <summary>
   1.159 +        ///
   1.160 +        /// </summary>
   1.161 +        public void CloseConnectionThreadSafe()
   1.162 +        {
   1.163 +            if (this.InvokeRequired)
   1.164 +            {
   1.165 +                //Not in the proper thread, invoke ourselves
   1.166 +                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   1.167 +                this.Invoke(d, new object[] { });
   1.168 +            }
   1.169 +            else
   1.170 +            {
   1.171 +                //We are in the proper thread
   1.172 +                if (IsClientReady())
   1.173 +                {
   1.174 +                    string sessionId = iClient.SessionId;
   1.175 +                    Trace.TraceInformation("Closing client: " + sessionId);
   1.176 +                    iClient.Close();
   1.177 +                    Trace.TraceInformation("Closed client: " + sessionId);
   1.178 +                }
   1.179 +
   1.180 +                iClient = null;
   1.181 +            }
   1.182 +        }
   1.183 +
   1.184 +        /// <summary>
   1.185 +        /// 
   1.186 +        /// </summary>
   1.187 +        /// <param name="sender"></param>
   1.188 +        /// <param name="e"></param>
   1.189 +        private void FormClientMessage_FormClosing(object sender, FormClosingEventArgs e)
   1.190 +        {
   1.191 +            CloseConnectionThreadSafe();
   1.192 +        }
   1.193 +
   1.194 +
   1.195 +        /// <summary>
   1.196 +        /// 
   1.197 +        /// </summary>
   1.198 +        /// <param name="sender"></param>
   1.199 +        /// <param name="e"></param>
   1.200 +        private void iTimer_Tick(object sender, EventArgs e)
   1.201 +        {
   1.202 +            Close();
   1.203 +        }
   1.204 +
   1.205 +
   1.206 +        /// <summary>
   1.207 +        /// 
   1.208 +        /// </summary>
   1.209 +        /// <param name="sender"></param>
   1.210 +        /// <param name="e"></param>
   1.211 +        private void FormClientMessage_Shown(object sender, EventArgs e)
   1.212 +        {
   1.213 +            //Visible = false;
   1.214 +        }
   1.215 +
   1.216 +    }
   1.217 +
   1.218 +}