public static StandardRequestModel creatDelPackageRequest(ClientInfoModel client, PackageInfoModel delPackage)
 {
     StandardRequestModel request = null;
     if (client == null)
     {
         return request;
     }
     request = new StandardRequestModel();
     request.Client = client;
     request.request = RequestTyp.delDatabaseSoftwarePackage;
     request.requestData = delPackage;
     return request;
 }
    private void btnDeleteSoftware_Click(object sender, EventArgs e)
    {
        TreeNode NodeToDelete = new TreeNode();
            for (int i = 0; i < TreeView3.Nodes[0].Nodes.Count; i++)
                {
                    if (TreeView3.Nodes[0].Nodes[i].Checked)
                    {
                        NodeToDelete = TreeView3.Nodes[0].Nodes[i];
                    }
            }
            if (NodeToDelete.TreeView == null)
        {
         MessageBox.Show("Bitte wählen Sie ein zu löschendes Programm/Paket aus.");
                return;
        }

            if (MessageBox.Show("Möchten Sie das ausgewählte Programm/Paket aus der gewählten Gruppe löschen?", "Achtung!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                for (int i = 0; i < TreeView3.Nodes[0].Nodes.Count; i++)
                {
                    if (TreeView3.Nodes[0].Nodes[i].Checked)
                    {
                        PackageInfoModel CurrentPackage = new PackageInfoModel();
                        CurrentPackage.ID = Convert.ToInt32(TreeView3.Nodes[0].Nodes[i].Tag);
                        CurrentPackage.Name = TreeView3.Nodes[0].Nodes[i].Text;
                        request.delPackageInfo(client, CurrentPackage);
                        TreeView3.Nodes[0].Nodes[i].Parent.Nodes.Remove(TreeView3.Nodes[0].Nodes[i]);

                        System.IO.File.Delete(Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\" + CurrentPackage.Name + ".zip");
                    }
                }
            }
    }
    private void btnAddSoftware_Click(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode == null | object.ReferenceEquals(TreeView1.SelectedNode, TreeView1.Nodes[0]))
            {
             MessageBox.Show("Programme können nur Gruppen hinzugefügt werden. Bitte wählen Sie eine Gruppe aus.");
                return;
            }

            OpenFileDialog FileDialog = new OpenFileDialog();
            FileDialog.Multiselect = true;

            FileDialog.Filter = "msi files (*.msi)|*.msi|exe files (*.exe*)|*.exe*|All files (*.*)|*.*";
            FileDialog.FilterIndex = 1;
            FileDialog.RestoreDirectory = true;

            //geben sie einen namen für paket an falls kein paket markiert ist!
            if (FileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (FileDialog.SafeFileNames.Length == 0)
                {
                    return;
                }
                TreeNode NewListMainItem = new TreeNode("Neuer Eintrag");
                TreeView3.Nodes[0].Nodes.Add(NewListMainItem);

            frmNewName NewName = new frmNewName();
            NewName.ShowDialog();

            if (!string.IsNullOrEmpty(NewGroupName))
            {
                NewListMainItem.Text = NewGroupName;
                NewGroupName = "";
                TreeView3.Nodes[0].Expand();
            }
            else
            {
                MessageBox.Show("Kein Softwarepaketname angegeben. Vorgang wird abgebrochen.");
                return;
            }

                foreach (string File in FileDialog.SafeFileNames)
                {
                    TreeNode NewListItem = new TreeNode(File);
                    NewListMainItem.Nodes.Add(NewListItem);
                }
                string[] FileArray = new string[FileDialog.FileNames.Length];
                int counter = 0;

                string archit = "32bit";
                string Path = Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\";
                Guid PacketGuid = Guid.NewGuid();
                foreach (string File in FileDialog.FileNames)
                {
                    //Assembly ExeAssembly = Assembly.LoadFile(File);
                    //System.Reflection.AssemblyName AssemblyAr = new System.Reflection.AssemblyName(ExeAssembly.FullName);
                    //if (archit != "" && archit != AssemblyAr.ProcessorArchitecture.ToString())
                    //{
                    //    MessageBox.Show("Die Datei: " + File + " hat das falsche Format(" + AssemblyAr.ProcessorArchitecture.ToString() + "). Bitte erstellen Sie für 32/64 bit Programme einzelne Softwarepakete.");
                    //    continue;
                    //}
                    //archit = AssemblyAr.ProcessorArchitecture.ToString();

                      clsZipFile.CreatePackage(Path + PacketGuid + ".zip", File);
                    FileArray[counter] = File;
                    counter++;
                }

                if (!Directory.Exists(Path))
                {
                    Directory.CreateDirectory(Path);
                }

                NewListMainItem.Expand();

                //Assembly assembly = Assembly.LoadFile(Path + PacketGuid + ".zip");
                //System.Reflection.AssemblyName AssemblyName = new System.Reflection.AssemblyName(assembly.FullName);
                FileInfo fileinfo_ = new FileInfo(Path + PacketGuid + ".zip");

                PackageInfoModel CurrentPackage = new PackageInfoModel();
                CurrentPackage.showName = NewListMainItem.Text;
                CurrentPackage.Name =  PacketGuid.ToString();
                CurrentPackage.arc = archit;
                CurrentPackage.size  =Convert.ToInt32( fileinfo_.Length ) ;
                CurrentPackage.group = Convert.ToInt32( TreeView1.SelectedNode.Tag);

                request.addPackageInfo(client, CurrentPackage);

                GroupInfoModel GroupModel = new GroupInfoModel();
                GroupModel.ID = Convert.ToInt32( TreeView1.SelectedNode.Tag);
                GroupModel.Name = TreeView1.SelectedNode.Text;
                request.sendFile(client, GroupModel, CurrentPackage, Path + PacketGuid + ".zip");
            }

        //Füge Kopie der Datei hinzu und unten entfernen und client gui trayicon

                    //In Datenbank für diese Gruppe anlegen falls nicht exitstiert
                    //Pfad auf ServerClient dazu speichern
                    //msi gui
    }
    private void btnAddExistingPacket_Click(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode == null | object.ReferenceEquals(TreeView1.SelectedNode, TreeView1.Nodes[0]))
            {
                MessageBox.Show("Programme können nur Gruppen hinzugefügt werden. Bitte wählen Sie eine Gruppe aus.");
                return;
            }

             OpenFileDialog FileDialog = new OpenFileDialog();
            FileDialog.Multiselect = true;

            FileDialog.Filter = "zip (*.zip)|*.zip";
            FileDialog.FilterIndex = 1;
            FileDialog.InitialDirectory = Server_Client.Properties.Settings.Default.SavePath + "Groups\\";
            FileDialog.RestoreDirectory = true;

            //geben sie einen namen für paket an falls kein paket markiert ist!
            if (FileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (FileDialog.SafeFileNames.Length == 0)
                {
                    return;
                }

                for (int i = 0; i < FileDialog.FileNames.Length; i++)
            {
                    Guid PacketGuid = Guid.NewGuid();
                    string Path = Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\" + PacketGuid + ".zip";
                    System.IO.File.Copy(FileDialog.FileNames[i], Path);

                    frmNewName NewName = new frmNewName();
                    NewName.ShowDialog();
                    //Hier Datenbankabfrage der Gruppen auf Gruppe mit ID (Diesen Namen nehmen)

                    TreeNode NewPacketNode = new TreeNode();
                    if (!string.IsNullOrEmpty(NewGroupName))
                    {
                        NewPacketNode.Text = NewGroupName;
                        NewGroupName = "";
                        TreeView3.Nodes[0].Nodes.Add(NewPacketNode);
                        TreeView3.Nodes[0].Expand();
                    }
                    else
                    {
                        MessageBox.Show("Kein Softwarepaketname angegeben. Vorgang wird abgebrochen.");
                        return;
                    }

                   clsZipFile.Unzip(FileDialog.FileNames[i].ToString(), Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\Temp\\");

                   string[] FileArr=  System.IO.Directory.GetFiles(Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\Temp\\");

                   for (int d = 0; d < FileArr.Length; d++)
                   {
                       System.IO.DirectoryInfo oDir = new System.IO.DirectoryInfo(FileArr[0]);
                       NewPacketNode.Nodes.Add(oDir.Name);
                       System.IO.File.Delete(oDir.FullName);
                   }

                   System.IO.File.Copy(FileDialog.FileNames[i].ToString(), Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\" + NewPacketNode.Text);
                   string CurrentName = FileDialog.SafeFileName[i].ToString().Replace(".zip", "");

                 List<PackageInfoModel> AllPackages = request.getDatabasePackages(client);
                 PackageInfoModel CurrentPackage = new PackageInfoModel();
                 CurrentPackage.Name = PacketGuid.ToString();
                 CurrentPackage.group = Convert.ToInt32(TreeView1.SelectedNode.Tag);

                    foreach (PackageInfoModel Package in AllPackages)
                 {
                     if (Package.Name == CurrentName)
                     {
                         CurrentPackage.showName = Package.showName;
                         CurrentPackage.arc = Package.arc;
                         CurrentPackage.size = Package.size;
                         break;
                     }
                 }
                    request.addPackageInfo(client, CurrentPackage);

                    GroupInfoModel GroupModel = new GroupInfoModel();
                    GroupModel.ID = Convert.ToInt32(TreeView1.SelectedNode.Tag);
                    GroupModel.Name = TreeView1.SelectedNode.Text;
                    request.sendFile(client, GroupModel, CurrentPackage, Path + PacketGuid + ".zip");

                    NewPacketNode.Expand();
            }

                TreeView3.Nodes[0].Expand();
            }
    }
        public bool sendFile(ClientInfoModel client, GroupInfoModel group, PackageInfoModel package, string filePath)
        {
            bool value = false;
            StandardResultModel resultInfo = null;

            if (!client.admin)
            {
                return value;
            }

            mRequest = RequestController.creatSendSoftwarePackageRequest(client, group, package);

            if (mRequest == null)
            {
                return false;
            }

            resultInfo = mConnection.startConnection(mRequest);

            if (resultInfo == null)
            {
                return false;
            }

            if (resultInfo.successful && resultInfo.type == ResultType.readPackage)
            {
                value = mConnection.sendFile(filePath);
            }

            mConnection.closeConnection();

            return false;
        }
        public bool delPackageInfo(ClientInfoModel client, PackageInfoModel delPackage)
        {
            bool value = false;
            StandardResultModel resultInfo = null;

            if (!client.admin)
            {
                return value;
            }

            mRequest = RequestController.creatDelPackageRequest(client, delPackage);

            if (mRequest == null)
            {
                return value;
            }

            resultInfo = mConnection.startConnection(mRequest);
            mConnection.closeConnection();

            if (resultInfo == null)
            {
                return value;
            }

            if (resultInfo.successful && resultInfo.type == ResultType.delDatabaseSoftwarePackage)
            {
                value = true;
            }

            mRequest = null;
            return value;
        }
        public int addPackageInfo(ClientInfoModel client, PackageInfoModel addPackage)
        {
            int value = -1;
            StandardResultModel resultInfo = null;

            if (!client.admin)
            {
                return value;
            }

            mRequest = RequestController.creatAddPackageRequest(client, addPackage);

            if (mRequest == null)
            {
                return value;
            }

            resultInfo = mConnection.startConnection(mRequest);
            mConnection.closeConnection();

            if (resultInfo == null)
            {
                return value;
            }

            if (resultInfo.result == null)
            {
                return value;
            }

            PackageInfoModel resultPackage = JsonConvert.DeserializeObject<PackageInfoModel>(resultInfo.result.ToString());

            if (resultInfo.successful && resultInfo.type == ResultType.addClient)
            {
                value = resultPackage.ID;
            }

            mRequest = null;
            return value;
        }
        public static StandardRequestModel creatSendSoftwarePackageRequest(ClientInfoModel client, GroupInfoModel group, PackageInfoModel package)
        {
            StandardRequestModel request = null;
            if (client == null)
            {
                return request;
            }

            string info = group.Name + "," + package.Name;
            request = new StandardRequestModel();
            request.Client = client;
            request.request = RequestTyp.sendSoftwarePackage;
            request.requestData = info;
            return request;
        }