Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'test-uri-handler' into 'master'
Change mimetype to "x-url-handler/{domain}" when handling http/https uris and...

See merge request mer-core/libcontentaction!18
  • Loading branch information
pvuorela committed Feb 24, 2021
2 parents 5502ebe + 83cb1c6 commit 988b5d3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/contentaction.h
Expand Up @@ -58,6 +58,7 @@ class LCA_EXPORT Action
static Action defaultActionForFile(const QUrl& fileUri, const QString& mimeType);
static Action defaultActionForFile(const QList<QUrl>& fileUris, const QString& mimeType);
static Action defaultActionForScheme(const QString& uri);
static Action defaultActionForUrl(const QString& uri);
static Action defaultActionForString(const QString& param);

static QList<Action> actions(const QString& uri);
Expand All @@ -66,6 +67,7 @@ class LCA_EXPORT Action
static QList<Action> actionsForFile(const QUrl& fileUri, const QString& mimeType);
static QList<Action> actionsForFile(const QList<QUrl>& fileUri, const QString& mimeType);
static QList<Action> actionsForScheme(const QString& uri);
static QList<Action> actionsForUrl(const QString& uri);
static QList<Action> actionsForString(const QString& param);

static Action launcherAction(const QString& app, const QStringList& params);
Expand Down
50 changes: 50 additions & 0 deletions src/mime.cpp
Expand Up @@ -517,6 +517,22 @@ QString Internal::mimeForScheme(const QString& uri)
return mime;
}

/// Returns the x-url-handler of the scheme of \a uri. Method can still change as
/// the mime handler currently doesn't support parameters.
QString mimeForUrl(const QString& uri)
{
QString mime;

if (uri.startsWith("http:") || uri.startsWith("https:")) {
int n = uri.indexOf(':');
QString domain = uri.mid(n + 3);
n = domain.indexOf('/');
domain = domain.mid(n);
mime = QString("x-url-handler/") + domain;
}
return mime;
}

/// Returns the pseudo-mimetypes of the \a param string. Mime types are found
/// based on exact matching against regexps in the highlighter configuration.
QStringList Internal::mimeForString(const QString& param)
Expand Down Expand Up @@ -547,6 +563,20 @@ Action Action::defaultActionForScheme(const QString& uri)
return Action();
}

/// Returns the default action for handling the scheme of the passed \a uri.
/// \sa actionsForScheme(). It also converts domain part of uri into
/// "x-url-handler/<domain>" and prioritizes it. Method can still change as
/// the mime handler currently doesn't support parameters.
Action Action::defaultActionForUrl(const QString& uri)
{
QString urlMimeType = mimeForUrl(uri);
QString defApp = findDesktopFile(defaultAppForContentType(urlMimeType));
if (!defApp.isEmpty())
return createAction(defApp, QStringList() << uri);

return defaultActionForScheme(uri);
}

/// Returns all actions handling the scheme of the given \a uri. The uri
/// scheme is mapped to mime types by prefixing it with \c
/// "x-scheme-handler/". For example an email client may declare to handle
Expand All @@ -561,6 +591,26 @@ QList<Action> Action::actionsForScheme(const QString& uri)
return result;
}

/// Returns all actions handling the scheme of the given \a uri. The uri
/// scheme is mapped to mime types by prefixing it with \c
/// "x-scheme-handler/". For example an email client may declare to handle
/// the \c "x-scheme-handler/mailto" mimetype and a browser then just
/// triggers the returned Action to activate a \c mailto: link.
/// Additionally domain part of uri is converted into
/// "x-url-handler/<domain>".
QList<Action> Action::actionsForUrl(const QString& uri)
{
QList<Action> result;

QString urlMimeType = mimeForUrl(uri);
Q_FOREACH (const QString& app, appsForContentType(urlMimeType)) {
result << createAction(findDesktopFile(app), QStringList() << uri);
}

result.append(actionsForScheme(uri));
return result;
}

/// Returns the default action for handling the passed \a param.
/// \sa actionsForString().
Action Action::defaultActionForString(const QString& param)
Expand Down

0 comments on commit 988b5d3

Please sign in to comment.