Tuesday, May 24, 2005

Playlist copier

I have a pocket pc that I use to listen to music. I like to change up what i'm listening to while I'm using PocketMusic - which is excellent.
So I wrote a little app to take whatever playlists I've created on my computer and copy them over to my sd card for use on the go.
Things that I will do if I figure it out - try to go around having to take sd card out and use usb writer . see if there is someway to just go through active sync.

Since I didn't see this trivial app out there - I created it.
Here you go.
Gpl License please. If you make something better from this, please let me know and please give it away.
Following is the contents of mainwindow.cs


using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace PlaylistCopier
{
///
/// Summary description for playlistcopier
///

public class MainWindow : Form
{
private TextBox txtPlaylistPath;
private Button btnOpen;
private GroupBox grpPlaylist;
private GroupBox grpSaveTo;
private TextBox txtSavePath;
private Button btnSavePath;
private Button btnCopy;
private StatusBar sb;
private OpenFileDialog ofd;
private SaveFileDialog sfd;
private ProgressBar progressBar;
///
/// Required designer variable.
///

private Container components = null;

public MainWindow()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.txtPlaylistPath = new System.Windows.Forms.TextBox();
this.ofd = new System.Windows.Forms.OpenFileDialog();
this.sfd = new System.Windows.Forms.SaveFileDialog();
this.btnOpen = new System.Windows.Forms.Button();
this.grpPlaylist = new System.Windows.Forms.GroupBox();
this.grpSaveTo = new System.Windows.Forms.GroupBox();
this.txtSavePath = new System.Windows.Forms.TextBox();
this.btnSavePath = new System.Windows.Forms.Button();
this.btnCopy = new System.Windows.Forms.Button();
this.sb = new System.Windows.Forms.StatusBar();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.grpPlaylist.SuspendLayout();
this.grpSaveTo.SuspendLayout();
this.SuspendLayout();
//
// txtPlaylistPath
//
this.txtPlaylistPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtPlaylistPath.Location = new System.Drawing.Point(8, 24);
this.txtPlaylistPath.Name = "txtPlaylistPath";
this.txtPlaylistPath.Size = new System.Drawing.Size(352, 20);
this.txtPlaylistPath.TabIndex = 1;
this.txtPlaylistPath.Text = "";
//
// btnOpen
//
this.btnOpen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnOpen.Location = new System.Drawing.Point(368, 24);
this.btnOpen.Name = "btnOpen";
this.btnOpen.Size = new System.Drawing.Size(24, 24);
this.btnOpen.TabIndex = 2;
this.btnOpen.Text = "...";
this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
//
// grpPlaylist
//
this.grpPlaylist.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.grpPlaylist.Controls.Add(this.txtPlaylistPath);
this.grpPlaylist.Controls.Add(this.btnOpen);
this.grpPlaylist.Location = new System.Drawing.Point(16, 24);
this.grpPlaylist.Name = "grpPlaylist";
this.grpPlaylist.Size = new System.Drawing.Size(400, 56);
this.grpPlaylist.TabIndex = 3;
this.grpPlaylist.TabStop = false;
this.grpPlaylist.Text = "Find Playlist";
//
// grpSaveTo
//
this.grpSaveTo.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.grpSaveTo.Controls.Add(this.txtSavePath);
this.grpSaveTo.Controls.Add(this.btnSavePath);
this.grpSaveTo.Location = new System.Drawing.Point(16, 88);
this.grpSaveTo.Name = "grpSaveTo";
this.grpSaveTo.Size = new System.Drawing.Size(400, 56);
this.grpSaveTo.TabIndex = 4;
this.grpSaveTo.TabStop = false;
this.grpSaveTo.Text = "Copy To";
//
// txtSavePath
//
this.txtSavePath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtSavePath.Location = new System.Drawing.Point(8, 24);
this.txtSavePath.Name = "txtSavePath";
this.txtSavePath.Size = new System.Drawing.Size(352, 20);
this.txtSavePath.TabIndex = 1;
this.txtSavePath.Text = "";
//
// btnSavePath
//
this.btnSavePath.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnSavePath.Location = new System.Drawing.Point(368, 24);
this.btnSavePath.Name = "btnSavePath";
this.btnSavePath.Size = new System.Drawing.Size(24, 24);
this.btnSavePath.TabIndex = 2;
this.btnSavePath.Text = "...";
this.btnSavePath.Click += new System.EventHandler(this.btnSavePath_Click);
//
// btnCopy
//
this.btnCopy.Location = new System.Drawing.Point(16, 160);
this.btnCopy.Name = "btnCopy";
this.btnCopy.Size = new System.Drawing.Size(40, 23);
this.btnCopy.TabIndex = 5;
this.btnCopy.Text = "Copy";
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
//
// sb
//
this.sb.Location = new System.Drawing.Point(0, 199);
this.sb.Name = "sb";
this.sb.Size = new System.Drawing.Size(424, 22);
this.sb.TabIndex = 6;
this.sb.Text = "Ready";
//
// progressBar
//
this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.progressBar.Location = new System.Drawing.Point(64, 160);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(352, 23);
this.progressBar.TabIndex = 7;
//
// MainWindow
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 221);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.sb);
this.Controls.Add(this.btnCopy);
this.Controls.Add(this.grpSaveTo);
this.Controls.Add(this.grpPlaylist);
this.MaximumSize = new System.Drawing.Size(960, 264);
this.MinimumSize = new System.Drawing.Size(168, 248);
this.Name = "MainWindow";
this.Text = "Playlist Copier";
this.grpPlaylist.ResumeLayout(false);
this.grpSaveTo.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new MainWindow());
}

