summaryrefslogtreecommitdiffstats
path: root/src/knutrwvar.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/knutrwvar.cpp')
-rwxr-xr-xsrc/knutrwvar.cpp217
1 files changed, 217 insertions, 0 deletions
diff --git a/src/knutrwvar.cpp b/src/knutrwvar.cpp
new file mode 100755
index 0000000..20e45bc
--- /dev/null
+++ b/src/knutrwvar.cpp
@@ -0,0 +1,217 @@
+/***************************************************************************
+ knutrwvar.cpp - description
+ -------------------
+ begin : So ríj 26 2002
+ copyright : (C) 2002 by Daniel Prynych
+ email : Daniel.Prynych@alo.cz
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * This program 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 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+
+
+#include "knutrwvar.h"
+#include "knutvardata.h"
+#include "knutprintupsvar.h"
+
+//Od verze 3 je kapp jen odkaz na kapplication
+//#include <kapplication.h>
+#include <kapp.h>
+#include <kcombobox.h>
+#include <kmessagebox.h>
+#include <klocale.h>
+
+
+#include <qframe.h>
+#include <qlabel.h>
+#include <qstring.h>
+#include <qlayout.h>
+
+
+//#include <iostream>
+
+KNutRWVar::KNutRWVar(QString* userName, QString* userPassword, const QString uName, const QString password, KNutNet* const initUpsNet, QWidget* parent, const char* name, const bool modal)
+: KDialogBase(Plain, i18n("RW variables"),Ok|Cancel|Default,Ok, parent, name, modal, true), m_upsNet(initUpsNet){
+ upsVarDef upsVar;
+
+// int error;
+
+ if (m_upsNet->getState() != KNutNet::Connected) {
+ KNutVarData::showError (KNutNet::NotConnection);
+ m_upsConnectOk = false;
+ }
+ else {
+ m_oldUserName = userName;
+ m_oldUserPassword = userPassword;
+
+ m_upsConnectOk = true;
+ QFrame *page = plainPage();
+ QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
+ QLabel *label1 = new QLabel (i18n("SET RW VARIABLE"),page,"label1");
+ label1->setAlignment(Qt::AlignHCenter);
+ topLayout->addWidget(label1);
+
+
+ QLabel *label2 = new QLabel (i18n("Variable:"),page,"label2");
+ m_rWVarBox = new KComboBox(page,"rwvarbox");
+
+ QLabel *label3 = new QLabel (i18n("Value:"),page,"label2");
+ m_valueVarBox = new KComboBox(page,"valuevarbox");
+ m_valueVarLine = new KLineEdit(page,"valuevarLine");
+ m_valueVarLine->hide();
+
+ m_passLayout = new QGridLayout (4,2,5,"passLayout");
+ QLabel *labelName = new QLabel (i18n("User name:"),page,"labelName");
+ QLabel *labelPassword = new QLabel (i18n("Password:"),page,"labelPassword");
+ m_lineEditName = new KLineEdit( page, "LineEditName" );
+ m_lineEditPassword = new KLineEdit( page, "LineEditName" );
+
+
+ if (((*m_oldUserName) == "") && ((*m_oldUserPassword) == "")) {
+ m_lineEditName->setText(uName);
+ m_lineEditPassword->setText(password);
+ }
+ else {
+ m_lineEditName->setText(*m_oldUserName);
+ m_lineEditPassword->setText(*m_oldUserPassword);
+ }
+
+ if (!((*m_oldUserName) == "") || !((*m_oldUserPassword) == "")) {
+ m_lineEditName->setDisabled(true);
+ m_lineEditPassword->setDisabled(true);
+ }
+
+ m_lineEditPassword->setEchoMode(QLineEdit::Password);
+
+ topLayout->addLayout(m_passLayout);
+
+ m_passLayout->addWidget(label2,0,0);
+ m_passLayout->addWidget(m_rWVarBox,0,1);
+ m_passLayout->addWidget(label3,1,0);
+ m_passLayout->addWidget(labelName,2,0);
+ m_passLayout->addWidget(labelPassword,3,0);
+ m_passLayout->addWidget(m_lineEditName,2,1);
+ m_passLayout->addWidget(m_lineEditPassword,3,1);
+
+ topLayout->addStretch(10);
+ //loads ComboBox
+ int n;
+ if (( n = (m_upsNet->readNumberVars( KNutNet::RWVars )+1)) > 1 ) { // zvetsime si pocet o 1
+ for (int i =1; i < n ; i++) {
+ if (!m_upsNet->readVars(i, upsVar,KNutNet::RWVars))
+ m_rWVarBox->insertItem(upsVar.upsVarName);
+ }
+ slotChangeVar(0);
+ }
+ else {
+ m_passLayout->addWidget(m_valueVarLine,1,1);
+ m_valueVarLine->setMaxLength(upsVar.upsVarMax);
+ m_valueVarBox->hide();
+ m_valueVarLine->show();
+ m_upsValueType=true;
+ }
+ connect (m_rWVarBox,SIGNAL(activated(int)),this,SLOT(slotChangeVar(int)));
+ }
+ }
+
+int KNutRWVar::findItem(const KComboBox *myBox, const QString text) {
+ int n;
+
+ if ((n=myBox->count())) {
+ for (int i =0; i < n; i++)
+ if (text == myBox->text(i)) return i;
+ }
+ return myBox->currentItem();
+ }
+
+
+void KNutRWVar::slotDefault () {
+ upsVarDef upsVar;
+ int error;
+
+ QString varName = m_rWVarBox->currentText();
+ if (!( error = m_upsNet->readVars(varName,upsVar))) {
+ if (upsVar.upsValueType) m_valueVarLine->setText(upsVar.upsValue);
+ else m_valueVarBox->setCurrentItem(upsVar.upsValue);
+ }
+ else KNutVarData::showError (error);
+ }
+
+
+void KNutRWVar::slotChangeVar(int item) {
+ upsVarDef upsVar;
+
+ QString varName = m_rWVarBox->text(item);
+ int error = m_upsNet->readVars(varName,upsVar);
+ m_upsValueType=upsVar.upsValueType;
+ if (error || (upsVar.upsValueType)) {
+ // char
+ m_valueVarBox->hide();
+ m_valueVarLine->show();
+ m_passLayout->addWidget(m_valueVarLine,1,1);
+ m_valueVarLine->setText(upsVar.upsValue);
+ }
+ else {
+ //enum
+ m_valueVarLine->hide();
+ m_valueVarBox->show();
+ m_passLayout->addWidget(m_valueVarBox,1,1);
+ m_valueVarBox->clear();
+ if (upsVar.upsVarMax) {
+ for (int i = 0 ; i < upsVar.upsVarMax; i++)
+ m_valueVarBox->insertItem(m_upsNet->readEnumValueVar(upsVar.upsVarName,i+1));
+ }
+ m_valueVarBox->setCurrentItem(upsVar.upsValue);
+ }
+ }
+
+
+
+KNutRWVar::~KNutRWVar(){
+}
+
+bool KNutRWVar::upsOk (void) { return m_upsConnectOk; }
+
+
+void KNutRWVar::slotOk() {
+ QString value;
+ int error =0;
+ if (m_upsValueType) value=m_valueVarLine->text();
+ else value=m_valueVarBox->currentText();
+
+ if (((*m_oldUserName) == "") && ((*m_oldUserPassword) == "")) {
+ //the first connection sets name and password
+ //prvni propojeni nastavime jmeno a heslo
+ if (!(error = m_upsNet->setVariable(m_rWVarBox->currentText(), value, m_lineEditName->text(),m_lineEditPassword->text(),false))) {
+ // vzhledem k asynchronimu spracovani asi zbytecne
+// myUpsNet->getUpsValues(true);
+ emit signalChangeRWVars(m_rWVarBox->currentText());//emits command for loading of variable and repaint of panel
+
+ (*m_oldUserName) = m_lineEditName->text();
+ (*m_oldUserPassword) = m_lineEditPassword->text();
+ accept();
+ }
+ }
+ else {
+ if (((*m_oldUserName) == m_lineEditName->text()) && ((*m_oldUserPassword) == m_lineEditPassword->text())) {
+ if (!(error = m_upsNet->setVariable(m_rWVarBox->currentText(), value, m_lineEditName->text(),m_lineEditPassword->text(),true))) {
+ // next line isn't needed, maybe
+ // vzhledem k asynchronimu spracovani asi zbytecne
+// myUpsNet->getUpsValues(true);
+ emit signalChangeRWVars(m_rWVarBox->currentText()); //emits command for loading of variable and repaint of panel
+ accept();
+ }
+ }
+ }
+ //Nahlasime chybu
+ // sends information about error
+ if (error) KNutVarData::showError (error);
+ }
+
+#include "knutrwvar.moc"