summaryrefslogtreecommitdiff
path: root/gio/src/application.ccg
blob: d85ae8d758d881447ed261144d2c39a97a64710e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* Copyright (C) 2007 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <giomm/file.h>
#include <giomm/actiongroup.h>
#include <giomm/init.h>
#include <cstring> // std::memset()
#include <map>
#include <mutex>
#include <vector>

using Flags = Gio::Application::Flags;

namespace // anonymous
{

static void
Application_signal_open_callback(
  GApplication* self, GFile** files, gint n_files, const gchar* hint, void* data)
{
  using SlotType = sigc::slot<void(const Gio::Application::type_vec_files&, const Glib::ustring&)>;

  Gio::Application::type_vec_files vec_files(n_files);
  for (int i = 0; i < n_files; ++i)
  {
    vec_files[i] = Glib::wrap(files[i], true);
  }

  const auto hint_str = (hint ? hint : Glib::ustring());

  // Do not try to call a signal on a disassociated wrapper.
  if (Glib::ObjectBase::_get_current_wrapper((GObject*)self))
  {
    try
    {
      if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
      {
        (*static_cast<SlotType*>(slot))(vec_files, hint_str);
        return;
      }
    }
    catch (...)
    {
      Glib::exception_handlers_invoke();
    }
  }

  return;
}

static void
Application_signal_open_notify_callback(
  GApplication* self, GFile** files, gint n_files, const gchar* hint, void* data)
{
  using namespace Gio;
  using SlotType = sigc::slot<void(const Application::type_vec_files&, const Glib::ustring&)>;

  Application::type_vec_files vec_files(n_files);
  for (int i = 0; i < n_files; i++)
  {
    vec_files[i] = Glib::wrap(files[i], true);
  }

  const auto hint_str = (hint ? hint : Glib::ustring());

  // Do not try to call a signal on a disassociated wrapper.
  if (Glib::ObjectBase::_get_current_wrapper((GObject*)self))
  {
    try
    {
      if (const auto slot = Glib::SignalProxyNormal::data_to_slot(data))
      {
        (*static_cast<SlotType*>(slot))(vec_files, hint_str);
        return;
      }
    }
    catch (...)
    {
      Glib::exception_handlers_invoke();
    }
  }

  return;
}

static const Glib::SignalProxyInfo Application_signal_open_info = { "open",
  (GCallback)&Application_signal_open_callback,
  (GCallback)&Application_signal_open_notify_callback };

// The add_main_option_entry*() methods that take a slot parameter are handled
// similarly to the corresponding Glib::OptionGroup::add_entry*() methods.
// There is an important difference: In add_main_option_entry*() we can't pass
// an Application pointer to the used GOptionGroup.
// g_application_add_main_option_entries() creates a GOptionGroup with user_data == nullptr.
// Therefore Application_option_arg_callback() is called with data == nullptr.
// Application_option_arg_callback() does not know which Application instance
// the command-line option belongs to. All Application instances (usually only one)
// share a map, mapping the long command option name to an OptionArgCallbackData.
class OptionArgCallbackData
{
public:
  explicit OptionArgCallbackData(const Gio::Application* application,
    const Glib::ustring& long_name, gchar short_name,
    const Glib::ustring& description, const Glib::ustring& arg_description,
    const Glib::OptionGroup::SlotOptionArgString& slot)
  :
    application_(application),
    long_name_(g_strdup(long_name.c_str())), // must not be nullptr
    short_name_(short_name),
    description_(g_strdup(Glib::c_str_or_nullptr(description))),
    arg_description_(g_strdup(Glib::c_str_or_nullptr(arg_description))),
    slot_string_(new Glib::OptionGroup::SlotOptionArgString(slot)),
    slot_filename_(nullptr)
  {
  }

