source: extensions/Picasa2Piwigo/Picasa2Piwigo/Form1.cs @ 32008

Last change on this file since 32008 was 30536, checked in by kenl, 10 years ago

Version 1.4

File size: 9.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.IO;
10using System.Net;
11using System.Xml;
12
13namespace Picasa2Piwigo
14{
15    using Picasa2PiwigoConstants;
16    public partial class Form1 : Form
17    {       
18        string[] command_line_args;
19        XmlDocument AlbumList = new XmlDocument();
20        public struct AlbumListStruct
21        {
22            public string album_name;
23            public string full_album_path;
24            public int album_index;
25            public string album_parent_chain;
26        }
27        AlbumListStruct[] album_list_array = new AlbumListStruct[1024];
28        int album_count;
29
30        public Form1(string[] args)
31        {
32            InitializeComponent();
33            command_line_args = args;
34        }
35
36        private void Form1_Load(object sender, EventArgs e)
37        {
38           
39            this.SyncButton.Enabled = false;
40            this.Show();
41            this.Refresh();
42
43            //if no arguments then exit
44            if (command_line_args.Length == 0)
45            {
46                return;
47            }
48            AccessComboBox.SelectedIndex = Form1Settings.Default.AccessSetting;
49            this.SyncButton.Enabled = true;
50            //determine album name, if possible
51            AlbumNameTextBox.Text = DetermineAlbumNameFromCommandLine(command_line_args[0]);
52
53            if (FormSettings.Default.PiwigoUrlSetting.Length == 0 ||
54                FormSettings.Default.PicasaContactsLocation.Length == 0)
55            {
56                MessageBox.Show("Set up your Piwigo Server Connection using the Options button.", "Picasa2Piwigo");
57                return;
58            }
59
60            //ok to try to login etc
61            LoginBuildAlbumList();
62
63            //populate album listbox
64            PopulateAlbumListBox();
65        }
66
67        private void SyncButton_Click(object sender, EventArgs e)
68        {
69            DialogResult result;
70
71            if (NewAlbumRadioButton.Checked)
72            {
73                if (AlbumNameTextBox.Text.Length == 0)
74                {
75                    MessageBox.Show("Error:  Blank Album Name");
76                    return;
77                }           
78                GlobalClass.NewAlbumName = AlbumNameTextBox.Text;
79                if (NewAlbumParentAlbumComboBox.SelectedIndex == -1)
80                {
81                    GlobalClass.NewAlbumNameParentID = -1;
82                }
83                else
84                {
85                    GlobalClass.NewAlbumNameParentID = album_list_array[NewAlbumParentAlbumComboBox.SelectedIndex].album_index;
86                }
87                if (AlbumAlreadyExists(NewAlbumParentAlbumComboBox.SelectedIndex))
88                {
89                    MessageBox.Show("Album already exists in Piwigo.  Please choose a different album name.");
90                    return;
91                }
92
93                Form StatusForm = new StatusForm(constants.COMMAND_NEW_ALBUM, command_line_args);
94                result = StatusForm.ShowDialog();
95                StatusForm.Close();
96                if (result == DialogResult.No)
97                {
98                    MessageBox.Show(GlobalClass.StatusDialogErrorMessage, "Picasa2Piwigo Error",
99                        MessageBoxButtons.OK, MessageBoxIcon.Error);
100                }
101                else
102                {
103                    MessageBox.Show("Add to new album complete.");
104                    Application.Exit();
105                }
106            }
107
108            if (AddTopAlbumCheckbox.Checked)
109            {
110                if (AddToAlbumComboBox.SelectedIndex == -1)
111                {
112                    MessageBox.Show("Error:  No Album Selected");
113                    return;
114                }
115                GlobalClass.AddAlbumCategoryID = album_list_array[AddToAlbumComboBox.SelectedIndex].album_index;
116                Form StatusForm = new StatusForm(constants.COMMAND_ADD_TO_ALBUM, command_line_args);
117                result = StatusForm.ShowDialog();
118                StatusForm.Close();
119                if (result == DialogResult.No)
120                {
121                    MessageBox.Show(GlobalClass.StatusDialogErrorMessage, "Picasa2Piwigo Error",
122                        MessageBoxButtons.OK, MessageBoxIcon.Error);
123                }
124                else
125                {
126                    MessageBox.Show("Add to album complete.");
127                    Application.Exit();
128                }
129            }
130
131        }
132
133        private void OptionsButton_Click(object sender, EventArgs e)
134        {
135            DialogResult option_form_result;
136
137            Form OptionsForm = new OptionsForm();
138            OptionsButton.Enabled = false;
139            option_form_result = OptionsForm.ShowDialog();
140            OptionsButton.Enabled = true;
141            if (option_form_result == DialogResult.OK)
142            {
143                if (FormSettings.Default.PiwigoUrlSetting.Length != 0 &&
144                    FormSettings.Default.PicasaContactsLocation.Length != 0)
145                {
146                    this.SyncButton.Enabled = true;
147                    //ok to try to login etc
148                    LoginBuildAlbumList();
149
150                    //populate album listbox
151                    PopulateAlbumListBox();
152                }
153            }
154        }
155
156        private void NewAlbumRadioButton_CheckedChanged(object sender, EventArgs e)
157        {
158            if (NewAlbumRadioButton.Checked)
159            {
160                NewAlbumGroupBox.Enabled = true;               
161                AddToAlbumGroupbox.Enabled = false;
162            }
163        }
164        private void AddTopAlbumCheckbox_CheckedChanged(object sender, EventArgs e)
165        {
166            if (AddTopAlbumCheckbox.Checked)
167            {
168                NewAlbumGroupBox.Enabled = false;               
169                AddToAlbumGroupbox.Enabled = true;
170            }
171        }
172        public void LoginBuildAlbumList()
173        {
174            DialogResult result;
175            Form StatusForm = new StatusForm(constants.COMMAND_START_UP, command_line_args);
176            result = StatusForm.ShowDialog();
177            StatusForm.Close();
178            if (result == DialogResult.No)
179            {
180                MessageBox.Show(GlobalClass.StatusDialogErrorMessage, "Picasa2Piwigo Error",
181                    MessageBoxButtons.OK, MessageBoxIcon.Error);
182            }
183            AlbumList = GlobalClass.AlbumList;
184
185            //build album list
186            BuildAlbumList(AlbumList);
187        }
188        public string DetermineAlbumNameFromCommandLine(string command_line)
189        {
190            int k, l, m;
191            string album_name;
192 
193            k = command_line.LastIndexOf("\\");
194            album_name = "";
195            if (k != -1)
196            {
197                l = command_line.LastIndexOf("\\", k - 1);
198                m = command_line.LastIndexOf(":", k - 1);
199                if (l != -1)
200                {
201                    album_name = command_line.Substring(l + 1, k - l - 1);
202                }
203                else if (m != -1)
204                {
205                    album_name = command_line.Substring(m + 1, k - m - 1);
206                }
207            }
208            return album_name;
209        }
210        public void BuildAlbumList(XmlDocument AlbumList)
211        {           
212            album_count = 0;
213            XmlNodeList nodelist = AlbumList.GetElementsByTagName("category");
214            foreach (XmlNode anode in nodelist)
215            {
216                if (album_count >= album_list_array.Length)
217                {
218                    Array.Resize(ref album_list_array, album_list_array.Length + 1024);
219                }
220                if (anode.Attributes.Item(0).Name == "id")
221                {
222                    album_list_array[album_count].album_index = Convert.ToInt32(anode.Attributes.Item(0).Value);
223                }
224                album_list_array[album_count].album_name = anode.ChildNodes[0].InnerText;
225                album_count++;
226            }
227        }
228        public void PopulateAlbumListBox()
229        {
230            int i;
231
232            for (i = 0; i < album_count; i++)
233            {
234                NewAlbumParentAlbumComboBox.Items.Add(album_list_array[i].album_name);               
235                AddToAlbumComboBox.Items.Add(album_list_array[i].album_name);
236            }
237        }
238        public bool AlbumAlreadyExists(int selected_index)
239        {
240            int i;
241            string NewAlbumNameFull;
242
243            if (selected_index == -1)
244            {
245                NewAlbumNameFull = GlobalClass.NewAlbumName;
246            }
247            else
248            {
249                NewAlbumNameFull = album_list_array[selected_index].album_name + " / " + GlobalClass.NewAlbumName;
250            }
251            for (i = 0; i < album_count; i++)
252            {
253                if (album_list_array[i].album_name == NewAlbumNameFull)
254                {
255                    return true;
256                }
257            }
258            return false;
259        }
260
261        private void AccessComboBox_SelectedIndexChanged(object sender, EventArgs e)
262        {
263            Form1Settings.Default.AccessSetting = AccessComboBox.SelectedIndex;
264            Form1Settings.Default.AccessValue = AccessComboBox.Text;
265            Form1Settings.Default.Save();
266        }
267
268    }
269}
Note: See TracBrowser for help on using the repository browser.