void MainWindow::showEvent(QShowEvent *)
{
	if (!_device)
	{
		if (!reconnectToDevice())
			return;

		QSettings settings;
		restoreGeometry("main-window", *this);
		restoreState(settings.value("state/main-window").toByteArray());

		qDebug() << "device found, opening session...";
		mtp::SessionPtr session;
		static const int MaxAttempts = 3;
		for(int attempt = 0; attempt < MaxAttempts; ++attempt)
		{
			try
			{
				session = _device->OpenSession(1);
				mtp::msg::DeviceInfo di = session->GetDeviceInfo();
				qDebug() << "device info" << fromUtf8(di.Manufacturer) << " " << fromUtf8(di.Model);
				break;
			}
			catch(const mtp::usb::TimeoutException &ex)
			{
				qDebug() << "timed out getting device info: " << fromUtf8(ex.what()) << ", retrying...";
				if (attempt + 1 == MaxAttempts)
				{
					QMessageBox::critical(this, tr("MTP"), tr("MTP device does not respond"));
					_device.reset();
					return;
				}
			}
			catch(const mtp::usb::DeviceNotFoundException &ex)
			{
				qDebug() << "device disconnected, retrying...";
				if (!reconnectToDevice())
					return;
			}
		}

		_storageModel = new MtpStoragesModel(this);
		while (!_storageModel->update(session))
		{
			int r = QMessageBox::warning(this, tr("No MTP Storages"),
				tr("No MTP storage found, your device might be locked.\nPlease unlock and press Retry to continue or Abort to exit."),
				QMessageBox::Retry | QMessageBox::Abort);
			if (r & QMessageBox::Abort)
			{
				_device.reset();
				return;
			}
		}
		_ui->storageList->setModel(_storageModel);
		_objectModel->setSession(session);
		onStorageChanged(_ui->storageList->currentIndex());
		qDebug() << "session opened, starting";
		_proxyModel->setSourceModel(_objectModel);
	}
}
void MainWindow::showEvent(QShowEvent *)
{
	if (!_device)
	{
		if (!reconnectToDevice())
			return;

		QSettings settings;
		restoreGeometry("main-window", *this);
		restoreState(settings.value("state/main-window").toByteArray());

		_storageModel = new MtpStoragesModel(this);
		while (true)
		{
			try
			{
				if (_storageModel->update(_session))
					break;
			}
			catch(const mtp::usb::DeviceNotFoundException &ex)
			{
				qDebug() << "device disconnected, retrying...";
				if (!reconnectToDevice())
					return;
			}

			int r = QMessageBox::warning(this, tr("No MTP Storages"),
				tr("No MTP storage found, your device might be locked.\nPlease unlock and press Retry to continue or Abort to exit."),
				QMessageBox::Retry | QMessageBox::Abort);

			if (r & QMessageBox::Abort)
			{
				_device.reset();
				return;
			}

			if (!reconnectToDevice())
				return;
		}
		_ui->storageList->setModel(_storageModel);
		_objectModel->setSession(_session);
		onStorageChanged(_ui->storageList->currentIndex());
		qDebug() << "session opened, starting";
		_proxyModel->setSourceModel(_objectModel);
	}
}
Example #3
0
IconStorage::IconStorage(const QString &AStorage, const QString &ASubStorage, QObject *AParent) : FileStorage(AStorage,ASubStorage,AParent)
{
	connect(this,SIGNAL(storageChanged()),SLOT(onStorageChanged()));
}