private void btnOpen_Click(object sender, EventArgs e)
{

ofd.Multiselect = false;
ofd.Filter = "M3U Files|*.m3u";
if(ofd.ShowDialog() == DialogResult.OK)
{
this.txtPlaylistPath.Text = ofd.FileName;
}
}

private void btnSavePath_Click(object sender, EventArgs e)
{


//sfd.Multiselect = false;
//sfd.Filter = "*.m3u";
sfd.DefaultExt = ".m3u";


if(sfd.ShowDialog() == DialogResult.OK)
{
this.txtSavePath.Text = sfd.FileName;
}
}

private void btnCopy_Click(object sender, EventArgs e)
{

FileInfo fi = new FileInfo(txtPlaylistPath.Text.Trim());
FileInfo fo = new FileInfo(txtSavePath.Text);
DirectoryInfo dirOut = fo.Directory;
FileInfo fcur;
TextReader tr;
if(( tr = new StreamReader(fi.OpenRead())) == null)
{
MessageBox.Show("Problem Opening " + fi.Name + "for reading.");
return;
}
//get linecount of the playlist
int i = 0;
while(tr.ReadLine() != null)
{
i++;
}
tr.Close();
progressBar.Step = 1;
progressBar.Maximum = i;
progressBar.Minimum = 0;

tr = new StreamReader(fi.OpenRead());


TextWriter tw;
if((tw= new StreamWriter( fo.OpenWrite())) == null)
{
MessageBox.Show("Problem Opening " + fo.Name + "for writing.");
return;
}



try
{
sb.Text = "Copying";
string line;
while( (line = tr.ReadLine()) != null)
{
//is this a comment line?
if (line.StartsWith("#"))
{
//It's a comment line.

//progressBar.
//write it to the new file and then keep going
tw.WriteLine(line);

//increment progress
progressBar.PerformStep();

}
else
{
//it's a file path
//find the file
fcur = new FileInfo(fi.Directory.FullName + @"\" + line);

if(fcur.Exists) //only move good files
{
//only move files that don't already exist
FileInfo ftest = new FileInfo(dirOut.FullName + @"\" + line);
if(!ftest.Exists)
{
//copy it to the new file directory
DirectoryInfo dirnew = new DirectoryInfo(dirOut.FullName + @"\" + Path.GetDirectoryName(line));
if(!dirnew.Exists)
{
//Parent directory of new file doesn't exist yet


// need to create the parent dir for the file.
dirnew = dirOut.CreateSubdirectory(Path.GetDirectoryName(line));
if(! dirnew.Exists)
{
MessageBox.Show(@"Can't create "+ dirnew.FullName);
return;
}


fcur.CopyTo(dirOut.FullName + @"\"+ line);
}
}
//write the entry to the new playlist
tw.WriteLine(line);
//increment progress
progressBar.PerformStep();
}

}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
finally
{

tr.Close();
tw.Close();
progressBar.Value = 0;
sb.Text = "Ready";
}



}


}

//TODO: separate out the concept of a playlist into an interface


}