bool MTPDevice::connect(const std::string &dev_file)
{
    if (m_device) {
        logerr("Already connected.\n");
        return true;
    }

    LIBMTP_raw_device_t *raw_device = smtpfs_raw_device_new(dev_file);
    if (!raw_device) {
        logerr("Can not open such device '", dev_file, "'.\n");
        return false;
    }

    bool rval = connect(raw_device);

    // TODO:  Smart pointer with alloc, free hooks.
    smtpfs_raw_device_free(raw_device);

    return rval;
}
bool MTPDevice::connect(const std::string &dev_file)
{
    if (m_device) {
        logerr("Already connected.\n");
        return true;
    }

    LIBMTP_raw_device_t *raw_device = smtpfs_raw_device_new(dev_file);
    if (!raw_device) {
        logerr("Can not open such device '", dev_file, "'.\n");
        return false;
    }

#ifdef HAVE_LIBUSB1
    // Try to reset USB device, so we don't wait until LIBMTP times out.
    // We do this every time we are about to mount a device, but better
    // connect on first try, than wait for 60s timeout.
    smtpfs_reset_device(raw_device);
#endif // HAVE_LIBUSB1

    // Do not output LIBMTP debug stuff
    StreamHelper::off();
    m_device = LIBMTP_Open_Raw_Device_Uncached(raw_device);
    StreamHelper::on();
    smtpfs_raw_device_free(raw_device);

    if (!m_device) {
        LIBMTP_Dump_Errorstack(m_device);
        return false;
    }

    if (!enumStorages())
        return false;

    logmsg("Connected.\n");
    return true;
}