private static IPortableDeviceValues GetRequiredPropertiesForContentType(string folderName, string parentFolderId)
        {
            if (string.IsNullOrEmpty(folderName))
            {
                throw new ArgumentNullException(nameof(folderName));
            }
            if (string.IsNullOrEmpty(parentFolderId))
            {
                throw new ArgumentNullException(nameof(parentFolderId));
            }
            IPortableDeviceValues values =
                new PortableDeviceValues() as IPortableDeviceValues;

            values.SetGuidValue(ref PortableDevicePKeys.WPD_OBJECT_FORMAT, PortableDeviceGuids.WPD_OBJECT_FORMAT_PROPERTIES_ONLY);

            values.SetGuidValue(ref PortableDevicePKeys.WPD_OBJECT_CONTENT_TYPE, PortableDeviceGuids.WPD_CONTENT_TYPE_FOLDER);

            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_PARENT_ID, parentFolderId);

            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, folderName);

            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_NAME, folderName);


            return(values);
        }
Beispiel #2
0
        private static IPortableDeviceValues GetRequiredPropertiesForContentType(
            string fileName,
            string parentFolderId,
            ulong contentLength)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(fileName);
            }
            if (string.IsNullOrEmpty(parentFolderId))
            {
                throw new ArgumentNullException(nameof(parentFolderId));
            }
            IPortableDeviceValues values = new PortableDeviceValues() as IPortableDeviceValues;

            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_PARENT_ID, parentFolderId);

            values.SetUnsignedLargeIntegerValue(ref PortableDevicePKeys.WPD_OBJECT_SIZE, contentLength);

            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, fileName);

            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_NAME, fileName);

            return(values);
        }
Beispiel #3
0
        internal void UploadFile(string fileName, Stream stream)
        {
            IPortableDeviceValues portableDeviceValues = new PortableDeviceValues() as IPortableDeviceValues;

            portableDeviceValues.SetStringValue(ref WPD.OBJECT_PARENT_ID, this.Id);
            portableDeviceValues.SetUnsignedLargeIntegerValue(ref WPD.OBJECT_SIZE, (ulong)stream.Length);
            portableDeviceValues.SetStringValue(ref WPD.OBJECT_ORIGINAL_FILE_NAME, fileName);
            portableDeviceValues.SetStringValue(ref WPD.OBJECT_NAME, fileName);
            // test
            using (PropVariantFacade now = PropVariantFacade.DateTimeToPropVariant(DateTime.Now))
            {
                portableDeviceValues.SetValue(ref WPD.OBJECT_DATE_CREATED, ref now.Value);
                portableDeviceValues.SetValue(ref WPD.OBJECT_DATE_MODIFIED, ref now.Value);

                uint   num  = 0u;
                string text = null;
                this.device.deviceContent.CreateObjectWithPropertiesAndData(portableDeviceValues, out IStream wpdStream, ref num, ref text);

                using (StreamWrapper destinationStream = new StreamWrapper(wpdStream))
                {
                    stream.CopyTo(destinationStream);
                    destinationStream.Flush();
                }
            }
        }
Beispiel #4
0
        private IPortableDeviceValues GetRequiredPropertiesForContentTypeFromStream(MemoryStream inputStream, string fileName, string parentObjectId)
        {
            var values            = new PortableDeviceValues() as IPortableDeviceValues;
            var wpdObjectParentId = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 3
            };

            values.SetStringValue(ref wpdObjectParentId, parentObjectId);
            var wpdObjectSize = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 11
            };

            values.SetUnsignedLargeIntegerValue(wpdObjectSize, (ulong)inputStream.Length);
            var wpdObjectOriginalFileName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 12
            };

            values.SetStringValue(wpdObjectOriginalFileName, fileName);
            var wpdObjectName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 4
            };

            values.SetStringValue(wpdObjectName, fileName);
            return(values);
        }
