private bool importContacts(string iniPath)
 {
     try
     {
         var result = false;
         var size = 1024;
         var buffer = new string(' ', size);
         while (PNInterop.GetPrivateProfileString("contacts", null, null, buffer, size, iniPath) == size - 2)
         {
             // loop until sufficient buffer size
             size *= 2;
             buffer = new string(' ', size);
         }
         var names = buffer.Split('\0');
         var keys = names.Where(n => n.Trim().Length > 0);
         var sqlList = new List<string>();
         var contacts = new List<PNContact>();
         int tempID = 0;
         if (PNStatic.Contacts.Count > 0)
         {
             tempID = PNStatic.Contacts.Max(c => c.ID) + 1;
         }
         foreach (var key in keys)
         {
             var cont = new PCONTPROP();
             cont = PNInterop.ReadINIStructure(iniPath, "contacts", key, cont);
             if (cont.name != null && cont.name.Trim().Length > 0)
             {
                 var pnc = new PNContact
                 {
                     ComputerName = cont.host,
                     GroupID = cont.group,
                     Name = cont.name,
                     UseComputerName = cont.usename
                 };
                 if (cont.address != 0)
                 {
                     pnc.IpAddress = buildAddressString(cont.address);
                 }
                 var temp = PNStatic.Contacts.FirstOrDefault(c => c.Name == pnc.Name);
                 pnc.ID = temp != null ? temp.ID : tempID++;
                 contacts.Add(pnc);
             }
         }
         // get contact froups
         size = 1024;
         buffer = new string(' ', size);
         while (PNInterop.GetPrivateProfileString("cont_groups", null, null, buffer, size, iniPath) == size - 2)
         {
             // loop until sufficient buffer size
             size *= 2;
             buffer = new string(' ', size);
         }
         names = buffer.Split('\0');
         keys = names.Where(n => n.Trim().Length > 0);
         var groups = new List<PNContactGroup>();
         tempID = 0;
         if (PNStatic.ContactGroups.Count > 0)
         {
             tempID = PNStatic.ContactGroups.Max(g => g.ID) + 1;
         }
         foreach (var key in keys)
         {
             var grp = new PCONTGROUP();
             grp = PNInterop.ReadINIStructure(iniPath, "cont_groups", key, grp);
             if (grp.name == null || grp.name.Trim().Length <= 0) continue;
             var pg = new PNContactGroup { Name = grp.name };
             var temp = PNStatic.ContactGroups.FirstOrDefault(g => g.Name == grp.name);
             pg.ID = temp != null ? temp.ID : tempID++;
             groups.Add(pg);
         }
         // build sql
         foreach (var pg in groups)
         {
             var sb = new StringBuilder();
             sb.Append("INSERT OR REPLACE INTO CONTACT_GROUPS (ID, GROUP_NAME) VALUES(");
             sb.Append(pg.ID);
             sb.Append(", '");
             sb.Append(pg.Name);
             sb.Append("')");
             sqlList.Add(sb.ToString());
         }
         foreach (var pc in contacts)
         {
             var sb = new StringBuilder();
             sb.Append(
                 "INSERT OR REPLACE INTO CONTACTS (ID, GROUP_ID, CONTACT_NAME, COMP_NAME, IP_ADDRESS, USE_COMP_NAME) VALUES(");
             sb.Append(pc.ID);
             sb.Append(", ");
             sb.Append(pc.GroupID);
             sb.Append(", '");
             sb.Append(pc.Name);
             sb.Append("', '");
             sb.Append(pc.ComputerName);
             sb.Append("', '");
             sb.Append(pc.IpAddress);
             sb.Append("', ");
             sb.Append(Convert.ToInt32(pc.UseComputerName));
             sb.Append(")");
             sqlList.Add(sb.ToString());
         }
         if (sqlList.Count > 0 && PNData.ExecuteTransactionForList(sqlList, PNData.ConnectionString))
         {
             result = true;
             foreach (PNContactGroup pg in groups)
             {
                 PNStatic.ContactGroups.RemoveAll(g => g.Name == pg.Name);
                 PNStatic.ContactGroups.Add(pg);
             }
             foreach (PNContact pc in contacts)
             {
                 PNStatic.Contacts.RemoveAll(c => c.Name == pc.Name);
                 PNStatic.Contacts.Add(pc);
             }
         }
         return result;
     }
     catch (Exception ex)
     {
         PNStatic.LogException(ex);
         return false;
     }
 }
