FilterExpressionToolBar::FilterExpressionToolBar(QWidget * parent) :
    DragDropToolBar(parent)
{
    // Try to draw 1-pixel-wide separator lines from the button label
    // ascent to its baseline.
    int sep_margin = (fontMetrics().height() * 0.5) - 1;
    QColor sep_color = ColorUtils::alphaBlend(palette().text(), palette().base(), 0.3);
    setStyleSheet(QString(
                "QToolBar { background: none; border: none; spacing: 1px; }"
                "QFrame {"
                "  min-width: 1px; max-width: 1px;"
                "  margin: %1px 0 %2px 0; padding: 0;"
                "  background-color: %3;"
                "}"
                ).arg(sep_margin).arg(sep_margin - 1).arg(sep_color.name()));

    setContextMenuPolicy(Qt::CustomContextMenu);

    connect ( this, &QWidget::customContextMenuRequested, this, &FilterExpressionToolBar::onCustomMenuHandler );
    connect(this, &DragDropToolBar::actionMoved, this, &FilterExpressionToolBar::onActionMoved);
    connect(this, &DragDropToolBar::newFilterDropped, this, &FilterExpressionToolBar::onFilterDropped);

    connect(wsApp, SIGNAL(appInitialized()),
            this, SLOT(filterExpressionsChanged()));
    connect(wsApp, SIGNAL(filterExpressionsChanged()),
            this, SLOT(filterExpressionsChanged()));

}
FilterExpressionsPreferencesFrame::FilterExpressionsPreferencesFrame(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::FilterExpressionsPreferencesFrame),
    cur_line_edit_(NULL)
{
    ui->setupUi(this);

    int one_em = ui->expressionTreeWidget->fontMetrics().height();
    ui->expressionTreeWidget->resizeColumnToContents(enabled_col_);
    ui->expressionTreeWidget->setColumnWidth(label_col_, one_em * 10);
    ui->expressionTreeWidget->setColumnWidth(expression_col_, one_em * 5);

    ui->expressionTreeWidget->setMinimumWidth(one_em * 15);
    ui->expressionTreeWidget->setMinimumHeight(one_em * 10);

    ui->expressionTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->expressionTreeWidget->setDragEnabled(true);
    ui->expressionTreeWidget->viewport()->setAcceptDrops(true);
    ui->expressionTreeWidget->setDropIndicatorShown(true);
    ui->expressionTreeWidget->setDragDropMode(QAbstractItemView::InternalMove);

    updateWidgets();

    connect(wsApp, SIGNAL(filterExpressionsChanged()), this, SLOT(updateWidgets()));
}
void FilterExpressionFrame::on_okButton_clicked()
{
    QByteArray label_ba = ui->labelLineEdit->text().toUtf8();
    QByteArray expr_ba = ui->displayFilterLineEdit->text().toUtf8();

    filter_expression_new(label_ba.constData(), expr_ba.constData(), TRUE);

    on_cancelButton_clicked();
    emit filterExpressionsChanged();
    prefs_main_write();
}
void FilterExpressionToolBar::onFilterDropped(QString description, QString filter)
{
    if ( filter.length() == 0 )
        return;

    filter_expression_new(qUtf8Printable(description),
            qUtf8Printable(filter), qUtf8Printable(description), TRUE);

    save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
    filterExpressionsChanged();
}
void FilterExpressionToolBar::disableFilter()
{
    QString label = ((QAction *)sender())->property(dfe_property_label_).toString();
    QString expr = ((QAction *)sender())->property(dfe_property_expression_).toString();

    int idx = uatRowIndexForFilter(label, expr);
    UatModel * uatModel = new UatModel(this, "Display expressions");

    QModelIndex rowIndex = uatModel->index(idx, 0);
    if ( rowIndex.isValid() ) {
        uatModel->setData(rowIndex, QVariant::fromValue(false));

        save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
        filterExpressionsChanged();
    }
}
void FilterExpressionToolBar::removeFilter()
{
    UatModel * uatModel = new UatModel(this, "Display expressions");

    QString label = ((QAction *)sender())->property(dfe_property_label_).toString();
    QString expr = ((QAction *)sender())->property(dfe_property_expression_).toString();

    int idx = uatRowIndexForFilter(label, expr);

    QModelIndex rowIndex = uatModel->index(idx, 0);
    if ( rowIndex.isValid() ) {
        uatModel->removeRow(rowIndex.row());

        save_migrated_uat("Display expressions", &prefs.filter_expressions_old);
        filterExpressionsChanged();
    }
}
void WiresharkApplication::setConfigurationProfile(const gchar *profile_name)
{
    char  *gdp_path, *dp_path;
    char  *rf_path;
    int    rf_open_errno;
    gchar *err_msg = NULL;

    /* First check if profile exists */
    if (!profile_exists(profile_name, FALSE)) {
        if (profile_exists(profile_name, TRUE)) {
            char  *pf_dir_path, *pf_dir_path2, *pf_filename;
            /* Copy from global profile */
            if (create_persconffile_profile(profile_name, &pf_dir_path) == -1) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Can't create directory\n\"%s\":\n%s.",
                    pf_dir_path, g_strerror(errno));

                g_free(pf_dir_path);
            }

            if (copy_persconffile_profile(profile_name, profile_name, TRUE, &pf_filename,
                    &pf_dir_path, &pf_dir_path2) == -1) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Can't copy file \"%s\" in directory\n\"%s\" to\n\"%s\":\n%s.",
                    pf_filename, pf_dir_path2, pf_dir_path, g_strerror(errno));

                g_free(pf_filename);
                g_free(pf_dir_path);
                g_free(pf_dir_path2);
            }
        } else {
            /* No personal and no global profile exists */
            return;
        }
    }

    /* Then check if changing to another profile */
    if (profile_name && strcmp (profile_name, get_profile_name()) == 0) {
        return;
    }

    /* Get the current geometry, before writing it to disk */
    emit profileChanging();

    if (profile_exists(get_profile_name(), FALSE)) {
        /* Write recent file for profile we are leaving, if it still exists */
        write_profile_recent();
    }

    /* Set profile name and update the status bar */
    set_profile_name (profile_name);
    emit profileNameChanged(profile_name);

    /* Apply new preferences */
    readConfigurationFiles (&gdp_path, &dp_path, true);

    if (!recent_read_profile_static(&rf_path, &rf_open_errno)) {
        simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
            "Could not open common recent file\n\"%s\": %s.",
            rf_path, g_strerror(rf_open_errno));
        g_free(rf_path);
    }
    if (recent.gui_fileopen_remembered_dir &&
        test_for_directory(recent.gui_fileopen_remembered_dir) == EISDIR) {
        set_last_open_dir(recent.gui_fileopen_remembered_dir);
    }
    timestamp_set_type (recent.gui_time_format);
    timestamp_set_precision(recent.gui_time_precision);
    timestamp_set_seconds_type (recent.gui_seconds_format);
    packet_list_enable_color(recent.packet_list_colorize);
    tap_update_timer_.setInterval(prefs.tap_update_interval);

    prefs_to_capture_opts();
    prefs_apply_all();
