print functionality
[pykit.git] / PythonHTTP.cc
1 // $Id$
2 //
3 // Copyright (C) 2011
4
5 /** \file
6     \brief PythonHTTP non-inline non-template implementation */
7
8 //#include "PythonHTTP.hh"
9 //#include "PythonHTTP.ih"
10
11 // Custom includes
12 #include <boost/python.hpp>
13 #include <QNetworkAccessManager>
14 #include <QNetworkRequest>
15 #include <QNetworkReply>
16 #include <QTimer>
17
18 //#include "PythonHTTP.mpp"
19 #define prefix_
20 ///////////////////////////////cc.p////////////////////////////////////////
21
22 namespace py = boost::python;
23
24 namespace {
25
26     class Manager
27     {
28     public:
29         void post(QString const & url, QByteArray const & data, boost::python::object callback);
30
31     private:
32         QNetworkAccessManager mgr_;
33     };
34
35     class ReplyHandler : public QObject
36     {
37         Q_OBJECT;
38     public:
39         explicit ReplyHandler(boost::python::object callback, QNetworkReply * reply);
40         virtual ~ReplyHandler();
41
42     public slots:
43         void finished();
44
45     private:
46         boost::python::object callback_;
47         QNetworkReply * reply_;
48     };
49
50     class TimeoutHandler : public QObject
51     {
52         Q_OBJECT;
53     public:
54         explicit TimeoutHandler(boost::python::object callback, unsigned msecs);
55
56     public slots:
57         void timeout();
58
59     private:
60         boost::python::object callback_;
61         QTimer timer_;
62     };
63
64     void timeout(boost::python::object callback, unsigned msecs)
65     {
66         new TimeoutHandler(callback, msecs);
67     }
68 }
69
70 prefix_ void Manager::post(QString const & url, QByteArray const & data,
71                            boost::python::object callback)
72 {
73     QNetworkRequest req;
74     req.setUrl(QUrl(url));
75     QNetworkReply * reply (mgr_.post(req, data));
76     new ReplyHandler(callback, reply);
77 }
78
79 prefix_ ReplyHandler::ReplyHandler(boost::python::object callback, QNetworkReply * reply)
80     : callback_ (callback), reply_ (reply)
81 {
82     connect(reply_,SIGNAL(finished()),this,SLOT(finished()));
83 }
84
85 prefix_ ReplyHandler::~ReplyHandler()
86 {}
87
88 prefix_ void ReplyHandler::finished()
89 {
90     callback_(reply_->error() == QNetworkReply::NoError, reply_->readAll());
91     reply_->deleteLater();
92     deleteLater();
93 }
94
95 prefix_ TimeoutHandler::TimeoutHandler(boost::python::object callback, unsigned msecs)
96     : callback_ (callback)
97 {
98     timer_.setSingleShot(true);
99     timer_.start(msecs);
100     connect(&timer_, SIGNAL(timeout()), this, SLOT(timeout()));
101 }
102
103 prefix_ void TimeoutHandler::timeout()
104 {
105     callback_();
106     deleteLater();
107 }
108
109 BOOST_PYTHON_MODULE(_httpapi)
110 {
111     py::class_<Manager, boost::noncopyable>("Manager")
112         .def("post", &Manager::post)
113         ;
114
115     py::def("timeout", &timeout);
116 }
117
118 #include "PythonHTTP.moc"
119
120 ///////////////////////////////cc.e////////////////////////////////////////
121 #undef prefix_
122 //#include "PythonHTTP.mpp"
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // indent-tabs-mode: nil
128 // End: