/// <summary>
        /// create a new resource stream in a parent object
        /// </summary>
        /// <param name="deviceContent">unmanged device</param>
        /// <param name="parentObjectId">the parent object</param>
        /// <param name="fileName">file to create</param>
        /// <param name="length">length of the file in bytes</param>
        /// <returns></returns>
        public IStream CreateResourceStream(IPortableDeviceContent deviceContent, string parentObjectId, string fileName, long length)
        {
            IPortableDeviceProperties deviceProperties;

            deviceContent.Properties(out deviceProperties);

            var deviceValues = GetRequiredPropertiesForFile(parentObjectId, fileName, length);

            IStream stream;
            uint    optimalBufferSize = 0;

            deviceContent.CreateObjectWithPropertiesAndData(deviceValues, out stream, ref optimalBufferSize, null);

            return(stream);
        }
예제 #2
0
        /**
         * Copy To Phone
         */
        public void TransferContentToDevice(PortableDeviceFolder parentFolder, string filePath)
        {
            PortableDeviceApiLib.IStream tempStream = null;
            System.Runtime.InteropServices.ComTypes.IStream targetStream = null;
            try
            {
                string fileName = PortableDeviceFolder.last(filePath, "\\");

                // Remove existing remote file, if it exists.
                parentFolder.DeleteFile(this, fileName);

                // make sure that we are not holding on to a file.
                DisconnectConnect();

                string parentObjectId = parentFolder.Id;

                IPortableDeviceContent content = getContents();
                IPortableDeviceValues  values  = GetRequiredPropertiesForContentType(filePath, parentObjectId);

                uint optimalTransferSizeBytes = 0;
                content.CreateObjectWithPropertiesAndData(
                    values,
                    out tempStream,
                    ref optimalTransferSizeBytes,
                    null);

                targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream;

                long length  = new System.IO.FileInfo(filePath).Length;
                long written = 0;
                long lPCt    = 0;

                using (var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    var buffer = new byte[optimalTransferSizeBytes];
                    int bytesRead;
                    do
                    {
                        bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes);
                        IntPtr PCbWritten = IntPtr.Zero;
                        targetStream.Write(buffer, bytesRead, PCbWritten);

                        written += bytesRead;
                        long PCt = length > 0 ? (100 * written) / length : 100;
                        if (PCt != lPCt)
                        {
                            lPCt = PCt;
                            Console.WriteLine("Progress: " + lPCt);
                        }
                    } while (bytesRead > 0);
                }
                targetStream.Commit(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            finally
            {
                if (null != targetStream)
                {
                    Marshal.ReleaseComObject(targetStream);
                }
                if (null != tempStream)
                {
                    Marshal.ReleaseComObject(tempStream);
                }
                Disconnect();
            }
        }
예제 #3
0
        /// <summary>
        /// create a new resource stream in a parent object
        /// </summary>
        /// <param name="deviceContent">unmanged device</param>
        /// <param name="parentObjectId">the parent object</param>
        /// <param name="fileName">file to create</param>
        /// <param name="length">length of the file in bytes</param>
        /// <returns></returns>
        public IStream CreateResourceStream(IPortableDeviceContent deviceContent, string parentObjectId, string fileName, long length)
        {
            IPortableDeviceProperties deviceProperties;
            deviceContent.Properties(out deviceProperties);

            var deviceValues = GetRequiredPropertiesForFile(parentObjectId, fileName, length);

            IStream stream;
            uint optimalBufferSize = 0;
            deviceContent.CreateObjectWithPropertiesAndData(deviceValues, out stream, ref optimalBufferSize, null);

            return stream;
        }
예제 #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);
                }
            }
        }