Qt: Add copy from another profile in UAT dialogs
authorStig Bjørlykke <stig@bjorlykke.org>
Fri, 5 Oct 2018 06:33:24 +0000 (08:33 +0200)
committerStig Bjørlykke <stig@bjorlykke.org>
Fri, 5 Oct 2018 12:38:32 +0000 (12:38 +0000)
Add a new button to UAT dialogs to copy entries from another profile.

Change-Id: I641ba764d8738f738466529d74d4a21ff13075a0
Reviewed-on: https://code.wireshark.org/review/30028
Petri-Dish: Stig Bjørlykke <stig@bjorlykke.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
epan/dissectors/packet-dof.c
epan/uat.c
epan/uat.h
epan/uat_load.l
ui/qt/io_graph_dialog.cpp
ui/qt/models/uat_model.cpp
ui/qt/models/uat_model.h
ui/qt/sctp_chunk_statistics_dialog.cpp
ui/qt/uat_dialog.cpp
ui/qt/uat_dialog.h
ui/qt/uat_frame.cpp

index 90eb267ccd9edc56d2f2199a4130b637e1472c48..1049b4cd8f45d7802c6f60d2ee313c9d0436d004 100644 (file)
@@ -10960,9 +10960,9 @@ static void dof_register(void)
                                   "A table of secrets for different identities.",
                                   identsecret_uat);
 
-    uat_load(secmode_uat, &uat_load_err);
-    uat_load(seckey_uat, &uat_load_err);
-    uat_load(identsecret_uat, &uat_load_err);
+    uat_load(secmode_uat, NULL, &uat_load_err);
+    uat_load(seckey_uat, NULL, &uat_load_err);
+    uat_load(identsecret_uat, NULL, &uat_load_err);
 }
 
 static void dof_handoff(void)
