示例#1
0
        public override object Carica()
        {
            XmlDocument xdocument = new XmlDocument();

            try
            {
                //carico il documento
                xdocument.Load(Filename);
                //prendo tutti i nodi con il tag blacklist
                XmlNodeList nodelist = xdocument.SelectNodes("/blacklists/blacklist");
                //Preparo il contenitore
                ISet <string> blacklist = new HashSet <string>();
                //Itero su quegli elementi
                foreach (XmlNode node in nodelist)
                {
                    //Se quell'elemento blacklist ha "me" come proprietario allora è la mia blacklist
                    if (node.Attributes.GetNamedItem("proprietario").Value.Equals(Utente.GetNomeUtente()))
                    {
                        //Itero sui figli di quel nodo per prendere tutti gli utenti nella mia blacklist
                        foreach (XmlNode utenti in node.ChildNodes)
                        {
                            blacklist.Add(utenti.InnerText);
                        }
                    }
                }
                return(blacklist);
            }
            catch
            {
                //Gestione primo avvio
                throw new PersistenceException("File per il caricamento non trovato");
            }
        }
示例#2
0
        public static Entry CreateEntry(IController controller, string operazione,
                                        string utenteMalevolo = "", string sorgente = "", string destinazione = "")
        {
            Type tipo = controller.GetType();

            switch (tipo.Name)
            {
            case ("ImpostazioneTrasferimentoController"):
                return(new EntryImpostazione(operazione, sorgente, destinazione));

            case ("BlackListController"):
                return(new EntryBlacklist(operazione, Utente.GetNomeUtente(), utenteMalevolo));

            case ("GestoreSincronizzazioneController"):
                return(new EntrySincronizzazione(operazione, sorgente, destinazione));

            default:
                return(null);
            }
            //TODO bisogna implementare le altre entry
        }
        public override object Carica()
        {
            XmlDocument xdocument = new XmlDocument();

            try
            {
                xdocument.Load(Filename);
                XmlNodeList nodelist = xdocument.SelectNodes("/impostazioni/impostazione");
                ISet <ImpostazioneTrasferimento.ImpostazioneTrasferimento> settings = new HashSet <ImpostazioneTrasferimento.ImpostazioneTrasferimento>();
                foreach (XmlNode node in nodelist)
                {
                    if (node.Attributes.GetNamedItem("utente").Value.Equals(Utente.GetNomeUtente()))
                    {
                        string cartellaSorgente, cartellaDestinazione;
                        cartellaSorgente     = null;
                        cartellaDestinazione = null;
                        foreach (XmlNode settingNode in node.ChildNodes)
                        {
                            if (settingNode.Name.Equals("cartella-sorgente"))
                            {
                                cartellaSorgente = settingNode.InnerText;
                            }
                            if (settingNode.Name.Equals("cartella-destinazione"))
                            {
                                cartellaDestinazione = settingNode.InnerText;
                            }
                        }
                        settings.Add(new ImpostazioneTrasferimento.ImpostazioneTrasferimento(cartellaSorgente, cartellaDestinazione));
                    }
                }
                return(settings);
            }
            catch
            {
                //Andrà gestita a livello piu alto, semplicemente non facendo nullla.
                throw new PersistenceException("File per le impostazioni non trovato");
            }
        }
 /// <summary>
 /// Metodo necessario per il supporto della finestra di progettazione. Non modificare
 /// il contenuto del metodo con l'editor di codice.
 /// </summary>
 protected new void InitializeComponent()
 {
     this.splitContainer2        = new System.Windows.Forms.SplitContainer();
     this.tableLayoutPanel2      = new System.Windows.Forms.TableLayoutPanel();
     this.labelUtente            = new System.Windows.Forms.Label();
     this.labelDispositivo       = new System.Windows.Forms.Label();
     this.labelCartellaPrivata   = new System.Windows.Forms.Label();
     this.textBoxUtente          = new System.Windows.Forms.TextBox();
     this.textBoxDispositivo     = new System.Windows.Forms.TextBox();
     this.textBoxCartellaPrivata = new System.Windows.Forms.TextBox();
     this.buttonLogSincro        = new System.Windows.Forms.Button();
     this.buttonSincronizza      = new System.Windows.Forms.Button();
     ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
     this.splitContainer1.Panel2.SuspendLayout();
     this.splitContainer1.SuspendLayout();
     ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
     this.splitContainer2.Panel1.SuspendLayout();
     this.splitContainer2.Panel2.SuspendLayout();
     this.splitContainer2.SuspendLayout();
     this.tableLayoutPanel2.SuspendLayout();
     this.SuspendLayout();
     //
     // splitContainer1
     //
     //
     // splitContainer1.Panel2
     //
     this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
     //
     // splitContainer2
     //
     this.splitContainer2.Dock        = System.Windows.Forms.DockStyle.Fill;
     this.splitContainer2.Location    = new System.Drawing.Point(0, 0);
     this.splitContainer2.Name        = "splitContainer2";
     this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
     //
     // splitContainer2.Panel1
     //
     this.splitContainer2.Panel1.Controls.Add(this.tableLayoutPanel2);
     //
     // splitContainer2.Panel2
     //
     this.splitContainer2.Panel2.Controls.Add(this.buttonLogSincro);
     this.splitContainer2.Panel2.Controls.Add(this.buttonSincronizza);
     this.splitContainer2.Size             = new System.Drawing.Size(371, 372);
     this.splitContainer2.SplitterDistance = 123;
     this.splitContainer2.TabIndex         = 0;
     //
     // tableLayoutPanel2
     //
     this.tableLayoutPanel2.ColumnCount = 2;
     this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
     this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
     this.tableLayoutPanel2.Controls.Add(this.labelUtente, 0, 0);
     this.tableLayoutPanel2.Controls.Add(this.labelDispositivo, 0, 1);
     this.tableLayoutPanel2.Controls.Add(this.labelCartellaPrivata, 0, 2);
     this.tableLayoutPanel2.Controls.Add(this.textBoxUtente, 1, 0);
     this.tableLayoutPanel2.Controls.Add(this.textBoxDispositivo, 1, 1);
     this.tableLayoutPanel2.Controls.Add(this.textBoxCartellaPrivata, 1, 2);
     this.tableLayoutPanel2.Dock     = System.Windows.Forms.DockStyle.Fill;
     this.tableLayoutPanel2.Location = new System.Drawing.Point(0, 0);
     this.tableLayoutPanel2.Name     = "tableLayoutPanel2";
     this.tableLayoutPanel2.RowCount = 3;
     this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 36.84077F));
     this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 31.57961F));
     this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 31.57961F));
     this.tableLayoutPanel2.Size     = new System.Drawing.Size(371, 123);
     this.tableLayoutPanel2.TabIndex = 0;
     //
     // labelUtente
     //
     this.labelUtente.AutoSize  = true;
     this.labelUtente.Dock      = System.Windows.Forms.DockStyle.Fill;
     this.labelUtente.Font      = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.labelUtente.Location  = new System.Drawing.Point(3, 0);
     this.labelUtente.Name      = "labelUtente";
     this.labelUtente.Size      = new System.Drawing.Size(179, 45);
     this.labelUtente.TabIndex  = 0;
     this.labelUtente.Text      = "Utente";
     this.labelUtente.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
     //
     // labelDispositivo
     //
     this.labelDispositivo.AutoSize  = true;
     this.labelDispositivo.Dock      = System.Windows.Forms.DockStyle.Fill;
     this.labelDispositivo.Font      = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.labelDispositivo.Location  = new System.Drawing.Point(3, 45);
     this.labelDispositivo.Name      = "labelDispositivo";
     this.labelDispositivo.Size      = new System.Drawing.Size(179, 38);
     this.labelDispositivo.TabIndex  = 1;
     this.labelDispositivo.Text      = "Dispositivo";
     this.labelDispositivo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
     //
     // labelCartellaPrivata
     //
     this.labelCartellaPrivata.AutoSize  = true;
     this.labelCartellaPrivata.Dock      = System.Windows.Forms.DockStyle.Fill;
     this.labelCartellaPrivata.Font      = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.labelCartellaPrivata.Location  = new System.Drawing.Point(3, 83);
     this.labelCartellaPrivata.Name      = "labelCartellaPrivata";
     this.labelCartellaPrivata.Size      = new System.Drawing.Size(179, 40);
     this.labelCartellaPrivata.TabIndex  = 2;
     this.labelCartellaPrivata.Text      = "Cartella Privata";
     this.labelCartellaPrivata.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
     //
     // textBoxUtente
     //
     this.textBoxUtente.BackColor = System.Drawing.SystemColors.Window;
     this.textBoxUtente.Dock      = System.Windows.Forms.DockStyle.Fill;
     this.textBoxUtente.Location  = new System.Drawing.Point(188, 3);
     this.textBoxUtente.Name      = "textBoxUtente";
     this.textBoxUtente.ReadOnly  = true;
     this.textBoxUtente.Size      = new System.Drawing.Size(180, 21);
     this.textBoxUtente.TabIndex  = 3;
     this.textBoxUtente.Text      = Utente.GetNomeUtente();
     this.textBoxUtente.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
     //
     // textBoxDispositivo
     //
     this.textBoxDispositivo.BackColor = System.Drawing.SystemColors.Window;
     this.textBoxDispositivo.Dock      = System.Windows.Forms.DockStyle.Fill;
     this.textBoxDispositivo.Location  = new System.Drawing.Point(188, 48);
     this.textBoxDispositivo.Name      = "textBoxDispositivo";
     this.textBoxDispositivo.ReadOnly  = true;
     this.textBoxDispositivo.Size      = new System.Drawing.Size(180, 21);
     this.textBoxDispositivo.TabIndex  = 4;
     if (Utente.Dispositivo.Count < 1)
     {
         this.textBoxDispositivo.Text = "Nessun dispositivo USB inserito";
     }
     else
     {
         this.textBoxDispositivo.Text = "RV-Kingston";
     }
     this.textBoxDispositivo.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
     //
     // textBoxCartellaPrivata
     //
     this.textBoxCartellaPrivata.BackColor = System.Drawing.SystemColors.Window;
     this.textBoxCartellaPrivata.Dock      = System.Windows.Forms.DockStyle.Fill;
     this.textBoxCartellaPrivata.Location  = new System.Drawing.Point(188, 86);
     this.textBoxCartellaPrivata.Name      = "textBoxCartellaPrivata";
     this.textBoxCartellaPrivata.ReadOnly  = true;
     this.textBoxCartellaPrivata.Size      = new System.Drawing.Size(180, 21);
     this.textBoxCartellaPrivata.TabIndex  = 5;
     if (Utente.Dispositivo.Count < 1)
     {
         this.textBoxCartellaPrivata.Text = "No USB no party...";
     }
     else
     {
         this.textBoxCartellaPrivata.Text = "D:\\path\\to\\CartellaPrivata";
     }
     this.textBoxCartellaPrivata.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
     //
     // buttonLogSincro
     //
     this.buttonLogSincro.BackColor = System.Drawing.Color.CornflowerBlue;
     this.buttonLogSincro.Font      = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.buttonLogSincro.ForeColor = System.Drawing.SystemColors.Window;
     this.buttonLogSincro.Location  = new System.Drawing.Point(129, 131);
     this.buttonLogSincro.Name      = "buttonLogSincro";
     this.buttonLogSincro.Size      = new System.Drawing.Size(131, 47);
     this.buttonLogSincro.TabIndex  = 1;
     this.buttonLogSincro.Text      = "Visualizza Log di Sincronizzazione";
     this.buttonLogSincro.UseVisualStyleBackColor = false;
     //
     // buttonSincronizza
     //
     this.buttonSincronizza.BackColor = System.Drawing.Color.CornflowerBlue;
     this.buttonSincronizza.Font      = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.buttonSincronizza.ForeColor = System.Drawing.SystemColors.Window;
     this.buttonSincronizza.Location  = new System.Drawing.Point(129, 56);
     this.buttonSincronizza.Name      = "buttonSincronizza";
     this.buttonSincronizza.Size      = new System.Drawing.Size(131, 50);
     this.buttonSincronizza.TabIndex  = 0;
     this.buttonSincronizza.Text      = "Sincronizza";
     this.buttonSincronizza.UseVisualStyleBackColor = false;
     //
     // HomeSmartKey
     //
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.ClientSize          = new System.Drawing.Size(562, 372);
     this.Name = "HomeSmartKey";
     this.splitContainer1.Panel2.ResumeLayout(false);
     ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
     this.splitContainer1.ResumeLayout(false);
     this.splitContainer2.Panel1.ResumeLayout(false);
     this.splitContainer2.Panel2.ResumeLayout(false);
     ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
     this.splitContainer2.ResumeLayout(false);
     this.tableLayoutPanel2.ResumeLayout(false);
     this.tableLayoutPanel2.PerformLayout();
     this.ResumeLayout(false);
 }