#ifdef HAVE_LIBPCAP
    update_local_interfaces();
#endif

    setMonospaceFont(prefs.gui_qt_font_name);

    emit columnsChanged();
    emit preferencesChanged();
    emit recentFilesRead();
    emit filterExpressionsChanged();
    emit checkDisplayFilter();

    /* Enable all protocols and disable from the disabled list */
    proto_enable_all();
    if (gdp_path == NULL && dp_path == NULL) {
        set_disabled_protos_list();
        set_disabled_heur_dissector_list();
    }

    /* Reload color filters */
    if (!color_filters_reload(&err_msg, color_filter_add_cb)) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
        g_free(err_msg);
    }

    emit localInterfaceListChanged();
    emit packetDissectionChanged();
}
void WiresharkApplication::setConfigurationProfile(const gchar *profile_name)
{
    char  *gdp_path, *dp_path;
    char  *rf_path;
    int    rf_open_errno;

    /* First check if profile exists */
    if (!profile_exists(profile_name, FALSE)) {
        if (profile_exists(profile_name, TRUE)) {
            char  *pf_dir_path, *pf_dir_path2, *pf_filename;
            /* Copy from global profile */
            if (create_persconffile_profile(profile_name, &pf_dir_path) == -1) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Can't create directory\n\"%s\":\n%s.",
                    pf_dir_path, g_strerror(errno));

                g_free(pf_dir_path);
            }

            if (copy_persconffile_profile(profile_name, profile_name, TRUE, &pf_filename,
                    &pf_dir_path, &pf_dir_path2) == -1) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Can't copy file \"%s\" in directory\n\"%s\" to\n\"%s\":\n%s.",
                    pf_filename, pf_dir_path2, pf_dir_path, g_strerror(errno));

                g_free(pf_filename);
                g_free(pf_dir_path);
                g_free(pf_dir_path2);
            }
        } else {
            /* No personal and no global profile exists */
            return;
        }
    }

    /* Then check if changing to another profile */
    if (profile_name && strcmp (profile_name, get_profile_name()) == 0) {
        return;
    }

    /* Get the current geometry, before writing it to disk */
    emit profileChanging();

    if (profile_exists(get_profile_name(), FALSE)) {
        /* Write recent file for profile we are leaving, if it still exists */
        write_profile_recent();
    }

    /* Set profile name and update the status bar */
    set_profile_name (profile_name);
    emit profileNameChanged(profile_name);

    /* Reset current preferences and apply the new */
    prefs_reset();
//    menu_prefs_reset();

    (void) readConfigurationFiles (&gdp_path, &dp_path);

    recent_read_profile_static(&rf_path, &rf_open_errno);
    if (rf_path != NULL && rf_open_errno != 0) {
        simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
            "Could not open common recent file\n\"%s\": %s.",
            rf_path, g_strerror(rf_open_errno));
    }
    if (recent.gui_fileopen_remembered_dir &&
        test_for_directory(recent.gui_fileopen_remembered_dir) == EISDIR) {
        set_last_open_dir(recent.gui_fileopen_remembered_dir);
    }
    timestamp_set_type (recent.gui_time_format);
    timestamp_set_seconds_type (recent.gui_seconds_format);
    color_filters_enable(recent.packet_list_colorize);
    tap_update_timer_.setInterval(prefs.tap_update_interval);

    prefs_to_capture_opts();
    prefs_apply_all();

    emit preferencesChanged();
    emit columnsChanged();
    emit recentFilesRead();
    emit filterExpressionsChanged();
//    macros_post_update();

    /* Enable all protocols and disable from the disabled list */
    proto_enable_all();
    if (gdp_path == NULL && dp_path == NULL) {
        set_disabled_protos_list();
        set_disabled_heur_dissector_list();
    }

    /* Reload color filters */
    color_filters_reload();

//    user_font_apply();

    /* Update menus with new recent values */
    //    menu_recent_read_finished();
}