/// <summary>
        /// Move configuration from iTA to Notpod
        /// </summary>
        /// <returns></returns>
        public static bool MovePreNotpodConfiguration()
        {
            MessageBox.Show("It seems that you have recently installed me, or upgraded me from iTunes Agent - my previous name. "
                + "I need to configure myself for this new version.\n\nIf you "
                + "have existing configuration from an earlier version of the application, then I will help "
                + "you copy this. Please click 'OK' to continue.",
                "Configuration changes required", MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            try
            {
                WriteNewConfiguration();
            }
            catch (Exception ex)
            {
                string message = "I failed to write default configuration to "
                    + MainForm.DATA_PATH + "\\notpod-config.xml. I can "
                    + "not continue without this configuration.";
                
                l.Error(message, ex);

                MessageBox.Show(message, "Configuation failure",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);                
                return false;
            }

            try
            {
                WriteNewDeviceConfiguration();
            }
            catch (Exception ex)
            {
                string message = "I failed to write default device configuration to "
                    + MainForm.DATA_PATH + "\\device-config.xml.\n\nI can "
                    + "not continue without this configuration.";

                l.Error(message, ex);

                MessageBox.Show(message, "Configuation failure",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);                
                return false;
            }



            if (MessageBox.Show("Do you want me to import old device configuration from a previous version of "
                                + "Notpod, or iTunes Agent?\n\nIf this is a fresh installation of Notpod, not "
                                + "an upgrade, you can safely choose 'No'.", "Upgrade device configuration?", 
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                FolderBrowserDialog folders = new FolderBrowserDialog();
                folders.ShowNewFolderButton = false;
                folders.SelectedPath = GetLikelyPreNotpodPath();                                
                folders.Description = "Please choose the installation folder of your pre Notpod installation";

                if (folders.ShowDialog() == DialogResult.OK)
                {
                    string appPath = folders.SelectedPath;

                    // Existing configuration 
                    try
                    {
                        XmlSerializer deserializer = new XmlSerializer(typeof(Configuration));
                        Configuration oldConfig = (Configuration)deserializer.Deserialize(
                            new XmlTextReader(new StreamReader(appPath + "\\ita-config.xml")));
                        Configuration newConfig = new Configuration();
                        newConfig.ShowNotificationPopups = oldConfig.ShowNotificationPopups;
                        newConfig.UseListFolder = oldConfig.UseListFolder;

                        XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
                        XmlTextWriter xtw = new XmlTextWriter(new StreamWriter(MainForm.DATA_PATH
                            + "\\notpod-config.xml"));
                        serializer.Serialize(xtw, newConfig);
                        xtw.Close();

                    }
                    catch (Exception ex)
                    {
                        string message = "I was unable to read the old configuration file in " + appPath
                            + ", or write existing configuration to the new location " + MainForm.DATA_PATH
                            + "\\notpod-config.xml. I will now exit. You may start me again and try another "
                            + "time, or simply continue using the new default configration.\n\nError was: "
                            + ex.Message;

                        l.Error(message, ex);

                        MessageBox.Show(message,
                            "Failed to locate configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);                        
                        return false;
                    }

                    // Existing device configuration
                    try
                    {
                        XmlSerializer deserializer = new XmlSerializer(typeof(DeviceConfiguration));
                        DeviceConfiguration oldConfig = (DeviceConfiguration)deserializer.Deserialize(
                            new XmlTextReader(new StreamReader(appPath + "\\device-config.xml")));
                        DeviceConfiguration newConfig = (DeviceConfiguration)deserializer.Deserialize(
                            new XmlTextReader(new StringReader(Resources.device_config)));

                        foreach (SyncPattern sp in oldConfig.SyncPatterns) {
                            
                            if(!newConfig.ContainsSyncPattern(sp)) {
                                newConfig.AddSyncPattern(sp);
                            }
                        }

                        foreach (Device d in oldConfig.Devices)
                            newConfig.AddDevice(d);


                        XmlSerializer serializer = new XmlSerializer(typeof(DeviceConfiguration));
                        XmlTextWriter xtw = new XmlTextWriter(new StreamWriter(MainForm.DATA_PATH
                            + "\\device-config.xml"));
                        serializer.Serialize(xtw, newConfig);
                        xtw.Close();

                    }
                    catch (Exception ex)
                    {
                        string message = "I was unable to read the old configuration file in " + appPath
                            + ", or write existing configuration to the new location " + MainForm.DATA_PATH
                            + "\\notpod-config.xml. I will now exit. You may start me again and try another "
                            + "time, or simply continue using the new default configration.\n\nError was: "
                            + ex.Message;

                        l.Error(message, ex);

                        MessageBox.Show(message,
                            "Failed to locate configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);                        
                        return false;
                    }

                
                }            
            }

            // Done. Write file indicating that the upgrade was successful.
            try
            {
                FileStream convertDoneFile = File.Create(MainForm.DATA_PATH + "\\.ita-convert");
                convertDoneFile.Close();
                MessageBox.Show("I have successfully configured myself and we're ready to go. Enjoy!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                l.Error(ex);

                MessageBox.Show("I failed to move configuration for the new version. Please try restarting the application.", "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);                
                return false ;
            }

            return true;
                                
        }
Exemple #2
0
        /// <summary>
        /// Event handler for the form load event.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        private void MainForm_Load(object sender, EventArgs e)
        {

            this.Visible = false;

            this.ShowInTaskbar = true;

            // Check application configuration
            if (!File.Exists(MainForm.DATA_PATH + "\\.ita-convert"))
            {
                if (!ConfigurationHelper.MovePreNotpodConfiguration())
                {
                    Application.Exit();
                    return;
                }

            }

            //Try loading the configuration
            StreamReader stream = null;
            XmlTextReader reader = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
                stream = new StreamReader(MainForm.DATA_PATH + "\\notpod-config.xml");
                reader = new XmlTextReader(stream);

                configuration = (Configuration)serializer.Deserialize(reader);

            }
            catch (Exception ex)
            {
                string message = "I could not locate 'notpod-config.xml' which contains configuration data,"
                    + " or the file exists, but is invalid. Please make sure this file is "
                    + "valid and restart me.\n\nError message: " + ex.Message;

                l.Error(message, ex);

                MessageBox.Show(message, "Configuration not available",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
                if (reader != null)
                    reader.Close();
            }

            //Load devices that the agent recognizes
            StreamReader dcStream = null;
            XmlTextReader dcReader = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(DeviceConfiguration));
                dcStream = new StreamReader(MainForm.DATA_PATH + "\\device-config.xml");
                dcReader = new XmlTextReader(dcStream);

                deviceConfiguration = (DeviceConfiguration)serializer.Deserialize(dcReader);

            }
            catch (Exception ex)
            {
                l.Error(ex);

                MessageBox.Show("Unable to locate list of known devices. The agent needs this list.\n\nReason for failure: " + ex.Message,
                    "Missing list of devices", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            finally
            {
                if (dcStream != null)
                    dcStream.Close();
                if (dcReader != null)
                    dcReader.Close();
            }

            if (!CreateITunesInstance())
            {
                Application.Exit();
                return;
            }

            if (configuration.UseListFolder)
                CreateMyDevicesFolder();

            //Create ConnectedDevice instance which maintains a list of connected devices.
            connectedDevices = new ConnectedDevicesManagerImpl();
            connectedDevices.DeviceConfig = deviceConfiguration;
            connectedDevices.DeviceConnected += new DeviceConnectedEventHandler(OnDeviceConnected);
            connectedDevices.DeviceDisconnected += new DeviceDisconnectedEventHandler(OnDeviceDisconnect);

            timerDriveListUpdate.Start();


        }
        /// <summary>
        /// Create a new instance of configuration form.
        /// </summary>
        /// <param name="configuration">Configuration containing the configuration for the application.</param>
        /// <param name="deviceConfiguration">DeviceConfiguration containing all the configured devices.</param>
        /// <param name="itunes">Reference to iTunes interface.</param>
        public ConfigurationForm(ref Configuration configuration, ref DeviceConfiguration deviceConfiguration, ref iTunesApp itunes)
        {
            InitializeComponent();

            this.configuration = configuration;
            this.checkNotifications.Checked = configuration.ShowNotificationPopups;
            this.checkUseListFolder.Checked = configuration.UseListFolder;
            this.checkAutocloseSyncWindow.Checked = configuration.CloseSyncWindowOnSuccess;
            this.checkWarnOnSystemDrives.Checked = configuration.WarnOnSystemDrives;
            this.checkConfirmMusicLocation.Checked = configuration.ConfirmMusicLocation;

            this.deviceConfiguration = deviceConfiguration;
            for (int d = 0; d < deviceConfiguration.Devices.Length; d++)
            {
                Device device = deviceConfiguration.Devices[d];
                ListViewItem item = new ListViewItem(device.Name);
                if (listDevices.Items.Contains(item))
                {
                    MessageBox.Show(this, "Duplicate devices with name '" + device.Name
                                    + "' was found. The duplicated items will be removed from the "
                                    + "configuration.", "Invalid configuration", MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);

                    deviceConfigurationChanged = true;
                    buttonOK.Enabled = true;
                    deviceConfiguration.RemoveDevice(device);
                    continue;
                }

                listDevices.Items.Add(device.Name);
            }

            //Add synchronize patterns to combo box.
            for (int s = 0; s < deviceConfiguration.SyncPatterns.Length; s++)
            {
                SyncPattern pattern = deviceConfiguration.SyncPatterns[s];
                if (comboSyncPatterns.Items.Contains(pattern.Name))
                {
                    MessageBox.Show(this, "An inconsistency was found in the synchronize pattern "
                                    + "configuration. The same name was found for two synchronize patterns. One will be deleted.",
                                    "Invalid configuration", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    deviceConfigurationChanged = true;
                    deviceConfiguration.RemoveSyncPattern(pattern);
                    buttonOK.Enabled = true;
                    continue;
                }

                comboSyncPatterns.Items.Add(pattern.Name);
            }

            //Add playlists to "Associate with playlist" combo
            bool retry = true;
            while (retry)
            {
                try
                {
                    foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
                    {
                        if (pl.Kind != ITPlaylistKind.ITPlaylistKindLibrary
                            && pl.Kind != ITPlaylistKind.ITPlaylistKindRadioTuner
                            && pl.Kind != ITPlaylistKind.ITPlaylistKindDevice
                            && pl.Kind != ITPlaylistKind.ITPlaylistKindCD
                            && pl.Visible)
                            comboAssociatePlaylist.Items.Add(pl.Name);
                    }

                    comboAssociatePlaylist.SelectedIndex = 0;
                    retry = false;
                }
                catch (COMException comex)
                {
                    l.Warn(comex);

                    if (MessageBox.Show(this, "An error occured while getting the list of playlists. "
                                        + "This may be because one or more applications are busy. "
                                        + "Do you want to retry?\n\n(" + comex.Message + ")",
                                        "Communication error", MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        retry = true;
                        comboAssociatePlaylist.Items.Clear();
                    }
                    else
                    {
                        retry = false;
                    }
                }

            }
        }