示例#5
0
 public override string ToString()
 {
     return("" + base.DataOra + "\t" + "Impostazione\t" + Operazione + "\t" + Utente.GetNomeUtente() + "\t" + Sorgente + "\t" + Destinatario);
 }
        public void Visit(FileWrapper file)
        {
            FileInfo source      = new FileInfo(file.Path);
            String   fileDstPath = String.Join("\\", _pathDestinazione, source.Name);

            //Trovo l'autore del file
            string author = File.GetAccessControl(file.Path).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();

            //Prova va spostato sotto

            if (!author.Equals(string.Join("\\", Utente.GetNomeHost(), Utente.GetNomeUtente())))
            {
                if (!_blacklistController.IsBlackListed(author))
                {
                    bool riconosciuto = _viewHome.ChiediScelta(author);
                    if (riconosciuto)
                    {
                        //Se esiste Confronto gli hash se sono diversi sincronizzo
                        if (File.Exists(fileDstPath))
                        {
                            var    sourceStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
                            String srcSum       = GetChecksumBuffered(sourceStream);
                            var    dstStream    = new FileStream(fileDstPath, FileMode.Open, FileAccess.Read);
                            String dstSum       = GetChecksumBuffered(dstStream);

                            //Se gli hash non sono uguali
                            if (!srcSum.Equals(dstSum))
                            {
                                File.Copy(file.Path, fileDstPath);
                                ActionCompletedEvent args = new ActionCompletedEvent
                                {
                                    ToEntry = EntryFactory.CreateEntry(this, "file copiato", sorgente: file.Path, destinazione: fileDstPath)
                                };
                                ToLog?.Invoke(this, args);
                            }
                        }
                        //Copio diretto
                        else
                        {
                            File.Copy(file.Path, fileDstPath);
                            ActionCompletedEvent args = new ActionCompletedEvent
                            {
                                ToEntry = EntryFactory.CreateEntry(this, "file copiato", sorgente: file.Path, destinazione: fileDstPath)
                            };
                            ToLog?.Invoke(this, args);
                        }
                    }
                    else
                    {
                        _blacklistController.AggiungiUtente(author);
                        //Elimino il file
                        File.Delete(file.Path);
                        ActionCompletedEvent args = new ActionCompletedEvent
                        {
                            ToEntry = EntryFactory.CreateEntry(this, "file eliminato", sorgente: file.Path)
                        };
                        ToLog?.Invoke(this, args);
                    }
                }
            }
            //Il file è del proprietario non c'è bisogno di chiamare la blacklist
            else
            {
                if (File.Exists(fileDstPath))
                {
                    var    sourceStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
                    String srcSum       = GetChecksumBuffered(sourceStream);
                    var    dstStream    = new FileStream(fileDstPath, FileMode.Open, FileAccess.Read);
                    String dstSum       = GetChecksumBuffered(dstStream);

                    //Se gli hash non sono uguali
                    if (!srcSum.Equals(dstSum))
                    {
                        ActionCompletedEvent args = new ActionCompletedEvent
                        {
                            ToEntry = EntryFactory.CreateEntry(this, "file copiato", sorgente: file.Path, destinazione: fileDstPath)
                        };
                        ToLog?.Invoke(this, args);
                        File.Copy(file.Path, fileDstPath);
                    }
                }
                //Copio diretto
                else
                {
                    ActionCompletedEvent args = new ActionCompletedEvent
                    {
                        ToEntry = EntryFactory.CreateEntry(this, "file copiato", sorgente: file.Path, destinazione: fileDstPath)
                    };
                    ToLog?.Invoke(this, args);
                    File.Copy(file.Path, fileDstPath);
                }
            }
        }
        //Si suppone che object o sia una impostazione trasferimento

        public override void Salva(object o, PersistEvent param)
        {
            ImpostazioneTrasferimento.ImpostazioneTrasferimento toPut = (ImpostazioneTrasferimento.ImpostazioneTrasferimento)param.ToPersist;
            XmlDocument xdocument = new XmlDocument();

            try
            {
                xdocument.Load(Filename);
                //Dato che sono stato chiamato e il file esiste, si suppone che ci sia da fare un inserimento consono
                //Mi ri-permetto di andare liscio (a spade)
                XmlNode impostazioniMainNode = xdocument.DocumentElement;
                if (param.Action.Equals("aggiungi"))
                {
                    //Caso di aggiunta si veda sotto nel catch per commenti dettagliati
                    XmlElement xImpostazione = xdocument.CreateElement("impostazione");
                    xImpostazione.SetAttribute("utente", Utente.GetNomeUtente());
                    XmlElement cartellaSorgente = xdocument.CreateElement("cartella-sorgente");
                    cartellaSorgente.InnerText = toPut.CartellaSorgente.Path;
                    XmlElement cartellaDestinazione = xdocument.CreateElement("cartella-destinazione");
                    cartellaDestinazione.InnerText = toPut.CartellaDestinazione;
                    XmlElement verso = xdocument.CreateElement("verso");
                    verso.InnerText = toPut.Verso;
                    xImpostazione.AppendChild(cartellaSorgente);
                    xImpostazione.AppendChild(cartellaDestinazione);
                    xImpostazione.AppendChild(verso);
                    impostazioniMainNode.AppendChild(xImpostazione);
                }
                else if (param.Action.Equals("rimuovi"))
                {
                    //Caso di rimozione
                    //Itero, trovo l'impostazione che fa al caso mio e la rimuovo da impostazioni main node
                    //E tutti amici come prima
                    foreach (XmlNode impostazioneNode in impostazioniMainNode.ChildNodes)
                    {
                        if (impostazioneNode.Attributes.GetNamedItem("utente").Value.Equals(Utente.GetNomeUtente()))
                        {
                            //Controllo il contenuto
                            bool found = true;
                            foreach (XmlNode valueImpostazione in impostazioneNode.ChildNodes)
                            {
                                if (!valueImpostazione.Name.Equals("verso"))
                                {
                                    found = found && (
                                        ((valueImpostazione.Name.Equals("cartella-sorgente") &&
                                          valueImpostazione.InnerText.Equals(toPut.CartellaSorgente.Path))) ||
                                        (valueImpostazione.Name.Equals("cartella-destinazione") &&
                                         valueImpostazione.InnerText.Equals(toPut.CartellaDestinazione)));
                                }
                            }
                            if (found)
                            {
                                impostazioniMainNode.RemoveChild(impostazioneNode);
                            }
                        }
                    }
                }
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Indent = true
                };
                //Scrivo il file
                XmlWriter writer = XmlWriter.Create(Filename, settings);
                xdocument.Save(writer);
                //Ricordarsi la close sennò si hanno vari problemi
                writer.Close();
            }
            catch
            {
                //Il documento esiste, aggiungo andando liscio(a spade)
                xdocument = new XmlDocument();
                //Creo il tag contenitore
                XmlElement impostazioni = xdocument.CreateElement("impostazioni");
                //Creo il tag per la mia impostazione
                XmlElement impostazione = xdocument.CreateElement("impostazione");
                //Imposto l'attributo utente in modo consono
                impostazione.SetAttribute("utente", Utente.GetNomeUtente());

                //Creo gli i figli che compongono il tag impostazione e gli do il valore che devo inserire
                XmlElement cartellaSorgente = xdocument.CreateElement("cartella-sorgente");
                cartellaSorgente.InnerText = toPut.CartellaSorgente.Path;
                XmlElement cartellaDestinazione = xdocument.CreateElement("cartella-destinazione");
                cartellaDestinazione.InnerText = toPut.CartellaDestinazione;
                XmlElement verso = xdocument.CreateElement("verso");
                verso.InnerText = toPut.Verso;

                //Creo l'albero XML da aggiungere al DOM appena verra creato
                impostazione.AppendChild(cartellaSorgente);
                impostazione.AppendChild(cartellaDestinazione);
                impostazione.AppendChild(verso);
                impostazioni.AppendChild(impostazione);
                XmlDeclaration xmlDeclaration = xdocument.CreateXmlDeclaration("1.0", "UTF-8", null);
                //Creo la document root
                XmlElement root = xdocument.DocumentElement;
                xdocument.InsertBefore(xmlDeclaration, root);
                //Aggiungo impostazioni al dom
                xdocument.AppendChild(impostazioni);

                //Scrivo il file
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Indent = true
                };
                //Aggiungo il writer
                XmlWriter writer = XmlWriter.Create(Filename, settings);
                //TODO Aggiungere lo schema
                xdocument.Save(writer);
                writer.Close();
            }
        }
