summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-console/consoleconfigwidget.cpp
blob: 27d9a3ff1b2108d563954e8d053e2ebeb164908d (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
103
104
105
106
107
108
109
#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlistview.h>
#include <qhbox.h>
#include <stdio.h>

#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>

#include "consoleconfigwidget.h"

ConsoleConfigWidget::ConsoleConfigWidget( const QString& name, QWidget* parent,
                                const char* na )
    : ProfileDialogConnectionWidget( name, parent, na ) {
    m_lay = new QVBoxLayout( this );
    QLabel *label = new QLabel(tr("Command to execute"), this);
    m_lay->addWidget(label);
    m_cmd = new QLineEdit(this);
    m_lay->addWidget(m_cmd);
    label = new QLabel(tr("Environment Variables"), this);
    m_lay->addWidget(label);
    m_env = new QListView(this);
    m_env->addColumn(tr("Name"));
    m_env->addColumn(tr("Value"));
    m_lay->addWidget(m_env);

    QHBox *hbox = new QHBox(this);
    label = new QLabel(tr("Name :"), hbox);
    m_name = new QLineEdit(hbox);
    m_lay->addWidget(hbox);

    hbox = new QHBox(this);
    label = new QLabel(tr("Value :"), hbox);
    m_value = new QLineEdit(hbox);
    m_lay->addWidget(hbox);

    hbox = new QHBox(this);
    hbox->setSpacing(10);
    m_remove = new QPushButton(tr("Remove"), hbox);
    connect(m_remove, SIGNAL(clicked()), this, SLOT(slotRemove()));
    m_add = new QPushButton(tr("Add"), hbox);
    connect(m_add, SIGNAL(clicked()), this, SLOT(slotAdd()));
    m_lay->addWidget(hbox);
}

void ConsoleConfigWidget::slotAdd() {
    if (!(m_name->text().isEmpty() || m_value->text().isEmpty())) {
        QListViewItem *item = new QListViewItem(m_env);
        item->setText(0, m_name->text());
        item->setText(1, m_value->text());
        m_env->insertItem(item);
    }
}

void ConsoleConfigWidget::slotRemove() {
    QListViewItem *item = m_env->currentItem();
    if (item) {
        m_env->takeItem(item);
    }
}

ConsoleConfigWidget::~ConsoleConfigWidget() {
}

void ConsoleConfigWidget::load( const Profile& prof ) {
    /*
     * default to the users default shell
     */
    struct passwd *ent = 0;
    char *shell = "/bin/sh";
    int uid = getuid();

    ent = getpwuid(uid);
        if (ent->pw_shell != "")  {
            shell = ent->pw_shell;
        }

    m_cmd->setText(prof.readEntry("Command", shell ));
    int envcount = prof.readNumEntry("EnvVars", 0);
    for (int i=0; i<envcount; i++) {
        QString name = prof.readEntry("Env_Name_" + QString::number(i), "");
        QString value = prof.readEntry("Env_Value_" + QString::number(i), "");
        if (!(name.isEmpty() || value.isEmpty())) {
            QListViewItem *item = new QListViewItem(m_env);
            item->setText(0, name);
            item->setText(1, value);
            m_env->insertItem(item);
        }
    }
}

void ConsoleConfigWidget::save( Profile& prof ) {
    prof.writeEntry( "Command", m_cmd->text());
    QListViewItem *item = m_env->firstChild();
    int counter = 0;
    while (item) {
        QString name = item->text(0);
        QString value = item->text(1);
        prof.writeEntry("Env_Name_" + QString::number(counter), name);
        prof.writeEntry("Env_Value_" + QString::number(counter), value);
        item = item->nextSibling();
        counter++;
    }
    prof.writeEntry("EnvVars", QString::number(counter));
}