  explicit OptionArgCallbackData(const Gio::Application* application,
    const Glib::ustring& long_name, gchar short_name,
    const Glib::ustring& description, const Glib::ustring& arg_description,
    const Glib::OptionGroup::SlotOptionArgFilename& slot)
  :
    application_(application),
    long_name_(g_strdup(long_name.c_str())), // must not be nullptr
    short_name_(short_name),
    description_(g_strdup(Glib::c_str_or_nullptr(description))),
    arg_description_(g_strdup(Glib::c_str_or_nullptr(arg_description))),
    slot_string_(nullptr),
    slot_filename_(new Glib::OptionGroup::SlotOptionArgFilename(slot))
  {
  }

  const Gio::Application* get_application() const { return application_; }
  const gchar* get_long_name() const { return long_name_; }
  gchar get_short_name() const { return short_name_; }
  const gchar* get_description() const { return description_; }
  const gchar* get_arg_description() const { return arg_description_; }
  bool is_filename_option() const { return slot_filename_ != nullptr; }

  const Glib::OptionGroup::SlotOptionArgString* get_slot_string() const { return slot_string_; }

  const Glib::OptionGroup::SlotOptionArgFilename* get_slot_filename() const
  {
    return slot_filename_;
  }

  ~OptionArgCallbackData()
  {
    g_free(long_name_);
    g_free(description_);
    g_free(arg_description_);
    delete slot_string_;
    delete slot_filename_;
    // Don't delete application_. It's not owned by this class.
  }

private:
  const Gio::Application* application_;
  gchar* long_name_;
  gchar short_name_;
  gchar* description_;
  gchar* arg_description_;
  // One of these slot pointers is nullptr and the other one points to a slot.
  Glib::OptionGroup::SlotOptionArgString* slot_string_;
  Glib::OptionGroup::SlotOptionArgFilename* slot_filename_;