Beispiel #5
0
        public void SyncPlaylists(iTunesLibrary library)
        {
            if (playlistFolder == null)
            {
                foobalator.Log.WriteLine("Playlists folder could not be found");
                return;
            }

            foreach (var playlist in library.playlists)
            {
                try
                {
                    DeviceObject deviceObject = playlistFolder.GetChildByName(playlist.name);
                    if (deviceObject == null)
                    {
                        foobalator.Log.WriteLine(playlist.ToString());

                        var references = new PortableDevicePropVariantCollection();
                        foreach (var track in playlist.Tracks)
                        {
                            if (track.deviceObject == null)
                            {
                                continue;
                            }

                            //var item = Constants.ToPropVariant(track.deviceObject.id);
                            //var item = new System.Runtime.InteropServices.VariantWrapper(track.deviceObject.id);
                            //references.Add(item);
                        }

                        uint count = 0;
                        references.GetCount(ref count);

                        var values = new PortableDeviceValues();
                        values.SetStringValue(Constants.WPD_OBJECT_PARENT_ID, playlistFolder.id);
                        values.SetStringValue(Constants.WPD_OBJECT_NAME, playlist.name);
                        //values.SetStringValue(Constants.WPD_OBJECT_ORIGINAL_FILE_NAME, playlist.name);
                        values.SetGuidValue(Constants.WPD_OBJECT_CONTENT_TYPE, Constants.WPD_CONTENT_TYPE_PLAYLIST);
                        values.SetUnsignedIntegerValue(Constants.WPD_OBJECT_SIZE, count);
                        //values.SetGuidValue(Constants.WPD_OBJECT_FORMAT, Constants.WPD_OBJECT_FORMAT_WPLPLAYLIST);
                        //values.SetGuidValue(Constants.WPD_OBJECT_FORMAT, Constants.WPD_OBJECT_FORMAT_PROPERTIES_ONLY);
                        //values.SetIPortableDevicePropVariantCollectionValue(Constants.WPD_OBJECT_REFERENCES, references);

                        string playlistDeviceId = null;
                        content.CreateObjectWithPropertiesOnly(values, ref playlistDeviceId);
                        deviceObject = playlistFolder.GetChildByName(playlist.name, true);
                    }

                    playlist.deviceObject = deviceObject;
                }
                catch (Exception e)
                {
                    foobalator.Log.Write(playlist.ToString(), e);
                }
            }
        }
        public static tag_inner_PROPVARIANT ToPropVariant(string value)
        {
            PortableDeviceValues values = new PortableDeviceValues();

            values.SetStringValue(Constants.WPD_OBJECT_ID, value);

            tag_inner_PROPVARIANT result;

            values.GetValue(Constants.WPD_OBJECT_ID, out result);
            return(result);
        }
Beispiel #7
0
        internal void Rename(string newName)
        {
            IPortableDeviceValues portableDeviceValues = new PortableDeviceValues() as IPortableDeviceValues;
            IPortableDeviceValues result;

            portableDeviceValues.SetStringValue(ref WPD.OBJECT_NAME, newName);
            portableDeviceValues.SetStringValue(ref WPD.OBJECT_ORIGINAL_FILE_NAME, newName);
            this.deviceProperties.SetValues(this.Id, portableDeviceValues, out result);
            //ComTrace.WriteObject(result);

            Refresh();
        }
Beispiel #8
0
        internal void SetDateAuthored(DateTime value)
        {
            IPortableDeviceValues portableDeviceValues = new PortableDeviceValues() as IPortableDeviceValues;
            IPortableDeviceValues result;

            using (PropVariantFacade val = PropVariantFacade.DateTimeToPropVariant(value))
            {
                portableDeviceValues.SetValue(ref WPD.OBJECT_DATE_AUTHORED, ref val.Value);
                this.device.deviceProperties.SetValues(this.Id, portableDeviceValues, out result);
                ComTrace.WriteObject(result);
            }

            Refresh();
        }
Beispiel #9
0
        public Device(PortableDeviceManager devices, string id)
        {
            this.id = id;

            uint length = 0;

            devices.GetDeviceDescription(id, null, ref length);

            char[] bytes = new char[length];
            devices.GetDeviceDescription(id, bytes, ref length);
            description = new string(bytes, 0, (int)length - 1);

            foobalator.Log.WriteLine(string.Format("Discovered {0}", description));

            PortableDeviceValues values = new PortableDeviceValues();

            values.SetUnsignedIntegerValue(Constants.WPD_CLIENT_DESIRED_ACCESS, Constants.GENERIC_READ | Constants.GENERIC_WRITE);

            PortableDevice device = new PortableDevice();

            device.Open(id, values);
            device.Content(out content);
            content.Properties(out properties);

            DeviceObject root = new DeviceObject(this, "DEVICE");

            DeviceObject phoneFolder = root.GetChildByName("Phone");

            if (phoneFolder == null)
            {
                return;
            }

            musicFolder = phoneFolder.GetChildByName("Music");
            if (musicFolder == null)
            {
                return;
            }

            playlistFolder = musicFolder.GetChildByName("Playlist");
            if (playlistFolder == null)
            {
                return;
            }
        }
