draft for saving split files

This commit is contained in:
abdelkader
2024-04-02 12:31:33 -04:00
parent 4401f13fb4
commit 2023043889
11 changed files with 180 additions and 45 deletions

View File

@@ -44,6 +44,9 @@ namespace VCFEditor.Model
[Browsable(false)]
public bool isDeleted { get; set; }
[Browsable(false)]
public string path { get; set; }
public Contact()
@@ -53,6 +56,11 @@ namespace VCFEditor.Model
isDirty = false;
}
public Contact(string path) : this()
{
this.path = path;
}
private void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

View File

@@ -42,9 +42,14 @@ namespace VCFEditor.Presenter
_view.CountImagesEvent += _view_CountImages;
_view.ClearImagesEvent += _view_ClearImages;
_view.BatchExportImagesEvent += _view_BatchExportImagesEvent;
_view.SplitFileEvent += SaveSplittedFileHandler;
}
private void _view_BatchExportImagesEvent(object sender, EventArgs e)
{
if (_repository.Contacts == null || _repository.Contacts.Count == 0)
@@ -269,6 +274,19 @@ namespace VCFEditor.Presenter
}
private void SaveSplittedFileHandler(object sender, EventArgs e)
{
if (_repository.Contacts == null || _repository.Contacts.Count == 0)
return;
string Path = _view.DisplayOpenFolderDialog();
int count = 0;
if (!string.IsNullOrEmpty(Path))
count = _repository.SaveSplittedFiles(Path);
_view.DisplayMessage(string.Format("{0} contact(s) processed!", count), "Information");
}
private void BeforeOpeningNewFileHandler()
{
if (_repository.Contacts != null && _repository.dirty)

View File

@@ -389,8 +389,47 @@ namespace VCFEditor.Repository
public string GenerateFileName(string fileName, int index, string extension)
{
string result = Path.Combine(Path.GetDirectoryName(fileName), index.ToString() + "." + extension);
string result = _fileHandler.GetFileNameWithExtension(fileName, index, extension);
return result;
}
public int SaveSplittedFiles(string FolderPath)
{
//Do not save the deleted ones!
var contactsToSave = Contacts.Where(x => !x.isDeleted).ToList();
int count;
for (count = 0; count < contactsToSave.Count(); count++)
{
var entry = contactsToSave[count];
string SerializedCard = GenerateStringFromVCard(entry.card);
//Check if filename for the card is empty, and generate one if empty
if (string.IsNullOrEmpty(entry.path))
entry.path = GenerateFileName(FolderPath, entry.FamilyName, count);
_fileHandler.WriteAllText(entry.path, SerializedCard);
//Clean the flag for every contact, even the deleted ones.
entry.isDirty = false;
}
//Clean the global flag for the entire vCard Catalog.
_dirty = false;
//return number of contacts processed!
return count;
}
private string GenerateFileName(string FolderPath, string familyName, int index)
{
string FinalPath;
if (string.IsNullOrEmpty(familyName))
FinalPath = _fileHandler.GetVcfFileName(FolderPath, index.ToString());
else
FinalPath = _fileHandler.GetVcfFileName(FolderPath, familyName);
return FinalPath;
}
}
}

View File

@@ -45,5 +45,15 @@ namespace vCardEditor.Repository
ms.WriteTo(fs);
}
}
public string GetVcfFileName(string folderPath, string filename)
{
return Path.Combine(folderPath, filename + ".vcf");
}
public string GetFileNameWithExtension(string fileName, int index, string extension)
{
return Path.Combine(Path.GetDirectoryName(fileName), index.ToString() + "." + extension);
}
}
}

View File

@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thought.vCards;
using VCFEditor.Model;
using System.ComponentModel;
using vCardEditor.View;
namespace VCFEditor.Repository
@@ -28,5 +24,6 @@ namespace VCFEditor.Repository
string GenerateStringFromVCard(vCard card);
string GenerateFileName(string fileName, int index, string extension);
int SaveSplittedFiles(string Path);
}
}

View File

