Ticket #12793: 0001-HaikuDepot-populate-the-web-app-repository-code-into.patch

File 0001-HaikuDepot-populate-the-web-app-repository-code-into.patch, 28.6 KB (added by apl-haiku, 8 years ago)
  • src/apps/haikudepot/model/Model.cpp

    From f4623ea684a747fe175683c7fb57682840ae85e3 Mon Sep 17 00:00:00 2001
    From: Andrew Lindesay <apl@lindesay.co.nz>
    Date: Mon, 5 Sep 2016 23:07:50 +1200
    Subject: [PATCH] HaikuDepot: populate the web app repository code into data
     structures and use it when making api calls to the web app
    
    ---
     src/apps/haikudepot/model/Model.cpp           | 263 ++++++++++++++++++--------
     src/apps/haikudepot/model/Model.h             |   5 +
     src/apps/haikudepot/model/PackageInfo.cpp     |  33 +++-
     src/apps/haikudepot/model/PackageInfo.h       |  16 ++
     src/apps/haikudepot/model/WebAppInterface.cpp |  49 +++--
     src/apps/haikudepot/model/WebAppInterface.h   |   9 +
     src/apps/haikudepot/ui/MainWindow.cpp         |  26 ++-
     src/apps/haikudepot/ui/RatePackageWindow.cpp  | 128 ++++++++-----
     8 files changed, 381 insertions(+), 148 deletions(-)
    
    diff --git a/src/apps/haikudepot/model/Model.cpp b/src/apps/haikudepot/model/Model.cpp
    index 289e5e9..2fed66c 100644
    a b  
    11/*
    22 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
    33 * Copyright 2014, Axel Dörfler <axeld@pinc-software.de>.
     4 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    45 * All rights reserved. Distributed under the terms of the MIT License.
    56 */
    67
    Model::AddDepot(const DepotInfo& depot)  
    464465bool
    465466Model::HasDepot(const BString& name) const
    466467{
     468    return NULL != DepotForName(name);
     469}
     470
     471
     472const DepotInfo*
     473Model::DepotForName(const BString& name) const
     474{
    467475    for (int32 i = fDepots.CountItems() - 1; i >= 0; i--) {
    468476        if (fDepots.ItemAtFast(i).Name() == name)
    469             return true;
     477            return &fDepots.ItemAtFast(i);
    470478    }
    471     return false;
     479    return NULL;
    472480}
    473481
    474482
    Model::SetAuthorization(const BString& username, const BString& password,  
    827835
    828836
    829837void
     838Model::PopulateWebAppRepositoryCode(DepotInfo& depotInfo)
     839{
     840    if (depotInfo.BaseURL().Length() > 0) {
     841
     842        BMessage repositoriesEnvelope;
     843        BMessage result;
     844        double total;
     845        StringList repositorySourceBaseURLs;
     846
     847        repositorySourceBaseURLs.Add(depotInfo.BaseURL());
     848
     849        // TODO; better API call handling around errors.
     850
     851        if (fWebAppInterface.RetrieveRepositoriesForSourceBaseURLs(
     852            repositorySourceBaseURLs, repositoriesEnvelope) == B_OK
     853            && repositoriesEnvelope.FindMessage("result", &result) == B_OK
     854            && result.FindDouble("total", &total) == B_OK) {
     855
     856            if ((int64) total > 0) {
     857                BMessage repositories;
     858
     859                if (result.FindMessage("items", &repositories) == B_OK) {
     860                    int32 index = 0;
     861                    while (true) {
     862                        BString name;
     863                        name << index++;
     864                        BMessage repository;
     865                        if (repositories.FindMessage(name, &repository) != B_OK)
     866                            break;
     867
     868                        BString repositoryCode;
     869                        if (repository.FindString("code", &repositoryCode) != B_OK)
     870                            continue;
     871
     872                        depotInfo.SetWebAppRepositoryCode(repositoryCode);
     873
     874                        printf("did assign web app repository code '%s' to local depot '%s'\n",
     875                            depotInfo.WebAppRepositoryCode().String(),
     876                            depotInfo.Name().String());
     877                    }
     878                } else {
     879                    printf("unable to find the 'items' in the api response for local depot '%s'\n",
     880                        depotInfo.Name().String());
     881                }
     882            } else {
     883                printf("unable to find a repository code for '%s'\n",
     884                    depotInfo.BaseURL().String());
     885            }
     886        } else {
     887            printf("unexpected result obtaining repository code for '%s'\n",
     888                depotInfo.BaseURL().String());
     889        }
     890    } else {
     891        printf("missing base url for depot info %s --> will not obtain web app repository code\n",
     892            depotInfo.Name().String());
     893    }
     894}
     895
     896
     897void
    830898Model::_UpdateIsFeaturedFilter()
    831899{
    832900    if (fShowFeaturedPackages && SearchTerms().IsEmpty())
    Model::_PopulatePackageInfos(PackageList& packages, bool fromCacheOnly,  
    9871055
    9881056    StringList packageNames;
    9891057    StringList packageArchitectures;
     1058    StringList repositoryCodes;
     1059
    9901060    for (int i = 0; i < packages.CountItems(); i++) {
    9911061        const PackageInfoRef& package = packages.ItemAtFast(i);
    9921062        packageNames.Add(package->Name());
    993         packageArchitectures.Add(package->Architecture());
    994     }
    9951063
    996     status_t status = fWebAppInterface.RetrieveBulkPackageInfo(packageNames,
    997         packageArchitectures, info);
    998     if (status == B_OK) {
    999         // Parse message
    1000 //      info.PrintToStream();
    1001         BMessage result;
    1002         BMessage pkgs;
    1003         if (info.FindMessage("result", &result) == B_OK
    1004             && result.FindMessage("pkgs", &pkgs) == B_OK) {
    1005             int32 index = 0;
    1006             while (true) {
    1007                 if (fStopPopulatingAllPackages)
    1008                     return;
    1009                 BString name;
    1010                 name << index++;
    1011                 BMessage pkgInfo;
    1012                 if (pkgs.FindMessage(name, &pkgInfo) != B_OK)
    1013                     break;
     1064        if (!packageArchitectures.Contains(package->Architecture()))
     1065            packageArchitectures.Add(package->Architecture());
    10141066
    1015                 BString pkgName;
    1016                 if (pkgInfo.FindString("name", &pkgName) != B_OK)
    1017                     continue;
    1018 
    1019                 // Find the PackageInfoRef
    1020                 bool found = false;
    1021                 for (int i = 0; i < packages.CountItems(); i++) {
    1022                     const PackageInfoRef& package = packages.ItemAtFast(i);
    1023                     if (pkgName == package->Name()) {
    1024                         _PopulatePackageInfo(package, pkgInfo);
    1025                         if (_HasNativeIcon(pkgInfo))
    1026                             packagesWithIcons.Add(package);
    1027 
    1028                         // Store in cache
    1029                         BFile file;
    1030                         BPath path;
    1031                         BString fileName(package->Name());
    1032                         fileName << ".info";
    1033                         if (_GetCacheFile(path, file, B_USER_CACHE_DIRECTORY,
    1034                                 "HaikuDepot", fileName,
    1035                                 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE)) {
    1036                             pkgInfo.Flatten(&file);
    1037                         }
     1067        const DepotInfo *depot = DepotForName(package->DepotName());
    10381068
    1039                         packages.Remove(i);
    1040                         found = true;
     1069        if (NULL != depot) {
     1070            BString repositoryCode = depot->WebAppRepositoryCode();
     1071
     1072            if (0 != repositoryCode.Length()
     1073                && !repositoryCodes.Contains(repositoryCode)) {
     1074                repositoryCodes.Add(repositoryCode);
     1075            }
     1076        }
     1077    }
     1078
     1079    if (0 != repositoryCodes.CountItems()) {
     1080        status_t status = fWebAppInterface.RetrieveBulkPackageInfo(packageNames,
     1081            packageArchitectures, repositoryCodes, info);
     1082
     1083        if (status == B_OK) {
     1084            // Parse message
     1085    //      info.PrintToStream();
     1086            BMessage result;
     1087            BMessage pkgs;
     1088            if (info.FindMessage("result", &result) == B_OK
     1089                && result.FindMessage("pkgs", &pkgs) == B_OK) {
     1090                int32 index = 0;
     1091                while (true) {
     1092                    if (fStopPopulatingAllPackages)
     1093                        return;
     1094                    BString name;
     1095                    name << index++;
     1096                    BMessage pkgInfo;
     1097                    if (pkgs.FindMessage(name, &pkgInfo) != B_OK)
    10411098                        break;
     1099
     1100                    BString pkgName;
     1101                    if (pkgInfo.FindString("name", &pkgName) != B_OK)
     1102                        continue;
     1103
     1104                    // Find the PackageInfoRef
     1105                    bool found = false;
     1106                    for (int i = 0; i < packages.CountItems(); i++) {
     1107                        const PackageInfoRef& package = packages.ItemAtFast(i);
     1108                        if (pkgName == package->Name()) {
     1109                            _PopulatePackageInfo(package, pkgInfo);
     1110                            if (_HasNativeIcon(pkgInfo))
     1111                                packagesWithIcons.Add(package);
     1112
     1113                            // Store in cache
     1114                            BFile file;
     1115                            BPath path;
     1116                            BString fileName(package->Name());
     1117                            fileName << ".info";
     1118                            if (_GetCacheFile(path, file, B_USER_CACHE_DIRECTORY,
     1119                                    "HaikuDepot", fileName,
     1120                                    B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE)) {
     1121                                pkgInfo.Flatten(&file);
     1122                            }
     1123
     1124                            packages.Remove(i);
     1125                            found = true;
     1126                            break;
     1127                        }
    10421128                    }
     1129                    if (!found)
     1130                        printf("No matching package for %s\n", pkgName.String());
    10431131                }
    1044                 if (!found)
    1045                     printf("No matching package for %s\n", pkgName.String());
    10461132            }
    1047         }
    1048     } else {
    1049         printf("Error sending request: %s\n", strerror(status));
    1050         int count = packages.CountItems();
    1051         if (count >= 4) {
    1052             // Retry in smaller chunks
    1053             PackageList firstHalf;
    1054             PackageList secondHalf;
    1055             for (int i = 0; i < count / 2; i++)
    1056                 firstHalf.Add(packages.ItemAtFast(i));
    1057             for (int i = count / 2; i < count; i++)
    1058                 secondHalf.Add(packages.ItemAtFast(i));
    1059             packages.Clear();
    1060             _PopulatePackageInfos(firstHalf, fromCacheOnly, packagesWithIcons);
    1061             _PopulatePackageInfos(secondHalf, fromCacheOnly, packagesWithIcons);
    10621133        } else {
    1063             while (packages.CountItems() > 0) {
    1064                 const PackageInfoRef& package = packages.ItemAtFast(0);
    1065                 _PopulatePackageInfo(package, fromCacheOnly);
    1066                 packages.Remove(0);
     1134            printf("Error sending request: %s\n", strerror(status));
     1135            int count = packages.CountItems();
     1136            if (count >= 4) {
     1137                // Retry in smaller chunks
     1138                PackageList firstHalf;
     1139                PackageList secondHalf;
     1140                for (int i = 0; i < count / 2; i++)
     1141                    firstHalf.Add(packages.ItemAtFast(i));
     1142                for (int i = count / 2; i < count; i++)
     1143                    secondHalf.Add(packages.ItemAtFast(i));
     1144                packages.Clear();
     1145                _PopulatePackageInfos(firstHalf, fromCacheOnly, packagesWithIcons);
     1146                _PopulatePackageInfos(secondHalf, fromCacheOnly, packagesWithIcons);
     1147            } else {
     1148                while (packages.CountItems() > 0) {
     1149                    const PackageInfoRef& package = packages.ItemAtFast(0);
     1150                    _PopulatePackageInfo(package, fromCacheOnly);
     1151                    packages.Remove(0);
     1152                }
    10671153            }
    10681154        }
    10691155    }
    Model::_PopulatePackageInfo(const PackageInfoRef& package, bool fromCacheOnly)  
    10831169    if (fromCacheOnly)
    10841170        return;
    10851171
    1086     // Retrieve info from web-app
    1087     BMessage info;
     1172    BString repositoryCode;
     1173    const DepotInfo* depot = DepotForName(package->DepotName());
    10881174
    1089     status_t status = fWebAppInterface.RetrievePackageInfo(package->Name(),
    1090         package->Architecture(), info);
    1091     if (status == B_OK) {
    1092         // Parse message
    1093 //      info.PrintToStream();
    1094         BMessage result;
    1095         if (info.FindMessage("result", &result) == B_OK)
    1096             _PopulatePackageInfo(package, result);
     1175    if (NULL != depot) {
     1176        repositoryCode = depot->WebAppRepositoryCode();
     1177
     1178        if (repositoryCode.Length() > 0) {
     1179            // Retrieve info from web-app
     1180            BMessage info;
     1181
     1182            status_t status = fWebAppInterface.RetrievePackageInfo(
     1183                package->Name(), package->Architecture(), repositoryCode,
     1184                info);
     1185
     1186            if (status == B_OK) {
     1187                // Parse message
     1188        //      info.PrintToStream();
     1189                BMessage result;
     1190                if (info.FindMessage("result", &result) == B_OK)
     1191                    _PopulatePackageInfo(package, result);
     1192            }
     1193        }
     1194        else {
     1195            printf("unable to find the web app repository code for depot; %s\n",
     1196                package->DepotName().String());
     1197        }
     1198    } else {
     1199        printf("no depot for name; %s\n",
     1200            package->DepotName().String());
    10971201    }
    10981202}
    10991203
    Model::_PopulatePackageInfo(const PackageInfoRef& package, const BMessage& data)  
    11891293
    11901294        append_word_list(foundInfo, "prominence");
    11911295    }
    1192    
     1296
    11931297    BString changelog;
    11941298    if (data.FindString("pkgChangelogContent", &changelog) == B_OK) {
    11951299        package->SetChangelog(changelog);
    Model::_NotifyAuthorizationChanged()  
    13801484            listener->AuthorizationChanged();
    13811485    }
    13821486}
    1383 
  • src/apps/haikudepot/model/Model.h

    diff --git a/src/apps/haikudepot/model/Model.h b/src/apps/haikudepot/model/Model.h
    index 36454a9..253cc43 100644
    a b  
    11/*
    22 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
     3 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
    56#ifndef MODEL_H
    public:  
    5657            bool                HasDepot(const BString& name) const;
    5758            const DepotList&    Depots() const
    5859                                    { return fDepots; }
     60            const DepotInfo*    DepotForName(const BString& name) const;
    5961            bool                SyncDepot(const DepotInfo& depot);
    6062
    6163            void                Clear();
    public:  
    115117            bool                ShowDevelopPackages() const
    116118                                    { return fShowDevelopPackages; }
    117119
     120            void                PopulateWebAppRepositoryCode(
     121                                    DepotInfo& depotInfo);
     122
    118123            // Retrieve package information
    119124    static  const uint32        POPULATE_CACHED_RATING  = 1 << 0;
    120125    static  const uint32        POPULATE_CACHED_ICON    = 1 << 1;
  • src/apps/haikudepot/model/PackageInfo.cpp

    diff --git a/src/apps/haikudepot/model/PackageInfo.cpp b/src/apps/haikudepot/model/PackageInfo.cpp
    index 4befa8f..973daf8 100644
    a b  
    11/*
    22 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
    33 * Copyright 2013, Rene Gollent <rene@gollent.com>.
     4 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    45 * All rights reserved. Distributed under the terms of the MIT License.
    56 */
    67
    PackageInfo::SetSize(int64 size)  
    931932}
    932933
    933934
     935void
     936PackageInfo::SetDepotName(const BString& depotName)
     937{
     938    fDepotName = depotName;
     939}
     940
     941
    934942bool
    935943PackageInfo::AddListener(const PackageInfoListenerRef& listener)
    936944{
    PackageInfo::_NotifyListeners(uint32 changes)  
    982990DepotInfo::DepotInfo()
    983991    :
    984992    fName(),
    985     fPackages()
     993    fPackages(),
     994    fWebAppRepositoryCode()
    986995{
    987996}
    988997
    DepotInfo::DepotInfo()  
    990999DepotInfo::DepotInfo(const BString& name)
    9911000    :
    9921001    fName(name),
    993     fPackages()
     1002    fPackages(),
     1003    fWebAppRepositoryCode()
    9941004{
    9951005}
    9961006
    DepotInfo::DepotInfo(const BString& name)  
    9981008DepotInfo::DepotInfo(const DepotInfo& other)
    9991009    :
    10001010    fName(other.fName),
    1001     fPackages(other.fPackages)
     1011    fPackages(other.fPackages),
     1012    fWebAppRepositoryCode(other.fWebAppRepositoryCode),
     1013    fBaseURL(other.fBaseURL)
    10021014{
    10031015}
    10041016
    DepotInfo::operator=(const DepotInfo& other)  
    10081020{
    10091021    fName = other.fName;
    10101022    fPackages = other.fPackages;
     1023    fBaseURL = other.fBaseURL;
     1024    fWebAppRepositoryCode = other.fWebAppRepositoryCode;
    10111025    return *this;
    10121026}
    10131027
    DepotInfo::SyncPackages(const PackageList& otherPackages)  
    10711085    }
    10721086}
    10731087
     1088
     1089void
     1090DepotInfo::SetBaseURL(const BString& baseURL)
     1091{
     1092    fBaseURL = baseURL;
     1093}
     1094
     1095
     1096void
     1097DepotInfo::SetWebAppRepositoryCode(const BString& code)
     1098{
     1099    fWebAppRepositoryCode = code;
     1100}
  • src/apps/haikudepot/model/PackageInfo.h

    diff --git a/src/apps/haikudepot/model/PackageInfo.h b/src/apps/haikudepot/model/PackageInfo.h
    index 61480fa..c611e0b 100644
    a b  
    11/*
    22 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
     3 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
    56#ifndef PACKAGE_INFO_H
    public:  
    340341            int64               Size() const
    341342                                    { return fSize; }
    342343
     344            void                SetDepotName(const BString& depotName);
     345            const BString&      DepotName() const
     346                                    { return fDepotName; }
     347
    343348            bool                AddListener(
    344349                                    const PackageInfoListenerRef& listener);
    345350            void                RemoveListener(
    private:  
    376381            BString             fLocalFilePath;
    377382            BString             fFileName;
    378383            int64               fSize;
     384            BString             fDepotName;
    379385
    380386    static  BitmapRef           sDefaultIcon;
    381387};
    public:  
    407413
    408414            void                SyncPackages(const PackageList& packages);
    409415
     416            void                SetBaseURL(const BString& baseURL);
     417            const BString&      BaseURL() const
     418                                    { return fBaseURL; }
     419
     420            void                SetWebAppRepositoryCode(const BString& code);
     421            const BString&      WebAppRepositoryCode() const
     422                                    { return fWebAppRepositoryCode; }
     423
    410424private:
    411425            BString             fName;
    412426            PackageList         fPackages;
     427            BString             fWebAppRepositoryCode;
     428            BString             fBaseURL;
    413429};
    414430
    415431
  • src/apps/haikudepot/model/WebAppInterface.cpp

    diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp b/src/apps/haikudepot/model/WebAppInterface.cpp
    index 3469ec1..1fabbe5 100644
    a b  
    11/*
    22 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
     3 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
    56
     
    2627#include "PackageInfo.h"
    2728
    2829
    29 #define CODE_REPOSITORY_DEFAULT "haikuports"
    3030#define BASEURL_DEFAULT "https://depot.haiku-os.org"
    3131#define USERAGENT_FALLBACK_VERSION "0.0.0"
    3232
    WebAppInterface::SetPreferredLanguage(const BString& language)  
    437437
    438438
    439439status_t
     440WebAppInterface::RetrieveRepositoriesForSourceBaseURLs(
     441    const StringList& repositorySourceBaseURLs,
     442    BMessage& message)
     443{
     444    BString jsonString = JsonBuilder()
     445        .AddValue("jsonrpc", "2.0")
     446        .AddValue("id", ++fRequestIndex)
     447        .AddValue("method", "searchRepositories")
     448        .AddArray("params")
     449            .AddObject()
     450                .AddArray("repositorySourceSearchUrls")
     451                    .AddStrings(repositorySourceBaseURLs)
     452                .EndArray()
     453                .AddValue("offset", 0)
     454                .AddValue("limit", 1000) // effectively a safety limit
     455            .EndObject()
     456        .EndArray()
     457    .End();
     458
     459    return _SendJsonRequest("repository", jsonString, 0, message);
     460}
     461
     462
     463status_t
    440464WebAppInterface::RetrievePackageInfo(const BString& packageName,
    441     const BString& architecture, BMessage& message)
     465    const BString& architecture, const BString& repositoryCode,
     466    BMessage& message)
    442467{
    443468    BString jsonString = JsonBuilder()
    444469        .AddValue("jsonrpc", "2.0")
    WebAppInterface::RetrievePackageInfo(const BString& packageName,  
    449474                .AddValue("name", packageName)
    450475                .AddValue("architectureCode", architecture)
    451476                .AddValue("naturalLanguageCode", fLanguage)
    452                 .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT)
     477                .AddValue("repositoryCode", repositoryCode)
    453478                .AddValue("versionType", "NONE")
    454479            .EndObject()
    455480        .EndArray()
    WebAppInterface::RetrievePackageInfo(const BString& packageName,  
    461486
    462487status_t
    463488WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames,
    464     const StringList& packageArchitectures, BMessage& message)
     489    const StringList& packageArchitectures,
     490    const StringList& repositoryCodes, BMessage& message)
    465491{
    466492    BString jsonString = JsonBuilder()
    467493        .AddValue("jsonrpc", "2.0")
    WebAppInterface::RetrieveBulkPackageInfo(const StringList& packageNames,  
    476502                    .AddStrings(packageArchitectures)
    477503                .EndArray()
    478504                .AddArray("repositoryCodes")
    479                     .AddItem(CODE_REPOSITORY_DEFAULT)
     505                    .AddStrings(repositoryCodes)
    480506                .EndArray()
    481507                .AddValue("naturalLanguageCode", fLanguage)
    482508                .AddValue("versionType", "LATEST")
    WebAppInterface::RetrieveUserRatings(const BString& packageName,  
    552578status_t
    553579WebAppInterface::RetrieveUserRating(const BString& packageName,
    554580    const BPackageVersion& version, const BString& architecture,
    555     const BString& username, BMessage& message)
     581    const BString &repositoryCode, const BString& username,
     582    BMessage& message)
    556583{
    557584    BString jsonString = JsonBuilder()
    558585        .AddValue("jsonrpc", "2.0")
    WebAppInterface::RetrieveUserRating(const BString& packageName,  
    568595                .AddValue("pkgVersionMicro", version.Micro(), true)
    569596                .AddValue("pkgVersionPreRelease", version.PreRelease(), true)
    570597                .AddValue("pkgVersionRevision", (int)version.Revision())
    571                 .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT)
     598                .AddValue("repositoryCode", repositoryCode)
    572599            .EndObject()
    573600        .EndArray()
    574601    .End();
    WebAppInterface::RetrieveUserRating(const BString& packageName,  
    579606
    580607status_t
    581608WebAppInterface::CreateUserRating(const BString& packageName,
    582     const BString& architecture, const BString& languageCode,
    583     const BString& comment, const BString& stability, int rating,
    584     BMessage& message)
     609    const BString& architecture, const BString& repositoryCode,
     610    const BString& languageCode, const BString& comment,
     611    const BString& stability, int rating, BMessage& message)
    585612{
    586613    BString jsonString = JsonBuilder()
    587614        .AddValue("jsonrpc", "2.0")
    WebAppInterface::CreateUserRating(const BString& packageName,  
    596623                .AddValue("rating", rating)
    597624                .AddValue("userRatingStabilityCode", stability, true)
    598625                .AddValue("comment", comment)
    599                 .AddValue("repositoryCode", CODE_REPOSITORY_DEFAULT)
     626                .AddValue("repositoryCode", repositoryCode)
    600627                .AddValue("naturalLanguageCode", languageCode)
    601628            .EndObject()
    602629        .EndArray()
  • src/apps/haikudepot/model/WebAppInterface.h

    diff --git a/src/apps/haikudepot/model/WebAppInterface.h b/src/apps/haikudepot/model/WebAppInterface.h
    index f5fffec..04cf67f 100644
    a b  
    11/*
    22 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
     3 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
    56#ifndef WEB_APP_INTERFACE_H
    public:  
    3738            void                SetPreferredLanguage(const BString& language);
    3839            void                SetArchitecture(const BString& architecture);
    3940
     41            status_t            RetrieveRepositoriesForSourceBaseURLs(
     42                                    const StringList& repositorySourceBaseURL,
     43                                    BMessage& message);
     44
    4045            status_t            RetrievePackageInfo(
    4146                                    const BString& packageName,
    4247                                    const BString& architecture,
     48                                    const BString& repositoryCode,
    4349                                    BMessage& message);
    4450
    4551            status_t            RetrieveBulkPackageInfo(
    4652                                    const StringList& packageNames,
    4753                                    const StringList& packageArchitectures,
     54                                    const StringList& repositoryCodes,
    4855                                    BMessage& message);
    4956
    5057            status_t            RetrievePackageIcon(
    public:  
    6168                                    const BString& packageName,
    6269                                    const BPackageVersion& version,
    6370                                    const BString& architecture,
     71                                    const BString& repositoryCode,
    6472                                    const BString& username,
    6573                                    BMessage& message);
    6674
    6775            status_t            CreateUserRating(
    6876                                    const BString& packageName,
    6977                                    const BString& architecture,
     78                                    const BString& repositoryCode,
    7079                                    const BString& languageCode,
    7180                                    const BString& comment,
    7281                                    const BString& stability,
  • src/apps/haikudepot/ui/MainWindow.cpp

    diff --git a/src/apps/haikudepot/ui/MainWindow.cpp b/src/apps/haikudepot/ui/MainWindow.cpp
    index b9f7529..addf626 100644
    a b  
    33 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
    44 * Copyright 2013, Rene Gollent, rene@gollent.com.
    55 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
     6 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    67 * All rights reserved. Distributed under the terms of the MIT License.
    78 */
    89
    MainWindow::_RefreshPackageList(bool force)  
    834835    DepotInfoMap depots;
    835836    for (int32 i = 0; i < repositoryNames.CountStrings(); i++) {
    836837        const BString& repoName = repositoryNames.StringAt(i);
    837         depots[repoName] = DepotInfo(repoName);
     838        DepotInfo depotInfo = DepotInfo(repoName);
     839
     840        BRepositoryConfig repoConfig;
     841        status_t getRepositoryConfigStatus = roster.GetRepositoryConfig(
     842            repoName, &repoConfig);
     843
     844        if (getRepositoryConfigStatus == B_OK) {
     845            depotInfo.SetBaseURL(repoConfig.BaseURL());
     846
     847            // it would be nice if this could be more logically located such as
     848            // when the repository is added to the model, but that is probably
     849            // a bigger change.
     850
     851            fModel.PopulateWebAppRepositoryCode(depotInfo);
     852        } else {
     853            printf("unable to obtain the repository config for local repository '%s'; %s\n",
     854                repoName.String(), strerror(getRepositoryConfigStatus));
     855        }
     856
     857        depots[repoName] = depotInfo;
    838858    }
    839859
    840860    PackageManager manager(B_PACKAGE_INSTALLATION_LOCATION_HOME);
    MainWindow::_RefreshPackageList(bool force)  
    891911    for (int32 i = 0; i < packages.CountItems(); i++) {
    892912        BSolverPackage* package = packages.ItemAt(i);
    893913        const BPackageInfo& repoPackageInfo = package->Info();
     914        const BString repositoryName = package->Repository()->Name();
    894915        PackageInfoRef modelInfo;
    895916        PackageInfoMap::iterator it = foundPackages.find(
    896917            repoPackageInfo.Name());
    MainWindow::_RefreshPackageList(bool force)  
    904925            if (modelInfo.Get() == NULL)
    905926                return;
    906927
     928            modelInfo->SetDepotName(repositoryName);
     929
    907930            foundPackages[repoPackageInfo.Name()] = modelInfo;
    908931        }
    909932
    MainWindow::_ShowScreenshot()  
    12441267
    12451268    fScreenshotWindow->Unlock();
    12461269}
    1247 
  • src/apps/haikudepot/ui/RatePackageWindow.cpp

    diff --git a/src/apps/haikudepot/ui/RatePackageWindow.cpp b/src/apps/haikudepot/ui/RatePackageWindow.cpp
    index 2d0bca6..090cf09 100644
    a b  
    11/*
    22 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
     3 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>.
    34 * All rights reserved. Distributed under the terms of the MIT License.
    45 */
    56
    RatePackageWindow::_QueryRatingThread()  
    408409
    409410    WebAppInterface interface;
    410411    BMessage info;
     412    const DepotInfo* depot = fModel.DepotForName(package->DepotName());
     413    BString repositoryCode;
    411414
    412     status_t status = interface.RetrieveUserRating(
    413         package->Name(), package->Version(), package->Architecture(),
    414         username, info);
     415    if (depot != NULL)
     416        repositoryCode = depot->WebAppRepositoryCode();
    415417
    416 //  info.PrintToStream();
    417 
    418     BMessage result;
    419     if (status == B_OK && info.FindMessage("result", &result) == B_OK
    420         && Lock()) {
    421 
    422         result.FindString("code", &fRatingID);
    423         result.FindBool("active", &fRatingActive);
    424         BString comment;
    425         if (result.FindString("comment", &comment) == B_OK) {
    426             MarkupParser parser;
    427             fRatingText = parser.CreateDocumentFromMarkup(comment);
    428             fTextView->SetTextDocument(fRatingText);
    429         }
    430         if (result.FindString("userRatingStabilityCode",
    431             &fStability) == B_OK) {
    432             int32 index = 0;
    433             for (int32 i = fStabilityCodes.CountItems() - 1; i >= 0; i--) {
    434                 const StabilityRating& stability
    435                     = fStabilityCodes.ItemAtFast(i);
    436                 if (stability.Name() == fStability) {
    437                     index = i;
    438                     break;
     418    if (0 == repositoryCode.Length()) {
     419        printf("unable to obtain the repository code for depot; %s\n",
     420            package->DepotName().String());
     421    }
     422    else {
     423        status_t status = interface.RetrieveUserRating(
     424            package->Name(), package->Version(), package->Architecture(),
     425            repositoryCode, username, info);
     426
     427    //  info.PrintToStream();
     428
     429        BMessage result;
     430        if (status == B_OK && info.FindMessage("result", &result) == B_OK
     431            && Lock()) {
     432
     433            result.FindString("code", &fRatingID);
     434            result.FindBool("active", &fRatingActive);
     435            BString comment;
     436            if (result.FindString("comment", &comment) == B_OK) {
     437                MarkupParser parser;
     438                fRatingText = parser.CreateDocumentFromMarkup(comment);
     439                fTextView->SetTextDocument(fRatingText);
     440            }
     441            if (result.FindString("userRatingStabilityCode",
     442                &fStability) == B_OK) {
     443                int32 index = 0;
     444                for (int32 i = fStabilityCodes.CountItems() - 1; i >= 0; i--) {
     445                    const StabilityRating& stability
     446                        = fStabilityCodes.ItemAtFast(i);
     447                    if (stability.Name() == fStability) {
     448                        index = i;
     449                        break;
     450                    }
    439451                }
     452                BMenuItem* item = fStabilityField->Menu()->ItemAt(index);
     453                if (item != NULL)
     454                    item->SetMarked(true);
     455            }
     456            if (result.FindString("naturalLanguageCode",
     457                &fCommentLanguage) == B_OK) {
     458                BMenuItem* item = fCommentLanguageField->Menu()->ItemAt(
     459                    fModel.SupportedLanguages().IndexOf(fCommentLanguage));
     460                if (item != NULL)
     461                    item->SetMarked(true);
     462            }
     463            double rating;
     464            if (result.FindDouble("rating", &rating) == B_OK) {
     465                fRating = (float)rating;
     466                fSetRatingView->SetPermanentRating(fRating);
    440467            }
    441             BMenuItem* item = fStabilityField->Menu()->ItemAt(index);
    442             if (item != NULL)
    443                 item->SetMarked(true);
    444         }
    445         if (result.FindString("naturalLanguageCode",
    446             &fCommentLanguage) == B_OK) {
    447             BMenuItem* item = fCommentLanguageField->Menu()->ItemAt(
    448                 fModel.SupportedLanguages().IndexOf(fCommentLanguage));
    449             if (item != NULL)
    450                 item->SetMarked(true);
    451         }
    452         double rating;
    453         if (result.FindDouble("rating", &rating) == B_OK) {
    454             fRating = (float)rating;
    455             fSetRatingView->SetPermanentRating(fRating);
    456         }
    457468
    458         fRatingActiveCheckBox->SetValue(fRatingActive);
    459         fRatingActiveCheckBox->Show();
     469            fRatingActiveCheckBox->SetValue(fRatingActive);
     470            fRatingActiveCheckBox->Show();
    460471
    461         fSendButton->SetLabel(B_TRANSLATE("Update"));
     472            fSendButton->SetLabel(B_TRANSLATE("Update"));
    462473
    463         Unlock();
    464     } else {
    465         fprintf(stderr, "rating query: Failed response: %s\n",
    466             strerror(status));
    467         if (!info.IsEmpty())
    468             info.PrintToStream();
     474            Unlock();
     475        } else {
     476            fprintf(stderr, "rating query: Failed response: %s\n",
     477                strerror(status));
     478            if (!info.IsEmpty())
     479                info.PrintToStream();
     480        }
    469481    }
    470482
    471483    _SetWorkerThread(-1);
    RatePackageWindow::_SendRatingThread()  
    491503
    492504    BString package = fPackage->Name();
    493505    BString architecture = fPackage->Architecture();
     506    BString repositoryCode;
    494507    int rating = (int)fRating;
    495508    BString stability = fStability;
    496509    BString comment = fRatingText->Text();
    RatePackageWindow::_SendRatingThread()  
    498511    BString ratingID = fRatingID;
    499512    bool active = fRatingActive;
    500513
     514    const DepotInfo* depot = fModel.DepotForName(fPackage->DepotName());
     515
     516    if (NULL != depot)
     517        repositoryCode = depot->WebAppRepositoryCode();
     518
    501519    WebAppInterface interface = fModel.GetWebAppInterface();
    502520
    503521    Unlock();
    504522
     523    if (repositoryCode.Length() == 0) {
     524        printf("unable to find the web app repository code for the local depot %s\n",
     525            fPackage->DepotName().String());
     526        return;
     527    }
     528
    505529    if (stability == "unspecified")
    506530        stability = "";
    507531
    RatePackageWindow::_SendRatingThread()  
    512536        languageCode, comment, stability, rating, active, info);
    513537    } else {
    514538        status = interface.CreateUserRating(package, architecture,
    515         languageCode, comment, stability, rating, info);
     539            repositoryCode, languageCode, comment, stability, rating, info);
    516540    }
    517541
    518542    BString error = B_TRANSLATE(