aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.PPCConsole/ViewModels/RemoteDesktopViewVM.cs
blob: 9bd25a99a3f76837336ba243371f49ed8e471daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Tango.Core.Commands;
using Tango.Core.Threading;
using Tango.FSE.Common;
using Tango.FSE.Common.RemoteDesktop;
using Tango.RemoteDesktop.Frames;
using Tango.RemoteDesktop.Network;

namespace Tango.FSE.PPCConsole.ViewModels
{
    /// <summary>
    /// Represents the PPC remote dekstop view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.Common.FSEViewModel" />
    public class RemoteDesktopViewVM : FSEViewModel
    {
        #region Properties

        private BitmapSource _source;
        /// <summary>
        /// Gets or sets the remote desktop image source.
        /// </summary>
        public BitmapSource Source
        {
            get { return _source; }
            set { _source = value; RaisePropertyChangedAuto(); }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Starts a remote desktop session with the currently connected machine.
        /// </summary>
        public RelayCommand StartCommand { get; set; }

        /// <summary>
        /// Stops the current remote desktop session.
        /// </summary>
        public RelayCommand StopCommand { get; set; }

        /// <summary>
        /// Executes the task manager on the remote PPC.
        /// </summary>
        public RelayCommand OpenTaskManagerCommand { get; set; }

        /// <summary>
        /// Hides the remote PPC application and opens the windows shell.
        /// </summary>
        public RelayCommand HideAndOpenShellCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RemoteDesktopViewVM"/> class.
        /// </summary>
        public RemoteDesktopViewVM()
        {
            StartCommand = new RelayCommand(StartRemoteDesktop);
            StopCommand = new RelayCommand(StopRemoteDesktop);
            OpenTaskManagerCommand = new RelayCommand(OpenTaskManager);
            HideAndOpenShellCommand = new RelayCommand(HideAndOpenShell);
        }

        #endregion

        #region Override Methods

        public override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            RemoteDesktopProvider.FrameReceived += RemoteDesktopProvider_FrameReceived;
            MachineProvider.MachineConnected += MachineProvider_MachineConnected;
        }

        public override async Task<bool> OnApplicationLogout()
        {
            if (RemoteDesktopProvider.InSession)
            {
                if (await NotificationProvider.ShowWarningQuestion("The remote desktop session with the remote machine will be closed.\nAre you sure you want to log out?"))
                {
                    await RemoteDesktopProvider.EndSession();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region Event Handlers

        private void MachineProvider_MachineConnected(object sender, Common.Connection.MachineConnectedEventArgs e)
        {
            Source = null;
        }

        private void RemoteDesktopProvider_FrameReceived(object sender, DesktopFrameReceivedEventArgs e)
        {
            Source = e.Source;
        }

        #endregion

        #region Start / Stop Session

        private async void StartRemoteDesktop()
        {
            try
            {
                await RemoteDesktopProvider.StartSession();
            }
            catch (Exception ex)
            {
                await NotificationProvider.ShowError($"Error starting the remote desktop session.\n{ex.FlattenMessage()}");
            }
        }

        private async void StopRemoteDesktop()
        {
            try
            {
                await RemoteDesktopProvider.EndSession();
            }
            catch (Exception ex)
            {
                await NotificationProvider.ShowError($"Error stopping the remote desktop session.\n{ex.FlattenMessage()}");
            }
        }

        #endregion

        #region Mouse & Keyboard Handlers From View

        public void OnMouseDown(MouseButton changedButton, System.Windows.Point point, System.Windows.Size size)
        {
            RemoteDesktopProvider.MouseDown(changedButton, point, size);
        }

        public void OnMouseUp(MouseButton changedButton, System.Windows.Point point, System.Windows.Size size)
        {
            RemoteDesktopProvider.MouseUp(changedButton, point, size);
        }

        public void OnMouseMove(System.Windows.Point point, System.Windows.Size size)
        {
            RemoteDesktopProvider.MouseMove(point, size);
        }

        public void OnMouseDoubleClick(MouseButton changedButton, System.Windows.Point point, System.Windows.Size size)
        {
            RemoteDesktopProvider.MouseDoubleClick(changedButton, point, size);
        }

        public void OnKeyboardDown(Key key, bool ctrlDown, bool shitDown, bool altDown)
        {
            RemoteDesktopProvider.KeyboardDown(key, ctrlDown, shitDown, altDown);
        }

        public void OnKeyboardUp(Key key, bool ctrlDown, bool shitDown, bool altDown)
        {
            RemoteDesktopProvider.KeyboardUp(key, ctrlDown, shitDown, altDown);
        }

        #endregion

        #region Remote Actions

        private void OpenTaskManager()
        {
            RemoteDesktopProvider.KeyboardDown(Key.Escape, true, true, false);
            RemoteDesktopProvider.KeyboardDown(Key.Escape, false, false, false);
        }

        private void HideAndOpenShell()
        {
            RemoteDesktopProvider.SendCommand(RemoteDesktopCommand.HideAndOpenShell);
        }

        #endregion
    }
}