summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/ppp/accounts.cpp
blob: 04905e89f494c455d248141593bac932e3792222 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 *           kPPP: A pppd front end for the KDE project
 *
 * $Id$
 *
 *            Copyright (C) 1997 Bernd Johannes Wuebben
 *                   wuebben@math.cornell.edu
 *
 * based on EzPPP:
 * Copyright (C) 1997  Jay Painter
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <qdir.h>
#include <stdlib.h>
#include <qlayout.h>
#include <qtabwidget.h>
#include <qtabdialog.h>
#include <qwhatsthis.h>
#include <qmessagebox.h>

#include <qapplication.h>
#include <qbuttongroup.h>
#include <qmessagebox.h>
#include <qvgroupbox.h>

#include "accounts.h"
#include "authwidget.h"
#include "pppdata.h"
#include "edit.h"

void parseargs(char* buf, char** args);

AccountWidget::AccountWidget( PPPData *pd, QWidget *parent, const char *name )
    : QWidget( parent, name )//, _pppdata(pd)
{
    _pppdata = pd;
  QVBoxLayout *l1 = new QVBoxLayout(this, 10, 10);
  accountlist_l = new QListBox(this);

  connect(accountlist_l, SIGNAL(highlighted(int)),
	  this, SLOT(slotListBoxSelect(int)));
  connect(accountlist_l, SIGNAL(selected(int)),
	  this, SLOT(editaccount()));
  l1->addWidget(accountlist_l, 10);

  edit_b = new QPushButton(tr("&Edit..."), this);
  connect(edit_b, SIGNAL(clicked()), SLOT(editaccount()));
  QWhatsThis::add(edit_b, tr("Allows you to modify the selected account"));
  l1->addWidget(edit_b);

  new_b = new QPushButton(tr("&New..."), this);
  connect(new_b, SIGNAL(clicked()), SLOT(newaccount()));
  l1->addWidget(new_b);
  QWhatsThis::add(new_b, tr("Create a new dialup connection\n"
  			      "to the Internet"));

  copy_b = new QPushButton(tr("Co&py"), this);
  connect(copy_b, SIGNAL(clicked()), SLOT(copyaccount()));
  l1->addWidget(copy_b);
  QWhatsThis::add(copy_b,
		  tr("Makes a copy of the selected account. All\n"
		       "settings of the selected account are copied\n"
		       "to a new account, that you can modify to fit your\n"
		       "needs"));

  delete_b = new QPushButton(tr("De&lete"), this);
  connect(delete_b, SIGNAL(clicked()), SLOT(deleteaccount()));
  l1->addWidget(delete_b);
  QWhatsThis::add(delete_b,
		  tr("<p>Deletes the selected account\n\n"
		       "<font color=\"red\"><b>Use with care!</b></font>"));

  QHBoxLayout *l12 = new QHBoxLayout;
  l1->addStretch(1);
  l1->addLayout(l12);

  int currAccId = _pppdata->currentAccountID();
  qDebug("currentAccountID %i", currAccId);

  //load up account list from gppdata to the list box
  if(_pppdata->count() > 0) {
    for(int i=0; i <= _pppdata->count()-1; i++) {
      _pppdata->setAccountbyIndex(i);
      accountlist_l->insertItem(_pppdata->accname());
    }
  }
  _pppdata->setAccountbyIndex( currAccId );

  qDebug("setting listview index to %i",_pppdata->currentAccountID() );
  accountlist_l->setCurrentItem( _pppdata->currentAccountID() );
  slotListBoxSelect( _pppdata->currentAccountID() );

  l1->activate();
}



void AccountWidget::slotListBoxSelect(int idx) {
  delete_b->setEnabled((bool)(idx != -1));
  edit_b->setEnabled((bool)(idx != -1));
  copy_b->setEnabled((bool)(idx != -1));
  if(idx!=-1) {
      qDebug("setting account to %i", idx);
    QString account = _pppdata->accname();
    _pppdata->setAccountbyIndex(accountlist_l->currentItem());
 }
}

void AccountWidget::editaccount() {
  _pppdata->setAccount(accountlist_l->text(accountlist_l->currentItem()));

  int result = doTab();

  if(result == QDialog::Accepted) {
    accountlist_l->changeItem(_pppdata->accname(),accountlist_l->currentItem());
//    emit resetaccounts();
    _pppdata->save();
  }
}


void AccountWidget::newaccount() {

    if(accountlist_l->count() == MAX_ACCOUNTS) {
        QMessageBox::information(this, "sorry",
                                 tr("Maximum number of accounts reached."));
        return;
    }

    int result;
    if (_pppdata->newaccount() == -1){
        qDebug("_pppdata->newaccount() == -1");
        return;
    }
    result = doTab();

    if(result == QDialog::Accepted) {
        accountlist_l->insertItem(_pppdata->accname());
        accountlist_l->setSelected(accountlist_l->findItem(_pppdata->accname()),true);

        _pppdata->save();
    } else
        _pppdata->deleteAccount();
}


void AccountWidget::copyaccount() {
  if(accountlist_l->count() == MAX_ACCOUNTS) {
    QMessageBox::information(this, "sorry", tr("Maximum number of accounts reached."));
    return;
  }

  if(accountlist_l->currentItem()<0) {
    QMessageBox::information(this, "sorry", tr("No account selected."));
    return;
  }

  _pppdata->copyaccount(accountlist_l->currentItem());

  accountlist_l->insertItem(_pppdata->accname());
//  emit resetaccounts();
  _pppdata->save();
}


