コード例 #1
0
ファイル: SyncPattern.cs プロジェクト: JoeGilkey/RadioLog
        public static bool[] getPattern(SyncPattern pattern)
        {
            switch (pattern)
            {
                case SyncPattern.FLEETSYNC1: return new bool[] 
	{ 
		false, true, false, true, false,     //end of revs
		false, true, true, true,             //0111 0x7 
		false, true, true, false,            //0110 0x6
		false, true, false, true,            //0101 0x5
		false, false, false, false           //0000 0x0
	};
                case SyncPattern.FLEETSYNC2: return new bool[] 
	{ 
		false, true, false, true, false,	//End of bit revs
		false, false, true, false,          //0010 0x2 
		false, false, true, true,           //0011 0x3
		true, true, true, false,            //1110 0xE
		true, false, true, true             //1011 0xB
	};
                case SyncPattern.MDC1200: return new bool[]
	{
		false, false, false, false,//0000 0x0
		false, true, true, true,   //0111 0x7
		false, false, false, false,//0000 0x0
		true, false, false, true,  //1001 0x9
		false, false, true, false, //0010 0x2
		true, false, true, false,  //1010 0xA
		false, true, false, false, //0100 0x4
		false, true, false, false, //0100 0x4
		false, true, true, false,  //0110 0x6
		true, true, true, true     //1111 0xF
	};
                case SyncPattern.MPT1327_CONTROL: return new bool[]
	{
		true, false, true, false,   //Includes 4 of 16 rev bits
		true, true, false, false,   //1100 0xA
		false, true, false, false,  //0100 0x4
		true, true, false, true,    //1101 0xD
		false, true, true, true     //0111 0x7
	};
                case SyncPattern.MPT1327_CONTROL_FRENCH: return new bool[]
	{
		true, false, true, false,   //Includes final 4 of 16 rev bits
		true, false, true, true,    //1011 0xB
		false, true, false, false,  //0100 0x4
		false, false, true, true,   //0011 0x3
		false, false, true, true    //0011 0x3
	};
                case SyncPattern.MPT1327_TRAFFIC: return new bool[]
	{
		true, false, true, false,   //1010 Includes 4 of 16 rev bits
		false, false, true, true,   //0011 0x3
		true, false, true, true,    //1011 0xB
		false, false, true, false,  //0010 0x2
		true, false, false, false   //1000 0x8
	};
                case SyncPattern.MPT1327_TRAFFIC_FRENCH: return new bool[]
	{
		true, false, true, false,   //Includes final 4 of 16 rev bits
		false, true, false, false,  //0100 0x4
		true, false, true, true,    //1011 0xB
		true, true, false, false,   //1100 0xC
		true, true, false, false   //1100 0xC
	};
                case SyncPattern.PASSPORT: return new bool[] 
	{
		true, 						//0001 0x1
		false, true, false, true, 	//0101 0x5
		true, false, false, false	//1000 0x8
	};
                case SyncPattern.LTR_STANDARD_OSW: return new bool[] 
	{
		true,false,true,false,true,true,false,false,false
	};
                case SyncPattern.LTR_STANDARD_ISW: return new bool[] 
	{
		false, true, false, true,false, false, true, true, true
	};
            }
            return null;
        }
コード例 #2
0
 public static AsyncPattern COnce(SyncPattern target) => _AsGCR(target, GCP.Times(_ => 1));
