summaryrefslogtreecommitdiff
path: root/python/progress.h
blob: 88c0a21b7a59bf87840557423a3874350983325e (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Description								/*{{{*/
// $Id: progress.h,v 1.5 2003/06/03 03:03:23 mdz Exp $
/* ######################################################################

   Progress - Wrapper for the progress related functions

   ##################################################################### */

#ifndef PROGRESS_H
#define PROGRESS_H

#include <apt-pkg/progress.h>
#include <apt-pkg/acquire.h>
#include <apt-pkg/packagemanager.h>
#include <apt-pkg/cdrom.h>
#include <Python.h>

/* PyCbObj_BEGIN_ALLOW_THREADS and PyCbObj_END_ALLOW_THREADS are sligthly
 * modified versions of Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS.
 * Instead of storing the thread state in a function-local variable these
 * use a class attribute (with the same) name, allowing blocking and 
 * unblocking from different class methods.
 * Py_BLOCK_THREADS and Py_UNBLOCK_THREADS do not define their own
 * local variable but use the one provided by PyCbObj_BEGIN_ALLOW_THREADS
 * and thus are the same as Py_BLOCK_THREADS and Py_UNBLOCK_THREADS.
 */
#define PyCbObj_BEGIN_ALLOW_THREADS \
  _save = PyEval_SaveThread();
#define PyCbObj_END_ALLOW_THREADS \
  PyEval_RestoreThread(_save); \
  _save = NULL;
#define PyCbObj_BLOCK_THREADS Py_BLOCK_THREADS
#define PyCbObj_UNBLOCK_THREADS Py_UNBLOCK_THREADS

typedef struct {
    PyObject_HEAD
    PyObject *op;
    PyObject *subop;
#ifdef T_BOOL
    char major_change;
#else
    int major_change;
#endif
    float percent;
} PyOpProgressObject;


typedef struct {
    PyObject_HEAD
    int total_steps;
} PyCdromProgressObject;

typedef struct {
    PyObject_HEAD
    double last_bytes;
    double current_cps;
    double current_bytes;
    double total_bytes;
    double fetched_bytes;
    unsigned long elapsed_time;
    unsigned long total_items;
    unsigned long current_items;
} PyAcquireProgressObject;


class PyCallbackObj {
 protected:
   PyObject *callbackInst;
   PyThreadState *_save;

 public:
   void setCallbackInst(PyObject *o) {
      Py_INCREF(o);
      callbackInst = o;
   }

   bool RunSimpleCallback(const char *method, PyObject *arglist=NULL,
			  PyObject **result=NULL);

   PyCallbackObj() : callbackInst(0) {};
   ~PyCallbackObj()  {Py_DECREF(callbackInst); };
};

struct PyOpProgress : public OpProgress, public PyCallbackObj
{

   virtual void Update();
   virtual void Done();

   PyOpProgress() : OpProgress(), PyCallbackObj() {};
};


struct PyFetchProgress : public pkgAcquireStatus, public PyCallbackObj
{
   enum {
      DLDone, DLQueued, DLFailed, DLHit, DLIgnored
   };

   void UpdateStatus(pkgAcquire::ItemDesc & Itm, int status);

   virtual bool MediaChange(string Media, string Drive);

   /* apt stuff */
   virtual void IMSHit(pkgAcquire::ItemDesc &Itm);
   virtual void Fetch(pkgAcquire::ItemDesc &Itm);
   virtual void Done(pkgAcquire::ItemDesc &Itm);
   virtual void Fail(pkgAcquire::ItemDesc &Itm);
   virtual void Start();
   virtual void Stop();

   bool Pulse(pkgAcquire * Owner);
   PyFetchProgress() : PyCallbackObj() {};
};

struct PyInstallProgress : public PyCallbackObj
{
   void StartUpdate();
   void UpdateInterface();
   void FinishUpdate();

   pkgPackageManager::OrderResult Run(pkgPackageManager *pm);

   PyInstallProgress() : PyCallbackObj() {};
};

struct PyCdromProgress : public pkgCdromStatus, public PyCallbackObj
{
   // update steps, will be called regularly as a "pulse"
   virtual void Update(string text="", int current=0);
   // ask for cdrom insert
   virtual bool ChangeCdrom();
   // ask for cdrom name
   virtual bool AskCdromName(string &Name);

   PyCdromProgress() : PyCallbackObj() {};
};


#endif