File: download_callbacks.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (54 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* includes; won't compile in tests, in the docs we leave out the comment lines to show them
# include <libdnf5/repo/download_callbacks.hpp>
*/

class PackageDownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
private:
    using TransferStatus = libdnf5::repo::DownloadCallbacks::TransferStatus;

    /// List of descriptions of downloads.
    std::forward_list<std::string> download_descriptions;

    void * add_new_download([[maybe_unused]] void * user_data, const char * description, double total_to_download) {
        std::cout << "Started downloading: " << description << ", size: " << total_to_download << std::endl;

        // Store the message describing the new download and return a pointer to it.
        return &download_descriptions.emplace_front(description);
    }

    int end(void * user_cb_data, TransferStatus status, const char * msg) {
        // Check that user_cb_data is present in download_descriptions.
        std::string * description{nullptr};
        for (const auto & item : download_descriptions) {
            if (&item == user_cb_data) {
                // Get the description from the user_cb_data.
                description = reinterpret_cast<std::string *>(user_cb_data);
                break;
            }
        }
        if (!description) {
            return 0;
        }

        std::string message;
        switch (status) {
            case TransferStatus::SUCCESSFUL:
                std::cout << "  Downloaded: " << *description << std::endl;
                break;
            case TransferStatus::ALREADYEXISTS:
                std::cout << "  Already downloaded: " << *description << std::endl;
                break;
            case TransferStatus::ERROR:
                std::cout << "  Error downloading: " << *description << ": " << msg << std::endl;
                break;
        }

        return 0;
    }
};

// Set the download callbacks.
base.set_download_callbacks(std::make_unique<PackageDownloadCallbacks>());

// Download the packages.
transaction.download();