@@ -9,5 +9,7 @@
string GetExtension(string path);
string ChangeExtension(string path, string extension);
void WriteBytesToFile(string imageFile, byte[] image);
string GetVcfFileName(string folderPath, string familyName);
string GetFileNameWithExtension(string fileName, int index, string extension);
}
}

View File

@@ -30,6 +30,7 @@ namespace VCFEditor.View
event EventHandler CountImagesEvent;
event EventHandler ClearImagesEvent;
event EventHandler BatchExportImagesEvent;
event EventHandler SplitFileEvent;
int SelectedContactIndex { get; }
void DisplayContacts(SortableBindingList<Contact> contacts);
void DisplayContactDetail(vCard card, string FileName);
@@ -48,5 +49,6 @@ namespace VCFEditor.View
void DisplayQRCode(string content);
void ClearImageFromForm();
string DisplayOpenFolderDialog();
}
}

View File

@@ -32,7 +32,7 @@ namespace vCardEditor.View
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.miSave = new System.Windows.Forms.ToolStripMenuItem();
@@ -56,8 +56,8 @@ namespace vCardEditor.View
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.tbsNew = new System.Windows.Forms.ToolStripButton();
this.tbsOpen = new System.Windows.Forms.ToolStripButton();
this.tbsSave = new System.Windows.Forms.ToolStripButton();
this.tbsOpen = new System.Windows.Forms.ToolStripSplitButton();
this.openFolderToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.tbsDelete = new System.Windows.Forms.ToolStripButton();
this.tbsQR = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
@@ -104,6 +104,8 @@ namespace vCardEditor.View
this.menuExtraField = new System.Windows.Forms.ContextMenuStrip(this.components);
this.miNote = new System.Windows.Forms.ToolStripMenuItem();
this.miOrg = new System.Windows.Forms.ToolStripMenuItem();
this.tbsSave = new System.Windows.Forms.ToolStripSplitButton();
this.splitToFilesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.toolStrip1.SuspendLayout();
this.gbNameList.SuspendLayout();
@@ -135,7 +137,7 @@ namespace vCardEditor.View
this.helpToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1202, 30);
this.menuStrip1.Size = new System.Drawing.Size(1202, 28);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
@@ -149,7 +151,7 @@ namespace vCardEditor.View
this.recentFilesMenuItem,
this.miQuit});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 26);
this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 24);
this.fileToolStripMenuItem.Text = "File";
//
// miSave
@@ -200,7 +202,7 @@ namespace vCardEditor.View
this.copyToolStripMenuItem,
this.extraFieldsToolStripMenuItem});
this.editToolStripMenuItem.Name = "editToolStripMenuItem";
this.editToolStripMenuItem.Size = new System.Drawing.Size(49, 26);
this.editToolStripMenuItem.Size = new System.Drawing.Size(49, 24);
this.editToolStripMenuItem.Text = "Edit";
//
// copyToolStripMenuItem
@@ -238,7 +240,7 @@ namespace vCardEditor.View
this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.imagesToolStripMenuItem});
this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
this.toolsToolStripMenuItem.Size = new System.Drawing.Size(58, 26);
this.toolsToolStripMenuItem.Size = new System.Drawing.Size(58, 24);
this.toolsToolStripMenuItem.Text = "Tools";
//
// imagesToolStripMenuItem
@@ -277,7 +279,7 @@ namespace vCardEditor.View
this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.miAbout});
this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
this.helpToolStripMenuItem.Size = new System.Drawing.Size(55, 26);
this.helpToolStripMenuItem.Size = new System.Drawing.Size(55, 24);
this.helpToolStripMenuItem.Text = "Help";
//
// miAbout
@@ -311,9 +313,9 @@ namespace vCardEditor.View
this.toolStripSeparator1,
this.tbsAbout,
this.toolStripSeparator});
this.toolStrip1.Location = new System.Drawing.Point(0, 30);
this.toolStrip1.Location = new System.Drawing.Point(0, 28);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(1202, 31);
this.toolStrip1.Size = new System.Drawing.Size(1202, 27);
this.toolStrip1.TabIndex = 1;
this.toolStrip1.Text = "toolStrip1";
//
@@ -323,29 +325,28 @@ namespace vCardEditor.View
this.tbsNew.Image = ((System.Drawing.Image)(resources.GetObject("tbsNew.Image")));
this.tbsNew.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsNew.Name = "tbsNew";
this.tbsNew.Size = new System.Drawing.Size(29, 28);
this.tbsNew.Size = new System.Drawing.Size(29, 24);
this.tbsNew.Text = "&Nouveau";
this.tbsNew.Click += new System.EventHandler(this.tbsNew_Click);
//
// tbsOpen
//
this.tbsOpen.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tbsOpen.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.openFolderToolStripMenuItem});
this.tbsOpen.Image = ((System.Drawing.Image)(resources.GetObject("tbsOpen.Image")));
this.tbsOpen.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsOpen.Name = "tbsOpen";
this.tbsOpen.Size = new System.Drawing.Size(29, 28);
this.tbsOpen.Size = new System.Drawing.Size(39, 24);
this.tbsOpen.Text = "&Open";
this.tbsOpen.Click += new System.EventHandler(this.tbsOpen_Click);
this.tbsOpen.ButtonClick += new System.EventHandler(this.tbsOpen_Click);
//
// tbsSave
// openFolderToolStripMenuItem
//
this.tbsSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tbsSave.Image = ((System.Drawing.Image)(resources.GetObject("tbsSave.Image")));
this.tbsSave.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsSave.Name = "tbsSave";
this.tbsSave.Size = new System.Drawing.Size(29, 28);
this.tbsSave.Text = "&Save";
this.tbsSave.Click += new System.EventHandler(this.tbsSave_Click);
this.openFolderToolStripMenuItem.Name = "openFolderToolStripMenuItem";
this.openFolderToolStripMenuItem.Size = new System.Drawing.Size(174, 26);
this.openFolderToolStripMenuItem.Text = "Open Folder";
this.openFolderToolStripMenuItem.Click += new System.EventHandler(this.openFolderToolStripMenuItem_Click);
//
// tbsDelete
//
@@ -353,7 +354,7 @@ namespace vCardEditor.View
this.tbsDelete.Image = ((System.Drawing.Image)(resources.GetObject("tbsDelete.Image")));
this.tbsDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsDelete.Name = "tbsDelete";
this.tbsDelete.Size = new System.Drawing.Size(29, 28);
this.tbsDelete.Size = new System.Drawing.Size(29, 24);
this.tbsDelete.Text = "Delete";
this.tbsDelete.Click += new System.EventHandler(this.tbsDelete_Click);
//
@@ -363,14 +364,14 @@ namespace vCardEditor.View
this.tbsQR.Image = global::vCardEditor.Properties.Resources.nuget_icon;
this.tbsQR.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsQR.Name = "tbsQR";
this.tbsQR.Size = new System.Drawing.Size(29, 28);
this.tbsQR.Size = new System.Drawing.Size(29, 24);
this.tbsQR.Text = "&QR Code";
this.tbsQR.Click += new System.EventHandler(this.tbsQR_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 31);
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 27);
//
// tbsAbout
//
@@ -378,14 +379,14 @@ namespace vCardEditor.View
this.tbsAbout.Image = ((System.Drawing.Image)(resources.GetObject("tbsAbout.Image")));
this.tbsAbout.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsAbout.Name = "tbsAbout";
this.tbsAbout.Size = new System.Drawing.Size(29, 28);
this.tbsAbout.Size = new System.Drawing.Size(29, 24);
this.tbsAbout.Text = "&?";
this.tbsAbout.Click += new System.EventHandler(this.tbsAbout_Click);
//
// toolStripSeparator
//
this.toolStripSeparator.Name = "toolStripSeparator";
this.toolStripSeparator.Size = new System.Drawing.Size(6, 31);
this.toolStripSeparator.Size = new System.Drawing.Size(6, 27);
//
// openFileDialog
//
@@ -402,7 +403,7 @@ namespace vCardEditor.View
this.gbNameList.Margin = new System.Windows.Forms.Padding(4);
this.gbNameList.Name = "gbNameList";
this.gbNameList.Padding = new System.Windows.Forms.Padding(4);
this.gbNameList.Size = new System.Drawing.Size(398, 580);
this.gbNameList.Size = new System.Drawing.Size(398, 586);
this.gbNameList.TabIndex = 2;
this.gbNameList.TabStop = false;
this.gbNameList.Text = "Name List :";
@@ -412,8 +413,8 @@ namespace vCardEditor.View
this.dgContacts.AllowUserToAddRows = false;
this.dgContacts.AllowUserToDeleteRows = false;
this.dgContacts.AllowUserToResizeRows = false;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.dgContacts.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.dgContacts.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle2;
this.dgContacts.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
@@ -434,7 +435,7 @@ namespace vCardEditor.View
this.dgContacts.RowHeadersVisible = false;
this.dgContacts.RowHeadersWidth = 51;
this.dgContacts.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dgContacts.Size = new System.Drawing.Size(390, 527);
this.dgContacts.Size = new System.Drawing.Size(390, 533);
this.dgContacts.TabIndex = 2;
this.dgContacts.CellContextMenuStripNeeded += new System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventHandler(this.dgContacts_CellContextMenuStripNeeded);
this.dgContacts.RowLeave += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgContacts_RowLeave);
@@ -517,7 +518,7 @@ namespace vCardEditor.View
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 61);
this.splitContainer1.Location = new System.Drawing.Point(0, 55);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
@@ -527,7 +528,7 @@ namespace vCardEditor.View
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.tcMainTab);
this.splitContainer1.Size = new System.Drawing.Size(1202, 580);
this.splitContainer1.Size = new System.Drawing.Size(1202, 586);
this.splitContainer1.SplitterDistance = 398;
this.splitContainer1.TabIndex = 4;
//
@@ -540,7 +541,7 @@ namespace vCardEditor.View
this.tcMainTab.Location = new System.Drawing.Point(0, 0);
this.tcMainTab.Name = "tcMainTab";
this.tcMainTab.SelectedIndex = 0;
this.tcMainTab.Size = new System.Drawing.Size(800, 580);
this.tcMainTab.Size = new System.Drawing.Size(800, 586);
this.tcMainTab.TabIndex = 0;
//
// TapPageMain
@@ -556,7 +557,7 @@ namespace vCardEditor.View
this.TapPageMain.Location = new System.Drawing.Point(4, 25);
this.TapPageMain.Name = "TapPageMain";
this.TapPageMain.Padding = new System.Windows.Forms.Padding(3);
this.TapPageMain.Size = new System.Drawing.Size(792, 551);
this.TapPageMain.Size = new System.Drawing.Size(792, 557);
this.TapPageMain.TabIndex = 0;
this.TapPageMain.Text = "Main";
//
@@ -569,7 +570,7 @@ namespace vCardEditor.View
this.extendedPanelWeb.Location = new System.Drawing.Point(386, 389);
this.extendedPanelWeb.Name = "extendedPanelWeb";
this.extendedPanelWeb.panelType = vCardEditor.View.Customs.PanelType.Web;
this.extendedPanelWeb.Size = new System.Drawing.Size(397, 155);
this.extendedPanelWeb.Size = new System.Drawing.Size(397, 161);
this.extendedPanelWeb.TabIndex = 59;
//
// extendedPanelPhones
@@ -580,7 +581,7 @@ namespace vCardEditor.View
this.extendedPanelPhones.Location = new System.Drawing.Point(13, 389);
this.extendedPanelPhones.Name = "extendedPanelPhones";
this.extendedPanelPhones.panelType = vCardEditor.View.Customs.PanelType.Phone;
this.extendedPanelPhones.Size = new System.Drawing.Size(367, 155);
this.extendedPanelPhones.Size = new System.Drawing.Size(367, 161);
this.extendedPanelPhones.TabIndex = 58;
//
// btnExportImage
@@ -861,6 +862,25 @@ namespace vCardEditor.View
this.miOrg.Text = "Organisation";
this.miOrg.Click += new System.EventHandler(this.miOrg_Click);
//
// tbsSave
//
this.tbsSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tbsSave.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.splitToFilesToolStripMenuItem});
this.tbsSave.Image = ((System.Drawing.Image)(resources.GetObject("tbsSave.Image")));
this.tbsSave.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbsSave.Name = "tbsSave";
this.tbsSave.Size = new System.Drawing.Size(39, 24);
this.tbsSave.Text = "&Save";
this.tbsSave.ButtonClick += new System.EventHandler(this.tbsSave_Click);
//
// splitToFilesToolStripMenuItem
//
this.splitToFilesToolStripMenuItem.Name = "splitToFilesToolStripMenuItem";
this.splitToFilesToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.splitToFilesToolStripMenuItem.Text = "Split to files";
this.splitToFilesToolStripMenuItem.Click += new System.EventHandler(this.splitToFilesToolStripMenuItem_Click);
//
// MainForm
//
this.AllowDrop = true;
@@ -919,8 +939,6 @@ namespace vCardEditor.View
private System.Windows.Forms.ToolStripMenuItem miAbout;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton tbsOpen;
private System.Windows.Forms.ToolStripButton tbsSave;
private System.Windows.Forms.ToolStripButton tbsDelete;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripButton tbsAbout;
@@ -981,5 +999,9 @@ namespace vCardEditor.View
private System.Windows.Forms.ToolStripMenuItem miOrg;
private ExtendedPanel extendedPanelWeb;
private ExtendedPanel extendedPanelPhones;
private System.Windows.Forms.ToolStripSplitButton tbsOpen;
private System.Windows.Forms.ToolStripMenuItem openFolderToolStripMenuItem;
private System.Windows.Forms.ToolStripSplitButton tbsSave;
private System.Windows.Forms.ToolStripMenuItem splitToFilesToolStripMenuItem;
}
}

