using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Net; using System.Xml; using System.Globalization; using System.Threading; namespace Picasa2Piwigo { using Picasa2PiwigoConstants; using Picasa2PiwigoCommonFunctions; public partial class MainForm : Form { string[] command_line_args; XmlDocument AlbumList = new XmlDocument(); public struct AlbumListStruct { public string album_name; public string full_album_path; public int album_index; public string album_parent_chain; } AlbumListStruct[] album_list_array = new AlbumListStruct[1024]; int album_count; public MainForm(string[] args) { CultureInfo ci; ci = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name); Thread.CurrentThread.CurrentUICulture = ci; Thread.CurrentThread.CurrentCulture = ci; if (Picasa2PiwigoConstants.constants.DEBUG_LOCALE) { #pragma warning disable 0162 Thread.CurrentThread.CurrentUICulture = new CultureInfo(Picasa2PiwigoConstants.constants.SET_LOCALE); #pragma warning restore 0162 } InitializeComponent(); command_line_args = args; } private void Form1_Load(object sender, EventArgs e) { int command_length, i; this.AddToAlbumGroupbox.Text = WinFormStrings.MFAddToAlbumGroupboxStrRes; this.AddTopAlbumCheckbox.Text = WinFormStrings.MFAddTopAlbumCheckboxStrRes; this.label1.Text = WinFormStrings.MFlabelStrRes; this.label2.Text = WinFormStrings.MFlabel2StrRes; this.label3.Text = WinFormStrings.MFlabel3StrRes; this.label4.Text = WinFormStrings.MFlabel4StrRes; this.label5.Text = WinFormStrings.MFlabel5StrRes; this.NewAlbumGroupBox.Text = WinFormStrings.MFNewAlbumGroupBoxStrRes; this.NewAlbumRadioButton.Text = WinFormStrings.MFNewAlbumRadioButtonStrRes; this.OptionsButton.Text = WinFormStrings.MFOptionsButtonStrRes; this.SyncButton.Text = WinFormStrings.MFSyncButtonStrRes; this.Text = WinFormStrings.MFVersionStrRes; this.AccessComboBox.Items.Add(WinFormStrings.PublicStrRes); this.AccessComboBox.Items.Add(WinFormStrings.PrivateStrRes); LabelAdjust AdjustLabelClass = new LabelAdjust(); AdjustLabelClass.AdjustLabelSize(label1); AdjustLabelClass.AdjustLabelSize(label2); AdjustLabelClass.AdjustLabelSize(label3); AdjustLabelClass.AdjustLabelSize(label4); AdjustLabelClass.AdjustRadioButtonSize(NewAlbumRadioButton); AdjustLabelClass.AdjustRadioButtonSize(AddTopAlbumCheckbox); AdjustLabelClass.AdjustButtonSize(SyncButton); AdjustLabelClass.AdjustButtonSize(OptionsButton); this.SyncButton.Enabled = false; this.Show(); this.Refresh(); //if no arguments then exit if (command_line_args.Length == 0) { Application.Exit(); } command_length = 0; for (i = 0; i < command_line_args.Length; i++) { command_length += command_line_args[i].Length; } if (command_length >= ((32 * 1024) - 512)) { MessageBox.Show(WinFormStrings.CommandLineTooLongStrRes, "Picasa2Piwigo"); Application.Exit(); } AccessComboBox.SelectedIndex = Form1Settings.Default.AccessSetting; this.SyncButton.Enabled = true; //determine album name, if possible AlbumNameTextBox.Text = DetermineAlbumNameFromCommandLine(command_line_args[0]); //determine album description, if possible DescriptionTextBox.Text = DetermineAlbumDescription(command_line_args[0]); if (FormSettings.Default.PiwigoUrlSetting.Length == 0 || FormSettings.Default.PicasaContactsLocation.Length == 0) { MessageBox.Show(WinFormStrings.SetUpConnStrRes, "Picasa2Piwigo"); return; } //ok to try to login etc LoginBuildAlbumList(); //populate album listbox PopulateAlbumListBox(); //see if there's a hint in the pathname to an existing parent album DetermineParentAlbum(command_line_args[0]); } private void SyncButton_Click(object sender, EventArgs e) { DialogResult result; if (NewAlbumRadioButton.Checked) { if (AlbumNameTextBox.Text.Length == 0) { MessageBox.Show(WinFormStrings.BlankAlbumNameStrRes); return; } GlobalClass.NewAlbumName = AlbumNameTextBox.Text.Trim(); GlobalClass.NewAlbumDescription = DescriptionTextBox.Text.Trim(); if (NewAlbumParentAlbumComboBox.SelectedIndex == -1) { GlobalClass.NewAlbumNameParentID = -1; } else { GlobalClass.NewAlbumNameParentID = album_list_array[NewAlbumParentAlbumComboBox.SelectedIndex].album_index; } if (AlbumAlreadyExists(NewAlbumParentAlbumComboBox.SelectedIndex)) { MessageBox.Show(WinFormStrings.AlbumExistsStrRes); return; } Form StatusForm = new StatusForm(constants.COMMAND_NEW_ALBUM, command_line_args); result = StatusForm.ShowDialog(); StatusForm.Close(); if (result == DialogResult.No) { MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(WinFormStrings.AddNewCompleteStrRes); } Application.Exit(); } if (AddTopAlbumCheckbox.Checked) { if (AddToAlbumComboBox.SelectedIndex == -1) { MessageBox.Show(WinFormStrings.NoAlbumSelectedStrRes); return; } GlobalClass.AddAlbumCategoryID = album_list_array[AddToAlbumComboBox.SelectedIndex].album_index; Form StatusForm = new StatusForm(constants.COMMAND_ADD_TO_ALBUM, command_line_args); result = StatusForm.ShowDialog(); StatusForm.Close(); if (result == DialogResult.No) { MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(WinFormStrings.AddToAlbumStrRes); } Application.Exit(); } } private void OptionsButton_Click(object sender, EventArgs e) { DialogResult option_form_result; Form OptionsForm = new OptionsForm(); OptionsButton.Enabled = false; option_form_result = OptionsForm.ShowDialog(); OptionsButton.Enabled = true; if (option_form_result == DialogResult.OK) { if (FormSettings.Default.PiwigoUrlSetting.Length != 0 && FormSettings.Default.PicasaContactsLocation.Length != 0) { this.SyncButton.Enabled = true; //ok to try to login etc LoginBuildAlbumList(); //populate album listbox PopulateAlbumListBox(); } } } private void NewAlbumRadioButton_CheckedChanged(object sender, EventArgs e) { if (NewAlbumRadioButton.Checked) { NewAlbumGroupBox.Enabled = true; AddToAlbumGroupbox.Enabled = false; } } private void AddTopAlbumCheckbox_CheckedChanged(object sender, EventArgs e) { if (AddTopAlbumCheckbox.Checked) { NewAlbumGroupBox.Enabled = false; AddToAlbumGroupbox.Enabled = true; } } public void LoginBuildAlbumList() { DialogResult result; Form StatusForm = new StatusForm(constants.COMMAND_START_UP, command_line_args); result = StatusForm.ShowDialog(); StatusForm.Close(); if (result == DialogResult.No) { MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes, MessageBoxButtons.OK, MessageBoxIcon.Error); } AlbumList = GlobalClass.AlbumList; //build album list BuildAlbumList(AlbumList); } public string DetermineAlbumNameFromCommandLine(string command_line) { int k, l, m; string album_name; k = command_line.LastIndexOf("\\"); album_name = ""; if (k != -1) { l = command_line.LastIndexOf("\\", k - 1); m = command_line.LastIndexOf(":", k - 1); if (l != -1) { album_name = command_line.Substring(l + 1, k - l - 1); } else if (m != -1) { album_name = command_line.Substring(m + 1, k - m - 1); } } return album_name; } public string DetermineAlbumDescription(string command_line) { string album_desc, album_path, inidata; int k; System.IO.StreamReader inifile; bool found_album_mdata; album_desc = ""; k = command_line.LastIndexOf("\\"); if (k != -1) { album_path = command_line.Substring(0, k); album_path += "\\.picasa.ini"; inifile = null; found_album_mdata = false; try { inifile = new System.IO.StreamReader(album_path); while ((inidata = inifile.ReadLine()) != null) { if (!found_album_mdata) { if (inidata.IndexOf("[Picasa]") != -1) { found_album_mdata = true; } } else { if (inidata.IndexOf("[") != -1 && inidata.IndexOf("]") != -1) { found_album_mdata = false; continue; } k = inidata.IndexOf("description="); if (k != -1) { album_desc += inidata.Substring("description=".Length); } k = inidata.IndexOf("location="); if (k != -1) { if (album_desc.Length != 0) { album_desc += " "; } album_desc += "(location: " + inidata.Substring("location=".Length) + ")"; } } } inifile.Close(); } catch { } } return album_desc; } public void BuildAlbumList(XmlDocument AlbumList) { album_count = 0; XmlNodeList nodelist = AlbumList.GetElementsByTagName("category"); foreach (XmlNode anode in nodelist) { if (album_count >= album_list_array.Length) { Array.Resize(ref album_list_array, album_list_array.Length + 1024); } if (anode.Attributes.Item(0).Name == "id") { album_list_array[album_count].album_index = Convert.ToInt32(anode.Attributes.Item(0).Value); } album_list_array[album_count].album_name = anode.ChildNodes[0].InnerText; album_count++; } } public void PopulateAlbumListBox() { int i; NewAlbumParentAlbumComboBox.Items.Clear(); AddToAlbumComboBox.Items.Clear(); for (i = 0; i < album_count; i++) { NewAlbumParentAlbumComboBox.Items.Add(album_list_array[i].album_name); AddToAlbumComboBox.Items.Add(album_list_array[i].album_name); } } public bool AlbumAlreadyExists(int selected_index) { int i; string NewAlbumNameFull; if (selected_index == -1) { NewAlbumNameFull = GlobalClass.NewAlbumName; } else { NewAlbumNameFull = album_list_array[selected_index].album_name + " / " + GlobalClass.NewAlbumName; } for (i = 0; i < album_count; i++) { if (album_list_array[i].album_name == NewAlbumNameFull) { return true; } } return false; } private void AccessComboBox_SelectedIndexChanged(object sender, EventArgs e) { Form1Settings.Default.AccessSetting = AccessComboBox.SelectedIndex; Form1Settings.Default.AccessValue = AccessComboBox.Text; Form1Settings.Default.Save(); } public void DetermineParentAlbum(string command_line) { int k, l, i; string possible_parent_album, tempstr; bool found; possible_parent_album = ""; k = command_line.LastIndexOf("\\"); if (k != -1 && k > 1) { k = command_line.LastIndexOf("\\",k-1); if (k != -1 && k > 1) { l = command_line.LastIndexOf("\\", k - 1); possible_parent_album = command_line.Substring(l+1, k - l - 1); found = false; for (i=0;i