using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Johnvey.GmailAgent;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace Johnvey.GmailAgent.Applet {
/// <summary>
/// Summary description for Contacts.
/// </summary>
public class Contacts : System.Windows.Forms.Form {
private GmailSessionCollection accounts;
private GmailAdapter gmail;
public DataTable tableToImport;
private System.Windows.Forms.DataGrid localContactsGrid;
private System.Windows.Forms.DataGrid gmailContactsGrid;
private System.Windows.Forms.ComboBox accountsList;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button openAddressButton;
private System.Windows.Forms.Button importButton;
private System.Windows.Forms.Button closeButton;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.Button clearButton;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ProgressBar progressBar;
private System.Windows.Forms.Label label6;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Contacts(BaseWindow parentForm) {
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.gmail = new GmailAdapter();
this.gmail.JsVersion = parentForm.gmail.JsVersion;
this.accounts = parentForm.accounts;
foreach(GmailSession session in accounts) {
accountsList.Items.Add(session);
accountsList.DisplayMember = "Username";
}
this.tableToImport = new DataTable();
tableToImport.Columns.Add(new DataColumn("Name", typeof(string)));
tableToImport.Columns.Add(new DataColumn("Email", typeof(string)));
tableToImport.Columns.Add(new DataColumn("Notes", typeof(string)));
this.localContactsGrid.DataSource = tableToImport;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Contacts));
this.localContactsGrid = new System.Windows.Forms.DataGrid();
this.gmailContactsGrid = new System.Windows.Forms.DataGrid();
this.accountsList = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.importButton = new System.Windows.Forms.Button();
this.openAddressButton = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.closeButton = new System.Windows.Forms.Button();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.clearButton = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.label6 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.localContactsGrid)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gmailContactsGrid)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// localContactsGrid
//
this.localContactsGrid.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)));
this.localContactsGrid.CaptionText = "Add contacts below";
this.localContactsGrid.CaptionVisible = false;
this.localContactsGrid.DataMember = "";
this.localContactsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.localContactsGrid.Location = new System.Drawing.Point(24, 120);
this.localContactsGrid.Name = "localContactsGrid";
this.localContactsGrid.Size = new System.Drawing.Size(272, 284);
this.localContactsGrid.TabIndex = 0;
//
// gmailContactsGrid
//
this.gmailContactsGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.gmailContactsGrid.CaptionVisible = false;
this.gmailContactsGrid.DataMember = "";
this.gmailContactsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.gmailContactsGrid.Location = new System.Drawing.Point(320, 120);
this.gmailContactsGrid.Name = "gmailContactsGrid";
this.gmailContactsGrid.PreferredColumnWidth = 100;
this.gmailContactsGrid.ReadOnly = true;
this.gmailContactsGrid.RowHeadersVisible = false;
this.gmailContactsGrid.Size = new System.Drawing.Size(272, 284);
this.gmailContactsGrid.TabIndex = 2;
//
// accountsList
//
this.accountsList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.accountsList.Location = new System.Drawing.Point(120, 48);
this.accountsList.Name = "accountsList";
this.accountsList.Size = new System.Drawing.Size(232, 21);
this.accountsList.TabIndex = 0;
this.accountsList.SelectedIndexChanged += new System.EventHandler(this.accountsList_onChange);
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(24, 96);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 3;
this.label1.Text = "Local Contacts";
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(320, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(176, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Gmail Contacts";
//
// label3
//
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.Location = new System.Drawing.Point(8, 52);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(112, 16);
this.label3.TabIndex = 5;
this.label3.Text = "Select an account:";
//
// groupBox1
//
this.groupBox1.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)));
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.clearButton);
this.groupBox1.Controls.Add(this.importButton);
this.groupBox1.Controls.Add(this.openAddressButton);
this.groupBox1.Location = new System.Drawing.Point(8, 80);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(600, 376);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
//
// importButton
//
this.importButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.importButton.Enabled = false;
this.importButton.Location = new System.Drawing.Point(312, 336);
this.importButton.Name = "importButton";
this.importButton.Size = new System.Drawing.Size(104, 23);
this.importButton.TabIndex = 3;
this.importButton.Text = "Import contacts";
this.importButton.Click += new System.EventHandler(this.import_onClick);
//
// openAddressButton
//
this.openAddressButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.openAddressButton.Location = new System.Drawing.Point(16, 336);
this.openAddressButton.Name = "openAddressButton";
this.openAddressButton.Size = new System.Drawing.Size(128, 23);
this.openAddressButton.TabIndex = 1;
this.openAddressButton.Text = "Open address file...";
this.openAddressButton.Click += new System.EventHandler(this.openAddress_onClick);
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 16);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(400, 23);
this.label4.TabIndex = 7;
this.label4.Text = "Import contacts into your Gmail address book from a tab-delimited text file.";
//
// closeButton
//
this.closeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.closeButton.Location = new System.Drawing.Point(528, 467);
this.closeButton.Name = "closeButton";
this.closeButton.TabIndex = 2;
this.closeButton.Text = "Close";
this.closeButton.Click += new System.EventHandler(this.close_onClick);
//
// openFileDialog
//
this.openFileDialog.Filter = "Tab-delimited Text File (*.txt)|*.txt|All Files (*.*)|*.*";
this.openFileDialog.Title = "Open Address File";
//
// clearButton
//
this.clearButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.clearButton.Location = new System.Drawing.Point(160, 336);
this.clearButton.Name = "clearButton";
this.clearButton.TabIndex = 4;
this.clearButton.Text = "Clear";
this.clearButton.Click += new System.EventHandler(this.clear_onClick);
//
// label5
//
this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label5.Location = new System.Drawing.Point(16, 464);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(440, 32);
this.label5.TabIndex = 8;
this.label5.Text = "Tip: Your address file must have 3 tab-delimited columns: Name, Email, and Notes." +
" Once you open the file, you can edit the datagrid directly before you import.";
//
// progressBar
//
this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.progressBar.Location = new System.Drawing.Point(184, 232);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(248, 40);
this.progressBar.Step = 1;
this.progressBar.TabIndex = 9;
this.progressBar.Visible = false;
//
// label6
//
this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label6.Location = new System.Drawing.Point(288, 152);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(24, 32);
this.label6.TabIndex = 5;
this.label6.Text = "�";
//
// Contacts
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 505);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.label5);
this.Controls.Add(this.closeButton);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.accountsList);
this.Controls.Add(this.gmailContactsGrid);
this.Controls.Add(this.localContactsGrid);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(600, 500);
this.Name = "Contacts";
this.Text = "Contacts";
((System.ComponentModel.ISupportInitialize)(this.localContactsGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gmailContactsGrid)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void accountsList_onChange(object sender, System.EventArgs e) {
GmailSession workingSession = (GmailSession)(((ComboBox)sender).SelectedItem);
UpdateGmailContacts(workingSession);
}
private void UpdateGmailContacts(GmailSession workingSession) {
this.Cursor = Cursors.WaitCursor;
DataTable displayTable = new DataTable();
displayTable.Columns.Add("Name", typeof(string));
displayTable.Columns.Add("Email", typeof(string));
displayTable.Columns.Add("Notes", typeof(string));
this.gmail.Refresh(workingSession);
if(!workingSession.HasConnectionError) {
GmailContactCollection contacts = this.gmail.GetContacts();
foreach(GmailContact contact in contacts) {
DataRow tmpRow = displayTable.NewRow();
tmpRow["Name"] = contact.Name;
tmpRow["Email"] = contact.Email;
tmpRow["Notes"] = contact.Notes;
displayTable.Rows.Add(tmpRow);
}
this.gmailContactsGrid.DataSource = displayTable;
this.importButton.Enabled = true;
} else {
MessageBox.Show("There was an error connecting to Gmail. Check your login information.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Cursor = Cursors.Default;
}
private void openAddress_onClick(object sender, System.EventArgs e) {
this.Cursor = Cursors.WaitCursor;
if(this.openFileDialog.ShowDialog() == DialogResult.OK) {
this.tableToImport.Rows.Clear();
ReadAddresses(openFileDialog.FileName);
}
this.Cursor = Cursors.Default;
}
private void ReadAddresses(string fileName) {
StreamReader sr = new StreamReader(fileName);
string currentLine;
while((currentLine = sr.ReadLine()) != null) {
currentLine = currentLine.Replace("\"", "");
string[] components = currentLine.Split(new char[] {'\t'});
if(components.Length >= 2) {
if(Regex.Match(components[1], @"[A-Za-z0-9\-_\.]+@[A-Za-z0-9]+([\-\.][A-Za-z0-9]+)+", RegexOptions.Compiled).Success) {
DataRow tmpRow = this.tableToImport.NewRow();
tmpRow["Name"] = components[0].Trim();
tmpRow["Email"] = components[1].Trim();
if(components.Length >=3) {
tmpRow["Notes"] = components[2].Trim();
}
this.tableToImport.Rows.Add(tmpRow);
}
}
}
}
private void close_onClick(object sender, System.EventArgs e) {
this.Close();
}
private void import_onClick(object sender, System.EventArgs e) {
this.Cursor = Cursors.WaitCursor;
this.gmailContactsGrid.ForeColor = Color.Gray;
this.gmailContactsGrid.Enabled = false;
this.progressBar.Value = 0;
this.progressBar.Maximum = this.tableToImport.Rows.Count;
this.progressBar.Visible = true;
foreach(DataRow row in this.tableToImport.Rows) {
gmail.AddContact(row["Name"].ToString(), row["Email"].ToString(), row["Notes"].ToString());
this.progressBar.Value += 1;
}
UpdateGmailContacts((GmailSession)this.accountsList.SelectedItem);
MessageBox.Show("Finished importing contacts", "Contact Import", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.progressBar.Visible = false;
this.gmailContactsGrid.Enabled = true;
this.gmailContactsGrid.ForeColor = Color.Black;
this.Cursor = Cursors.Default;
}
private void clear_onClick(object sender, System.EventArgs e) {
this.tableToImport.Rows.Clear();
}
}
}