View File

@@ -37,9 +37,11 @@ namespace vCardEditor.View
public event EventHandler CountImagesEvent;
public event EventHandler ClearImagesEvent;
public event EventHandler BatchExportImagesEvent;
public event EventHandler SplitFileEvent;
ComponentResourceManager resources;
private int LastRowIndex = -1;
@@ -99,6 +101,9 @@ namespace vCardEditor.View
private void tbsSave_Click(object sender, EventArgs e)
{
if (dgContacts.RowCount == 0)
return;
if (SaveContactsSelected != null)
{
//make sure the last changes in the textboxes is saved.
@@ -703,5 +708,25 @@ namespace vCardEditor.View
{
BatchExportImagesEvent?.Invoke(sender, e);
}
private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
private void splitToFilesToolStripMenuItem_Click(object sender, EventArgs e)
{
SplitFileEvent?.Invoke(sender, e);
}
public string DisplayOpenFolderDialog()
{
string result = string.Empty;
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
result = dialog.SelectedPath;
return result;
}
}
}

View File

@@ -178,8 +178,7 @@ namespace vCardEditor_Test
repo.AddEmptyContact();
Assert.IsTrue(repo.Contacts.Count == 0);
Assert.IsTrue(repo.Contacts.Count == 1);
}
}
}

View File

@@ -259,6 +259,19 @@ namespace vCardEditor_Test
Assert.IsTrue(repo.Contacts[0].isDirty);
}
[TestMethod]
public void AddContact_ShouldCreateEmtptyContactFile_Test()
{
var fileHandler = Substitute.For<IFileHandler>();
var repo = Substitute.For<ContactRepository>(fileHandler);
var view = Substitute.For<IMainView>();
_ = new MainPresenter(view, repo);
view.AddContact += Raise.Event();
view.Received().DisplayContacts(Arg.Any<SortableBindingList<Contact>>());
Assert.IsTrue(repo.Contacts.Count == 1);
}
}
}