Esempio n. 1
0
 private void LogInOut_Click(object sender, RoutedEventArgs e)
 {
     if (loggedin)
     {
         forceStop();
         lUsername.Content = "Please login";
         bLogInOut.Content = "Login";
         bStart.IsEnabled  = false;
         loggedin          = false;
         try
         {
             Command logoutComm = new ComandoEsci();
             logoutComm.esegui();
         }
         catch { }
     }
     lDetails.Items.Clear();
     lFileVersions.Items.Clear();
     lDeletedFiles.Items.Clear();
     lDeletedFileVersions.Items.Clear();
     this.openLogin();
 }
 private void LogInOut_Click(object sender, RoutedEventArgs e)
 {
     if (loggedin)
     {
         forceStop();
         lUsername.Content = "Please login";
         bLogInOut.Content = "Login";
         bStart.IsEnabled = false;
         loggedin = false;
         try
         {
             Command logoutComm = new ComandoEsci();
             logoutComm.esegui();
         }
         catch { }
     }
     lDetails.Items.Clear();
     lFileVersions.Items.Clear();
     lDeletedFiles.Items.Clear();
     lDeletedFileVersions.Items.Clear();
     this.openLogin();
 }
        /// <summary>
        /// Inizializza il controllo periodico del contenuto della cartella.
        /// Controlla anche le credenziali con il server testando il login.
        /// </summary>
        /// <exception cref="ServerException"></exception>
        /// <exception cref="ClientException"></exception>
        static public void Inizializza()
        {
            connectionSetting = new ConnectionSettings();
            user      = connectionSetting.readSetting("account", "username");
            pwd       = connectionSetting.readSetting("account", "password");
            base_path = connectionSetting.readSetting("account", "directory");
            interval  = Int32.Parse(connectionSetting.readSetting("connection", "syncTime"));

            if (!base_path.EndsWith("\\"))
            {
                base_path += "\\";
            }

            Properties.Settings.Default.user       = user;
            Properties.Settings.Default.pwd        = pwd;
            Properties.Settings.Default.base_path  = base_path;
            Properties.Settings.Default.intervallo = interval;
            Properties.Settings.Default.Save();

            if (!init)
            {
                if (user == null || pwd == null)
                {
                    throw new ClientException("Mancano le credenziali dell'utente. Fare il login.", ClientErrorCode.CredenzialiUtenteMancanti);
                }
                if (base_path == null)
                {
                    throw new ClientException("Il percorso di controllo non è specificato", ClientErrorCode.PercorsoNonSpecificato);
                }
                //Controllo se nome utente e password sono giusti
                if (!Command.Logged)
                {
                    try
                    {
                        ComandoLogin login = new ComandoLogin(user, pwd);
                        login.esegui();
                        ComandoEsci esci = new ComandoEsci();
                        esci.esegui();
                    }
                    catch (ServerException e) //Se il server non è disponibile l'eccezione non viene catturata...
                    {
                        switch (e.ErrorCode)
                        {
                        case ServerErrorCode.DatiErrati:
                            throw new ClientException("Le credenziali dell'utente sono errate. Rifare il login o registrarsi.", ClientErrorCode.CredenzialiUtenteErrate);

                        default:
                            throw;
                        }
                    }
                }
                //Controlla se il DB è ok

                //if (!DB_Table.DBEsiste)
                //throw new ClientException("Il database non esiste", ClientErrorCode.DatabaseNonPresente);

                /*
                 * string sha_db = DB_Table.HashDB;
                 * ComandoScaricaHashDB c = new ComandoScaricaHashDB();
                 * c.esegui();
                 * if(sha_db != c.hash)
                 * {
                 *  throw new ClientException("Il database è corrotto o non esiste");
                 * }
                 */
                //Imposta il timer per il controllo periodico
                checker           = new System.Timers.Timer(interval * 1000);
                checker.AutoReset = true;
                checker.Elapsed  += Checker_Elapsed;
                init = true;
                if (!Directory.Exists(base_path))
                {
                    throw new ClientException(Properties.Messaggi.CartellaNonEsistente, ClientErrorCode.CartellaNonEsistente);
                }
            }
            checker.Enabled = true;
        }
        /// <summary>
        /// Inizializza il controllo periodico del contenuto della cartella.
        /// Controlla anche le credenziali con il server testando il login.
        /// </summary>
        /// <exception cref="ServerException"></exception>
        /// <exception cref="ClientException"></exception>
        public static void Inizializza()
        {
            connectionSetting = new ConnectionSettings();
            user = connectionSetting.readSetting("account", "username");
            pwd = connectionSetting.readSetting("account", "password");
            base_path = connectionSetting.readSetting("account", "directory");
            interval = Int32.Parse(connectionSetting.readSetting("connection", "syncTime"));

            if (!base_path.EndsWith("\\"))
            {
                base_path += "\\";
            }

            Properties.Settings.Default.user = user;
            Properties.Settings.Default.pwd = pwd;
            Properties.Settings.Default.base_path = base_path;
            Properties.Settings.Default.intervallo = interval;
            Properties.Settings.Default.Save();

            if (!init)
            {
                if (user == null || pwd == null)
                    throw new ClientException("Mancano le credenziali dell'utente. Fare il login.", ClientErrorCode.CredenzialiUtenteMancanti);
                if (base_path == null)
                    throw new ClientException("Il percorso di controllo non è specificato", ClientErrorCode.PercorsoNonSpecificato);
                //Controllo se nome utente e password sono giusti
                if (!Command.Logged)
                {
                    try
                    {
                        ComandoLogin login = new ComandoLogin(user, pwd);
                        login.esegui();
                        ComandoEsci esci = new ComandoEsci();
                        esci.esegui();
                    }
                    catch (ServerException e) //Se il server non è disponibile l'eccezione non viene catturata...
                    {
                        switch (e.ErrorCode)
                        {
                            case ServerErrorCode.DatiErrati:
                                throw new ClientException("Le credenziali dell'utente sono errate. Rifare il login o registrarsi.", ClientErrorCode.CredenzialiUtenteErrate);
                            default:
                                throw;
                        }
                    }
                }
                //Controlla se il DB è ok

                //if (!DB_Table.DBEsiste)
                    //throw new ClientException("Il database non esiste", ClientErrorCode.DatabaseNonPresente);
                /*
                string sha_db = DB_Table.HashDB;
                ComandoScaricaHashDB c = new ComandoScaricaHashDB();
                c.esegui();
                if(sha_db != c.hash)
                {
                    throw new ClientException("Il database è corrotto o non esiste");
                }
                */
                //Imposta il timer per il controllo periodico
                checker = new System.Timers.Timer(interval * 1000);
                checker.AutoReset = true;
                checker.Elapsed += Checker_Elapsed;
                init = true;
                if (!Directory.Exists(base_path))
                    throw new ClientException(Properties.Messaggi.CartellaNonEsistente, ClientErrorCode.CartellaNonEsistente);
            }
            checker.Enabled = true;
        }