Debian Codegraph @

examples/gio-qt-async.cpp

raw symbols SCIP
1 #define GI_INLINE 1
2 #include <gio/gio.hpp>
3
4 #include <functional>
5 #include <iostream>
6 #include <memory>
7
8 #include <QCoreApplication>
9
10 #include <QFuture>
11 #include <QFutureWatcher>
12 #include <QObject>
13
14 namespace GLib = gi::repository::GLib;
15 namespace GObject_ = gi::repository::GObject;
16 namespace Gio = gi::repository::Gio;
17
18 template<typename T>
19 using ResultExtractor = std::function<void(
20 GObject_::Object, Gio::AsyncResult result, QFutureInterface<T> &fut)>;
21
22 template<typename T>
23 class qt_future
24 {
25 struct CallbackData
26 {
27 QFutureInterface<T> promise;
28 QFutureWatcher<T> watcher;
29 Gio::Cancellable cancel;
30 ResultExtractor<T> handler;
31 };
32
33 std::shared_ptr<CallbackData> data_;
34
35 public:
36 qt_future(ResultExtractor<T> h)
37 {
38 data_ = std::make_shared<CallbackData>();
39 data_->handler = h;
40 }
41
42 QFuture<T> future() { return data_->promise.future(); }
43
44 QFutureInterface<T> promise() { return data_->promise; }
45
46 operator Gio::AsyncReadyCallback()
47 {
48 auto d = data_;
49 return [d](GObject_::Object obj, Gio::AsyncResult result) {
50 assert(d->handler);
51 d->handler(obj, result, d->promise);
52 };
53 }
54
55 Gio::Cancellable cancellable()
56 {
57 if (!data_->cancel) {
58 auto cancel = data_->cancel = Gio::Cancellable::new_();
59 data_->watcher.setFuture(future());
60 auto h = [cancel]() mutable { cancel.cancel(); };
61 data_->watcher.connect(
62 &data_->watcher, &decltype(data_->watcher)::finished, std::move(h));
63 }
64 return data_->cancel;
65 }
66
67 // why not ... ??
68 operator Gio::Cancellable() { return cancellable(); }
69 };
70
71 #if GI_CONST_METHOD
72 #define CONST_METHOD const
73 #else
74 #define CONST_METHOD
75 #endif
76
77 template<typename Result, typename Object>
78 qt_future<Result>
79 make_future(Result (Object::*mf)(Gio::AsyncResult, GLib::Error *) CONST_METHOD)
80 {
81 ResultExtractor<Result> f;
82 f = [mf](GObject_::Object cbobj, Gio::AsyncResult result,
83 QFutureInterface<Result> &fut) mutable {
84 auto obj = gi::object_cast<Object>(cbobj);
85 GLib::Error error{};
86 auto res = (obj.*mf)(result, &error);
87 if (error.gobj_()) {
88 // FIXME use std::expected<Result> as a result type ??
89 // reportException might be an option, if enabled, but leads to throw
90 fut.reportFinished();
91 qWarning() << "error code " << error.code_();
92 qWarning() << "error message "
93 << QString::fromUtf8(error.message_().c_str());
94 } else {
95 fut.reportFinished(&res);
96 }
97 };
98 return {f};
99 }
100
101 // ====
102 // some small future-centric API wrappers using above helper class
103
104 QFuture<Gio::FileInfo>
105 file_query_file_system_info(
106 Gio::File f, const std::string &attributes, gint io_priority)
107 {
108 auto fut = make_future(&Gio::File::query_filesystem_info_finish);
109 f.query_filesystem_info_async(attributes, io_priority, fut, fut);
110 return fut.future();
111 }
112
113 QFuture<bool>
114 file_copy(Gio::File src, Gio::File destination, Gio::FileCopyFlags flags,
115 gint io_priority)
116 {
117 auto fut = make_future(&Gio::File::copy_finish);
118 auto promise = fut.promise();
119 auto p = [promise](
120 goffset current_num_bytes, goffset total_num_bytes) mutable {
121 promise.setProgressRange(0, total_num_bytes);
122 promise.setProgressValue(current_num_bytes);
123 };
124 src.copy_async(destination, flags, io_priority, fut, p, fut);
125 return fut.future();
126 }
127
128 // ====
129
130 // save on typing elsewhere
131 // also makes it movable in a way
132 template<typename T>
133 std::shared_ptr<QFutureWatcher<T>>
134 make_watcher(QFuture<T> fut)
135 {
136 auto watcher = std::make_shared<QFutureWatcher<T>>();
137 watcher->setFuture(fut);
138 return watcher;
139 }
140
141 // type-erased owner/cleanup type
142 using Retainer = std::shared_ptr<std::nullptr_t>;
143
144 Retainer
145 do_query(QCoreApplication &app, const std::string &fpath)
146 {
147 auto file = Gio::File::new_for_commandline_arg(fpath);
148 auto fut = file_query_file_system_info(file, "*", GLib::PRIORITY_DEFAULT_);
149 auto watcher = make_watcher(fut);
150 auto h = [fut, &app]() {
151 Q_ASSERT(fut.isFinished());
152 std::cout << "query finished" << std::endl;
153 if (fut.resultCount()) {
154 auto finfo = fut.result();
155 for (auto attr : {Gio::FILE_ATTRIBUTE_FILESYSTEM_TYPE_,
156 Gio::FILE_ATTRIBUTE_FILESYSTEM_FREE_,
157 Gio::FILE_ATTRIBUTE_FILESYSTEM_SIZE_}) {
158 std::cout << attr << ": " << finfo.get_attribute_as_string(attr)
159 << std::endl;
160 }
161 } else {
162 std::cout << "... but no results" << std::endl;
163 }
164 app.quit();
165 };
166 watcher->connect(
167 watcher.get(), &decltype(watcher)::element_type::finished, &app, h);
168 return {watcher, nullptr};
169 }
170
171 Retainer
172 do_copy(QCoreApplication &app, const std::string &src, const std::string &dest)
173 {
174 auto fsrc = Gio::File::new_for_commandline_arg(src);
175 auto fdest = Gio::File::new_for_commandline_arg(dest);
176 auto fut = file_copy(
177 fsrc, fdest, Gio::FileCopyFlags::ALL_METADATA_, GLib::PRIORITY_DEFAULT_);
178 auto watcher = make_watcher(fut);
179 auto h = [fut, &app]() {
180 Q_ASSERT(fut.isFinished());
181 std::cout << "copy finished" << std::endl;
182 if (fut.resultCount()) {
183 auto ok = fut.result();
184 if (ok) {
185 std::cout << "copy ok" << std::endl;
186 } else {
187 std::cout << "copy failed" << std::endl;
188 }
189 } else {
190 std::cout << "... but no results" << std::endl;
191 }
192 app.quit();
193 };
194 watcher->connect(
195 watcher.get(), &decltype(watcher)::element_type::finished, &app, h);
196 // also monitor progress
197 auto progress = [fut](int) {
198 auto max = fut.progressMaximum();
199 auto min = fut.progressMinimum();
200 auto current = fut.progressValue();
201 std::cout << "copy progress " << current << " (" << min << " -> " << max
202 << ")" << std::endl;
203 };
204 watcher->connect(watcher.get(),
205 &decltype(watcher)::element_type::progressValueChanged, &app, progress);
206 return {watcher, nullptr};
207 }
208
209 int
210 main(int argc, char **argv)
211 {
212 QCoreApplication app(argc, argv);
213
214 if (argc < 2)
215 return -1;
216
217 Retainer r;
218 if (argc == 2) {
219 r = do_query(app, argv[1]);
220 } else if (argc == 3) {
221 r = do_copy(app, argv[1], argv[2]);
222 }
223
224 return app.exec();
225 }