aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/copy_from_profile_menu.cpp
blob: c97002cdbb7b3c25d2c032fd46bd7d39d9ebab75 (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
/* copy_from_profile_menu.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/widgets/copy_from_profile_menu.h>
#include <ui/qt/models/profile_model.h>
#include <wsutil/filesystem.h>

#include <QDir>
#include <QFileInfo>

CopyFromProfileMenu::CopyFromProfileMenu(QString filename, QWidget *parent) :
    QMenu(parent),
    have_profiles_(false)
{
    ProfileModel model(this);

    QList<QAction *> global;
    QList<QAction *> user;

    QAction * pa = systemDefault(filename);
    if ( pa )
        global << pa;

    for(int cnt = 0; cnt < model.rowCount(); cnt++)
    {
        QModelIndex idx = model.index(cnt, ProfileModel::COL_NAME);
        QString profilePath = idx.data(ProfileModel::DATA_PATH).toString();
        if ( ! idx.isValid() || profilePath.isEmpty() )
            continue;

        if ( ! idx.data(ProfileModel::DATA_PATH_IS_NOT_DESCRIPTION).toBool() || idx.data(ProfileModel::DATA_IS_SELECTED).toBool() )
            continue;

        QDir profileDir(profilePath);
        if ( ! profileDir.exists() )
            continue;

        QFileInfo fi = profileDir.filePath(filename);
        if ( ! fi.exists() )
            continue;

        if ( ! config_file_exists_with_entries(fi.absoluteFilePath().toUtf8().constData(), '#') )
            continue;

        QString name = idx.data().toString();
        pa = new QAction(name, this);
        if ( idx.data(ProfileModel::DATA_IS_DEFAULT).toBool() )
            addAction(pa);
        else if ( idx.data(ProfileModel::DATA_IS_GLOBAL).toBool() )
            global << pa;
        else
            user << pa;

        pa->setFont(idx.data(Qt::FontRole).value<QFont>());
        pa->setProperty("profile_name", name);
        pa->setProperty("profile_is_global", idx.data(ProfileModel::DATA_IS_GLOBAL));

        pa->setProperty("filename", fi.absoluteFilePath());
        pa->setData(fi.absoluteFilePath().toUtf8().constData());
    }

    addActions(user);
    if (global.count() > 0)
    {
        if ( actions().count() > 0 )
            addSeparator();
        addActions(global);
    }

    have_profiles_ = actions().count() > 0;
}

bool CopyFromProfileMenu::haveProfiles()
{
    return have_profiles_;
}

// "System default" is not a profile.
// Add a special entry for this if the filename exists.
QAction * CopyFromProfileMenu::systemDefault(QString filename)
{
    QAction * data = Q_NULLPTR;

    QDir dataDir(get_datafile_dir());
    QString path = dataDir.filePath(filename);
    if ( QFile::exists(path) )
    {
        data = new QAction(tr("System default"), this);
        data->setData(path);
        QFont font = data->font();
        font.setItalic(true);
        data->setFont(font);
    }

    return data;
}