コード例 #3
0
        /// <summary>
        /// <see cref="Notpod.ISynchronizer#SynchronizeDevice(List<IITPlaylist>, string, Device)"/>
        /// </summary>
        public void SynchronizeDevice(List <IITPlaylist> playlist, string drive, Device device)
        {
            //Check that configuration has been set.
            if (configuration == null)
            {
                throw new SynchronizeException("Configuration has not been set.");
            }

            DirectoryInfo di = new DirectoryInfo(drive + device.MediaRoot);

            // Check if the media root directory actually exists
            // Thanks to Robert Grabowski for the contribution.
            try
            {
                if (!di.Exists)
                {
                    di.Create();
                }
            }
            catch (IOException ex)
            {
                string message = "Could not create directory '"
                                 + di.Name + "' for device '" + device.Name
                                 + "'. Unable to complete synchronization.";
                l.Error(message, ex);

                syncForm.AddLogText(message, Color.Red);
            }

            // Perform a write check to make sure Notpod has write
            // access to the music folder of the device.
            String writeCheckPath = drive + device.MediaRoot + "\\wrtchk.ita";

            try
            {
                FileStream writeCheckStream = File.Create(writeCheckPath);
                writeCheckStream.Close();
                File.Delete(writeCheckPath);
            }
            catch (Exception e)
            {
                l.Error("Could not write " + writeCheckPath + ".", e);

                String message = "Error: I am unable to write to the music folder of "
                                 + "your device. Please make sure I have the proper permissions"
                                 + " and try again.";

                syncForm.AddLogText(message, Color.Red);
                return;
            }

            FileInfo[] files = FileHelper.GetFilesRecursive(di.ToString()).ToArray();

            //Find correct synchronize pattern for the device.
            SyncPattern devicePattern = null;

            foreach (SyncPattern sp in configuration.SyncPatterns)
            {
                if (sp.Identifier == device.SyncPattern)
                {
                    devicePattern = sp;
                }
            }

            //Throw an exception if the pattern could not be found.
            if (devicePattern == null)
            {
                OnSynchronizeError(device, "Illegal synchronize pattern '" + device.SyncPattern + "' for device '" + device.Name + "'. Unable to complete synchronization.");
                return;
            }

            syncForm.AddLogText("Synchronizing '" + device.Name + "'...");
            syncForm.SetDeviceName(device.Name, drive);

            syncForm.SetCurrentStatus("Initializing...");
            int trackCount = 0;

            for (int i = 0; i < playlist.Count; i++)
            {
                trackCount += playlist[i].Tracks.Count;
            }
            syncForm.SetMaxProgressValue(trackCount);
            syncForm.SetProgressValue(0);


            // maintain a filename -> track object dictionary for the tracks to be copied onto the device
            // Thanks to Robert Grabowski for the contribution.
            Dictionary <string, IITFileOrCDTrack> syncList = new Dictionary <string, IITFileOrCDTrack>();

            string deviceMediaRoot = drive + (device.MediaRoot.Length > 0 ? device.MediaRoot + "\\" : "");

            try
            {
                foreach (IITPlaylist pl in playlist)
                {
                    foreach (IITTrack track in pl.Tracks)
                    {
                        if (syncForm.GetOperationCancelled())
                        {
                            syncForm.SetCurrentStatus("Synchronization cancelled. 0 tracks added, 0 tracks removed.");
                            syncForm.AddLogText("Synchronization cancelled.", Color.OrangeRed);
                            OnSynchronizeCancelled();
                            return;
                        }

                        syncForm.SetProgressValue(syncForm.GetProgressValue() + 1);

                        //Continue if the track is not of kind "file" or the track is one of the initial tracks on the device.
                        if (track.Kind != ITTrackKind.ITTrackKindFile || device.InitialTracks.Contains(track))
                        {
                            continue;
                        }


                        string pathOnDevice = "";

                        IITTrack addTrack = track;

                        try
                        {
                            pathOnDevice = SyncPatternTranslator.Translate(devicePattern, (IITFileOrCDTrack)addTrack, pl.Name);
                        }
                        catch (Exception ex)
                        {
                            syncForm.AddLogText("An error occured while working with \"" + track.Artist + " - " + track.Name
                                                + "\". This may be because the track has been deleted from disk. Look for an exclamation mark"
                                                + "next to the track in your playlist.", Color.Orange);
                            l.Debug("An error occured while working with \"" + track.Artist + " - " + track.Name
                                    + "\". This may be because the track has been deleted from disk. Look for an exclamation mark"
                                    + "next to the track in your playlist.", ex);
                            continue;
                        }
                        string fullPath = deviceMediaRoot + pathOnDevice;
                        l.Debug(fullPath);

                        // Check if the list already contains a key - this happens in cases where there are duplicate
                        // entries in the playlist for the same track. Although the track may have different locations on
                        // the user's computer, Notpod will not handle this.
                        if (syncList.ContainsKey(fullPath))
                        {
                            syncForm.AddLogText("You have duplicate listings for " + track.Artist + " - " + track.Name
                                                + " in your playlist. I will continue for now, but you should remove any duplicates "
                                                + "when the synchronization is complete.", Color.Orange);
                            continue;
                        }

                        syncList.Add(fullPath, (IITFileOrCDTrack)addTrack);
                    }
                }
            }
            catch (Exception ex)
            {
                syncForm.SetCurrentStatus("");
                String message = "Error occured while initializing: " + ex.Message;
                syncForm.AddLogText(message, Color.Red);
                syncForm.DisableCancelButton();
                syncForm.SetProgressValue(0);
                OnSynchronizeError(device, message);

                l.Error(message, ex);
                return;
            }
            syncForm.AddLogText("Initialization completed.");

            syncForm.SetCurrentStatus("Checking tracks. Removing those that are no longer in the playlist...");
            int totalTracks = files.Length;

            syncForm.SetMaxProgressValue(totalTracks);
            syncForm.SetProgressValue(0);

            int  tracksRemoved = 0;
            int  tracksAdded   = 0;
            long existingSize  = 0;

            try
            {
                //Remove tracks from device which are no longer in the playlist.
                foreach (FileInfo file in files)
                {
                    l.Debug("Checking file: " + file.FullName);

                    // Check for cancelled operation.
                    if (syncForm.GetOperationCancelled())
                    {
                        syncForm.SetCurrentStatus("Synchronization cancelled. " + tracksAdded
                                                  + " track(s) added, " + tracksRemoved + " track(s) removed.");
                        syncForm.AddLogText("Synchronization cancelled.", Color.OrangeRed);
                        OnSynchronizeCancelled();
                        return;
                    }

                    //Increase progress bar
                    syncForm.SetProgressValue(syncForm.GetProgressValue() + 1);

                    //Continue with next track if it is not of a supported extension.
                    //if (file.Extension != ".mp3" && file.Extension != ".acc" && file.Extension != ".m4p" && file.Extension != ".m4a")
                    if (!extensions.Contains(file.Extension))
                    {
                        continue;
                    }

                    if (syncList.ContainsKey(file.FullName))
                    {
                        FileInfo fi = new FileInfo(file.FullName);
                        existingSize += fi.Length;
                        continue;
                    }

                    //If the track was not found --- delete it!
                    string fileFullName = file.FullName;
                    file.Delete();

                    l.Debug("Removing file no longer in playlist: " + fileFullName);

                    CheckAndRemoveFolders(fileFullName, drive, device);

                    tracksRemoved++;
                }

                syncForm.AddLogText(tracksRemoved + " track(s) was removed from the device.",
                                    Color.Orange);
            }
            catch (MissingTrackException ex)
            {
                syncForm.SetCurrentStatus("");
                String message = "You have a missing file in your library. Please clean up "
                                 + "your playlist and remove the track '" + ex.Track.Artist + " - "
                                 + ex.Track.Name + "' before re-synchronizing.";
                syncForm.AddLogText(message, Color.Red);
                syncForm.DisableCancelButton();
                syncForm.SetProgressValue(0);
                OnSynchronizeError(device, message);

                l.Error(message, ex);

                return;
            }
            catch (Exception ex)
            {
                syncForm.SetCurrentStatus("");
                String message = "Error occured while checking for deleted tracks: " + ex.Message;
                syncForm.AddLogText(message, Color.Red);
                syncForm.DisableCancelButton();
                syncForm.SetProgressValue(0);
                OnSynchronizeError(device, message);

                l.Error(message, ex);
                return;
            }

            files = null;

            // Check free space on the device
            double playlistSize = 0;

            for (int i = 0; i < playlist.Count; i++)
            {
                playlistSize += playlist[i].Size;
            }
            DriveInfo driveInfo  = new DriveInfo(drive.Substring(0, 1));
            long      freeOnDisk = driveInfo.AvailableFreeSpace;

            if (freeOnDisk < playlistSize - existingSize)
            {
                string message = "There is not enough space on your device to synchronize the playlist.";
                OnSynchronizeError(device, message);
                syncForm.AddLogText(message, Color.Red);
                syncForm.SetCurrentStatus(message);
                syncForm.DisableCancelButton();
                syncForm.SetProgressValue(0);
                return;
            }

            try
            {
                syncForm.SetCurrentStatus("Copying new files...");
                syncForm.AddLogText("Preparing to copy new files.", Color.Black);
                syncForm.SetMaxProgressValue(syncList.Count);
                syncForm.SetProgressValue(0);

                //Check for new track in the playlist which should be copied to the device
                // NEW foreach: traverse synchronization list instead of playlist
                // Thanks to Robert Grabowski.
                foreach (string filePath in syncList.Keys)
                {
                    IITTrack track = syncList[filePath];

                    // Check for cancelled operation.
                    if (syncForm.GetOperationCancelled())
                    {
                        syncForm.SetCurrentStatus("Synchronization cancelled. " + tracksAdded
                                                  + " track(s) added, " + tracksRemoved + " track(s) removed.");
                        syncForm.AddLogText("Synchronization cancelled.", Color.OrangeRed);
                        syncForm.DisableCancelButton();
                        syncForm.SetProgressValue(0);
                        OnSynchronizeCancelled();
                        return;
                    }


                    //Increase progress bar
                    syncForm.SetProgressValue(syncForm.GetProgressValue() + 1);

                    string trackPath = filePath.Substring(deviceMediaRoot.Length); // hack: cut out media root
                    l.Debug("Checking for copy: " + filePath);

                    if (File.Exists(filePath))
                    {
                        continue;
                    }

                    try
                    {
                        CheckAndCreateFolders(trackPath, drive, device);
                        syncForm.SetCurrentStatus("Copying " + filePath
                                                  + " (" + syncForm.GetProgressValue() + "/" + syncForm.GetMaxProgressValue() + ")");

                        File.Copy(((IITFileOrCDTrack)track).Location, filePath, true);
                        File.SetAttributes(filePath, FileAttributes.Normal);


                        syncForm.AddLogText(filePath + " copied successfully.", Color.Green);

                        l.Debug("Copied: " + filePath);
                    }
                    catch (Exception ex)
                    {
                        String message = "Failed to copy " + filePath + ".\n-> " + ex.Message;
                        syncForm.AddLogText(message, Color.Red);
                        OnSynchronizeError(device, message);

                        l.Error(message, ex);

                        return;
                    }

                    tracksAdded++;
                }
            }
            catch (MissingTrackException ex)
            {
                syncForm.SetCurrentStatus("");
                String message = "You have a missing file in your library. Please remove the track '" + ex.Track.Artist + " - " + ex.Track.Name + "' and try again. I am sorry for the inconvenience.";
                syncForm.AddLogText(message, Color.Red);
                syncForm.DisableCancelButton();
                syncForm.SetProgressValue(0);
                OnSynchronizeError(device, message);

                l.Error(message, ex);

                return;
            }
            catch (Exception ex)
            {
                string message = "An error occured while copying new tracks: " + ex.Message;
                syncForm.SetCurrentStatus("");
                syncForm.AddLogText(message,
                                    Color.Red);
                syncForm.DisableCancelButton();
                syncForm.SetProgressValue(0);
                OnSynchronizeError(device, message);

                l.Error(message, ex);

                return;
            }

            syncForm.SetCurrentStatus("Synchronization completed. " + tracksAdded
                                      + " track(s) added, " + tracksRemoved + " track(s) removed.");
            syncForm.AddLogText("Completed. " + tracksAdded + " track(s) copied to your device.", Color.Green);
            syncForm.DisableCancelButton();
            OnSynchronizeComplete();
        }
コード例 #4
0
 private static AsyncPattern _AsGCR(SyncPattern target, GenCtxProperty[] props1, params GenCtxProperty[] props) =>
 _AsGCR(new[] { target }, props1, props);
コード例 #5
0
        /// <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 from iTunes. This may be because iTunes is 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;
                    }
                }
            }
        }