void ApplicationController::onMenuRebootClick()
{
    Evas_Object *table = createPaddingTable(evas, layout, 260, 200);

    Evas_Object *glist = elm_genlist_add(layout);
    elm_object_style_set(glist, "calaos");
    elm_genlist_select_mode_set(glist, ELM_OBJECT_SELECT_MODE_ALWAYS);
    evas_object_size_hint_fill_set(glist, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_size_hint_weight_set(glist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_show(glist);

    Evas_Object *popup = elm_ctxpopup_add(layout);
    elm_object_style_set(popup, "calaos");
    evas_object_size_hint_min_set(popup, 300, 240);

    GenlistItemSimple *item;

    item = new GenlistItemSimple(evas, glist, _("Restart application only"), true);
    item->Append(glist);
    item->item_selected.connect([this, popup](void *)
    {
        elm_ctxpopup_dismiss(popup);
        menuView->CloseLinkMenu();

        system("systemctl restart calaos-home");
    });

    item = new GenlistItemSimple(evas, glist, _("Reboot machine"), true);
    item->Append(glist);
    item->item_selected.connect([this, popup](void *)
    {
        elm_ctxpopup_dismiss(popup);
        menuView->CloseLinkMenu();

        system("sync");
        system("reboot");
    });

    elm_table_pack(table, glist, 1, 1, 1, 1);

    elm_object_content_set(popup, table);

    Evas_Coord x,y;
    evas_pointer_canvas_xy_get(evas, &x, &y);
    evas_object_move(popup, x, y);
    evas_object_show(popup);
}
void ActivityAudioListView::itemListLoaded(list<Params> &infos)
{
    list<Params>::iterator it = infos.begin();

    CREATE_GENLIST_HELPER(glist);

    for (;it != infos.end();it++)
    {
        Params &pitem = *it;

        if (pitem["type"] == "track")
        {
            int track_id;
            from_string(pitem["id"], track_id);
            GenlistItemTrack *item = new GenlistItemTrack(evas, parent, player_current->getPlayer(), track_id, GenlistItemTrack::TRACK_ID, 0);
            item->Append(glist, NULL);
        }
        else if (pitem["type"] == "folder")
        {
            GenlistItemSimple *item = new GenlistItemSimple(evas, parent, pitem["name"], true);
            item->setIcon("calaos/icons/genlist/folder");
            item->Append(glist);
            item->setUserData(item);
            item->item_selected.connect(sigc::bind(sigc::mem_fun(*this, &ActivityAudioListView::folderSelected),
                                                   pitem["id"]));
        }
        else if (pitem["type"] == "playlist")
        {
            GenlistItemBrowserPlaylist *item = new GenlistItemBrowserPlaylist(evas, parent, player_current->getPlayer(), pitem);
            item->Append(glist);
            item->setUserData(item);
            item->item_selected.connect(sigc::mem_fun(*this, &ActivityAudioListView::playlistSelected));
        }
    }

    elm_naviframe_item_push(pager_browser, NULL, NULL, NULL, glist, "calaos");

    EmitSignal("browser,loading,stop", "calaos");
}
void ActivityAudioListView::itemRadioLoaded(list<Params> &infos)
{
    list<Params>::iterator it = infos.begin();

    CREATE_GENLIST_HELPER(glist);
    elm_genlist_homogeneous_set(glist, false);
    elm_genlist_mode_set(glist, ELM_LIST_COMPRESS);

    for (;it != infos.end();it++)
    {
        Params &pitem = *it;

        cDebug() << "item type: " << pitem.toString();

        if (pitem["type"] == "xmlbrowser")
        {
            GenlistItemSimple *item = new GenlistItemSimple(evas, parent, pitem["name"], true);
            item->setIcon("calaos/icons/genlist/radio");
            item->Append(glist);
            item->setUserData(item);
            item->item_selected.connect(sigc::bind(
                                            sigc::mem_fun(*this, &ActivityAudioListView::radioSelected),
                                            pitem["id"], ""));
        }
        else
        {
            if (pitem["isaudio"] == "1")
            {
                pitem.Add("radio_id", radio_id);
                GenlistItemRadio *item = new GenlistItemRadio(evas, parent, player_current->getPlayer(), pitem);
                item->Append(glist);

                if (pitem["hasitems"] == "1")
                {
                    item->setUserData(item);
                    item->item_selected.connect(sigc::bind(
                                                    sigc::mem_fun(*this, &ActivityAudioListView::radioSelected),
                                                    radio_id ,pitem["id"]));
                }
            }
            else
            {
                if (pitem["hasitems"] == "1" && pitem["type"] != "search" && pitem["type"] != "xmlbrowser_search")
                {
                    GenlistItemSimple *item;
                    if (pitem["type"] == "text" || pitem["type"] == "textarea")
                        item = new GenlistItemSimple(evas, parent, pitem["name"], true, true);
                    else
                        item = new GenlistItemSimple(evas, parent, pitem["name"], true);
                    item->setIcon("calaos/icons/genlist/folder");
                    item->Append(glist);
                    item->setUserData(item);
                    item->item_selected.connect(sigc::bind(
                                                    sigc::mem_fun(*this, &ActivityAudioListView::radioSelected),
                                                    radio_id ,pitem["id"]));
                }
                else
                {
                    GenlistItemSimple *item = new GenlistItemSimple(evas, parent, pitem["name"], false, true);
                    item->Append(glist);

                    if (pitem["type"] == "search" || pitem["type"] == "xmlbrowser_search")
                    {
                        item->setIcon("calaos/icons/genlist/search");
                        item->item_selected.connect(sigc::bind(
                                                        sigc::mem_fun(*this, &ActivityAudioListView::searchRadioSelected),
                                                        radio_id ,pitem["id"]));
                    }
                }
            }
        }
    }

    elm_naviframe_item_push(pager_browser, NULL, NULL, NULL, glist, "calaos");

    EmitSignal("browser,loading,stop", "calaos");
}
void ActivityScheduleScenarioView::showWeekSelection(void *data, Evas_Object *edje_object, string emission, string source)
{
    if (cycle)
    {
        if (edit_range.end_type == TimeRange::HTYPE_NORMAL ||
            edit_range.end_offset != 0)
        {
            edit_range.ehour = Utils::to_string(elm_spinner_value_get(spin_end_hours));
            edit_range.emin = Utils::to_string(elm_spinner_value_get(spin_end_min));
            edit_range.esec = Utils::to_string(elm_spinner_value_get(spin_end_sec));
        }
    }
    else
    {
        if (edit_range.start_type == TimeRange::HTYPE_NORMAL ||
            edit_range.start_offset != 0)
        {
            edit_range.shour = Utils::to_string(elm_spinner_value_get(spin_start_hours));
            edit_range.smin = Utils::to_string(elm_spinner_value_get(spin_start_min));
            edit_range.ssec = Utils::to_string(elm_spinner_value_get(spin_start_sec));
        }

        edit_range.end_type = edit_range.start_type;
        edit_range.end_offset = edit_range.start_offset;
        edit_range.ehour = edit_range.shour;
        edit_range.emin = edit_range.smin;
        edit_range.esec = edit_range.ssec;
    }

    Evas_Object *table = createPaddingTable(evas, parent, 300, 300);

    Evas_Object *glist = elm_genlist_add(table);
    elm_object_style_set(glist, "calaos");
    elm_genlist_select_mode_set(glist, ELM_OBJECT_SELECT_MODE_ALWAYS);
    evas_object_size_hint_fill_set(glist, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_size_hint_weight_set(glist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    elm_genlist_multi_select_set(glist, true);
    evas_object_show(glist);

    string title_label = _("Days of the week<br><small><light_blue>Days of the week when scenario is executed.</light_blue></small>");
    GenlistItemSimpleHeader *header = new GenlistItemSimpleHeader(evas, glist, title_label, "navigation");
    header->Append(glist);

    if (cycle)
        header->setButtonLabel("button.back", _("End"));
    else
        header->setButtonLabel("button.back", _("Beginning"));
    header->button_click.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::headerWeekButtonClick));

    week_days.clear();
    week_days.reserve(8);
    GenlistItemSimple *item;

    for (int i = 0;i < 8;i++)
    {
        string label;
        switch (i)
        {
        case 0: label = _("Everyday"); break;
        case 1: label = _("Monday"); break;
        case 2: label = _("Tuesday"); break;
        case 3: label = _("Wednesday"); break;
        case 4: label = _("Thursday"); break;
        case 5: label = _("Friday"); break;
        case 6: label = _("Saturday"); break;
        case 7: label = _("Sunday"); break;
        default: label = _("ERROR");
        }

        item = new GenlistItemSimple(evas, glist, label, true, false, NULL, "check");
        item->Append(glist, header);
        week_days.push_back(item);

        if (i == 0)
        {
            item->item_selected.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::unselectWeekDays));
            item->setSelected(true);
        }
        else
        {
            item->item_selected.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::unselectAllWeekDays));
        }
    }

    elm_table_pack(table, glist, 1, 1, 1, 1);

    elm_naviframe_item_push(pager_popup, NULL, NULL, NULL, table, "calaos");
}
void ActivityScheduleScenarioView::selectTimeType(void *data)
{
    int *user_data = reinterpret_cast<int *>(data);

    if (editState == EDIT_START_TYPE)
    {
        editStatesHist.push(editState);
        edit_range.start_type = *user_data;
        if (edit_range.start_type == TimeRange::HTYPE_NORMAL)
            editState = EDIT_START_TIME;
        else
            editState = EDIT_START_OFFSET;
    }
    else if (editState == EDIT_END_TYPE)
    {
        edit_range.end_type = *user_data;
        editStatesHist.push(editState);
        if (edit_range.end_type == TimeRange::HTYPE_NORMAL)
            editState = EDIT_END_TIME;
        else
            editState = EDIT_END_OFFSET;
    }

    if (editState == EDIT_END_TIME ||
        editState == EDIT_START_TIME)
    {
        if (editState == EDIT_END_TIME)
            edit_range.end_type = TimeRange::HTYPE_NORMAL;
        else
            edit_range.start_type = TimeRange::HTYPE_NORMAL;
        showTimeSelection(NULL);
    }
    else
    {
        Evas_Object *table = createPaddingTable(evas, parent, 300, 300);

        Evas_Object *glist = elm_genlist_add(table);
        elm_object_style_set(glist, "calaos");
        elm_genlist_select_mode_set(glist, ELM_OBJECT_SELECT_MODE_ALWAYS);
        evas_object_size_hint_fill_set(glist, EVAS_HINT_FILL, EVAS_HINT_FILL);
        evas_object_size_hint_weight_set(glist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
        evas_object_show(glist);

        string title_label = _("<b>Offset time</b><br><light_blue><small>You can choose to shift the sun time</small></light_blue>");
        GenlistItemSimpleHeader *header = new GenlistItemSimpleHeader(evas, glist, title_label, "navigation_back");
        header->Append(glist);
        header->setButtonLabel("button.back", "Début");
        header->button_click.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::buttonHeaderBackClick));

        GenlistItemSimple *item = NULL;

        for (int i = 0;i < 3;i++)
        {
            switch (i)
            {
            case 0: item = new GenlistItemSimple(evas, glist, _("Use the exact sun time"), true, false, new int(i)); break;
            case 1: item = new GenlistItemSimple(evas, glist, _("Add time"), true, false, new int(i)); break;
            case 2: item = new GenlistItemSimple(evas, glist, _("Substract time"), true, false, new int(i)); break;
            default: break;
            }

            item->Append(glist, header);
            item->setAutoDeleteUserData(new DeletorT<int *>);
            item->item_selected.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::showTimeSelection));
        }

        elm_table_pack(table, glist, 1, 1, 1, 1);

        elm_naviframe_item_push(pager_popup, NULL, NULL, NULL, table, "calaos");
    }
}
ActivityScheduleScenarioView::ActivityScheduleScenarioView(Evas *_e, Evas_Object *_parent):
    ActivityView(_e, _parent, "calaos/page/schedule_scenario"),
    schedule_list(NULL),
    month_list(NULL)
{
    setPartText("header.label", _("Scheduling"));

    addCallback("button.*", "pressed", sigc::mem_fun(*this, &ActivityScheduleScenarioView::buttonPressed));

    schedule_list = elm_genlist_add(parent);
    Swallow(schedule_list, "schedule.list");
    elm_object_style_set(schedule_list, "calaos");
    elm_genlist_homogeneous_set(schedule_list, true);
    evas_object_show(schedule_list);

    month_list = elm_genlist_add(parent);
    Swallow(month_list, "month.list");
    elm_object_style_set(month_list, "calaos");
    elm_genlist_homogeneous_set(month_list, true);
    evas_object_show(month_list);
    elm_genlist_multi_select_set(month_list, true);

    GenlistItemScenarioHeader *header;
    GenlistItemSimple *item;
    header = new GenlistItemScenarioHeader(evas, parent, _("Month of year"));
    header->Append(month_list);

    item = new GenlistItemSimple(evas, parent, _("All year"), true, false, NULL, "check");
    item->Append(month_list);
    item_all = item;
    item_all->setSelected(true);

    item = new GenlistItemSimple(evas, parent, _("January"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("February"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("March"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("April"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("May"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("June"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("July"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("August"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("September"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("October"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("November"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    item = new GenlistItemSimple(evas, parent, _("December"), true, false, NULL, "check");
    item->Append(month_list);
    items_months.push_back(item);

    /*
         * Can't use that for now. Seasons are not the same all over the world at the same monthes
         * (northern hemisphere/southern hemisphere have the opposite) We need to handle that
         * maybe from longitude/latitude in local_config.xml
         *
        header = new GenlistItemScenarioHeader(evas, parent, "Périodes prédéfinies");
        header->Append(month_list);

        item = new GenlistItemSimple(evas, parent, _("Spring"), true, false, NULL, "check");
        item->Append(month_list);
        items_periods.push_back(item);

        item = new GenlistItemSimple(evas, parent, _("Summer"), true, false, NULL, "check");
        item->Append(month_list);
        items_periods.push_back(item);

        item = new GenlistItemSimple(evas, parent, _("Fall"), true, false, NULL, "check");
        item->Append(month_list);
        items_periods.push_back(item);

        item = new GenlistItemSimple(evas, parent, _("Winter"), true, false, NULL, "check");
        item->Append(month_list);
        items_periods.push_back(item);
    */

    //Set up selection callback
    item_all->item_selected.connect([=](void *data)
    {
        if (item_all->isSelected())
        {
            for (uint i = 0;i < items_months.size();i++)
                items_months[i]->setSelected(false);
        }
    });
    for (uint i = 0;i < items_months.size();i++)
    {
        GenlistItemSimple *it = items_months[i];
        it->item_selected.connect([=](void *data)
        {
            if (item_all->isSelected())
                item_all->setSelected(false);
        });
    }
//    for (uint i = 0;i < items_periods.size();i++)
//        items_periods[i]->item_selected.connect(sigc::bind(sigc::mem_fun(*this, &ActivityScheduleScenarioView::itemPeriodSelected), items_periods[i]));

    Evas_Object *btn = edje_object_part_external_object_get(edje, "button.valid");
    elm_object_text_set(btn, _("Apply"));

    btn = edje_object_part_external_object_get(edje, "button.add");
    elm_object_text_set(btn, _("Add"));

    setPartText("hours_text", _("<big><disabled>Hours of execution</disabled></big>"));
    setPartText("month_text", _("<big><disabled>Specific schedule</disabled></big>"));
}
void ActivityScheduleScenarioView::createTimeSelectTypeList(void *data, Evas_Object *edje_object, string emission, string source)
{
    if ((editState == EDIT_START_TIME || editState == EDIT_START_TIME_OFFSET) && cycle)
    {
        editStatesHist.push(editState);
        editState = EDIT_END_TYPE;
    }

    if (editState == EDIT_END_TYPE)
    {
        if (edit_range.start_type == TimeRange::HTYPE_NORMAL ||
            edit_range.start_offset != 0)
        {
            edit_range.shour = Utils::to_string(elm_spinner_value_get(spin_start_hours));
            edit_range.smin = Utils::to_string(elm_spinner_value_get(spin_start_min));
            edit_range.ssec = Utils::to_string(elm_spinner_value_get(spin_start_sec));
        }
        else
        {
            edit_range.shour = "0";
            edit_range.smin = "0";
            edit_range.ssec = "0";
        }
    }

    Evas_Object *table = createPaddingTable(evas, parent, 320, 300);

    Evas_Object *glist = elm_genlist_add(table);
    elm_object_style_set(glist, "calaos");
    elm_genlist_select_mode_set(glist, ELM_OBJECT_SELECT_MODE_ALWAYS);
    evas_object_size_hint_fill_set(glist, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_size_hint_weight_set(glist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_show(glist);

    string title_label;
    GenlistItemSimpleHeader *header = NULL;
    if (editState == EDIT_START_TYPE)
    {
        title_label = _("Start of schedulling<br><small><light_blue>Start time of the scenario</light_blue></small>");
        header = new GenlistItemSimpleHeader(evas, glist, title_label);
        header->Append(glist);
    }
    else
    {
        title_label = _("End of schedulling<br><small><light_blue>End time of the scenario</light_blue></small>");
        header = new GenlistItemSimpleHeader(evas, glist, title_label, "navigation_back");
        header->Append(glist);
        header->setButtonLabel("button.back", _("Start"));
        header->button_click.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::buttonHeaderBackClick));
    }

    GenlistItemSimple *item = nullptr;

    for (int i = 0;i < 4;i++)
    {
        switch (i)
        {
        case 0: item = new GenlistItemSimple(evas, glist, _("Use a fixed time"), true, false, new int(TimeRange::HTYPE_NORMAL)); break;
        case 1: item = new GenlistItemSimple(evas, glist, _("Use the sunrise"), true, false, new int(TimeRange::HTYPE_SUNRISE)); break;
        case 2: item = new GenlistItemSimple(evas, glist, _("Use the sunset"), true, false, new int(TimeRange::HTYPE_SUNSET)); break;
        case 3: item = new GenlistItemSimple(evas, glist, _("Use the midday sun"), true, false, new int(TimeRange::HTYPE_NOON)); break;
        default: break;
        }

        item->Append(glist, header);
        item->setAutoDeleteUserData(new DeletorT<int *>);
        item->item_selected.connect(sigc::mem_fun(*this, &ActivityScheduleScenarioView::selectTimeType));
    }

    elm_table_pack(table, glist, 1, 1, 1, 1);

    elm_naviframe_item_push(pager_popup, NULL, NULL, NULL, table, "calaos");
}