Imported Upstream version 14.27.2
[platform/upstream/libzypp.git] / zypp / ZYppCallbacks.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ZYppCallbacks.h
10  *
11 */
12 #ifndef ZYPP_ZYPPCALLBACKS_H
13 #define ZYPP_ZYPPCALLBACKS_H
14
15 #include "zypp/base/EnumClass.h"
16 #include "zypp/Callback.h"
17 #include "zypp/UserData.h"
18 #include "zypp/Resolvable.h"
19 #include "zypp/RepoInfo.h"
20 #include "zypp/Pathname.h"
21 #include "zypp/Package.h"
22 #include "zypp/Patch.h"
23 #include "zypp/Url.h"
24 #include "zypp/ProgressData.h"
25 #include "zypp/media/MediaUserAuth.h"
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30
31   ///////////////////////////////////////////////////////////////////
32   namespace sat
33   {
34     class Queue;
35     class FileConflicts;
36   } // namespace sat
37   ///////////////////////////////////////////////////////////////////
38
39   struct ProgressReport : public callback::ReportBase
40   {
41     virtual void start( const ProgressData &/*task*/ )
42     {}
43
44     virtual bool progress( const ProgressData &/*task*/ )
45     { return true; }
46
47 //     virtual Action problem(
48 //         Repo /*source*/
49 //         , Error /*error*/
50 //         , const std::string &/*description*/ )
51 //     { return ABORT; }
52
53     virtual void finish( const ProgressData &/*task*/ )
54     {}
55
56   };
57
58   struct ProgressReportAdaptor
59   {
60
61     ProgressReportAdaptor( const ProgressData::ReceiverFnc &fnc,
62                            callback::SendReport<ProgressReport> &report )
63       : _fnc(fnc)
64       , _report(report)
65       , _first(true)
66     {
67     }
68
69     bool operator()( const ProgressData &progress )
70     {
71       if ( _first )
72       {
73         _report->start(progress);
74         _first = false;
75       }
76
77       _report->progress(progress);
78       bool value = true;
79       if ( _fnc )
80         value = _fnc(progress);
81
82
83       if ( progress.finalReport() )
84       {
85         _report->finish(progress);
86       }
87       return value;
88     }
89
90     ProgressData::ReceiverFnc _fnc;
91     callback::SendReport<ProgressReport> &_report;
92     bool _first;
93   };
94
95   ////////////////////////////////////////////////////////////////////////////
96
97   namespace repo
98   {
99     // progress for downloading a resolvable
100     struct DownloadResolvableReport : public callback::ReportBase
101     {
102       enum Action {
103         ABORT,  // abort and return error
104         RETRY,  // retry
105         IGNORE, // ignore this resolvable but continue
106       };
107
108       enum Error {
109         NO_ERROR,
110         NOT_FOUND,      // the requested Url was not found
111         IO,             // IO error
112         INVALID         // the downloaded file is invalid
113       };
114
115       /** Hint that package is available in the local cache (no download needed).
116        * This will be the only trigger for an already cached package.
117        */
118       virtual void infoInCache( Resolvable::constPtr res_r, const Pathname & localfile_r )
119       {}
120
121       virtual void start(
122         Resolvable::constPtr /*resolvable_ptr*/
123         , const Url &/*url*/
124       ) {}
125
126
127       // Dowmload delta rpm:
128       // - path below url reported on start()
129       // - expected download size (0 if unknown)
130       // - download is interruptable
131       // - problems are just informal
132       virtual void startDeltaDownload( const Pathname & /*filename*/, const ByteCount & /*downloadsize*/ )
133       {}
134
135       virtual bool progressDeltaDownload( int /*value*/ )
136       { return true; }
137
138       virtual void problemDeltaDownload( const std::string &/*description*/ )
139       {}
140
141       virtual void finishDeltaDownload()
142       {}
143
144       // Apply delta rpm:
145       // - local path of downloaded delta
146       // - aplpy is not interruptable
147       // - problems are just informal
148       virtual void startDeltaApply( const Pathname & /*filename*/ )
149       {}
150
151       virtual void progressDeltaApply( int /*value*/ )
152       {}
153
154       virtual void problemDeltaApply( const std::string &/*description*/ )
155       {}
156
157       virtual void finishDeltaApply()
158       {}
159
160       /** \deprecated Unused since 2008 */
161       virtual ZYPP_DEPRECATED void startPatchDownload( const Pathname & /*filename*/, const ByteCount & /*downloadsize*/ )
162       {}
163
164       /** \deprecated Unused since 2008 */
165       virtual ZYPP_DEPRECATED bool progressPatchDownload( int /*value*/ )
166       { return true; }
167
168       /** \deprecated Unused since 2008 */
169       virtual ZYPP_DEPRECATED void problemPatchDownload( const std::string &/*description*/ )
170       {}
171
172       /** \deprecated Unused since 2008 */
173       virtual ZYPP_DEPRECATED void finishPatchDownload()
174       {}
175
176
177       // return false if the download should be aborted right now
178       virtual bool progress(int /*value*/, Resolvable::constPtr /*resolvable_ptr*/)
179       { return true; }
180
181       virtual Action problem(
182         Resolvable::constPtr /*resolvable_ptr*/
183         , Error /*error*/
184         , const std::string &/*description*/
185       ) { return ABORT; }
186
187       virtual void finish(Resolvable::constPtr /*resolvable_ptr*/
188         , Error /*error*/
189         , const std::string &/*reason*/
190       ) {}
191     };
192
193     // progress for probing a source
194     struct ProbeRepoReport : public callback::ReportBase
195     {
196       enum Action {
197         ABORT,  // abort and return error
198         RETRY   // retry
199       };
200
201       enum Error {
202         NO_ERROR,
203         NOT_FOUND,      // the requested Url was not found
204         IO,             // IO error
205         INVALID,                // th source is invalid
206         UNKNOWN
207       };
208
209       virtual void start(const Url &/*url*/) {}
210       virtual void failedProbe( const Url &/*url*/, const std::string &/*type*/ ) {}
211       virtual void successProbe( const Url &/*url*/, const std::string &/*type*/ ) {}
212       virtual void finish(const Url &/*url*/, Error /*error*/, const std::string &/*reason*/ ) {}
213
214       virtual bool progress(const Url &/*url*/, int /*value*/)
215       { return true; }
216
217       virtual Action problem( const Url &/*url*/, Error /*error*/, const std::string &/*description*/ ) { return ABORT; }
218     };
219
220     struct RepoCreateReport : public callback::ReportBase
221     {
222       enum Action {
223         ABORT,  // abort and return error
224         RETRY,  // retry
225         IGNORE  // skip refresh, ignore failed refresh
226       };
227
228       enum Error {
229         NO_ERROR,
230         NOT_FOUND,      // the requested Url was not found
231         IO,             // IO error
232         REJECTED,
233         INVALID, // th source is invali
234         UNKNOWN
235       };
236
237       virtual void start( const zypp::Url &/*url*/ ) {}
238       virtual bool progress( int /*value*/ )
239       { return true; }
240
241       virtual Action problem(
242           const zypp::Url &/*url*/
243           , Error /*error*/
244           , const std::string &/*description*/ )
245       { return ABORT; }
246
247       virtual void finish(
248           const zypp::Url &/*url*/
249           , Error /*error*/
250           , const std::string &/*reason*/ )
251       {}
252     };
253
254     struct RepoReport : public callback::ReportBase
255     {
256       enum Action {
257         ABORT,  // abort and return error
258         RETRY,  // retry
259         IGNORE  // skip refresh, ignore failed refresh
260       };
261
262       enum Error {
263         NO_ERROR,
264         NOT_FOUND,      // the requested Url was not found
265         IO,             // IO error
266         INVALID         // th source is invalid
267       };
268
269       virtual void start( const ProgressData &/*task*/, const RepoInfo /*repo*/  ) {}
270       virtual bool progress( const ProgressData &/*task*/ )
271       { return true; }
272
273       virtual Action problem(
274           Repository /*source*/
275           , Error /*error*/
276           , const std::string &/*description*/ )
277       { return ABORT; }
278
279       virtual void finish(
280           Repository /*source*/
281           , const std::string &/*task*/
282           , Error /*error*/
283           , const std::string &/*reason*/ )
284       {}
285     };
286
287
288     /////////////////////////////////////////////////////////////////
289   } // namespace source
290   ///////////////////////////////////////////////////////////////////
291
292   ///////////////////////////////////////////////////////////////////
293   namespace media
294   {
295     // media change request callback
296     struct MediaChangeReport : public callback::ReportBase
297     {
298       enum Action {
299         ABORT,  // abort and return error
300         RETRY,  // retry
301         IGNORE, // ignore this media in future, not available anymore
302         IGNORE_ID,      // ignore wrong medium id
303         CHANGE_URL,     // change media URL
304         EJECT           // eject the medium
305       };
306
307       enum Error {
308         NO_ERROR,
309         NOT_FOUND,  // the medie not found at all
310         IO,     // error accessing the media
311         INVALID, // media is broken
312         WRONG,  // wrong media, need a different one
313         IO_SOFT       /**< IO error which can happen on worse connection like timeout exceed */
314       };
315
316       /**
317        *
318        * \param url         in: url for which the media is requested,
319        *                    out: url to use instead of the original one
320        * \param mediumNr    requested medium number
321        * \param label       label of requested medium
322        * \param error       type of error from \ref Error enum
323        * \param description error message (media not desired or error foo occured)
324        * \param devices     list of the available devices (for eject)
325        * \param dev_current in: index of the currently used device in the \a devices list
326        *                    out: index of the devices to be ejected in the \a devices list
327        * \return \ref Action (ABORT by default)
328        */
329       virtual Action requestMedia(
330         Url & /* url (I/O parameter) */
331         , unsigned /*mediumNr*/
332         , const std::string & /* label */
333         , Error /*error*/
334         , const std::string & /*description*/
335         , const std::vector<std::string> & /* devices */
336         , unsigned int & /* dev_current (I/O param) */
337       ) { return ABORT; }
338     };
339
340     // progress for downloading a file
341     struct DownloadProgressReport : public callback::ReportBase
342     {
343         enum Action {
344           ABORT,  // abort and return error
345           RETRY,        // retry
346           IGNORE        // ignore the failure
347         };
348
349         enum Error {
350           NO_ERROR,
351           NOT_FOUND,    // the requested Url was not found
352           IO,           // IO error
353           ACCESS_DENIED, // user authent. failed while accessing restricted file
354           ERROR // other error
355         };
356
357         virtual void start( const Url &/*file*/, Pathname /*localfile*/ ) {}
358
359         /**
360          * Download progress.
361          *
362          * \param value        Percentage value.
363          * \param file         File URI.
364          * \param dbps_avg     Average download rate so far. -1 if unknown.
365          * \param dbps_current Current download (cca last 1 sec). -1 if unknown.
366          */
367         virtual bool progress(int /*value*/, const Url &/*file*/,
368                               double dbps_avg = -1,
369                               double dbps_current = -1)
370         { return true; }
371
372         virtual Action problem(
373           const Url &/*file*/
374           , Error /*error*/
375           , const std::string &/*description*/
376         ) { return ABORT; }
377
378         virtual void finish(
379           const Url &/*file*/
380           , Error /*error*/
381           , const std::string &/*reason*/
382         ) {}
383     };
384
385     // authentication issues report
386     struct AuthenticationReport : public callback::ReportBase
387     {
388       /**
389        * Prompt for authentication data.
390        *
391        * \param url       URL which required the authentication
392        * \param msg       prompt text
393        * \param auth_data input/output object for handling authentication
394        *        data. As an input parameter auth_data can be prefilled with
395        *        username (from previous try) or auth_type (available
396        *        authentication methods aquired from server (only CurlAuthData)).
397        *        As an output parameter it serves for sending username, pasword
398        *        or other special data (derived AuthData objects).
399        * \return true if user chooses to continue with authentication,
400        *         false otherwise
401        */
402       virtual bool prompt(const Url & /* url */,
403         const std::string & /* msg */,
404         AuthData & /* auth_data */)
405       {
406         return false;
407       }
408     };
409
410     /////////////////////////////////////////////////////////////////
411   } // namespace media
412   ///////////////////////////////////////////////////////////////////
413
414   ///////////////////////////////////////////////////////////////////
415   namespace target
416   {
417     /** Request to display the pre commit message of a patch. */
418     struct PatchMessageReport : public callback::ReportBase
419     {
420       /** Display \c patch->message().
421        * Return \c true to continue, \c false to abort commit.
422       */
423       virtual bool show( Patch::constPtr & /*patch*/ )
424       { return true; }
425     };
426
427     /** Indicate execution of a patch script. This is a sort of
428      * \c %post script shipped by a package and to be executed
429      * after the package was installed.
430     */
431     struct PatchScriptReport : public callback::ReportBase
432     {
433       enum Notify { OUTPUT, PING };
434       enum Action {
435         ABORT,  // abort commit and return error
436         RETRY,  // (re)try to execute this script
437         IGNORE  // ignore any failue and continue
438       };
439
440       /** Start executing the script provided by package.
441       */
442       virtual void start( const Package::constPtr & /*package*/,
443                           const Pathname & /*script path*/ )
444       {}
445       /** Progress provides the script output. If the script is quiet,
446        * from time to time still-alive pings are sent to the ui. Returning \c FALSE
447        * aborts script execution.
448       */
449       virtual bool progress( Notify /*OUTPUT or PING*/,
450                              const std::string & /*output*/ = std::string() )
451       { return true; }
452       /** Report error. */
453       virtual Action problem( const std::string & /*description*/ )
454       { return ABORT; }
455       /** Report success. */
456       virtual void finish()
457       {}
458     };
459
460     ///////////////////////////////////////////////////////////////////
461     /// \class FindFileConflictstReport
462     /// \brief Check for package file conflicts in commit (after download)
463     ///
464     /// File conflict check requires that all packages are downloaded and
465     /// now available in the cache (need to access the filelists). Missing
466     /// packages are omitted from check and their number is reported in
467     /// \a noFilelist_r. This usually happens if download mode 'as-needed'
468     /// is used.
469     ///////////////////////////////////////////////////////////////////
470     struct FindFileConflictstReport : public callback::ReportBase
471     {
472       /**
473        * \param progress_r      Progress counter for packages to check.
474        * \return \c true to continue, \c false upon user abort request.
475        */
476       virtual bool start( const ProgressData & progress_r )
477       { return true; }
478
479       /**
480        * \param progress_r      Progress counter for packages to check.
481        * \param noFilelist_r    Queue of so far skipped solvables (no filelist/not yet downloaded).
482        * \return \c true to continue, \c false upon user abort request.
483        */
484       virtual bool progress( const ProgressData & progress_r, const sat::Queue & noFilelist_r )
485       { return true; }
486
487       /**
488        * \param progress_r      Progress counter for packages to check.
489        * \param noFilelist_r    Queue of skipped solvables (no filelist/not yet downloaded).
490        * \param conflicts_r     File conflits queue.
491        * \return \c true to continue, \c false upon user abort request.
492        */
493       virtual bool result( const ProgressData & progress_r, const sat::Queue & noFilelist_r, const sat::FileConflicts & conflicts_r )
494       { return true; }
495     };
496
497
498     ///////////////////////////////////////////////////////////////////
499     namespace rpm
500     {
501
502       // progress for installing a resolvable
503       struct InstallResolvableReport : public callback::ReportBase
504       {
505         enum Action {
506           ABORT,  // abort and return error
507           RETRY,        // retry
508           IGNORE        // ignore the failure
509         };
510
511         enum Error {
512           NO_ERROR,
513           NOT_FOUND,    // the requested Url was not found
514           IO,           // IO error
515           INVALID               // th resolvable is invalid
516         };
517
518         // the level of RPM pushing
519         /** \deprecated We fortunately no longer do 3 attempts. */
520         enum RpmLevel {
521             RPM,
522             RPM_NODEPS,
523             RPM_NODEPS_FORCE    //!< only this one used
524         };
525
526         virtual void start(
527           Resolvable::constPtr /*resolvable*/
528         ) {}
529
530         virtual bool progress(int /*value*/, Resolvable::constPtr /*resolvable*/)
531         { return true; }
532
533         virtual Action problem(
534           Resolvable::constPtr /*resolvable*/
535           , Error /*error*/
536           , const std::string &/*description*/
537           , RpmLevel /*level*/
538         ) { return ABORT; }
539
540         virtual void finish(
541           Resolvable::constPtr /*resolvable*/
542           , Error /*error*/
543           , const std::string &/*reason*/
544           , RpmLevel /*level*/
545         ) {}
546       };
547
548       // progress for removing a resolvable
549       struct RemoveResolvableReport : public callback::ReportBase
550       {
551         enum Action {
552           ABORT,  // abort and return error
553           RETRY,        // retry
554           IGNORE        // ignore the failure
555         };
556
557         enum Error {
558           NO_ERROR,
559           NOT_FOUND,    // the requested Url was not found
560           IO,           // IO error
561           INVALID               // th resolvable is invalid
562         };
563
564         virtual void start(
565           Resolvable::constPtr /*resolvable*/
566         ) {}
567
568         virtual bool progress(int /*value*/, Resolvable::constPtr /*resolvable*/)
569         { return true; }
570
571         virtual Action problem(
572           Resolvable::constPtr /*resolvable*/
573           , Error /*error*/
574           , const std::string &/*description*/
575         ) { return ABORT; }
576
577         virtual void finish(
578           Resolvable::constPtr /*resolvable*/
579           , Error /*error*/
580           , const std::string &/*reason*/
581         ) {}
582       };
583
584       // progress for rebuilding the database
585       struct RebuildDBReport : public callback::ReportBase
586       {
587         enum Action {
588           ABORT,  // abort and return error
589           RETRY,        // retry
590           IGNORE        // ignore the failure
591         };
592
593         enum Error {
594           NO_ERROR,
595           FAILED                // failed to rebuild
596         };
597
598         virtual void start(Pathname /*path*/) {}
599
600         virtual bool progress(int /*value*/, Pathname /*path*/)
601         { return true; }
602
603         virtual Action problem(
604           Pathname /*path*/
605          , Error /*error*/
606          , const std::string &/*description*/
607         ) { return ABORT; }
608
609         virtual void finish(
610           Pathname /*path*/
611           , Error /*error*/
612           , const std::string &/*reason*/
613         ) {}
614       };
615
616       // progress for converting the database
617       struct ConvertDBReport : public callback::ReportBase
618       {
619         enum Action {
620           ABORT,  // abort and return error
621           RETRY,        // retry
622           IGNORE        // ignore the failure
623         };
624
625         enum Error {
626           NO_ERROR,
627           FAILED                // conversion failed
628         };
629
630         virtual void start(
631           Pathname /*path*/
632         ) {}
633
634         virtual bool progress(int /*value*/, Pathname /*path*/)
635         { return true; }
636
637         virtual Action problem(
638           Pathname /*path*/
639           , Error /*error*/
640          , const std::string &/*description*/
641         ) { return ABORT; }
642
643         virtual void finish(
644           Pathname /*path*/
645           , Error /*error*/
646           , const std::string &/*reason*/
647         ) {}
648       };
649
650       /////////////////////////////////////////////////////////////////
651     } // namespace rpm
652     ///////////////////////////////////////////////////////////////////
653
654     /////////////////////////////////////////////////////////////////
655   } // namespace target
656   ///////////////////////////////////////////////////////////////////
657
658   class PoolQuery;
659
660   /** \name Locks */
661   //@{
662   /**
663    * Callback for cleaning locks which doesn't lock anything in pool.
664    */
665
666   struct CleanEmptyLocksReport : public callback::ReportBase
667   {
668     /**
669      * action performed by cleaning api to specific lock
670      */
671     enum Action {
672       ABORT,  /**< abort and return error */
673       DELETE, /**< delete empty lock    */
674       IGNORE  /**< skip empty lock */
675     };
676
677     /**
678      * result of cleaning
679      */
680     enum Error {
681       NO_ERROR, /**< no problem */
682       ABORTED /** cleaning aborted by user */
683     };
684
685     /**
686      * cleaning is started
687      */
688     virtual void start(
689     ) {}
690
691     /**
692      * progress of cleaning specifies in percents
693      * \return if continue
694      */
695     virtual bool progress(int /*value*/)
696     { return true; }
697
698     /**
699      * When find empty lock ask what to do with it
700      * \return action
701      */
702     virtual Action execute(
703         const PoolQuery& /*error*/
704      ) { return DELETE; }
705
706       /**
707        * cleaning is done
708        */
709      virtual void finish(
710        Error /*error*/
711       ) {}
712
713   };
714
715   /**
716    * this callback handles merging old locks with newly added or removed
717    */
718   struct SavingLocksReport : public callback::ReportBase
719   {
720     /**
721      * action for old lock which is in conflict
722      * \see ConflictState
723      */
724     enum Action {
725       ABORT,  /**< abort and return error*/
726       DELETE, /**< delete conflicted lock    */
727       IGNORE  /**< skip conflict lock */
728     };
729
730     /**
731      * result of merging
732      */
733     enum Error {
734       NO_ERROR, /**< no problem */
735       ABORTED  /**< cleaning aborted by user */
736     };
737
738     /**
739      * type of conflict of old and new lock
740      */
741     enum ConflictState{
742       SAME_RESULTS, /**< locks lock same item in pool but his parameters is different */
743       INTERSECT /**< locks lock some file and unlocking lock unlock only part
744       * of iti, so removing old lock can unlock more items in pool */
745     };
746
747     virtual void start() {}
748
749     /**
750      * merging still live
751      * \return if continue
752      */
753     virtual bool progress()
754     { return true; }
755
756     /**
757      * When user unlock something which is locked by non-identical query
758      */
759     virtual Action conflict(
760          const PoolQuery&, /**< problematic query*/
761        ConflictState
762      ) { return DELETE; }
763
764      virtual void finish(
765        Error /*error*/
766       ) {}
767   };
768
769   ///////////////////////////////////////////////////////////////////
770   /// \class JobReport
771   /// \brief Generic report for sending messages.
772   ///////////////////////////////////////////////////////////////////
773   struct JobReport : public callback::ReportBase
774   {
775   public:
776     /** message type (use like 'enum class \ref MsgType') */
777     struct _MsgTypeDef {
778       enum Enum { debug, info, warning, error, important, data };
779     };
780     typedef base::EnumClass<_MsgTypeDef> MsgType;       ///< 'enum class MsgType'
781
782     /** typsafe map of userdata */
783     typedef callback::UserData UserData;
784
785   public:
786     /** Send a ready to show message text. */
787     virtual bool message( MsgType type_r, const std::string & msg_r, const UserData & userData_r ) const
788     { return true; }
789
790
791     /** \name Static sender instance */
792     //@{
793     /** Singleton sender instance */
794     static callback::SendReport<JobReport> & instance();        // impl in ZYppImpl.cc
795
796     /** send debug message text */
797     static bool debug( const MessageString & msg_r, const UserData & userData_r = UserData() )
798     { return instance()->message( MsgType::debug, msg_r, userData_r ); }
799
800     /** send message text */
801     static bool info( const MessageString & msg_r, const UserData & userData_r = UserData() )
802     { return instance()->message( MsgType::info, msg_r, userData_r ); }
803
804     /** send warning text */
805     static bool warning( const MessageString & msg_r, const UserData & userData_r = UserData() )
806     { return instance()->message( MsgType::warning, msg_r, userData_r ); }
807
808     /** send error text */
809     static bool error( const MessageString & msg_r, const UserData & userData_r = UserData() )
810     { return instance()->message( MsgType::error, msg_r, userData_r ); }
811
812     /** send important message text */
813     static bool important( const MessageString & msg_r, const UserData & userData_r = UserData() )
814     { return instance()->message( MsgType::important, msg_r, userData_r ); }
815
816     /** send data message */
817     static bool data( const MessageString & msg_r, const UserData & userData_r = UserData() )
818     { return instance()->message( MsgType::data, msg_r, userData_r ); }
819     //@}
820   };
821
822
823   /////////////////////////////////////////////////////////////////
824 } // namespace zypp
825 ///////////////////////////////////////////////////////////////////
826
827 #endif // ZYPP_ZYPPCALLBACKS_H