1/*
2 * kPPP: A pppd front end for the KDE project
3 *
4 *
5 *
6 *
7 * Copyright (C) 2004 Simone Gotti
8 * <simone.gotti@email.it>
9 *
10 * based on EzPPP:
11 * Copyright (C) 1997 Jay Painter
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
22 *
23 * You should have received a copy of the GNU Library General Public
24 * License along with this program; if not, write to the Free
25 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27
28#include <qdir.h>
29//Added by qt3to4:
30#include <QVBoxLayout>
31#include <QHBoxLayout>
32#include <stdlib.h>
33#include <qlayout.h>
34#include <q3tabdialog.h>
35
36#include <qmessagebox.h>
37#include <QApplication>
38#include <kmessagebox.h>
39#include <klocale.h>
40#include <kglobal.h>
41#include <kwindowsystem.h>
42#include <kpagedialog.h>
43#include <kiconloader.h>
44
45#include "general.h"
46#include "pppdata.h"
47#include "modems.h"
48#include "accounting.h"
49#include "providerdb.h"
50#include "edit.h"
51
52void parseargs(char* buf, char** args);
53
54ModemsWidget::ModemsWidget( QWidget *parent, const char *name )
55 : QWidget( parent )
56{
57 setObjectName(name);
58
59 int min = 0;
60 QVBoxLayout *l1 = new QVBoxLayout(parent);
61 l1->setSpacing(10);
62 l1->setMargin(10);
63
64 // add a hbox
65 QHBoxLayout *l11 = new QHBoxLayout;
66 l1->addLayout(l11);
67
68 modemlist_l = new Q3ListBox(parent);
69 modemlist_l->setMinimumSize(160, 128);
70 connect(modemlist_l, SIGNAL(highlighted(int)),
71 this, SLOT(slotListBoxSelect(int)));
72 connect(modemlist_l, SIGNAL(selected(int)),
73 this, SLOT(editmodem()));
74 l11->addWidget(modemlist_l, 10);
75
76 QVBoxLayout *l111 = new QVBoxLayout;
77 l11->addLayout(l111, 1);
78 edit_b = new QPushButton(i18n("&Edit..."), parent);
79 connect(edit_b, SIGNAL(clicked()), SLOT(editmodem()));
80 edit_b->setWhatsThis( i18n("Allows you to modify the selected account"));
81
82 min = edit_b->sizeHint().width();
83 min = qMax(70,min);
84 edit_b->setMinimumWidth(min);
85
86 l111->addWidget(edit_b);
87
88 new_b = new QPushButton(i18n("&New..."), parent);
89 connect(new_b, SIGNAL(clicked()), SLOT(newmodem()));
90 l111->addWidget(new_b);
91 new_b->setWhatsThis( i18n("Create a new dialup connection\n"
92 "to the Internet"));
93
94 copy_b = new QPushButton(i18n("Co&py"), parent);
95 connect(copy_b, SIGNAL(clicked()), SLOT(copymodem()));
96 l111->addWidget(copy_b);
97 copy_b->setWhatsThis(
98 i18n("Makes a copy of the selected account. All\n"
99 "settings of the selected account are copied\n"
100 "to a new account that you can modify to fit your\n"
101 "needs"));
102
103 delete_b = new QPushButton(i18n("De&lete"), parent);
104 connect(delete_b, SIGNAL(clicked()), SLOT(deletemodem()));
105 l111->addWidget(delete_b);
106 delete_b->setWhatsThis(
107 i18n("<p>Deletes the selected account\n\n"
108 "<font color=\"red\"><b>Use with care!</b></font>"));
109
110 //load up account list from gppdata to the list box
111 // but keep the current one selected in gpppdata
112 if(gpppdata.modemCount() > 0) {
113 const QString currentmodem = gpppdata.modname();
114 for(int i=0; i <= gpppdata.modemCount()-1; i++) {
115 gpppdata.setModemByIndex(i);
116 modemlist_l->insertItem(gpppdata.modname());
117 }
118 gpppdata.setModem(currentmodem);
119 }
120
121 slotListBoxSelect(modemlist_l->currentItem());
122
123 l1->activate();
124}
125
126
127
128void ModemsWidget::slotListBoxSelect(int idx) {
129 delete_b->setEnabled((bool)(idx != -1));
130 edit_b->setEnabled((bool)(idx != -1));
131 copy_b->setEnabled((bool)(idx != -1));
132 if(idx!=-1) {
133 QString modem = gpppdata.modname();
134 gpppdata.setModemByIndex(modemlist_l->currentItem());
135 gpppdata.setModem(modem);
136 }
137}
138
139void ModemsWidget::editmodem() {
140 gpppdata.setModem(modemlist_l->text(modemlist_l->currentItem()));
141
142 int result = doTab();
143
144 if(result == QDialog::Accepted) {
145 modemlist_l->changeItem(gpppdata.modname(),modemlist_l->currentItem());
146 emit resetmodems();
147 gpppdata.save();
148 }
149}
150
151
152void ModemsWidget::newmodem() {
153 if(modemlist_l->count() == MAX_MODEMS) {
154 KMessageBox::sorry(this, i18n("Maximum number of modems reached."));
155 return;
156 }
157
158 int result;
159
160 if (gpppdata.newmodem() == -1)
161 return;
162 result = doTab();
163
164
165 if(result == QDialog::Accepted) {
166 modemlist_l->insertItem(gpppdata.modname());
167 modemlist_l->setSelected(modemlist_l->findItem(gpppdata.modname()),
168 true);
169 emit resetmodems();
170 gpppdata.save();
171 } else
172 gpppdata.deleteModem();
173}
174
175
176void ModemsWidget::copymodem() {
177 if(modemlist_l->count() == MAX_MODEMS) {
178 KMessageBox::sorry(this, i18n("Maximum number of modems reached."));
179 return;
180 }
181
182 if(modemlist_l->currentItem()<0) {
183 KMessageBox::sorry(this, i18n("No modem selected."));
184 return;
185 }
186
187 gpppdata.copymodem(modemlist_l->currentItem());
188
189 modemlist_l->insertItem(gpppdata.modname());
190 emit resetmodems();
191 gpppdata.save();
192}
193
194
195void ModemsWidget::deletemodem() {
196
197 QString s = i18n("Are you sure you want to delete\nthe modem \"%1\"?",
198 modemlist_l->text(modemlist_l->currentItem()));
199
200 if(KMessageBox::warningContinueCancel(this, s, i18n("Confirm"), KStandardGuiItem::del()) != KMessageBox::Continue)
201 return;
202
203 if(gpppdata.deleteModem(modemlist_l->text(modemlist_l->currentItem())))
204 modemlist_l->removeItem(modemlist_l->currentItem());
205
206 emit resetmodems();
207 gpppdata.save();
208
209 slotListBoxSelect(modemlist_l->currentItem());
210
211}
212
213
214int ModemsWidget::doTab(){
215 tabWindow = new KPageDialog( this );
216 tabWindow->setModal( true );
217 tabWindow->setButtons( KDialog::Ok|KDialog::Cancel );
218 tabWindow->setDefaultButton( KDialog::Ok );
219 tabWindow->setFaceType( KPageDialog::Tabbed );
220 KWindowSystem::setIcons(tabWindow->winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
221 bool isnewmodem;
222
223 if(gpppdata.modname().isEmpty()) {
224 tabWindow->setCaption(i18n("New Modem"));
225 isnewmodem = true;
226 } else {
227 QString tit = i18n("Edit Modem: ");
228 tit += gpppdata.modname();
229 tabWindow->setCaption(tit);
230 isnewmodem = false;
231 }
232 QFrame *frame = new QFrame();
233 KPageWidgetItem *pageItem = new KPageWidgetItem( frame, i18n("&Device") );
234 pageItem->setHeader( i18n("Serial Device") );
235 tabWindow->addPage( pageItem );
236 modem1 = new ModemWidget(frame, isnewmodem );
237
238 frame = new QFrame();
239 pageItem = new KPageWidgetItem( frame, i18n("&Modem") );
240 pageItem->setHeader( i18n("Modem Settings") );
241 tabWindow->addPage( pageItem );
242
243 modem2 = new ModemWidget2(frame);
244 connect ( modem1->connectName(), SIGNAL(textChanged(QString)), this, SLOT(modemNameChanged(QString)));
245
246 modemNameChanged(modem1->connectName()->text());
247 int result = 0;
248 bool ok = false;
249 while (!ok){
250
251 result = tabWindow->exec();
252 ok = true;
253
254 if(result == QDialog::Accepted) {
255 if(modem1->save()) {
256 modem2->save();
257 } else {
258 KMessageBox::error(this, i18n( "You must enter a unique\n"
259 "modem name"));
260 ok = false;
261 }
262 }
263 }
264
265 delete tabWindow;
266 return result;
267}
268
269void ModemsWidget::modemNameChanged(const QString & text)
270{
271 tabWindow->enableButtonOk( !text.isEmpty() );
272}
273
274QString ModemsWidget::prettyPrintVolume(unsigned int n) {
275 int idx = 0;
276 const QString quant[] = {i18n("Byte"), i18n("KiB"),
277 i18n("MiB"), i18n("GiB"), QString()};
278
279 float n1 = n;
280 while(n >= 1024 && !quant[idx].isNull()) {
281 idx++;
282 n /= 1024;
283 }
284
285 int i = idx;
286 while(i--)
287 n1 = n1 / 1024.0;
288
289 QString s = KGlobal::locale()->formatNumber( n1, idx==0 ? 0 : 1 );
290 s += ' ' + quant[idx];
291 return s;
292}
293
294#include "modems.moc"
295