Ejemplo n.º 1
0
void WebFetch::finished() {
	// Note that if this functions succeeds, it should deleteLater() itself, as this is a temporary object.
	Q_ASSERT(qobject_cast<QNetworkReply *>(sender()) == qnr);
	qnr->disconnect();
	qnr->deleteLater();

	QUrl url = qnr->request().url();

	if (qnr->error() == QNetworkReply::NoError) {
		const QByteArray &a=qnr->readAll();

		QMap<QString, QString> headers;

		foreach(const QByteArray &headerName, qnr->rawHeaderList()) {
			QString name = fromUtf8(headerName);
			QString value = fromUtf8(qnr->rawHeader(headerName));
			if (! name.isEmpty() && ! value.isEmpty()) {
				headers.insert(name, value);
				if (name == QLatin1String("Geo-Country-Code"))
					g.s.qsRegionalHost = value.toLower() + QLatin1String(".mumble.info");
			}
		}

		emit fetched(a, url, headers);
		deleteLater();
	} else if (url.host() == g.s.qsRegionalHost) {
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);
	}
}
Ejemplo n.º 3
0
 BINLINE const wstring& BFile::getFullName() const {
   if (fname.size() == 0) {
     BFile* pthis = const_cast<BFile*>(this);
     pthis->fname = fromUtf8(fnameUtf8);
   }
   return fname;
 }
