在 C++ 中从范围打开 url 的正确方法是什么?
这是我尝试过的(该函数是从 ActivationResponse 调用的):
static void open_url(std::string const& uri) {
// url_dispatch_send(uri.c_str(), NULL, NULL);
/*
* I found this in the libertine-scope, but I wasn't able to import
* the right libraries
*/
// Qt.openUrlExternally(uri.c_str());
/*
* I found this at https://developer.ubuntu.com/en/phone/platform/guides/url-dispatcher-guide/
* and https://developer.ubuntu.com/en/phone/apps/qml/tutorials/register-your-app-url-dispatcher/
* but it is actually meant to be used in QML, so it isn't recognized.
* I hoped it would work by using QtQuick or QtQml
*/
// QDesktopServices::openUrl(QUrl(QString(uri), QUrl::TolerantMode));
// QDesktopServices::openUrl(QUrl("http://www.google.com", QUrl::TolerantMode));
/*
* I found this at https://developer.ubuntu.com/en/phone/platform/guides/url-dispatcher-guide/
* and http://doc.qt.io/qt-5/qdesktopservices.html#openUrl
* this compiles, but the scope crashes on execution.
*
*/
}
编辑:更具体地说,我评论的三个函数是我找到并尝试过的三个不同选项。它们都不起作用。在函数下的评论中,我试图解释什么不起作用。
函数 open_url 从 ActivationResponse 中调用,如下所示:
Action::Action(unity::scopes::Result const& result,
unity::scopes::ActionMetadata const& metadata,
Action::OpenUriAction open_uri)
: sc::ActivationQueryBase(result, metadata)
, open_uri_(open_uri)
{
}
sc::ActivationResponse
Action::activate()
{
open_uri_(result().uri());
return sc::ActivationResponse(sc::ActivationResponse::Status::NotHandled);
}
答案1
正如我所料,有一个好方法可以做到这一点。
1:在范围类中你必须重写激活方法
unity::scopes::ActivationQueryBase::UPtr NewsScope::activate(const sc::Result &result,
const unity::scopes::ActionMetadata &metadata) {
return unity::scopes::ActivationQueryBase::UPtr(new NewsAction(result, metadata));
}
2:在 unity::scopes::ActivationQueryBase 类(此处为 NewsAction)中定义 activate 方法,例如像这样:
sc::ActivationResponse NewsAction::activate()
{
initScope();
if (openWithoutPreview) {
return sc::ActivationResponse(sc::ActivationResponse::Status::NotHandled);
}
return sc::ActivationResponse(sc::ActivationResponse::Status::ShowPreview);
}
(观察激活响应:NotHandled 告诉范围将其发送到外部调度程序。)
3:最后,如果在searchquery类中创建卡片(CategorisedResult),你必须告诉他们拦截激活。(Result.set_intercept_activation())例如,像这样:
for (const Result &result : results) {
sc::CategorisedResult card(category);
fillCard(card, result);
card.set_intercept_activation();
// Push the result
if (!reply->push(card)) {
// If we fail to push, it means the query has been cancelled.
// So don't continue;
return;
}
}
我在本手册中找到了解决方案(隐藏): https://developer.ubuntu.com/api/scopes/cpp/development/index/