Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / devel / devel.dmacvicar / getfile.cc
1 #include <sys/time.h>
2
3 #include <iostream>
4 #include <fstream>
5
6 #include <zypp/base/Logger.h>
7 #include <zypp/ZYpp.h>
8 #include <zypp/ZYppFactory.h>
9
10 #include "zypp/Product.h"
11 #include "zypp/Package.h"
12 #include "zypp/Fetcher.h"
13 #include "zypp/TmpPath.h"
14 #include "zypp/ProgressData.h"
15
16 #include "zypp/sat/Pool.h"
17
18 #include "zypp/ZYppCallbacks.h"
19
20 using namespace std;
21 using namespace zypp;
22 using namespace zypp::repo;
23 using zypp::media::MediaChangeReport;
24 using zypp::media::DownloadProgressReport;
25
26
27 bool result_cb( const ResObject::Ptr &r )
28 {
29   cout << r << endl;
30 }
31
32 struct MediaChangeReportReceiver : public zypp::callback::ReceiveReport<MediaChangeReport>
33   {
34     virtual MediaChangeReport::Action
35     requestMedia(zypp::Url & url,
36                  unsigned                         mediumNr,
37                  const std::string &              label,
38                  MediaChangeReport::Error         error,
39                  const std::string &              description,
40                  const std::vector<std::string> & devices,
41                  unsigned int &                   index)
42     {
43       cout << label << " " <<description << std::endl;
44       MIL << "media problem, url: " << url.asString() << std::endl;
45       return MediaChangeReport::IGNORE;
46     }
47   };
48
49 struct DownloadProgressReportReceiver : public zypp::callback::ReceiveReport<DownloadProgressReport>
50 {
51
52     virtual void start( const Url &/*file*/, Pathname /*localfile*/ )
53     {
54     }
55     
56     virtual bool progress(int value, const Url &file,
57                           double dbps_avg,
58                           double dbps_current)
59     { 
60         cout << file << " " << value << "% speed:" << dbps_current << " avg:" << dbps_avg << endl;
61         return true; 
62     }
63     
64     virtual Action problem( const Url &/*file*/
65                             , Error /*error*/
66                             , const std::string &description )
67     {
68         cout << "PROBLEM: " << description << endl;
69         return ABORT; 
70     }
71     
72     virtual void finish(
73         const Url &/*file*/
74         , Error /*error*/
75         , const std::string &reason
76         )
77         {
78             cout << "finish:" << endl;            
79             cout << reason << endl;
80         }
81 };
82
83 int main(int argc, char **argv)
84 {
85     try
86     {
87       ZYpp::Ptr z = getZYpp();
88     
89       MediaChangeReportReceiver change_report;
90       DownloadProgressReportReceiver progress_report;
91       change_report.connect();
92       progress_report.connect();
93       
94       MediaSetAccess access(Url("http://download.opensuse.org/update/11.1/rpm/x86_64"));
95       OnMediaLocation loc;
96       loc.setLocation("java-1_5_0-sun-1.5.0_update17-1.1.x86_64.rpm");
97       //loc.setOptional(true);
98
99       Fetcher fetcher;
100       fetcher.enqueue(loc);
101       fetcher.start("./", access);
102       
103     }
104     catch ( const Exception &e )
105     {
106       ZYPP_CAUGHT(e);
107       cout << e.msg() << endl;
108       cout << e.historyAsString();
109     }
110     
111     return 0;
112 }
113
114
115