summaryrefslogtreecommitdiffstats
path: root/src/knutrwvar.cpp
blob: 7cc665918e4025c74a155c617f821895b50f0423 (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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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 <tdeapplication.h>
#include <kapp.h>
#include <kcombobox.h>
#include <kmessagebox.h>
#include <klocale.h>


#include <tqframe.h>
#include <tqlabel.h>
#include <tqstring.h>
#include <tqlayout.h>


//#include <iostream>

KNutRWVar::KNutRWVar(TQString* userName, TQString* userPassword, const TQString uName, const TQString password, KNutNet* const initUpsNet, TQWidget* 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;
    TQFrame *page = plainPage();
    TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0, spacingHint() );
    TQLabel *label1 = new TQLabel (i18n("SET RW VARIABLE"),page,"label1");
    label1->setAlignment(TQt::AlignHCenter);
    topLayout->addWidget(label1);


    TQLabel *label2 = new TQLabel (i18n("Variable:"),page,"label2");
    m_rWVarBox = new KComboBox(page,"rwvarbox");

    TQLabel *label3 = new TQLabel (i18n("Value:"),page,"label2");
    m_valueVarBox = new KComboBox(page,"valuevarbox");
    m_valueVarLine = new KLineEdit(page,"valuevarLine");
    m_valueVarLine->hide();

    m_passLayout = new TQGridLayout (4,2,5,"passLayout");
    TQLabel *labelName = new TQLabel (i18n("User name:"),page,"labelName");
    TQLabel *labelPassword = new TQLabel (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(TQLineEdit::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,TQT_SIGNAL(activated(int)),this,TQT_SLOT(slotChangeVar(int)));
    }
  }

int KNutRWVar::findItem(const KComboBox *myBox, const TQString 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;

  TQString 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;

  TQString 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() {
  TQString 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"