public DownloadFile(ClientLogic clientLogic, string root, string nameFile, Boolean searchPass, MainWindow mainw, String idFile)
        {
            try
            {
                InitializeComponent();
                mw = mainw;
                clientlogic = clientLogic;
                search = searchPass;
                folder = root;
                namefile = nameFile;
                completePath = folder + @"\" + namefile;
                App.Current.MainWindow.Width = 600;
                App.Current.MainWindow.Height = 430;
                clientLogic.WriteStringOnStream(ClientLogic.GETVFILE + clientLogic.username + "+" + folder + "+" + completePath + "+" + idFile);
                String retFiles;
                Boolean exit = false;

                while (!exit)
                {
                    retFiles = clientLogic.ReadStringFromStream();
                    String[] parametri = retFiles.Split('+');
                    String comando = parametri[1];
                    if (comando.Equals("FLP"))
                    {
                        addElementToListbox(parametri[3]);
                        clientlogic.WriteStringOnStream(ClientLogic.OK);
                    }
                    else if (comando.Equals("ENDLIST") || comando.Equals("INFO"))
                    {
                        exit = true;
                    }
                    else
                    {
                        exit = true;
                    }
                }
                if (!search)
                    addElementToListbox("...");
            }
            catch
            {
                if (App.Current.MainWindow is Restore)
                    App.Current.MainWindow.Close();
                if (clientLogic.clientsocket.Client.Connected)
                {
                    clientLogic.clientsocket.GetStream().Close();
                    clientLogic.clientsocket.Close();
                }
                App.Current.MainWindow = mainw;
                MainControl main = new MainControl(1);
                App.Current.MainWindow.Content = main;
                return;
            }
        }
 public MainWindow()
 {
     InitializeComponent();
     closing = false;
     this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
     this.Top = SystemParameters.PrimaryScreenHeight - this.Height - 40;
     MainControl main = new MainControl(0);
     App.Current.MainWindow.Content = main;
     MyNotifyIcon = new System.Windows.Forms.NotifyIcon();
     MyNotifyIcon.Icon = new System.Drawing.Icon(@"Images/uploadicon.ico");
     MyNotifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(MyNotifyIcon_MouseClick);
 }
        private void File_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FileSelection w = new FileSelection(clientLogic, folderRoot, "", false, ((Restore)App.Current.MainWindow).mw);
                if (App.Current.MainWindow is Restore)
                    App.Current.MainWindow.Content = w;
            }
            catch
            {
                if (App.Current.MainWindow is Restore)
                    App.Current.MainWindow.Close();
                App.Current.MainWindow = mw;
                if (mw.clientLogic.clientsocket.Connected)
                {
                    mw.clientLogic.clientsocket.GetStream().Close();
                    mw.clientLogic.clientsocket.Close();
                }
                MainControl main = new MainControl(1);
                App.Current.MainWindow.Content = main;

            }

        }
        private void ExitStub(int obj)
        {
            if (App.Current.MainWindow is Restore)
                App.Current.MainWindow.Close();
            if (clientLogic.clientsocket.Client.Connected)
            {
                clientLogic.clientsocket.GetStream().Close();
                clientLogic.clientsocket.Close();
            }
            App.Current.MainWindow = mw;
            MainControl main = new MainControl(1);
            mw.Content = main;
            return;

        }
        public RestoreUC(ClientLogic clientLogic, MainWindow mainw)
        {
            try
            {
                InitializeComponent();
                clientlogic = clientLogic;
                mw = mainw;
                App.Current.MainWindow.Width = 400;
                App.Current.MainWindow.Height = 400;
                clientLogic.WriteStringOnStream(ClientLogic.GETFOLDERUSER + clientLogic.username);
                String retFolders = clientLogic.ReadStringFromStream();
                String[] parametri = retFolders.Split('+');
                String comando = parametri[1];
                if (comando.Equals("OK"))
                {
                    String[] folders = parametri[2].Split(';');
                    int numParametri = folders.Length;
                    if (numParametri > 1)
                    {
                        noFolder.Visibility = Visibility.Hidden;
                        for (int i = 0; i < numParametri; i++)
                        {
                            if (folders[i] != string.Empty)
                            {
                                addElementToListbox(folders[i]);
                            }
                        }

                    }
                    else
                    {
                        noFolder.Visibility = Visibility.Visible;
                    }
                }
                else
                {
                    if (App.Current.MainWindow is Restore)
                        App.Current.MainWindow.Close();
                    if (clientLogic.clientsocket.Client.Connected)
                    {
                        clientLogic.clientsocket.GetStream().Close();
                        clientLogic.clientsocket.Close();
                    }
                    App.Current.MainWindow = mainw;
                    MainControl main = new MainControl(1);
                    App.Current.MainWindow.Content = main;
                    return;
                }
            }
            catch
            {
                if (App.Current.MainWindow is Restore)
                    App.Current.MainWindow.Close();
                if (clientLogic.clientsocket.Client.Connected)
                {
                    clientLogic.clientsocket.GetStream().Close();
                    clientLogic.clientsocket.Close();
                }
                App.Current.MainWindow = mainw;
                MainControl main = new MainControl(1);
                App.Current.MainWindow.Content = main;
                return;
            }


        }
        private void RestoreFile_Click(object sender, RoutedEventArgs e)
        {
            if (mw.clientLogic.clientsocket.Client.Poll(1000, SelectMode.SelectRead))
            {
                MainControl main = new MainControl(1);
                App.Current.MainWindow.Content = main;
                messaggioErrore("Connessione Persa");
                return;

            }

            ClientLogic clRestore = new ClientLogic(mw.clientLogic.ip, mw.clientLogic.porta, mw.clientLogic.folder, mw.clientLogic.username, mw.clientLogic.folderR);
            Window w = null;
            try
            {
                w = new Restore(clRestore, mw);
                w.ShowDialog();
            }
            catch (Exception)
            {
                if (clRestore.clientsocket.Client.Connected)
                {
                    clRestore.clientsocket.GetStream().Close();
                    clRestore.clientsocket.Client.Close();
                }

                if (mw.clientLogic.clientsocket.Client.Connected)
                {
                    mw.clientLogic.clientsocket.GetStream().Close();
                    mw.clientLogic.clientsocket.Close();
                }

                if (App.Current.MainWindow is Restore)
                    App.Current.MainWindow.Close();
                MainControl main = new MainControl(1);
                App.Current.MainWindow.Content = main;
                messaggioErrore("Connessione Persa");
                return;
            }

            App.Current.MainWindow = mw;
            App.Current.MainWindow.Width = 400;
            App.Current.MainWindow.Height = 400;
        }
 private void Workertransaction_WaitedCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     try
     {
         pbStatus.Value = 0;
         pbStatus.Visibility = Visibility.Hidden;
         Wait.Visibility = Visibility.Hidden;
         EffettuaBackup.IsEnabled = true;
         FolderButton.IsEnabled = true;
         EffettuaBackup.Visibility = Visibility.Visible;
         FileUploading.Text = "Ultima sincronizzazione : " + DateTime.Now;
         watcher.EnableRaisingEvents = false;
         watcher.Dispose();
         updating = false;
         if (exit)
         {
             MainWindow mainw = (MainWindow)App.Current.MainWindow;
             mainw.clientLogic.monitorando = false;
             mainw.clientLogic.lavorandoInvio = false;
             mainw.clientLogic.WriteStringOnStream(ClientLogic.DISCONETTIUTENTE + mainw.clientLogic.username + "+" + mainw.clientLogic.mac);
             mainw.clientLogic.connesso = false;
             ClientLogic.UpdateNotifyIconDisconnesso();
             if (mainw.clientLogic.clientsocket.Client.Connected)
             {
                 mainw.clientLogic.clientsocket.GetStream().Close();
                 mainw.clientLogic.clientsocket.Close();
             }
             MainControl main = new MainControl(0);
             App.Current.MainWindow.Content = main;
         }
     }
     catch
     {
         Thread t = new Thread(new ThreadStart(delegate { Dispatcher.Invoke(DispatcherPriority.Normal, new Action<MainWindow>(ChangeWindow), mw); }));
         t.Start();
     }
 }
        private void ChangeWindow(MainWindow obj)
        {
            if (App.Current.MainWindow is Restore)
                App.Current.MainWindow.Close();
            if (obj.clientLogic.clientsocket.Connected)
            {
                Console.WriteLine("socket CHIUSO");
                obj.clientLogic.clientsocket.GetStream().Close();
                obj.clientLogic.clientsocket.Close();
            }

            App.Current.MainWindow = obj;
            updating = false;
            MainControl main = new MainControl(1);
            mw.Content = main;
        }
        public FileSelection(ClientLogic clientLogic, string folder, string likeNomePass, Boolean searchBar, MainWindow mainw)
        {
            try
            {
                InitializeComponent();
                mw = mainw;
                listElement = new List<string>();
                search = searchBar;
                selFolderPath = folder;
                likeNome = likeNomePass;
                clientlogic = clientLogic;
                App.Current.MainWindow.Width = 600;
                App.Current.MainWindow.Height = 430;
                clientLogic.WriteStringOnStream(ClientLogic.LISTFILES + clientLogic.username + "+" + folder + "+" + likeNome);



                String retFiles;
                Boolean exit = false;
                Boolean fine = false;
                while (!exit)
                {
                    retFiles = clientLogic.ReadStringFromStream();
                    String[] parametri = retFiles.Split('+');
                    String comando = parametri[1];
                    if (comando.Equals("FLP"))
                    {
                        noFile.Visibility = Visibility.Hidden;
                        addElementToListbox(parametri[3]);
                        clientlogic.WriteStringOnStream(ClientLogic.OK);
                    }
                    else if (comando.Equals("ENDLIST") || comando.Equals("INFO"))
                    {
                        exit = true;
                    }
                    else
                    {
                        exit = true;
                        fine = true;
                    }
                }
                if (fine)
                {
                    App.Current.MainWindow.Close();
                }
                if (!likeNome.Equals(String.Empty) && !search)
                {
                    addElementToListbox("...");
                }
            }
            catch (Exception)
            {
                if (App.Current.MainWindow is Restore)
                    App.Current.MainWindow.Close();
                if (clientLogic.clientsocket.Client.Connected)
                {
                    clientLogic.clientsocket.GetStream().Close();
                    clientLogic.clientsocket.Close();
                }
                App.Current.MainWindow = mainw;
                MainControl main = new MainControl(1);
                App.Current.MainWindow.Content = main;
                return;

            }

        }