/// <summary>
        /// create a folder storage object withing the specified parent
        /// </summary>
        /// <param name="deviceContent">unmanaged device</param>
        /// <param name="parentObjectId">parent object id</param>
        /// <param name="newFolder">name of the new folder</param>
        /// <returns>created object id</returns>
        public string CreateFolderObject(IPortableDeviceContent deviceContent, string parentObjectId, string newFolder)
        {
            IPortableDeviceProperties deviceProperties;

            deviceContent.Properties(out deviceProperties);

            IPortableDeviceValues deviceValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues();

            GetRequiredPropertiesForFolder(parentObjectId, newFolder, ref deviceValues);

            string objectId = string.Empty;

            deviceContent.CreateObjectWithPropertiesOnly(deviceValues, ref objectId);
            return(objectId);
        }
        /**
         * Device must be connected!
         *
         * Returns the id of the new folder.
         */
        public PortableDeviceFolder CreateDir(PortableDevice device, string folderName)
        {
            PortableDeviceFolder result = null;

            try
            {
                String newFolderId = "";

                // Already got folderName?
                result = FindDir(folderName);
                if (null == result)
                {
                    IPortableDeviceContent content = device.getContents();

                    // Get the properties of the object
                    IPortableDeviceProperties properties;
                    content.Properties(out properties);

                    IPortableDeviceValues createFolderValues = GetRequiredCreateDirPropertiesForContentType(Id, folderName);

                    content.CreateObjectWithPropertiesOnly(createFolderValues, ref newFolderId);

                    // Last, add to list of files.
                    result = new PortableDeviceFolder(newFolderId, folderName);

                    this.Files.Add(result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                result = null;
            }

            return(result);
        }
        /// <summary>
        /// create a folder storage object withing the specified parent
        /// </summary>
        /// <param name="deviceContent">unmanaged device</param>
        /// <param name="parentObjectId">parent object id</param>
        /// <param name="newFolder">name of the new folder</param>
        /// <returns>created object id</returns>
        public string CreateFolderObject(IPortableDeviceContent deviceContent, string parentObjectId, string newFolder)
        {
            IPortableDeviceProperties deviceProperties;
            deviceContent.Properties(out deviceProperties);
            
            IPortableDeviceValues deviceValues = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValues();
            GetRequiredPropertiesForFolder(parentObjectId, newFolder, ref deviceValues);

            string objectId = string.Empty;
            deviceContent.CreateObjectWithPropertiesOnly(deviceValues,ref objectId);
            return objectId;
        }
Example #4
0
        public void SyncTracks(iTunesLibrary library)
        {
            if (musicFolder == null)
            {
                foobalator.Log.WriteLine("Music folder could not be found");
                return;
            }

            foreach (var iTunesTrack in library.selected.Values)
            {
                try
                {
                    if (iTunesTrack.path == null)
                    {
                        foobalator.Log.WriteLine(string.Format("{0}/{1}/{2} has no source.  tracktype: {3}", iTunesTrack.artist, iTunesTrack.album, iTunesTrack.name, iTunesTrack.trackType));
                        continue;
                    }

                    if (!File.Exists(iTunesTrack.path))
                    {
                        foobalator.Log.WriteLine(string.Format("file not found: \"{0}\"", iTunesTrack.path));
                        continue;
                    }

                    if (iTunesTrack.format == default(Guid))
                    {
                        foobalator.Log.WriteLine(string.Format("unsupported file type: \"{0}\"", iTunesTrack.path));
                        continue;
                    }

                    DeviceObject artistFolder = musicFolder.GetChildByName(iTunesTrack.artistFolder);
                    if (artistFolder == null)
                    {
                        PortableDeviceValues values = new PortableDeviceValues();
                        values.SetStringValue(Constants.WPD_OBJECT_PARENT_ID, musicFolder.id);
                        values.SetStringValue(Constants.WPD_OBJECT_NAME, iTunesTrack.artistFolder);
                        values.SetStringValue(Constants.WPD_OBJECT_ORIGINAL_FILE_NAME, iTunesTrack.artistFolder);
                        values.SetGuidValue(Constants.WPD_OBJECT_CONTENT_TYPE, Constants.WPD_CONTENT_TYPE_FOLDER);
                        values.SetGuidValue(Constants.WPD_OBJECT_FORMAT, Constants.WPD_OBJECT_FORMAT_PROPERTIES_ONLY);

                        string artistFolderDeviceId = null;
                        content.CreateObjectWithPropertiesOnly(values, ref artistFolderDeviceId);
                        artistFolder = GetObject(artistFolderDeviceId);
                    }

                    DeviceObject albumFolder = artistFolder.GetChildByName(iTunesTrack.albumFolder);
                    if (albumFolder == null)
                    {
                        PortableDeviceValues values = new PortableDeviceValues();
                        values.SetStringValue(Constants.WPD_OBJECT_PARENT_ID, artistFolder.id);
                        values.SetStringValue(Constants.WPD_OBJECT_NAME, iTunesTrack.albumFolder);
                        values.SetStringValue(Constants.WPD_OBJECT_ORIGINAL_FILE_NAME, iTunesTrack.albumFolder);
                        values.SetGuidValue(Constants.WPD_OBJECT_CONTENT_TYPE, Constants.WPD_CONTENT_TYPE_FOLDER);
                        values.SetGuidValue(Constants.WPD_OBJECT_FORMAT, Constants.WPD_OBJECT_FORMAT_PROPERTIES_ONLY);

                        string albumFolderDeviceId = null;
                        content.CreateObjectWithPropertiesOnly(values, ref albumFolderDeviceId);
                        albumFolder = GetObject(albumFolderDeviceId);
                    }

                    DeviceObject deviceObject = albumFolder.GetChildByOriginalFilename(iTunesTrack.filename);
                    if (deviceObject == null)
                    {
                        foobalator.Log.WriteLine(iTunesTrack.ToString());

                        PortableDeviceValues values = new PortableDeviceValues();
                        values.SetStringValue(Constants.WPD_OBJECT_PARENT_ID, albumFolder.id);
                        values.SetStringValue(Constants.WPD_OBJECT_NAME, iTunesTrack.filename);
                        values.SetGuidValue(Constants.WPD_OBJECT_FORMAT, iTunesTrack.format);
                        values.SetGuidValue(Constants.WPD_OBJECT_CONTENT_TYPE, Constants.WPD_CONTENT_TYPE_AUDIO);
                        values.SetStringValue(Constants.WPD_OBJECT_ORIGINAL_FILE_NAME, iTunesTrack.filename);
                        values.SetStringValue(Constants.WPD_MEDIA_ARTIST, iTunesTrack.artist);
                        values.SetStringValue(Constants.WPD_MEDIA_ALBUM_ARTIST, iTunesTrack.albumArtist);
                        values.SetStringValue(Constants.WPD_MUSIC_ALBUM, iTunesTrack.album);
                        values.SetUnsignedIntegerValue(Constants.WPD_MUSIC_TRACK, iTunesTrack.number);

                        System.Runtime.InteropServices.ComTypes.IStream deviceData;
                        uint   writeSize = 0x10000;
                        string cookie    = "";

                        content.CreateObjectWithPropertiesAndData(values, out deviceData, ref writeSize, ref cookie);

                        try
                        {
                            using (FileStream sourceData = File.OpenRead(iTunesTrack.path))
                            {
                                while (true)
                                {
                                    byte[] bytes = new byte[writeSize];

                                    int length = sourceData.Read(bytes, 0, (int)bytes.Length);
                                    if (length <= 0)
                                    {
                                        break;
                                    }

                                    deviceData.Write(bytes, length, new IntPtr());
                                }
                            }

                            deviceData.Commit(0);
                        }
                        catch
                        {
                            deviceData.Revert();
                            content.Cancel();

                            throw;
                        }

                        deviceObject = albumFolder.GetChildByOriginalFilename(iTunesTrack.filename, true);
                    }

                    iTunesTrack.deviceObject = deviceObject;
                }
                catch (Exception e)
                {
                    foobalator.Log.Write(iTunesTrack.ToString(), e);
                }
            }
        }