/// <summary> /// Event handler for connected devices. /// </summary> /// <param name="sender">Sender of the event.</param> /// <param name="args">Event arguments.</param> private void OnDeviceConnected(object sender, CDMEventArgs args) { Device device = args.Device; if (configuration.ShowNotificationPopups) { string message = "'" + device.Name + "' has been connected. " + "You may now synchronize the device " + "with the playlist for this device.\n\nDevices currently connected:"; foreach (Device d in connectedDevices.GetConnectedDevices()) { message += "\n - " + d.Name; } itaTray.ShowBalloonTip(5, "Device connected!", message, ToolTipIcon.Info); } IITPlaylist playlist = PlaylistExists(device); //Delete playlist if it exists. //if (playlist != null) // playlist.Delete(); if (playlist == null) { try { if (configuration.UseListFolder) { CreateMyDevicesFolder(); playlist = folderMyDevices.CreatePlaylist(device.Name); } else { playlist = itunes.CreatePlaylist(device.Name); } } catch (Exception e) { l.Error(e); MessageBox.Show("Failed to create list for device '" + device.Name + "'. You will not be able to synchronize this device with iTunes.", "Playlist error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { //If the option to use "My Devices" folder is set, move the playlist to that folder. if (configuration.UseListFolder && (playlist.Kind == ITPlaylistKind.ITPlaylistKindUser) && (device.Playlist == null || device.Playlist.Length == 0)) { CreateMyDevicesFolder(); object parent = (object)folderMyDevices; ((IITUserPlaylist)playlist).set_Parent(ref parent); } } }
public static IITUserPlaylist ShufflePlayList(IITUserPlaylist playlist) { IITTrackCollection tracks = playlist.Tracks; Random rndm = new Random((int)DateTime.Now.Ticks); int nTracks = tracks.Count; // Generate shuffled track order int[] shuffle = new int[nTracks]; for (int n = 0; n < nTracks; n++) { shuffle[n] = n + 1; } for (int n = 0; n < nTracks; n++) { int i1 = rndm.Next(nTracks); int i2 = rndm.Next(nTracks); int t = shuffle[i1]; shuffle[i1] = shuffle[i2]; shuffle[i2] = t; } // Create temporary playlist string plName = playlist.Name; string tplName = string.Format("{0}_shfl", plName); IITUserPlaylist parent = playlist.get_Parent(); IITUserPlaylist pl1 = (parent == null) ? App.iTunes.CreatePlaylist(tplName) as IITUserPlaylist : parent.CreatePlaylist(tplName) as IITUserPlaylist; // populate playlist with shuffled track order for (int n = 0; n < nTracks; n++) { IITTrack trk = tracks[shuffle[n]]; pl1.AddTrack(trk); } // delete original playlist playlist.Delete(); // rename new playlist to original name pl1.Name = plName; // Retuen new playlist so tree can be updated. return(pl1); }