  // Not copyable
  OptionArgCallbackData(const OptionArgCallbackData&) = delete;
  OptionArgCallbackData& operator=(const OptionArgCallbackData&) = delete;

}; // end class OptionArgCallbackData

using OptionArgCallbackDataMap = std::map<Glib::ustring, OptionArgCallbackData*>;
OptionArgCallbackDataMap option_arg_callback_data;

// Gio::Application instances may be used in different threads.
// Accesses to option_arg_callback_data must be thread-safe.
std::mutex option_arg_callback_data_mutex;

gboolean
Application_option_arg_callback(
  const gchar* option_name, const gchar* value, gpointer /* data */, GError** error)
{
  const Glib::ustring cpp_option_name(option_name);

  // option_name is either a single dash followed by a single letter (for a
  // short name) or two dashes followed by a long option name.
  std::unique_lock<std::mutex> lock(option_arg_callback_data_mutex);
  OptionArgCallbackDataMap::const_iterator iterFind = option_arg_callback_data.end();
  if (option_name[1] == '-')
  {
    // Long option name.
    const auto long_option_name = Glib::ustring(option_name + 2);
    iterFind = option_arg_callback_data.find(long_option_name);
  }
  else
  {
    // Short option name.
    const auto short_option_name = option_name[1];
    for (iterFind = option_arg_callback_data.begin(); iterFind != option_arg_callback_data.end();
         ++iterFind)
    {
      if (iterFind->second->get_short_name() == short_option_name)
        break;
    }
  }

  if (iterFind == option_arg_callback_data.end())
  {
    Glib::OptionError(Glib::OptionError::UNKNOWN_OPTION, "Application_option_arg_callback(): "
                                                         "Unknown option " +
                                                           cpp_option_name)
      .propagate(error);
    return false;
  }

  const bool has_value = (value != nullptr);
  const OptionArgCallbackData* const option_arg = iterFind->second;
  try
  {
    if (option_arg->is_filename_option())
    {
      const auto the_slot = option_arg->get_slot_filename();
      lock.unlock();
      const std::string cpp_value(value ? value : "");
      return (*the_slot)(cpp_option_name, cpp_value, has_value);
    }
    else
    {
      const auto the_slot = option_arg->get_slot_string();
      lock.unlock();
      const Glib::ustring cpp_value(value ? value : "");
      return (*the_slot)(cpp_option_name, cpp_value, has_value);
    }
  }
  catch (Glib::Error& err)
  {
    err.propagate(error);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return false;
}

} // anonymous namespace

namespace Gio
{

const Glib::Class&
Application::custom_class_init()
{
  Glib::init();
  Gio::init();
  return application_class_.init();
}

Application::Application(const Glib::ustring& application_id, Flags flags)
: // Mark this class as non-derived to allow C++ vfuncs to be skipped.
  // GApplication complains about "" but allows nullptr, so we avoid passing "".
  Glib::ObjectBase(nullptr),
  Glib::Object(Glib::ConstructParams(custom_class_init(),
    "application_id", Glib::c_str_or_nullptr(application_id),
    "flags", static_cast<GApplicationFlags>(flags), nullptr))
{
}

Application::~Application() noexcept
{
  // Delete all OptionArgCallbackData instances that belong to this application.
  std::lock_guard<std::mutex> lock(option_arg_callback_data_mutex);
  OptionArgCallbackDataMap::iterator iter = option_arg_callback_data.begin();
  while (iter != option_arg_callback_data.end())
  {
    OptionArgCallbackDataMap::iterator saved_iter = iter;
    ++iter;
    if (saved_iter->second->get_application() == this)
    {
      delete saved_iter->second;
      option_arg_callback_data.erase(saved_iter);
    }
  }
}

// static
void
Application::unset_default()
{
  g_application_set_default(nullptr);
}

void
Application_Class::open_callback(GApplication* self, GFile** files, gint n_files, const gchar* hint)
{
  const auto obj_base =
    static_cast<Glib::ObjectBase*>(Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:

  if (obj_base && obj_base->is_derived_())
  {
    const auto obj = dynamic_cast<CppObjectType* const>(obj_base);
    if (obj) // This can be nullptr during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        Application::type_vec_files vec_files(n_files);
        for (int i = 0; i < n_files; i++)
        {
          vec_files[i] = Glib::wrap(files[i], true);
        }

        const auto hint_str = (hint ? hint : Glib::ustring());

        obj->on_open(vec_files, hint_str);
        return;
      }
      catch (...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }

  const auto base = static_cast<BaseClassType*>(g_type_class_peek_parent(G_OBJECT_GET_CLASS(
    self)) // Get the parent class of the object class (The original underlying C class).
    );

  // Call the original underlying C function:
  if (base && base->open)
    (*base->open)(self, files, n_files, hint);
}

Glib::SignalProxy<void(const Application::type_vec_files&, const Glib::ustring&)>
Application::signal_open()
{
  return Glib::SignalProxy<void(const Application::type_vec_files&, const Glib::ustring&)>(
    this, &Application_signal_open_info);
}

void
Gio::Application::on_open(const Application::type_vec_files& files, const Glib::ustring& hint)
{
  const auto base = static_cast<BaseClassType*>(g_type_class_peek_parent(G_OBJECT_GET_CLASS(
    gobject_)) // Get the parent class of the object class (The original underlying C class).
    );

  if (base && base->open)
  {
    (*base->open)(gobj(),
      Glib::ArrayHandler<type_vec_files::value_type>::vector_to_array(files).data(), files.size(),
      hint.c_str());
  }
}

void
Application::open(const type_vec_files& files, const Glib::ustring& hint)
{
  g_application_open(gobj(),
    Glib::ArrayHandler<type_vec_files::value_type>::vector_to_array(files).data(), files.size(),
    hint.c_str());
}

void
Application::open(const Glib::RefPtr<Gio::File>& file, const Glib::ustring& hint)
{
  type_vec_files files(1);
  files[0] = file;
  open(files, hint);
}

void
Application::add_main_option_entry(OptionType arg_type, const Glib::ustring& long_name,
  gchar short_name, const Glib::ustring& description, const Glib::ustring& arg_description,
  Glib::OptionEntry::Flags flags)
{
  // g_application_add_main_option() saves copies of the strings.
  // No need to keep copies in Gio::Application.
  g_application_add_main_option(gobj(), long_name.c_str(), short_name,
    static_cast<GOptionFlags>(flags), static_cast<GOptionArg>(arg_type),
    description.c_str(), Glib::c_str_or_nullptr(arg_description));
}

void
Application::add_main_option_entry(const Glib::OptionGroup::SlotOptionArgString& slot,
  const Glib::ustring& long_name, gchar short_name, const Glib::ustring& description,
  const Glib::ustring& arg_description, Glib::OptionEntry::Flags flags)
{
  OptionArgCallbackData* callback_data = nullptr;
  {
    std::lock_guard<std::mutex> lock(option_arg_callback_data_mutex);
    OptionArgCallbackDataMap::iterator iterFind = option_arg_callback_data.find(long_name);
    if (iterFind != option_arg_callback_data.end())
      return; // Ignore duplicates

    callback_data = new OptionArgCallbackData(
      this, long_name, short_name, description, arg_description, slot);
    option_arg_callback_data[long_name] = callback_data;
  } // option_arg_callback_data_mutex.unlock()

  add_main_option_entry_private(callback_data->get_long_name(), short_name,
    callback_data->get_description(), callback_data->get_arg_description(),
    flags & ~Glib::OptionEntry::Flags::FILENAME);
}

void
Application::add_main_option_entry_filename(const Glib::OptionGroup::SlotOptionArgFilename& slot,
  const Glib::ustring& long_name, gchar short_name, const Glib::ustring& description,
  const Glib::ustring& arg_description, Glib::OptionEntry::Flags flags)
{
  OptionArgCallbackData* callback_data = nullptr;
  {
    std::lock_guard<std::mutex> lock(option_arg_callback_data_mutex);
    OptionArgCallbackDataMap::iterator iterFind = option_arg_callback_data.find(long_name);
    if (iterFind != option_arg_callback_data.end())
      return; // Ignore duplicates

    callback_data = new OptionArgCallbackData(
      this, long_name, short_name, description, arg_description, slot);
    option_arg_callback_data[long_name] = callback_data;
  } // option_arg_callback_data_mutex.unlock()

  add_main_option_entry_private(callback_data->get_long_name(), short_name,
    callback_data->get_description(), callback_data->get_arg_description(),
    flags | Glib::OptionEntry::Flags::FILENAME);
}

void
Application::add_main_option_entry_private(const gchar* long_name,
  gchar short_name, const gchar* description,
  const gchar* arg_description, Glib::OptionEntry::Flags flags)
{
  // g_application_add_main_option() can't be used for options with
  // a callback slot, because GOptionEntry.arg_data must be non-null.

  // Create a temporary array, just so we can give the correct thing to
  // g_application_add_main_option_entries():
  GOptionEntry array[2];
  std::memset(array, 0, 2 * sizeof(GOptionEntry)); // null-termination

  // g_application_add_main_option_entries() does not take its own copy
  // of the strings. We must keep them alive, and keep pointers to them,
  // so we can delete them when the Application instance is deleted.
  // This is handled in OptionArgCallbackData.

  // Fill in array[0].
  array[0].arg = G_OPTION_ARG_CALLBACK;
  array[0].long_name = long_name;
  array[0].short_name = short_name;
  array[0].description = description;
  array[0].arg_description = arg_description;
  array[0].flags = static_cast<int>(flags);

  // GoptionEntry.arg_data is a function pointer, cast to void*.
  // See Glib::OptionGroup::CppOptionEntry::allocate_c_arg() for a discussion
  // of this hack.
  union {
    void* dp;
    GOptionArgFunc fp;
  } u;
  u.fp = &Application_option_arg_callback;
  array[0].arg_data = u.dp;

  g_application_add_main_option_entries(gobj(), array);
}

void
Application::unset_resource_base_path()
{
  g_application_set_resource_base_path(gobj(), nullptr /* see the C docs. */);
}

} // namespace Gio