?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /*********************************************************************
2 *
3 * Main Dialog for MPFS21
4 *
5 *********************************************************************
6 * FileName: MPFS21Form.cs
7 * Dependencies: Microsoft .NET Framework 2.0
8 * Processor: x86
9 * Complier: Microsoft Visual C# 2008 Express Edition
10 * Company: Microchip Technology, Inc.
11 *
12 * Software License Agreement
13 *
14 * This software is owned by Microchip Technology Inc. ("Microchip")
15 * and is supplied to you for use exclusively as described in the
16 * associated software agreement. This software is protected by
17 * software and other intellectual property laws. Any use in
18 * violation of the software license may subject the user to criminal
19 * sanctions as well as civil liability. Copyright 2008 Microchip
20 * Technology Inc. All rights reserved.
21 *
22 *
23 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
24 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
25 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
26 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
27 * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
28 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
29 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
30 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
31 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
32 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
33 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
34 *
35 *
36 * Author Date Comment
37 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38 * Elliott Wood 4/17/2008 Original
39 ********************************************************************/
40 using System;
41 using System.Collections.Generic;
42 using System.ComponentModel;
43 using System.Drawing;
44 using System.Text;
45 using System.Windows.Forms;
46 using System.Threading;
47 using Microchip;
48 using System.IO;
49 using System.Net;
50  
51 namespace MPFS21
52 {
53 public partial class MPFS21Form : Form
54 {
55  
56 #region Fields
57 private int groupBox1Height, groupBox2Height, groupBox3Height, groupBox4Height;
58 private String strVersion, strBuildDate, strWebPageDestpathMDD;
59 private bool lockDisplay = true;
60 private MPFS2WebClient web;
61 private bool generationResult;
62 private List<string> generateLog;
63 #endregion
64  
65 public MPFS21Form()
66 {
67 InitializeComponent();
68 groupBox1Height = groupBox1.Height;
69 groupBox2Height = groupBox2.Height;
70 groupBox3Height = groupBox3.Height;
71 groupBox4Height = groupBox4.Height;
72 }
73  
74 #region Form Load and Unload Functions
75 /// <summary>
76 /// Configures the form on load
77 /// </summary>
78 /// <param name="sender"></param>
79 /// <param name="e"></param>
80 private void MPFS21Form_Load(object sender, EventArgs e)
81 {
82 // Load version and build date
83 Version ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
84 strVersion = ver.Major + "." + ver.Minor + "." + ver.Build + "." + ver.Revision;
85 strBuildDate = new DateTime(2000, 1, 1).AddDays(ver.Build).ToString("MMMM d, yyyy");
86 lblAbout.Text = strBuildDate + "\nVersion " + strVersion;
87 lblAbout.Margin = new Padding(190 - lblAbout.Width, 3, 0, 0);
88  
89 // Marshal in a few config things
90 if (Settings.Default.StartWithDirectory)
91 radStartDir.Checked = true;
92 else
93 radStartImg.Checked = true;
94 switch (Settings.Default.OutputFormat)
95 {
96 case 1:
97 radOutputC18.Checked = true;
98 break;
99 case 2:
100 radOutputASM30.Checked = true;
101 break;
102 case 3:
103 radOutputMDD.Checked = true;
104 break;
105 default:
106 radOutputBIN.Checked = true;
107 break;
108 }
109  
110 txtSourceDir.Text = Settings.Default.SourceDirectory;
111 txtSourceImage.Text = Settings.Default.SourceImage;
112 txtImageName.Text = Settings.Default.ImageName;
113 txtProjectDir.Text = Settings.Default.ProjectDirectory;
114  
115 lockDisplay = false;
116 CorrectDisplay(sender, e);
117 }
118  
119 /// <summary>
120 /// Saves application settings before exiting
121 /// </summary>
122 /// <param name="sender"></param>
123 /// <param name="e"></param>
124 private void MPFS21Form_FormClosing(object sender, FormClosingEventArgs e)
125 {
126 // Marshal out a few config things
127 if (radStartDir.Checked)
128 Settings.Default.StartWithDirectory = true;
129 else
130 Settings.Default.StartWithDirectory = false;
131 if (radOutputBIN.Checked)
132 Settings.Default.OutputFormat = 0;
133 else if (radOutputC18.Checked)
134 Settings.Default.OutputFormat = 1;
135 else if (radOutputASM30.Checked)
136 Settings.Default.OutputFormat = 2;
137 else if (radOutputMDD.Checked)
138 Settings.Default.OutputFormat = 3;
139  
140 Settings.Default.SourceDirectory = txtSourceDir.Text;
141 Settings.Default.SourceImage = txtSourceImage.Text;
142 Settings.Default.ImageName = txtImageName.Text;
143 Settings.Default.ProjectDirectory = txtProjectDir.Text;
144  
145 // Save application settings
146 global::MPFS21.Settings.Default.Save();
147 }
148 #endregion
149  
150 #region Display Option Manager
151 /// <summary>
152 /// Makes sure the proper options are visible at all times
153 /// </summary>
154 /// <param name="sender"></param>
155 /// <param name="e"></param>
156 private void CorrectDisplay(object sender, EventArgs e)
157 {
158 if (lockDisplay)
159 return;
160  
161 lockDisplay = true;
162 // Correct the display settings as per MDD requirement
163  
164 if (radOutputMDD.Checked)
165 {
166 // Disable Pre built MPFS image option when MDD is selected
167 radStartImg.Enabled = false;
168 // Disable advanced setting option when MDD is selected
169 btnAdvanced.Enabled = false;
170 // No file name option when MDD is selected
171 txtImageName.Visible = false;
172 label10.Visible = false;
173 lblType.Visible = false;
174 }
175 else
176 {
177 radStartImg.Enabled = true;
178 btnAdvanced.Enabled = true;
179  
180 txtImageName.Visible = true;
181 label10.Visible = true;
182 lblType.Visible = true;
183 }
184  
185 // Properly configure the output extension
186 if (radOutputBIN.Checked)
187 lblType.Text = "(*.bin)";
188 else if (radOutputC18.Checked)
189 lblType.Text = "(*.c)";
190 else if (radOutputASM30.Checked)
191 lblType.Text = "(*.s)";
192 // else if (radOutputMDD.Checked)
193 // lblType.Text = "(*.c)";
194  
195 // Configure upload settings enabled/disbled
196 if (chkUpload.Checked || radStartImg.Checked)
197 {
198 txtUploadDestination.Enabled = true;
199 btnUploadSettings.Enabled = true;
200 }
201 else
202 {
203 txtUploadDestination.Enabled = false;
204 btnUploadSettings.Enabled = false;
205 }
206  
207 // Show correct input label
208 if (radStartImg.Checked)
209 lblInput.Text = "Source Image:";
210 else
211 lblInput.Text = "Source Directory:";
212  
213 // Show the correct text on the button
214 if (radStartImg.Checked)
215 btnGenerate.Text = "Upload";
216 else if (radOutputBIN.Checked && chkUpload.Checked)
217 btnGenerate.Text = "Generate and Upload";
218 else
219 btnGenerate.Text = "Generate";
220  
221 // Show the correct upload path option
222 txtUploadDestination.Text = GetProtocol() + "://" + Settings.Default.UploadUser +
223 "@" + Settings.Default.UploadAddress + "/";
224 txtUploadDestination.Text += " ( ==> to modify ==> )";
225  
226 // Show only the appropriate steps
227 if (radStartImg.Checked)
228 {
229 txtSourceImage.Visible = true;
230 txtSourceDir.Visible = false;
231 chkUpload.Visible = false;
232 this.ToggleSteps(0, 0, groupBox4Height);
233 }
234 else if (radOutputBIN.Checked)
235 {
236 txtSourceImage.Visible = false;
237 txtSourceDir.Visible = true;
238 chkUpload.Visible = true;
239 this.ToggleSteps(groupBox2Height, groupBox3Height, groupBox4Height);
240 }
241 else
242 {
243 txtSourceImage.Visible = false;
244 txtSourceDir.Visible = true;
245 chkUpload.Visible = true;
246 this.ToggleSteps(groupBox2Height, groupBox3Height, 0);
247 }
248  
249 lockDisplay = false;
250 }
251 #endregion
252  
253 #region Animation Functions
254 /// <summary>
255 /// Hides or shows the various steps
256 /// </summary>
257 /// <param name="g2Target">Target height for groupBox2</param>
258 /// <param name="g3Target">Target height for groupBox3</param>
259 /// <param name="g4Target">Target height for groupBox4</param>
260 private void ToggleSteps(int g2Target, int g3Target, int g4Target)
261 {
262 // Step towards the target height slowly
263 while(groupBox2.Height != g2Target ||
264 groupBox3.Height != g3Target ||
265 groupBox4.Height != g4Target)
266 {
267 StepToTarget(groupBox2, g2Target);
268 StepToTarget(groupBox3, g3Target);
269 StepToTarget(groupBox4, g4Target);
270 this.Refresh();
271 Thread.Sleep(20);
272 }
273 }
274  
275 /// <summary>
276 /// Steps a GroupBox towards its target size
277 /// </summary>
278 /// <param name="obj">The GroupBox to step</param>
279 /// <param name="target">Target size for the object</param>
280 private void StepToTarget(GroupBox obj, int target)
281 {
282 if (obj.Height == target)
283 return;
284  
285 int step = (int)((obj.Height - target) * 0.25);
286 if (obj.Height > target)
287 step += 1;
288 else
289 step -= 1;
290  
291 obj.Height -= step;
292 this.Height -= step;
293 }
294 #endregion
295  
296 /// <summary>
297 /// Handles the generation when clicked.
298 /// </summary>
299 /// <param name="sender"></param>
300 /// <param name="e"></param>
301 private void btnGenerate_Click(object sender, EventArgs e)
302 {
303  
304 // Disable the button
305 btnGenerate.Enabled = false;
306  
307 // Build an image
308 if(radStartDir.Checked)
309 {
310 //// Make sure the project directory is correct
311 //if(txtProjectDir.Text.Contains(txtSourceDir.Text))
312 //{
313 // generationResult = false;
314 // generateLog = new List<string>();
315 // generateLog.Add("ERROR: The project directory is located in the source " +
316 // "directory. The generator cannot run if the image is to be placed " +
317 // "in the source directory. Please select the base MPLAB project " +
318 // "directory before continuing.");
319 // generationResult = false;
320 // ShowResultDialog("The image could not be built.");
321 // return;
322 //}
323  
324 // Set up an appropriate builder
325 MPFSBuilder builder;
326 if(Settings.Default.OutputVersion == 2)
327 {
328 builder = new MPFS2Builder(txtProjectDir.Text, txtImageName.Text);
329 ((MPFS2Builder)builder).DynamicTypes = Settings.Default.DynamicFiles;
330 ((MPFS2Builder)builder).NonGZipTypes = Settings.Default.NoCompressFiles;
331 }
332 else
333 {
334 builder = new MPFSClassicBuilder(txtProjectDir.Text, txtImageName.Text);
335 ((MPFSClassicBuilder)builder).ReserveBlock = (UInt32)Settings.Default.ReserveBlockClassic;
336 }
337  
338 // Add the files to the image
339 myStatusMsg.Text = "Adding source files to image...";
340 builder.AddDirectory(txtSourceDir.Text, "");
341  
342 // Generate the image
343 myStatusMsg.Text = "Generating output image...";
344 myProgress.Value = (radOutputBIN.Checked && chkUpload.Checked) ? 20 : 70;
345 if (radOutputBIN.Checked)
346 generationResult = builder.Generate(MPFSOutputFormat.BIN);
347 else if (radOutputC18.Checked)
348 generationResult = builder.Generate(MPFSOutputFormat.C18);
349 else if (radOutputMDD.Checked)
350 generationResult = builder.Generate(MPFSOutputFormat.MDD);
351 else if (radOutputASM30.Checked)
352 generationResult = builder.Generate(MPFSOutputFormat.ASM30);
353  
354 // Indicate full progress for non-uploads
355 myProgress.Value = (radOutputBIN.Checked && chkUpload.Checked) ? 20 : 120;
356 Thread.Sleep(10);
357  
358 // Retrieve the log
359 generateLog = builder.Log;
360  
361 // Perform the upload if needed
362 if (radOutputBIN.Checked && chkUpload.Checked && generationResult)
363 {
364 UploadImage(builder.GeneratedImageFileName);
365 }
366 else
367 {
368 if (generationResult)
369 ShowResultDialog("The MPFS" + ((Settings.Default.OutputVersion == 1) ? "" : "2") +
370 " image was successfully generated.");
371 else
372 ShowResultDialog("Errors were encountered while generating the MPFS image.");
373 }
374  
375 // Show a warning if index has changed
376 if (builder.IndexUpdated)
377 {
378 MessageBox.Show("The dynamic variables in your web pages have changed!\n\n" +
379 "Remember to recompile your MPLAB project before continuing\n" +
380 "to ensure that the project is in sync.",
381 "MPFS2 Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
382 }
383 }
384 // This is just an upload
385 else
386 {
387 generationResult = true;
388 generateLog = new List<string>();
389 UploadImage(txtSourceImage.Text);
390 }
391 }
392  
393 /// <summary>
394 /// Upload a file from disk to the board
395 /// </summary>
396 /// <param name="filename"></param>
397 private void UploadImage(String filename)
398 {
399 if (!File.Exists(filename))
400 {
401 generateLog.Add("ERROR: Could not open " + filename);
402 generationResult = false;
403 ShowResultDialog("The image could not be uploaded.");
404 }
405  
406 String protocol = GetProtocol();
407  
408 FileInfo fileinfo = new FileInfo(filename);
409 if (protocol == "http")
410 generateLog.Add("\r\nUploading MPFS2 image: " + fileinfo.Length + " bytes");
411 else
412 generateLog.Add("\r\nUploading MPFS Classic image: " + fileinfo.Length + " bytes");
413  
414 // Set up web client and the credentials
415 web = new MPFS2WebClient();
416 if (Settings.Default.UploadUser.Length > 0)
417 {
418 web.UseDefaultCredentials = false;
419 web.Credentials = new NetworkCredential(Settings.Default.UploadUser, Settings.Default.UploadPass);
420 }
421  
422 // Update the status bar display
423 myStatusMsg.Text = "Contacting device for upload...";
424 myProgress.Style = ProgressBarStyle.Marquee;
425 Refresh();
426  
427 // Register event handlers and start the upload
428 web.UploadProgressChanged += new UploadProgressChangedEventHandler(web_UploadProgressChanged);
429 web.UploadFileCompleted += new UploadFileCompletedEventHandler(web_UploadFileCompleted);
430 web.UploadFileAsync(new Uri(
431 protocol + "://" +
432 Settings.Default.UploadAddress + "/" +
433 Settings.Default.UploadPath),
434 filename);
435 }
436  
437 /// <summary>
438 /// Handle status updates from the web client
439 /// </summary>
440 /// <param name="sender"></param>
441 /// <param name="e"></param>
442 void web_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
443 {
444 myProgress.Style = ProgressBarStyle.Blocks;
445 myStatusMsg.Text = "Uploading image (" + e.BytesSent + " / " + e.TotalBytesToSend + " bytes)";
446 if(e.ProgressPercentage < 50 && e.ProgressPercentage >= 0)
447 myProgress.Value = 20 + (int)(1.5*e.ProgressPercentage);
448 if (e.ProgressPercentage == 50)
449 {
450 myStatusMsg.Text = "Waiting for upload to complete...";
451 myProgress.Style = ProgressBarStyle.Marquee;
452 }
453 }
454  
455 /// <summary>
456 /// Handles the completion event from the web client
457 /// </summary>
458 /// <param name="sender"></param>
459 /// <param name="e"></param>
460 void web_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
461 {
462 // First, stop the marquee
463 myProgress.Style = ProgressBarStyle.Blocks;
464 myProgress.Value = 120;
465 myStatusMsg.Text = "Process Complete... See status dialog.";
466  
467 // Display the results
468 if (e.Error == null)
469 ShowResultDialog("The MPFS image upload was successfully completed.");
470 else
471 {
472 generationResult = false;
473 generateLog.Add("\r\nERROR: Could not contact remote device for upload.");
474 generateLog.Add("ERROR: " + e.Error.Message);
475 ShowResultDialog("The MPFS image could not be uploaded.");
476 }
477 }
478  
479 /// <summary>
480 /// Displays the results of a generation / upload routine
481 /// </summary>
482 /// <param name="message"></param>
483 private void ShowResultDialog(String message)
484 {
485 LogWindow dlg = new LogWindow();
486  
487 if (generationResult)
488 dlg.Image = SystemIcons.Asterisk;
489 else
490 dlg.Image = SystemIcons.Error;
491  
492 dlg.Message = message;
493 dlg.Log = generateLog;
494  
495 // This forces the log window to the top if
496 // the application is behind another.
497 this.Focus();
498  
499 // Show the log window
500 dlg.ShowDialog();
501  
502 myProgress.Style = ProgressBarStyle.Blocks;
503 myProgress.Value = 0;
504 myStatusMsg.Text = "[Generator Idle]";
505 btnGenerate.Enabled = true;
506 }
507  
508 #region Button Handlers
509 /// <summary>
510 /// Selects the source file or directory
511 /// </summary>
512 /// <param name="sender"></param>
513 /// <param name="e"></param>
514 private void btnSourceDir_Click(object sender, EventArgs e)
515 {
516 if (radStartDir.Checked)
517 {
518 FolderBrowserDialog dlg = new FolderBrowserDialog();
519 dlg.SelectedPath = txtSourceDir.Text;
520 dlg.Description = "Select the directory in which your web pages are stored:";
521 if (dlg.ShowDialog() == DialogResult.OK)
522 txtSourceDir.Text = dlg.SelectedPath;
523 DirectoryInfo dir = new DirectoryInfo(dlg.SelectedPath);
524 txtProjectDir.Text = dir.Parent.FullName;
525 }
526 else
527 {
528 OpenFileDialog dlg = new OpenFileDialog();
529 dlg.Filter = "MPFS Image (*.bin)|*.bin";
530 dlg.FileName = txtSourceImage.Text;
531 if (dlg.ShowDialog() == DialogResult.OK)
532 txtSourceImage.Text = dlg.FileName;
533 }
534 }
535  
536 /// <summary>
537 /// Selects the project directory
538 /// </summary>
539 /// <param name="sender"></param>
540 /// <param name="e"></param>
541 private void btnProjectDir_Click(object sender, EventArgs e)
542 {
543 FolderBrowserDialog dlg = new FolderBrowserDialog();
544 dlg.SelectedPath = txtProjectDir.Text;
545 dlg.Description = "Select the directory in which your MPLAB project is located:";
546 if (dlg.ShowDialog() == DialogResult.OK)
547 txtProjectDir.Text = dlg.SelectedPath;
548 }
549  
550 /// <summary>
551 /// Shows the advanced options dialog
552 /// </summary>
553 /// <param name="sender"></param>
554 /// <param name="e"></param>
555 private void btnAdvanced_Click(object sender, EventArgs e)
556 {
557 AdvancedOptions dlg = new AdvancedOptions();
558 dlg.ShowDialog();
559 CorrectDisplay(sender, e);
560 }
561  
562 /// <summary>
563 /// Shows the About box when the version label is clicked
564 /// </summary>
565 /// <param name="sender"></param>
566 /// <param name="e"></param>
567 private void lblAbout_Click(object sender, EventArgs e)
568 {
569 AboutBox dlg = new AboutBox();
570 dlg.ShowDialog();
571 }
572  
573 /// <summary>
574 /// Shows the upload settings dialog when the button is clicked
575 /// </summary>
576 /// <param name="sender"></param>
577 /// <param name="e"></param>
578 private void btnUploadSettings_Click(object sender, EventArgs e)
579 {
580 UploadSettings dlg = new UploadSettings();
581 dlg.ShowDialog();
582 CorrectDisplay(sender, e);
583 }
584  
585 #endregion
586  
587 /// <summary>
588 /// Obtains the protocol to be used for uploading
589 /// </summary>
590 /// <returns></returns>
591 private String GetProtocol()
592 {
593 // For images being built, base on the output version
594 if (radStartDir.Checked)
595 {
596 if (Settings.Default.OutputVersion == 2)
597 return "http";
598 else
599 return "ftp";
600 }
601 // For images being uploaded from disk, try to read the file header
602 // If file does not exist, default to http
603 else
604 {
605 try
606 {
607 BinaryReader bin = new BinaryReader(new FileStream(txtSourceImage.Text, FileMode.Open), Encoding.ASCII);
608 if (bin.ReadByte() == (byte)'M' && bin.ReadByte() == (byte)'P' &&
609 bin.ReadByte() == (byte)'F' && bin.ReadByte() == (byte)'S' &&
610 bin.ReadByte() == (byte)0x02)
611 {
612 // Upload an MPFS2 image
613 bin.Close();
614 return "http";
615 }
616 else
617 {
618 // Upload an MPFS Classic image
619 bin.Close();
620 return "ftp";
621 }
622 }
623 catch
624 {
625 // Will trap if the file did not exist or was unreadable
626 return "http";
627 }
628 }
629 }
630 }
631  
632 /// <summary>
633 /// Overrides the WebClient class to force all FTP connections to passive mode
634 /// </summary>
635 public class MPFS2WebClient : System.Net.WebClient
636 {
637 protected override WebRequest GetWebRequest(Uri address)
638 {
639 WebRequest req = base.GetWebRequest(address);
640 if (req is FtpWebRequest)
641 ((FtpWebRequest)req).UsePassive = false;
642 return req;
643 }
644 }
645 }
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3