Esempio n. 2
0
 private bool importContacts(string iniPath)
 {
     try
     {
         var result = false;
         var size   = 1024;
         var buffer = new string(' ', size);
         while (PNInterop.GetPrivateProfileString("contacts", null, null, buffer, size, iniPath) == size - 2)
         {
             // loop until sufficient buffer size
             size  *= 2;
             buffer = new string(' ', size);
         }
         var names    = buffer.Split('\0');
         var keys     = names.Where(n => n.Trim().Length > 0);
         var sqlList  = new List <string>();
         var contacts = new List <PNContact>();
         int tempID   = 0;
         if (PNStatic.Contacts.Count > 0)
         {
             tempID = PNStatic.Contacts.Max(c => c.ID) + 1;
         }
         foreach (var key in keys)
         {
             var cont = new PCONTPROP();
             cont = PNInterop.ReadINIStructure(iniPath, "contacts", key, cont);
             if (cont.name != null && cont.name.Trim().Length > 0)
             {
                 var pnc = new PNContact
                 {
                     ComputerName    = cont.host,
                     GroupID         = cont.group,
                     Name            = cont.name,
                     UseComputerName = cont.usename
                 };
                 if (cont.address != 0)
                 {
                     pnc.IpAddress = buildAddressString(cont.address);
                 }
                 var temp = PNStatic.Contacts.FirstOrDefault(c => c.Name == pnc.Name);
                 pnc.ID = temp != null ? temp.ID : tempID++;
                 contacts.Add(pnc);
             }
         }
         // get contact froups
         size   = 1024;
         buffer = new string(' ', size);
         while (PNInterop.GetPrivateProfileString("cont_groups", null, null, buffer, size, iniPath) == size - 2)
         {
             // loop until sufficient buffer size
             size  *= 2;
             buffer = new string(' ', size);
         }
         names = buffer.Split('\0');
         keys  = names.Where(n => n.Trim().Length > 0);
         var groups = new List <PNContactGroup>();
         tempID = 0;
         if (PNStatic.ContactGroups.Count > 0)
         {
             tempID = PNStatic.ContactGroups.Max(g => g.ID) + 1;
         }
         foreach (var key in keys)
         {
             var grp = new PCONTGROUP();
             grp = PNInterop.ReadINIStructure(iniPath, "cont_groups", key, grp);
             if (grp.name == null || grp.name.Trim().Length <= 0)
             {
                 continue;
             }
             var pg = new PNContactGroup {
                 Name = grp.name
             };
             var temp = PNStatic.ContactGroups.FirstOrDefault(g => g.Name == grp.name);
             pg.ID = temp != null ? temp.ID : tempID++;
             groups.Add(pg);
         }
         // build sql
         foreach (var pg in groups)
         {
             var sb = new StringBuilder();
             sb.Append("INSERT OR REPLACE INTO CONTACT_GROUPS (ID, GROUP_NAME) VALUES(");
             sb.Append(pg.ID);
             sb.Append(", '");
             sb.Append(pg.Name);
             sb.Append("')");
             sqlList.Add(sb.ToString());
         }
         foreach (var pc in contacts)
         {
             var sb = new StringBuilder();
             sb.Append(
                 "INSERT OR REPLACE INTO CONTACTS (ID, GROUP_ID, CONTACT_NAME, COMP_NAME, IP_ADDRESS, USE_COMP_NAME) VALUES(");
             sb.Append(pc.ID);
             sb.Append(", ");
             sb.Append(pc.GroupID);
             sb.Append(", '");
             sb.Append(pc.Name);
             sb.Append("', '");
             sb.Append(pc.ComputerName);
             sb.Append("', '");
             sb.Append(pc.IpAddress);
             sb.Append("', ");
             sb.Append(Convert.ToInt32(pc.UseComputerName));
             sb.Append(")");
             sqlList.Add(sb.ToString());
         }
         if (sqlList.Count > 0 && PNData.ExecuteTransactionForList(sqlList, PNData.ConnectionString))
         {
             result = true;
             foreach (PNContactGroup pg in groups)
             {
                 PNStatic.ContactGroups.RemoveAll(g => g.Name == pg.Name);
                 PNStatic.ContactGroups.Add(pg);
             }
             foreach (PNContact pc in contacts)
             {
                 PNStatic.Contacts.RemoveAll(c => c.Name == pc.Name);
                 PNStatic.Contacts.Add(pc);
             }
         }
         return(result);
     }
     catch (Exception ex)
     {
         PNStatic.LogException(ex);
         return(false);
     }
 }