File: profilewidget.cpp

package info (click to toggle)
wicd-kde 0.3.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,000 kB
  • sloc: cpp: 2,963; sh: 3; makefile: 2
file content (178 lines) | stat: -rw-r--r-- 7,249 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/****************************************************************************
 *  Copyright (c) 2011 Anthony Vital <anthony.vital@gmail.com>              *
 *                                                                          *
 *  This file is part of Wicd Client KDE.                                   *
 *                                                                          *
 *  Wicd Client KDE is free software: you can redistribute it and/or modify *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation, either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  Wicd Client KDE 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 General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with Wicd Client KDE.  If not, see <http://www.gnu.org/licenses/>.*
 ****************************************************************************/

#include "profilewidget.h"
#include "global.h"

#include <QGraphicsLinearLayout>

#include <KComboBox>
#include <KInputDialog>
#include <KLocalizedString>

#include <Plasma/DataEngineManager>
#include <Plasma/ServiceJob>
#include <Plasma/PushButton>

ProfileWidget::ProfileWidget(QGraphicsItem * parent, Qt::WindowFlags wFlags)
    : QGraphicsWidget(parent, wFlags)
{
    QGraphicsLinearLayout *vLayout = new QGraphicsLinearLayout(Qt::Vertical);

    m_defaultBox = new Plasma::CheckBox(this);
    m_defaultBox->setText(i18n("Use as default profile"));
    vLayout->addItem(m_defaultBox);

    QGraphicsLinearLayout *hLayout = new QGraphicsLinearLayout(Qt::Horizontal);
    m_comboBox = new Plasma::ComboBox(this);
    m_comboBox->nativeWidget()->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    hLayout->addItem(m_comboBox);
    Plasma::PushButton *addButton = new Plasma::PushButton(this);
    addButton->setToolTip(i18n("Add a profile..."));
    addButton->setPreferredWidth(addButton->size().height());
    addButton->setIcon(KIcon("list-add"));
    hLayout->addItem(addButton);
    Plasma::PushButton *removeButton = new Plasma::PushButton(this);
    removeButton->setToolTip(i18n("Remove the profile"));
    removeButton->setPreferredWidth(removeButton->size().height());
    removeButton->setIcon(KIcon("list-remove"));
    hLayout->addItem(removeButton);
    hLayout->addStretch();
    vLayout->addItem(hLayout);

    setLayout(vLayout);

    m_wicdService = engine()->serviceForSource("");
    m_wicdService->setParent(this);
    KConfigGroup op = m_wicdService->operationDescription("getWiredProfileList");
    Plasma::ServiceJob *job = m_wicdService->startOperationCall(op);
    //don't wait for the event loop, we need the result right now
    job->start();
    QStringList profileList = job->result().toStringList();
    m_comboBox->nativeWidget()->addItems(profileList);

    connect(m_defaultBox, SIGNAL(toggled(bool)),this, SLOT(toggleDefault(bool)));
    connect(m_comboBox, SIGNAL(textChanged(QString)),this, SLOT(profileChanged(QString)));
    connect(addButton, SIGNAL(clicked()),this, SLOT(addProfile()));
    connect(removeButton, SIGNAL(clicked()),this, SLOT(removeProfile()));

    int currentProfileIndex = profileList.indexOf(Wicd::currentprofile);
    //for the profile dialog
    if (currentProfileIndex < 0)
        currentProfileIndex = 0;
    m_comboBox->setCurrentIndex(currentProfileIndex);
}

Plasma::DataEngine* ProfileWidget::engine()
{
    Plasma::DataEngine *e = Plasma::DataEngineManager::self()->engine("wicd");
    if (e->isValid()) {
        return e;
    } else {
        return 0;
    }
}

void ProfileWidget::toggleDefault(bool toggle)
{
    KConfigGroup op = m_wicdService->operationDescription("setProfileDefaultProperty");
    op.writeEntry("profile", m_comboBox->text());
    op.writeEntry("default", toggle);
    m_wicdService->startOperationCall(op);
}

void ProfileWidget::profileChanged(QString profile)
{
    KConfigGroup op = m_wicdService->operationDescription("setCurrentProfile");
    op.writeEntry("profile", profile);
    Plasma::ServiceJob *job = m_wicdService->startOperationCall(op);
    //don't wait for the event loop, we need the result right now
    job->start();
    m_defaultBox->setChecked(job->result().toBool());
    emit profileSelected(profile);
    Wicd::currentprofile = profile;
}

void ProfileWidget::addProfile()
{
    bool ok;
    QString newprofile = KInputDialog::getText(i18n("Add a profile"),
                                               i18n("New profile name:"),
                                               QString(), &ok, 0);
    if ((!ok) || newprofile.isEmpty())
        return;

    if (m_comboBox->nativeWidget()->contains(newprofile))//the profile already exists
        return;

    KConfigGroup op = m_wicdService->operationDescription("createWiredNetworkProfile");
    op.writeEntry("profile", newprofile);
    m_wicdService->startOperationCall(op);
    m_comboBox->nativeWidget()->insertItem(0, newprofile);
    m_comboBox->setCurrentIndex(0);
}

void ProfileWidget::removeProfile()
{
    QString profile = m_comboBox->text();
    KConfigGroup op = m_wicdService->operationDescription("deleteWiredNetworkProfile");
    op.writeEntry("profile", profile);
    m_wicdService->startOperationCall(op);
    m_comboBox->nativeWidget()->removeItem(m_comboBox->currentIndex());
    m_comboBox->setCurrentIndex(0);
}

ProfileDialog::ProfileDialog(QGraphicsWidget *parent)
    : Plasma::Dialog(0)
{
    setAttribute(Qt::WA_DeleteOnClose);

    QGraphicsWidget *mainWidget = new QGraphicsWidget(parent);
    QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Vertical);
    mainWidget->setLayout(mainLayout);
    m_profileWidget = new ProfileWidget(mainWidget);
    mainLayout->addItem(m_profileWidget);
    Plasma::PushButton *okButton = new Plasma::PushButton(mainWidget);
    okButton->setIcon(KIcon("dialog-ok"));
    okButton->setText(i18n("Ok"));
    QGraphicsLinearLayout *bottomLayout = new QGraphicsLinearLayout(Qt::Horizontal);
    bottomLayout->addStretch();
    bottomLayout->addItem(okButton);
    mainLayout->addItem(bottomLayout);
    setGraphicsWidget(mainWidget);
    connect(okButton, SIGNAL(clicked()), this, SLOT(accepted()));
}

void ProfileDialog::closeEvent(QCloseEvent *event)
 {
    Plasma::Service *service = m_profileWidget->engine()->serviceForSource("");
    service->setParent(this);
    KConfigGroup op = service->operationDescription("setProfileNotNeeded");
    service->startOperationCall(op);
    Plasma::Dialog::closeEvent(event);
 }

void ProfileDialog::accepted()
{
    Plasma::Service *service = m_profileWidget->engine()->serviceForSource("");
    service->setParent(this);
    KConfigGroup op = service->operationDescription("connectWired");
    service->startOperationCall(op);
    close();
}