Esempio n. 1
0
        public override void Dispose()
        {
            if (disposed)
            {
                return;
            }

            disposed = true;
            base.Dispose();

            if (mtp_device != null)
            {
                lock (mtp_device) {
                    mtp_device.Dispose();
                }
            }

            mtp_device = null;
        }
        public override void Dispose()
        {
            if (disposed)
            {
                return;
            }

            disposed = true;
            base.Dispose();

            if (mtp_device != null)
            {
                lock (mtp_device) {
                    mtp_device.Dispose();
                }
            }

            ServiceManager.SourceManager.RemoveSource(this);
            mtp_device = null;
        }
Esempio n. 3
0
        public MtpTrackInfo(MtpDevice device, Track file) : base()
        {
            this.file  = file;
            ExternalId = file.FileId;

            AlbumTitle  = file.Album;
            ArtistName  = file.Artist;
            Duration    = TimeSpan.FromMilliseconds(file.Duration);
            Genre       = file.Genre;
            PlayCount   = file.UseCount < 0 ? 0 : (int)file.UseCount;
            Rating      = file.Rating < 0 ? 0 : (file.Rating / 20);
            TrackTitle  = file.Title;
            TrackNumber = file.TrackNumber < 0 ? 0 : (int)file.TrackNumber;
            Year        = file.Year;
            BitRate     = (int)file.Bitrate;
            SampleRate  = (int)file.SampleRate;
            FileSize    = (long)file.FileSize;

            MediaAttributes = TrackMediaAttributes.AudioStream;
            if (device != null)
            {
                SetAttributeIf(file.InFolder(device.PodcastFolder) || Genre == "Podcast", TrackMediaAttributes.Podcast);
                SetAttributeIf(file.InFolder(device.MusicFolder), TrackMediaAttributes.Music);
                SetAttributeIf(file.InFolder(device.VideoFolder), TrackMediaAttributes.VideoStream);
            }

            // This can be implemented if there's enough people requesting it
            CanPlay           = false;
            CanSaveToDatabase = true;
            //NeedSync = false;

            // TODO detect if this is a video file and set the MediaAttributes appropriately?

            /*Profile profile = ServiceManager.Get<MediaProfileManager> ().GetProfileForExtension (System.IO.Path.GetExtension (file.FileName));
             * if (profile != null) {
             *  profile.
             * }*/

            // Set a URI even though it's not actually accessible through normal API's.
            Uri = new SafeUri(GetPathFromMtpTrack(file));
        }
Esempio n. 4
0
        public override void DeviceInitialize(IDevice device, bool force)
        {
            base.DeviceInitialize(device, force);

            var portInfo = device.ResolveUsbPortInfo();

            if (portInfo == null || portInfo.DeviceNumber == 0)
            {
                throw new InvalidDeviceException();
            }

            //int busnum = portInfo.BusNumber;
            int devnum = portInfo.DeviceNumber;

            List <RawMtpDevice> devices = null;

            try {
                devices = MtpDevice.Detect();
            } catch (TypeInitializationException e) {
                Log.Error(e);  // even if this is not a generic-catch block, e.InnerException should probably be checked here
                Log.Error(
                    Catalog.GetString("Error Initializing MTP Device Support"),
                    Catalog.GetString("There was an error initializing MTP device support."), true
                    );
                throw new InvalidDeviceException();
            } catch (Exception e) {
                Log.Error(e);
                //ShowGeneralExceptionDialog (e);
                throw new InvalidDeviceException();
            }

            raw_device = devices.FirstOrDefault(x => x.DeviceNumber == devnum);

            if (raw_device == null)
            {
                throw new InvalidDeviceException();
            }

            Initialize();
        }
