~kyrofa/junk/pay-service-2

« back to all changes in this revision

Viewing changes to libpay/pay-package.cpp

  • Committer: CI bot
  • Author(s): Ted Gould
  • Date: 2014-06-18 18:47:04 UTC
  • mfrom: (8.1.10 devel)
  • Revision ID: ps-jenkins@lists.canonical.com-20140618184704-roe2dx6dwvvw37km
Fixes from integration work to make the pay demo come together. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
class Package
32
32
{
33
33
    std::string id;
 
34
    std::string path;
34
35
    /* NOTE: Using the shared_ptr here because gcc 4.7 map doesn't have emplace */
35
36
    std::map <std::pair<PayPackageItemObserver, void*>, std::shared_ptr<core::ScopedConnection>> observers;
36
37
    core::Signal<std::string, PayPackageItemStatus> itemChanged;
46
47
public:
47
48
    Package (const char* packageid) : id(packageid), cancellable(g_cancellable_new()), loop(nullptr)
48
49
    {
 
50
        path = std::string("/com/canonical/pay/");
 
51
        path += encodePath(id);
 
52
 
49
53
        /* Keeps item cache up-to-data as we get signals about it */
50
54
        itemChanged.connect([this](std::string itemid, PayPackageItemStatus status)
51
55
        {
62
66
            g_main_context_push_thread_default(context);
63
67
            context_mutex.unlock();
64
68
 
65
 
            std::string path("/com/canonical/pay/");
66
 
            path += id; /* TODO: encode */
67
69
            proxy = proxy_pay_package_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
68
70
                                                             G_DBUS_PROXY_FLAGS_NONE,
69
71
                                                             "com.canonical.pay",
70
72
                                                             path.c_str(),
71
 
                                                             cancellable,
 
73
                                                             NULL,
72
74
                                                             &error);
73
75
 
74
76
            if (error != nullptr)
248
250
    {
249
251
        return functionCall(proxy_pay_package_call_purchase_item, proxy_pay_package_call_purchase_item_finish, itemid);
250
252
    }
 
253
 
 
254
    std::string
 
255
    encodePath (const std::string& input)
 
256
    {
 
257
        std::string output = "";
 
258
        bool first = true;
 
259
 
 
260
        for (unsigned char c : input)
 
261
        {
 
262
            std::string retval;
 
263
 
 
264
            if ((c >= 'a' && c <= 'z') ||
 
265
                    (c >= 'A' && c <= 'Z') ||
 
266
                    (c >= '0' && c <= '9' && !first))
 
267
            {
 
268
                retval = std::string((char*)&c, 1);
 
269
            }
 
270
            else
 
271
            {
 
272
                char buffer[5] = {0};
 
273
                std::snprintf(buffer, 4, "_%2X", c);
 
274
                retval = std::string(buffer);
 
275
            }
 
276
 
 
277
            output += retval;
 
278
            first = false;
 
279
        }
 
280
 
 
281
        return output;
 
282
    }
251
283
};
252
284
 
253
285