示例#8
0
        public override void Salva(object o, PersistEvent param)
        {
            //TODO bel controllo sul tipo con reflection
            string badUser = (string)param.ToPersist;
            //Questo metodo riceverà un set di stringhe quindi effettuo il cast
            //prima il documento andrà letto
            XmlDocument xdocument = new XmlDocument();

            try
            {
                //Carico il documento xml
                xdocument.Load(Filename);
                //Flusso normale il file esiste
                //Prendo tutti i nodi blacklist
                XmlNodeList blacklists = xdocument.SelectNodes("/blacklists/blacklist");
                /*DA LEGGERE PER CAPIRE IL FLUSSO*/

                //Da inserire cosa fare dipendentemente dall'azione
                //se l'aggiunta va a buon fine aggiungo senza controllare
                //Dato che il set mi ha dato true nel controller
                //Questa voce è univoca

                //Idem per la delete, se lo tolgo e l'operazione ha dato true
                //Cerco la entry e la tolgo a colpo sicuro
                //Per prima cosa trovo la mia blacklist
                foreach (XmlNode blacklist in blacklists)
                {
                    if (blacklist.Attributes.GetNamedItem("proprietario").Value.Equals(Utente.GetNomeUtente()))
                    {
                        //Se l'operazione era di aggiunta appendo l'elemento
                        if (param.Action.Equals("aggiungi"))
                        {
                            var xutente = xdocument.CreateElement("utente");
                            xutente.InnerText = badUser;
                            blacklist.AppendChild(xutente);
                            //Se era rimuovi -> rimuovo like pino la lavatrice
                        }
                        else if (param.Action.Equals("rimuovi"))
                        {
                            foreach (XmlNode user in blacklist.ChildNodes)
                            {
                                if (user.InnerText.Equals(badUser))
                                {
                                    blacklist.RemoveChild(user);
                                }
                            }
                        }
                    }
                }
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Indent = true
                };
                //Scrivo il file
                XmlWriter writer = XmlWriter.Create(Filename, settings);
                xdocument.Save(writer);
                //Ricordarsi la close sennò si hanno vari problemi
                writer.Close();
            }
            catch (Exception e)
            {
                //Tutto da fare solamente in caso di add,
                //Dato che il file è vuoto, non esistono regole
                //La remove non avrebbe senso
                xdocument = new XmlDocument();
                //flusso anormale il file non esiste
                //creo il tag blacklists
                XmlElement blacklists = xdocument.CreateElement("blacklists");
                //creo il tag blacklist
                XmlElement blacklist = xdocument.CreateElement("blacklist");
                //metto l'attributo per il proprietario
                blacklist.SetAttribute("proprietario", Utente.GetNomeUtente());
                //Aggiungo l'utente al dom

                var utentexml = xdocument.CreateElement("utente");
                utentexml.InnerText = badUser;
                blacklist.AppendChild(utentexml);

                //Aggangio la blacklist al nostro contenitore blacklists
                blacklists.AppendChild(blacklist);
                //Solfa per creare il documento xml
                XmlDeclaration xmlDeclaration = xdocument.CreateXmlDeclaration("1.0", "UTF-8", null);
                XmlElement     root           = xdocument.DocumentElement;
                xdocument.InsertBefore(xmlDeclaration, root);
                //Aggiungo blacklists al dom
                xdocument.AppendChild(blacklists);
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    Indent = true
                };
                //Aggiungo il writer
                XmlWriter writer = XmlWriter.Create(Filename, settings);
                //XmlSchemaSet schemas = new XmlSchemaSet();
                //Bisogna trovare una LLLLLLOCATION per tutte queste impostazioni
                //chemas.Add("", @"C:\Users\massi\Desktop\blacklist.xsd");
                //xdocument.Schemas = schemas;
                try
                {
                    //Scrivo il file
                    //  xdocument.Validate((oggetto, handler) => { });
                    xdocument.Save(writer);
                    writer.Close();
                }catch (Exception ex)
                {
                    throw new PersistenceException("Non è stato possibile scrivere il file .xml");
                }
            }
        }
