public int addGroupInfo(ClientInfoModel client, GroupInfoModel addGroup)
        {
            int value = -1;
            StandardResultModel resultInfo = null;

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

            mRequest = RequestController.creatAddGroupRequest(client, addGroup);

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

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

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

            GroupInfoModel resultGroup = JsonConvert.DeserializeObject<GroupInfoModel>(resultInfo.result.ToString());

            if (resultInfo.successful && resultInfo.type == ResultType.addGroup)
            {
                value = resultGroup.ID;
            }

            mRequest = null;
            return value;
        }
 public static StandardRequestModel creatDelGroupRequest(ClientInfoModel client, GroupInfoModel delGroup)
 {
     StandardRequestModel request = null;
     if (client == null)
     {
         return request;
     }
     request = new StandardRequestModel();
     request.Client = client;
     request.request = RequestTyp.delDatabaseGroup;
     request.requestData = delGroup;
     return request;
 }
        public frmDefaulClients()
        {
            InitializeComponent();

            GroupInfoModel Group = new GroupInfoModel();
            Group.ID = 0;

            List<ClientInfoModel> ClientInfoModelArrayLoading;
            ClientInfoModelArrayLoading = frmSoftwareverteilung.request.getGroupClients(frmSoftwareverteilung.client, Group);

            foreach (ClientInfoModel Client in ClientInfoModelArrayLoading)
            {
                TreeNode CurrentItem = new TreeNode();
                CurrentItem.Tag = Client.ID;
                CurrentItem.Text = Client.pcName;

                treeView1.Nodes.Add(CurrentItem);

            }
        }
    private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node == TreeView1.Nodes[0])
            {
                return;
            }

            TreeView2.Nodes[0].Nodes.Clear();
            TreeView3.Nodes[0].Nodes.Clear();

            TreeView3.Nodes[0].Text = "Pakete";
            TreeView3.Nodes[0].ForeColor = System.Drawing.Color.Black;

            GroupInfoModel CurrentGroup = new GroupInfoModel();
            CurrentGroup.ID = Convert.ToInt32(e.Node.Tag);

            List<ClientInfoModel> clientList = null;
            clientList = request.getGroupClients(client, CurrentGroup);

            foreach (ClientInfoModel Client in clientList)
            {
                TreeNode CurrentNode = new TreeNode(Client.pcName);
                CurrentNode.Tag = Client.ID;
                TreeView2.Nodes[0].Nodes.Add(CurrentNode);
            }

            List<PackageInfoModel> softwareList = null;
            softwareList = request.getGroupPackages(client, CurrentGroup);

            foreach (PackageInfoModel Package in softwareList)
            {
                TreeNode CurrentNode = new TreeNode(Package.showName);
                CurrentNode.Tag = Package.ID;
                TreeView3.Nodes[0].Nodes.Add(CurrentNode);

                clsZipFile.Unzip(Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text + "\\" + Package.Name + ".zip", 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]);
                    CurrentNode.Nodes.Add(oDir.Name);
                    System.IO.File.Delete(oDir.FullName);
                }
            }
            TreeView2.Nodes[0].Expand();
            TreeView3.Nodes[0].Expand();
    }
    private void btnDeleteGroup_Click(object sender, EventArgs e)
    {
        if ((TreeView1.SelectedNode != null))
            {
                if (TreeView1.SelectedNode.Text == "Standard")
                {
                    return;
                }
                if (MessageBox.Show("Möchten Sie die ausgewählten Gruppen mit allen enthaltenen Clients löschen?" , "Achtung!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                  //  System.IO.Directory.Delete(Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + TreeView1.SelectedNode.Text, true);

                    GroupInfoModel CurrentGroup = new GroupInfoModel();
                    CurrentGroup.ID = Convert.ToInt32(TreeView1.SelectedNode.Tag);
                    CurrentGroup.Name = TreeView1.SelectedNode.Text;
                    request.delGroupInfo(client, CurrentGroup);

                    TreeView1.SelectedNode.Parent.Nodes.Remove(TreeView1.SelectedNode);

                }
            }
            TreeView2.Nodes[0].Nodes.Clear();
            TreeView3.Nodes[0].Nodes.Clear();
    }
    private void btnAddUser_Click(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode == null || TreeView1.SelectedNode == TreeView1.Nodes[0] || TreeView1.SelectedNode.Text == "Standard")
            {
                MessageBox.Show("Benutzer können nur Gruppen hinzugefügt werden. Bitte wählen Sie eine Gruppe aus.");
                return;
            }

            TreeView2.Nodes[0].Nodes.Clear();
            frmDefaulClients DefaultClients = new frmDefaulClients();
            DefaultClients.ShowDialog();

            if (ClientInfoModelArray != null)
            {
                List<ClientInfoModel> InfoModelArray = request.getDatabaseClients(client);
            foreach (ClientInfoModel CurrentClient in ClientInfoModelArray)
            {
                CurrentClient.group =Convert.ToInt32( TreeView1.SelectedNode.Tag);

                foreach (ClientInfoModel CurrClient in InfoModelArray)
                {
                    if (CurrClient.ID == CurrentClient.ID)
                    {
                        CurrentClient.arc = CurrClient.arc;
                        CurrentClient.admin = CurrClient.admin;
                        CurrentClient.macAddress = CurrClient.macAddress;
                        break;
                    }
                }
                request.addClientInfo(client, CurrentClient);
            }
            }
            GroupInfoModel UpdatedGroup = new GroupInfoModel();
            UpdatedGroup.ID =Convert.ToInt32( TreeView1.SelectedNode.Tag);

            List<ClientInfoModel> clientList = null;
            clientList = request.getGroupClients(client, UpdatedGroup);

            foreach (ClientInfoModel Client in clientList)
            {
                TreeNode CurrentNode = new TreeNode(Client.pcName);
                CurrentNode.Tag = Client.ID;
                TreeView2.Nodes[0].Nodes.Add(CurrentNode);
            }

            ClientInfoModelArray = null;

            TreeView2.Nodes[0].Expand();
    }
    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();
            }
    }
    private bool AddGroup()
    {
        TreeNode NewListItem = new TreeNode("Neuer Eintrag");
            TreeView1.Nodes[0].Nodes.Add(NewListItem);

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

            if (!string.IsNullOrEmpty(NewGroupName))
            {
                if (CheckIfEntryExists(ref NewListItem))
                {
                    MessageBox.Show("Gruppenname bereits vorhanden. Bitte geben Sie einen neuen Namen an.");
                    NewGroupName = "";
                    NewListItem.Parent.Nodes.Remove(NewListItem);
                    return false;
                }
                else
                {
                    NewListItem.Text = NewGroupName;
                    if (System.IO.Directory.Exists(Server_Client.Properties.Settings.Default.SavePath + "Groups") == false)
                    {
                        System.IO.Directory.CreateDirectory(Server_Client.Properties.Settings.Default.SavePath + "Groups");
                    }
                    System.IO.Directory.CreateDirectory(Server_Client.Properties.Settings.Default.SavePath + "Groups\\" + NewGroupName);

                    //Safe to DB
                    GroupInfoModel group = new GroupInfoModel();
                    group.Name = NewGroupName;
                    group.ID = -1;
                    NewListItem.Tag = request.addGroupInfo(client, group);
                    if (Convert.ToInt32(NewListItem.Tag) == -1)
                    {
                    MessageBox.Show("Es ist ein Fehler beim Hinzufügen aufgetreten.", "Achtung",MessageBoxButtons.OK ,MessageBoxIcon.Error);
                    NewListItem.Parent.Nodes.Remove(NewListItem);
                    }
                    NewGroupName = "";
                }
            }
            else
            {
                NewListItem.Parent.Nodes.Remove(NewListItem);
            }

            return true;
    }
        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 List<PackageInfoModel> getGroupPackages(ClientInfoModel client, GroupInfoModel group)
        {
            List<PackageInfoModel> packageInfo = null;
            StandardResultModel resultInfo = null;

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

            mRequest = RequestController.creatGetGrupePackages(client,group);

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

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

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

            if (resultInfo.successful && resultInfo.type == ResultType.GrupePackages)
            {
                packageInfo = new List<PackageInfoModel>();
                JArray array = (JArray)resultInfo.result;
                if (array != null)
                {
                    packageInfo = JsonConvert.DeserializeObject<List<PackageInfoModel>>(array.ToString());
                }
            }

            mRequest = null;
            return packageInfo;
        }
        public bool delGroupInfo(ClientInfoModel client, GroupInfoModel delGroup)
        {
            bool value = false;
            StandardResultModel resultInfo = null;

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

            mRequest = RequestController.creatDelGroupRequest(client, delGroup);

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

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

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

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

            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;
        }
 public static StandardRequestModel creatGetGrupePackages(ClientInfoModel client, GroupInfoModel group)
 {
     StandardRequestModel request = null;
     if (client == null)
     {
         return request;
     }
     request = new StandardRequestModel();
     request.Client = client;
     request.request = RequestTyp.getGrupePackages;
     request.requestData = group;
     return request;
 }