index 13b9854798a273d253cae0855960255031400764..43315e9419645c7bb8fd564fd75fb896d64a57fb 100644 (file)
@@ -529,7 +529,7 @@ void uat_load_all(void) {
 
         if (!u->loaded) {
             err = NULL;
-            if (!uat_load(u, &err)) {
+            if (!uat_load(u, NULL, &err)) {
                 report_failure("Error loading table '%s': %s",u->name,err);
                 g_free(err);
             }
index f0426ce1ecf4da432c5f2ec53146168a0f0cf6c0..89fba7ed6e6a10f47024866fad49355f293b31b8 100644 (file)
@@ -301,12 +301,13 @@ void uat_cleanup(void);
 /** Populate a uat using its file.
  *
  * @param uat_in Pointer to a uat. Must not be NULL.
+ * @param filename Filename to load, NULL to fetch from current profile.
  * @param err Upon failure, points to an error string.
  *
  * @return TRUE on success, FALSE on failure.
  */
 WS_DLL_PUBLIC
-gboolean uat_load(uat_t* uat_in, char** err);
+gboolean uat_load(uat_t* uat_in, const gchar *filename, char** err);
 
 /** Create or update a single uat entry using a string.
  *
index 981a3621399188a5eedc5eb8c63dc13a4eaafe93..1aceb6a703de3c2aa7ded5e0094f0c9099b7b4b1 100644 (file)
@@ -357,13 +357,19 @@ comment #[^\n]*\n
 DIAG_ON_FLEX
 
 gboolean
-uat_load(uat_t *uat, char **errx)
+uat_load(uat_t *uat, const gchar *filename, char **errx)
 {
-       gchar *fname = uat_get_actual_filename(uat, FALSE);
+       gchar *fname;
        FILE *in;
        yyscan_t scanner;
        uat_load_scanner_state_t state;
 
+       if (filename) {
+               fname = g_strdup(filename);
+       } else {
+               fname = uat_get_actual_filename(uat, FALSE);
+       }
+
        if (!fname) {
                UAT_UPDATE(uat);
 
index 132e3574ffacddb16b7af7e83e8611b6bb024178..3adf01dda762e3b317a66e83ef3710aecb536bf0 100644 (file)
@@ -1134,7 +1134,7 @@ void IOGraphDialog::loadProfileGraphs()
                            io_graph_fields);
 
         char* err = NULL;
-        if (!uat_load(iog_uat_, &err)) {
+        if (!uat_load(iog_uat_, NULL, &err)) {
             report_failure("Error while loading %s: %s.  Default graph values will be used", iog_uat_->name, err);
             g_free(err);
             uat_clear(iog_uat_);
index a5adcdde7841cf98164dc643844db880e0e0037e..e482bb6d37ff98ae9c61c58f9097bb2c30690efd 100644 (file)
@@ -44,6 +44,13 @@ void UatModel::loadUat(epan_uat * uat)
     }
 }
 
+void UatModel::reloadUat()
+{
+    beginResetModel();
+    loadUat(uat_);
+    endResetModel();
+}
+
 Qt::ItemFlags UatModel::flags(const QModelIndex &index) const
 {
     if (!index.isValid())
index 489992c26b4847282bef0b36a6dc7d8054dec03c..d7ffa62d4fec575f24e29ee63e849ae1d098b0ea 100644 (file)
@@ -44,6 +44,7 @@ public:
     bool copyRow(int dst_row, int src_row);
     bool moveRow(int src_row, int dst_row);
 
+    void reloadUat();
     bool hasErrors() const;
     void clearAll();
 
index e2f37a801175fdf44f4751527b630936d3cbbfcd..aa35c01207d47aa1a009924ac3f2b40fa19d9f6b 100644 (file)
@@ -297,7 +297,7 @@ void SCTPChunkStatisticsDialog::on_actionChunkTypePreferences_triggered()
     uat_t *uat = prefs_get_uat_value(pref);
     uat_clear(uat);
 
-    if (!uat_load(uat, &err)) {
+    if (!uat_load(uat, NULL, &err)) {
         /* XXX - report this through the GUI */
         g_log(NULL, G_LOG_LEVEL_WARNING, "Error loading table '%s': %s", uat->name, err);
         g_free(err);
index 607c3ef8ef427e0d9df0c2a9df54ec3b33247ec1..c0318f6b36f9bad44711b79594f0309a04470a99 100644 (file)
 #include "ui/help_url.h"
 #include <wsutil/report_message.h>
 
+#include <ui/qt/widgets/copy_from_profile_button.h>
 #include <ui/qt/utils/qt_ui_utils.h>
 
 #include <QDesktopServices>
 #include <QPushButton>
 #include <QUrl>
+#include <QMenu>
 
 #include <QDebug>
 
@@ -39,6 +41,12 @@ UatDialog::UatDialog(QWidget *parent, epan_uat *uat) :
     ok_button_ = ui->buttonBox->button(QDialogButtonBox::Ok);
     help_button_ = ui->buttonBox->button(QDialogButtonBox::Help);
 
+    if (uat->from_profile) {
+        QPushButton *copy_button = new CopyFromProfileButton(uat->filename);
+        ui->buttonBox->addButton(copy_button, QDialogButtonBox::ApplyRole);
+        connect(copy_button->menu(), SIGNAL(triggered(QAction *)), this, SLOT(copyFromProfile(QAction *)));
+    }
+
 #ifdef Q_OS_MAC
     ui->newToolButton->setAttribute(Qt::WA_MacSmallSize, true);
     ui->deleteToolButton->setAttribute(Qt::WA_MacSmallSize, true);
@@ -77,6 +85,20 @@ UatDialog::~UatDialog()
     delete uat_model_;
 }
 
+void UatDialog::copyFromProfile(QAction *action)
+{
+    QString filename = action->data().toString();
+
+    gchar *err = NULL;
+    if (uat_load(uat_, filename.toUtf8().constData(), &err)) {
+        uat_->changed = TRUE;
+        uat_model_->reloadUat();
+    } else {
+        report_failure("Error while loading %s: %s", uat_->name, err);
+        g_free(err);
+    }
+}
+
 void UatDialog::setUat(epan_uat *uat)
 {
     QString title(tr("Unknown User Accessible Table"));
@@ -152,7 +174,7 @@ void UatDialog::modelRowsRemoved()
 void UatDialog::modelRowsReset()
 {
     ui->deleteToolButton->setEnabled(false);
-    ui->clearToolButton->setEnabled(false);
+    ui->clearToolButton->setEnabled(uat_model_->rowCount() != 0);
     ui->copyToolButton->setEnabled(false);
     ui->moveUpToolButton->setEnabled(false);
     ui->moveDownToolButton->setEnabled(false);
@@ -344,7 +366,7 @@ void UatDialog::rejectChanges()
     if (uat_->changed) {
         gchar *err = NULL;
         uat_clear(uat_);
-        if (!uat_load(uat_, &err)) {
+        if (!uat_load(uat_, NULL, &err)) {
             report_failure("Error while loading %s: %s", uat_->name, err);
             g_free(err);
         }
index 66bde76abfbdccd05ee2ccf09d75adc567868733..7bce5434dca68d64fbde1030cd66d3959db39b66 100644 (file)
@@ -38,6 +38,7 @@ public:
     void setUat(struct epan_uat *uat = NULL);
 
 private slots:
+    void copyFromProfile(QAction *action);
     void modelDataChanged(const QModelIndex &topLeft);
     void modelRowsRemoved();
     void modelRowsReset();
index ed0e5f8916e850cbc011acd2f81414c1674c257d..14b222d07bcba36608620f2c1649f4de0f032752 100644 (file)
@@ -145,7 +145,7 @@ void UatFrame::rejectChanges()
     if (uat_->changed) {
         gchar *err = NULL;
         uat_clear(uat_);
-        if (!uat_load(uat_, &err)) {
+        if (!uat_load(uat_, NULL, &err)) {
             report_failure("Error while loading %s: %s", uat_->name, err);
             g_free(err);
         }