示例#9
0
        public void TestNomeHost()
        {
            string me = Utente.GetNomeUtente();

            Assert.AreEqual(me, WindowsIdentity.GetCurrent().Name.ToString());
        }
示例#10
0
        public ModelLog.Log LeggiLog()
        {
            ModelLog.Log log = new ModelLog.Log();
            try
            {
                using (StreamReader readtext = new StreamReader(_filename))
                {
                    string readMeText = null;
                    while ((readMeText = readtext.ReadLine()) != null)
                    {
                        //  29 / 06 / 2018 09:14:20 Blacklist aggiunto DESKTOP-TF7TLNM\massi    riccardo
                        //  29 / 06 / 2018 09:14:20 Impostazione aggiunta    DESKTOP - TF7TLNM\massi mydir   yourDir
                        //Esempio righe da parsare

                        //Parsing della parte fissa.
                        string[] fields     = readMeText.Split('\t');
                        string[] dateHour   = fields[0].Split(' ');
                        string   date       = dateHour[0];
                        string   hour       = dateHour[1];
                        string   entryType  = fields[1];
                        string   operazione = fields[2];
                        string   utente     = fields[3];

                        //Assegno i parametri che variano
                        switch (entryType)
                        {
                        case ("Blacklist"):
                            string badUser = fields[4];
                            log.AddEntry(EntryFactory.CreateEntry(entryType, operazione, date, hour,
                                                                  utenteMalevolo: badUser, utenteProprietario: Utente.GetNomeUtente()));
                            break;

                        case ("Impostazione"):
                            string sorgente     = fields[4];
                            string destinazione = fields[5];
                            log.AddEntry(EntryFactory.CreateEntry(entryType, operazione, date, hour,
                                                                  sorgente: sorgente, destinazione: destinazione));
                            break;

                        case ("Sincronizzazione"):
                            string source = fields[4];
                            if (operazione.Equals("file eliminato"))
                            {
                                log.AddEntry(EntryFactory.CreateEntry(entryType, operazione, date, hour,
                                                                      sorgente: source));
                            }
                            else
                            {
                                string dst = fields[5];
                                log.AddEntry(EntryFactory.CreateEntry(entryType, operazione, date, hour,
                                                                      sorgente: source, destinazione: dst));
                            }
                            break;
                        }
                    }
                }
            }catch (Exception e)
            {
            }
            return(log);
        }