interlace /source/samples/ObviousCode.Interlace.BitTunnel/BitTunnelClientExample/Controls/SharedFilesControl.cs

Language C# Lines 303
MD5 Hash a4fd49d5dd966b5089e3f658064bcb80 Estimated Cost $6,143 (why?)
Repository https://bitbucket.org/VahidN/interlace.git View Raw File View Project SPDX
  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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObviousCode.Interlace.BitTunnel.Connectivity;
using ObviousCode.Interlace.BitTunnelLibrary.Events;
using ObviousCode.Interlace.BitTunnelLibrary.File;
using System.IO;
using ExampleLibrary;
using BitTunnelExampleLibrary.File;

namespace BitTunnelClientExample.Controls
{
    public partial class SharedFilesControl : UserControl, IClientTabControl
    {
        ClientInstance _client;
        ListViewHelper _helper;
        List<FileSystemWatcher> _watchers;

        public SharedFilesControl(ClientInstance client)
        {
            InitializeComponent();
            Client = client;

            _helper = new ListViewHelper(_sharedFilesView, ListViewHelper.ComparisonKey.Hash);
            _watchers = new List<FileSystemWatcher>();
        }

        #region IClientTabControl Members

        public string TabText
        {
            get 
            {
                return "Locally Shared Files";
            }
        }

        
        public ObviousCode.Interlace.BitTunnel.Connectivity.ClientInstance Client
        {
            get
            {
                return _client;
            }
            set
            {
                _client = value;
                
                _client.FileListUpdateReceived +=new EventHandler<FileListModificationEventArgs>(_client_FileListUpdateReceived);
                _client.FullFileListReceived += new EventHandler<FileListEventArgs>(_client_FullFileListReceived);
                _client.ConnectionTerminated += new EventHandler(_client_ConnectionTerminated);
                _client.LostConnection += new EventHandler<ExceptionEventArgs>(_client_LostConnection);
            }
        }

        void _client_LostConnection(object sender, ExceptionEventArgs e)
        {
            EnableToolbar(false);                        
        }

        void _client_ConnectionTerminated(object sender, EventArgs e)
        {
            EnableToolbar(false);
        }

        void _client_FullFileListReceived(object sender, FileListEventArgs e)
        {
            EnableToolbar(true);
        }

        private void EnableToolbar(bool enable)
        {
            if (InvokeRequired)
            {
                Invoke((MethodInvoker)delegate
                {
                    EnableToolbar(enable);
                });
            }
            else
            {
                _tools.Enabled = true;
            }
        }

        void _client_FileListUpdateReceived(object sender, ObviousCode.Interlace.BitTunnelLibrary.Events.FileListModificationEventArgs e)
        {
            foreach (FileModificationDescriptor file in e.Modifications)
            {
                if (file.OriginId != _client.ConnectionDetails.InstanceId) continue;

                if (file.Mode == FileModificationMode.New)
                {
                    if (file.OriginId != _client.ConnectionDetails.InstanceId) continue;

                    SetFileAsAvailable(file.Hash);
                } 
                else if (file.Mode == FileModificationMode.Renamed)
                {
                    RenameFile(file.ToFileDescriptor());
                }
            }
        }

        private void RenameFile(FileDescriptor file)
        {
            if (InvokeRequired)
            {
                Invoke((MethodInvoker)
                    delegate{RenameFile(file);}
                    );
            }
            else
            {
                ListViewItem item = _helper.GetListViewItem(file.Hash);

                item.Text = file.FileName;

                item.Tag = CreateSharedFile(file);
            }
        }

        
        private void SetFileAsAvailable(string hash)
        {
            if (InvokeRequired)
            {
                Invoke((MethodInvoker)delegate
                {
                    SetFileAsAvailable(hash);
                });
            }
            else
            {                 
                ListViewItem item = _helper.GetListViewItem(hash);

                if (item == null)
                {
                    MessageBox.Show(string.Format("Warning, locally available file (hash: {0}) not in list", hash));
                }

                (item.Tag as SharedFile).SetFileAvailable();
            }
        }

