summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/lancard/lancardedit.cpp
blob: ffe9bf68f618262f035f4e9f40995bdd00f6e9c6 (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
#include <GUIUtils.h>
#include <resources.h>
#include <qarray.h>
#include <qlistview.h>
#include <qcheckbox.h>
#include <qheader.h>
#include <qregexp.h>

#include "lancardedit.h"
#include "lancard_NN.h"
#include "lancard_NNI.h"

LanCardEdit::LanCardEdit( QWidget * Parent ) : LanCardGUI( Parent ){
    LanCards_LV->header()->hide();

}

QString LanCardEdit::acceptable( void ) {
    return QString();
}

bool LanCardEdit::commit( LanCardData & Data ) {
    bool SM = 0;
    CBM( Data.AnyLanCard, AnyCard_CB, SM );

    if( ! Data.AnyLanCard ) {
      // take copy for orig list
      QStringList NewList( Data.HWAddresses );

      // update HWAddresses to new state
      // remove item also from NewList
      int idx;
      QCheckListItem * CLI = (QCheckListItem *)LanCards_LV->firstChild();
      while( CLI ) {
        idx = Data.HWAddresses.findIndex(CLI->text(0));
        if( CLI->isOn() ) {
          if( idx < 0 ) {
            // should be in list
            Data.HWAddresses.append( CLI->text(0) );
            SM = 1;
          }
        } else {
          // should not be in list
          if( idx >= 0 ) {
            NewList.remove( CLI->text(0) );
            Data.HWAddresses.remove( CLI->text(0) );
            SM = 1;
          }
        }
        CLI = (QCheckListItem *)CLI->nextSibling();
      }

      // if newlist still contains items. it were items
      // that were checked but no longer are present in the system
      SM |= ( NewList.count() > 0 ) ;
    }
    return SM;
}

void LanCardEdit::showData( ALanCard * LC ) {
    NNI = LC;
    LanCardData & Data = *((LanCardData *)LC->data());

    AnyCard_CB->setChecked( Data.AnyLanCard );

    // load all cards
    populateList();

    // set checks
    QCheckListItem * CLI = (QCheckListItem *)LanCards_LV->firstChild();
    while( CLI ) {
      CLI->setOn( Data.HWAddresses.findIndex(CLI->text(0)) >= 0 );
      CLI = (QCheckListItem *)CLI->nextSibling();
    }
}

// load all known cards in list
void LanCardEdit::populateList( void ) {
    LanCardNetNode *NN = (LanCardNetNode *)NNI->nodeClass();
    QCheckListItem * CLI;
    bool Found;

    LanCards_LV->clear();

    for( QStringList::Iterator it = NN->addressesOfNIC().begin();
         it != NN->addressesOfNIC().end(); 
         ++it ) {
      CLI = new QCheckListItem( LanCards_LV, (*it), QCheckListItem::CheckBox );

      // check interfaces and see if this card is present
      Found = 0;
      for( QDictIterator<InterfaceInfo> NIt(NSResources->system().interfaces());
           NIt.current();
           ++NIt ) {
        if( NIt.current()->MACAddress == (*it) ) {
          Found = 1;
          break;
        }
      }

      CLI->setPixmap( 0, NSResources->getPixmap( 
        (Found) ? "add" : "remove" ) );
    }
}

// rescan system for new cards
void LanCardEdit::SLOT_ScanCards( void ) {
    LanCardNetNode *NN = (LanCardNetNode *)NNI->nodeClass();

    // add any NIC that is new and matches our interfacename
    System & S = NSResources->system();
    QRegExp R( "eth[0-9]" );
    // populate with all lancards in system
    for( QDictIterator<InterfaceInfo> It(S.interfaces());
         It.current();
         ++It ) {
      fprintf( stderr, "TEST %s %s\n", 
          It.current()->Name.latin1(),
          It.current()->MACAddress.latin1() );
      if( R.match( It.current()->Name ) >= 0 &&
          ( It.current()->CardType == ARPHRD_ETHER
#ifdef ARPHRD_IEEE1394
            || It.current()->CardType == ARPHRD_IEEE1394
#endif
          )
        ) {
        // old item ?
        QCheckListItem * CLI = 
                          (QCheckListItem *)LanCards_LV->firstChild();
        while( CLI ) {
          if( CLI->text(0) == It.current()->MACAddress ) {
            break;
          }
          CLI = (QCheckListItem *)CLI->nextSibling();
        }

        if( ! CLI ) {
          // new item
          CLI = new QCheckListItem( LanCards_LV, 
                It.current()->MACAddress,
                QCheckListItem::CheckBox );
        }

        // mark present
        CLI->setPixmap( 0, NSResources->getPixmap( 
                    "add" ) );

        if( NN->addressesOfNIC().findIndex( It.current()->MACAddress) < 0 ) {
          // new
          NN->addressesOfNIC().append( It.current()->MACAddress );
        }
      }
    }

}

// remove all cards that are not present -> flagged with 'remove'
// and unchecked
void LanCardEdit::SLOT_RemoveUnknown( void ) {
    QArray<QCheckListItem *> AllItems;

    LanCardNetNode *NN = (LanCardNetNode *)NNI->nodeClass();

    QCheckListItem * CLI = (QCheckListItem *)LanCards_LV->firstChild();
    while( CLI ) {
      AllItems.resize( AllItems.size()+1 );
      AllItems[ AllItems.size()-1 ] = CLI;
      CLI = (QCheckListItem *)CLI->nextSibling();
    }

    // force update of system
    System & S = NSResources->system();
    S.probeInterfaces();

    // add any NIC that is new and matches our interfacename
    QRegExp R( "eth[0-9]" );


    for( QDictIterator<InterfaceInfo> It(S.interfaces());
         It.current();
         ++It ) {
      if( R.match( It.current()->Name ) >= 0 &&
          ( It.current()->CardType == ARPHRD_ETHER
#ifdef ARPHRD_IEEE1394
            || It.current()->CardType == ARPHRD_IEEE1394
#endif
          )
        ) {

        for ( unsigned i = 0; i< AllItems.size(); i++ ) {
          if( AllItems[i] &&
              AllItems[i]->text(0) == It.current()->MACAddress ) {
            AllItems[i] = 0;
            break;
          }
        }
      }
    }

    // AllItems now contains all cards NOT present
    // remove all items non null and not ON
    for ( unsigned i = 0; i< AllItems.size(); i++ ) {
      if( AllItems[i] && ! AllItems[i]->isOn() ) {
        NN->addressesOfNIC().remove( AllItems[i]->text(0) );
        delete AllItems[i];
      }
    }
}