private void copyTablestoFile(bool fileSelected) { int sourceId; List <TableData> sourceTdList = new List <TableData>(); if (fileSelected) { int row = -1; if (dataGridFiles.SelectedCells.Count > 0) { row = dataGridFiles.SelectedCells[0].RowIndex; } else if (dataGridFiles.SelectedRows.Count > 0) { row = dataGridFiles.SelectedRows[0].Index; } else { return; } sourceId = Convert.ToInt32(dataGridFiles.Rows[row].Cells["id"].Value); for (int t = 0; t < tunerFiles[sourceId].tableDatas.Count; t++) { sourceTdList.Add(tunerFiles[sourceId].tableDatas[t]); } } else { //Tables selected from datagridview1 sourceId = -1; for (int c = 0; c < dataGridView1.SelectedCells.Count; c++) { int r = dataGridView1.SelectedCells[c].RowIndex; int id = Convert.ToInt32(dataGridView1.Rows[r].Cells["id"].Value); sourceTdList.Add(displayDatas[id].td); } } frmSelectMassTarget frmS = new frmSelectMassTarget(); frmS.chkSelectAll.Checked = false; frmS.dataGridView1.DataSource = tunerFiles.Where(t => t.id != sourceId).ToList(); frmS.dataGridView1.Columns[0].Width = 50; frmS.dataGridView1.Columns[1].Width = 50; frmS.dataGridView1.Columns[2].Width = 1000; frmS.Text = "Select target file(s)"; frmS.btnOK.Text = "Next >"; if (frmS.ShowDialog() != DialogResult.OK) { return; } List <int> selectedFiles = new List <int>(); for (int r = 0; r < frmS.dataGridView1.Rows.Count; r++) { if (Convert.ToBoolean(frmS.dataGridView1.Rows[r].Cells["Select"].Value) == true) { int tId = Convert.ToInt32(frmS.dataGridView1.Rows[r].Cells["id"].Value); if (sourceId == tId) { LoggerBold("Source can't be destination! (" + tunerFiles[sourceId].FileName + ")"); } else { selectedFiles.Add(tId); } } } frmS.Dispose(); if (selectedFiles.Count == 0) { Logger("No files selected"); return; } frmSelectTableDataProperties fst = new frmSelectTableDataProperties(); fst.groupBox2.Visible = true; TableData tmpTd = new TableData(); fst.loadProperties(tmpTd, false); if (fst.ShowDialog() != DialogResult.OK) { return; } List <string> selectedProps = new List <string>(); for (int p = 0; p < fst.chkBoxes.Count; p++) { CheckBox chk = fst.chkBoxes[p]; if (chk.Checked) { selectedProps.Add(chk.Name); } } CopyMode copyMode = CopyMode.add; if (fst.radioAddMissing.Checked) { copyMode = CopyMode.addMissing; } if (fst.radioOwerwrite.Checked) { copyMode = CopyMode.overwrite; } if (fst.radioOwerwriteAdd.Checked) { copyMode = CopyMode.overwriteAdd; } fst.Dispose(); Logger("Copying tables..."); //Now we have list of target files, and list of properties to copy, lets do it for (int tf = 0; tf < selectedFiles.Count; tf++) { int dstF = selectedFiles[tf]; Logger(tunerFiles[dstF].FileName, false); Application.DoEvents(); int copiedTables = 0; for (int t = 0; t < sourceTdList.Count; t++) { TableData sourceTd = sourceTdList[t]; Type tdType = sourceTd.GetType(); int dstId = findTableDataId(sourceTd, tunerFiles[dstF].tableDatas); if (copyMode == CopyMode.overwrite) { if (dstId > -1) { for (int s = 0; s < selectedProps.Count; s++) { PropertyInfo tdProp = tdType.GetProperty(selectedProps[s]); tdProp.SetValue(tunerFiles[dstF].tableDatas[dstId], tdProp.GetValue(sourceTd, null), null); } copiedTables++; } } else if (copyMode == CopyMode.addMissing || copyMode == CopyMode.overwriteAdd) { if (dstId > -1) { if (copyMode == CopyMode.overwriteAdd) { for (int s = 0; s < selectedProps.Count; s++) { PropertyInfo tdProp = tdType.GetProperty(selectedProps[s]); tdProp.SetValue(tunerFiles[dstF].tableDatas[dstId], tdProp.GetValue(sourceTd, null), null); } copiedTables++; } } else { //Not found, add it TableData newTd = new TableData(); Type type = newTd.GetType(); foreach (var prop in sourceTd.GetType().GetProperties()) { if (selectedProps.Contains(prop.Name)) { prop.SetValue(newTd, prop.GetValue(sourceTd, null), null); } } tunerFiles[dstF].tableDatas.Add(newTd); copiedTables++; } } else if (copyMode == CopyMode.add) { TableData newTd = new TableData(); Type type = newTd.GetType(); foreach (var prop in sourceTd.GetType().GetProperties()) { if (selectedProps.Contains(prop.Name)) { prop.SetValue(newTd, prop.GetValue(sourceTd, null), null); } } tunerFiles[dstF].tableDatas.Add(newTd); copiedTables++; } } if (!modifiedFiles.Contains(dstF)) { modifiedFiles.Add(dstF); } tunerFiles[dstF].Modified = true; Logger(" [OK] (" + copiedTables.ToString() + " tables)"); } Logger("Done, you can now save files"); }
private void pasteSpecialToolStripMenuItem_Click(object sender, EventArgs e) { int row = -1; if (dataGridView1.SelectedCells.Count > 0) { row = dataGridView1.SelectedCells[0].RowIndex; } else if (dataGridView1.SelectedRows.Count > 0) { row = dataGridView1.SelectedRows[0].Index; } else { return; } int mmid = Convert.ToInt32(dataGridView1.Rows[row].Cells["id"].Value); TableData td = displayDatas[mmid].td; frmSelectTableDataProperties fst = new frmSelectTableDataProperties(); fst.loadProperties(td); for (int c = 0; c < fst.chkBoxes.Count; c++) { if (fst.chkBoxes[c].Name == "TableName") { fst.chkBoxes[c].Checked = true; } else { fst.chkBoxes[c].Checked = false; } } fst.btnOK.Text = "Next >"; fst.Text = "Select table search criteria"; fst.labelAction.Text = "Select table search criteria"; if (fst.ShowDialog() != DialogResult.OK) { fst.Dispose(); return; } fst.Dispose(); List <ClibBrd> myCriteria = new List <ClibBrd>(); for (int p = 0; p < fst.chkBoxes.Count; p++) { CheckBox chk = fst.chkBoxes[p]; if (chk.Checked && chk.Tag != null) { ClibBrd cb = new ClibBrd(); cb.Property = chk.Name; cb.Value = chk.Tag.ToString(); myCriteria.Add(cb); } } List <TableDataExtended> tdeList = new List <TableDataExtended>(); for (int tf = 0; tf < tunerFiles.Count; tf++) { for (int t = 0; t < tunerFiles[tf].tableDatas.Count; t++) { bool match = true; Type type = tunerFiles[tf].tableDatas[t].GetType(); for (int mc = 0; mc < myCriteria.Count; mc++) { PropertyInfo prop = type.GetProperty(myCriteria[mc].Property); if (prop.PropertyType == typeof(string)) { double percentage = ComputeSimilarity.CalculateSimilarity((string)prop.GetValue(tunerFiles[tf].tableDatas[t], null), myCriteria[mc].Value); if ((percentage * 100) < (double)Properties.Settings.Default.TableEditorMinOtherEquivalency) { match = false; break; } else { Debug.WriteLine((string)prop.GetValue(tunerFiles[tf].tableDatas[t], null) + " <=> " + myCriteria[mc].Value + "; Equivalency: " + (percentage * 100).ToString() + "%"); } } else if ((string)prop.GetValue(tunerFiles[tf].tableDatas[t], null).ToString() != myCriteria[mc].Value) { match = false; break; } } if (match) { //This table definition match criteria TableDataExtended tde = new TableDataExtended(); TableData tabledata = tunerFiles[tf].tableDatas[t]; foreach (var tdProp in tabledata.GetType().GetProperties()) { Type tdeType = tde.GetType(); PropertyInfo tdeProp = tdeType.GetProperty(tdProp.Name); tdeProp.SetValue(tde, tdProp.GetValue(tabledata, null), null); } tde.id = (uint)t; tde.fileId = tf; tde.File = tunerFiles[tf].FileName; tde.Select = true; tdeList.Add(tde); } } } frmSelectMassTarget frmSmt = new frmSelectMassTarget(); Application.DoEvents(); frmSmt.loadData(tdeList); if (frmSmt.ShowDialog() != DialogResult.OK) { frmSmt.Dispose(); return; } for (int r = 0; r < frmSmt.dataGridView1.Rows.Count; r++) { if (Convert.ToBoolean(frmSmt.dataGridView1.Rows[r].Cells["Select"].Value) == true) { int tf = Convert.ToInt32(frmSmt.dataGridView1.Rows[r].Cells["fileId"].Value); int t = Convert.ToInt32(frmSmt.dataGridView1.Rows[r].Cells["id"].Value); Logger("Updating table list: " + tunerFiles[tf].FileName); for (int cb = 0; cb < clipBrd.Count; cb++) { TableData tabledata = tunerFiles[tf].tableDatas[t]; Type tdType = tabledata.GetType(); PropertyInfo tdProp = tdType.GetProperty(clipBrd[cb].Property); if (tdProp.PropertyType.IsEnum) { tdProp.SetValue(tunerFiles[tf].tableDatas[t], Enum.Parse(tdProp.PropertyType, clipBrd[cb].Value), null); } else { tdProp.SetValue(tunerFiles[tf].tableDatas[t], Convert.ChangeType(clipBrd[cb].Value, tdProp.PropertyType), null); } Logger("Property: " + clipBrd[cb].Property + ", value: " + clipBrd[cb].Value); } modifiedFiles.Add(tf); tunerFiles[tf].Modified = true; } } Logger("Done. (Save files manually)"); }