Beispiel #10
0
        internal void UploadFile(string fileName, Stream stream)
        {
            IPortableDeviceValues portableDeviceValues = new PortableDeviceValues() as IPortableDeviceValues;

            portableDeviceValues.SetStringValue(ref WPD.OBJECT_PARENT_ID, this.Id);
            portableDeviceValues.SetUnsignedLargeIntegerValue(ref WPD.OBJECT_SIZE, (ulong)stream.Length);
            portableDeviceValues.SetStringValue(ref WPD.OBJECT_ORIGINAL_FILE_NAME, fileName);
            portableDeviceValues.SetStringValue(ref WPD.OBJECT_NAME, fileName);

            uint   num  = 0u;
            string text = null;

            this.device.deviceContent.CreateObjectWithPropertiesAndData(portableDeviceValues, out PortableDeviceApiLib.IStream wpdStream, ref num, ref text);

            using (StreamWrapper destinationStream = new StreamWrapper(wpdStream))
            {
                stream.CopyTo(destinationStream);
                destinationStream.Flush();
            }
        }
Beispiel #11
0
        private IPortableDeviceValues GetRequiredPropertiesForFolder(string folderName, string parentObjectId)
        {
            var values = new PortableDeviceValues() as IPortableDeviceValues;

            var wpdObjectContentType = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 7
            };

            values.SetGuidValue(ref wpdObjectContentType, CreateContentTypeFolderGuid());

            var wpdObjectParentId = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 3
            };

            values.SetStringValue(ref wpdObjectParentId, parentObjectId);

            var wpdObjectOriginalFileName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 12
            };

            values.SetStringValue(wpdObjectOriginalFileName, Path.GetFileName(folderName));

            var wpdObjectName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 4
            };

            values.SetStringValue(wpdObjectName, Path.GetFileName(folderName));

            return(values);
        }
Beispiel #12
0
        internal bool Rename(string newName)
        {
            IPortableDeviceValues portableDeviceValues = new PortableDeviceValues() as IPortableDeviceValues;
            IPortableDeviceValues result;

            // with OBJECT_NAME does not work for Amazon Kindle Paperwhite
            portableDeviceValues.SetStringValue(ref WPD.OBJECT_ORIGINAL_FILE_NAME, newName);
            this.device.deviceProperties.SetValues(this.Id, portableDeviceValues, out result);
            ComTrace.WriteObject(result);

            if (result.TryGetStringValue(WPD.OBJECT_ORIGINAL_FILE_NAME, out string check))
            {
                if (check == "Error: S_OK")
                {
                    // id can change on rename (e.g. Amazon Kindle Paperwhite) so find new one
                    var newItem = this.parent.GetChildren().FirstOrDefault(i => device.EqualsName(i.Name, newName));
                    this.Id = newItem.Id;

                    Refresh();
                    return(true);
                }
            }
            return(false);
        }
 public virtual extern void SetIPortableDeviceValuesValue([In] ref _tagpropertykey key, [In, MarshalAs(UnmanagedType.Interface)] PortableDeviceValues pValue);
 public virtual extern void GetAt([In] uint dwIndex, [MarshalAs(UnmanagedType.Interface)] out PortableDeviceValues ppValues);
 public virtual extern void Add([In, MarshalAs(UnmanagedType.Interface)] PortableDeviceValues pValues);
