summaryrefslogtreecommitdiffstats
path: root/src/knutinstcomms.cpp
blob: d6cdc073589eb4253b989638c307aa03a2991cf5 (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
/***************************************************************************
                          knutinstcomms.cpp  -  description
                             -------------------
    begin                : ct ríj 24 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 "knutinstcomms.h"
#include "knutprintupsvar.h"
#include "knutvardata.h"

#include <kcombobox.h>
#include <tdemessagebox.h>


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


KNutInstComms::KNutInstComms(TQString* userName, TQString* userPassword, const TQString uName, const TQString password, KNutNet* const upsNet, TQWidget* parent,  const char* name, const bool modal)
: KDialogBase(Plain, i18n("Instant commands"),Ok|Cancel,Ok, parent, name, modal, true), m_upsNet(upsNet),m_oldUserName(userName), m_oldUserPassword(userPassword)  {
  upsICommDef upsIComm;


   if (m_upsNet->getState() != KNutNet::Connected) { // no connection - no instant commnads
     KNutVarData::showError (KNutNet::NotConnection);
     m_upsConnectOk=false;
     }
  else {
    m_upsConnectOk=true;
    TQFrame *page = plainPage();
    TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0, spacingHint() );
    TQLabel *label1 = new TQLabel (i18n("RUN INSTANT COMMAND"),page,"label1");
    label1->setAlignment(TQt::AlignHCenter);
    topLayout->addWidget(label1);

    TQLabel *label2 = new TQLabel (i18n("Command:"),page,"label2");
    m_instCommBox = new KComboBox(page,"instcommbox");

    TQGridLayout *passLayout = new TQGridLayout (3,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);
      m_lineEditName->setDisabled(true);
      m_lineEditPassword->setDisabled(true);
      }

 //   if (!((*oldUserName) == "") || !((*oldUserPassword) == "")) {
 //   if (!((*oldUserName) == "") && ((*oldUserPassword) == "")) {
 //     lineEditName->setDisabled(true);
 //     lineEditPassword->setDisabled(true);
 //     }

    m_lineEditPassword->setEchoMode(TQLineEdit::Password);

    topLayout->addLayout(passLayout);

    passLayout->addWidget(label2,0,0);
    passLayout->addWidget(m_instCommBox,0,1);

    passLayout->addWidget(labelName,1,0);
    passLayout->addWidget(labelPassword,2,0);
    passLayout->addWidget(m_lineEditName,1,1);
    passLayout->addWidget(m_lineEditPassword,2,1);

    topLayout->addStretch(10);

   //loads ComboBox
    int n = m_upsNet->readNumberComms()+1;
    for (int i =1; i < n ; i++) { // for se testuje na zacatku pokud je n < 1 neprovede se ani jednou
      if (!m_upsNet->readIComm(i, upsIComm))
        m_instCommBox->insertItem(upsIComm.upsCommName);
      }
    }
  }

KNutInstComms::~KNutInstComms(){
}


void KNutInstComms::slotOk() {
  int error =0;


  if (((*m_oldUserName) == "") && ((*m_oldUserPassword) == "")) {
    // prvni propojeni nastavime jmeno a heslo
    //for the first connection we have to set name and password
    if (!( error = m_upsNet->instantCommand(m_instCommBox->currentText(),m_lineEditName->text(),m_lineEditPassword->text(),false))) {
      (*m_oldUserName) = m_lineEditName->text();
      (*m_oldUserPassword) = m_lineEditPassword->text();
      accept();
      }
    }
  else {
    // for other connection name and password are not setted
    if (((*m_oldUserName) == m_lineEditName->text()) && ((*m_oldUserPassword) == m_lineEditPassword->text())) {
      // jen posleme prikaz // sends command only
      if (!( error = m_upsNet->instantCommand(m_instCommBox->currentText(),m_lineEditName->text(),m_lineEditPassword->text(),true))) accept();
      }
// az v pristi verzi
//    else
//      {
//      // odpojime a pripojime
//      // nastavime jmeno a heslo
//      if (!( error = myUpsNet->instantCommand(instCommBox->currentText(),lineEditName->text(),lineEditPassword->text(),false))) {
//        (*oldUserName) = lineEditName->text();
//        (*oldUserPassword) = lineEditPassword->text();
//        accept();
//        }
//      }
    }
  if (error) KNutVarData::showError (error);
  }

//bool KNutInstComms::upsOk (void) { return m_upsConnectOk; }