﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 3/5/2010
 * Time: 9:33 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using UsbDevice.Library;

namespace USBToolMonitor {
    
    /// <summary>
    /// Description of SourceDeviceSelectDialog.
    /// </summary>
    public partial class SourceDeviceSelectDialog : Form {        
        private DataSet drivesModel = new DataSet("RemovableDrives");
        public string selectedSerial { get; set; }
        
        public SourceDeviceSelectDialog() {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            setupModel();
            loadDriveList();
            loadItemsInView();
        }
        
        private void loadItemsInView() {
            ImageList imageList = new ImageList();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SourceDeviceSelectDialog));
            imageList.Images.Add((Bitmap)resources.GetObject("$this.removable_icon"));
            driveListView.View = View.LargeIcon;
            driveListView.Columns.Add("Drive", -2, HorizontalAlignment.Left);
            driveListView.LargeImageList = imageList;
        }
        
        private void setupModel() {
            DataTable table = drivesModel.Tables.Add("drives");
            table.Columns.Add("letter");
            table.Columns.Add("serial");
        }
        
        private void loadDriveList() {
            VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();
            Object[] row = new Object[2];
			foreach (Volume device in volumeDeviceClass.Devices) {
				if (device.IsUsb) {
                    row[0] = device.LogicalDrive;
                    row[1] = device.Serial;
                    drivesModel.Tables[0].Rows.Add(row);
                    driveListView.Items.Add(row[0].ToString(), 0);
				}
			}
        }
        
        void DriveListViewSelectedIndexChanged(object sender, EventArgs e) {
            ListView.SelectedListViewItemCollection selection = driveListView.SelectedItems;
            if (selection.Count == 0)
                return;
            selectedSerial = drivesModel.Tables[0].Rows[selection[0].Index][1].ToString();
            DialogResult = DialogResult.OK;
        }
    }
}
