source: extensions/Picasa2Piwigo/Picasa2Piwigo/MainForm.cs @ 21244

Last change on this file since 21244 was 21241, checked in by kenl, 11 years ago
File size: 16.3 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;
12using System.Globalization;
13using System.Threading;
14
15namespace Picasa2Piwigo
16{
17    using Picasa2PiwigoConstants;
18    using Picasa2PiwigoCommonFunctions;
19
20    public partial class MainForm : Form
21    {       
22        string[] command_line_args;
23        XmlDocument AlbumList = new XmlDocument();
24        public struct AlbumListStruct
25        {
26            public string album_name;
27            public string full_album_path;
28            public int album_index;
29            public string album_parent_chain;
30        }
31        AlbumListStruct[] album_list_array = new AlbumListStruct[1024];
32        int album_count;
33
34        public MainForm(string[] args)
35        {
36            CultureInfo ci;
37            ci = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
38            Thread.CurrentThread.CurrentUICulture = ci;
39            Thread.CurrentThread.CurrentCulture = ci;
40            if (Picasa2PiwigoConstants.constants.DEBUG_LOCALE)
41            {               
42#pragma warning disable 0162
43                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Picasa2PiwigoConstants.constants.SET_LOCALE);
44#pragma warning restore 0162
45            }
46            InitializeComponent();
47            command_line_args = args;
48        }
49
50        private void Form1_Load(object sender, EventArgs e)
51        {
52            int command_length, i;
53           
54            this.AddToAlbumGroupbox.Text = WinFormStrings.MFAddToAlbumGroupboxStrRes;
55            this.AddTopAlbumCheckbox.Text = WinFormStrings.MFAddTopAlbumCheckboxStrRes;
56            this.label1.Text = WinFormStrings.MFlabelStrRes;   
57            this.label2.Text = WinFormStrings.MFlabel2StrRes;
58            this.label3.Text = WinFormStrings.MFlabel3StrRes;
59            this.label4.Text = WinFormStrings.MFlabel4StrRes;           
60            this.label5.Text = WinFormStrings.MFlabel5StrRes;           
61            this.NewAlbumGroupBox.Text = WinFormStrings.MFNewAlbumGroupBoxStrRes;
62            this.NewAlbumRadioButton.Text = WinFormStrings.MFNewAlbumRadioButtonStrRes; 
63            this.OptionsButton.Text = WinFormStrings.MFOptionsButtonStrRes;     
64            this.SyncButton.Text = WinFormStrings.MFSyncButtonStrRes;   
65            this.Text = WinFormStrings.MFVersionStrRes;
66            this.AccessComboBox.Items.Add(WinFormStrings.PublicStrRes);
67            this.AccessComboBox.Items.Add(WinFormStrings.PrivateStrRes);
68
69            LabelAdjust AdjustLabelClass = new LabelAdjust();           
70            AdjustLabelClass.AdjustLabelSize(label1);
71            AdjustLabelClass.AdjustLabelSize(label2);
72            AdjustLabelClass.AdjustLabelSize(label3);
73            AdjustLabelClass.AdjustLabelSize(label4);
74            AdjustLabelClass.AdjustRadioButtonSize(NewAlbumRadioButton);
75            AdjustLabelClass.AdjustRadioButtonSize(AddTopAlbumCheckbox);
76            AdjustLabelClass.AdjustButtonSize(SyncButton);
77            AdjustLabelClass.AdjustButtonSize(OptionsButton);
78
79            this.SyncButton.Enabled = false;
80            this.Show();
81            this.Refresh();
82
83            //if no arguments then exit
84            if (command_line_args.Length == 0)
85            {
86                Application.Exit();
87            }
88
89            command_length = 0;
90            for (i = 0; i < command_line_args.Length; i++)
91            {
92                command_length += command_line_args[i].Length;
93            }
94
95            if (command_length >= ((32 * 1024) - 512))
96            {
97                MessageBox.Show(WinFormStrings.CommandLineTooLongStrRes, "Picasa2Piwigo");
98                Application.Exit();
99            }
100
101            AccessComboBox.SelectedIndex = Form1Settings.Default.AccessSetting;
102            this.SyncButton.Enabled = true;
103            //determine album name, if possible
104            AlbumNameTextBox.Text = DetermineAlbumNameFromCommandLine(command_line_args[0]);
105
106            //determine album description, if possible
107            DescriptionTextBox.Text = DetermineAlbumDescription(command_line_args[0]);
108
109            if (FormSettings.Default.PiwigoUrlSetting.Length == 0 ||
110                FormSettings.Default.PicasaContactsLocation.Length == 0)
111            {
112                MessageBox.Show(WinFormStrings.SetUpConnStrRes, "Picasa2Piwigo");
113                return;
114            }
115
116            //ok to try to login etc
117            LoginBuildAlbumList();
118
119            //populate album listbox
120            PopulateAlbumListBox();
121
122            //see if there's a hint in the pathname to an existing parent album
123            DetermineParentAlbum(command_line_args[0]);
124        }
125
126        private void SyncButton_Click(object sender, EventArgs e)
127        {
128            DialogResult result;
129
130            if (NewAlbumRadioButton.Checked)
131            {
132                if (AlbumNameTextBox.Text.Length == 0)
133                {
134                    MessageBox.Show(WinFormStrings.BlankAlbumNameStrRes);
135                    return;
136                }           
137                GlobalClass.NewAlbumName = AlbumNameTextBox.Text.Trim();
138                GlobalClass.NewAlbumDescription = DescriptionTextBox.Text.Trim();
139                if (NewAlbumParentAlbumComboBox.SelectedIndex == -1)
140                {
141                    GlobalClass.NewAlbumNameParentID = -1;
142                }
143                else
144                {
145                    GlobalClass.NewAlbumNameParentID = album_list_array[NewAlbumParentAlbumComboBox.SelectedIndex].album_index;
146                }
147                if (AlbumAlreadyExists(NewAlbumParentAlbumComboBox.SelectedIndex))
148                {
149                    MessageBox.Show(WinFormStrings.AlbumExistsStrRes);
150                    return;
151                }
152
153                Form StatusForm = new StatusForm(constants.COMMAND_NEW_ALBUM, command_line_args);
154                result = StatusForm.ShowDialog();
155                StatusForm.Close();
156                if (result == DialogResult.No)
157                {
158                    MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes,
159                        MessageBoxButtons.OK, MessageBoxIcon.Error);
160                }
161                else
162                {
163                    MessageBox.Show(WinFormStrings.AddNewCompleteStrRes);                   
164                }
165                Application.Exit();
166            }
167
168            if (AddTopAlbumCheckbox.Checked)
169            {
170                if (AddToAlbumComboBox.SelectedIndex == -1)
171                {
172                    MessageBox.Show(WinFormStrings.NoAlbumSelectedStrRes);
173                    return;
174                }
175                GlobalClass.AddAlbumCategoryID = album_list_array[AddToAlbumComboBox.SelectedIndex].album_index;
176                Form StatusForm = new StatusForm(constants.COMMAND_ADD_TO_ALBUM, command_line_args);
177                result = StatusForm.ShowDialog();
178                StatusForm.Close();
179                if (result == DialogResult.No)
180                {
181                    MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes,
182                        MessageBoxButtons.OK, MessageBoxIcon.Error);
183                }
184                else
185                {
186                    MessageBox.Show(WinFormStrings.AddToAlbumStrRes);                   
187                }
188                Application.Exit();
189            }
190        }
191
192        private void OptionsButton_Click(object sender, EventArgs e)
193        {
194            DialogResult option_form_result;
195
196            Form OptionsForm = new OptionsForm();
197            OptionsButton.Enabled = false;
198            option_form_result = OptionsForm.ShowDialog();
199            OptionsButton.Enabled = true;
200            if (option_form_result == DialogResult.OK)
201            {
202                if (FormSettings.Default.PiwigoUrlSetting.Length != 0 &&
203                    FormSettings.Default.PicasaContactsLocation.Length != 0)
204                {
205                    this.SyncButton.Enabled = true;
206                    //ok to try to login etc
207                    LoginBuildAlbumList();
208
209                    //populate album listbox
210                    PopulateAlbumListBox();
211                }
212            }
213        }
214
215        private void NewAlbumRadioButton_CheckedChanged(object sender, EventArgs e)
216        {
217            if (NewAlbumRadioButton.Checked)
218            {
219                NewAlbumGroupBox.Enabled = true;               
220                AddToAlbumGroupbox.Enabled = false;
221            }
222        }
223        private void AddTopAlbumCheckbox_CheckedChanged(object sender, EventArgs e)
224        {
225            if (AddTopAlbumCheckbox.Checked)
226            {
227                NewAlbumGroupBox.Enabled = false;               
228                AddToAlbumGroupbox.Enabled = true;
229            }
230        }
231        public void LoginBuildAlbumList()
232        {
233            DialogResult result;
234            Form StatusForm = new StatusForm(constants.COMMAND_START_UP, command_line_args);
235            result = StatusForm.ShowDialog();
236            StatusForm.Close();
237            if (result == DialogResult.No)
238            {
239                MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes,
240                    MessageBoxButtons.OK, MessageBoxIcon.Error);
241            }
242            AlbumList = GlobalClass.AlbumList;
243
244            //build album list
245            BuildAlbumList(AlbumList);
246        }
247        public string DetermineAlbumNameFromCommandLine(string command_line)
248        {
249            int k, l, m;
250            string album_name;
251 
252            k = command_line.LastIndexOf("\\");
253            album_name = "";
254            if (k != -1)
255            {
256                l = command_line.LastIndexOf("\\", k - 1);
257                m = command_line.LastIndexOf(":", k - 1);
258                if (l != -1)
259                {
260                    album_name = command_line.Substring(l + 1, k - l - 1);
261                }
262                else if (m != -1)
263                {
264                    album_name = command_line.Substring(m + 1, k - m - 1);
265                }
266            }
267            return album_name;
268        }
269        public string DetermineAlbumDescription(string command_line)
270        {
271            string album_desc, album_path, inidata;
272            int k;
273            System.IO.StreamReader inifile;
274            bool found_album_mdata;
275
276            album_desc = "";
277            k = command_line.LastIndexOf("\\");
278            if (k != -1)
279            {
280                album_path = command_line.Substring(0, k);
281                album_path += "\\.picasa.ini";
282                inifile = null;
283                found_album_mdata = false;
284                try
285                {
286                    inifile = new System.IO.StreamReader(album_path);
287                    while ((inidata = inifile.ReadLine()) != null)
288                    {
289                        if (!found_album_mdata)
290                        {
291                            if (inidata.IndexOf("[Picasa]") != -1)
292                            {
293                                found_album_mdata = true;
294                            }
295                        }
296                        else
297                        {
298                            if (inidata.IndexOf("[") != -1 && 
299                                inidata.IndexOf("]") != -1)
300                            {
301                                found_album_mdata = false;
302                                continue;
303                            }
304                            k = inidata.IndexOf("description=");
305                            if (k != -1)
306                            {
307                                album_desc += inidata.Substring("description=".Length);
308                            }
309                            k = inidata.IndexOf("location=");
310                            if (k != -1)
311                            {
312                                if (album_desc.Length != 0)
313                                {
314                                    album_desc += " ";
315                                }
316                                album_desc += "(location: " + inidata.Substring("location=".Length) + ")";
317                            }
318                        }
319                    }
320                    inifile.Close();
321                }
322                catch
323                {             
324                }
325            }
326            return album_desc;
327        }
328        public void BuildAlbumList(XmlDocument AlbumList)
329        {           
330            album_count = 0;
331            XmlNodeList nodelist = AlbumList.GetElementsByTagName("category");
332            foreach (XmlNode anode in nodelist)
333            {
334                if (album_count >= album_list_array.Length)
335                {
336                    Array.Resize(ref album_list_array, album_list_array.Length + 1024);
337                }
338                if (anode.Attributes.Item(0).Name == "id")
339                {
340                    album_list_array[album_count].album_index = Convert.ToInt32(anode.Attributes.Item(0).Value);
341                }
342                album_list_array[album_count].album_name = anode.ChildNodes[0].InnerText;
343                album_count++;
344            }
345        }
346        public void PopulateAlbumListBox()
347        {
348            int i;
349
350            NewAlbumParentAlbumComboBox.Items.Clear();
351            AddToAlbumComboBox.Items.Clear();
352            for (i = 0; i < album_count; i++)
353            {
354                NewAlbumParentAlbumComboBox.Items.Add(album_list_array[i].album_name);               
355                AddToAlbumComboBox.Items.Add(album_list_array[i].album_name);
356            }
357        }
358        public bool AlbumAlreadyExists(int selected_index)
359        {
360            int i;
361            string NewAlbumNameFull;
362
363            if (selected_index == -1)
364            {
365                NewAlbumNameFull = GlobalClass.NewAlbumName;
366            }
367            else
368            {
369                NewAlbumNameFull = album_list_array[selected_index].album_name + " / " + GlobalClass.NewAlbumName;
370            }
371            for (i = 0; i < album_count; i++)
372            {
373                if (album_list_array[i].album_name == NewAlbumNameFull)
374                {
375                    return true;
376                }
377            }
378            return false;
379        }
380
381        private void AccessComboBox_SelectedIndexChanged(object sender, EventArgs e)
382        {
383            Form1Settings.Default.AccessSetting = AccessComboBox.SelectedIndex;
384            Form1Settings.Default.AccessValue = AccessComboBox.Text;
385            Form1Settings.Default.Save();
386        }
387        public void DetermineParentAlbum(string command_line)
388        {
389            int k, l, i;
390            string possible_parent_album, tempstr;
391            bool found;
392
393            possible_parent_album = "";
394            k = command_line.LastIndexOf("\\");
395            if (k != -1 && k > 1)
396            {
397                k = command_line.LastIndexOf("\\",k-1);
398                if (k != -1 && k > 1)
399                {
400                    l = command_line.LastIndexOf("\\", k - 1);
401                    possible_parent_album = command_line.Substring(l+1, k - l - 1);
402                    found = false;
403                    for (i=0;i<NewAlbumParentAlbumComboBox.Items.Count;i++)
404                    {
405                        tempstr = NewAlbumParentAlbumComboBox.Items[i].ToString();
406                        k = tempstr.LastIndexOf(" / ");
407                        if (k != -1)
408                        {
409                            tempstr = tempstr.Substring(k+3);
410                        }
411                        if (tempstr == possible_parent_album)
412                        {
413                            found = true;
414                            break;
415                        }
416                    }
417                    if (found)
418                    {
419                        NewAlbumParentAlbumComboBox.SelectedIndex = i;
420                        AddToAlbumComboBox.SelectedIndex = i;
421                    }
422                }
423            }
424        }
425    }
426}
Note: See TracBrowser for help on using the repository browser.