Beispiel #16
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);
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// Sets the values of multiple properties.
        /// </summary>
        /// <param name="data">The keys and values of properties to set.</param>
        /// <returns>The new values of the properties. Actual values may not match requested values.</returns>
        public IDictionary <PropertyKey, object> SetProperties(DataFieldInfo[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("data", "Data field array must not be null or empty.");
            }

            IPortableDeviceValues pdv = new PortableDeviceValues();

            for (int i = 0; i < data.Length; i++)
            {
                PropertyKey propKey = data[i].Key;
                object      value   = data[i].Value;
                if (value == null)
                {
                    throw new ArgumentNullException("data", String.Format("Data contains a null value at index {0}", i));
                }

                if (value is string)
                {
                    pdv.SetStringValue(ref propKey, (string)value);
                }
                else if (value is uint)
                {
                    pdv.SetUnsignedIntegerValue(ref propKey, (uint)value);
                }
                else if (value is int)
                {
                    pdv.SetSignedIntegerValue(ref propKey, (int)value);
                }
                else if (value is ulong)
                {
                    pdv.SetUnsignedLargeIntegerValue(ref propKey, (ulong)value);
                }
                else if (value is long)
                {
                    pdv.SetSignedLargeIntegerValue(ref propKey, (long)value);
                }
                else if (value is float || value is double)
                {
                    pdv.SetFloatValue(ref propKey, (float)value);
                }
                else if (value is bool)
                {
                    pdv.SetBoolValue(ref propKey, ((bool)value) ? 1 : 0);
                }
                else if (value is Guid)
                {
                    Guid guid = (Guid)value;
                    pdv.SetGuidValue(ref propKey, ref guid);
                }
                else if (value is byte[])
                {
                    byte[] buffer = (byte[])value;
                    pdv.SetBufferValue(ref propKey, buffer, (uint)buffer.Length);
                }
                else
                {
                    pdv.SetIUnknownValue(ref propKey, value);
                }
            }

            IPortableDeviceValues pdv2 = null;

            _iSensor.SetProperties(pdv, out pdv2);

            Dictionary <PropertyKey, object> results = new Dictionary <PropertyKey, object>();

            if (pdv2 == null)
            {
                return(results);
            }

            uint count = 0;

            pdv2.GetCount(ref count);

            for (uint i = 0; i < count; i++)
            {
                PropertyKey propKey = new PropertyKey();
                PROPVARIANT propVal = new PROPVARIANT();
                try
                {
                    pdv2.GetAt(i, ref propKey, out propVal);
                    results.Add(propKey, propVal.Value);
                }
                finally
                {
                    propVal.Clear();
                }
            }

            return(results);
        }
        /// <summary>
        /// Sets the values of multiple properties.
        /// </summary>
        /// <param name="data">An array that contains the property keys and values.</param>
        /// <returns>A dictionary of the new values for the properties. Actual values may not match the requested values.</returns>
        public IDictionary <PropertyKey, object> SetProperties(DataFieldInfo[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentException("LocalizedMessages.SensorEmptyData", "data");
            }

            IPortableDeviceValues pdv = new PortableDeviceValues();

            for (int i = 0; i < data.Length; i++)
            {
                PropertyKey propKey = data[i].Key;
                object      value   = data[i].Value;
                if (value == null)
                {
                    throw new ArgumentException(
                              string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                            "LocalizedMessages.SensorNullValueAtIndex", i),
                              "data");
                }

                try
                {
                    // new PropVariant will throw an ArgumentException if the value can
                    // not be converted to an appropriate PropVariant.
                    using (PropVariant pv = PropVariant.FromObject(value))
                    {
                        pdv.SetValue(ref propKey, pv);
                    }
                }
                catch (ArgumentException)
                {
                    byte[] buffer;
                    if (value is Guid)
                    {
                        Guid guid = (Guid)value;
                        pdv.SetGuidValue(ref propKey, ref guid);
                    }
                    else if ((buffer = value as byte[]) != null)
                    {
                        pdv.SetBufferValue(ref propKey, buffer, (uint)buffer.Length);
                    }
                    else
                    {
                        pdv.SetIUnknownValue(ref propKey, value);
                    }
                }
            }

            Dictionary <PropertyKey, object> results = new Dictionary <PropertyKey, object>();
            IPortableDeviceValues            pdv2    = null;
            HResult hr = nativeISensor.SetProperties(pdv, out pdv2);

            if (hr == HResult.Ok)
            {
                try
                {
                    uint count = 0;
                    pdv2.GetCount(ref count);

                    for (uint i = 0; i < count; i++)
                    {
                        PropertyKey propKey = new PropertyKey();
                        using (PropVariant propVal = new PropVariant())
                        {
                            pdv2.GetAt(i, ref propKey, propVal);
                            results.Add(propKey, propVal.Value);
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(pdv2);
                    pdv2 = null;
                }
            }

            return(results);
        }