private void Sync_init_button_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         DispatcherOperation result = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new LoginDelegate(updateUI_logged));
         result.Wait();
         RootDirectory = (String)result.Result;
         if (RootDirectory != null && RootDirectory != String.Empty)
         {
             this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
             {
                 this.Sync_init_button.Visibility = Visibility.Hidden;
                 this.Home_button.Visibility = Visibility.Hidden;
                 this.Restore_init_button.Visibility = Visibility.Hidden;
                 this.progressBar_file.Visibility = Visibility.Visible;
                 this.progressBar_file.IsIndeterminate = true;
                 this.Label_log.Content = "Retrieving directory infromation . . .";
                 this.Label_log.Visibility = Visibility.Visible;
             }));
             if (!client.resumeSession())
             {
                 connected = false;
                 client.TcpClient.Close();
                 String msg = "Log in to the remote server";
                 String title = "You were disconnected";
                 String bannerMsg = "Your session is expired, please login again";
                 this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, title, bannerMsg);
             }
             else
             {
                 DirectoryStatus local = new DirectoryStatus();
                 local.FolderPath = RootDirectory;
                 local.Username = client.UserId;
                 CurrentDirectory = String.Copy(RootDirectory);
                 client.fillDirectoryStatus(local, RootDirectory);
                 DirectoryStatus remote = new DirectoryStatus();
                 remote.Username = client.UserId;
                 remote.FolderPath = RootDirectory;
                 // for updating the progress bar
                 PbUpdater up = new PbUpdater();
                 up.rootDir = RootDirectory;
                 up.remote = remote;
                 up.local = local;
                 sync_worker.RunWorkerAsync(up);
                 this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                 {
                     this.progressBar_file.IsIndeterminate = false;
                     this.Label_log.Content = "Checking for previous sessions of the directory ...";
                 }));
                 // try to free some memory ...
                 local = null;
                 remote = null;
                 GC.Collect();
             }
         }
     }
     catch (UnauthorizedAccessException)
     {
         this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(async () =>
         {
             await this.ShowMessageAsync("Operation not permitted", "You don't have the rights to synchornize this folder. Please choose another folder and retry.");
             this.Grid_initial.Visibility = Visibility.Collapsed;
             this.progressBar_file.Visibility = Visibility.Hidden;
             this.Grid_logged.Visibility = Visibility.Visible;
             this.file_grid.Visibility = Visibility.Hidden;
             this.dir_grid.Visibility = Visibility.Hidden;
             this.fileSnap_grid.Visibility = Visibility.Hidden;
             this.Back_button.Visibility = Visibility.Hidden;
             this.Back_button_2.Visibility = Visibility.Collapsed;
             this.Refresh_button.Visibility = Visibility.Hidden;
             this.Home_button.Visibility = Visibility.Hidden;
             this.Sync_init_button.Visibility = Visibility.Visible;
             this.Restore_init_button.Visibility = Visibility.Visible;
             this.Label_log.Visibility = Visibility.Visible;
             this.Label_log.Content = "Welcome back, " + client.UserId + "! Choose one of the following options";
         }));
     }
     catch (IOException)
     {
         this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(async () =>
         {
             await this.ShowMessageAsync("Something were wrong", "An error occurred. Please check if one file that must be synchronized is opened by another process.");
             this.Grid_initial.Visibility = Visibility.Collapsed;
             this.Grid_logged.Visibility = Visibility.Visible;
             this.progressBar_file.Visibility = Visibility.Hidden;
             this.file_grid.Visibility = Visibility.Hidden;
             this.dir_grid.Visibility = Visibility.Hidden;
             this.fileSnap_grid.Visibility = Visibility.Hidden;
             this.Back_button.Visibility = Visibility.Hidden;
             this.Back_button_2.Visibility = Visibility.Collapsed;
             this.Refresh_button.Visibility = Visibility.Hidden;
             this.Home_button.Visibility = Visibility.Hidden;
             this.Sync_init_button.Visibility = Visibility.Visible;
             this.Restore_init_button.Visibility = Visibility.Visible;
             this.Label_log.Visibility = Visibility.Visible;
             this.Label_log.Content = "Welcome back, " + client.UserId + "! Choose one of the following options";
         }));
     }
 }
 private void Restore_Button_Click(object sender, RoutedEventArgs e)
 {
     if (!client.resumeSession())
     {
         connected = false;
         client.TcpClient.Close();
         if (watcher != null)
             watcher.Stop();
         String msg = "Log in to the remote server";
         String title = "You were disconnected";
         String bannerMsg = "Your session is expired, please login again";
         this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, title, bannerMsg);
         return;
     }
     ListItem item = ((FrameworkElement)sender).DataContext as ListItem;
     String path = System.IO.Path.Combine(item.Path, item.Filename);
     if (item.Directory)
     {
         //restore directory
         watcher.Stop();
         if (!Directory.Exists(path))
             Directory.CreateDirectory(path);
         PbUpdater up = new PbUpdater();
         up.path = path;  
         restore_worker.RunWorkerAsync(up);
     }
     else
     {
         if (item.Deleted)
         {
             //restore file
             watcher.Stop();
             if (!Directory.Exists(item.Path))
                 Directory.CreateDirectory(item.Path);
             PbUpdater up = new PbUpdater();
             up.path = path;
             up.id = item.Id;
             restore_worker.RunWorkerAsync(up);
         }
         else
         {
             //for turn back on the directory
             CurrentDirectory = path;
             //see older versions of a file
             DirectoryStatus dir = new DirectoryStatus();
             this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
             {
                 this.Back_button.IsEnabled = false;
                 this.Refresh_button.IsEnabled = false;
             }));
             int n = client.getPreviousVersions(dir, CurrentDirectory);
             if (n < 0)
             {
                 connected = false;
                 client.TcpClient.Close();
                 if (watcher != null)
                     watcher.Stop();
                 String msg = "Log in to the remote server";
                 String title = "You were disconnected";
                 String bannerMsg = "A Network Error occurred, please try later";
                 this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, title, bannerMsg);
             }
             if (n == 0)
             {
                 this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                 {
                     FileList.Clear();
                     this.file_grid.ItemsSource = FileList;
                     this.Back_button.IsEnabled = true;
                     this.Label_log.Content = "No previous version for the file selected";
                     this.Label_log.Visibility = Visibility.Visible;
                 }));
             }
             else
                 this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new FillGrid(fill_prev_grid), dir);
         }
     }
 }
        private void rw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null || e.Cancelled)
                {
                    //something went wrong during restore
                    connected = false;
                    client.TcpClient.Close();
                    String msg = "Log in to the remote server";
                    String title = "You were disconnected";
                    String bannerMsg = "An error on the server occurred, please try later";
                    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, title, bannerMsg);
                    return;
                }
                else
                {
                    bool result = (bool)e.Result;
                    if (result)
                    {
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                        {
                            this.progressBar_file.Visibility = Visibility.Visible;
                            this.progressBar_file.IsIndeterminate = true;
                            this.Label_log.Content = "Resyncing with the server . . .";
                            this.Label_log.Visibility = Visibility.Visible;
                        }));

                        DirectoryStatus local = new DirectoryStatus();
                        local.FolderPath = RootDirectory;
                        local.Username = client.UserId;
                        CurrentDirectory = String.Copy(RootDirectory);
                        client.fillDirectoryStatus(local, RootDirectory);
                        DirectoryStatus remote = new DirectoryStatus();
                        remote.Username = client.UserId;
                        remote.FolderPath = RootDirectory;
                        // for updating the progress bar
                        PbUpdater up = new PbUpdater();
                        up.rootDir = RootDirectory;
                        up.remote = remote;
                        up.local = local;
                        sync_worker.RunWorkerAsync(up);
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                        {
                            this.progressBar_file.IsIndeterminate = true;
                            this.progressBar_file.Visibility = Visibility.Visible;
                            this.file_grid.Visibility = Visibility.Hidden;
                            this.Back_button.Visibility = Visibility.Hidden;
                            this.Home_button.Visibility = Visibility.Hidden;
                            this.Refresh_button.Visibility = Visibility.Hidden;
                            this.Label_log.Content = "Restoring directory " + up.rootDir + "...";
                            this.Label_log.Visibility = Visibility.Visible;
                        }));
                        // try to free some memory ...
                        local = null;
                        remote = null;
                        //GC.Collect();
                    }
                    else
                    {
                        //something went wrong during restore
                        connected = false;
                        client.TcpClient.Close();
                        String msg = "Log in to the remote server";
                        String title = "You were disconnected";
                        String bannerMsg = "An error on the server occurred, please try later";
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, title, bannerMsg);
                    }
                }
            }
            catch (UnauthorizedAccessException) { }
            catch (IOException) { }
        }
        private void Download_Button_Click(object sender, RoutedEventArgs e)
        {
            if (!client.resumeSession())
            {
                connected = false;
                if (watcher != null)
                    watcher.Stop();
                client.TcpClient.Close();
                String msg = "Log in to the remote server";
                String title = "You were disconnected";
                String bannerMsg = "Your session is expired, please login again";
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, title, bannerMsg);
                return;
            }
            ListItem item = ((FrameworkElement)sender).DataContext as ListItem;
            DispatcherOperation result = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new LoginDelegate(chooseDirectoryWhereDownload));
            result.Wait();
            String basedir = (String)result.Result;
            if (basedir != null && basedir != String.Empty)
            {
                String dirName = System.IO.Path.GetFileName(item.Path);
                String newRoot = System.IO.Path.Combine(basedir, dirName);
                if (!Directory.Exists(newRoot))
                    Directory.CreateDirectory(newRoot);
                else
                {
                    dirName += "_" + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss");
                    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(async () => {
                        await this.ShowMessageAsync("Attention!", "In the selected path already exist a directory called " +
                            System.IO.Path.GetFileName(item.Path) + ", so it will be renamed in " + dirName);
                    }));
                    newRoot = System.IO.Path.Combine(basedir, dirName);
                }

                PbUpdater up = new PbUpdater();
                up.rootDir = newRoot;
                up.path = item.Path;
                up.creationTime = Convert.ToDateTime(item.LastModificationTime);
                download_worker.RunWorkerAsync(up);
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    this.progressBar_file.IsIndeterminate = true;
                    this.progressBar_file.Visibility = Visibility.Visible;
                    this.Back_button.Visibility = Visibility.Hidden;
                    this.Back_button_2.Visibility = Visibility.Hidden;
                    this.Home_button.Visibility = Visibility.Hidden;
                    this.Refresh_button.Visibility = Visibility.Hidden;
                    this.fileSnap_grid.Visibility = Visibility.Hidden;
                    this.dir_grid.Visibility = Visibility.Hidden;
                    this.file_grid.Visibility = Visibility.Hidden;
                    this.Label_log.Content = "Downloading directory . . .";
                    this.Label_log.Visibility = Visibility.Visible;
                }));
            }
        }