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

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

Version 1.4

File size: 18.1 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            byte[] temp_array2 = new byte[2];
55            string tempstr;
56
57            temp_array2[0] = 0xC3;
58            temp_array2[1] = 0xAF;
59            tempstr = Encoding.UTF8.GetString(temp_array2);
60
61
62            this.AddToAlbumGroupbox.Text = WinFormStrings.MFAddToAlbumGroupboxStrRes;
63            this.AddTopAlbumCheckbox.Text = WinFormStrings.MFAddTopAlbumCheckboxStrRes;
64            this.label1.Text = WinFormStrings.MFlabelStrRes;   
65            this.label2.Text = WinFormStrings.MFlabel2StrRes;
66            this.label3.Text = WinFormStrings.MFlabel3StrRes;
67            this.label4.Text = WinFormStrings.MFlabel4StrRes;           
68            this.label5.Text = WinFormStrings.MFlabel5StrRes;           
69            this.NewAlbumGroupBox.Text = WinFormStrings.MFNewAlbumGroupBoxStrRes;
70            this.NewAlbumRadioButton.Text = WinFormStrings.MFNewAlbumRadioButtonStrRes; 
71            this.OptionsButton.Text = WinFormStrings.MFOptionsButtonStrRes;     
72            this.SyncButton.Text = WinFormStrings.MFSyncButtonStrRes;   
73            this.Text = WinFormStrings.MFVersionStrRes;
74            this.AccessComboBox.Items.Add(WinFormStrings.PublicStrRes);
75            this.AccessComboBox.Items.Add(WinFormStrings.PrivateStrRes);
76
77            LabelAdjust AdjustLabelClass = new LabelAdjust();           
78            AdjustLabelClass.AdjustLabelSize(label1);
79            AdjustLabelClass.AdjustLabelSize(label2);
80            AdjustLabelClass.AdjustLabelSize(label3);
81            AdjustLabelClass.AdjustLabelSize(label4);
82            AdjustLabelClass.AdjustRadioButtonSize(NewAlbumRadioButton);
83            AdjustLabelClass.AdjustRadioButtonSize(AddTopAlbumCheckbox);
84            AdjustLabelClass.AdjustButtonSize(SyncButton);
85            AdjustLabelClass.AdjustButtonSize(OptionsButton);
86
87            this.SyncButton.Enabled = false;
88            this.Show();
89            this.Refresh();
90
91            logit.WriteLog("Program Start up");
92
93            //if no arguments then exit
94            if (command_line_args.Length == 0)
95            {
96                Application.Exit();
97            }
98
99            command_length = 0;
100            for (i = 0; i < command_line_args.Length; i++)
101            {
102                command_length += command_line_args[i].Length;
103            }
104
105            if (command_length >= ((32 * 1024) - 512))
106            {
107                MessageBox.Show(WinFormStrings.CommandLineTooLongStrRes, "Picasa2Piwigo");
108                Application.Exit();
109            }
110
111            logit.WriteLog("Reading command line arguments. Length: " + command_length.ToString());
112            AccessComboBox.SelectedIndex = Form1Settings.Default.AccessSetting;
113            this.SyncButton.Enabled = true;
114            //determine album name, if possible
115            AlbumNameTextBox.Text = DetermineAlbumNameFromCommandLine(command_line_args[0]);
116
117            //determine album description, if possible
118            DescriptionTextBox.Text = DetermineAlbumDescription(command_line_args[0]);
119
120            if (FormSettings.Default.PiwigoUrlSetting.Length == 0)
121            {
122                logit.WriteLog("Piwigo URL must be configured.");
123                MessageBox.Show(WinFormStrings.SetUpConnStrRes, "Picasa2Piwigo");
124                this.SyncButton.Enabled = false;
125                return;
126            }
127            if (FormSettings.Default.PicasaContactsLocation.Length == 0)
128            {
129                logit.WriteLog("Picasa contacts not set");               
130            }
131
132            if (FormSettings.Default.DebugEnabled)
133            {
134                if (FormSettings.Default.DebugFileLocation.Length < 4)
135                {
136                    MessageBox.Show( WinFormStrings.LogPathErrStrRes, "Picasa2Piwigo");                       
137                }
138            }   
139
140            logit.WriteLog("Going to log in to piwigo.");
141            //ok to try to login etc
142            LoginBuildAlbumList();
143
144            logit.WriteLog("Populating album list box.");
145            //populate album listbox
146            PopulateAlbumListBox();
147
148            //see if there's a hint in the pathname to an existing parent album
149            DetermineParentAlbum(command_line_args[0]);
150        }
151
152        private void SyncButton_Click(object sender, EventArgs e)
153        {
154            DialogResult result;
155
156            if (NewAlbumRadioButton.Checked)
157            {
158                if (AlbumNameTextBox.Text.Length == 0)
159                {
160                    MessageBox.Show(WinFormStrings.BlankAlbumNameStrRes);
161                    return;
162                }           
163                GlobalClass.NewAlbumName = AlbumNameTextBox.Text.Trim();
164                GlobalClass.NewAlbumDescription = DescriptionTextBox.Text.Trim();
165                if (NewAlbumParentAlbumComboBox.SelectedIndex == -1)
166                {
167                    GlobalClass.NewAlbumNameParentID = -1;
168                }
169                else
170                {
171                    GlobalClass.NewAlbumNameParentID = album_list_array[NewAlbumParentAlbumComboBox.SelectedIndex].album_index;
172                }
173                if (AlbumAlreadyExists(NewAlbumParentAlbumComboBox.SelectedIndex))
174                {
175                    MessageBox.Show(WinFormStrings.AlbumExistsStrRes);
176                    return;
177                }
178
179                Form StatusForm = new StatusForm(constants.COMMAND_NEW_ALBUM, command_line_args);
180                result = StatusForm.ShowDialog();
181                StatusForm.Close();
182
183                //bring window to front
184                this.WindowState = FormWindowState.Minimized;
185                this.Show();
186                this.WindowState = FormWindowState.Normal;
187
188                if (result == DialogResult.No)
189                {
190                    MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes,
191                        MessageBoxButtons.OK, MessageBoxIcon.Error);
192                }
193                else
194                {
195                    MessageBox.Show(WinFormStrings.AddNewCompleteStrRes);                   
196                }
197                Application.Exit();
198            }
199
200            if (AddTopAlbumCheckbox.Checked)
201            {
202                if (AddToAlbumComboBox.SelectedIndex == -1)
203                {
204                    MessageBox.Show(WinFormStrings.NoAlbumSelectedStrRes);
205                    return;
206                }
207                GlobalClass.AddAlbumCategoryID = album_list_array[AddToAlbumComboBox.SelectedIndex].album_index;
208                Form StatusForm = new StatusForm(constants.COMMAND_ADD_TO_ALBUM, command_line_args);
209                result = StatusForm.ShowDialog();
210                StatusForm.Close();
211
212                //bring window to front
213                this.WindowState = FormWindowState.Minimized;
214                this.Show();
215                this.WindowState = FormWindowState.Normal;
216
217                if (result == DialogResult.No)
218                {
219                    MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes,
220                        MessageBoxButtons.OK, MessageBoxIcon.Error);
221                }
222                else
223                {
224                    MessageBox.Show(WinFormStrings.AddToAlbumStrRes);                   
225                }
226                Application.Exit();
227            }
228        }
229
230        private void OptionsButton_Click(object sender, EventArgs e)
231        {
232            DialogResult option_form_result;
233
234            Form OptionsForm = new OptionsForm();
235            OptionsButton.Enabled = false;
236            option_form_result = OptionsForm.ShowDialog();
237            OptionsButton.Enabled = true;
238            if (option_form_result == DialogResult.OK)
239            {
240                if (FormSettings.Default.DebugEnabled)
241                {
242                    if (FormSettings.Default.DebugFileLocation.Length < 4)
243                    {
244                        MessageBox.Show(WinFormStrings.LogPathErrStrRes, "Picasa2Piwigo");
245                    }
246                }           
247
248                if (FormSettings.Default.PiwigoUrlSetting.Length != 0 &&
249                    FormSettings.Default.PicasaContactsLocation.Length != 0)
250                {
251                    this.SyncButton.Enabled = true;
252                    //ok to try to login etc
253                    LoginBuildAlbumList();
254
255                    //populate album listbox
256                    PopulateAlbumListBox();
257                }
258            }
259        }
260
261        private void NewAlbumRadioButton_CheckedChanged(object sender, EventArgs e)
262        {
263            if (NewAlbumRadioButton.Checked)
264            {
265                NewAlbumGroupBox.Enabled = true;               
266                AddToAlbumGroupbox.Enabled = false;
267            }
268        }
269        private void AddTopAlbumCheckbox_CheckedChanged(object sender, EventArgs e)
270        {
271            if (AddTopAlbumCheckbox.Checked)
272            {
273                NewAlbumGroupBox.Enabled = false;               
274                AddToAlbumGroupbox.Enabled = true;
275            }
276        }
277        public void LoginBuildAlbumList()
278        {
279            DialogResult result;
280            Form StatusForm = new StatusForm(constants.COMMAND_START_UP, command_line_args);
281            result = StatusForm.ShowDialog();
282            StatusForm.Close();
283            if (result == DialogResult.No)
284            {
285                MessageBox.Show(GlobalClass.StatusDialogErrorMessage, WinFormStrings.Picasa2PiwigoErrStrRes,
286                    MessageBoxButtons.OK, MessageBoxIcon.Error);
287                this.SyncButton.Enabled = false;
288            }
289            AlbumList = GlobalClass.AlbumList;
290
291            //build album list
292            BuildAlbumList(AlbumList);
293        }
294        public string DetermineAlbumNameFromCommandLine(string command_line)
295        {
296            int k, l, m;
297            string album_name;
298 
299            k = command_line.LastIndexOf("\\");
300            album_name = "";
301            if (k != -1)
302            {
303                l = command_line.LastIndexOf("\\", k - 1);
304                m = command_line.LastIndexOf(":", k - 1);
305                if (l != -1)
306                {
307                    album_name = command_line.Substring(l + 1, k - l - 1);
308                }
309                else if (m != -1)
310                {
311                    album_name = command_line.Substring(m + 1, k - m - 1);
312                }
313            }
314            return album_name;
315        }
316        public string DetermineAlbumDescription(string command_line)
317        {
318            string album_desc, album_path, inidata;
319            int k;
320            System.IO.StreamReader inifile;
321            bool found_album_mdata;
322
323            album_desc = "";
324            k = command_line.LastIndexOf("\\");
325            if (k != -1)
326            {
327                album_path = command_line.Substring(0, k);
328                album_path += "\\.picasa.ini";
329                inifile = null;
330                found_album_mdata = false;
331                try
332                {
333                    inifile = new System.IO.StreamReader(album_path);
334                    while ((inidata = inifile.ReadLine()) != null)
335                    {
336                        if (!found_album_mdata)
337                        {
338                            if (inidata.IndexOf("[Picasa]") != -1)
339                            {
340                                found_album_mdata = true;
341                            }
342                        }
343                        else
344                        {
345                            if (inidata.IndexOf("[") != -1 && 
346                                inidata.IndexOf("]") != -1)
347                            {
348                                found_album_mdata = false;
349                                continue;
350                            }
351                            k = inidata.IndexOf("description=");
352                            if (k != -1)
353                            {
354                                album_desc += inidata.Substring("description=".Length);
355                            }
356                            k = inidata.IndexOf("location=");
357                            if (k != -1)
358                            {
359                                if (album_desc.Length != 0)
360                                {
361                                    album_desc += " ";
362                                }
363                                album_desc += "(location: " + inidata.Substring("location=".Length) + ")";
364                            }
365                        }
366                    }
367                    inifile.Close();
368                }
369                catch
370                {             
371                }
372            }
373            return album_desc;
374        }
375        public void BuildAlbumList(XmlDocument AlbumList)
376        {           
377            album_count = 0;
378            XmlNodeList nodelist = AlbumList.GetElementsByTagName("category");
379            foreach (XmlNode anode in nodelist)
380            {
381                if (album_count >= album_list_array.Length)
382                {
383                    Array.Resize(ref album_list_array, album_list_array.Length + 1024);
384                }
385                if (anode.Attributes.Item(0).Name == "id")
386                {
387                    album_list_array[album_count].album_index = Convert.ToInt32(anode.Attributes.Item(0).Value);
388                }
389                album_list_array[album_count].album_name = anode.ChildNodes[0].InnerText;
390                album_count++;
391            }
392        }
393        public void PopulateAlbumListBox()
394        {
395            int i;
396
397            NewAlbumParentAlbumComboBox.Items.Clear();
398            AddToAlbumComboBox.Items.Clear();
399            for (i = 0; i < album_count; i++)
400            {
401                NewAlbumParentAlbumComboBox.Items.Add(album_list_array[i].album_name);               
402                AddToAlbumComboBox.Items.Add(album_list_array[i].album_name);
403            }
404        }
405        public bool AlbumAlreadyExists(int selected_index)
406        {
407            int i;
408            string NewAlbumNameFull;
409
410            if (selected_index == -1)
411            {
412                NewAlbumNameFull = GlobalClass.NewAlbumName;
413            }
414            else
415            {
416                NewAlbumNameFull = album_list_array[selected_index].album_name + " / " + GlobalClass.NewAlbumName;
417            }
418            for (i = 0; i < album_count; i++)
419            {
420                if (album_list_array[i].album_name == NewAlbumNameFull)
421                {
422                    return true;
423                }
424            }
425            return false;
426        }
427
428        private void AccessComboBox_SelectedIndexChanged(object sender, EventArgs e)
429        {
430            Form1Settings.Default.AccessSetting = AccessComboBox.SelectedIndex;
431            Form1Settings.Default.AccessValue = AccessComboBox.Text;
432            Form1Settings.Default.Save();
433        }
434        public void DetermineParentAlbum(string command_line)
435        {
436            int k, l, i;
437            string possible_parent_album, tempstr;
438            bool found;
439
440            possible_parent_album = "";
441            k = command_line.LastIndexOf("\\");
442            if (k != -1 && k > 1)
443            {
444                k = command_line.LastIndexOf("\\",k-1);
445                if (k != -1 && k > 1)
446                {
447                    l = command_line.LastIndexOf("\\", k - 1);
448                    possible_parent_album = command_line.Substring(l+1, k - l - 1);
449                    found = false;
450                    for (i=0;i<NewAlbumParentAlbumComboBox.Items.Count;i++)
451                    {
452                        tempstr = NewAlbumParentAlbumComboBox.Items[i].ToString();
453                        k = tempstr.LastIndexOf(" / ");
454                        if (k != -1)
455                        {
456                            tempstr = tempstr.Substring(k+3);
457                        }
458                        if (tempstr == possible_parent_album)
459                        {
460                            found = true;
461                            break;
462                        }
463                    }
464                    if (found)
465                    {
466                        NewAlbumParentAlbumComboBox.SelectedIndex = i;
467                        AddToAlbumComboBox.SelectedIndex = i;
468                    }
469                }
470            }
471        }
472    }
473}
Note: See TracBrowser for help on using the repository browser.