Esempio n. 5
0
        protected override bool Claim()
        {
            IDevice device = Device;
            IVolume volume = device as IVolume;

            if (volume != null && volume.IsMounted)
            {
                Log.DebugFormat("MtpSource: attempting to unmount {0}", volume.Name);
                volume.Unmount();
            }

            if (volume != null && volume.IsMounted)
            {
                throw new InvalidDeviceStateException();
            }

            mtp_device = MtpDevice.Connect(raw_device);

            if (mtp_device == null)
            {
                Log.DebugFormat("Failed to connect to mtp device {0}", device.Name);
                throw new InvalidDeviceStateException();
            }

            // libmtp sometimes returns '?????'. I assume this is if the device does
            // not supply a friendly name. In this case show the model name.
            if (string.IsNullOrEmpty(mtp_device.Name) || mtp_device.Name == "?????")
            {
                Name = mtp_device.ModelName;
            }
            else
            {
                Name = mtp_device.Name;
            }

            Initialize(true);

            List <string> mimetypes = new List <string> ();

            foreach (FileType format in mtp_device.GetFileTypes())
            {
                if (format == FileType.JPEG)
                {
                    supports_jpegs = true;
                }
                else
                {
                    string mimetype = MtpDevice.GetMimeTypeFor(format);
                    if (mimetype != null)
                    {
                        mimetypes.Add(mimetype);
                    }
                }
            }
            AcceptableMimeTypes = mimetypes.ToArray();

            AddDapProperty(Catalog.GetString("Required Folder Depth"), 2.ToString());
            AddDapProperty(Catalog.GetString("Serial number"), mtp_device.SerialNumber);
            AddDapProperty(Catalog.GetString("Version"), mtp_device.Version);
            try {
                AddDapProperty(Catalog.GetString("Battery level"), String.Format("{0:0%}", mtp_device.BatteryLevel / 100.0));
            } catch (LibMtpException e) {
                Log.Warning("Unable to get battery level from MTP device", e);
            }

            return(true);
        }
        public override void DeviceInitialize(IDevice device)
        {
            base.DeviceInitialize(device);

            var portInfo = device.ResolveUsbPortInfo();

            if (portInfo == null || portInfo.DeviceNumber == 0)
            {
                throw new InvalidDeviceException();
            }

            //int busnum = portInfo.BusNumber;
            int devnum = portInfo.DeviceNumber;

            List <RawMtpDevice> devices = null;

            try {
                devices = MtpDevice.Detect();
            } catch (TypeInitializationException e) {
                Log.Exception(e);
                Log.Error(
                    Catalog.GetString("Error Initializing MTP Device Support"),
                    Catalog.GetString("There was an error initializing MTP device support."), true
                    );
                throw new InvalidDeviceException();
            } catch (Exception e) {
                Log.Exception(e);
                //ShowGeneralExceptionDialog (e);
                throw new InvalidDeviceException();
            }

            IVolume volume = device as IVolume;

            foreach (var v in devices)
            {
                // Using the HAL hardware backend, HAL says the busnum is 2, but libmtp says it's 0, so disabling that check
                //if (v.BusNumber == busnum && v.DeviceNumber == devnum) {
                if (v.DeviceNumber == devnum)
                {
                    // If gvfs-gphoto has it mounted, unmount it
                    if (volume != null && volume.IsMounted)
                    {
                        volume.Unmount();
                    }

                    for (int i = 5; i > 0 && mtp_device == null; i--)
                    {
                        try {
                            mtp_device = MtpDevice.Connect(v);
                        } catch (Exception) {}

                        if (mtp_device == null)
                        {
                            Log.DebugFormat("Failed to connect to mtp device. Trying {0} more times...", i - 1);
                            Thread.Sleep(2000);
                        }
                    }
                }
            }

            if (mtp_device == null)
            {
                throw new InvalidDeviceException();
            }

            // libmtp sometimes returns '?????'. I assume this is if the device does
            // not supply a friendly name. In this case show the model name.
            if (string.IsNullOrEmpty(mtp_device.Name) || mtp_device.Name == "?????")
            {
                Name = mtp_device.ModelName;
            }
            else
            {
                Name = mtp_device.Name;
            }

            Initialize();

            List <string> mimetypes = new List <string> ();

            foreach (FileType format in mtp_device.GetFileTypes())
            {
                if (format == FileType.JPEG)
                {
                    supports_jpegs = true;
                }
                else
                {
                    string mimetype = MtpDevice.GetMimeTypeFor(format);
                    if (mimetype != null)
                    {
                        mimetypes.Add(mimetype);
                    }
                }
            }
            AcceptableMimeTypes = mimetypes.ToArray();

            AddDapProperty(Catalog.GetString("Serial number"), mtp_device.SerialNumber);
            AddDapProperty(Catalog.GetString("Version"), mtp_device.Version);
            try {
                AddDapProperty(Catalog.GetString("Battery level"), String.Format("{0:0%}", mtp_device.BatteryLevel / 100.0));
            } catch (Exception e) {
                Log.Exception("Unable to get battery level from MTP device", e);
            }
        }
