/* Copyright (C) 2006 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 <https://www.gnu.org/licenses/>.
 */

#include <glibmm/vectorutils.h>

#include <gtk/gtk.h>

namespace Gtk
{

std::vector<Glib::ustring> RecentInfo::get_applications() const
{
  gsize length = 0;
  char** const applications =
    gtk_recent_info_get_applications(const_cast<GtkRecentInfo*>(gobj()), &length);

  return Glib::ArrayHandler<Glib::ustring>::array_to_vector(applications, length, Glib::OWNERSHIP_DEEP);
}

bool RecentInfo::get_application_info(const Glib::ustring& app_name, std::string& app_exec,
                                      guint& count, Glib::DateTime& stamp) const
{
  const char* app_exec_cstr = nullptr;
  GDateTime* date_time = nullptr;
  const int found = gtk_recent_info_get_application_info(
      const_cast<GtkRecentInfo*>(gobj()), app_name.c_str(), &app_exec_cstr, &count, &date_time);
  stamp = Glib::wrap(date_time, true);

  if (app_exec_cstr)
    app_exec = app_exec_cstr;
  else
    app_exec.erase();

  return (found != 0);
}

std::vector<Glib::ustring> RecentInfo::get_groups() const
{
  gsize length = 0;
  char** const groups = gtk_recent_info_get_groups(const_cast<GtkRecentInfo*>(gobj()), &length);

  return Glib::ArrayHandler<Glib::ustring>::array_to_vector(groups, length, Glib::OWNERSHIP_DEEP);
}

RecentInfoTraits::CppType RecentInfoTraits::to_cpp_type(const CType& obj)
{
  return Glib::wrap(const_cast<CTypeNonConst>(obj), true);
}

} // namespace Gtk

namespace Glib
{

GType Value<RefPtr<Gtk::RecentInfo> >::value_type()
{
  return gtk_recent_info_get_type();
}

void Value<RefPtr<Gtk::RecentInfo> >::set(const CppType& data)
{
  set_boxed(Glib::unwrap(data));
}

Value<RefPtr<Gtk::RecentInfo> >::CppType Value<RefPtr<Gtk::RecentInfo> >::get() const
{
  return Glib::wrap(static_cast<CType>(get_boxed()), true);
}

} // namespace Glib
