source: trunk/CrypPlugins/ClipboardInput/ClipboardInput.cs @ 1778

Last change on this file since 1778 was 1259, checked in by Matthäus Wander, 12 years ago

shortened license text to boilerplate notice in all source files

File size: 7.0 KB
Line 
1/*
2   Copyright 2008 Timm Korte, University of Siegen
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.IO;
22using Cryptool.PluginBase;
23using System.Windows.Forms;
24using System.Threading;
25using System.ComponentModel;
26using Cryptool.PluginBase.IO;
27
28namespace ClipboardInput
29{
30  // Retrieves data from clipboard and passes it on as a stream
31  [Author("Timm Korte", "cryptool@easycrypt.de", "Uni Bochum", "http://www.ruhr-uni-bochum.de")]
32  [PluginInfo(true, "ClipboardInput", "Simple clipboard input plugin.", "", "ClipboardInput/icon.png")]
33  public class ClipboardInput : IInput
34  {
35
36    #region Private Variables
37    private List<CryptoolStream> listCryptoolStreamsOut = new List<CryptoolStream>();
38    // private CryptoolStream clipboard;
39    private ClipboardInputSettings settings;
40
41    public ISettings Settings
42    {
43      get { return (ISettings)settings; }
44      set { settings = (ClipboardInputSettings)value; }
45    }
46
47    #endregion
48
49    public ClipboardInput()
50    {
51      this.settings = new ClipboardInputSettings();
52    }
53
54
55    private string data;
56    // [QuickWatch(QuickWatchFormat.Text, DisplayLevel.Beginner, null)]
57    public string Data
58    {
59      get { return data; }
60      set
61      {
62        data = value;
63        // OnPropertyChanged("Data");
64      }
65    }
66
67    private void GetClipboardData()
68    {
69      try
70      {
71        Data = Clipboard.GetText();
72      }
73      catch (Exception ex)
74      {
75        GuiLogMessage(ex.Message, NotificationLevel.Error);
76      }
77    }
78
79    #region Interface
80    [PropertyInfo(Direction.OutputData, "Clipboard data stream", "Stream of data recieved from clipboard", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
81    public CryptoolStream StreamOutput
82    {
83      get
84      {
85        Progress(0.5, 1.0);
86        Thread t = new Thread(new ThreadStart(GetClipboardData));
87        t.SetApartmentState(ApartmentState.STA);
88        t.Start();
89        t.Join();
90
91        if (Data != null && Data != string.Empty)
92        {
93          CryptoolStream cryptoolStream = null;
94          switch (settings.Format)
95          { //0="Text", 1="Hex", 2="Base64"
96            case 1:
97              GuiLogMessage("converting " + Data.Length + " bytes of clipboard data from hex...", NotificationLevel.Debug);
98              cryptoolStream = Hex2Stream(Data);
99              break;
100            case 2:
101              GuiLogMessage("converting " + Data.Length + " bytes of clipboard data from base64...", NotificationLevel.Debug);
102              cryptoolStream = Base642Stream(Data);
103              break;
104            default:
105              GuiLogMessage("converting " + Data.Length + " bytes of clipboard data from text...", NotificationLevel.Debug);
106              cryptoolStream = Text2Stream(Data);
107              break;
108          }
109          Progress(1.0, 1.0);
110          listCryptoolStreamsOut.Add(cryptoolStream);
111          return cryptoolStream;
112        }
113        else
114        {
115          GuiLogMessage("clipboard did not contain any text data", NotificationLevel.Error);
116          return null;
117        }
118      }
119      set
120      {
121      }
122    }
123    #endregion
124
125    private CryptoolStream Text2Stream(string data)
126    {
127      CryptoolStream cs = new CryptoolStream();
128      cs.OpenRead(this.GetPluginInfoAttribute().Caption, Encoding.Default.GetBytes(data.ToCharArray()));
129      return cs;
130    }
131
132    private CryptoolStream Base642Stream(string data)
133    {
134      byte[] temp = new byte[0];
135      try
136      {
137        temp = Convert.FromBase64String(data);
138      }
139      catch (Exception exception)
140      {
141        GuiLogMessage(exception.Message, NotificationLevel.Error);
142      }
143      CryptoolStream cs = new CryptoolStream();
144      cs.OpenRead(this.GetPluginInfoAttribute().Caption, temp);
145      return cs;     
146    }
147
148    private CryptoolStream Hex2Stream(string data)
149    {
150      CryptoolStream cs = new CryptoolStream();
151      cs.OpenRead(this.GetPluginInfoAttribute().Caption, ToByteArray(data));
152      return cs;
153    }
154
155    public byte[] ToByteArray(String HexString)
156    {
157      int NumberChars = HexString.Length;
158      byte[] bytes = new byte[NumberChars / 2];
159      for (int i = 0; i < NumberChars; i += 2)
160      {
161        try
162        {
163          bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
164        }
165        catch (Exception exception)
166        {
167
168          GuiLogMessage(exception.Message, NotificationLevel.Error);
169          bytes[i / 2] = 0;
170        }
171      }
172      return bytes;
173    }
174
175    #region IPlugin Member
176    public event StatusChangedEventHandler OnPluginStatusChanged;
177    public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
178    public event PluginProgressChangedEventHandler OnPluginProgressChanged;
179
180    public System.Windows.Controls.UserControl Presentation
181    {
182      get { return null; }
183    }
184
185    public System.Windows.Controls.UserControl QuickWatchPresentation
186    {
187      get { return null; }
188    }
189
190    public void Initialize()
191    {
192    }
193
194    public void Dispose()
195    {
196      foreach (CryptoolStream cryptoolStream in listCryptoolStreamsOut)
197      {
198        cryptoolStream.Close();
199      }
200      listCryptoolStreamsOut.Clear();
201    }
202
203    public void Stop()
204    {
205
206    }
207
208    public void PreExecution()
209    {
210      Dispose();
211    }
212
213    public void PostExecution()
214    {
215      Dispose();
216    }
217
218    #endregion
219
220    #region INotifyPropertyChanged Members
221
222    public event PropertyChangedEventHandler PropertyChanged;
223
224    public void OnPropertyChanged(string name)
225    {
226      if (PropertyChanged != null)
227      {
228        PropertyChanged(this, new PropertyChangedEventArgs(name));
229      }
230    }
231
232    #endregion
233
234    private void Progress(double value, double max)
235    {
236      if (OnPluginProgressChanged != null)
237        OnPluginProgressChanged(this, new PluginProgressEventArgs(value, max));
238    }
239
240    private void GuiLogMessage(string message, NotificationLevel logLevel)
241    {
242      if (OnGuiLogNotificationOccured != null)
243      {
244        OnGuiLogNotificationOccured(this, new GuiLogEventArgs(message, this, logLevel));
245      }
246    }
247
248    #region IPlugin Members
249
250
251    public void Execute()
252    {
253      OnPropertyChanged("StreamOutput");
254    }
255
256    public void Pause()
257    {
258
259    }
260
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.