~ted/unity-scope-click/ual-3-no-change-rebuild

« back to all changes in this revision

Viewing changes to libclickscope/click/preview.cpp

  • Committer: Bileto Bot
  • Author(s): Rodney Dawes
  • Date: 2016-08-08 15:11:40 UTC
  • mfrom: (465.1.8 short-appid)
  • Revision ID: ci-train-bot@canonical.com-20160808151140-g1razio290v424lb
Use shortened appid for click app launching url. (LP: #1594874, #1600593)

Approved by: Charles Kerr, unity-api-1-bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
 
53
53
#include <functional>
54
54
#include <iostream>
 
55
#include <regex>
55
56
#include <sstream>
56
57
 
57
58
#include <click/click-i18n.h>
945
946
            submit_future.get();
946
947
        }
947
948
    }
948
 
    getApplicationUri(manifest, [this, reply, manifest, app_name, &review, userid, force_cache](const std::string& uri) {
949
 
            populateDetails([this, reply, uri, manifest, app_name](const PackageDetails &details){
950
 
                cachedDetails = details;
951
 
                store_department(details);
952
 
                pushPackagePreviewWidgets(cachedWidgets, details, createButtons(uri, manifest));
953
 
            },
954
 
            [this, reply, &review, manifest, userid](const ReviewList& reviewlist,
955
 
                                                     click::Reviews::Error error) {
956
 
                auto reviews = bring_to_front(reviewlist, userid);
957
 
                if (manifest.removable && !cachedDetails.download_url.empty()) {
958
 
                    scopes::PreviewWidgetList review_input;
959
 
                    bool has_reviewed = reviews.size() > 0 && reviews.front().reviewer_username == userid;
960
 
 
961
 
                    Review existing_review;
962
 
                    existing_review.id = 0;
963
 
                    if (has_reviewed) {
964
 
                        existing_review = reviews.front();
965
 
                        reviews.pop_front();
966
 
                    }
967
 
                    review_input.push_back(createRatingWidget(existing_review));
968
 
                    cachedWidgets.push(review_input);
969
 
                    cachedWidgets.layout.appendToColumn(cachedWidgets.layout.singleColumn.column1, review_input);
970
 
                    cachedWidgets.layout.appendToColumn(cachedWidgets.layout.twoColumns.column1, review_input);
971
 
                }
972
 
 
973
 
                if (error == click::Reviews::Error::NoError) {
974
 
                    auto const revs = reviewsWidgets(reviews);
975
 
                    cachedWidgets.push(revs);
976
 
                    cachedWidgets.layout.appendToColumn(cachedWidgets.layout.singleColumn.column1, revs);
977
 
                    cachedWidgets.layout.appendToColumn(cachedWidgets.layout.twoColumns.column1, revs);
978
 
                } else {
979
 
                    qDebug() << "There was an error getting reviews for:" << result["name"].get_string().c_str();
980
 
                }
981
 
                cachedWidgets.flush(reply);
982
 
                reply->finished();
 
949
    populateDetails([this, reply, manifest, app_name](const PackageDetails &details){
 
950
            cachedDetails = details;
 
951
            store_department(details);
 
952
            pushPackagePreviewWidgets(cachedWidgets, details, createButtons(manifest));
 
953
        },
 
954
        [this, reply, &review, manifest, userid](const ReviewList& reviewlist,
 
955
                                                 click::Reviews::Error error) {
 
956
            auto reviews = bring_to_front(reviewlist, userid);
 
957
            if (manifest.removable && !cachedDetails.download_url.empty()) {
 
958
                scopes::PreviewWidgetList review_input;
 
959
                bool has_reviewed = reviews.size() > 0 && reviews.front().reviewer_username == userid;
 
960
 
 
961
                Review existing_review;
 
962
                existing_review.id = 0;
 
963
                if (has_reviewed) {
 
964
                    existing_review = reviews.front();
 
965
                    reviews.pop_front();
 
966
                }
 
967
                review_input.push_back(createRatingWidget(existing_review));
 
968
                cachedWidgets.push(review_input);
 
969
                cachedWidgets.layout.appendToColumn(cachedWidgets.layout.singleColumn.column1, review_input);
 
970
                cachedWidgets.layout.appendToColumn(cachedWidgets.layout.twoColumns.column1, review_input);
 
971
            }
 
972
 
 
973
            if (error == click::Reviews::Error::NoError) {
 
974
                auto const revs = reviewsWidgets(reviews);
 
975
                cachedWidgets.push(revs);
 
976
                cachedWidgets.layout.appendToColumn(cachedWidgets.layout.singleColumn.column1, revs);
 
977
                cachedWidgets.layout.appendToColumn(cachedWidgets.layout.twoColumns.column1, revs);
 
978
            } else {
 
979
                qDebug() << "There was an error getting reviews for:" << result["name"].get_string().c_str();
 
980
            }
 
981
            cachedWidgets.flush(reply);
 
982
            reply->finished();
983
983
        }, force_cache);
984
 
    });
985
984
}
986
985
 
987
 
scopes::PreviewWidgetList InstalledPreview::createButtons(const std::string& uri,
988
 
                                                          const Manifest& manifest)
 
986
scopes::PreviewWidgetList InstalledPreview::createButtons(const Manifest& manifest)
989
987
{
990
988
    scopes::PreviewWidgetList widgets;
991
989
    scopes::PreviewWidget buttons("buttons", "actions");
993
991
 
994
992
    std::string open_label = _("Open");
995
993
 
 
994
    auto uri = getApplicationUri(manifest);
 
995
 
996
996
    if (!manifest.has_any_apps() && manifest.has_any_scopes()) {
997
997
        open_label = _("Search");
998
998
    }
1030
1030
    return widgets;
1031
1031
}
1032
1032
 
1033
 
void InstalledPreview::getApplicationUri(const Manifest& manifest, std::function<void(const std::string&)> callback)
 
1033
std::string InstalledPreview::getApplicationUri(const Manifest& manifest)
1034
1034
{
1035
 
    QString app_url = QString::fromStdString(result.uri());
1036
 
 
1037
 
    // asynchronously get application uri based on app name, if the uri is not application://.
1038
 
    // this can happen if the app was just installed and we have its http uri from the Result.
1039
 
    if (!app_url.startsWith("application:///")) {
1040
 
        const std::string name = result["name"].get_string();
1041
 
 
 
1035
    static std::regex appurl_match{"^(application|appid)://[a-zA-Z\\._/-]+$"};
 
1036
 
 
1037
    if (!std::regex_match(result.uri(), appurl_match)) {
1042
1038
        if (manifest.has_any_apps()) {
1043
 
            qt::core::world::enter_with_task([this, name, callback] ()
1044
 
            {
1045
 
                click::Interface().get_dotdesktop_filename(name,
1046
 
                                              [callback, name] (std::string val, click::InterfaceError error) {
1047
 
                                              std::string uri;
1048
 
                                              if (error == click::InterfaceError::NoError) {
1049
 
                                                  uri = "application:///" + val;
1050
 
                                              } else {
1051
 
                                                  qWarning() << "Can't get .desktop filename for"
1052
 
                                                             << QString::fromStdString(name);
1053
 
                                              }
1054
 
                                              callback(uri);
1055
 
                                     }
1056
 
                    );
1057
 
            });
 
1039
            std::string uri = "appid://" + manifest.name + "/" +
 
1040
                manifest.first_app_name + "/current-user-version";
 
1041
            return uri;
 
1042
        } else if (manifest.has_any_scopes()) {
 
1043
            unity::scopes::CannedQuery cq(manifest.first_scope_id);
 
1044
            auto scope_uri = cq.to_uri();
 
1045
            qDebug() << "Found uri for scope"
 
1046
                     << QString::fromStdString(manifest.first_scope_id)
 
1047
                     << "-" << QString::fromStdString(scope_uri);
 
1048
            return scope_uri;
1058
1049
        } else {
1059
 
            if (manifest.has_any_scopes()) {
1060
 
                unity::scopes::CannedQuery cq(manifest.first_scope_id);
1061
 
                auto scope_uri = cq.to_uri();
1062
 
                qDebug() << "Found uri for scope" << QString::fromStdString(manifest.first_scope_id)
1063
 
                         << "-" << QString::fromStdString(scope_uri);
1064
 
                callback(scope_uri);
1065
 
            }
 
1050
            qWarning() << "Unable to find app or scope URI for:"
 
1051
                       << QString::fromStdString(manifest.name);
 
1052
            return "";
1066
1053
        }
1067
 
    } else {
1068
 
        callback(result.uri());
1069
1054
    }
 
1055
    return result.uri();
1070
1056
}
1071
1057
 
1072
1058
// class InstalledScopePreview