Esempio n. 7
0
        public override void DeviceInitialize(IDevice device)
        {
            base.DeviceInitialize(device);

            if (MediaCapabilities == null || !MediaCapabilities.IsType("mtp"))
            {
                throw new InvalidDeviceException();
            }

            // libmtp only allows us to have one MTP device active
            if (mtp_source != null)
            {
                Log.Information(
                    Catalog.GetString("MTP Support Ignoring Device"),
                    Catalog.GetString("Banshee's MTP audio player support can only handle one device at a time."),
                    true
                    );
                throw new InvalidDeviceException();
            }

            List <MtpDevice> devices = null;

            try {
                devices = MtpDevice.Detect();
            } catch (TypeInitializationException e) {
                Log.Exception(e);
                Log.Error(
                    Catalog.GetString("Error Initializing MTP Device Support"),
                    Catalog.GetString("There was an error intializing MTP device support.  See http://www.banshee-project.org/Guide/DAPs/MTP for more information."), true
                    );
                throw new InvalidDeviceException();
            } catch (Exception e) {
                Log.Exception(e);
                //ShowGeneralExceptionDialog (e);
                throw new InvalidDeviceException();
            }

            if (devices == null || devices.Count == 0)
            {
                Log.Error(
                    Catalog.GetString("Error Finding MTP Device Support"),
                    Catalog.GetString("An MTP device was detected, but Banshee was unable to load support for it."), true
                    );
            }
            else
            {
                string mtp_serial = devices[0].SerialNumber;
                if (!String.IsNullOrEmpty(mtp_serial))
                {
                    if (mtp_serial.Contains(device.Serial))
                    {
                        mtp_device = devices[0];
                        mtp_source = this;
                    }
                    else if (device.Serial.Contains(mtp_serial.TrimStart('0')))
                    {
                        // Special case for sony walkman players; BGO #543938
                        mtp_device = devices[0];
                        mtp_source = this;
                    }
                }

                if (mtp_device == null)
                {
                    Log.Information(
                        Catalog.GetString("MTP Support Ignoring Device"),
                        Catalog.GetString("Banshee's MTP audio player support can only handle one device at a time."),
                        true
                        );
                }
            }

            if (mtp_device == null)
            {
                throw new InvalidDeviceException();
            }

            Name = mtp_device.Name;
            Initialize();

            List <string> mimetypes = new List <string> ();

            foreach (FileType format in mtp_device.GetFileTypes())
            {
                if (format == FileType.JPEG)
                {
                    supports_jpegs = true;
                }
                else
                {
                    string mimetype = MtpDevice.GetMimeTypeFor(format);
                    if (mimetype != null)
                    {
                        mimetypes.Add(mimetype);
                    }
                }
            }
            AcceptableMimeTypes = mimetypes.ToArray();

            AddDapProperty(Catalog.GetString("Serial number"), mtp_device.SerialNumber);
            AddDapProperty(Catalog.GetString("Version"), mtp_device.Version);
            try {
                AddDapProperty(Catalog.GetString("Battery level"), String.Format("{0:0%}", mtp_device.BatteryLevel / 100.0));
            } catch (Exception e) {
                Log.Exception("Unable to get battery level from MTP device", e);
            }
        }