        private void AddFileToList(FileDescriptor file)
        {
            Invoke((MethodInvoker)delegate
            {
                if (_helper.GetListViewItem(file.FileFullName, ListViewHelper.ComparisonKey.FileFullName) != null) return;

                _helper.AddFileToList(CreateSharedFile(file),
                    file.FileName,
                    file.FileFullName,
                    string.Format("{0} kb", Math.Round((double)(file.Size / 1024), 2)),
                    ""                    
                    );
            });            
        }        

        private SharedFile CreateSharedFile(FileDescriptor fileDescriptor)
        {
            SharedFile shared = new SharedFile(fileDescriptor);

            shared.PropertyChanged += new PropertyChangedEventHandler(shared_PropertyChanged);

            return shared;
        }

        void shared_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "State")
            {
                SetViewState(sender as SharedFile);                                
            }
        }

        private void SetViewState(SharedFile sharedFile)
        {
            if (InvokeRequired)
            {
                Invoke((MethodInvoker)delegate { SetViewState(sharedFile); });
            }
            else
            {
                ListViewItem item = _helper.GetListViewItem(sharedFile.File.Hash);

                if (item == null) return;

                item.SubItems[3] = new ListViewItem.ListViewSubItem(item, sharedFile.State.ToString());            
            }
        }        


        #endregion

        private void _addFilesToolStripButton_Click(object sender, EventArgs e)
        {
            if (_fileDialog.ShowDialog() == DialogResult.OK)
            {
                List<FileDescriptor> files = new List<FileDescriptor>();

                foreach (string file in _fileDialog.FileNames)
                {
                    if (_helper.GetListViewItem(file, ListViewHelper.ComparisonKey.FileFullName) != null) continue;

                    ShareFile(file);
                }
            }
        }

        private void ShareFile(string file)
        {
            FileDescriptor descriptor = FileDescriptor.Create(file, false);
            AddFileToList(descriptor);

            descriptor.HashGenerationCompleted += new EventHandler(descriptor_HashGenerationCompleted);

            descriptor.GenerateHash();
        }

        void descriptor_HashGenerationCompleted(object sender, EventArgs e)
        {
            FileDescriptor descriptor = sender as FileDescriptor;

            _client.AddFiles(new FileDescriptor[] { descriptor });
        }

        private void _sharedFilesView_SelectedIndexChanged(object sender, EventArgs e)
        {
            _removeSelectedFiles.Enabled = _sharedFilesView.SelectedItems.Count > 0;
            _removeSelectedFiles.Text = string.Format("Remove File{0}", _sharedFilesView.SelectedItems.Count > 1 ? "s" : "");
        }

        private void  _removeSelectedFiles_Click(object sender, EventArgs e)
        {
            foreach(ListViewItem item in _sharedFilesView.SelectedItems)
            {
                RemoveFile(item.Tag as SharedFile);
            }
        }

        private void RemoveFile(SharedFile sharedFile)
        {
            try
            {
                sharedFile.SetFileAsRemoving();

                _client.RemoveFiles(new FileDescriptor[] { sharedFile.File });

                Invoke((MethodInvoker)delegate { _helper.RemoveFileFromListView(sharedFile.File.Hash); });

            }
            catch(NotImplementedException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void _addFolderToolstripButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                foreach (FileInfo file in new DirectoryInfo(fbd.SelectedPath).GetFiles())
                {
                    if (((int)file.Attributes & (int)FileAttributes.System) != 0) continue;

                    ShareFile(file.FullName);
                }

                _watchers.Add(GetFileSystemWatcher(fbd.SelectedPath));                
            }
        }

        private FileSystemWatcher GetFileSystemWatcher(string path)
        {
            FileSystemWatcher watcher = new FileSystemWatcher(path);
            
            watcher.Created += new FileSystemEventHandler(watcher_Created);
            watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);

            return watcher;
        }

        void watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            
        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            
        }   
    }
}
Back to Top