Friday, June 26, 2009

Base functionality of the Form

Here is a screenshot of the form in designer view:

image

This form is just relatively simple C'# code (the LinQ2XML will be described in future posts). I’ll just point out a few of the important parts:

Choosing the iTunes library

This field holds the fully qualified filename for the iTunes library. There is a sensible default (line 11), but you can choose a different location with the “Choose …” button (code in lines 27ff.)

Populating the listbox of playlists (lbPlaylists)

This listbox holds the playlists in the iTunes library. It is populated at the startup of the form and whenever a different library path is chosen. We’ll have a closer look at the code that achieves this behavior in another post (lines 18ff.).

Select all / Select None of the entries in lbPlaylists

These are just shortcuts to make selecting all or unselecting of the playlists easier. (Or is it select none?!) It’s just a simple loop over all the items in the listbox (lines 64ff and 72ff).

Choosing the path for the converted playlists (noxon Library)

Another simple data field that holds the directory the converted playlists are to be written to. The behavior of the selection dialog is a little bit different than the one for the iTunes library (lines 46ff).

Convert the playlists

The code that is triggered by clicking the “Convert” button will be described in another post.

Here’s the main part of the form code:

public partial class frmConverter : Form
  {
      private Library _library;

      public frmConverter()
      {
          InitializeComponent();
          txtLibrary.Text =
                 Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)
                 + "\\iTunes\\iTunes Music Library.xml";
          txtResultPath.Text = "\\\\Marvin\\musik\\Playlists";
          // for testing
          //txtResultPath.Text = "C:\\temp\\Playlists\\";
          _library = new Library(txtLibrary.Text);
          populate_lbPlaylists();          
      }

      private void populate_lbPlaylists()
      {
          lbPlaylists.Items.Clear();
          foreach (Playlist pl in _library.getPlaylists())
          {
              lbPlaylists.Items.Add(pl);
          }
      }

      private void btnChooseLibrary_Click(object sender, EventArgs e)
      {
          OpenFileDialog dlg = new OpenFileDialog();
          dlg.Filter = "iTunes Library (*.xml)|*.xml";
          dlg.InitialDirectory = Path.GetDirectoryName(txtLibrary.Text);

          if (dlg.ShowDialog() == DialogResult.OK)
          {
              txtLibrary.Text = dlg.FileName;
              _library = new Library(txtLibrary.Text);
              populate_lbPlaylists();
          }
      }

      private void btnClose_Click(object sender, EventArgs e)
      {
          this.Close();
      }

      private void btnChooseResult_Click(object sender, EventArgs e)
      {
          FolderBrowserDialog dlg = new FolderBrowserDialog();
          dlg.SelectedPath = Path.GetDirectoryName(txtResultPath.Text+"\\");
          dlg.Description = "Choose the path to write the playlists to:";
          dlg.ShowNewFolderButton = false;

          if (dlg.ShowDialog() == DialogResult.OK)
          {
              txtResultPath.Text = dlg.SelectedPath;
          }
      }

      private void btnConvert_Click(object sender, EventArgs e)
      {
   // more on this later
      }

      private void btnAll_Click(object sender, EventArgs e)
      {
             for (int i = 0; i < lbPlaylists.Items.Count; i++)
              {
                  lbPlaylists.SetSelected (i, true );
              }
      }

      private void btnNone_Click(object sender, EventArgs e)
      {
          for (int i = 0; i < lbPlaylists.Items.Count; i++)
          {
              lbPlaylists.SetSelected(i, false);
          }
      }
  }

Main | Next

No comments:

Post a Comment