bool MainWindow::reconnectToDevice()
{
	_session.reset();
	_device.reset();

	try
	{ _device = mtp::Device::Find(); }
	catch(const mtp::usb::DeviceBusyException &ex)
	{
		QMessageBox::critical(this, tr("Device is busy"), tr("Device is busy, maybe another process is using it. Close other MTP applications and restart Android File Transfer."));
		return false;
	}

	if (!_device)
	{
		QMessageBox::critical(this, tr("No MTP device found"), tr("No MTP device found"));
		return false;
	}

	qDebug() << "device found, opening 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 false;
			}
		}
		catch(const mtp::usb::DeviceNotFoundException &ex)
		{
			qDebug() << "device disconnected, retrying...";
		}
	}
	return true;
}
MtpObjectsModel::ObjectInfo MtpObjectsModel::getInfoById(mtp::u32 objectId) const
{
	mtp::msg::ObjectInfo oi(_session->GetObjectInfo(objectId));
	qint64 size = oi.ObjectCompressedSize;
	if (size == mtp::MaxObjectSize)
		size = _session->GetObjectIntegerProperty(objectId, mtp::ObjectProperty::ObjectSize);
	return ObjectInfo(fromUtf8(oi.Filename), oi.ObjectFormat, size);
}
Ejemplo n.º 6
0
std::wstring fromUtf8(const char *str, int len)
{
    char *buf = (char*)malloc(len + 1);
    if (! buf)
        throw Exception(L"Error allocating memory");
    memcpy(buf, str, len);
    buf[len] = 0;
    std::string s(buf);
    free(buf);
    return fromUtf8(s);
}
Ejemplo n.º 7
0
bool getFileAttributes(const char* path, uint64_t* mtime, uint64_t* size)
{
	WIN32_FILE_ATTRIBUTE_DATA data;

	if (GetFileAttributesExW(fromUtf8(path).c_str(), GetFileExInfoStandard, &data))
	{
		*mtime = combine(data.ftLastWriteTime.dwHighDateTime, data.ftLastWriteTime.dwLowDateTime);
		*size = combine(data.nFileSizeHigh, data.nFileSizeLow);
		return true;
	}

	return false;
}
QVariant MtpStoragesModel::data(const QModelIndex &index, int role) const
{
	const mtp::msg::StorageInfo & si = _storages[index.row()].second;
	switch(role)
	{
	case Qt::DisplayRole:
		{
			const std::string & title = !si.StorageDescription.empty()? si.StorageDescription:  si.VolumeLabel;
			return fromUtf8(title);
		}
	default:
		return QVariant();
	}
}
mtp::msg::ObjectInfoPtr MtpObjectsModel::Row::GetInfo(mtp::SessionPtr session)
{
	if (!_info)
	{
		_info = std::make_shared<mtp::msg::ObjectInfo>();
		try
		{
			*_info = session->GetObjectInfo(ObjectId);
			//qDebug() << fromUtf8(row.Info->Filename);
		}
		catch(const std::exception &ex)
		{ qDebug() << "failed to get object info " << fromUtf8(ex.what()); }
	}
	return _info;
}
Ejemplo n.º 10
0
FILE* openFile(const char* path, const char* mode)
{
	// we need to get a full path to the file for relative paths (normalizePath will always work, isFullPath is an optimization)
	std::wstring wpath = fromUtf8(isFullPath(path) ? path : normalizePath(getCurrentDirectory().c_str(), path).c_str());

	// this makes sure we can use long paths and, more importantly, allows us to open files with special names such as 'aux.c'
	wpath.insert(0, L"\\\\?\\");
	std::replace(wpath.begin(), wpath.end(), '/', '\\');

	// convert file mode, assume short ASCII literal string
	wchar_t wmode[8] = {};
	assert(strlen(mode) < ARRAYSIZE(wmode));
	std::copy(mode, mode + strlen(mode), wmode);

	return _wfopen(wpath.c_str(), wmode);
}
void MainWindow::createDirectory()
{
	CreateDirectoryDialog d(this);
	restoreGeometry("create-directory-dialog", d);
	if (d.exec() && !d.name().isEmpty())
	{
		try
		{ _objectModel->createDirectory(d.name(), mtp::AssociationType::GenericFolder); }
		catch(const std::exception &ex)
		{
			QMessageBox::warning(this, tr("Error"),
				tr("Failed to create new directory:\n") + fromUtf8(ex.what())
			);
		}
	}
	saveGeometry("create-directory-dialog", d);
}
Ejemplo n.º 12
0
bool watchDirectory(const char* path, const std::function<void (const char* name)>& callback)
{
	HANDLE h = CreateFileW(fromUtf8(path).c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

	if (h == INVALID_HANDLE_VALUE)
		return false;

	char buf[65536];
	DWORD bufsize = 0;

	unsigned int filter = FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION;

	while (ReadDirectoryChangesW(h, buf, sizeof(buf), true, filter, &bufsize, NULL, NULL))
	{
		if (bufsize == 0)
			continue;

		size_t offset = 0;

		for (;;)
		{
			assert(offset < bufsize);

			FILE_NOTIFY_INFORMATION* file = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(buf + offset);

			if (file->Action == FILE_ACTION_ADDED || file->Action == FILE_ACTION_MODIFIED || file->Action == FILE_ACTION_REMOVED || file->Action == FILE_ACTION_RENAMED_NEW_NAME)
			{
				std::string fp = toUtf8(file->FileName, file->FileNameLength / sizeof(WCHAR));

				std::replace(fp.begin(), fp.end(), '\\', '/');

				callback(fp.c_str());
			}

			if (!file->NextEntryOffset)
				break;

			offset += file->NextEntryOffset;
		}
	}

	CloseHandle(h);

	return true;
}
Ejemplo n.º 13
0
  BINLINE BFile BFile::createTempFile(const wstring& suffix, const wstring& postfix) {
    char fnameBuf[L_tmpnam] = {0};
    tmpnam(fnameBuf);
    wstring path = fromUtf8(fnameBuf);
    wstring dir, name;
    splitpath(path, dir, name);
    wstringstream wss;
    bool nameEndsWithDot = name.size() != 0 && name.find_last_of(L'.') == name.size()-1;
    bool postfixStartsWithDot = postfix.find(L'.') == 0;
    wss << dir << suffix << name;
    if (nameEndsWithDot && postfixStartsWithDot) {
      wss << postfix.substr(1);
    }
    else {
      wss << postfix;
    }

    return BFile(wss.str());
  }
Ejemplo n.º 14
0
QVariant ConnList::data(const QModelIndex& index, int role) const
{
    const auto col = index.column();
    const auto row = index.row();
    const auto& ui = conns_[row];

    if (role == Qt::DisplayRole)
    {
        switch ((Columns)col)
        {
            case Columns::Status:
                if (ui.state == states::error)
                    return str(ui.error);
                return str(ui.state);

            case Columns::Server: return fromLatin(ui.host);
            case Columns::Data:   return toString(size{ui.down});
            case Columns::Kbs:    return toString(speed{ui.bps});
            case Columns::Desc:   return fromUtf8(ui.desc);
            case Columns::LAST:  Q_ASSERT(false);
        }
    }
    else if (role == Qt::DecorationRole && col == (int)Columns::Status)
    {
        switch (ui.state)
        {
            case states::disconnected:  return QIcon("icons:ico_conn_disconnected.png");
            case states::resolving:     return QIcon("icons:ico_conn_connecting.png");
            case states::connecting:    return QIcon("icons:ico_conn_connecting.png");
            case states::initializing:  return QIcon("icons:ico_conn_connecting.png");
            case states::connected:     return QIcon("icons:ico_conn_connected.png");
            case states::active:        return QIcon("icons:ico_conn_active.png");                                                                
            case states::error:         return QIcon("icons:ico_conn_error.png");                                                                                
        }
    }
    else if (role == Qt::DecorationRole && col == (int)Columns::Server)
    {
       return ui.secure ? 
            QIcon("icons:ico_lock.png") :
            QIcon("icons:ico_unlock.png");
    }
    return QVariant();
}
Ejemplo n.º 15
0
Template::Template(QFile& file, QTextCodec* textCodec)
{
	this->warnings=false;
	sourceName=QFileInfo(file.fileName()).baseName();
	if (file.open(QFile::ReadOnly | QFile::Text))
	{
		QByteArray data=file.readAll();
		file.close();

		if(textCodec)
			append(textCodec->toUnicode(data));
		else
			append(fromUtf8(data));
	}
	else
	{
		qCritical("Template: cannot read from %s, %s",qPrintable(sourceName),qPrintable(file.errorString()));
	}
}
QVariant MtpObjectsModel::data(const QModelIndex &index, int role) const
{
	int row_idx = index.row();
	if (row_idx < 0 || row_idx > _rows.size())
		return QVariant();

	Row &row = _rows[row_idx];

	switch(role)
	{
	case Qt::DisplayRole:
		return fromUtf8(row.GetInfo(_session)->Filename);

	case Qt::ForegroundRole:
		return row.IsAssociation(_session)? QBrush(QColor(0, 0, 128)): QBrush(Qt::black);

	default:
		return QVariant();
	}
}
QModelIndex MtpObjectsModel::findObject(const QString &filename) const
{
	auto idx = std::find_if(_rows.begin(), _rows.end(), [filename, this](Row & row) { return fromUtf8(row.GetInfo(_session)->Filename) == filename; } );
	return idx != _rows.end()? createIndex(std::distance(_rows.begin(), idx), 0): QModelIndex();
}
Ejemplo n.º 18
0
YString::YString(const char * str, size_type n) {
	*this = fromUtf8(std::string(str, n));
}
Ejemplo n.º 19
0
YString::YString(const char * str) {
	*this = fromUtf8(str);
}
Ejemplo n.º 20
0
YString::YString(const std::string & s, YString::size_type pos, YString::size_type n) {
	*this = fromUtf8(std::string(s, pos, n));
}
Ejemplo n.º 21
0
YString YString::fromUtf8(const char * utf8) throw(std::domain_error) {
	return fromUtf8(std::string(utf8));
}
Ejemplo n.º 22
0
YString & YString::operator=(const char * rhs) {
	return *this = fromUtf8(rhs);
}
Ejemplo n.º 23
0
bool renameFile(const char* oldpath, const char* newpath)
{
	return !!MoveFileExW(fromUtf8(oldpath).c_str(), fromUtf8(newpath).c_str(), MOVEFILE_REPLACE_EXISTING);
}
Ejemplo n.º 24
0
bool traverseDirectory(const char* path, const std::function<void (const char* name, uint64_t mtime, uint64_t size)>& callback)
{
	return traverseDirectoryRec(fromUtf8(path).c_str(), "", callback);
}
Ejemplo n.º 25
0
void createDirectory(const char* path)
{
    CreateDirectoryW(fromUtf8(path).c_str(), NULL);
}
Ejemplo n.º 26
0
YString & YString::operator=(const std::string & rhs) {
	return *this = fromUtf8(rhs);
}
Ejemplo n.º 27
0
UnicodeString StringConverter::fromUtf8(const int8_t* buffer, int bufferSize) {
    return fromUtf8(reinterpret_cast<const char*>(buffer), bufferSize);
}
Ejemplo n.º 28
0
bool removeFile(const char* path)
{
	return !!DeleteFileW(fromUtf8(path).c_str());
}