void AccountWidget::deleteaccount() {

  QString s = tr("Are you sure you want to delete\nthe account \"%1\"?")
    .arg(accountlist_l->text(accountlist_l->currentItem()));

  if(QMessageBox::warning(this, s, tr("Confirm")) != QMessageBox::Yes)
    return;

  if(_pppdata->deleteAccount(accountlist_l->text(accountlist_l->currentItem())))
    accountlist_l->removeItem(accountlist_l->currentItem());

  emit resetaccounts();
  _pppdata->save();

  slotListBoxSelect(accountlist_l->currentItem());

}


int AccountWidget::doTab(){
    QDialog *dlg = new QDialog( 0, "newAccount", true );
    QVBoxLayout *layout = new QVBoxLayout( dlg );
    layout->setSpacing( 0 );
    layout->setMargin( 1 );

    tabWindow = new QTabWidget( dlg, "tabWindow" );
    layout->addWidget( tabWindow );

    bool isnewaccount;

    if(_pppdata->accname().isEmpty()) {
        dlg->setCaption(tr("New Account"));
        isnewaccount = true;
    } else {
        QString tit = tr("Edit Account: ");
        tit += _pppdata->accname();
        dlg->setCaption(tit);
        isnewaccount = false;
    }

//   // DIAL WIDGET
    dial_w = new DialWidget( _pppdata, tabWindow, isnewaccount, "Dial Setup");
    tabWindow->addTab( dial_w, tr("Dial") );

//   // AUTH WIDGET
   auth_w = new AuthWidget( _pppdata, tabWindow, isnewaccount, tr("Edit Login Script"));
   tabWindow->addTab( auth_w, tr("Authentication") );

//   // IP WIDGET
    ip_w = new IPWidget( _pppdata, tabWindow, isnewaccount, tr("IP Setup"));
    tabWindow->addTab( ip_w, tr("IP") );

//   // GATEWAY WIDGET
    gateway_w = new GatewayWidget( _pppdata, tabWindow, isnewaccount, tr("Gateway Setup"));
    tabWindow->addTab( gateway_w, tr("Gateway") );

//   // DNS WIDGET
    dns_w = new DNSWidget( _pppdata, tabWindow, isnewaccount, tr("DNS Servers") );
    tabWindow->addTab( dns_w, tr("DNS") );

//   // EXECUTE WIDGET
   ExecWidget *exec_w = new ExecWidget( _pppdata, tabWindow, isnewaccount, tr("Execute Programs"));
   tabWindow->addTab( exec_w, tr("Execute") );

    int result = 0;
    bool ok = false;

    while (!ok){
        dlg->showMaximized();
        result = dlg->exec();
        ok = true;

        if(result == QDialog::Accepted) {
            if (!auth_w->check()){
                ok = false;
            } else if(!dial_w->save()) {
                QMessageBox::critical(this, "error", tr( "You must enter a unique account name"));
                ok = false;
            }else{
                ip_w->save();
                dns_w->save();
                gateway_w->save();
                auth_w->save();
                exec_w->save();
            }
        }
    }

    delete dlg;

    return result;
}


QString AccountWidget::prettyPrintVolume(unsigned int n) {
  int idx = 0;
  const QString quant[] = {tr("Byte"), tr("KB"),
		   tr("MB"), tr("GB"), QString::null};

  float n1 = n;
  while(n >= 1024 && quant[idx] != QString::null) {
    idx++;
    n /= 1024;
  }

  int i = idx;
  while(i--)
    n1 = n1 / 1024.0;

  QString s = QString::number( n1, 'f', idx==0 ? 0 : 1 );
  s += " " + quant[idx];
  return s;
}


/////////////////////////////////////////////////////////////////////////////
//
// Queries the user what to reset: costs, volume or both
//
/////////////////////////////////////////////////////////////////////////////
// QueryReset::QueryReset(QWidget *parent) : QDialog(parent, 0, true) {
// //  KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
//   setCaption(tr("Reset Accounting"));

//   QVBoxLayout *tl = new QVBoxLayout(this, 10, 10);
//   QVGroupBox *f = new QVGroupBox(tr("What to Reset"), this);

//   QVBoxLayout *l1 = new QVBoxLayout(this, 10, 10);
// //   costs = new QCheckBox(tr("Reset the accumulated phone costs"), f);
// //   costs->setChecked(true);
// //   l1->addWidget(costs);
// //   QWhatsThis::add(costs, tr("Check this to set the phone costs\n"
// // 			      "to zero. Typically you'll want to\n"
// // 			      "do this once a month."));

// //   volume = new QCheckBox(tr("Reset volume accounting"), f);
// //   volume->setChecked(true);
// //   l1->addWidget(volume);
// //   QWhatsThis::add(volume, tr("Check this to set the volume accounting\n"
// // 			       "to zero. Typically you'll want to do this\n"
// // 			       "once a month."));

//   l1->activate();

//   // this activates the f-layout and sets minimumSize()
//   f->show();

//   tl->addWidget(f);

//   QButtonGroup *bbox = new QButtonGroup(this);
// //  bbox->addStretch(1);
//   QPushButton *ok = new QPushButton( bbox, tr("OK") );
//   bbox->insert(ok);
//   ok->setDefault(true);
//   QPushButton *cancel = new QPushButton( bbox, tr("Cancel") );
//   bbox->insert(cancel);

//   connect(ok, SIGNAL(clicked()),
// 	  this, SLOT(accepted()));
//   connect(cancel, SIGNAL(clicked()),
// 	  this, SLOT(reject()));

//   bbox->layout();
//   tl->addWidget(bbox);

// }


// void QueryReset::accepted() {
//   int result = costs->isChecked() ? COSTS : 0;
//   result += volume->isChecked() ? VOLUME : 0;

//   done(result);
// }