PDA

View Full Version : Url protocol scheme



#Dragon
15th January 2016, 00:04
Hi, last time I am trying to implement a Url protocol scheme, How it works ? Very simple, as first we need to register a url scheme in our system, to assign url address with our app and then we can send some message using web browser address bar.

Code to register url address




bool Settings::registerProtocol()
{
#ifdef Q_OS_WIN
const QString urlScheme = "myapp";
const QString appPath = QDir::toNativeSeparators(QCoreApplication::applica tionFilePath());
const QString regPath = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes\\") + urlScheme;

QScopedPointer<QSettings> reg(new QSettings(regPath, QSettings::NativeFormat));

reg->setValue(QStringLiteral("Default"), "download manager");
reg->setValue(QStringLiteral("URL Protocol"), QString());

reg->beginGroup(QStringLiteral("DefaultIcon"));
reg->setValue(QStringLiteral("Default"), QString("%1,1").arg(appPath));
reg->endGroup();

reg->beginGroup(QStringLiteral("shell"));
reg->beginGroup(QStringLiteral("open"));
reg->beginGroup(QStringLiteral("command"));
reg->setValue(QStringLiteral("Default"), appPath + QLatin1String(" %1"));

return true;

#elif defined(Q_OS_UNIX)
//TODO
Logger::getInstance()->Info(tr("Cannot integrate with web browser - unsupported system"));
return false;
#endif
return false;
}


Receiving messages


void MainWindow::checkApplicationArguments()
{
for(int i = 0; i < QCoreApplication::arguments().count(); i++)
{
QString arg = QCoreApplication::arguments().at(i);
if(arg.contains("myapp:["))
{
//do something
}
}
}


... and then if we will register it, we can enter a message into web browser address bar and send it to our app, for example:

myapp:[hello]
then program will start and we can receive our message, but I'm wondering and I have completely no idea how to receive a message when app is currently running... normally web browser tries to open a new instance of application...
Have you got some idea how to handle application args when program is open ? I know that this is possible, I have seen it in different app about two years ago but the project is no longer being developed and I can't remember how it worked...

Thank you for time spent reading this topic
Cheers

jefftee
15th January 2016, 06:03
I believe you will have to subclass QNetworkAccessManager to implement your own protocol and then see the doc for QNetworkAccessManager::supportedSchemesImplementat ion().

anda_skoa
15th January 2016, 09:40
You are looking for something like a single instance application.
E.g. http://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection

Cheers,
_

#Dragon
15th January 2016, 09:52
I've got single instance app protection using QSharedMemory

I've attached example app

http://www79.zippyshare.com/v/gW9uCi3f/file.html

Please open it, and click button "Enable integration with web browsers", then close app and enter myapp:[hello] into web browser address bar.
App will start and you will see received message, but how can I get message when app is already running ?

Cheers

anda_skoa
15th January 2016, 11:23
App will start and you will see received message, but how can I get message when app is already running ?

That's what single instance mechanisms usually do.
They detect that there is an already running instance and then pass the argument to the running instance.

Since you implement your own single instance mechanism, it is also up to you to implement instance communication.

Cheers,
_

#Dragon
15th January 2016, 19:25
Thank You again, all is done and now works properly :)