00001 #include <qlabel.h>
00002 #include <qlayout.h>
00003 #include <qlineedit.h>
00004 #include <qpushbutton.h>
00005 #include <qlistview.h>
00006 #include <qhbox.h>
00007 #include <stdio.h>
00008
00009 #include <pwd.h>
00010 #include <sys/types.h>
00011 #include <unistd.h>
00012
00013 #include "consoleconfigwidget.h"
00014
00015 ConsoleConfigWidget::ConsoleConfigWidget( const QString& name, QWidget* parent,
00016 const char* na )
00017 : ProfileDialogConnectionWidget( name, parent, na ) {
00018 m_lay = new QVBoxLayout( this );
00019 QLabel *label = new QLabel(tr("Command to execute"), this);
00020 m_lay->addWidget(label);
00021 m_cmd = new QLineEdit(this);
00022 m_lay->addWidget(m_cmd);
00023 label = new QLabel(tr("Environment Variables"), this);
00024 m_lay->addWidget(label);
00025 m_env = new QListView(this);
00026 m_env->addColumn(tr("Name"));
00027 m_env->addColumn(tr("Value"));
00028 m_lay->addWidget(m_env);
00029
00030 QHBox *hbox = new QHBox(this);
00031 label = new QLabel(tr("Name :"), hbox);
00032 m_name = new QLineEdit(hbox);
00033 m_lay->addWidget(hbox);
00034
00035 hbox = new QHBox(this);
00036 label = new QLabel(tr("Value :"), hbox);
00037 m_value = new QLineEdit(hbox);
00038 m_lay->addWidget(hbox);
00039
00040 hbox = new QHBox(this);
00041 hbox->setSpacing(10);
00042 m_remove = new QPushButton(tr("Remove"), hbox);
00043 connect(m_remove, SIGNAL(clicked()), this, SLOT(slotRemove()));
00044 m_add = new QPushButton(tr("Add"), hbox);
00045 connect(m_add, SIGNAL(clicked()), this, SLOT(slotAdd()));
00046 m_lay->addWidget(hbox);
00047 }
00048
00049 void ConsoleConfigWidget::slotAdd() {
00050 if (!(m_name->text().isEmpty() || m_value->text().isEmpty())) {
00051 QListViewItem *item = new QListViewItem(m_env);
00052 item->setText(0, m_name->text());
00053 item->setText(1, m_value->text());
00054 m_env->insertItem(item);
00055 }
00056 }
00057
00058 void ConsoleConfigWidget::slotRemove() {
00059 QListViewItem *item = m_env->currentItem();
00060 if (item) {
00061 m_env->takeItem(item);
00062 }
00063 }
00064
00065 ConsoleConfigWidget::~ConsoleConfigWidget() {
00066 }
00067
00068 void ConsoleConfigWidget::load( const Profile& prof ) {
00069
00070
00071
00072 struct passwd *ent = 0;
00073 char *shell = "/bin/sh";
00074 int uid = getuid();
00075
00076 ent = getpwuid(uid);
00077 if (ent->pw_shell != "") {
00078 shell = ent->pw_shell;
00079 }
00080
00081 m_cmd->setText(prof.readEntry("Command", shell ));
00082 int envcount = prof.readNumEntry("EnvVars", 0);
00083 for (int i=0; i<envcount; i++) {
00084 QString name = prof.readEntry("Env_Name_" + QString::number(i), "");
00085 QString value = prof.readEntry("Env_Value_" + QString::number(i), "");
00086 if (!(name.isEmpty() || value.isEmpty())) {
00087 QListViewItem *item = new QListViewItem(m_env);
00088 item->setText(0, name);
00089 item->setText(1, value);
00090 m_env->insertItem(item);
00091 }
00092 }
00093 }
00094
00095 void ConsoleConfigWidget::save( Profile& prof ) {
00096 prof.writeEntry( "Command", m_cmd->text());
00097 QListViewItem *item = m_env->firstChild();
00098 int counter = 0;
00099 while (item) {
00100 QString name = item->text(0);
00101 QString value = item->text(1);
00102 prof.writeEntry("Env_Name_" + QString::number(counter), name);
00103 prof.writeEntry("Env_Value_" + QString::number(counter), value);
00104 item = item->nextSibling();
00105 counter++;
00106 }
00107 prof.writeEntry("EnvVars", QString::number(counter));
00108 }
00109